ixp425_if_npe.c revision 1.52 1 /* $NetBSD: ixp425_if_npe.c,v 1.52 2022/09/18 15:49:42 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Sam Leffler. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 #if 0
29 __FBSDID("$FreeBSD: src/sys/arm/xscale/ixp425/if_npe.c,v 1.1 2006/11/19 23:55:23 sam Exp $");
30 #endif
31 __KERNEL_RCSID(0, "$NetBSD: ixp425_if_npe.c,v 1.52 2022/09/18 15:49:42 thorpej Exp $");
32
33 /*
34 * Intel XScale NPE Ethernet driver.
35 *
36 * This driver handles the two ports present on the IXP425.
37 * Packet processing is done by the Network Processing Engines
38 * (NPE's) that work together with a MAC and PHY. The MAC
39 * is also mapped to the XScale cpu; the PHY is accessed via
40 * the MAC. NPE-XScale communication happens through h/w
41 * queues managed by the Q Manager block.
42 *
43 * The code here replaces the ethAcc, ethMii, and ethDB classes
44 * in the Intel Access Library (IAL) and the OS-specific driver.
45 *
46 * XXX add vlan support
47 * XXX NPE-C port doesn't work yet
48 */
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/callout.h>
55 #include <sys/mbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/socket.h>
58 #include <sys/endian.h>
59 #include <sys/ioctl.h>
60 #include <sys/syslog.h>
61 #include <sys/bus.h>
62 #include <sys/rndsource.h>
63
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_ether.h>
68 #include <net/bpf.h>
69
70 #include <arm/xscale/ixp425reg.h>
71 #include <arm/xscale/ixp425var.h>
72 #include <arm/xscale/ixp425_qmgr.h>
73 #include <arm/xscale/ixp425_npevar.h>
74 #include <arm/xscale/ixp425_if_npereg.h>
75
76 #include <dev/mii/miivar.h>
77
78 #include "locators.h"
79
80 struct npebuf {
81 struct npebuf *ix_next; /* chain to next buffer */
82 void *ix_m; /* backpointer to mbuf */
83 bus_dmamap_t ix_map; /* bus dma map for associated data */
84 struct npehwbuf *ix_hw; /* associated h/w block */
85 uint32_t ix_neaddr; /* phys address of ix_hw */
86 };
87
88 struct npedma {
89 const char* name;
90 int nbuf; /* # npebuf's allocated */
91 bus_dmamap_t m_map;
92 struct npehwbuf *hwbuf; /* NPE h/w buffers */
93 bus_dmamap_t buf_map;
94 bus_addr_t buf_phys; /* phys addr of buffers */
95 struct npebuf *buf; /* s/w buffers (1-1 w/ h/w) */
96 };
97
98 struct npe_softc {
99 device_t sc_dev;
100 struct ethercom sc_ethercom;
101 uint8_t sc_enaddr[ETHER_ADDR_LEN];
102 struct mii_data sc_mii;
103 bus_space_tag_t sc_iot;
104 bus_dma_tag_t sc_dt;
105 bus_space_handle_t sc_ioh; /* MAC register window */
106 bus_space_handle_t sc_miih; /* MII register window */
107 struct ixpnpe_softc *sc_npe; /* NPE support */
108 int sc_unit;
109 int sc_phy;
110 struct callout sc_tick_ch; /* Tick callout */
111 struct npedma txdma;
112 struct npebuf *tx_free; /* list of free tx buffers */
113 struct npedma rxdma;
114 int rx_qid; /* rx qid */
115 int rx_freeqid; /* rx free buffers qid */
116 int tx_qid; /* tx qid */
117 int tx_doneqid; /* tx completed qid */
118 struct npestats *sc_stats;
119 bus_dmamap_t sc_stats_map;
120 bus_addr_t sc_stats_phys; /* phys addr of sc_stats */
121 u_short sc_if_flags; /* keep last if_flags */
122 krndsource_t rnd_source; /* random source */
123 };
124
125 /*
126 * Per-unit static configuration for IXP425. The tx and
127 * rx free Q id's are fixed by the NPE microcode. The
128 * rx Q id's are programmed to be separate to simplify
129 * multi-port processing. It may be better to handle
130 * all traffic through one Q (as done by the Intel drivers).
131 *
132 * Note that the PHY's are accessible only from MAC A
133 * on the IXP425. This and other platform-specific
134 * assumptions probably need to be handled through hints.
135 */
136 static const struct {
137 const char *desc; /* device description */
138 int npeid; /* NPE assignment */
139 int macport; /* Port number of the MAC */
140 uint32_t imageid; /* NPE firmware image id */
141 uint32_t regbase;
142 int regsize;
143 uint32_t miibase;
144 int miisize;
145 uint8_t rx_qid;
146 uint8_t rx_freeqid;
147 uint8_t tx_qid;
148 uint8_t tx_doneqid;
149 } npeconfig[NPE_PORTS_MAX] = {
150 { .desc = "IXP NPE-B",
151 .npeid = NPE_B,
152 .macport = 0x10,
153 .imageid = IXP425_NPE_B_IMAGEID,
154 .regbase = IXP425_MAC_A_HWBASE,
155 .regsize = IXP425_MAC_A_SIZE,
156 .miibase = IXP425_MAC_A_HWBASE,
157 .miisize = IXP425_MAC_A_SIZE,
158 .rx_qid = 4,
159 .rx_freeqid = 27,
160 .tx_qid = 24,
161 .tx_doneqid = 31
162 },
163 { .desc = "IXP NPE-C",
164 .npeid = NPE_C,
165 .macport = 0x20,
166 .imageid = IXP425_NPE_C_IMAGEID,
167 .regbase = IXP425_MAC_B_HWBASE,
168 .regsize = IXP425_MAC_B_SIZE,
169 .miibase = IXP425_MAC_A_HWBASE,
170 .miisize = IXP425_MAC_A_SIZE,
171 .rx_qid = 12,
172 .rx_freeqid = 28,
173 .tx_qid = 25,
174 .tx_doneqid = 31
175 },
176 };
177 static struct npe_softc *npes[NPE_MAX]; /* NB: indexed by npeid */
178
179 static __inline uint32_t
180 RD4(struct npe_softc *sc, bus_size_t off)
181 {
182 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, off);
183 }
184
185 static __inline void
186 WR4(struct npe_softc *sc, bus_size_t off, uint32_t val)
187 {
188 bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
189 }
190
191 static int npe_activate(struct npe_softc *);
192 #if 0
193 static void npe_deactivate(struct npe_softc *);
194 #endif
195 static void npe_setmac(struct npe_softc *, const u_char *);
196 static void npe_getmac(struct npe_softc *);
197 static void npe_txdone(int, void *);
198 static int npe_rxbuf_init(struct npe_softc *, struct npebuf *,
199 struct mbuf *);
200 static void npe_rxdone(int, void *);
201 static void npeinit_macreg(struct npe_softc *);
202 static int npeinit(struct ifnet *);
203 static void npeinit_resetcb(void *);
204 static void npeinit_locked(void *);
205 static void npestart(struct ifnet *);
206 static void npestop(struct ifnet *, int);
207 static void npewatchdog(struct ifnet *);
208 static int npeioctl(struct ifnet *, u_long, void *);
209
210 static int npe_setrxqosentry(struct npe_softc *, int, int, int);
211 static int npe_updatestats(struct npe_softc *);
212 #if 0
213 static int npe_getstats(struct npe_softc *);
214 static uint32_t npe_getimageid(struct npe_softc *);
215 static int npe_setloopback(struct npe_softc *, int);
216 #endif
217
218 static int npe_miibus_readreg(device_t, int, int, uint16_t *);
219 static int npe_miibus_writereg(device_t, int, int, uint16_t);
220 static void npe_miibus_statchg(struct ifnet *);
221
222 static int npe_debug;
223 #define DPRINTF(sc, fmt, ...) do { \
224 if (npe_debug) printf(fmt, __VA_ARGS__); \
225 } while (0)
226 #define DPRINTFn(n, sc, fmt, ...) do { \
227 if (npe_debug >= n) printf(fmt, __VA_ARGS__); \
228 } while (0)
229
230 #define NPE_TXBUF 128
231 #define NPE_RXBUF 64
232
233 #define MAC2UINT64(addr) (((uint64_t)addr[0] << 40) \
234 + ((uint64_t)addr[1] << 32) \
235 + ((uint64_t)addr[2] << 24) \
236 + ((uint64_t)addr[3] << 16) \
237 + ((uint64_t)addr[4] << 8) \
238 + (uint64_t)addr[5])
239
240 /* NB: all tx done processing goes through one queue */
241 static int tx_doneqid = -1;
242
243 void (*npe_getmac_md)(int, uint8_t *);
244
245 static int npe_match(device_t, cfdata_t, void *);
246 static void npe_attach(device_t, device_t, void *);
247
248 CFATTACH_DECL_NEW(npe, sizeof(struct npe_softc),
249 npe_match, npe_attach, NULL, NULL);
250
251 static int
252 npe_match(device_t parent, cfdata_t cf, void *arg)
253 {
254 struct ixpnpe_attach_args *na = arg;
255
256 return (na->na_unit == NPE_B || na->na_unit == NPE_C);
257 }
258
259 static void
260 npe_attach(device_t parent, device_t self, void *arg)
261 {
262 struct npe_softc *sc = device_private(self);
263 struct ixpnpe_softc *isc = device_private(parent);
264 struct ixpnpe_attach_args *na = arg;
265 struct ifnet *ifp;
266 struct mii_data * const mii = &sc->sc_mii;
267
268 aprint_naive("\n");
269 aprint_normal(": Ethernet co-processor\n");
270
271 sc->sc_dev = self;
272 sc->sc_iot = na->na_iot;
273 sc->sc_dt = na->na_dt;
274 sc->sc_npe = na->na_npe;
275 sc->sc_unit = (na->na_unit == NPE_B) ? 0 : 1;
276 sc->sc_phy = na->na_phy;
277
278 memset(&sc->sc_ethercom, 0, sizeof(sc->sc_ethercom));
279 memset(mii, 0, sizeof(*mii));
280
281 callout_init(&sc->sc_tick_ch, 0);
282
283 if (npe_activate(sc)) {
284 aprint_error_dev(sc->sc_dev,
285 "Failed to activate NPE (missing microcode?)\n");
286 return;
287 }
288
289 npe_getmac(sc);
290 npeinit_macreg(sc);
291
292 aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
293 ether_sprintf(sc->sc_enaddr));
294
295 ifp = &sc->sc_ethercom.ec_if;
296 mii->mii_ifp = ifp;
297 mii->mii_readreg = npe_miibus_readreg;
298 mii->mii_writereg = npe_miibus_writereg;
299 mii->mii_statchg = npe_miibus_statchg;
300 sc->sc_ethercom.ec_mii = mii;
301
302 ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
303 ether_mediastatus);
304
305 mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
306 MII_OFFSET_ANY, MIIF_DOPAUSE);
307 if (LIST_FIRST(&mii->mii_phys) == NULL) {
308 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
309 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
310 } else
311 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
312
313 ifp->if_softc = sc;
314 strcpy(ifp->if_xname, device_xname(sc->sc_dev));
315 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
316 ifp->if_start = npestart;
317 ifp->if_ioctl = npeioctl;
318 ifp->if_watchdog = npewatchdog;
319 ifp->if_init = npeinit;
320 ifp->if_stop = npestop;
321 IFQ_SET_READY(&ifp->if_snd);
322
323 /* VLAN capable */
324 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
325
326 if_attach(ifp);
327 if_deferred_start_init(ifp, NULL);
328 ether_ifattach(ifp, sc->sc_enaddr);
329 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
330 RND_TYPE_NET, RND_FLAG_DEFAULT);
331
332 /* callback function to reset MAC */
333 isc->macresetcbfunc = npeinit_resetcb;
334 isc->macresetcbarg = sc;
335 }
336
337 /*
338 * Compute and install the multicast filter.
339 */
340 static void
341 npe_setmcast(struct npe_softc *sc)
342 {
343 struct ethercom *ec = &sc->sc_ethercom;
344 struct ifnet *ifp = &ec->ec_if;
345 uint8_t mask[ETHER_ADDR_LEN], addr[ETHER_ADDR_LEN];
346 uint32_t reg;
347 uint32_t msg[2];
348 int i;
349
350 /* Always use filter. Is here a correct position? */
351 reg = RD4(sc, NPE_MAC_RX_CNTRL1);
352 WR4(sc, NPE_MAC_RX_CNTRL1, reg | NPE_RX_CNTRL1_ADDR_FLTR_EN);
353
354 if (ifp->if_flags & IFF_PROMISC) {
355 memset(mask, 0, ETHER_ADDR_LEN);
356 memset(addr, 0, ETHER_ADDR_LEN);
357 } else if (ifp->if_flags & IFF_ALLMULTI) {
358 static const uint8_t allmulti[ETHER_ADDR_LEN] =
359 { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
360 all_multi:
361 memcpy(mask, allmulti, ETHER_ADDR_LEN);
362 memcpy(addr, allmulti, ETHER_ADDR_LEN);
363 } else {
364 uint8_t clr[ETHER_ADDR_LEN], set[ETHER_ADDR_LEN];
365 struct ether_multistep step;
366 struct ether_multi *enm;
367
368 memset(clr, 0, ETHER_ADDR_LEN);
369 memset(set, 0xff, ETHER_ADDR_LEN);
370
371 ETHER_LOCK(ec);
372 ETHER_FIRST_MULTI(step, ec, enm);
373 while (enm != NULL) {
374 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
375 ETHER_ADDR_LEN)) {
376 ifp->if_flags |= IFF_ALLMULTI;
377 ETHER_UNLOCK(ec);
378 goto all_multi;
379 }
380
381 for (i = 0; i < ETHER_ADDR_LEN; i++) {
382 clr[i] |= enm->enm_addrlo[i];
383 set[i] &= enm->enm_addrlo[i];
384 }
385
386 ETHER_NEXT_MULTI(step, enm);
387 }
388 ETHER_UNLOCK(ec);
389
390 for (i = 0; i < ETHER_ADDR_LEN; i++) {
391 mask[i] = set[i] | ~clr[i];
392 addr[i] = set[i];
393 }
394 }
395
396 /*
397 * Write the mask and address registers.
398 */
399 for (i = 0; i < ETHER_ADDR_LEN; i++) {
400 WR4(sc, NPE_MAC_ADDR_MASK(i), mask[i]);
401 WR4(sc, NPE_MAC_ADDR(i), addr[i]);
402 }
403
404 msg[0] = NPE_ADDRESSFILTERCONFIG << NPE_MAC_MSGID_SHL
405 | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
406 msg[1] = ((ifp->if_flags & IFF_PROMISC) ? 1 : 0) << 24
407 | ((RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff) << 16)
408 | (addr[5] << 8) | mask[5];
409 ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
410 }
411
412 static int
413 npe_dma_setup(struct npe_softc *sc, struct npedma *dma,
414 const char *name, int nbuf, int maxseg)
415 {
416 bus_dma_segment_t seg;
417 int rseg, error, i;
418 void *hwbuf;
419 size_t size;
420
421 memset(dma, 0, sizeof(*dma));
422
423 dma->name = name;
424 dma->nbuf = nbuf;
425
426 size = nbuf * sizeof(struct npehwbuf);
427
428 /* XXX COHERENT for now */
429 error = bus_dmamem_alloc(sc->sc_dt, size, sizeof(uint32_t), 0, &seg,
430 1, &rseg, BUS_DMA_NOWAIT);
431 if (error) {
432 aprint_error_dev(sc->sc_dev,
433 "unable to %s for %s %s buffers, error %u\n",
434 "allocate memory", dma->name, "h/w", error);
435 }
436
437 error = bus_dmamem_map(sc->sc_dt, &seg, 1, size, &hwbuf,
438 BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
439 if (error) {
440 aprint_error_dev(sc->sc_dev,
441 "unable to %s for %s %s buffers, error %u\n",
442 "map memory", dma->name, "h/w", error);
443 free_dmamem:
444 bus_dmamem_free(sc->sc_dt, &seg, rseg);
445 return error;
446 }
447 dma->hwbuf = (void *)hwbuf;
448
449 error = bus_dmamap_create(sc->sc_dt, size, 1, size, 0,
450 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &dma->buf_map);
451 if (error) {
452 aprint_error_dev(sc->sc_dev,
453 "unable to %s for %s %s buffers, error %u\n",
454 "create map", dma->name, "h/w", error);
455 unmap_dmamem:
456 dma->hwbuf = NULL;
457 bus_dmamem_unmap(sc->sc_dt, hwbuf, size);
458 goto free_dmamem;
459 }
460
461 error = bus_dmamap_load(sc->sc_dt, dma->buf_map, hwbuf, size, NULL,
462 BUS_DMA_NOWAIT);
463 if (error) {
464 aprint_error_dev(sc->sc_dev,
465 "unable to %s for %s %s buffers, error %u\n",
466 "load map", dma->name, "h/w", error);
467 bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
468 goto unmap_dmamem;
469 }
470
471 /* XXX M_TEMP */
472 dma->buf = malloc(nbuf * sizeof(struct npebuf), M_TEMP,
473 M_WAITOK | M_ZERO);
474 dma->buf_phys = dma->buf_map->dm_segs[0].ds_addr;
475 for (i = 0; i < dma->nbuf; i++) {
476 struct npebuf *npe = &dma->buf[i];
477 struct npehwbuf *hw = &dma->hwbuf[i];
478
479 /* Calculate offset to shared area */
480 npe->ix_neaddr = dma->buf_phys +
481 ((uintptr_t)hw - (uintptr_t)dma->hwbuf);
482 KASSERT((npe->ix_neaddr & 0x1f) == 0);
483 error = bus_dmamap_create(sc->sc_dt, MCLBYTES, maxseg,
484 MCLBYTES, 0, 0, &npe->ix_map);
485 if (error != 0) {
486 aprint_error_dev(sc->sc_dev,
487 "unable to %s for %s buffer %u, error %u\n",
488 "create dmamap", dma->name, i, error);
489 /* XXXSCW: Free up maps... */
490 return error;
491 }
492 npe->ix_hw = hw;
493 }
494 bus_dmamap_sync(sc->sc_dt, dma->buf_map, 0, dma->buf_map->dm_mapsize,
495 BUS_DMASYNC_PREWRITE);
496 return 0;
497 }
498
499 #if 0
500 static void
501 npe_dma_destroy(struct npe_softc *sc, struct npedma *dma)
502 {
503 int i;
504
505 /* XXXSCW: Clean this up */
506
507 if (dma->hwbuf != NULL) {
508 for (i = 0; i < dma->nbuf; i++) {
509 struct npebuf *npe = &dma->buf[i];
510 bus_dmamap_destroy(sc->sc_dt, npe->ix_map);
511 }
512 bus_dmamap_unload(sc->sc_dt, dma->buf_map);
513 bus_dmamem_free(sc->sc_dt, (void *)dma->hwbuf, dma->buf_map);
514 bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
515 }
516 if (dma->buf != NULL)
517 free(dma->buf, M_TEMP);
518 memset(dma, 0, sizeof(*dma));
519 }
520 #endif
521
522 static int
523 npe_activate(struct npe_softc *sc)
524 {
525 bus_dma_segment_t seg;
526 int unit = sc->sc_unit;
527 int error, i, rseg;
528 void *statbuf;
529
530 /* load NPE firmware and start it running */
531 error = ixpnpe_init(sc->sc_npe, "npe_fw", npeconfig[unit].imageid);
532 if (error != 0)
533 return error;
534
535 if (bus_space_map(sc->sc_iot, npeconfig[unit].regbase,
536 npeconfig[unit].regsize, 0, &sc->sc_ioh)) {
537 aprint_error_dev(sc->sc_dev, "Cannot map registers 0x%x:0x%x\n",
538 npeconfig[unit].regbase, npeconfig[unit].regsize);
539 return ENOMEM;
540 }
541
542 if (npeconfig[unit].miibase != npeconfig[unit].regbase) {
543 /*
544 * The PHY's are only accessible from one MAC (it appears)
545 * so for other MAC's setup an additional mapping for
546 * frobbing the PHY registers.
547 */
548 if (bus_space_map(sc->sc_iot, npeconfig[unit].miibase,
549 npeconfig[unit].miisize, 0, &sc->sc_miih)) {
550 aprint_error_dev(sc->sc_dev,
551 "Cannot map MII registers 0x%x:0x%x\n",
552 npeconfig[unit].miibase, npeconfig[unit].miisize);
553 return ENOMEM;
554 }
555 } else
556 sc->sc_miih = sc->sc_ioh;
557 error = npe_dma_setup(sc, &sc->txdma, "tx", NPE_TXBUF, NPE_MAXSEG);
558 if (error != 0)
559 return error;
560 error = npe_dma_setup(sc, &sc->rxdma, "rx", NPE_RXBUF, 1);
561 if (error != 0)
562 return error;
563
564 /* setup statistics block */
565 error = bus_dmamem_alloc(sc->sc_dt, sizeof(struct npestats),
566 sizeof(uint32_t), 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
567 if (error) {
568 aprint_error_dev(sc->sc_dev,
569 "unable to %s for %s, error %u\n",
570 "allocate memory", "stats block", error);
571 return error;
572 }
573
574 error = bus_dmamem_map(sc->sc_dt, &seg, 1, sizeof(struct npestats),
575 &statbuf, BUS_DMA_NOWAIT);
576 if (error) {
577 aprint_error_dev(sc->sc_dev,
578 "unable to %s for %s, error %u\n",
579 "map memory", "stats block", error);
580 return error;
581 }
582 sc->sc_stats = (void *)statbuf;
583
584 error = bus_dmamap_create(sc->sc_dt, sizeof(struct npestats), 1,
585 sizeof(struct npestats), 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
586 &sc->sc_stats_map);
587 if (error) {
588 aprint_error_dev(sc->sc_dev,
589 "unable to %s for %s, error %u\n",
590 "create map", "stats block", error);
591 return error;
592 }
593
594 error = bus_dmamap_load(sc->sc_dt, sc->sc_stats_map, sc->sc_stats,
595 sizeof(struct npestats), NULL, BUS_DMA_NOWAIT);
596 if (error) {
597 aprint_error_dev(sc->sc_dev,
598 "unable to %s for %s, error %u\n",
599 "load map", "stats block", error);
600 return error;
601 }
602 sc->sc_stats_phys = sc->sc_stats_map->dm_segs[0].ds_addr;
603
604 /* XXX disable half-bridge LEARNING+FILTERING feature */
605
606 /*
607 * Setup h/w rx/tx queues. There are four q's:
608 * rx inbound q of rx'd frames
609 * rx_free pool of ixpbuf's for receiving frames
610 * tx outbound q of frames to send
611 * tx_done q of tx frames that have been processed
612 *
613 * The NPE handles the actual tx/rx process and the q manager
614 * handles the queues. The driver just writes entries to the
615 * q manager mailbox's and gets callbacks when there are rx'd
616 * frames to process or tx'd frames to reap. These callbacks
617 * are controlled by the q configurations; e.g. we get a
618 * callback when tx_done has 2 or more frames to process and
619 * when the rx q has at least one frame. These settings can
620 * changed at the time the q is configured.
621 */
622 sc->rx_qid = npeconfig[unit].rx_qid;
623 ixpqmgr_qconfig(sc->rx_qid, NPE_RXBUF, 0, 1,
624 IX_QMGR_Q_SOURCE_ID_NOT_E, npe_rxdone, sc);
625 sc->rx_freeqid = npeconfig[unit].rx_freeqid;
626 ixpqmgr_qconfig(sc->rx_freeqid, NPE_RXBUF, 0, NPE_RXBUF/2, 0, NULL, sc);
627 /* tell the NPE to direct all traffic to rx_qid */
628 #if 0
629 for (i = 0; i < 8; i++)
630 #else
631 printf("%s: remember to fix rx q setup\n", device_xname(sc->sc_dev));
632 for (i = 0; i < 4; i++)
633 #endif
634 npe_setrxqosentry(sc, i, 0, sc->rx_qid);
635
636 sc->tx_qid = npeconfig[unit].tx_qid;
637 sc->tx_doneqid = npeconfig[unit].tx_doneqid;
638 ixpqmgr_qconfig(sc->tx_qid, NPE_TXBUF, 0, NPE_TXBUF, 0, NULL, sc);
639 if (tx_doneqid == -1) {
640 ixpqmgr_qconfig(sc->tx_doneqid, NPE_TXBUF, 0, 2,
641 IX_QMGR_Q_SOURCE_ID_NOT_E, npe_txdone, sc);
642 tx_doneqid = sc->tx_doneqid;
643 }
644
645 KASSERT(npes[npeconfig[unit].npeid] == NULL);
646 npes[npeconfig[unit].npeid] = sc;
647
648 return 0;
649 }
650
651 #if 0
652 static void
653 npe_deactivate(struct npe_softc *sc);
654 {
655 int unit = sc->sc_unit;
656
657 npes[npeconfig[unit].npeid] = NULL;
658
659 /* XXX disable q's */
660 if (sc->sc_npe != NULL)
661 ixpnpe_stop(sc->sc_npe);
662 if (sc->sc_stats != NULL) {
663 bus_dmamap_unload(sc->sc_stats_tag, sc->sc_stats_map);
664 bus_dmamem_free(sc->sc_stats_tag, sc->sc_stats,
665 sc->sc_stats_map);
666 bus_dmamap_destroy(sc->sc_stats_tag, sc->sc_stats_map);
667 }
668 if (sc->sc_stats_tag != NULL)
669 bus_dma_tag_destroy(sc->sc_stats_tag);
670 npe_dma_destroy(sc, &sc->txdma);
671 npe_dma_destroy(sc, &sc->rxdma);
672 bus_generic_detach(sc->sc_dev);
673 XXX ifmedia_fini somewhere
674 if (sc->sc_mii)
675 device_delete_child(sc->sc_dev, sc->sc_mii);
676 #if 0
677 /* XXX sc_ioh and sc_miih */
678 if (sc->mem_res)
679 bus_release_resource(dev, SYS_RES_IOPORT,
680 rman_get_rid(sc->mem_res), sc->mem_res);
681 sc->mem_res = 0;
682 #endif
683 }
684 #endif
685
686 static void
687 npe_addstats(struct npe_softc *sc)
688 {
689 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
690 struct npestats *ns = sc->sc_stats;
691
692 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
693 if_statadd_ref(nsr, if_oerrors,
694 be32toh(ns->dot3StatsInternalMacTransmitErrors)
695 + be32toh(ns->dot3StatsCarrierSenseErrors)
696 + be32toh(ns->TxVLANIdFilterDiscards)
697 );
698 if_statadd_ref(nsr, if_ierrors,
699 be32toh(ns->dot3StatsFCSErrors)
700 + be32toh(ns->dot3StatsInternalMacReceiveErrors)
701 + be32toh(ns->RxOverrunDiscards)
702 + be32toh(ns->RxUnderflowEntryDiscards)
703 );
704 if_statadd_ref(nsr, if_collisions,
705 be32toh(ns->dot3StatsSingleCollisionFrames)
706 + be32toh(ns->dot3StatsMultipleCollisionFrames)
707 );
708 IF_STAT_PUTREF(ifp);
709 }
710
711 static void
712 npe_tick(void *xsc)
713 {
714 #define ACK (NPE_RESETSTATS << NPE_MAC_MSGID_SHL)
715 struct npe_softc *sc = xsc;
716 uint32_t msg[2];
717
718 /*
719 * NB: to avoid sleeping with the softc lock held we
720 * split the NPE msg processing into two parts. The
721 * request for statistics is sent w/o waiting for a
722 * reply and then on the next tick we retrieve the
723 * results. This works because npe_tick is the only
724 * code that talks via the mailbox's (except at setup).
725 * This likely can be handled better.
726 */
727 if (ixpnpe_recvmsg(sc->sc_npe, msg) == 0 && msg[0] == ACK) {
728 bus_dmamap_sync(sc->sc_dt, sc->sc_stats_map, 0,
729 sizeof(struct npestats), BUS_DMASYNC_POSTREAD);
730 npe_addstats(sc);
731 }
732 npe_updatestats(sc);
733 mii_tick(&sc->sc_mii);
734
735 /* Schedule next poll */
736 callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
737 #undef ACK
738 }
739
740 static void
741 npe_setmac(struct npe_softc *sc, const u_char *eaddr)
742 {
743
744 WR4(sc, NPE_MAC_UNI_ADDR_1, eaddr[0]);
745 WR4(sc, NPE_MAC_UNI_ADDR_2, eaddr[1]);
746 WR4(sc, NPE_MAC_UNI_ADDR_3, eaddr[2]);
747 WR4(sc, NPE_MAC_UNI_ADDR_4, eaddr[3]);
748 WR4(sc, NPE_MAC_UNI_ADDR_5, eaddr[4]);
749 WR4(sc, NPE_MAC_UNI_ADDR_6, eaddr[5]);
750 }
751
752 static void
753 npe_getmac(struct npe_softc *sc)
754 {
755 uint8_t *eaddr = sc->sc_enaddr;
756
757 if (npe_getmac_md != NULL) {
758 (*npe_getmac_md)(device_unit(sc->sc_dev), eaddr);
759 } else {
760 /*
761 * Some system's unicast address appears to be loaded from
762 * EEPROM on reset
763 */
764 eaddr[0] = RD4(sc, NPE_MAC_UNI_ADDR_1) & 0xff;
765 eaddr[1] = RD4(sc, NPE_MAC_UNI_ADDR_2) & 0xff;
766 eaddr[2] = RD4(sc, NPE_MAC_UNI_ADDR_3) & 0xff;
767 eaddr[3] = RD4(sc, NPE_MAC_UNI_ADDR_4) & 0xff;
768 eaddr[4] = RD4(sc, NPE_MAC_UNI_ADDR_5) & 0xff;
769 eaddr[5] = RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff;
770 }
771 }
772
773 struct txdone {
774 struct npebuf *head;
775 struct npebuf **tail;
776 int count;
777 };
778
779 static __inline void
780 npe_txdone_finish(struct npe_softc *sc, const struct txdone *td)
781 {
782 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
783
784 *td->tail = sc->tx_free;
785 sc->tx_free = td->head;
786 /*
787 * We're no longer busy, so clear the busy flag and call the
788 * start routine to xmit more packets.
789 */
790 if_statadd(ifp, if_opackets, td->count);
791 ifp->if_timer = 0;
792 if_schedule_deferred_start(ifp);
793 }
794
795 /*
796 * Q manager callback on tx done queue. Reap mbufs
797 * and return tx buffers to the free list. Finally
798 * restart output. Note the microcode has only one
799 * txdone q wired into it so we must use the NPE ID
800 * returned with each npehwbuf to decide where to
801 * send buffers.
802 */
803 static void
804 npe_txdone(int qid, void *arg)
805 {
806 #define P2V(a, dma) \
807 &(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
808 struct npe_softc *sc;
809 struct npebuf *npe;
810 struct txdone *td, q[NPE_MAX];
811 uint32_t entry;
812
813 /* XXX no NPE-A support */
814 q[NPE_B].tail = &q[NPE_B].head; q[NPE_B].count = 0;
815 q[NPE_C].tail = &q[NPE_C].head; q[NPE_C].count = 0;
816 /* XXX max # at a time? */
817 while (ixpqmgr_qread(qid, &entry) == 0) {
818 sc = npes[NPE_QM_Q_NPE(entry)];
819 DPRINTF(sc, "%s: entry 0x%x NPE %u port %u\n",
820 __func__, entry, NPE_QM_Q_NPE(entry), NPE_QM_Q_PORT(entry));
821 rnd_add_uint32(&sc->rnd_source, entry);
822
823 npe = P2V(NPE_QM_Q_ADDR(entry), &sc->txdma);
824 m_freem(npe->ix_m);
825 npe->ix_m = NULL;
826
827 td = &q[NPE_QM_Q_NPE(entry)];
828 *td->tail = npe;
829 td->tail = &npe->ix_next;
830 td->count++;
831 }
832
833 if (q[NPE_B].count)
834 npe_txdone_finish(npes[NPE_B], &q[NPE_B]);
835 if (q[NPE_C].count)
836 npe_txdone_finish(npes[NPE_C], &q[NPE_C]);
837 #undef P2V
838 }
839
840 static __inline struct mbuf *
841 npe_getcl(void)
842 {
843 struct mbuf *m;
844
845 MGETHDR(m, M_DONTWAIT, MT_DATA);
846 if (m != NULL) {
847 MCLGET(m, M_DONTWAIT);
848 if ((m->m_flags & M_EXT) == 0) {
849 m_freem(m);
850 m = NULL;
851 }
852 }
853 return m;
854 }
855
856 static int
857 npe_rxbuf_init(struct npe_softc *sc, struct npebuf *npe, struct mbuf *m)
858 {
859 struct npehwbuf *hw;
860 int error;
861
862 if (m == NULL) {
863 m = npe_getcl();
864 if (m == NULL)
865 return ENOBUFS;
866 }
867 KASSERT(m->m_ext.ext_size >= (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
868 m->m_pkthdr.len = m->m_len = NPE_FRAME_SIZE_DEFAULT;
869 /* backload payload and align ip hdr */
870 m->m_data = m->m_ext.ext_buf + (m->m_ext.ext_size
871 - (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
872 error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
873 BUS_DMA_READ | BUS_DMA_NOWAIT);
874 if (error != 0) {
875 m_freem(m);
876 return error;
877 }
878 hw = npe->ix_hw;
879 hw->ix_ne[0].data = htobe32(npe->ix_map->dm_segs[0].ds_addr);
880 /* NB: NPE requires length be a multiple of 64 */
881 /* NB: buffer length is shifted in word */
882 hw->ix_ne[0].len = htobe32(npe->ix_map->dm_segs[0].ds_len << 16);
883 hw->ix_ne[0].next = 0;
884 npe->ix_m = m;
885 /* Flush the memory in the mbuf */
886 bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0, npe->ix_map->dm_mapsize,
887 BUS_DMASYNC_PREREAD);
888 return 0;
889 }
890
891 /*
892 * RX q processing for a specific NPE. Claim entries
893 * from the hardware queue and pass the frames up the
894 * stack. Pass the rx buffers to the free list.
895 */
896 static void
897 npe_rxdone(int qid, void *arg)
898 {
899 #define P2V(a, dma) \
900 &(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
901 struct npe_softc *sc = arg;
902 struct npedma *dma = &sc->rxdma;
903 uint32_t entry;
904
905 while (ixpqmgr_qread(qid, &entry) == 0) {
906 struct npebuf *npe = P2V(NPE_QM_Q_ADDR(entry), dma);
907 struct mbuf *m;
908
909 DPRINTF(sc, "%s: entry 0x%x neaddr 0x%x ne_len 0x%x\n",
910 __func__, entry, npe->ix_neaddr, npe->ix_hw->ix_ne[0].len);
911 rnd_add_uint32(&sc->rnd_source, entry);
912 /*
913 * Allocate a new mbuf to replenish the rx buffer.
914 * If doing so fails we drop the rx'd frame so we
915 * can reuse the previous mbuf. When we're able to
916 * allocate a new mbuf dispatch the mbuf w/ rx'd
917 * data up the stack and replace it with the newly
918 * allocated one.
919 */
920 m = npe_getcl();
921 if (m != NULL) {
922 struct mbuf *mrx = npe->ix_m;
923 struct npehwbuf *hw = npe->ix_hw;
924 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
925
926 /* Flush mbuf memory for rx'd data */
927 bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
928 npe->ix_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
929
930 /* XXX flush hw buffer; works now 'cuz coherent */
931 /* set m_len etc. per rx frame size */
932 mrx->m_len = be32toh(hw->ix_ne[0].len) & 0xffff;
933 mrx->m_pkthdr.len = mrx->m_len;
934 m_set_rcvif(mrx, ifp);
935 /* Don't add M_HASFCS. See below */
936
937 #if 1
938 if (mrx->m_pkthdr.len < sizeof(struct ether_header)) {
939 log(LOG_INFO, "%s: too short frame (len=%d)\n",
940 device_xname(sc->sc_dev),
941 mrx->m_pkthdr.len);
942 /* Back out "newly allocated" mbuf. */
943 m_freem(m);
944 if_statinc(ifp, if_ierrors);
945 goto fail;
946 }
947 if ((ifp->if_flags & IFF_PROMISC) == 0) {
948 struct ether_header *eh;
949
950 /*
951 * Workaround for "Non-Intel XScale Technology
952 * Eratta" No. 29. AA:BB:CC:DD:EE:xF's packet
953 * matches the filter (both unicast and
954 * multicast).
955 */
956 eh = mtod(mrx, struct ether_header *);
957 if (ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
958 /* Unicast */
959
960 if (sc->sc_enaddr[5] != eh->ether_dhost[5]) {
961 /* Discard it */
962 #if 0
963 printf("discard it\n");
964 #endif
965 /*
966 * Back out "newly allocated"
967 * mbuf.
968 */
969 m_freem(m);
970 goto fail;
971 }
972 } else if (memcmp(eh->ether_dhost,
973 etherbroadcastaddr, 6) == 0) {
974 /* Always accept broadcast packet*/
975 } else {
976 struct ethercom *ec = &sc->sc_ethercom;
977 struct ether_multi *enm;
978 struct ether_multistep step;
979 int match = 0;
980
981 /* Multicast */
982
983 ETHER_LOCK(ec);
984 ETHER_FIRST_MULTI(step, ec, enm);
985 while (enm != NULL) {
986 uint64_t lowint, highint, dest;
987
988 lowint = MAC2UINT64(enm->enm_addrlo);
989 highint = MAC2UINT64(enm->enm_addrhi);
990 dest = MAC2UINT64(eh->ether_dhost);
991 #if 0
992 printf("%llx\n", lowint);
993 printf("%llx\n", dest);
994 printf("%llx\n", highint);
995 #endif
996 if ((lowint <= dest) && (dest <= highint)) {
997 match = 1;
998 break;
999 }
1000 ETHER_NEXT_MULTI(step, enm);
1001 }
1002 ETHER_UNLOCK(ec);
1003
1004 if (match == 0) {
1005 /* Discard it */
1006 #if 0
1007 printf("discard it(M)\n");
1008 #endif
1009 /*
1010 * Back out "newly allocated"
1011 * mbuf.
1012 */
1013 m_freem(m);
1014 goto fail;
1015 }
1016 }
1017 }
1018 if (mrx->m_pkthdr.len > NPE_FRAME_SIZE_DEFAULT) {
1019 log(LOG_INFO, "%s: oversized frame (len=%d)\n",
1020 device_xname(sc->sc_dev), mrx->m_pkthdr.len);
1021 /* Back out "newly allocated" mbuf. */
1022 m_freem(m);
1023 if_statinc(ifp, if_ierrors);
1024 goto fail;
1025 }
1026 #endif
1027
1028 /*
1029 * Trim FCS!
1030 * NPE always adds the FCS by this driver's setting,
1031 * so we always trim it here and not add M_HASFCS.
1032 */
1033 m_adj(mrx, -ETHER_CRC_LEN);
1034
1035 /*
1036 * Tap off here if there is a bpf listener.
1037 */
1038
1039 if_percpuq_enqueue(ifp->if_percpuq, mrx);
1040 } else {
1041 fail:
1042 /* discard frame and re-use mbuf */
1043 m = npe->ix_m;
1044 }
1045 if (npe_rxbuf_init(sc, npe, m) == 0) {
1046 /* return npe buf to rx free list */
1047 ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
1048 } else {
1049 /* XXX should not happen */
1050 }
1051 }
1052 #undef P2V
1053 }
1054
1055 static void
1056 npe_startxmit(struct npe_softc *sc)
1057 {
1058 struct npedma *dma = &sc->txdma;
1059 int i;
1060
1061 sc->tx_free = NULL;
1062 for (i = 0; i < dma->nbuf; i++) {
1063 struct npebuf *npe = &dma->buf[i];
1064 if (npe->ix_m != NULL) {
1065 /* NB: should not happen */
1066 printf("%s: %s: free mbuf at entry %u\n",
1067 device_xname(sc->sc_dev), __func__, i);
1068 m_freem(npe->ix_m);
1069 }
1070 npe->ix_m = NULL;
1071 npe->ix_next = sc->tx_free;
1072 sc->tx_free = npe;
1073 }
1074 }
1075
1076 static void
1077 npe_startrecv(struct npe_softc *sc)
1078 {
1079 struct npedma *dma = &sc->rxdma;
1080 struct npebuf *npe;
1081 int i;
1082
1083 for (i = 0; i < dma->nbuf; i++) {
1084 npe = &dma->buf[i];
1085 npe_rxbuf_init(sc, npe, npe->ix_m);
1086 /* Set npe buf on rx free list */
1087 ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
1088 }
1089 }
1090
1091 static void
1092 npeinit_macreg(struct npe_softc *sc)
1093 {
1094
1095 /*
1096 * Reset MAC core.
1097 */
1098 WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
1099 DELAY(NPE_MAC_RESET_DELAY);
1100 /* Configure MAC to generate MDC clock */
1101 WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
1102
1103 /* Disable transmitter and receiver in the MAC */
1104 WR4(sc, NPE_MAC_RX_CNTRL1,
1105 RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
1106 WR4(sc, NPE_MAC_TX_CNTRL1,
1107 RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
1108
1109 /*
1110 * Set the MAC core registers.
1111 */
1112 WR4(sc, NPE_MAC_INT_CLK_THRESH, 0x1); /* clock ratio: for ipx4xx */
1113 WR4(sc, NPE_MAC_TX_CNTRL2, 0xf); /* max retries */
1114 WR4(sc, NPE_MAC_RANDOM_SEED, 0x8); /* LFSR back-off seed */
1115 /* Thresholds determined by NPE firmware FS */
1116 WR4(sc, NPE_MAC_THRESH_P_EMPTY, 0x12);
1117 WR4(sc, NPE_MAC_THRESH_P_FULL, 0x30);
1118 WR4(sc, NPE_MAC_BUF_SIZE_TX, NPE_MAC_BUF_SIZE_TX_DEFAULT);
1119 /* tx fifo threshold (bytes) */
1120 WR4(sc, NPE_MAC_TX_DEFER, 0x15); /* for single deferral */
1121 WR4(sc, NPE_MAC_RX_DEFER, 0x16); /* deferral on inter-frame gap*/
1122 WR4(sc, NPE_MAC_TX_TWO_DEFER_1, 0x8); /* for 2-part deferral */
1123 WR4(sc, NPE_MAC_TX_TWO_DEFER_2, 0x7); /* for 2-part deferral */
1124 WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
1125 /* assumes MII mode */
1126 WR4(sc, NPE_MAC_TX_CNTRL1,
1127 NPE_TX_CNTRL1_RETRY /* retry failed xmits */
1128 | NPE_TX_CNTRL1_FCS_EN /* append FCS */
1129 | NPE_TX_CNTRL1_2DEFER /* 2-part deferal */
1130 | NPE_TX_CNTRL1_PAD_EN); /* pad runt frames */
1131 /* XXX pad strip? */
1132 WR4(sc, NPE_MAC_RX_CNTRL1,
1133 NPE_RX_CNTRL1_CRC_EN /* include CRC/FCS */
1134 | NPE_RX_CNTRL1_PAUSE_EN); /* ena pause frame handling */
1135 WR4(sc, NPE_MAC_RX_CNTRL2, 0);
1136 }
1137
1138 static void
1139 npeinit_resetcb(void *xsc)
1140 {
1141 struct npe_softc *sc = xsc;
1142 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1143 uint32_t msg[2];
1144
1145 if_statinc(ifp, if_oerrors);
1146 npeinit_locked(sc);
1147
1148 msg[0] = NPE_NOTIFYMACRECOVERYDONE << NPE_MAC_MSGID_SHL
1149 | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
1150 msg[1] = 0;
1151 ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1152 }
1153
1154 /*
1155 * Reset and initialize the chip
1156 */
1157 static void
1158 npeinit_locked(void *xsc)
1159 {
1160 struct npe_softc *sc = xsc;
1161 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1162
1163 /* Cancel any pending I/O. */
1164 npestop(ifp, 0);
1165
1166 /* Reset the chip to a known state. */
1167 npeinit_macreg(sc);
1168 npe_setmac(sc, CLLADDR(ifp->if_sadl));
1169 ether_mediachange(ifp);
1170 npe_setmcast(sc);
1171
1172 npe_startxmit(sc);
1173 npe_startrecv(sc);
1174
1175 ifp->if_flags |= IFF_RUNNING;
1176 ifp->if_timer = 0; /* just in case */
1177
1178 /* Enable transmitter and receiver in the MAC */
1179 WR4(sc, NPE_MAC_RX_CNTRL1,
1180 RD4(sc, NPE_MAC_RX_CNTRL1) | NPE_RX_CNTRL1_RX_EN);
1181 WR4(sc, NPE_MAC_TX_CNTRL1,
1182 RD4(sc, NPE_MAC_TX_CNTRL1) | NPE_TX_CNTRL1_TX_EN);
1183
1184 callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
1185 }
1186
1187 static int
1188 npeinit(struct ifnet *ifp)
1189 {
1190 struct npe_softc *sc = ifp->if_softc;
1191 int s;
1192
1193 s = splnet();
1194 npeinit_locked(sc);
1195 splx(s);
1196
1197 return 0;
1198 }
1199
1200 /*
1201 * Defragment an mbuf chain, returning at most maxfrags separate
1202 * mbufs+clusters. If this is not possible NULL is returned and
1203 * the original mbuf chain is left in its present (potentially
1204 * modified) state. We use two techniques: collapsing consecutive
1205 * mbufs and replacing consecutive mbufs by a cluster.
1206 */
1207 static __inline struct mbuf *
1208 npe_defrag(struct mbuf *m0)
1209 {
1210 struct mbuf *m;
1211
1212 MGETHDR(m, M_DONTWAIT, MT_DATA);
1213 if (m == NULL)
1214 return NULL;
1215 m_copy_pkthdr(m, m0);
1216
1217 if ((m->m_len = m0->m_pkthdr.len) > MHLEN) {
1218 MCLGET(m, M_DONTWAIT);
1219 if ((m->m_flags & M_EXT) == 0) {
1220 m_freem(m);
1221 return NULL;
1222 }
1223 }
1224
1225 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
1226 m_freem(m0);
1227
1228 return m;
1229 }
1230
1231 /*
1232 * Dequeue packets and place on the h/w transmit queue.
1233 */
1234 static void
1235 npestart(struct ifnet *ifp)
1236 {
1237 struct npe_softc *sc = ifp->if_softc;
1238 struct npebuf *npe;
1239 struct npehwbuf *hw;
1240 struct mbuf *m, *n;
1241 bus_dma_segment_t *segs;
1242 int nseg, len, error, i;
1243 uint32_t next;
1244
1245 if ((ifp->if_flags & IFF_RUNNING) == 0)
1246 return;
1247
1248 while (sc->tx_free != NULL) {
1249 IFQ_DEQUEUE(&ifp->if_snd, m);
1250 if (m == NULL)
1251 break;
1252 npe = sc->tx_free;
1253 error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
1254 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1255 if (error == EFBIG) {
1256 n = npe_defrag(m);
1257 if (n == NULL) {
1258 printf("%s: %s: too many fragments\n",
1259 device_xname(sc->sc_dev), __func__);
1260 m_freem(m);
1261 return; /* XXX? */
1262 }
1263 m = n;
1264 error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map,
1265 m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1266 }
1267 if (error != 0) {
1268 printf("%s: %s: error %u\n",
1269 device_xname(sc->sc_dev), __func__, error);
1270 m_freem(m);
1271 return; /* XXX? */
1272 }
1273 sc->tx_free = npe->ix_next;
1274
1275 /*
1276 * Tap off here if there is a bpf listener.
1277 */
1278 bpf_mtap(ifp, m, BPF_D_OUT);
1279
1280 bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
1281 npe->ix_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1282
1283 npe->ix_m = m;
1284 hw = npe->ix_hw;
1285 len = m->m_pkthdr.len;
1286 nseg = npe->ix_map->dm_nsegs;
1287 segs = npe->ix_map->dm_segs;
1288 next = npe->ix_neaddr + sizeof(hw->ix_ne[0]);
1289 for (i = 0; i < nseg; i++) {
1290 hw->ix_ne[i].data = htobe32(segs[i].ds_addr);
1291 hw->ix_ne[i].len = htobe32((segs[i].ds_len<<16) | len);
1292 hw->ix_ne[i].next = htobe32(next);
1293
1294 len = 0; /* zero for segments > 1 */
1295 next += sizeof(hw->ix_ne[0]);
1296 }
1297 hw->ix_ne[i-1].next = 0; /* zero last in chain */
1298 /* XXX flush descriptor instead of using uncached memory */
1299
1300 DPRINTF(sc, "%s: qwrite(%u, 0x%x) ne_data %x ne_len 0x%x\n",
1301 __func__, sc->tx_qid, npe->ix_neaddr,
1302 hw->ix_ne[0].data, hw->ix_ne[0].len);
1303 /* stick it on the tx q */
1304 /* XXX add vlan priority */
1305 ixpqmgr_qwrite(sc->tx_qid, npe->ix_neaddr);
1306
1307 ifp->if_timer = 5;
1308 }
1309 }
1310
1311 static void
1312 npe_stopxmit(struct npe_softc *sc)
1313 {
1314 struct npedma *dma = &sc->txdma;
1315 int i;
1316
1317 /* XXX qmgr */
1318 for (i = 0; i < dma->nbuf; i++) {
1319 struct npebuf *npe = &dma->buf[i];
1320
1321 if (npe->ix_m != NULL) {
1322 bus_dmamap_unload(sc->sc_dt, npe->ix_map);
1323 m_freem(npe->ix_m);
1324 npe->ix_m = NULL;
1325 }
1326 }
1327 }
1328
1329 static void
1330 npe_stoprecv(struct npe_softc *sc)
1331 {
1332 struct npedma *dma = &sc->rxdma;
1333 int i;
1334
1335 /* XXX qmgr */
1336 for (i = 0; i < dma->nbuf; i++) {
1337 struct npebuf *npe = &dma->buf[i];
1338
1339 if (npe->ix_m != NULL) {
1340 bus_dmamap_unload(sc->sc_dt, npe->ix_map);
1341 m_freem(npe->ix_m);
1342 npe->ix_m = NULL;
1343 }
1344 }
1345 }
1346
1347 /*
1348 * Turn off interrupts, and stop the nic.
1349 */
1350 void
1351 npestop(struct ifnet *ifp, int disable)
1352 {
1353 struct npe_softc *sc = ifp->if_softc;
1354
1355 /* Disable transmitter and receiver in the MAC */
1356 WR4(sc, NPE_MAC_RX_CNTRL1,
1357 RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
1358 WR4(sc, NPE_MAC_TX_CNTRL1,
1359 RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
1360
1361 callout_stop(&sc->sc_tick_ch);
1362
1363 npe_stopxmit(sc);
1364 npe_stoprecv(sc);
1365 /* XXX go into loopback & drain q's? */
1366 /* XXX but beware of disabling tx above */
1367
1368 /*
1369 * The MAC core rx/tx disable may leave the MAC hardware in an
1370 * unpredictable state. A hw reset is executed before resetting
1371 * all the MAC parameters to a known value.
1372 */
1373 WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
1374 DELAY(NPE_MAC_RESET_DELAY);
1375 WR4(sc, NPE_MAC_INT_CLK_THRESH, NPE_MAC_INT_CLK_THRESH_DEFAULT);
1376 WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
1377
1378 ifp->if_timer = 0;
1379 ifp->if_flags &= ~IFF_RUNNING;
1380 }
1381
1382 void
1383 npewatchdog(struct ifnet *ifp)
1384 {
1385 struct npe_softc *sc = ifp->if_softc;
1386 int s;
1387
1388 aprint_error_dev(sc->sc_dev, "device timeout\n");
1389 s = splnet();
1390 if_statinc(ifp, if_oerrors);
1391 npeinit_locked(sc);
1392 splx(s);
1393 }
1394
1395 static int
1396 npeioctl(struct ifnet *ifp, u_long cmd, void *data)
1397 {
1398 struct npe_softc *sc = ifp->if_softc;
1399 struct ifreq *ifr = (struct ifreq *) data;
1400 int s, error = 0;
1401
1402 s = splnet();
1403
1404 switch (cmd) {
1405 case SIOCSIFMEDIA:
1406 #if 0 /* not yet */
1407 /* Flow control requires full-duplex mode. */
1408 if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
1409 (ifr->ifr_media & IFM_FDX) == 0)
1410 ifr->ifr_media &= ~IFM_ETH_FMASK;
1411 if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
1412 if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
1413 /* We can do both TXPAUSE and RXPAUSE. */
1414 ifr->ifr_media |=
1415 IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
1416 }
1417 sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
1418 }
1419 #endif
1420 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1421 break;
1422 case SIOCSIFFLAGS:
1423 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
1424 /*
1425 * If interface is marked down and it is running,
1426 * then stop and disable it.
1427 */
1428 if_stop(ifp, 1);
1429 } else if ((ifp->if_flags & (IFF_UP |IFF_RUNNING)) == IFF_UP) {
1430 /*
1431 * If interface is marked up and it is stopped, then
1432 * start it.
1433 */
1434 error = if_init(ifp);
1435 } else if ((ifp->if_flags & IFF_UP) != 0) {
1436 u_short diff;
1437
1438 /* Up (AND RUNNING). */
1439
1440 diff = (ifp->if_flags ^ sc->sc_if_flags)
1441 & (IFF_PROMISC | IFF_ALLMULTI);
1442 if ((diff & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
1443 /*
1444 * If the difference between last flag and
1445 * new flag only IFF_PROMISC or IFF_ALLMULTI,
1446 * set multicast filter only (don't reset to
1447 * prevent link down).
1448 */
1449 npe_setmcast(sc);
1450 } else {
1451 /*
1452 * Reset the interface to pick up changes in
1453 * any other flags that affect the hardware
1454 * state.
1455 */
1456 error = if_init(ifp);
1457 }
1458 }
1459 sc->sc_if_flags = ifp->if_flags;
1460 break;
1461 default:
1462 error = ether_ioctl(ifp, cmd, data);
1463 if (error == ENETRESET) {
1464 /*
1465 * Multicast list has changed; set the hardware filter
1466 * accordingly.
1467 */
1468 npe_setmcast(sc);
1469 error = 0;
1470 }
1471 }
1472
1473 npestart(ifp);
1474
1475 splx(s);
1476 return error;
1477 }
1478
1479 /*
1480 * Setup a traffic class -> rx queue mapping.
1481 */
1482 static int
1483 npe_setrxqosentry(struct npe_softc *sc, int classix, int trafclass, int qid)
1484 {
1485 int npeid = npeconfig[sc->sc_unit].npeid;
1486 uint32_t msg[2];
1487
1488 msg[0] = (NPE_SETRXQOSENTRY << NPE_MAC_MSGID_SHL) | (npeid << 20)
1489 | classix;
1490 msg[1] = (trafclass << 24) | (1 << 23) | (qid << 16) | (qid << 4);
1491 return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1492 }
1493
1494 /*
1495 * Update and reset the statistics in the NPE.
1496 */
1497 static int
1498 npe_updatestats(struct npe_softc *sc)
1499 {
1500 uint32_t msg[2];
1501
1502 msg[0] = NPE_RESETSTATS << NPE_MAC_MSGID_SHL;
1503 msg[1] = sc->sc_stats_phys; /* physical address of stat block */
1504 return ixpnpe_sendmsg(sc->sc_npe, msg); /* NB: no recv */
1505 }
1506
1507 #if 0
1508 /*
1509 * Get the current statistics block.
1510 */
1511 static int
1512 npe_getstats(struct npe_softc *sc)
1513 {
1514 uint32_t msg[2];
1515
1516 msg[0] = NPE_GETSTATS << NPE_MAC_MSGID_SHL;
1517 msg[1] = sc->sc_stats_phys; /* physical address of stat block */
1518 return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1519 }
1520
1521 /*
1522 * Query the image id of the loaded firmware.
1523 */
1524 static uint32_t
1525 npe_getimageid(struct npe_softc *sc)
1526 {
1527 uint32_t msg[2];
1528
1529 msg[0] = NPE_GETSTATUS << NPE_MAC_MSGID_SHL;
1530 msg[1] = 0;
1531 return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg) == 0 ? msg[1] : 0;
1532 }
1533
1534 /*
1535 * Enable/disable loopback.
1536 */
1537 static int
1538 npe_setloopback(struct npe_softc *sc, int ena)
1539 {
1540 uint32_t msg[2];
1541
1542 msg[0] = (NPE_SETLOOPBACK << NPE_MAC_MSGID_SHL) | (ena != 0);
1543 msg[1] = 0;
1544 return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1545 }
1546 #endif
1547
1548 /*
1549 * MII bus support routines.
1550 *
1551 * NB: ixp425 has one PHY per NPE
1552 */
1553 static uint32_t
1554 npe_mii_mdio_read(struct npe_softc *sc, int reg)
1555 {
1556 #define MII_RD4(sc, reg) bus_space_read_4(sc->sc_iot, sc->sc_miih, reg)
1557 uint32_t v;
1558
1559 /* NB: registers are known to be sequential */
1560 v = (MII_RD4(sc, reg+0) & 0xff) << 0;
1561 v |= (MII_RD4(sc, reg+4) & 0xff) << 8;
1562 v |= (MII_RD4(sc, reg+8) & 0xff) << 16;
1563 v |= (MII_RD4(sc, reg+12) & 0xff) << 24;
1564 return v;
1565 #undef MII_RD4
1566 }
1567
1568 static void
1569 npe_mii_mdio_write(struct npe_softc *sc, int reg, uint32_t cmd)
1570 {
1571 #define MII_WR4(sc, reg, v) \
1572 bus_space_write_4(sc->sc_iot, sc->sc_miih, reg, v)
1573
1574 /* NB: registers are known to be sequential */
1575 MII_WR4(sc, reg+0, cmd & 0xff);
1576 MII_WR4(sc, reg+4, (cmd >> 8) & 0xff);
1577 MII_WR4(sc, reg+8, (cmd >> 16) & 0xff);
1578 MII_WR4(sc, reg+12, (cmd >> 24) & 0xff);
1579 #undef MII_WR4
1580 }
1581
1582 static int
1583 npe_mii_mdio_wait(struct npe_softc *sc)
1584 {
1585 #define MAXTRIES 100 /* XXX */
1586 uint32_t v;
1587 int i;
1588
1589 for (i = 0; i < MAXTRIES; i++) {
1590 v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_CMD);
1591 if ((v & NPE_MII_GO) == 0)
1592 return 0;
1593 }
1594 return ETIMEDOUT;
1595 #undef MAXTRIES
1596 }
1597
1598 static int
1599 npe_miibus_readreg(device_t self, int phy, int reg, uint16_t *val)
1600 {
1601 struct npe_softc *sc = device_private(self);
1602 uint32_t v;
1603
1604 if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
1605 return -1;
1606 v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
1607 | NPE_MII_GO;
1608 npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
1609 if (npe_mii_mdio_wait(sc) == 0)
1610 v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_STS);
1611 else
1612 v = 0xffff | NPE_MII_READ_FAIL;
1613
1614 if ((v & NPE_MII_READ_FAIL) != 0)
1615 return -1;
1616
1617 *val = v & 0xffff;
1618 return 0;
1619 #undef MAXTRIES
1620 }
1621
1622 static int
1623 npe_miibus_writereg(device_t self, int phy, int reg, uint16_t val)
1624 {
1625 struct npe_softc *sc = device_private(self);
1626 uint32_t v;
1627
1628 if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
1629 return -1;
1630 v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
1631 | val | NPE_MII_WRITE
1632 | NPE_MII_GO;
1633 npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
1634
1635 return npe_mii_mdio_wait(sc);
1636 }
1637
1638 static void
1639 npe_miibus_statchg(struct ifnet *ifp)
1640 {
1641 struct npe_softc *sc = ifp->if_softc;
1642 uint32_t tx1, rx1;
1643 uint32_t randoff;
1644
1645 /* Sync MAC duplex state */
1646 tx1 = RD4(sc, NPE_MAC_TX_CNTRL1);
1647 rx1 = RD4(sc, NPE_MAC_RX_CNTRL1);
1648 if (sc->sc_mii.mii_media_active & IFM_FDX) {
1649 WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
1650 tx1 &= ~NPE_TX_CNTRL1_DUPLEX;
1651 rx1 |= NPE_RX_CNTRL1_PAUSE_EN;
1652 } else {
1653 struct timeval now;
1654 getmicrotime(&now);
1655 randoff = (RD4(sc, NPE_MAC_UNI_ADDR_6) ^ now.tv_usec)
1656 & 0x7f;
1657 WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT
1658 + randoff);
1659 tx1 |= NPE_TX_CNTRL1_DUPLEX;
1660 rx1 &= ~NPE_RX_CNTRL1_PAUSE_EN;
1661 }
1662 WR4(sc, NPE_MAC_RX_CNTRL1, rx1);
1663 WR4(sc, NPE_MAC_TX_CNTRL1, tx1);
1664 }
1665