if_gfe.c revision 1.5 1 /* $NetBSD: if_gfe.c,v 1.5 2003/03/24 17:00:54 matt Exp $ */
2
3 /*
4 * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Allegro Networks, Inc., and Wasabi Systems, Inc.
19 * 4. The name of Allegro Networks, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
21 * written permission.
22 * 5. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
27 * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * if_gfe.c -- GT ethernet MAC driver
42 */
43
44 #include "opt_inet.h"
45 #include "bpfilter.h"
46
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/inttypes.h>
50 #include <sys/queue.h>
51
52 #include <sys/callout.h>
53 #include <sys/device.h>
54 #include <sys/errno.h>
55 #include <sys/ioctl.h>
56 #include <sys/mbuf.h>
57 #include <sys/socket.h>
58
59 #include <machine/bus.h>
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_ether.h>
64 #include <net/if_media.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/if_inarp.h>
69 #endif
70 #if NBPFILTER > 0
71 #include <net/bpf.h>
72 #endif
73
74 #include <dev/mii/miivar.h>
75
76 #include <dev/marvell/gtintrreg.h>
77 #include <dev/marvell/gtethreg.h>
78
79 #include <dev/marvell/gtvar.h>
80 #include <dev/marvell/if_gfevar.h>
81
82 #define GE_READ(sc, reg) \
83 bus_space_read_4((sc)->sc_gt_memt, (sc)->sc_memh, ETH__ ## reg)
84 #define GE_WRITE(sc, reg, v) \
85 bus_space_write_4((sc)->sc_gt_memt, (sc)->sc_memh, ETH__ ## reg, (v))
86
87 #define GE_DEBUG
88 #if 0
89 #define GE_NOHASH
90 #define GE_NORX
91 #endif
92
93 #ifdef GE_DEBUG
94 #define GE_DPRINTF(sc, a) do \
95 if ((sc)->sc_ec.ec_if.if_flags & IFF_DEBUG) \
96 printf a; \
97 while (0)
98 #define GE_FUNC_ENTER(sc, func) GE_DPRINTF(sc, ("[" func))
99 #define GE_FUNC_EXIT(sc, str) GE_DPRINTF(sc, (str "]"))
100 #else
101 #define GE_DPRINTF(sc, a) do { } while (0)
102 #define GE_FUNC_ENTER(sc, func) do { } while (0)
103 #define GE_FUNC_EXIT(sc, str) do { } while (0)
104 #endif
105 enum gfe_whack_op {
106 GE_WHACK_START, GE_WHACK_RESTART,
107 GE_WHACK_CHANGE, GE_WHACK_STOP
108 };
109
110 enum gfe_hash_op {
111 GE_HASH_ADD, GE_HASH_REMOVE,
112 };
113
114 #if 1
115 #define htogt32(a) htobe32(a)
116 #define gt32toh(a) be32toh(a)
117 #else
118 #define htogt32(a) htole32(a)
119 #define gt32toh(a) le32toh(a)
120 #endif
121
122 #define STATIC
123
124 STATIC int gfe_match (struct device *, struct cfdata *, void *);
125 STATIC void gfe_attach (struct device *, struct device *, void *);
126
127 STATIC int gfe_dmamem_alloc(struct gfe_softc *, struct gfe_dmamem *, int,
128 size_t, int);
129 STATIC void gfe_dmamem_free(struct gfe_softc *, struct gfe_dmamem *);
130
131 STATIC int gfe_ifioctl (struct ifnet *, u_long, caddr_t);
132 STATIC void gfe_ifstart (struct ifnet *);
133 STATIC void gfe_ifwatchdog (struct ifnet *);
134
135 STATIC int gfe_mii_mediachange (struct ifnet *);
136 STATIC void gfe_mii_mediastatus (struct ifnet *, struct ifmediareq *);
137 STATIC int gfe_mii_read (struct device *, int, int);
138 STATIC void gfe_mii_write (struct device *, int, int, int);
139 STATIC void gfe_mii_statchg (struct device *);
140
141 STATIC void gfe_tick(void *arg);
142
143 STATIC void gfe_tx_restart(void *);
144 STATIC int gfe_tx_enqueue(struct gfe_softc *, enum gfe_txprio);
145 STATIC uint32_t gfe_tx_done(struct gfe_softc *, enum gfe_txprio, uint32_t);
146 STATIC void gfe_tx_cleanup(struct gfe_softc *, enum gfe_txprio, int);
147 STATIC int gfe_tx_start(struct gfe_softc *, enum gfe_txprio);
148 STATIC void gfe_tx_stop(struct gfe_softc *, enum gfe_whack_op);
149
150 STATIC void gfe_rx_cleanup(struct gfe_softc *, enum gfe_rxprio);
151 STATIC void gfe_rx_get(struct gfe_softc *, enum gfe_rxprio);
152 STATIC int gfe_rx_prime(struct gfe_softc *);
153 STATIC uint32_t gfe_rx_process(struct gfe_softc *, uint32_t, uint32_t);
154 STATIC int gfe_rx_rxqalloc(struct gfe_softc *, enum gfe_rxprio);
155 STATIC void gfe_rx_stop(struct gfe_softc *, enum gfe_whack_op);
156
157 STATIC int gfe_intr(void *);
158
159 STATIC int gfe_whack(struct gfe_softc *, enum gfe_whack_op);
160
161 STATIC int gfe_hash_compute(struct gfe_softc *, const u_int8_t [ETHER_ADDR_LEN]);
162 STATIC int gfe_hash_entry_op(struct gfe_softc *, enum gfe_hash_op,
163 enum gfe_rxprio, const u_int8_t [ETHER_ADDR_LEN]);
164 STATIC int gfe_hash_multichg(struct ethercom *, const struct ether_multi *,
165 u_long);
166 STATIC int gfe_hash_fill(struct gfe_softc *);
167 STATIC int gfe_hash_alloc(struct gfe_softc *);
168
169 /* Linkup to the rest of the kernel */
170 CFATTACH_DECL(gfe, sizeof(struct gfe_softc),
171 gfe_match, gfe_attach, NULL, NULL);
172
173 extern struct cfdriver gfe_cd;
174
175 int
176 gfe_match(struct device *parent, struct cfdata *cf, void *aux)
177 {
178 struct gt_softc *gt = (struct gt_softc *) parent;
179 struct gt_attach_args *ga = aux;
180 uint8_t enaddr[6];
181
182 if (!GT_ETHEROK(gt, ga, &gfe_cd))
183 return 0;
184
185 if (gtget_macaddr(gt, ga->ga_unit, enaddr) < 0)
186 return 0;
187
188 if (enaddr[0] == 0 && enaddr[1] == 0 && enaddr[2] == 0 &&
189 enaddr[3] == 0 && enaddr[4] == 0 && enaddr[5] == 0)
190 return 0;
191
192 return 1;
193 }
194
195 /*
196 * Attach this instance, and then all the sub-devices
197 */
198 void
199 gfe_attach(struct device *parent, struct device *self, void *aux)
200 {
201 struct gt_attach_args * const ga = aux;
202 struct gt_softc * const gt = (struct gt_softc *) parent;
203 struct gfe_softc * const sc = (struct gfe_softc *) self;
204 struct ifnet * const ifp = &sc->sc_ec.ec_if;
205 uint32_t data;
206 uint8_t enaddr[6];
207 int phyaddr;
208 uint32_t sdcr;
209
210 GT_ETHERFOUND(gt, ga);
211
212 sc->sc_gt_memt = ga->ga_memt;
213 sc->sc_gt_memh = ga->ga_memh;
214 sc->sc_dmat = ga->ga_dmat;
215 sc->sc_macno = ga->ga_unit;
216
217 if (bus_space_subregion(sc->sc_gt_memt, sc->sc_gt_memh,
218 ETH_BASE(sc->sc_macno), ETH_SIZE, &sc->sc_memh)) {
219 aprint_error(": failed to map registers\n");
220 }
221
222 callout_init(&sc->sc_co);
223
224 data = bus_space_read_4(sc->sc_gt_memt, sc->sc_gt_memh, ETH_EPAR);
225 phyaddr = ETH_EPAR_PhyAD_GET(data, sc->sc_macno);
226
227 gtget_macaddr(gt, sc->sc_macno, enaddr);
228
229 sc->sc_pcr = GE_READ(sc, EPCR);
230 sc->sc_pcxr = GE_READ(sc, EPCXR);
231 sc->sc_intrmask = GE_READ(sc, EIMR) | ETH_IR_MIIPhySTC;
232
233 aprint_normal(": address %s", ether_sprintf(enaddr));
234
235 #if defined(DEBUG)
236 aprint_normal(", pcr %#x, pcxr %#x", sc->sc_pcr, sc->sc_pcxr);
237 #endif
238
239 sc->sc_pcxr &= ~ETH_EPCXR_PRIOrx_Override;
240 if (sc->sc_dev.dv_cfdata->cf_flags & 1) {
241 aprint_normal(", phy %d (rmii)", phyaddr);
242 sc->sc_pcxr |= ETH_EPCXR_RMIIEn;
243 } else {
244 aprint_normal(", phy %d (mii)", phyaddr);
245 sc->sc_pcxr &= ~ETH_EPCXR_RMIIEn;
246 }
247 sc->sc_pcxr &= ~(3 << 14);
248 sc->sc_pcxr |= (ETH_EPCXR_MFL_1536 << 14);
249
250 if (sc->sc_pcr & ETH_EPCR_EN) {
251 int tries = 1000;
252 /*
253 * Abort transmitter and receiver and wait for them to quiese
254 */
255 GE_WRITE(sc, ESDCMR, ETH_ESDCMR_AR|ETH_ESDCMR_AT);
256 do {
257 delay(100);
258 } while (tries-- > 0 && (GE_READ(sc, ESDCMR) & (ETH_ESDCMR_AR|ETH_ESDCMR_AT)));
259 }
260
261 sc->sc_pcr &= ~ETH_EPCR_EN;
262
263 #if defined(DEBUG)
264 aprint_normal(", pcr %#x, pcxr %#x", sc->sc_pcr, sc->sc_pcxr);
265 #endif
266
267 /*
268 * Now turn off the GT. If it didn't quiese, too ***ing bad.
269 */
270 GE_WRITE(sc, EPCR, sc->sc_pcr);
271 GE_WRITE(sc, EIMR, sc->sc_intrmask);
272 sdcr = GE_READ(sc, ESDCR);
273 ETH_ESDCR_BSZ_SET(sdcr, ETH_ESDCR_BSZ_4);
274 sdcr |= ETH_ESDCR_RIFB;
275 GE_WRITE(sc, ESDCR, sdcr);
276 sc->sc_max_frame_length = 1536;
277
278 aprint_normal("\n");
279 sc->sc_mii.mii_ifp = ifp;
280 sc->sc_mii.mii_readreg = gfe_mii_read;
281 sc->sc_mii.mii_writereg = gfe_mii_write;
282 sc->sc_mii.mii_statchg = gfe_mii_statchg;
283
284 ifmedia_init(&sc->sc_mii.mii_media, 0, gfe_mii_mediachange,
285 gfe_mii_mediastatus);
286
287 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, phyaddr,
288 MII_OFFSET_ANY, MIIF_NOISOLATE);
289 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
290 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
291 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
292 } else {
293 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
294 }
295
296 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
297 ifp->if_softc = sc;
298 /* ifp->if_mowner = &sc->sc_mowner; */
299 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
300 #if 0
301 ifp->if_flags |= IFF_DEBUG;
302 #endif
303 ifp->if_ioctl = gfe_ifioctl;
304 ifp->if_start = gfe_ifstart;
305 ifp->if_watchdog = gfe_ifwatchdog;
306
307 if_attach(ifp);
308 ether_ifattach(ifp, enaddr);
309 #if NBPFILTER > 0
310 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
311 #endif
312 #if NRND > 0
313 rnd_attach_source(&sc->sc_rnd_source, self->dv_xname, RND_TYPE_NET, 0);
314 #endif
315 intr_establish(IRQ_ETH0 + sc->sc_macno, IST_LEVEL, IPL_NET,
316 gfe_intr, sc);
317 }
318
319 int
320 gfe_dmamem_alloc(struct gfe_softc *sc, struct gfe_dmamem *gdm, int maxsegs,
321 size_t size, int flags)
322 {
323 int error = 0;
324 GE_FUNC_ENTER(sc, "gfe_dmamem_alloc");
325 gdm->gdm_size = size;
326 gdm->gdm_maxsegs = maxsegs;
327
328 #if 0
329 flags |= BUS_DMA_NOCACHE;
330 #endif
331
332 error = bus_dmamem_alloc(sc->sc_dmat, gdm->gdm_size, NBPG,
333 gdm->gdm_size, gdm->gdm_segs, gdm->gdm_maxsegs, &gdm->gdm_nsegs,
334 BUS_DMA_NOWAIT);
335 if (error)
336 goto fail;
337
338 error = bus_dmamem_map(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs,
339 gdm->gdm_size, &gdm->gdm_kva, flags | BUS_DMA_NOWAIT);
340 if (error)
341 goto fail;
342
343 error = bus_dmamap_create(sc->sc_dmat, gdm->gdm_size, gdm->gdm_nsegs,
344 gdm->gdm_size, 0, BUS_DMA_ALLOCNOW|BUS_DMA_NOWAIT, &gdm->gdm_map);
345 if (error)
346 goto fail;
347
348 error = bus_dmamap_load(sc->sc_dmat, gdm->gdm_map, gdm->gdm_kva,
349 gdm->gdm_size, NULL, BUS_DMA_NOWAIT);
350 if (error)
351 goto fail;
352
353 /* invalidate from cache */
354 bus_dmamap_sync(sc->sc_dmat, gdm->gdm_map, 0, gdm->gdm_size,
355 BUS_DMASYNC_PREREAD);
356 fail:
357 if (error) {
358 gfe_dmamem_free(sc, gdm);
359 GE_DPRINTF(sc, (":err=%d", error));
360 }
361 GE_DPRINTF(sc, (":kva=%p/%#x,map=%p,nsegs=%d,pa=%x/%x",
362 gdm->gdm_kva, gdm->gdm_size, gdm->gdm_map, gdm->gdm_map->dm_nsegs,
363 gdm->gdm_map->dm_segs->ds_addr, gdm->gdm_map->dm_segs->ds_len));
364 GE_FUNC_EXIT(sc, "");
365 return error;
366 }
367
368 void
369 gfe_dmamem_free(struct gfe_softc *sc, struct gfe_dmamem *gdm)
370 {
371 GE_FUNC_ENTER(sc, "gfe_dmamem_free");
372 if (gdm->gdm_map)
373 bus_dmamap_destroy(sc->sc_dmat, gdm->gdm_map);
374 if (gdm->gdm_kva)
375 bus_dmamem_unmap(sc->sc_dmat, gdm->gdm_kva, gdm->gdm_size);
376 if (gdm->gdm_nsegs > 0)
377 bus_dmamem_free(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs);
378 gdm->gdm_map = NULL;
379 gdm->gdm_kva = NULL;
380 gdm->gdm_nsegs = 0;
381 GE_FUNC_EXIT(sc, "");
382 }
383
384 int
385 gfe_ifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
386 {
387 struct gfe_softc * const sc = ifp->if_softc;
388 struct ifreq *ifr = (struct ifreq *) data;
389 struct ifaddr *ifa = (struct ifaddr *) data;
390 int s, error = 0;
391
392 GE_FUNC_ENTER(sc, "gfe_ifioctl");
393 s = splnet();
394
395 switch (cmd) {
396 case SIOCSIFADDR:
397 ifp->if_flags |= IFF_UP;
398 switch (ifa->ifa_addr->sa_family) {
399 #ifdef INET
400 case AF_INET:
401 error = gfe_whack(sc, GE_WHACK_START);
402 if (error == 0)
403 arp_ifinit(ifp, ifa);
404 break;
405 #endif
406 default:
407 error = gfe_whack(sc, GE_WHACK_START);
408 break;
409 }
410 break;
411
412 case SIOCSIFFLAGS:
413 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
414 case IFF_UP|IFF_RUNNING:/* active->active, update */
415 error = gfe_whack(sc, GE_WHACK_CHANGE);
416 break;
417 case IFF_RUNNING: /* not up, so we stop */
418 error = gfe_whack(sc, GE_WHACK_STOP);
419 break;
420 case IFF_UP: /* not running, so we start */
421 error = gfe_whack(sc, GE_WHACK_START);
422 break;
423 case 0: /* idle->idle: do nothing */
424 break;
425 }
426 break;
427
428 case SIOCADDMULTI:
429 case SIOCDELMULTI:
430 error = (cmd == SIOCADDMULTI)
431 ? ether_addmulti(ifr, &sc->sc_ec)
432 : ether_delmulti(ifr, &sc->sc_ec);
433 if (error == ENETRESET) {
434 if (ifp->if_flags & IFF_RUNNING)
435 error = gfe_whack(sc, GE_WHACK_CHANGE);
436 else
437 error = 0;
438 }
439 break;
440
441 case SIOCSIFMTU:
442 if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
443 error = EINVAL;
444 break;
445 }
446 ifp->if_mtu = ifr->ifr_mtu;
447 break;
448
449 case SIOCSIFMEDIA:
450 case SIOCGIFMEDIA:
451 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
452 break;
453
454 default:
455 error = EINVAL;
456 break;
457 }
458 splx(s);
459 GE_FUNC_EXIT(sc, "");
460 return error;
461 }
462
463 void
464 gfe_ifstart(struct ifnet *ifp)
465 {
466 struct gfe_softc * const sc = ifp->if_softc;
467 struct mbuf *m;
468
469 GE_FUNC_ENTER(sc, "gfe_ifstart");
470
471 if ((ifp->if_flags & IFF_RUNNING) == 0) {
472 GE_FUNC_EXIT(sc, "$");
473 return;
474 }
475
476 if (sc->sc_txq[GE_TXPRIO_HI] == NULL) {
477 ifp->if_flags |= IFF_OACTIVE;
478 #if defined(DEBUG) || defined(DIAGNOSTIC)
479 printf("%s: ifstart: txq not yet created\n", ifp->if_xname);
480 #endif
481 GE_FUNC_EXIT(sc, "");
482 return;
483 }
484
485 for (;;) {
486 IF_DEQUEUE(&ifp->if_snd, m);
487 if (m == NULL) {
488 ifp->if_flags &= ~IFF_OACTIVE;
489 GE_FUNC_EXIT(sc, "");
490 return;
491 }
492
493 /*
494 * No space in the pending queue? try later.
495 */
496 if (IF_QFULL(&sc->sc_txq[GE_TXPRIO_HI]->txq_pendq))
497 break;
498
499 /*
500 * Try to enqueue a mbuf to the device. If that fails, we
501 * can always try to map the next mbuf.
502 */
503 IF_ENQUEUE(&sc->sc_txq[GE_TXPRIO_HI]->txq_pendq, m);
504 GE_DPRINTF(sc, (">"));
505 #ifndef GE_NOTX
506 (void) gfe_tx_enqueue(sc, GE_TXPRIO_HI);
507 #endif
508 }
509
510 /*
511 * Attempt to queue the mbuf for send failed.
512 */
513 IF_PREPEND(&ifp->if_snd, m);
514 ifp->if_flags |= IFF_OACTIVE;
515 GE_FUNC_EXIT(sc, "%%");
516 }
517
518 void
519 gfe_ifwatchdog(struct ifnet *ifp)
520 {
521 struct gfe_softc * const sc = ifp->if_softc;
522 struct gfe_txqueue *txq;
523
524 GE_FUNC_ENTER(sc, "gfe_ifwatchdog");
525 printf("%s: device timeout",
526 sc->sc_dev.dv_xname);
527 if ((txq = sc->sc_txq[GE_TXPRIO_HI]) != NULL) {
528 unsigned int curtxdnum = (bus_space_read_4(sc->sc_gt_memt, sc->sc_gt_memh, txq->txq_ectdp) - txq->txq_desc_busaddr) / 16;
529 printf(" (fi=%d,lo=%d,cur=%d(%#x),icm=%#x) ",
530 txq->txq_fi, txq->txq_lo, curtxdnum,
531 txq->txq_descs[curtxdnum].ed_cmdsts,
532 GE_READ(sc, EICR));
533 }
534 printf("\n");
535 ifp->if_oerrors++;
536 (void) gfe_whack(sc, GE_WHACK_RESTART);
537 GE_FUNC_EXIT(sc, "");
538 }
539
540 int
542 gfe_rx_rxqalloc(struct gfe_softc *sc, enum gfe_rxprio rxprio)
543 {
544 struct gfe_rxqueue *rxq;
545 volatile struct gt_eth_desc *rxd;
546 const bus_dma_segment_t *ds;
547 int error;
548 int idx;
549 bus_addr_t nxtaddr;
550 bus_size_t boff;
551
552 GE_FUNC_ENTER(sc, "gfe_rx_rxqalloc");
553 GE_DPRINTF(sc, ("(%d)", rxprio));
554 if (sc->sc_rxq[rxprio] != NULL) {
555 GE_FUNC_EXIT(sc, "");
556 return 0;
557 }
558
559 rxq = (struct gfe_rxqueue *) malloc(sizeof(*rxq), M_DEVBUF, M_NOWAIT);
560 if (rxq == NULL) {
561 GE_FUNC_EXIT(sc, "!");
562 return ENOMEM;
563 }
564
565 memset(rxq, 0, sizeof(*rxq));
566
567 error = gfe_dmamem_alloc(sc, &rxq->rxq_desc_mem, 1,
568 GE_RXDESC_MEMSIZE, BUS_DMA_NOCACHE);
569 if (error) {
570 free(rxq, M_DEVBUF);
571 GE_FUNC_EXIT(sc, "!!");
572 return error;
573 }
574 error = gfe_dmamem_alloc(sc, &rxq->rxq_buf_mem, GE_RXBUF_NSEGS,
575 GE_RXBUF_MEMSIZE, 0);
576 if (error) {
577 gfe_dmamem_free(sc, &rxq->rxq_desc_mem);
578 free(rxq, M_DEVBUF);
579 GE_FUNC_EXIT(sc, "!!!");
580 return error;
581 }
582
583 memset(rxq->rxq_desc_mem.gdm_kva, 0, GE_TXMEM_SIZE);
584
585 sc->sc_rxq[rxprio] = rxq;
586 rxq->rxq_descs =
587 (volatile struct gt_eth_desc *) rxq->rxq_desc_mem.gdm_kva;
588 rxq->rxq_desc_busaddr = rxq->rxq_desc_mem.gdm_map->dm_segs[0].ds_addr;
589 rxq->rxq_bufs = (struct gfe_rxbuf *) rxq->rxq_buf_mem.gdm_kva;
590 rxq->rxq_fi = 0;
591 rxq->rxq_active = GE_RXDESC_MAX;
592 for (idx = 0, rxd = rxq->rxq_descs,
593 boff = 0, ds = rxq->rxq_buf_mem.gdm_map->dm_segs,
594 nxtaddr = rxq->rxq_desc_busaddr + sizeof(*rxd);
595 idx < GE_RXDESC_MAX;
596 idx++, rxd++, nxtaddr += sizeof(*rxd)) {
597 rxd->ed_lencnt = htogt32(GE_RXBUF_SIZE << 16);
598 rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
599 rxd->ed_bufptr = htogt32(ds->ds_addr + boff);
600 /*
601 * update the nxtptr to point to the next txd.
602 */
603 if (idx == GE_RXDESC_MAX - 1)
604 nxtaddr = rxq->rxq_desc_busaddr;
605 rxd->ed_nxtptr = htogt32(nxtaddr);
606 boff += GE_RXBUF_SIZE;
607 if (boff == ds->ds_len) {
608 ds++;
609 boff = 0;
610 }
611 }
612 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map, 0,
613 rxq->rxq_desc_mem.gdm_map->dm_mapsize,
614 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
615 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map, 0,
616 rxq->rxq_buf_mem.gdm_map->dm_mapsize,
617 BUS_DMASYNC_PREREAD);
618
619 rxq->rxq_intrbits = ETH_IR_RxBuffer|ETH_IR_RxError;
620 switch (rxprio) {
621 case GE_RXPRIO_HI:
622 rxq->rxq_intrbits |= ETH_IR_RxBuffer_3|ETH_IR_RxError_3;
623 rxq->rxq_efrdp = ETH_EFRDP3(sc->sc_macno);
624 rxq->rxq_ecrdp = ETH_ECRDP3(sc->sc_macno);
625 break;
626 case GE_RXPRIO_MEDHI:
627 rxq->rxq_intrbits |= ETH_IR_RxBuffer_2|ETH_IR_RxError_2;
628 rxq->rxq_efrdp = ETH_EFRDP2(sc->sc_macno);
629 rxq->rxq_ecrdp = ETH_ECRDP2(sc->sc_macno);
630 break;
631 case GE_RXPRIO_MEDLO:
632 rxq->rxq_intrbits |= ETH_IR_RxBuffer_1|ETH_IR_RxError_1;
633 rxq->rxq_efrdp = ETH_EFRDP1(sc->sc_macno);
634 rxq->rxq_ecrdp = ETH_ECRDP1(sc->sc_macno);
635 break;
636 case GE_RXPRIO_LO:
637 rxq->rxq_intrbits |= ETH_IR_RxBuffer_0|ETH_IR_RxError_0;
638 rxq->rxq_efrdp = ETH_EFRDP0(sc->sc_macno);
639 rxq->rxq_ecrdp = ETH_ECRDP0(sc->sc_macno);
640 break;
641 }
642 GE_FUNC_EXIT(sc, "");
643 return error;
644 }
645
646 void
647 gfe_rx_get(struct gfe_softc *sc, enum gfe_rxprio rxprio)
648 {
649 struct ifnet * const ifp = &sc->sc_ec.ec_if;
650 struct gfe_rxqueue * const rxq = sc->sc_rxq[rxprio];
651 struct mbuf *m = rxq->rxq_curpkt;
652
653 GE_FUNC_ENTER(sc, "gfe_rx_get");
654 GE_DPRINTF(sc, ("(%d)", rxprio));
655
656 while (rxq->rxq_active > 0) {
657 volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[rxq->rxq_fi];
658 struct gfe_rxbuf *rxb = &rxq->rxq_bufs[rxq->rxq_fi];
659 const struct ether_header *eh;
660 unsigned int cmdsts;
661 size_t buflen;
662
663 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
664 rxq->rxq_fi * sizeof(*rxd), sizeof(*rxd),
665 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
666 cmdsts = gt32toh(rxd->ed_cmdsts);
667 GE_DPRINTF(sc, (":%d=%#x", rxq->rxq_fi, cmdsts));
668 rxq->rxq_cmdsts = cmdsts;
669 /*
670 * Sometimes the GE "forgets" to reset the ownership bit.
671 * But if the length has been rewritten, the packet is ours
672 * so pretend the O bit is set.
673 */
674 buflen = gt32toh(rxd->ed_lencnt) & 0xffff;
675 if ((cmdsts & RX_CMD_O) && buflen == 0) {
676 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
677 rxq->rxq_fi * sizeof(*rxd), sizeof(*rxd),
678 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
679 break;
680 }
681
682 /*
683 * If this is not a single buffer packet with no errors
684 * or for some reason it's bigger than our frame size,
685 * ignore it and go to the next packet.
686 */
687 if ((cmdsts & (RX_CMD_F|RX_CMD_L|RX_STS_ES)) !=
688 (RX_CMD_F|RX_CMD_L) ||
689 buflen > sc->sc_max_frame_length) {
690 GE_DPRINTF(sc, ("!"));
691 --rxq->rxq_active;
692 ifp->if_ipackets++;
693 ifp->if_ierrors++;
694 goto give_it_back;
695 }
696
697 if (m == NULL) {
698 MGETHDR(m, M_DONTWAIT, MT_DATA);
699 if (m == NULL) {
700 GE_DPRINTF(sc, ("?"));
701 break;
702 }
703 }
704 if ((m->m_flags & M_EXT) == 0 && buflen > MHLEN - 2) {
705 MCLGET(m, M_DONTWAIT);
706 if ((m->m_flags & M_EXT) == 0) {
707 GE_DPRINTF(sc, ("?"));
708 break;
709 }
710 }
711 m->m_data += 2;
712 m->m_len = 0;
713 m->m_pkthdr.len = 0;
714 m->m_pkthdr.rcvif = ifp;
715 rxq->rxq_cmdsts = cmdsts;
716 --rxq->rxq_active;
717
718 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map,
719 rxq->rxq_fi * sizeof(*rxb), buflen, BUS_DMASYNC_POSTREAD);
720
721 KASSERT(m->m_len == 0 && m->m_pkthdr.len == 0);
722 memcpy(m->m_data + m->m_len, rxb->rb_data, buflen);
723 m->m_len = buflen;
724 m->m_pkthdr.len = buflen;
725 m->m_flags |= M_HASFCS;
726
727 ifp->if_ipackets++;
728 #if NBPFILTER > 0
729 if (ifp->if_bpf != NULL)
730 bpf_mtap(ifp->if_bpf, m);
731 #endif
732
733 eh = (const struct ether_header *) m->m_data;
734 if ((ifp->if_flags & IFF_PROMISC) ||
735 (rxq->rxq_cmdsts & RX_STS_M) == 0 ||
736 (rxq->rxq_cmdsts & RX_STS_HE) ||
737 (eh->ether_dhost[0] & 1) != 0 ||
738 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
739 ETHER_ADDR_LEN) == 0) {
740 (*ifp->if_input)(ifp, m);
741 m = NULL;
742 GE_DPRINTF(sc, (">"));
743 } else {
744 m->m_len = 0;
745 m->m_pkthdr.len = 0;
746 GE_DPRINTF(sc, ("+"));
747 }
748 rxq->rxq_cmdsts = 0;
749
750 give_it_back:
751 rxd->ed_lencnt &= ~0xffff; /* zero out length */
752 rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
753 #if 0
754 GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)",
755 rxq->rxq_fi,
756 ((unsigned long *)rxd)[0], ((unsigned long *)rxd)[1],
757 ((unsigned long *)rxd)[2], ((unsigned long *)rxd)[3]));
758 #endif
759 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
760 rxq->rxq_fi * sizeof(*rxd), sizeof(*rxd),
761 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
762 if (++rxq->rxq_fi == GE_RXDESC_MAX)
763 rxq->rxq_fi = 0;
764 rxq->rxq_active++;
765 }
766 rxq->rxq_curpkt = m;
767 GE_FUNC_EXIT(sc, "");
768 }
769
770 uint32_t
771 gfe_rx_process(struct gfe_softc *sc, uint32_t cause, uint32_t intrmask)
772 {
773 struct ifnet * const ifp = &sc->sc_ec.ec_if;
774 struct gfe_rxqueue *rxq;
775 uint32_t rxbits;
776 #define RXPRIO_DECODER 0xffffaa50
777 GE_FUNC_ENTER(sc, "gfe_rx_process");
778
779 rxbits = ETH_IR_RxBuffer_GET(cause);
780 while (rxbits) {
781 enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
782 GE_DPRINTF(sc, ("%1x", rxbits));
783 rxbits &= ~(1 << rxprio);
784 gfe_rx_get(sc, rxprio);
785 }
786
787 rxbits = ETH_IR_RxError_GET(cause);
788 while (rxbits) {
789 enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
790 uint32_t masks[(GE_RXDESC_MAX + 31) / 32];
791 int idx;
792 rxbits &= ~(1 << rxprio);
793 rxq = sc->sc_rxq[rxprio];
794 sc->sc_idlemask |= (rxq->rxq_intrbits & ETH_IR_RxBits);
795 intrmask &= ~(rxq->rxq_intrbits & ETH_IR_RxBits);
796 if ((sc->sc_tickflags & GE_TICK_RX_RESTART) == 0) {
797 sc->sc_tickflags |= GE_TICK_RX_RESTART;
798 callout_reset(&sc->sc_co, 1, gfe_tick, sc);
799 }
800 ifp->if_ierrors++;
801 GE_DPRINTF(sc, ("%s: rx queue %d filled at %u\n",
802 sc->sc_dev.dv_xname, rxprio, rxq->rxq_fi));
803 memset(masks, 0, sizeof(masks));
804 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
805 0, rxq->rxq_desc_mem.gdm_size,
806 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
807 for (idx = 0; idx < GE_RXDESC_MAX; idx++) {
808 volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[idx];
809
810 if (RX_CMD_O & gt32toh(rxd->ed_cmdsts))
811 masks[idx/32] |= 1 << (idx & 31);
812 }
813 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
814 0, rxq->rxq_desc_mem.gdm_size,
815 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
816 #if defined(DEBUG)
817 printf("%s: rx queue %d filled at %u=%#x(%#x/%#x)\n",
818 sc->sc_dev.dv_xname, rxprio, rxq->rxq_fi,
819 rxq->rxq_cmdsts, masks[0], masks[1]);
820 #endif
821 }
822 if ((intrmask & ETH_IR_RxBits) == 0)
823 intrmask &= ~(ETH_IR_RxBuffer|ETH_IR_RxError);
824
825 GE_FUNC_EXIT(sc, "");
826 return intrmask;
827 }
828
829 int
830 gfe_rx_prime(struct gfe_softc *sc)
831 {
832 struct gfe_rxqueue *rxq;
833 int error;
834
835 GE_FUNC_ENTER(sc, "gfe_rx_prime");
836
837 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_HI);
838 if (error)
839 goto bail;
840 rxq = sc->sc_rxq[GE_RXPRIO_HI];
841 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
842 GE_WRITE(sc, EFRDP3, rxq->rxq_desc_busaddr);
843 GE_WRITE(sc, ECRDP3, rxq->rxq_desc_busaddr);
844 }
845 sc->sc_intrmask |= rxq->rxq_intrbits;
846
847 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDHI);
848 if (error)
849 goto bail;
850 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
851 rxq = sc->sc_rxq[GE_RXPRIO_MEDHI];
852 GE_WRITE(sc, EFRDP2, rxq->rxq_desc_busaddr);
853 GE_WRITE(sc, ECRDP2, rxq->rxq_desc_busaddr);
854 sc->sc_intrmask |= rxq->rxq_intrbits;
855 }
856
857 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDLO);
858 if (error)
859 goto bail;
860 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
861 rxq = sc->sc_rxq[GE_RXPRIO_MEDLO];
862 GE_WRITE(sc, EFRDP1, rxq->rxq_desc_busaddr);
863 GE_WRITE(sc, ECRDP1, rxq->rxq_desc_busaddr);
864 sc->sc_intrmask |= rxq->rxq_intrbits;
865 }
866
867 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_LO);
868 if (error)
869 goto bail;
870 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
871 rxq = sc->sc_rxq[GE_RXPRIO_LO];
872 GE_WRITE(sc, EFRDP0, rxq->rxq_desc_busaddr);
873 GE_WRITE(sc, ECRDP0, rxq->rxq_desc_busaddr);
874 sc->sc_intrmask |= rxq->rxq_intrbits;
875 }
876
877 bail:
878 GE_FUNC_EXIT(sc, "");
879 return error;
880 }
881
882 void
883 gfe_rx_cleanup(struct gfe_softc *sc, enum gfe_rxprio rxprio)
884 {
885 struct gfe_rxqueue *rxq = sc->sc_rxq[rxprio];
886 GE_FUNC_ENTER(sc, "gfe_rx_cleanup");
887 if (rxq == NULL) {
888 GE_FUNC_EXIT(sc, "");
889 return;
890 }
891
892 if (rxq->rxq_curpkt)
893 m_freem(rxq->rxq_curpkt);
894 gfe_dmamem_free(sc, &rxq->rxq_desc_mem);
895 gfe_dmamem_free(sc, &rxq->rxq_buf_mem);
896 free(rxq, M_DEVBUF);
897 sc->sc_rxq[rxprio] = NULL;
898 GE_FUNC_EXIT(sc, "");
899 }
900
901 void
902 gfe_rx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
903 {
904 GE_FUNC_ENTER(sc, "gfe_rx_stop");
905 sc->sc_flags &= ~GE_RXACTIVE;
906 sc->sc_idlemask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
907 sc->sc_intrmask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
908 GE_WRITE(sc, EIMR, sc->sc_intrmask);
909 GE_WRITE(sc, ESDCMR, ETH_ESDCMR_AR);
910 do {
911 delay(10);
912 } while (GE_READ(sc, ESDCMR) & ETH_ESDCMR_AR);
913 gfe_rx_cleanup(sc, GE_RXPRIO_HI);
914 gfe_rx_cleanup(sc, GE_RXPRIO_MEDHI);
915 gfe_rx_cleanup(sc, GE_RXPRIO_MEDLO);
916 gfe_rx_cleanup(sc, GE_RXPRIO_LO);
917 GE_FUNC_EXIT(sc, "");
918 }
919
920 void
922 gfe_tick(void *arg)
923 {
924 struct gfe_softc * const sc = arg;
925 uint32_t intrmask;
926 unsigned int tickflags;
927 int s;
928
929 GE_FUNC_ENTER(sc, "gfe_tick");
930
931 s = splnet();
932
933 tickflags = sc->sc_tickflags;
934 sc->sc_tickflags = 0;
935 intrmask = sc->sc_intrmask;
936 if (tickflags & GE_TICK_TX_IFSTART)
937 gfe_ifstart(&sc->sc_ec.ec_if);
938 if (tickflags & GE_TICK_RX_RESTART) {
939 intrmask |= sc->sc_idlemask;
940 if (sc->sc_idlemask & (ETH_IR_RxBuffer_3|ETH_IR_RxError_3)) {
941 struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_HI];
942 rxq->rxq_fi = 0;
943 GE_WRITE(sc, EFRDP3, rxq->rxq_desc_busaddr);
944 GE_WRITE(sc, ECRDP3, rxq->rxq_desc_busaddr);
945 }
946 if (sc->sc_idlemask & (ETH_IR_RxBuffer_2|ETH_IR_RxError_2)) {
947 struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_MEDHI];
948 rxq->rxq_fi = 0;
949 GE_WRITE(sc, EFRDP2, rxq->rxq_desc_busaddr);
950 GE_WRITE(sc, ECRDP2, rxq->rxq_desc_busaddr);
951 }
952 if (sc->sc_idlemask & (ETH_IR_RxBuffer_1|ETH_IR_RxError_1)) {
953 struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_MEDLO];
954 rxq->rxq_fi = 0;
955 GE_WRITE(sc, EFRDP1, rxq->rxq_desc_busaddr);
956 GE_WRITE(sc, ECRDP1, rxq->rxq_desc_busaddr);
957 }
958 if (sc->sc_idlemask & (ETH_IR_RxBuffer_0|ETH_IR_RxError_0)) {
959 struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_LO];
960 rxq->rxq_fi = 0;
961 GE_WRITE(sc, EFRDP0, rxq->rxq_desc_busaddr);
962 GE_WRITE(sc, ECRDP0, rxq->rxq_desc_busaddr);
963 }
964 sc->sc_idlemask = 0;
965 }
966 if (intrmask != sc->sc_intrmask) {
967 sc->sc_intrmask = intrmask;
968 GE_WRITE(sc, EIMR, sc->sc_intrmask);
969 }
970 gfe_intr(sc);
971 splx(s);
972
973 GE_FUNC_EXIT(sc, "");
974 }
975
976 int
977 gfe_tx_enqueue(struct gfe_softc *sc, enum gfe_txprio txprio)
978 {
979 const int dcache_line_size = curcpu()->ci_ci.dcache_line_size;
980 struct ifnet * const ifp = &sc->sc_ec.ec_if;
981 struct gfe_txqueue * const txq = sc->sc_txq[txprio];
982 volatile struct gt_eth_desc * const txd = &txq->txq_descs[txq->txq_lo];
983 uint32_t intrmask = sc->sc_intrmask;
984 struct mbuf *m;
985
986 GE_FUNC_ENTER(sc, "gfe_tx_enqueue");
987
988 /*
989 * Anything in the pending queue to enqueue? if not, punt.
990 * otherwise grab its dmamap.
991 */
992 if ((m = txq->txq_pendq.ifq_head) == NULL) {
993 GE_FUNC_EXIT(sc, "-");
994 return 0;
995 }
996
997 /*
998 * Have we [over]consumed our limit of descriptors?
999 * Do we have enough free descriptors?
1000 */
1001 if (GE_TXDESC_MAX == txq->txq_nactive + 1) {
1002 volatile struct gt_eth_desc * const txd2 = &txq->txq_descs[txq->txq_fi];
1003 uint32_t cmdsts;
1004 size_t pktlen;
1005 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
1006 txq->txq_fi * sizeof(*txd), sizeof(*txd),
1007 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1008 cmdsts = gt32toh(txd2->ed_cmdsts);
1009 if (cmdsts & TX_CMD_O) {
1010 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
1011 txq->txq_fi * sizeof(*txd), sizeof(*txd),
1012 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1013 GE_FUNC_EXIT(sc, "@");
1014 return 0;
1015 }
1016 if (++txq->txq_fi == GE_TXDESC_MAX)
1017 txq->txq_fi = 0;
1018 txq->txq_inptr = gt32toh(txd2->ed_bufptr) - txq->txq_buf_busaddr;
1019 pktlen = (gt32toh(txd2->ed_lencnt) >> 16) & 0xffff;
1020 txq->txq_inptr += roundup(pktlen, dcache_line_size);
1021 txq->txq_nactive--;
1022
1023 /* statistics */
1024 ifp->if_opackets++;
1025 if (cmdsts & TX_STS_ES)
1026 ifp->if_oerrors++;
1027 GE_DPRINTF(sc, ("%%"));
1028 }
1029
1030 /*
1031 * If this packet would wrap around the end of the buffer, reset back
1032 * to the beginning.
1033 */
1034 if (txq->txq_outptr + m->m_pkthdr.len > GE_TXBUF_SIZE) {
1035 txq->txq_ei_gapcount += GE_TXBUF_SIZE - txq->txq_outptr;
1036 txq->txq_outptr = 0;
1037 }
1038
1039 /*
1040 * Make sure the output packet doesn't run over the beginning of
1041 * what we've already given the GT.
1042 */
1043 if (txq->txq_nactive > 0 && txq->txq_outptr <= txq->txq_inptr &&
1044 txq->txq_outptr + m->m_pkthdr.len > txq->txq_inptr) {
1045 intrmask |= txq->txq_intrbits &
1046 (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow);
1047 if (sc->sc_intrmask != intrmask) {
1048 sc->sc_intrmask = intrmask;
1049 GE_WRITE(sc, EIMR, sc->sc_intrmask);
1050 }
1051 GE_FUNC_EXIT(sc, "#");
1052 return 0;
1053 }
1054
1055 /*
1056 * The end-of-list descriptor we put on last time is the starting point
1057 * for this packet. The GT is supposed to terminate list processing on
1058 * a NULL nxtptr but that currently is broken so a CPU-owned descriptor
1059 * must terminate the list.
1060 */
1061 intrmask = sc->sc_intrmask;
1062
1063 m_copydata(m, 0, m->m_pkthdr.len,
1064 txq->txq_buf_mem.gdm_kva + txq->txq_outptr);
1065 bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
1066 txq->txq_outptr, m->m_pkthdr.len, BUS_DMASYNC_PREWRITE);
1067 txd->ed_bufptr = htogt32(txq->txq_buf_busaddr + txq->txq_outptr);
1068 txd->ed_lencnt = htogt32(m->m_pkthdr.len << 16);
1069 #if 0
1070 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
1071 txq->txq_lo * sizeof(*txd), sizeof(*txd),
1072 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1073 #endif
1074
1075 /*
1076 * Request a buffer interrupt every 2/3 of the way thru the transmit
1077 * buffer.
1078 */
1079 txq->txq_ei_gapcount += m->m_pkthdr.len + 7;
1080 if (txq->txq_ei_gapcount > 2 * GE_TXBUF_SIZE / 3) {
1081 txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST|TX_CMD_EI);
1082 txq->txq_ei_gapcount = 0;
1083 } else {
1084 txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST);
1085 }
1086 #if 0
1087 GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)", txq->txq_lo,
1088 ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
1089 ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
1090 #endif
1091 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
1092 txq->txq_lo * sizeof(*txd), sizeof(*txd),
1093 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1094
1095 txq->txq_outptr += roundup(m->m_pkthdr.len, dcache_line_size);
1096 /*
1097 * Tell the SDMA engine to "Fetch!"
1098 */
1099 GE_WRITE(sc, ESDCMR,
1100 txq->txq_esdcmrbits & (ETH_ESDCMR_TXDH|ETH_ESDCMR_TXDL));
1101
1102 GE_DPRINTF(sc, ("(%d)", txq->txq_lo));
1103
1104 /*
1105 * Update the last out appropriately.
1106 */
1107 txq->txq_nactive++;
1108 if (++txq->txq_lo == GE_TXDESC_MAX)
1109 txq->txq_lo = 0;
1110
1111 /*
1112 * Move mbuf from the pending queue to the snd queue.
1113 */
1114 IF_DEQUEUE(&txq->txq_pendq, m);
1115 #if NBPFILTER > 0
1116 if (ifp->if_bpf != NULL)
1117 bpf_mtap(ifp->if_bpf, m);
1118 #endif
1119 m_freem(m);
1120 ifp->if_flags &= ~IFF_OACTIVE;
1121
1122 /*
1123 * Since we have put an item into the packet queue, we now want
1124 * an interrupt when the transmit queue finishes processing the
1125 * list. But only update the mask if needs changing.
1126 */
1127 intrmask |= txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow);
1128 if (sc->sc_intrmask != intrmask) {
1129 sc->sc_intrmask = intrmask;
1130 GE_WRITE(sc, EIMR, sc->sc_intrmask);
1131 }
1132 if (ifp->if_timer == 0)
1133 ifp->if_timer = 5;
1134 GE_FUNC_EXIT(sc, "*");
1135 return 1;
1136 }
1137
1138 uint32_t
1139 gfe_tx_done(struct gfe_softc *sc, enum gfe_txprio txprio, uint32_t intrmask)
1140 {
1141 struct gfe_txqueue * const txq = sc->sc_txq[txprio];
1142 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1143
1144 GE_FUNC_ENTER(sc, "gfe_tx_done");
1145
1146 if (txq == NULL) {
1147 GE_FUNC_EXIT(sc, "");
1148 return intrmask;
1149 }
1150
1151 while (txq->txq_nactive > 0) {
1152 const int dcache_line_size = curcpu()->ci_ci.dcache_line_size;
1153 volatile struct gt_eth_desc *txd = &txq->txq_descs[txq->txq_fi];
1154 uint32_t cmdsts;
1155 size_t pktlen;
1156
1157 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
1158 txq->txq_fi * sizeof(*txd), sizeof(*txd),
1159 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1160 if ((cmdsts = gt32toh(txd->ed_cmdsts)) & TX_CMD_O) {
1161 /*
1162 * If the GT owns this descriptor and according
1163 * to the status register, the transmit engine
1164 * is not running, restart it.
1165 */
1166 #if 0
1167 if ((GE_READ(sc, EPSR) & txq->txq_epsrbits &
1168 (ETH_EPSR_TxHigh|ETH_EPSR_TxLow)) == 0) {
1169 /*
1170 * If the current transmit descriptor isn't
1171 * pointing at this descriptor, then we've
1172 * lost synch, reset it to this one before
1173 * restarting.
1174 */
1175 unsigned int curtxdnum = (
1176 gt_read(sc->sc_dev.dv_parent,
1177 txq->txq_ectdp) -
1178 txq->txq_desc_busaddr) / 16;
1179 if (curtxdnum != txq->txq_fi) {
1180 gt_write(sc->sc_dev.dv_parent,
1181 txq->txq_ectdp,
1182 txq->txq_desc_busaddr +
1183 sizeof(*ed) * txq->txq_fi);
1184 GE_DPRINTF(sc,
1185 ("(oldcur=%d,newcur=fi(%d))",
1186 curtxdnum, txq->txq_fi));
1187 printf("%s: transmitter synchronization"
1188 " lost at %d; repositioning"
1189 " to %d\n",
1190 sc->sc_dev.dv_xname,
1191 curtxdnum, txq->txq_fi);
1192 }
1193 /*
1194 * [Re-] Kick the transmit engine.
1195 */
1196 GE_WRITE(sc, ESDCMR,
1197 txq->txq_esdcmrbits &
1198 (ETH_ESDCMR_TXDH|ETH_ESDCMR_TXDL));
1199 GE_DPRINTF(sc, ("*"));
1200 }
1201 #endif
1202 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
1203 txq->txq_fi * sizeof(*txd), sizeof(*txd),
1204 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1205 GE_FUNC_EXIT(sc, "");
1206 return intrmask;
1207 }
1208 #if 0
1209 GE_DPRINTF(sc, ("([%d]<-%08lx.%08lx.%08lx.%08lx)",
1210 txq->txq_lo,
1211 ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
1212 ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
1213 #endif
1214 GE_DPRINTF(sc, ("(%d)", txq->txq_fi));
1215 if (++txq->txq_fi == GE_TXDESC_MAX)
1216 txq->txq_fi = 0;
1217 txq->txq_inptr = gt32toh(txd->ed_bufptr) - txq->txq_buf_busaddr;
1218 pktlen = (gt32toh(txd->ed_lencnt) >> 16) & 0xffff;
1219 txq->txq_inptr += roundup(pktlen, dcache_line_size);
1220 bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
1221 txq->txq_inptr, pktlen, BUS_DMASYNC_POSTWRITE);
1222
1223 /* statistics */
1224 ifp->if_opackets++;
1225 if (cmdsts & TX_STS_ES)
1226 ifp->if_oerrors++;
1227
1228 txd->ed_bufptr = 0;
1229
1230 ifp->if_timer = 5;
1231 --txq->txq_nactive;
1232 }
1233 if (txq->txq_nactive != 0)
1234 panic("%s: transmit fifo%d empty but active count (%d) > 0!",
1235 sc->sc_dev.dv_xname, txprio, txq->txq_nactive);
1236 ifp->if_timer = 0;
1237 intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow));
1238 intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow));
1239 GE_FUNC_EXIT(sc, "");
1240 return intrmask;
1241 }
1242
1243 int
1244 gfe_tx_start(struct gfe_softc *sc, enum gfe_txprio txprio)
1245 {
1246 struct gfe_txqueue *txq;
1247 volatile struct gt_eth_desc *txd;
1248 unsigned int i;
1249 bus_addr_t addr;
1250
1251 GE_FUNC_ENTER(sc, "gfe_tx_start");
1252
1253 sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1254 ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1255
1256 if ((txq = sc->sc_txq[txprio]) == NULL) {
1257 int error;
1258 txq = (struct gfe_txqueue *) malloc(sizeof(*txq),
1259 M_DEVBUF, M_NOWAIT);
1260 if (txq == NULL) {
1261 GE_FUNC_EXIT(sc, "");
1262 return ENOMEM;
1263 }
1264 memset(txq, 0, sizeof(*txq));
1265 error = gfe_dmamem_alloc(sc, &txq->txq_desc_mem, 1,
1266 GE_TXMEM_SIZE, BUS_DMA_NOCACHE);
1267 if (error) {
1268 free(txq, M_DEVBUF);
1269 GE_FUNC_EXIT(sc, "");
1270 return error;
1271 }
1272 error = gfe_dmamem_alloc(sc, &txq->txq_buf_mem, 1,
1273 GE_TXBUF_SIZE, 0);
1274 if (error) {
1275 gfe_dmamem_free(sc, &txq->txq_desc_mem);
1276 free(txq, M_DEVBUF);
1277 GE_FUNC_EXIT(sc, "");
1278 return error;
1279 }
1280 sc->sc_txq[txprio] = txq;
1281 }
1282
1283 txq->txq_descs =
1284 (volatile struct gt_eth_desc *) txq->txq_desc_mem.gdm_kva;
1285 txq->txq_desc_busaddr = txq->txq_desc_mem.gdm_map->dm_segs[0].ds_addr;
1286 txq->txq_buf_busaddr = txq->txq_buf_mem.gdm_map->dm_segs[0].ds_addr;
1287
1288 txq->txq_pendq.ifq_maxlen = 10;
1289 txq->txq_ei_gapcount = 0;
1290 txq->txq_nactive = 0;
1291 txq->txq_fi = 0;
1292 txq->txq_lo = 0;
1293 txq->txq_inptr = GE_TXBUF_SIZE;
1294 txq->txq_outptr = 0;
1295 for (i = 0, txd = txq->txq_descs,
1296 addr = txq->txq_desc_busaddr + sizeof(*txd);
1297 i < GE_TXDESC_MAX - 1;
1298 i++, txd++, addr += sizeof(*txd)) {
1299 /*
1300 * update the nxtptr to point to the next txd.
1301 */
1302 txd->ed_cmdsts = 0;
1303 txd->ed_nxtptr = htogt32(addr);
1304 }
1305 txq->txq_descs[GE_TXDESC_MAX-1].ed_nxtptr =
1306 htogt32(txq->txq_desc_busaddr);
1307 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map, 0,
1308 GE_TXMEM_SIZE, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1309
1310 switch (txprio) {
1311 case GE_TXPRIO_HI:
1312 txq->txq_intrbits = ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh;
1313 txq->txq_esdcmrbits = ETH_ESDCMR_TXDH;
1314 txq->txq_epsrbits = ETH_EPSR_TxHigh;
1315 txq->txq_ectdp = ETH_ECTDP1(sc->sc_macno);
1316 GE_WRITE(sc, ECTDP1, txq->txq_desc_busaddr);
1317 break;
1318
1319 case GE_TXPRIO_LO:
1320 txq->txq_intrbits = ETH_IR_TxEndLow|ETH_IR_TxBufferLow;
1321 txq->txq_esdcmrbits = ETH_ESDCMR_TXDL;
1322 txq->txq_epsrbits = ETH_EPSR_TxLow;
1323 txq->txq_ectdp = ETH_ECTDP0(sc->sc_macno);
1324 GE_WRITE(sc, ECTDP0, txq->txq_desc_busaddr);
1325 break;
1326
1327 case GE_TXPRIO_NONE:
1328 break;
1329 }
1330 #if 0
1331 GE_DPRINTF(sc, ("(ectdp=%#x", txq->txq_ectdp));
1332 gt_write(sc->sc_dev.dv_parent, txq->txq_ectdp, txq->txq_desc_busaddr);
1333 GE_DPRINTF(sc, (")"));
1334 #endif
1335
1336 /*
1337 * If we are restarting, there may be packets in the pending queue
1338 * waiting to be enqueued. Try enqueuing packets from both priority
1339 * queues until the pending queue is empty or there no room for them
1340 * on the device.
1341 */
1342 while (gfe_tx_enqueue(sc, txprio))
1343 continue;
1344
1345 GE_FUNC_EXIT(sc, "");
1346 return 0;
1347 }
1348
1349 void
1350 gfe_tx_cleanup(struct gfe_softc *sc, enum gfe_txprio txprio, int flush)
1351 {
1352 struct gfe_txqueue * const txq = sc->sc_txq[txprio];
1353
1354 GE_FUNC_ENTER(sc, "gfe_tx_cleanup");
1355 if (txq == NULL) {
1356 GE_FUNC_EXIT(sc, "");
1357 return;
1358 }
1359
1360 if (!flush) {
1361 GE_FUNC_EXIT(sc, "");
1362 return;
1363 }
1364
1365 gfe_dmamem_free(sc, &txq->txq_desc_mem);
1366 gfe_dmamem_free(sc, &txq->txq_buf_mem);
1367 free(txq, M_DEVBUF);
1368 sc->sc_txq[txprio] = NULL;
1369 GE_FUNC_EXIT(sc, "-F");
1370 }
1371
1372 void
1373 gfe_tx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
1374 {
1375 GE_FUNC_ENTER(sc, "gfe_tx_stop");
1376
1377 GE_WRITE(sc, ESDCMR, ETH_ESDCMR_STDH|ETH_ESDCMR_STDL);
1378
1379 sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, sc->sc_intrmask);
1380 sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, sc->sc_intrmask);
1381 sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1382 ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1383
1384 gfe_tx_cleanup(sc, GE_TXPRIO_HI, op == GE_WHACK_STOP);
1385 gfe_tx_cleanup(sc, GE_TXPRIO_LO, op == GE_WHACK_STOP);
1386
1387 sc->sc_ec.ec_if.if_timer = 0;
1388 GE_FUNC_EXIT(sc, "");
1389 }
1390
1391 int
1393 gfe_intr(void *arg)
1394 {
1395 struct gfe_softc * const sc = arg;
1396 uint32_t cause;
1397 uint32_t intrmask = sc->sc_intrmask;
1398 int claim = 0;
1399 int cnt;
1400
1401 GE_FUNC_ENTER(sc, "gfe_intr");
1402
1403 for (cnt = 0; cnt < 4; cnt++) {
1404 if (sc->sc_intrmask != intrmask) {
1405 sc->sc_intrmask = intrmask;
1406 GE_WRITE(sc, EIMR, sc->sc_intrmask);
1407 }
1408 cause = GE_READ(sc, EICR);
1409 cause &= sc->sc_intrmask;
1410 GE_DPRINTF(sc, (".%#x", cause));
1411 if (cause == 0)
1412 break;
1413
1414 claim = 1;
1415
1416 GE_WRITE(sc, EICR, ~cause);
1417 #ifndef GE_NORX
1418 if (cause & (ETH_IR_RxBuffer|ETH_IR_RxError))
1419 intrmask = gfe_rx_process(sc, cause, intrmask);
1420 #endif
1421
1422 #ifndef GE_NOTX
1423 if (cause & (ETH_IR_TxBufferHigh|ETH_IR_TxEndHigh))
1424 intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, intrmask);
1425 if (cause & (ETH_IR_TxBufferLow|ETH_IR_TxEndLow))
1426 intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, intrmask);
1427 #endif
1428 if (cause & ETH_IR_MIIPhySTC) {
1429 sc->sc_flags |= GE_PHYSTSCHG;
1430 /* intrmask &= ~ETH_IR_MIIPhySTC; */
1431 }
1432 }
1433
1434 GE_FUNC_EXIT(sc, "");
1435 return claim;
1436 }
1437
1438 int
1440 gfe_mii_mediachange (struct ifnet *ifp)
1441 {
1442 struct gfe_softc *sc = ifp->if_softc;
1443
1444 if (ifp->if_flags & IFF_UP)
1445 mii_mediachg(&sc->sc_mii);
1446
1447 return (0);
1448 }
1449 void
1450 gfe_mii_mediastatus (struct ifnet *ifp, struct ifmediareq *ifmr)
1451 {
1452 struct gfe_softc *sc = ifp->if_softc;
1453
1454 if (sc->sc_flags & GE_PHYSTSCHG) {
1455 sc->sc_flags &= ~GE_PHYSTSCHG;
1456 mii_pollstat(&sc->sc_mii);
1457 }
1458 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1459 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1460 }
1461
1462 int
1463 gfe_mii_read (struct device *self, int phy, int reg)
1464 {
1465 return gt_mii_read(self, self->dv_parent, phy, reg);
1466 }
1467
1468 void
1469 gfe_mii_write (struct device *self, int phy, int reg, int value)
1470 {
1471 gt_mii_write(self, self->dv_parent, phy, reg, value);
1472 }
1473
1474 void
1475 gfe_mii_statchg (struct device *self)
1476 {
1477 /* struct gfe_softc *sc = (struct gfe_softc *) self; */
1478 /* do nothing? */
1479 }
1480
1481 int
1483 gfe_whack(struct gfe_softc *sc, enum gfe_whack_op op)
1484 {
1485 int error = 0;
1486 GE_FUNC_ENTER(sc, "gfe_whack");
1487
1488 switch (op) {
1489 case GE_WHACK_RESTART:
1490 #ifndef GE_NOTX
1491 gfe_tx_stop(sc, op);
1492 #endif
1493 /* sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING; */
1494 /* FALLTHROUGH */
1495 case GE_WHACK_START:
1496 #ifndef GE_NOHASH
1497 if (error == 0 && sc->sc_hashtable == NULL) {
1498 error = gfe_hash_alloc(sc);
1499 if (error)
1500 break;
1501 }
1502 if (op != GE_WHACK_RESTART)
1503 gfe_hash_fill(sc);
1504 #endif
1505 #ifndef GE_NORX
1506 if (op != GE_WHACK_RESTART) {
1507 error = gfe_rx_prime(sc);
1508 if (error)
1509 break;
1510 }
1511 #endif
1512 #ifndef GE_NOTX
1513 error = gfe_tx_start(sc, GE_TXPRIO_HI);
1514 if (error)
1515 break;
1516 #endif
1517 sc->sc_ec.ec_if.if_flags |= IFF_RUNNING;
1518 GE_WRITE(sc, EPCR, sc->sc_pcr | ETH_EPCR_EN);
1519 GE_WRITE(sc, EPCXR, sc->sc_pcxr);
1520 GE_WRITE(sc, EICR, 0);
1521 GE_WRITE(sc, EIMR, sc->sc_intrmask);
1522 #ifndef GE_NOHASH
1523 GE_WRITE(sc, EHTPR, sc->sc_hash_mem.gdm_map->dm_segs->ds_addr);
1524 #endif
1525 #ifndef GE_NORX
1526 GE_WRITE(sc, ESDCMR, ETH_ESDCMR_ERD);
1527 sc->sc_flags |= GE_RXACTIVE;
1528 #endif
1529 /* FALLTHROUGH */
1530 case GE_WHACK_CHANGE:
1531 GE_DPRINTF(sc, ("(pcr=%#x,imr=%#x)",
1532 GE_READ(sc, EPCR), GE_READ(sc, EIMR)));
1533 GE_WRITE(sc, EPCR, sc->sc_pcr | ETH_EPCR_EN);
1534 GE_WRITE(sc, EIMR, sc->sc_intrmask);
1535 gfe_ifstart(&sc->sc_ec.ec_if);
1536 GE_DPRINTF(sc, ("(ectdp0=%#x, ectdp1=%#x)",
1537 GE_READ(sc, ECTDP0), GE_READ(sc, ECTDP1)));
1538 GE_FUNC_EXIT(sc, "");
1539 return error;
1540 case GE_WHACK_STOP:
1541 break;
1542 }
1543
1544 #ifdef GE_DEBUG
1545 if (error)
1546 GE_DPRINTF(sc, (" failed: %d\n", error));
1547 #endif
1548 GE_WRITE(sc, EPCR, sc->sc_pcr);
1549 GE_WRITE(sc, EIMR, 0);
1550 sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING;
1551 #ifndef GE_NOTX
1552 gfe_tx_stop(sc, GE_WHACK_STOP);
1553 #endif
1554 #ifndef GE_NORX
1555 gfe_rx_stop(sc, GE_WHACK_STOP);
1556 #endif
1557 #ifndef GE_NOHASH
1558 gfe_dmamem_free(sc, &sc->sc_hash_mem);
1559 sc->sc_hashtable = NULL;
1560 #endif
1561
1562 GE_FUNC_EXIT(sc, "");
1563 return error;
1564 }
1565
1566 int
1568 gfe_hash_compute(struct gfe_softc *sc, const uint8_t eaddr[ETHER_ADDR_LEN])
1569 {
1570 uint32_t w0, add0, add1;
1571 uint32_t result;
1572
1573 GE_FUNC_ENTER(sc, "gfe_hash_compute");
1574 add0 = ((uint32_t) eaddr[5] << 0) |
1575 ((uint32_t) eaddr[4] << 8) |
1576 ((uint32_t) eaddr[3] << 16);
1577
1578 add0 = ((add0 & 0x00f0f0f0) >> 4) | ((add0 & 0x000f0f0f) << 4);
1579 add0 = ((add0 & 0x00cccccc) >> 2) | ((add0 & 0x00333333) << 2);
1580 add0 = ((add0 & 0x00aaaaaa) >> 1) | ((add0 & 0x00555555) << 1);
1581
1582 add1 = ((uint32_t) eaddr[2] << 0) |
1583 ((uint32_t) eaddr[1] << 8) |
1584 ((uint32_t) eaddr[0] << 16);
1585
1586 add1 = ((add1 & 0x00f0f0f0) >> 4) | ((add1 & 0x000f0f0f) << 4);
1587 add1 = ((add1 & 0x00cccccc) >> 2) | ((add1 & 0x00333333) << 2);
1588 add1 = ((add1 & 0x00aaaaaa) >> 1) | ((add1 & 0x00555555) << 1);
1589
1590 GE_DPRINTF(sc, ("%s=", ether_sprintf(eaddr)));
1591 /*
1592 * hashResult is the 15 bits Hash entry address.
1593 * ethernetADD is a 48 bit number, which is derived from the Ethernet
1594 * MAC address, by nibble swapping in every byte (i.e MAC address
1595 * of 0x123456789abc translates to ethernetADD of 0x21436587a9cb).
1596 */
1597
1598 if ((sc->sc_pcr & ETH_EPCR_HM) == 0) {
1599 /*
1600 * hashResult[14:0] = hashFunc0(ethernetADD[47:0])
1601 *
1602 * hashFunc0 calculates the hashResult in the following manner:
1603 * hashResult[ 8:0] = ethernetADD[14:8,1,0]
1604 * XOR ethernetADD[23:15] XOR ethernetADD[32:24]
1605 */
1606 result = (add0 & 3) | ((add0 >> 6) & ~3);
1607 result ^= (add0 >> 15) ^ (add1 >> 0);
1608 result &= 0x1ff;
1609 /*
1610 * hashResult[14:9] = ethernetADD[7:2]
1611 */
1612 result |= (add0 & ~3) << 7; /* excess bits will be masked */
1613 GE_DPRINTF(sc, ("0(%#x)", result & 0x7fff));
1614 } else {
1615 #define TRIBITFLIP 073516240 /* yes its in octal */
1616 /*
1617 * hashResult[14:0] = hashFunc1(ethernetADD[47:0])
1618 *
1619 * hashFunc1 calculates the hashResult in the following manner:
1620 * hashResult[08:00] = ethernetADD[06:14]
1621 * XOR ethernetADD[15:23] XOR ethernetADD[24:32]
1622 */
1623 w0 = ((add0 >> 6) ^ (add0 >> 15) ^ (add1)) & 0x1ff;
1624 /*
1625 * Now bitswap those 9 bits
1626 */
1627 result = 0;
1628 result |= ((TRIBITFLIP >> (((w0 >> 0) & 7) * 3)) & 7) << 6;
1629 result |= ((TRIBITFLIP >> (((w0 >> 3) & 7) * 3)) & 7) << 3;
1630 result |= ((TRIBITFLIP >> (((w0 >> 6) & 7) * 3)) & 7) << 0;
1631
1632 /*
1633 * hashResult[14:09] = ethernetADD[00:05]
1634 */
1635 result |= ((TRIBITFLIP >> (((add0 >> 0) & 7) * 3)) & 7) << 12;
1636 result |= ((TRIBITFLIP >> (((add0 >> 3) & 7) * 3)) & 7) << 9;
1637 GE_DPRINTF(sc, ("1(%#x)", result));
1638 }
1639 GE_FUNC_EXIT(sc, "");
1640 return result & ((sc->sc_pcr & ETH_EPCR_HS_512) ? 0x7ff : 0x7fff);
1641 }
1642
1643 int
1644 gfe_hash_entry_op(struct gfe_softc *sc, enum gfe_hash_op op,
1645 enum gfe_rxprio prio, const u_int8_t eaddr[ETHER_ADDR_LEN])
1646 {
1647 uint64_t he;
1648 uint64_t *maybe_he_p = NULL;
1649 int limit;
1650 int hash;
1651 int maybe_hash = 0;
1652
1653 GE_FUNC_ENTER(sc, "gfe_hash_entry_op");
1654
1655 hash = gfe_hash_compute(sc, eaddr);
1656
1657 if (sc->sc_hashtable == NULL) {
1658 panic("%s:%d: hashtable == NULL!", sc->sc_dev.dv_xname,
1659 __LINE__);
1660 }
1661
1662 /*
1663 * Assume we are going to insert so create the hash entry we
1664 * are going to insert. We also use it to match entries we
1665 * will be removing.
1666 */
1667 he = ((uint64_t) eaddr[5] << 43) |
1668 ((uint64_t) eaddr[4] << 35) |
1669 ((uint64_t) eaddr[3] << 27) |
1670 ((uint64_t) eaddr[2] << 19) |
1671 ((uint64_t) eaddr[1] << 11) |
1672 ((uint64_t) eaddr[0] << 3) |
1673 HSH_PRIO_INS(prio) | HSH_V | HSH_R;
1674
1675 /*
1676 * The GT will search upto 12 entries for a hit, so we must mimic that.
1677 */
1678 hash &= sc->sc_hashmask / sizeof(he);
1679 for (limit = HSH_LIMIT; limit > 0 ; --limit) {
1680 /*
1681 * Does the GT wrap at the end, stop at the, or overrun the
1682 * end? Assume it wraps for now. Stash a copy of the
1683 * current hash entry.
1684 */
1685 uint64_t *he_p = &sc->sc_hashtable[hash];
1686 uint64_t thishe = *he_p;
1687
1688 /*
1689 * If the hash entry isn't valid, that break the chain. And
1690 * this entry a good candidate for reuse.
1691 */
1692 if ((thishe & HSH_V) == 0) {
1693 maybe_he_p = he_p;
1694 break;
1695 }
1696
1697 /*
1698 * If the hash entry has the same address we are looking for
1699 * then ... if we are removing and the skip bit is set, its
1700 * already been removed. if are adding and the skip bit is
1701 * clear, then its already added. In either return EBUSY
1702 * indicating the op has already been done. Otherwise flip
1703 * the skip bit and return 0.
1704 */
1705 if (((he ^ thishe) & HSH_ADDR_MASK) == 0) {
1706 if (((op == GE_HASH_REMOVE) && (thishe & HSH_S)) ||
1707 ((op == GE_HASH_ADD) && (thishe & HSH_S) == 0))
1708 return EBUSY;
1709 *he_p = thishe ^ HSH_S;
1710 bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1711 hash * sizeof(he), sizeof(he),
1712 BUS_DMASYNC_PREWRITE);
1713 GE_FUNC_EXIT(sc, "^");
1714 return 0;
1715 }
1716
1717 /*
1718 * If we haven't found a slot for the entry and this entry
1719 * is currently being skipped, return this entry.
1720 */
1721 if (maybe_he_p == NULL && (thishe & HSH_S)) {
1722 maybe_he_p = he_p;
1723 maybe_hash = hash;
1724 }
1725
1726 hash = (hash + 1) & (sc->sc_hashmask / sizeof(he));
1727 }
1728
1729 /*
1730 * If we got here, then there was no entry to remove.
1731 */
1732 if (op == GE_HASH_REMOVE) {
1733 GE_FUNC_EXIT(sc, "?");
1734 return ENOENT;
1735 }
1736
1737 /*
1738 * If we couldn't find a slot, return an error.
1739 */
1740 if (maybe_he_p == NULL) {
1741 GE_FUNC_EXIT(sc, "!");
1742 return ENOSPC;
1743 }
1744
1745 /* Update the entry.
1746 */
1747 *maybe_he_p = he;
1748 bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1749 maybe_hash * sizeof(he), sizeof(he), BUS_DMASYNC_PREWRITE);
1750 GE_FUNC_EXIT(sc, "+");
1751 return 0;
1752 }
1753
1754 int
1755 gfe_hash_multichg(struct ethercom *ec, const struct ether_multi *enm, u_long cmd)
1756 {
1757 struct gfe_softc * const sc = ec->ec_if.if_softc;
1758 int error;
1759 enum gfe_hash_op op;
1760 enum gfe_rxprio prio;
1761
1762 GE_FUNC_ENTER(sc, "hash_multichg");
1763 /*
1764 * Is this a wildcard entry? If so and its being removed, recompute.
1765 */
1766 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
1767 if (cmd == SIOCDELMULTI) {
1768 GE_FUNC_EXIT(sc, "");
1769 return ENETRESET;
1770 }
1771
1772 /*
1773 * Switch in
1774 */
1775 sc->sc_flags |= GE_ALLMULTI;
1776 if ((sc->sc_pcr & ETH_EPCR_PM) == 0) {
1777 sc->sc_pcr |= ETH_EPCR_PM;
1778 GE_WRITE(sc, EPCR, sc->sc_pcr);
1779 GE_FUNC_EXIT(sc, "");
1780 return 0;
1781 }
1782 GE_FUNC_EXIT(sc, "");
1783 return ENETRESET;
1784 }
1785
1786 prio = GE_RXPRIO_MEDLO;
1787 op = (cmd == SIOCDELMULTI ? GE_HASH_REMOVE : GE_HASH_ADD);
1788
1789 if (sc->sc_hashtable == NULL) {
1790 GE_FUNC_EXIT(sc, "");
1791 return 0;
1792 }
1793
1794 error = gfe_hash_entry_op(sc, op, prio, enm->enm_addrlo);
1795 if (error == EBUSY) {
1796 printf("%s: multichg: tried to %s %s again\n",
1797 sc->sc_dev.dv_xname,
1798 cmd == SIOCDELMULTI ? "remove" : "add",
1799 ether_sprintf(enm->enm_addrlo));
1800 GE_FUNC_EXIT(sc, "");
1801 return 0;
1802 }
1803
1804 if (error == ENOENT) {
1805 printf("%s: multichg: failed to remove %s: not in table\n",
1806 sc->sc_dev.dv_xname,
1807 ether_sprintf(enm->enm_addrlo));
1808 GE_FUNC_EXIT(sc, "");
1809 return 0;
1810 }
1811
1812 if (error == ENOSPC) {
1813 printf("%s: multichg: failed to add %s: no space; regenerating table\n",
1814 sc->sc_dev.dv_xname,
1815 ether_sprintf(enm->enm_addrlo));
1816 GE_FUNC_EXIT(sc, "");
1817 return ENETRESET;
1818 }
1819 GE_DPRINTF(sc, ("%s: multichg: %s: %s succeeded\n",
1820 sc->sc_dev.dv_xname,
1821 cmd == SIOCDELMULTI ? "remove" : "add",
1822 ether_sprintf(enm->enm_addrlo)));
1823 GE_FUNC_EXIT(sc, "");
1824 return 0;
1825 }
1826
1827 int
1828 gfe_hash_fill(struct gfe_softc *sc)
1829 {
1830 struct ether_multistep step;
1831 struct ether_multi *enm;
1832 int error;
1833
1834 GE_FUNC_ENTER(sc, "gfe_hash_fill");
1835
1836 error = gfe_hash_entry_op(sc, GE_HASH_ADD, GE_RXPRIO_HI,
1837 LLADDR(sc->sc_ec.ec_if.if_sadl));
1838 if (error)
1839 GE_FUNC_EXIT(sc, "!");
1840 return error;
1841
1842 sc->sc_flags &= ~GE_ALLMULTI;
1843 if ((sc->sc_ec.ec_if.if_flags & IFF_PROMISC) == 0)
1844 sc->sc_pcr &= ~ETH_EPCR_PM;
1845 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
1846 while (enm != NULL) {
1847 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1848 sc->sc_flags |= GE_ALLMULTI;
1849 sc->sc_pcr |= ETH_EPCR_PM;
1850 } else {
1851 error = gfe_hash_entry_op(sc, GE_HASH_ADD,
1852 GE_RXPRIO_MEDLO, enm->enm_addrlo);
1853 if (error == ENOSPC)
1854 break;
1855 }
1856 ETHER_NEXT_MULTI(step, enm);
1857 }
1858
1859 GE_FUNC_EXIT(sc, "");
1860 return error;
1861 }
1862
1863 int
1864 gfe_hash_alloc(struct gfe_softc *sc)
1865 {
1866 int error;
1867 GE_FUNC_ENTER(sc, "gfe_hash_alloc");
1868 sc->sc_hashmask = (sc->sc_pcr & ETH_EPCR_HS_512 ? 16 : 256)*1024 - 1;
1869 error = gfe_dmamem_alloc(sc, &sc->sc_hash_mem, 1, sc->sc_hashmask + 1,
1870 BUS_DMA_NOCACHE);
1871 if (error) {
1872 printf("%s: failed to allocate %d bytes for hash table: %d\n",
1873 sc->sc_dev.dv_xname, sc->sc_hashmask + 1, error);
1874 GE_FUNC_EXIT(sc, "");
1875 return error;
1876 }
1877 sc->sc_hashtable = (uint64_t *) sc->sc_hash_mem.gdm_kva;
1878 memset(sc->sc_hashtable, 0, sc->sc_hashmask + 1);
1879 bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1880 0, sc->sc_hashmask + 1, BUS_DMASYNC_PREWRITE);
1881 GE_FUNC_EXIT(sc, "");
1882 return 0;
1883 }
1884