octeon_pip.c revision 1.9 1 1.9 jmcneill /* $NetBSD: octeon_pip.c,v 1.9 2020/07/16 11:49:37 jmcneill Exp $ */
2 1.1 hikaru
3 1.1 hikaru /*
4 1.1 hikaru * Copyright (c) 2007 Internet Initiative Japan, Inc.
5 1.1 hikaru * All rights reserved.
6 1.1 hikaru *
7 1.1 hikaru * Redistribution and use in source and binary forms, with or without
8 1.1 hikaru * modification, are permitted provided that the following conditions
9 1.1 hikaru * are met:
10 1.1 hikaru * 1. Redistributions of source code must retain the above copyright
11 1.1 hikaru * notice, this list of conditions and the following disclaimer.
12 1.1 hikaru * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 hikaru * notice, this list of conditions and the following disclaimer in the
14 1.1 hikaru * documentation and/or other materials provided with the distribution.
15 1.1 hikaru *
16 1.1 hikaru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1 hikaru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 hikaru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 hikaru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1 hikaru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 hikaru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 hikaru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 hikaru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 hikaru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 hikaru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 hikaru * SUCH DAMAGE.
27 1.1 hikaru */
28 1.1 hikaru
29 1.1 hikaru #include <sys/cdefs.h>
30 1.9 jmcneill __KERNEL_RCSID(0, "$NetBSD: octeon_pip.c,v 1.9 2020/07/16 11:49:37 jmcneill Exp $");
31 1.1 hikaru
32 1.1 hikaru #include <sys/param.h>
33 1.1 hikaru #include <sys/systm.h>
34 1.1 hikaru #include <sys/malloc.h>
35 1.1 hikaru #include <sys/syslog.h>
36 1.1 hikaru #include <sys/time.h>
37 1.1 hikaru #include <net/if.h>
38 1.8 simonb
39 1.1 hikaru #include <mips/locore.h>
40 1.8 simonb
41 1.1 hikaru #include <mips/cavium/octeonvar.h>
42 1.8 simonb #include <mips/cavium/dev/octeon_gmxreg.h>
43 1.1 hikaru #include <mips/cavium/dev/octeon_pipreg.h>
44 1.1 hikaru #include <mips/cavium/dev/octeon_pipvar.h>
45 1.8 simonb #include <mips/cavium/include/iobusvar.h>
46 1.8 simonb
47 1.9 jmcneill #include <dev/fdt/fdtvar.h>
48 1.8 simonb
49 1.9 jmcneill static int octpip_iobus_match(device_t, struct cfdata *, void *);
50 1.9 jmcneill static void octpip_iobus_attach(device_t, device_t, void *);
51 1.9 jmcneill
52 1.9 jmcneill static int octpip_fdt_match(device_t, struct cfdata *, void *);
53 1.9 jmcneill static void octpip_fdt_attach(device_t, device_t, void *);
54 1.9 jmcneill
55 1.9 jmcneill CFATTACH_DECL_NEW(octpip_iobus, sizeof(struct octpip_softc),
56 1.9 jmcneill octpip_iobus_match, octpip_iobus_attach, NULL, NULL);
57 1.9 jmcneill
58 1.9 jmcneill CFATTACH_DECL_NEW(octpip_fdt, sizeof(struct octpip_softc),
59 1.9 jmcneill octpip_fdt_match, octpip_fdt_attach, NULL, NULL);
60 1.9 jmcneill
61 1.9 jmcneill static const char * compatible[] = {
62 1.9 jmcneill "cavium,octeon-3860-pip",
63 1.9 jmcneill NULL
64 1.9 jmcneill };
65 1.9 jmcneill
66 1.9 jmcneill static const char * pip_interface_compatible[] = {
67 1.9 jmcneill "cavium,octeon-3860-pip-interface",
68 1.9 jmcneill NULL
69 1.9 jmcneill };
70 1.8 simonb
71 1.8 simonb static int
72 1.9 jmcneill octpip_iobus_match(device_t parent, struct cfdata *cf, void *aux)
73 1.8 simonb {
74 1.8 simonb struct iobus_attach_args *aa = aux;
75 1.8 simonb
76 1.8 simonb if (strcmp(cf->cf_name, aa->aa_name) != 0)
77 1.8 simonb return 0;
78 1.8 simonb return 1;
79 1.8 simonb }
80 1.8 simonb
81 1.8 simonb static void
82 1.9 jmcneill octpip_iobus_attach(device_t parent, device_t self, void *aux)
83 1.8 simonb {
84 1.8 simonb struct octpip_softc *sc = device_private(self);
85 1.8 simonb struct iobus_attach_args *aa = aux;
86 1.8 simonb struct iobus_attach_args gmxaa;
87 1.8 simonb struct iobus_unit gmxiu;
88 1.8 simonb int i, ndevs;
89 1.8 simonb
90 1.8 simonb sc->sc_dev = self;
91 1.8 simonb
92 1.8 simonb aprint_normal("\n");
93 1.8 simonb
94 1.8 simonb /*
95 1.8 simonb * XXX: In a non-FDT world, should allow for the configuration
96 1.8 simonb * of multple GMX devices.
97 1.8 simonb */
98 1.8 simonb ndevs = 1;
99 1.8 simonb
100 1.8 simonb for (i = 0; i < ndevs; i++) {
101 1.8 simonb memcpy(&gmxaa, aa, sizeof(gmxaa));
102 1.8 simonb memset(&gmxiu, 0, sizeof(gmxiu));
103 1.8 simonb
104 1.8 simonb gmxaa.aa_name = "octgmx";
105 1.8 simonb gmxaa.aa_unitno = i;
106 1.8 simonb gmxaa.aa_unit = &gmxiu;
107 1.8 simonb gmxaa.aa_bust = aa->aa_bust;
108 1.8 simonb gmxaa.aa_dmat = aa->aa_dmat;
109 1.8 simonb
110 1.8 simonb if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) == MIPS_CN68XX)
111 1.8 simonb gmxiu.addr = GMX_CN68XX_BASE_PORT(i, 0);
112 1.8 simonb else
113 1.8 simonb gmxiu.addr = GMX_BASE_PORT(i, 0);
114 1.8 simonb
115 1.8 simonb config_found(self, &gmxaa, NULL);
116 1.8 simonb }
117 1.8 simonb }
118 1.1 hikaru
119 1.9 jmcneill static int
120 1.9 jmcneill octpip_fdt_match(device_t parent, struct cfdata *cf, void *aux)
121 1.9 jmcneill {
122 1.9 jmcneill struct fdt_attach_args * const faa = aux;
123 1.9 jmcneill
124 1.9 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
125 1.9 jmcneill }
126 1.9 jmcneill
127 1.9 jmcneill static void
128 1.9 jmcneill octpip_fdt_attach(device_t parent, device_t self, void *aux)
129 1.9 jmcneill {
130 1.9 jmcneill struct octpip_softc *sc = device_private(self);
131 1.9 jmcneill struct fdt_attach_args * const faa = aux;
132 1.9 jmcneill const int phandle = faa->faa_phandle;
133 1.9 jmcneill struct iobus_attach_args gmxaa;
134 1.9 jmcneill struct iobus_unit gmxiu;
135 1.9 jmcneill bus_addr_t intno;
136 1.9 jmcneill int child;
137 1.9 jmcneill
138 1.9 jmcneill sc->sc_dev = self;
139 1.9 jmcneill
140 1.9 jmcneill aprint_normal("\n");
141 1.9 jmcneill
142 1.9 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
143 1.9 jmcneill if (!of_match_compatible(child, pip_interface_compatible))
144 1.9 jmcneill continue;
145 1.9 jmcneill
146 1.9 jmcneill if (fdtbus_get_reg(child, 0, &intno, NULL) != 0) {
147 1.9 jmcneill aprint_error_dev(self, "couldn't get interface number for %s\n",
148 1.9 jmcneill fdtbus_get_string(child, "name"));
149 1.9 jmcneill continue;
150 1.9 jmcneill }
151 1.9 jmcneill
152 1.9 jmcneill memset(&gmxaa, 0, sizeof(gmxaa));
153 1.9 jmcneill memset(&gmxiu, 0, sizeof(gmxiu));
154 1.9 jmcneill
155 1.9 jmcneill gmxaa.aa_name = "octgmx";
156 1.9 jmcneill gmxaa.aa_unitno = (int)intno;
157 1.9 jmcneill gmxaa.aa_unit = &gmxiu;
158 1.9 jmcneill gmxaa.aa_bust = faa->faa_bst;
159 1.9 jmcneill gmxaa.aa_dmat = faa->faa_dmat;
160 1.9 jmcneill
161 1.9 jmcneill if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) == MIPS_CN68XX)
162 1.9 jmcneill gmxiu.addr = GMX_CN68XX_BASE_PORT(intno, 0);
163 1.9 jmcneill else
164 1.9 jmcneill gmxiu.addr = GMX_BASE_PORT(intno, 0);
165 1.9 jmcneill
166 1.9 jmcneill config_found(self, &gmxaa, NULL);
167 1.9 jmcneill
168 1.9 jmcneill /* XXX only one interface supported by octgmx */
169 1.9 jmcneill return;
170 1.9 jmcneill }
171 1.9 jmcneill }
172 1.9 jmcneill
173 1.1 hikaru /* XXX */
174 1.1 hikaru void
175 1.4 simonb octpip_init(struct octpip_attach_args *aa, struct octpip_softc **rsc)
176 1.1 hikaru {
177 1.4 simonb struct octpip_softc *sc;
178 1.1 hikaru int status;
179 1.1 hikaru
180 1.1 hikaru sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
181 1.1 hikaru if (sc == NULL)
182 1.1 hikaru panic("can't allocate memory: %s", __func__);
183 1.1 hikaru
184 1.1 hikaru sc->sc_port = aa->aa_port;
185 1.1 hikaru sc->sc_regt = aa->aa_regt;
186 1.1 hikaru sc->sc_tag_type = aa->aa_tag_type;
187 1.1 hikaru sc->sc_receive_group = aa->aa_receive_group;
188 1.1 hikaru sc->sc_ip_offset = aa->aa_ip_offset;
189 1.1 hikaru
190 1.1 hikaru status = bus_space_map(sc->sc_regt, PIP_BASE, PIP_SIZE, 0,
191 1.1 hikaru &sc->sc_regh);
192 1.1 hikaru if (status != 0)
193 1.1 hikaru panic("can't map %s space", "pip register");
194 1.1 hikaru
195 1.1 hikaru *rsc = sc;
196 1.1 hikaru }
197 1.1 hikaru
198 1.1 hikaru #define _PIP_RD8(sc, off) \
199 1.1 hikaru bus_space_read_8((sc)->sc_regt, (sc)->sc_regh, (off))
200 1.1 hikaru #define _PIP_WR8(sc, off, v) \
201 1.1 hikaru bus_space_write_8((sc)->sc_regt, (sc)->sc_regh, (off), (v))
202 1.1 hikaru
203 1.1 hikaru int
204 1.4 simonb octpip_port_config(struct octpip_softc *sc)
205 1.1 hikaru {
206 1.1 hikaru uint64_t prt_cfg;
207 1.1 hikaru uint64_t prt_tag;
208 1.1 hikaru uint64_t ip_offset;
209 1.1 hikaru
210 1.1 hikaru /*
211 1.1 hikaru * Process the headers and place the IP header in the work queue
212 1.1 hikaru */
213 1.1 hikaru prt_cfg = 0;
214 1.1 hikaru if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) == MIPS_CN50XX) {
215 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_LENERR_EN);
216 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_MAXERR_EN);
217 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_MINERR_EN);
218 1.1 hikaru }
219 1.1 hikaru /* RAWDRP=0; don't allow raw packet drop */
220 1.1 hikaru /* TAGINC=0 */
221 1.8 simonb /* DYN_RS=0; disable dynamic short buffering */
222 1.1 hikaru /* INST_HDR=0 */
223 1.1 hikaru /* GRP_WAT=0 */
224 1.8 simonb SET(prt_cfg, __SHIFTIN(sc->sc_port, PIP_PRT_CFGN_QOS));
225 1.1 hikaru /* QOS_WAT=0 */
226 1.1 hikaru /* SPARE=0 */
227 1.1 hikaru /* QOS_DIFF=0 */
228 1.1 hikaru /* QOS_VLAN=0 */
229 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_CRC_EN);
230 1.1 hikaru /* SKIP=0 */
231 1.1 hikaru
232 1.1 hikaru prt_tag = 0;
233 1.1 hikaru SET(prt_tag, PIP_PRT_TAGN_INC_PRT);
234 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_DPRT);
235 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_DPRT);
236 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_SPRT);
237 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_SPRT);
238 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_NXTH);
239 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_PCTL);
240 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_DST);
241 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_SRC);
242 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_SRC);
243 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_DST);
244 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP6_TAG_ORDERED, PIP_PRT_TAGN_TCP6_TAG));
245 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP4_TAG_ORDERED, PIP_PRT_TAGN_TCP4_TAG));
246 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP6_TAG_ORDERED, PIP_PRT_TAGN_IP6_TAG));
247 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP4_TAG_ORDERED, PIP_PRT_TAGN_IP4_TAG));
248 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_NON_TAG_ORDERED, PIP_PRT_TAGN_NON_TAG));
249 1.1 hikaru SET(prt_tag, sc->sc_receive_group & PIP_PRT_TAGN_GRP);
250 1.1 hikaru
251 1.1 hikaru ip_offset = 0;
252 1.1 hikaru SET(ip_offset, (sc->sc_ip_offset / 8) & PIP_IP_OFFSET_MASK_OFFSET);
253 1.1 hikaru
254 1.1 hikaru _PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), prt_cfg);
255 1.1 hikaru _PIP_WR8(sc, PIP_PRT_TAG0_OFFSET + (8 * sc->sc_port), prt_tag);
256 1.1 hikaru _PIP_WR8(sc, PIP_IP_OFFSET_OFFSET, ip_offset);
257 1.1 hikaru
258 1.1 hikaru return 0;
259 1.1 hikaru }
260 1.1 hikaru
261 1.1 hikaru void
262 1.4 simonb octpip_prt_cfg_enable(struct octpip_softc *sc, uint64_t prt_cfg, int enable)
263 1.1 hikaru {
264 1.1 hikaru uint64_t tmp;
265 1.1 hikaru
266 1.1 hikaru tmp = _PIP_RD8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port));
267 1.1 hikaru if (enable)
268 1.1 hikaru tmp |= prt_cfg;
269 1.1 hikaru else
270 1.1 hikaru tmp &= ~prt_cfg;
271 1.1 hikaru _PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), tmp);
272 1.1 hikaru }
273 1.1 hikaru
274 1.1 hikaru void
275 1.4 simonb octpip_stats(struct octpip_softc *sc, struct ifnet *ifp, int gmx_port)
276 1.1 hikaru {
277 1.1 hikaru uint64_t tmp, pkts;
278 1.1 hikaru uint64_t pip_stat_ctl;
279 1.1 hikaru
280 1.1 hikaru if (sc == NULL || ifp == NULL)
281 1.1 hikaru panic("%s: invalid argument. sc=%p, ifp=%p\n", __func__,
282 1.1 hikaru sc, ifp);
283 1.1 hikaru
284 1.8 simonb if (gmx_port < 0 || gmx_port > GMX_PORT_NUNITS) {
285 1.1 hikaru printf("%s: invalid gmx_port %d\n", __func__, gmx_port);
286 1.1 hikaru return;
287 1.1 hikaru }
288 1.1 hikaru
289 1.1 hikaru pip_stat_ctl = _PIP_RD8(sc, PIP_STAT_CTL_OFFSET);
290 1.1 hikaru _PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl | PIP_STAT_CTL_RDCLR);
291 1.7 simonb tmp = _PIP_RD8(sc, PIP_STAT0_PRT_OFFSET(gmx_port));
292 1.5 simonb pkts = __SHIFTOUT(tmp, PIP_STAT0_PRTN_DRP_PKTS);
293 1.3 thorpej if_statadd(ifp, if_iqdrops, pkts);
294 1.1 hikaru
295 1.1 hikaru _PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl);
296 1.1 hikaru }
297