gem.c revision 1.2 1 /* $NetBSD: gem.c,v 1.2 2001/10/01 23:39:57 eeh Exp $ */
2
3 /*
4 *
5 * Copyright (C) 2001 Eduardo Horvath.
6 * All rights reserved.
7 *
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 /*
33 * Driver for Sun GEM ethernet controllers.
34 */
35
36 #define GEM_DEBUG
37 int gem_opdebug = 0;
38
39 #include "bpfilter.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/callout.h>
44 #include <sys/mbuf.h>
45 #include <sys/syslog.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/device.h>
52
53 #include <machine/endian.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
61
62 #if NBPFILTER > 0
63 #include <net/bpf.h>
64 #endif
65
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68
69 #include <dev/mii/mii.h>
70 #include <dev/mii/miivar.h>
71 #include <dev/mii/mii_bitbang.h>
72
73 #include <dev/ic/gemreg.h>
74 #include <dev/ic/gemvar.h>
75
76 #define TRIES 10000
77
78 void gem_start __P((struct ifnet *));
79 void gem_stop __P((struct ifnet *, int));
80 int gem_ioctl __P((struct ifnet *, u_long, caddr_t));
81 void gem_tick __P((void *));
82 void gem_watchdog __P((struct ifnet *));
83 void gem_shutdown __P((void *));
84 int gem_init __P((struct ifnet *));
85 void gem_init_regs(struct gem_softc *sc);
86 static int gem_ringsize(int sz);
87 int gem_meminit __P((struct gem_softc *));
88 void gem_mifinit __P((struct gem_softc *));
89 void gem_reset __P((struct gem_softc *));
90 int gem_reset_rx(struct gem_softc *sc);
91 int gem_reset_tx(struct gem_softc *sc);
92 int gem_disable_rx(struct gem_softc *sc);
93 int gem_disable_tx(struct gem_softc *sc);
94 void gem_rxdrain(struct gem_softc *sc);
95 int gem_add_rxbuf(struct gem_softc *sc, int idx);
96 void gem_setladrf __P((struct gem_softc *));
97
98 /* MII methods & callbacks */
99 static int gem_mii_readreg __P((struct device *, int, int));
100 static void gem_mii_writereg __P((struct device *, int, int, int));
101 static void gem_mii_statchg __P((struct device *));
102
103 int gem_mediachange __P((struct ifnet *));
104 void gem_mediastatus __P((struct ifnet *, struct ifmediareq *));
105
106 struct mbuf *gem_get __P((struct gem_softc *, int, int));
107 int gem_put __P((struct gem_softc *, int, struct mbuf *));
108 void gem_read __P((struct gem_softc *, int, int));
109 int gem_eint __P((struct gem_softc *, u_int));
110 int gem_rint __P((struct gem_softc *));
111 int gem_tint __P((struct gem_softc *));
112 void gem_power __P((int, void *));
113
114 static int ether_cmp __P((u_char *, u_char *));
115
116 /* Default buffer copy routines */
117 void gem_copytobuf_contig __P((struct gem_softc *, void *, int, int));
118 void gem_copyfrombuf_contig __P((struct gem_softc *, void *, int, int));
119 void gem_zerobuf_contig __P((struct gem_softc *, int, int));
120
121
122 #ifdef GEM_DEBUG
123 #define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
124 printf x
125 #else
126 #define DPRINTF(sc, x) /* nothing */
127 #endif
128
129
130 /*
131 * gem_config:
132 *
133 * Attach a Gem interface to the system.
134 */
135 void
136 gem_config(sc)
137 struct gem_softc *sc;
138 {
139 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
140 struct mii_data *mii = &sc->sc_mii;
141 struct mii_softc *child;
142 int i, error;
143
144 /* Make sure the chip is stopped. */
145 ifp->if_softc = sc;
146 gem_reset(sc);
147
148 /*
149 * Allocate the control data structures, and create and load the
150 * DMA map for it.
151 */
152 if ((error = bus_dmamem_alloc(sc->sc_dmatag,
153 sizeof(struct gem_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
154 1, &sc->sc_cdnseg, 0)) != 0) {
155 printf("%s: unable to allocate control data, error = %d\n",
156 sc->sc_dev.dv_xname, error);
157 goto fail_0;
158 }
159
160 /* XXX should map this in with correct endianness */
161 if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
162 sizeof(struct gem_control_data), (caddr_t *)&sc->sc_control_data,
163 BUS_DMA_COHERENT)) != 0) {
164 printf("%s: unable to map control data, error = %d\n",
165 sc->sc_dev.dv_xname, error);
166 goto fail_1;
167 }
168
169 if ((error = bus_dmamap_create(sc->sc_dmatag,
170 sizeof(struct gem_control_data), 1,
171 sizeof(struct gem_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
172 printf("%s: unable to create control data DMA map, "
173 "error = %d\n", sc->sc_dev.dv_xname, error);
174 goto fail_2;
175 }
176
177 if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
178 sc->sc_control_data, sizeof(struct gem_control_data), NULL,
179 0)) != 0) {
180 printf("%s: unable to load control data DMA map, error = %d\n",
181 sc->sc_dev.dv_xname, error);
182 goto fail_3;
183 }
184
185 /*
186 * Initialize the transmit job descriptors.
187 */
188 SIMPLEQ_INIT(&sc->sc_txfreeq);
189 SIMPLEQ_INIT(&sc->sc_txdirtyq);
190
191 /*
192 * Create the transmit buffer DMA maps.
193 */
194 for (i = 0; i < GEM_TXQUEUELEN; i++) {
195 struct gem_txsoft *txs;
196
197 txs = &sc->sc_txsoft[i];
198 txs->txs_mbuf = NULL;
199 if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES,
200 GEM_NTXSEGS, MCLBYTES, 0, 0,
201 &txs->txs_dmamap)) != 0) {
202 printf("%s: unable to create tx DMA map %d, "
203 "error = %d\n", sc->sc_dev.dv_xname, i, error);
204 goto fail_4;
205 }
206 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
207 }
208
209 /*
210 * Create the receive buffer DMA maps.
211 */
212 for (i = 0; i < GEM_NRXDESC; i++) {
213 if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
214 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
215 printf("%s: unable to create rx DMA map %d, "
216 "error = %d\n", sc->sc_dev.dv_xname, i, error);
217 goto fail_5;
218 }
219 sc->sc_rxsoft[i].rxs_mbuf = NULL;
220 }
221
222 /*
223 * From this point forward, the attachment cannot fail. A failure
224 * before this point releases all resources that may have been
225 * allocated.
226 */
227
228 /* Announce ourselves. */
229 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
230 ether_sprintf(sc->sc_enaddr));
231
232 /* Initialize ifnet structure. */
233 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
234 ifp->if_softc = sc;
235 ifp->if_flags =
236 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
237 ifp->if_start = gem_start;
238 ifp->if_ioctl = gem_ioctl;
239 ifp->if_watchdog = gem_watchdog;
240 ifp->if_stop = gem_stop;
241 ifp->if_init = gem_init;
242 IFQ_SET_READY(&ifp->if_snd);
243
244 /* Initialize ifmedia structures and MII info */
245 mii->mii_ifp = ifp;
246 mii->mii_readreg = gem_mii_readreg;
247 mii->mii_writereg = gem_mii_writereg;
248 mii->mii_statchg = gem_mii_statchg;
249
250 ifmedia_init(&mii->mii_media, 0, gem_mediachange, gem_mediastatus);
251
252 gem_mifinit(sc);
253
254 mii_attach(&sc->sc_dev, mii, 0xffffffff,
255 MII_PHY_ANY, MII_OFFSET_ANY, 0);
256
257 child = LIST_FIRST(&mii->mii_phys);
258 if (child == NULL) {
259 /* No PHY attached */
260 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
261 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
262 } else {
263 /*
264 * Walk along the list of attached MII devices and
265 * establish an `MII instance' to `phy number'
266 * mapping. We'll use this mapping in media change
267 * requests to determine which phy to use to program
268 * the MIF configuration register.
269 */
270 for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
271 /*
272 * Note: we support just two PHYs: the built-in
273 * internal device and an external on the MII
274 * connector.
275 */
276 if (child->mii_phy > 1 || child->mii_inst > 1) {
277 printf("%s: cannot accomodate MII device %s"
278 " at phy %d, instance %d\n",
279 sc->sc_dev.dv_xname,
280 child->mii_dev.dv_xname,
281 child->mii_phy, child->mii_inst);
282 continue;
283 }
284
285 sc->sc_phys[child->mii_inst] = child->mii_phy;
286 }
287
288 /*
289 * Now select and activate the PHY we will use.
290 *
291 * The order of preference is External (MDI1),
292 * Internal (MDI0), Serial Link (no MII).
293 */
294 if (sc->sc_phys[1]) {
295 #ifdef DEBUG
296 printf("using external phy\n");
297 #endif
298 sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
299 } else {
300 #ifdef DEBUG
301 printf("using internal phy\n");
302 #endif
303 sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
304 }
305 bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG,
306 sc->sc_mif_config);
307
308 /*
309 * XXX - we can really do the following ONLY if the
310 * phy indeed has the auto negotiation capability!!
311 */
312 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
313 }
314
315 /* claim 802.1q capability */
316 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
317
318 /* Attach the interface. */
319 if_attach(ifp);
320 ether_ifattach(ifp, sc->sc_enaddr);
321
322 sc->sc_sh = shutdownhook_establish(gem_shutdown, sc);
323 if (sc->sc_sh == NULL)
324 panic("gem_config: can't establish shutdownhook");
325
326 #if NRND > 0
327 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
328 RND_TYPE_NET, 0);
329 #endif
330
331
332 #if notyet
333 /*
334 * Add a suspend hook to make sure we come back up after a
335 * resume.
336 */
337 sc->sc_powerhook = powerhook_establish(gem_power, sc);
338 if (sc->sc_powerhook == NULL)
339 printf("%s: WARNING: unable to establish power hook\n",
340 sc->sc_dev.dv_xname);
341 #endif
342
343 callout_init(&sc->sc_tick_ch);
344 return;
345
346 /*
347 * Free any resources we've allocated during the failed attach
348 * attempt. Do this in reverse order and fall through.
349 */
350 fail_5:
351 for (i = 0; i < GEM_NRXDESC; i++) {
352 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
353 bus_dmamap_destroy(sc->sc_dmatag,
354 sc->sc_rxsoft[i].rxs_dmamap);
355 }
356 fail_4:
357 for (i = 0; i < GEM_TXQUEUELEN; i++) {
358 if (sc->sc_txsoft[i].txs_dmamap != NULL)
359 bus_dmamap_destroy(sc->sc_dmatag,
360 sc->sc_txsoft[i].txs_dmamap);
361 }
362 bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
363 fail_3:
364 bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
365 fail_2:
366 bus_dmamem_unmap(sc->sc_dmatag, (caddr_t)sc->sc_control_data,
367 sizeof(struct gem_control_data));
368 fail_1:
369 bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
370 fail_0:
371 return;
372 }
373
374
375 void
376 gem_tick(arg)
377 void *arg;
378 {
379 struct gem_softc *sc = arg;
380 int s;
381
382 s = splnet();
383 mii_tick(&sc->sc_mii);
384 splx(s);
385
386 callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
387
388 }
389
390 void
391 gem_reset(sc)
392 struct gem_softc *sc;
393 {
394 bus_space_tag_t t = sc->sc_bustag;
395 bus_space_handle_t h = sc->sc_h;
396 int i;
397 int s;
398
399 s = splnet();
400 DPRINTF(sc, ("%s: gem_reset\n", sc->sc_dev.dv_xname));
401 gem_reset_rx(sc);
402 gem_reset_tx(sc);
403
404 /* Do a full reset */
405 bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX|GEM_RESET_TX);
406 for (i=TRIES; i--; delay(100))
407 if ((bus_space_read_4(t, h, GEM_RESET) &
408 (GEM_RESET_RX|GEM_RESET_TX)) == 0)
409 break;
410 if ((bus_space_read_4(t, h, GEM_RESET) &
411 (GEM_RESET_RX|GEM_RESET_TX)) != 0) {
412 printf("%s: cannot reset device\n",
413 sc->sc_dev.dv_xname);
414 }
415 splx(s);
416 }
417
418
419 /*
420 * gem_rxdrain:
421 *
422 * Drain the receive queue.
423 */
424 void
425 gem_rxdrain(struct gem_softc *sc)
426 {
427 struct gem_rxsoft *rxs;
428 int i;
429
430 for (i = 0; i < GEM_NRXDESC; i++) {
431 rxs = &sc->sc_rxsoft[i];
432 if (rxs->rxs_mbuf != NULL) {
433 bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
434 m_freem(rxs->rxs_mbuf);
435 rxs->rxs_mbuf = NULL;
436 }
437 }
438 }
439
440 /*
441 * Reset the whole thing.
442 */
443 void
444 gem_stop(struct ifnet *ifp, int disable)
445 {
446 struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
447 struct gem_txsoft *txs;
448
449 if (gem_opdebug) printf("in stop %d\n", disable);
450 DPRINTF(sc, ("%s: gem_stop\n", sc->sc_dev.dv_xname));
451
452 callout_stop(&sc->sc_tick_ch);
453 mii_down(&sc->sc_mii);
454
455 /* XXX - Should we reset these instead? */
456 gem_disable_rx(sc);
457 gem_disable_rx(sc);
458
459 /*
460 * Release any queued transmit buffers.
461 */
462 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
463 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
464 if (txs->txs_mbuf != NULL) {
465 bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
466 m_freem(txs->txs_mbuf);
467 txs->txs_mbuf = NULL;
468 }
469 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
470 }
471
472 if (disable) {
473 gem_rxdrain(sc);
474 }
475
476 /*
477 * Mark the interface down and cancel the watchdog timer.
478 */
479 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
480 ifp->if_timer = 0;
481 }
482
483
484 /*
485 * Reset the receiver
486 */
487 int
488 gem_reset_rx(struct gem_softc *sc)
489 {
490 bus_space_tag_t t = sc->sc_bustag;
491 bus_space_handle_t h = sc->sc_h;
492 int i;
493
494
495 /*
496 * Resetting while DMA is in progress can cause a bus hang, so we
497 * disable DMA first.
498 */
499 gem_disable_rx(sc);
500 bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
501 /* Wait till it finishes */
502 for (i=TRIES; i--; delay(100))
503 if ((bus_space_read_4(t, h, GEM_RX_CONFIG) & 1) == 0)
504 break;
505 if ((bus_space_read_4(t, h, GEM_RX_CONFIG) & 1) != 0)
506 printf("%s: cannot disable read dma\n",
507 sc->sc_dev.dv_xname);
508
509 /* Wait 5ms extra. */
510 delay(5000);
511
512 /* Finally, reset the ERX */
513 bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX);
514 /* Wait till it finishes */
515 for (i=TRIES; i--; delay(100))
516 if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_RX) == 0)
517 break;
518 if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_RX) != 0) {
519 printf("%s: cannot reset receiver\n",
520 sc->sc_dev.dv_xname);
521 return (1);
522 }
523 return (0);
524 }
525
526
527 /*
528 * Reset the transmitter
529 */
530 int
531 gem_reset_tx(struct gem_softc *sc)
532 {
533 bus_space_tag_t t = sc->sc_bustag;
534 bus_space_handle_t h = sc->sc_h;
535 int i;
536
537 /*
538 * Resetting while DMA is in progress can cause a bus hang, so we
539 * disable DMA first.
540 */
541 gem_disable_tx(sc);
542 bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
543 /* Wait till it finishes */
544 for (i=TRIES; i--; delay(100))
545 if ((bus_space_read_4(t, h, GEM_TX_CONFIG) & 1) == 0)
546 break;
547 if ((bus_space_read_4(t, h, GEM_TX_CONFIG) & 1) != 0)
548 printf("%s: cannot disable read dma\n",
549 sc->sc_dev.dv_xname);
550
551 /* Wait 5ms extra. */
552 delay(5000);
553
554 /* Finally, reset the ETX */
555 bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX);
556 /* Wait till it finishes */
557 for (i=TRIES; i--; delay(100))
558 if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0)
559 break;
560 if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) != 0) {
561 printf("%s: cannot reset receiver\n",
562 sc->sc_dev.dv_xname);
563 return (1);
564 }
565 return (0);
566 }
567
568 /*
569 * disable receiver.
570 */
571 int
572 gem_disable_rx(struct gem_softc *sc)
573 {
574 bus_space_tag_t t = sc->sc_bustag;
575 bus_space_handle_t h = sc->sc_h;
576 int i;
577 u_int32_t cfg;
578
579 /* Flip the enable bit */
580 cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
581 cfg &= ~GEM_MAC_RX_ENABLE;
582 bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
583
584 /* Wait for it to finish */
585 for (i=TRIES; i--; delay(100))
586 if ((bus_space_read_4(t, h, GEM_MAC_RX_CONFIG) &
587 GEM_MAC_RX_ENABLE) == 0)
588 return (0);
589 return (1);
590 }
591
592 /*
593 * disable transmitter.
594 */
595 int
596 gem_disable_tx(struct gem_softc *sc)
597 {
598 bus_space_tag_t t = sc->sc_bustag;
599 bus_space_handle_t h = sc->sc_h;
600 int i;
601 u_int32_t cfg;
602
603 /* Flip the enable bit */
604 cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
605 cfg &= ~GEM_MAC_TX_ENABLE;
606 bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
607
608 /* Wait for it to finish */
609 for (i=TRIES; i--; delay(100))
610 if ((bus_space_read_4(t, h, GEM_MAC_TX_CONFIG) &
611 GEM_MAC_TX_ENABLE) == 0)
612 return (0);
613 return (1);
614 }
615
616 /*
617 * Initialize interface.
618 */
619 int
620 gem_meminit(struct gem_softc *sc)
621 {
622 struct gem_rxsoft *rxs;
623 int i, error;
624
625 /*
626 * Initialize the transmit descriptor ring.
627 */
628 memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
629 for (i = 0; i < GEM_NTXDESC; i++) {
630 sc->sc_txdescs[i].gd_flags = 0;
631 sc->sc_txdescs[i].gd_addr = 0;
632 }
633 GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
634 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
635 sc->sc_txfree = GEM_NTXDESC;
636 sc->sc_txnext = 0;
637
638 /*
639 * Initialize the receive descriptor and receive job
640 * descriptor rings.
641 */
642 for (i = 0; i < GEM_NRXDESC; i++) {
643 rxs = &sc->sc_rxsoft[i];
644 if (rxs->rxs_mbuf == NULL) {
645 if ((error = gem_add_rxbuf(sc, i)) != 0) {
646 printf("%s: unable to allocate or map rx "
647 "buffer %d, error = %d\n",
648 sc->sc_dev.dv_xname, i, error);
649 /*
650 * XXX Should attempt to run with fewer receive
651 * XXX buffers instead of just failing.
652 */
653 gem_rxdrain(sc);
654 return (1);
655 }
656 } else
657 GEM_INIT_RXDESC(sc, i);
658 }
659 sc->sc_rxptr = 0;
660
661 return (0);
662 }
663
664 static int
665 gem_ringsize(int sz)
666 {
667 int v;
668
669 switch (sz) {
670 case 32:
671 v = GEM_RING_SZ_32;
672 break;
673 case 64:
674 v = GEM_RING_SZ_64;
675 break;
676 case 128:
677 v = GEM_RING_SZ_128;
678 break;
679 case 256:
680 v = GEM_RING_SZ_256;
681 break;
682 case 512:
683 v = GEM_RING_SZ_512;
684 break;
685 case 1024:
686 v = GEM_RING_SZ_1024;
687 break;
688 case 2048:
689 v = GEM_RING_SZ_2048;
690 break;
691 case 4096:
692 v = GEM_RING_SZ_4096;
693 break;
694 case 8192:
695 v = GEM_RING_SZ_8192;
696 break;
697 default:
698 printf("gem: invalid Receive Descriptor ring size\n");
699 break;
700 }
701 return (v);
702 }
703
704 /*
705 * Initialization of interface; set up initialization block
706 * and transmit/receive descriptor rings.
707 */
708 int
709 gem_init(struct ifnet *ifp)
710 {
711 struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
712 bus_space_tag_t t = sc->sc_bustag;
713 bus_space_handle_t h = sc->sc_h;
714 int s;
715 u_int32_t v;
716
717 s = splnet();
718
719 DPRINTF(sc, ("%s: gem_init: calling stop\n", sc->sc_dev.dv_xname));
720 /*
721 * Initialization sequence. The numbered steps below correspond
722 * to the sequence outlined in section 6.3.5.1 in the Ethernet
723 * Channel Engine manual (part of the PCIO manual).
724 * See also the STP2002-STQ document from Sun Microsystems.
725 */
726
727 /* step 1 & 2. Reset the Ethernet Channel */
728 gem_stop(ifp, 0);
729 if (gem_opdebug) printf("in init\n");
730 gem_reset(sc);
731 DPRINTF(sc, ("%s: gem_init: restarting\n", sc->sc_dev.dv_xname));
732
733 /* Re-initialize the MIF */
734 gem_mifinit(sc);
735
736 /* Call MI reset function if any */
737 if (sc->sc_hwreset)
738 (*sc->sc_hwreset)(sc);
739
740 /* step 3. Setup data structures in host memory */
741 gem_meminit(sc);
742
743 /* step 4. TX MAC registers & counters */
744 gem_init_regs(sc);
745 bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
746 ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
747 ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN + sizeof(struct ether_header):
748 ETHER_MAX_LEN + sizeof(struct ether_header)) | (0x2000<<16));
749
750 /* step 5. RX MAC registers & counters */
751 gem_setladrf(sc);
752
753 /* step 6 & 7. Program Descriptor Ring Base Addresses */
754 bus_space_write_8(t, h, GEM_TX_RING_PTR,
755 GEM_CDTXADDR(sc, 0));
756 /* Yeeech. The following has endianness issues. */
757 bus_space_write_4(t, h, GEM_RX_RING_PTR_HI,
758 (((uint64_t)GEM_CDRXADDR(sc, 0))>>32));
759 bus_space_write_4(t, h, GEM_RX_RING_PTR_LO,
760 GEM_CDRXADDR(sc, 0));
761
762 /* step 8. Global Configuration & Interrupt Mask */
763 bus_space_write_4(t, h, GEM_INTMASK,
764 ~(GEM_INTR_TX_INTME|
765 GEM_INTR_TX_EMPTY|
766 GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF|
767 GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS|
768 GEM_INTR_MAC_CONTROL|GEM_INTR_MIF|
769 GEM_INTR_BERR));
770 bus_space_write_4(t, h, GEM_MAC_RX_MASK, 0); /* XXXX */
771 bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */
772 bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */
773 #if 0
774 if (!sc->sc_pci) {
775 /* Config SBus */
776 switch (sc->sc_burst) {
777 default:
778 v = 0;
779 break;
780 case 16:
781 v = GEM_SEB_CFG_BURST16;
782 break;
783 case 32:
784 v = GEM_SEB_CFG_BURST32;
785 break;
786 case 64:
787 v = GEM_SEB_CFG_BURST64;
788 break;
789 }
790 bus_space_write_4(t, seb, GEM_SEBI_CFG,
791 v|GE_SIOCFG_PARITY|GE_SIOCFG_BMODE64);
792 }
793 #endif
794 /* step 9. ETX Configuration: use mostly default values */
795
796 /* Enable DMA */
797 v = gem_ringsize(GEM_NTXDESC /*XXX*/);
798 bus_space_write_4(t, h, GEM_TX_CONFIG,
799 v|GEM_TX_CONFIG_TXDMA_EN|
800 ((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH));
801 bus_space_write_4(t, h, GEM_TX_KICK, sc->sc_txnext);
802
803 /* step 10. ERX Configuration */
804
805 /* Encode Receive Descriptor ring size: four possible values */
806 v = gem_ringsize(GEM_NRXDESC /*XXX*/);
807
808 /* Enable DMA */
809 bus_space_write_4(t, h, GEM_RX_CONFIG,
810 v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)|
811 (2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN|
812 (0<<GEM_RX_CONFIG_CXM_START_SHFT));
813 /*
814 * The following value is for an OFF Threshold of about 15.5 Kbytes
815 * and an ON Threshold of 4K bytes.
816 */
817 bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH, 0xf8 | (0x40 << 12));
818 bus_space_write_4(t, h, GEM_RX_BLANKING, (2<<12)|6);
819
820 /* step 11. Configure Media */
821 gem_mii_statchg(&sc->sc_dev);
822
823 /* XXXX Serial link needs a whole different setup. */
824
825
826 /* step 12. RX_MAC Configuration Register */
827 v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
828 v |= GEM_MAC_RX_ENABLE;
829 bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
830
831 /* step 14. Issue Transmit Pending command */
832
833 /* Call MI initialization function if any */
834 if (sc->sc_hwinit)
835 (*sc->sc_hwinit)(sc);
836
837
838 /* step 15. Give the reciever a swift kick */
839 bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
840
841 /* Start the one second timer. */
842 callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
843
844 ifp->if_flags |= IFF_RUNNING;
845 ifp->if_flags &= ~IFF_OACTIVE;
846 ifp->if_timer = 0;
847 splx(s);
848
849 return (0);
850 }
851
852 /*
853 * Compare two Ether/802 addresses for equality, inlined and unrolled for
854 * speed.
855 */
856 static __inline__ int
857 ether_cmp(a, b)
858 u_char *a, *b;
859 {
860
861 if (a[5] != b[5] || a[4] != b[4] || a[3] != b[3] ||
862 a[2] != b[2] || a[1] != b[1] || a[0] != b[0])
863 return (0);
864 return (1);
865 }
866
867
868 void
869 gem_init_regs(struct gem_softc *sc)
870 {
871 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
872 bus_space_tag_t t = sc->sc_bustag;
873 bus_space_handle_t h = sc->sc_h;
874
875 /* These regs are not cleared on reset */
876 sc->sc_inited = 0;
877 if (!sc->sc_inited) {
878
879 /* Wooo. Magic values. */
880 bus_space_write_4(t, h, GEM_MAC_IPG0, 0);
881 bus_space_write_4(t, h, GEM_MAC_IPG1, 8);
882 bus_space_write_4(t, h, GEM_MAC_IPG2, 4);
883
884 bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
885 /* Max frame and max burst size */
886 bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
887 (ifp->if_mtu+18) | (0x2000<<16)/* Burst size */);
888 bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7);
889 bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4);
890 bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
891 /* Dunno.... */
892 bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
893 bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
894 ((sc->sc_enaddr[5]<<8)|sc->sc_enaddr[4])&0x3ff);
895 /* Secondary MAC addr set to 0:0:0:0:0:0 */
896 bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
897 bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
898 bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
899 /* MAC control addr set to 0:1:c2:0:1:80 */
900 bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
901 bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
902 bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
903
904 /* MAC filter addr set to 0:0:0:0:0:0 */
905 bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
906 bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
907 bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
908
909 bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
910 bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
911
912 sc->sc_inited = 1;
913 }
914
915 /* Counters need to be zeroed */
916 bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
917 bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
918 bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
919 bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
920 bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
921 bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
922 bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
923 bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
924 bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
925 bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
926 bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
927
928 /* Un-pause stuff */
929 #if 0
930 bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
931 #else
932 bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0);
933 #endif
934
935 /*
936 * Set the station address.
937 */
938 bus_space_write_4(t, h, GEM_MAC_ADDR0,
939 (sc->sc_enaddr[4]<<8) | sc->sc_enaddr[5]);
940 bus_space_write_4(t, h, GEM_MAC_ADDR1,
941 (sc->sc_enaddr[2]<<8) | sc->sc_enaddr[3]);
942 bus_space_write_4(t, h, GEM_MAC_ADDR2,
943 (sc->sc_enaddr[0]<<8) | sc->sc_enaddr[1]);
944
945 }
946
947
948
949 void
950 gem_start(ifp)
951 struct ifnet *ifp;
952 {
953 struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
954 struct mbuf *m0, *m;
955 struct gem_txsoft *txs, *last_txs;
956 bus_dmamap_t dmamap;
957 int error, firsttx, nexttx, lasttx, ofree, seg;
958
959 if (gem_opdebug) printf("in start free %x next %x kick %x\n",
960 sc->sc_txfree, sc->sc_txnext,
961 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK));
962 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
963 return;
964
965 /*
966 * Remember the previous number of free descriptors and
967 * the first descriptor we'll use.
968 */
969 ofree = sc->sc_txfree;
970 firsttx = sc->sc_txnext;
971
972 DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
973 sc->sc_dev.dv_xname, ofree, firsttx));
974
975 /*
976 * Loop through the send queue, setting up transmit descriptors
977 * until we drain the queue, or use up all available transmit
978 * descriptors.
979 */
980 while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
981 sc->sc_txfree != 0) {
982 /*
983 * Grab a packet off the queue.
984 */
985 IFQ_POLL(&ifp->if_snd, m0);
986 if (m0 == NULL)
987 break;
988 m = NULL;
989
990 dmamap = txs->txs_dmamap;
991
992 /*
993 * Load the DMA map. If this fails, the packet either
994 * didn't fit in the alloted number of segments, or we were
995 * short on resources. In this case, we'll copy and try
996 * again.
997 */
998 if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m0,
999 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
1000 MGETHDR(m, M_DONTWAIT, MT_DATA);
1001 if (m == NULL) {
1002 printf("%s: unable to allocate Tx mbuf\n",
1003 sc->sc_dev.dv_xname);
1004 break;
1005 }
1006 if (m0->m_pkthdr.len > MHLEN) {
1007 MCLGET(m, M_DONTWAIT);
1008 if ((m->m_flags & M_EXT) == 0) {
1009 printf("%s: unable to allocate Tx "
1010 "cluster\n", sc->sc_dev.dv_xname);
1011 m_freem(m);
1012 break;
1013 }
1014 }
1015 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1016 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1017 error = bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap,
1018 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1019 if (error) {
1020 printf("%s: unable to load Tx buffer, "
1021 "error = %d\n", sc->sc_dev.dv_xname, error);
1022 break;
1023 }
1024 }
1025
1026 /*
1027 * Ensure we have enough descriptors free to describe
1028 * the packet.
1029 */
1030 if (dmamap->dm_nsegs > sc->sc_txfree) {
1031 /*
1032 * Not enough free descriptors to transmit this
1033 * packet. We haven't committed to anything yet,
1034 * so just unload the DMA map, put the packet
1035 * back on the queue, and punt. Notify the upper
1036 * layer that there are no more slots left.
1037 *
1038 * XXX We could allocate an mbuf and copy, but
1039 * XXX it is worth it?
1040 */
1041 ifp->if_flags |= IFF_OACTIVE;
1042 bus_dmamap_unload(sc->sc_dmatag, dmamap);
1043 if (m != NULL)
1044 m_freem(m);
1045 break;
1046 }
1047
1048 IFQ_DEQUEUE(&ifp->if_snd, m0);
1049 if (m != NULL) {
1050 m_freem(m0);
1051 m0 = m;
1052 }
1053
1054 /*
1055 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1056 */
1057
1058 /* Sync the DMA map. */
1059 bus_dmamap_sync(sc->sc_dmatag, dmamap, 0, dmamap->dm_mapsize,
1060 BUS_DMASYNC_PREWRITE);
1061
1062 /*
1063 * Initialize the transmit descriptors.
1064 */
1065 for (nexttx = sc->sc_txnext, seg = 0;
1066 seg < dmamap->dm_nsegs;
1067 seg++, nexttx = GEM_NEXTTX(nexttx)) {
1068 uint64_t flags;
1069
1070 /*
1071 * If this is the first descriptor we're
1072 * enqueueing, set the start of packet flag,
1073 * and the checksum stuff if we want the hardware
1074 * to do it.
1075 */
1076 sc->sc_txdescs[nexttx].gd_addr =
1077 GEM_DMA_WRITE(sc, dmamap->dm_segs[seg].ds_addr);
1078 flags = dmamap->dm_segs[seg].ds_len & GEM_TD_BUFSIZE;
1079 if (nexttx == firsttx) {
1080 flags |= GEM_TD_START_OF_PACKET;
1081 }
1082 if (seg == dmamap->dm_nsegs - 1) {
1083 flags |= GEM_TD_END_OF_PACKET;
1084 }
1085 sc->sc_txdescs[nexttx].gd_flags =
1086 GEM_DMA_WRITE(sc, flags);
1087 lasttx = nexttx;
1088 }
1089
1090 #ifdef GEM_DEBUG
1091 if (ifp->if_flags & IFF_DEBUG) {
1092 printf(" gem_start %p transmit chain:\n", txs);
1093 for (seg = sc->sc_txnext;; seg = GEM_NEXTTX(seg)) {
1094 printf("descriptor %d:\t", seg);
1095 printf("gd_flags: 0x%016llx\t", (long long)
1096 GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_flags));
1097 printf("gd_addr: 0x%016llx\n", (long long)
1098 GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_addr));
1099 if (seg == lasttx)
1100 break;
1101 }
1102 }
1103 #endif
1104
1105 /* Sync the descriptors we're using. */
1106 GEM_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1107 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1108
1109 /*
1110 * Store a pointer to the packet so we can free it later,
1111 * and remember what txdirty will be once the packet is
1112 * done.
1113 */
1114 txs->txs_mbuf = m0;
1115 txs->txs_firstdesc = sc->sc_txnext;
1116 txs->txs_lastdesc = lasttx;
1117 txs->txs_ndescs = dmamap->dm_nsegs;
1118
1119 /* Advance the tx pointer. */
1120 sc->sc_txfree -= dmamap->dm_nsegs;
1121 sc->sc_txnext = nexttx;
1122
1123 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
1124 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1125
1126 last_txs = txs;
1127
1128 #if NBPFILTER > 0
1129 /*
1130 * Pass the packet to any BPF listeners.
1131 */
1132 if (ifp->if_bpf)
1133 bpf_mtap(ifp->if_bpf, m0);
1134 #endif /* NBPFILTER > 0 */
1135 }
1136
1137 if (txs == NULL || sc->sc_txfree == 0) {
1138 /* No more slots left; notify upper layer. */
1139 ifp->if_flags |= IFF_OACTIVE;
1140 }
1141
1142 if (sc->sc_txfree != ofree) {
1143 DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
1144 sc->sc_dev.dv_xname, lasttx, firsttx));
1145 /*
1146 * The entire packet chain is set up.
1147 * Kick the transmitter.
1148 */
1149 DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
1150 sc->sc_dev.dv_xname, nexttx));
1151 if (gem_opdebug) {
1152 int i;
1153 int64_t pa;
1154 i = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK);
1155 printf("GEM_TX_KICK %x GEM_TX_DATA_PTR %llx GEM_TX_RING_PTR %llx\n",
1156 i,
1157 (long long)bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_DATA_PTR),
1158 (long long)bus_space_read_8(sc->sc_bustag, sc->sc_h, GEM_TX_RING_PTR));
1159 printf("descriptor %d: ", (i = lasttx));
1160 printf("gd_flags: 0x%016llx\t", (long long)
1161 GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
1162 pa = GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr);
1163 printf("gd_addr: 0x%016llx\n", (long long) pa);
1164 printf("GEM_TX_CONFIG %x GEM_MAC_XIF_CONFIG %x GEM_MAC_TX_CONFIG %x\n",
1165 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG),
1166 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_XIF_CONFIG),
1167 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG));
1168 }
1169 bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK,
1170 sc->sc_txnext);
1171 if (gem_opdebug) printf("gem_start: txkick %x\n",
1172 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK));
1173
1174 /* Set a watchdog timer in case the chip flakes out. */
1175 ifp->if_timer = 5;
1176 DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
1177 sc->sc_dev.dv_xname, ifp->if_timer));
1178 }
1179 }
1180
1181 /*
1182 * Transmit interrupt.
1183 */
1184 int
1185 gem_tint(sc)
1186 struct gem_softc *sc;
1187 {
1188 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1189 bus_space_tag_t t = sc->sc_bustag;
1190 bus_space_handle_t mac = sc->sc_h;
1191 struct gem_txsoft *txs;
1192 int txlast;
1193
1194
1195 DPRINTF(sc, ("%s: gem_tint\n", sc->sc_dev.dv_xname));
1196
1197 /*
1198 * Unload collision counters
1199 */
1200 ifp->if_collisions +=
1201 bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
1202 bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
1203 bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
1204 bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
1205
1206 /*
1207 * then clear the hardware counters.
1208 */
1209 bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
1210 bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
1211 bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
1212 bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
1213
1214 /*
1215 * Go through our Tx list and free mbufs for those
1216 * frames that have been transmitted.
1217 */
1218 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1219 GEM_CDTXSYNC(sc, txs->txs_lastdesc,
1220 txs->txs_ndescs,
1221 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1222
1223 #ifdef GEM_DEBUG
1224 if (ifp->if_flags & IFF_DEBUG) {
1225 int i;
1226 printf(" txsoft %p transmit chain:\n", txs);
1227 for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
1228 printf("descriptor %d: ", i);
1229 printf("gd_flags: 0x%016llx\t", (long long)
1230 GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
1231 printf("gd_addr: 0x%016llx\n", (long long)
1232 GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
1233 if (i == txs->txs_lastdesc)
1234 break;
1235 }
1236 }
1237 #endif
1238
1239 /*
1240 * In theory, we could harveast some descriptors before
1241 * the ring is empty, but that's a bit complicated.
1242 *
1243 * GEM_TX_COMPLETION points to the last descriptor
1244 * processed +1.
1245 */
1246 txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
1247 DPRINTF(sc,
1248 ("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
1249 txs->txs_lastdesc, txlast));
1250 if (txs->txs_firstdesc <= txs->txs_lastdesc) {
1251 if ((txlast >= txs->txs_firstdesc) &&
1252 (txlast <= txs->txs_lastdesc))
1253 break;
1254 } else {
1255 /* Ick -- this command wraps */
1256 if ((txlast >= txs->txs_firstdesc) ||
1257 (txlast <= txs->txs_lastdesc))
1258 break;
1259 }
1260
1261 DPRINTF(sc, ("gem_tint: releasing a desc\n"));
1262 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1263
1264 sc->sc_txfree += txs->txs_ndescs;
1265
1266 if (txs->txs_mbuf == NULL) {
1267 #ifdef DIAGNOSTIC
1268 panic("gem_txintr: null mbuf");
1269 #endif
1270 }
1271
1272 bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
1273 0, txs->txs_dmamap->dm_mapsize,
1274 BUS_DMASYNC_POSTWRITE);
1275 bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
1276 m_freem(txs->txs_mbuf);
1277 txs->txs_mbuf = NULL;
1278
1279 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1280
1281 ifp->if_opackets++;
1282 }
1283
1284 DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
1285 "GEM_TX_DATA_PTR %llx "
1286 "GEM_TX_COMPLETION %x\n",
1287 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
1288 (long long)bus_space_read_8(sc->sc_bustag, sc->sc_h,
1289 GEM_TX_DATA_PTR),
1290 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)));
1291
1292 gem_start(ifp);
1293
1294 if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) == NULL)
1295 ifp->if_timer = 0;
1296 DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
1297 sc->sc_dev.dv_xname, ifp->if_timer));
1298
1299 return (1);
1300 }
1301
1302 /*
1303 * Receive interrupt.
1304 */
1305 int
1306 gem_rint(sc)
1307 struct gem_softc *sc;
1308 {
1309 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1310 bus_space_tag_t t = sc->sc_bustag;
1311 bus_space_handle_t h = sc->sc_h;
1312 struct ether_header *eh;
1313 struct gem_rxsoft *rxs;
1314 struct mbuf *m;
1315 u_int64_t rxstat;
1316 int i, len;
1317
1318 DPRINTF(sc, ("%s: gem_rint\n", sc->sc_dev.dv_xname));
1319 /*
1320 * XXXX Read the lastrx only once at the top for speed.
1321 */
1322 DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
1323 sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
1324 for (i = sc->sc_rxptr; i != bus_space_read_4(t, h, GEM_RX_COMPLETION);
1325 i = GEM_NEXTRX(i)) {
1326 rxs = &sc->sc_rxsoft[i];
1327
1328 GEM_CDRXSYNC(sc, i,
1329 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1330
1331 rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
1332
1333 if (rxstat & GEM_RD_OWN) {
1334 printf("gem_rint: completed descriptor "
1335 "still owned %d\n", i);
1336 /*
1337 * We have processed all of the receive buffers.
1338 */
1339 break;
1340 }
1341
1342 if (rxstat & GEM_RD_BAD_CRC) {
1343 printf("%s: receive error: CRC error\n",
1344 sc->sc_dev.dv_xname);
1345 GEM_INIT_RXDESC(sc, i);
1346 continue;
1347 }
1348
1349 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1350 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1351 #ifdef GEM_DEBUG
1352 if (ifp->if_flags & IFF_DEBUG) {
1353 printf(" rxsoft %p descriptor %d: ", rxs, i);
1354 printf("gd_flags: 0x%016llx\t", (long long)
1355 GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
1356 printf("gd_addr: 0x%016llx\n", (long long)
1357 GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
1358 }
1359 #endif
1360
1361 /*
1362 * No errors; receive the packet. Note the Gem
1363 * includes the CRC with every packet.
1364 */
1365 len = GEM_RD_BUFLEN(rxstat);
1366
1367 /*
1368 * Allocate a new mbuf cluster. If that fails, we are
1369 * out of memory, and must drop the packet and recycle
1370 * the buffer that's already attached to this descriptor.
1371 */
1372 m = rxs->rxs_mbuf;
1373 if (gem_add_rxbuf(sc, i) != 0) {
1374 ifp->if_ierrors++;
1375 GEM_INIT_RXDESC(sc, i);
1376 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1377 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1378 continue;
1379 }
1380 m->m_data += 2; /* We're already off by two */
1381
1382 ifp->if_ipackets++;
1383 eh = mtod(m, struct ether_header *);
1384 m->m_flags |= M_HASFCS;
1385 m->m_pkthdr.rcvif = ifp;
1386 m->m_pkthdr.len = m->m_len = len;
1387
1388 #if NBPFILTER > 0
1389 /*
1390 * Pass this up to any BPF listeners, but only
1391 * pass it up the stack if its for us.
1392 */
1393 if (ifp->if_bpf)
1394 bpf_mtap(ifp->if_bpf, m);
1395 #endif /* NPBFILTER > 0 */
1396
1397 #if 0
1398 /*
1399 * We sometimes have to run the 21140 in Hash-Only
1400 * mode. If we're in that mode, and not in promiscuous
1401 * mode, and we have a unicast packet that isn't for
1402 * us, then drop it.
1403 */
1404 if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY &&
1405 (ifp->if_flags & IFF_PROMISC) == 0 &&
1406 ETHER_IS_MULTICAST(eh->ether_dhost) == 0 &&
1407 memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
1408 ETHER_ADDR_LEN) != 0) {
1409 m_freem(m);
1410 continue;
1411 }
1412 #endif
1413
1414 /* Pass it on. */
1415 (*ifp->if_input)(ifp, m);
1416 }
1417
1418 /* Update the receive pointer. */
1419 sc->sc_rxptr = i;
1420 bus_space_write_4(t, h, GEM_RX_KICK, i);
1421
1422 DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
1423 sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
1424
1425 return (1);
1426 }
1427
1428
1429 /*
1430 * gem_add_rxbuf:
1431 *
1432 * Add a receive buffer to the indicated descriptor.
1433 */
1434 int
1435 gem_add_rxbuf(struct gem_softc *sc, int idx)
1436 {
1437 struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
1438 struct mbuf *m;
1439 int error;
1440
1441 MGETHDR(m, M_DONTWAIT, MT_DATA);
1442 if (m == NULL)
1443 return (ENOBUFS);
1444
1445 MCLGET(m, M_DONTWAIT);
1446 if ((m->m_flags & M_EXT) == 0) {
1447 m_freem(m);
1448 return (ENOBUFS);
1449 }
1450
1451 #ifdef GEM_DEBUG
1452 /* bzero the packet to check dma */
1453 memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
1454 #endif
1455
1456 if (rxs->rxs_mbuf != NULL)
1457 bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
1458
1459 rxs->rxs_mbuf = m;
1460
1461 error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
1462 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1463 BUS_DMA_READ|BUS_DMA_NOWAIT);
1464 if (error) {
1465 printf("%s: can't load rx DMA map %d, error = %d\n",
1466 sc->sc_dev.dv_xname, idx, error);
1467 panic("gem_add_rxbuf"); /* XXX */
1468 }
1469
1470 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1471 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1472
1473 GEM_INIT_RXDESC(sc, idx);
1474
1475 return (0);
1476 }
1477
1478
1479 int
1480 gem_eint(sc, status)
1481 struct gem_softc *sc;
1482 u_int status;
1483 {
1484 char bits[128];
1485
1486 if ((status & GEM_INTR_MIF) != 0) {
1487 printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
1488 return (1);
1489 }
1490
1491 printf("%s: status=%s\n", sc->sc_dev.dv_xname,
1492 bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
1493 return (1);
1494 }
1495
1496
1497 int
1498 gem_intr(v)
1499 void *v;
1500 {
1501 struct gem_softc *sc = (struct gem_softc *)v;
1502 bus_space_tag_t t = sc->sc_bustag;
1503 bus_space_handle_t seb = sc->sc_h;
1504 u_int32_t status;
1505 int r = 0;
1506
1507 char bits[128];
1508
1509 status = bus_space_read_4(t, seb, GEM_STATUS);
1510 DPRINTF(sc, ("%s: gem_intr: cplt %xstatus %s\n",
1511 sc->sc_dev.dv_xname, (status>>19),
1512 bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits))));
1513 if (gem_opdebug) printf("%s: gem_intr: cplt %x status %s\n",
1514 sc->sc_dev.dv_xname, (status>>19),
1515 bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
1516 if (gem_opdebug && (status & GEM_INTR_TX_DONE)) {
1517 int i;
1518 int64_t pa;
1519 i = bus_space_read_4(t, seb, GEM_TX_KICK);
1520 printf("GEM_TX_KICK %x GEM_TX_DATA_PTR %llx GEM_TX_RING_PTR %llx\n",
1521 i, (long long)bus_space_read_4(t, seb, GEM_TX_DATA_PTR),
1522 (long long)bus_space_read_8(t, seb, GEM_TX_RING_PTR));
1523 printf("descriptor %d: ", --i);
1524 printf("gd_flags: 0x%016llx\t", (long long)
1525 GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
1526 pa = GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr);
1527 printf("gd_addr: 0x%016llx\n", (long long) pa);
1528 printf("GEM_TX_CONFIG %x GEM_MAC_XIF_CONFIG %x GEM_MAC_TX_CONFIG %x "
1529 "GEM_MAC_TX_STATUS %x\n",
1530 bus_space_read_4(t, seb, GEM_TX_CONFIG),
1531 bus_space_read_4(t, seb, GEM_MAC_XIF_CONFIG),
1532 bus_space_read_4(t, seb, GEM_MAC_TX_CONFIG),
1533 bus_space_read_4(t, seb, GEM_MAC_TX_STATUS));
1534 }
1535
1536 if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
1537 r |= gem_eint(sc, status);
1538
1539 if ((status &
1540 (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME))
1541 != 0)
1542 r |= gem_tint(sc);
1543
1544 if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
1545 r |= gem_rint(sc);
1546
1547 /* We should eventually do more than just print out error stats. */
1548 if (status & GEM_INTR_TX_MAC) {
1549 int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS);
1550 if (txstat & ~GEM_MAC_TX_XMIT_DONE)
1551 printf("MAC tx fault, status %x\n", txstat);
1552 }
1553 if (status & GEM_INTR_RX_MAC) {
1554 int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS);
1555 if (rxstat & ~GEM_MAC_RX_DONE)
1556 printf("MAC rx fault, status %x\n", rxstat);
1557 }
1558 return (r);
1559 }
1560
1561
1562 void
1563 gem_watchdog(ifp)
1564 struct ifnet *ifp;
1565 {
1566 struct gem_softc *sc = ifp->if_softc;
1567
1568 DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
1569 "GEM_MAC_RX_CONFIG %x\n",
1570 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
1571 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
1572 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)));
1573
1574 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1575 ++ifp->if_oerrors;
1576
1577 /* Try to get more packets going. */
1578 gem_start(ifp);
1579 }
1580
1581 /*
1582 * Initialize the MII Management Interface
1583 */
1584 void
1585 gem_mifinit(sc)
1586 struct gem_softc *sc;
1587 {
1588 bus_space_tag_t t = sc->sc_bustag;
1589 bus_space_handle_t mif = sc->sc_h;
1590
1591 /* Configure the MIF in frame mode */
1592 sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1593 sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
1594 bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
1595 }
1596
1597 /*
1598 * MII interface
1599 *
1600 * The GEM MII interface supports at least three different operating modes:
1601 *
1602 * Bitbang mode is implemented using data, clock and output enable registers.
1603 *
1604 * Frame mode is implemented by loading a complete frame into the frame
1605 * register and polling the valid bit for completion.
1606 *
1607 * Polling mode uses the frame register but completion is indicated by
1608 * an interrupt.
1609 *
1610 */
1611 static int
1612 gem_mii_readreg(self, phy, reg)
1613 struct device *self;
1614 int phy, reg;
1615 {
1616 struct gem_softc *sc = (void *)self;
1617 bus_space_tag_t t = sc->sc_bustag;
1618 bus_space_handle_t mif = sc->sc_h;
1619 int n;
1620 u_int32_t v;
1621
1622 #ifdef GEM_DEBUG1
1623 if (sc->sc_debug)
1624 printf("gem_mii_readreg: phy %d reg %d\n", phy, reg);
1625 #endif
1626
1627 #if 0
1628 /* Select the desired PHY in the MIF configuration register */
1629 v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1630 /* Clear PHY select bit */
1631 v &= ~GEM_MIF_CONFIG_PHY_SEL;
1632 if (phy == GEM_PHYAD_EXTERNAL)
1633 /* Set PHY select bit to get at external device */
1634 v |= GEM_MIF_CONFIG_PHY_SEL;
1635 bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
1636 #endif
1637
1638 /* Construct the frame command */
1639 v = (reg << GEM_MIF_REG_SHIFT) | (phy << GEM_MIF_PHY_SHIFT) |
1640 GEM_MIF_FRAME_READ;
1641
1642 bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
1643 for (n = 0; n < 100; n++) {
1644 DELAY(1);
1645 v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
1646 if (v & GEM_MIF_FRAME_TA0)
1647 return (v & GEM_MIF_FRAME_DATA);
1648 }
1649
1650 printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
1651 return (0);
1652 }
1653
1654 static void
1655 gem_mii_writereg(self, phy, reg, val)
1656 struct device *self;
1657 int phy, reg, val;
1658 {
1659 struct gem_softc *sc = (void *)self;
1660 bus_space_tag_t t = sc->sc_bustag;
1661 bus_space_handle_t mif = sc->sc_h;
1662 int n;
1663 u_int32_t v;
1664
1665 #ifdef GEM_DEBUG1
1666 if (sc->sc_debug)
1667 printf("gem_mii_writereg: phy %d reg %d val %x\n",
1668 phy, reg, val);
1669 #endif
1670
1671 #if 0
1672 /* Select the desired PHY in the MIF configuration register */
1673 v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1674 /* Clear PHY select bit */
1675 v &= ~GEM_MIF_CONFIG_PHY_SEL;
1676 if (phy == GEM_PHYAD_EXTERNAL)
1677 /* Set PHY select bit to get at external device */
1678 v |= GEM_MIF_CONFIG_PHY_SEL;
1679 bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
1680 #endif
1681 /* Construct the frame command */
1682 v = GEM_MIF_FRAME_WRITE |
1683 (phy << GEM_MIF_PHY_SHIFT) |
1684 (reg << GEM_MIF_REG_SHIFT) |
1685 (val & GEM_MIF_FRAME_DATA);
1686
1687 bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
1688 for (n = 0; n < 100; n++) {
1689 DELAY(1);
1690 v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
1691 if (v & GEM_MIF_FRAME_TA0)
1692 return;
1693 }
1694
1695 printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
1696 }
1697
1698 static void
1699 gem_mii_statchg(dev)
1700 struct device *dev;
1701 {
1702 struct gem_softc *sc = (void *)dev;
1703 int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1704 int phy = sc->sc_phys[instance];
1705 bus_space_tag_t t = sc->sc_bustag;
1706 bus_space_handle_t mac = sc->sc_h;
1707 u_int32_t v;
1708
1709 #ifdef GEM_DEBUG
1710 if (sc->sc_debug)
1711 printf("gem_mii_statchg: status change: phy = %d\n", phy);
1712 #endif
1713
1714
1715 /* Set tx full duplex options */
1716 bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
1717 delay(10000); /* reg must be cleared and delay before changing. */
1718 v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT|
1719 GEM_MAC_TX_ENABLE;
1720 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
1721 v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS;
1722 }
1723 bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v);
1724
1725 /* XIF Configuration */
1726 /* We should really calculate all this rather than rely on defaults */
1727 v = bus_space_read_4(t, mac, GEM_MAC_XIF_CONFIG);
1728 v = GEM_MAC_XIF_LINK_LED;
1729 v |= GEM_MAC_XIF_TX_MII_ENA;
1730 /* If an external transceiver is connected, enable its MII drivers */
1731 sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
1732 if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
1733 /* External MII needs echo disable if half duplex. */
1734 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1735 /* turn on full duplex LED */
1736 v |= GEM_MAC_XIF_FDPLX_LED;
1737 else
1738 /* half duplex -- disable echo */
1739 v |= GEM_MAC_XIF_ECHO_DISABL;
1740 } else
1741 /* Internal MII needs buf enable */
1742 v |= GEM_MAC_XIF_MII_BUF_ENA;
1743 bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
1744 }
1745
1746 int
1747 gem_mediachange(ifp)
1748 struct ifnet *ifp;
1749 {
1750 struct gem_softc *sc = ifp->if_softc;
1751
1752 if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
1753 return (EINVAL);
1754
1755 return (mii_mediachg(&sc->sc_mii));
1756 }
1757
1758 void
1759 gem_mediastatus(ifp, ifmr)
1760 struct ifnet *ifp;
1761 struct ifmediareq *ifmr;
1762 {
1763 struct gem_softc *sc = ifp->if_softc;
1764
1765 if ((ifp->if_flags & IFF_UP) == 0)
1766 return;
1767
1768 mii_pollstat(&sc->sc_mii);
1769 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1770 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1771 }
1772
1773 int gem_ioctldebug = 0;
1774 /*
1775 * Process an ioctl request.
1776 */
1777 int
1778 gem_ioctl(ifp, cmd, data)
1779 struct ifnet *ifp;
1780 u_long cmd;
1781 caddr_t data;
1782 {
1783 struct gem_softc *sc = ifp->if_softc;
1784 struct ifreq *ifr = (struct ifreq *)data;
1785 int s, error = 0;
1786
1787
1788 switch (cmd) {
1789 case SIOCGIFMEDIA:
1790 case SIOCSIFMEDIA:
1791 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1792 break;
1793
1794 default:
1795 error = ether_ioctl(ifp, cmd, data);
1796 if (error == ENETRESET) {
1797 /*
1798 * Multicast list has changed; set the hardware filter
1799 * accordingly.
1800 */
1801 if (gem_ioctldebug) printf("reset1\n");
1802 gem_init(ifp);
1803 delay(50000);
1804 error = 0;
1805 }
1806 break;
1807 }
1808
1809 /* Try to get things going again */
1810 if (ifp->if_flags & IFF_UP) {
1811 if (gem_ioctldebug) printf("start\n");
1812 gem_start(ifp);
1813 }
1814 splx(s);
1815 return (error);
1816 }
1817
1818
1819 void
1820 gem_shutdown(arg)
1821 void *arg;
1822 {
1823 struct gem_softc *sc = (struct gem_softc *)arg;
1824 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1825
1826 gem_stop(ifp, 1);
1827 }
1828
1829 /*
1830 * Set up the logical address filter.
1831 */
1832 void
1833 gem_setladrf(sc)
1834 struct gem_softc *sc;
1835 {
1836 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1837 struct ether_multi *enm;
1838 struct ether_multistep step;
1839 struct ethercom *ec = &sc->sc_ethercom;
1840 bus_space_tag_t t = sc->sc_bustag;
1841 bus_space_handle_t h = sc->sc_h;
1842 u_char *cp;
1843 u_int32_t crc;
1844 u_int32_t hash[16];
1845 u_int32_t v;
1846 int len;
1847
1848 /* Clear hash table */
1849 memset(hash, 0, sizeof(hash));
1850
1851 /* Get current RX configuration */
1852 v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
1853
1854 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1855 /* Turn on promiscuous mode; turn off the hash filter */
1856 v |= GEM_MAC_RX_PROMISCUOUS;
1857 v &= ~GEM_MAC_RX_HASH_FILTER;
1858 ifp->if_flags |= IFF_ALLMULTI;
1859 goto chipit;
1860 }
1861
1862 /* Turn off promiscuous mode; turn on the hash filter */
1863 v &= ~GEM_MAC_RX_PROMISCUOUS;
1864 v |= GEM_MAC_RX_HASH_FILTER;
1865
1866 /*
1867 * Set up multicast address filter by passing all multicast addresses
1868 * through a crc generator, and then using the high order 6 bits as an
1869 * index into the 256 bit logical address filter. The high order bit
1870 * selects the word, while the rest of the bits select the bit within
1871 * the word.
1872 */
1873
1874 ETHER_FIRST_MULTI(step, ec, enm);
1875 while (enm != NULL) {
1876 if (ether_cmp(enm->enm_addrlo, enm->enm_addrhi)) {
1877 /*
1878 * We must listen to a range of multicast addresses.
1879 * For now, just accept all multicasts, rather than
1880 * trying to set only those filter bits needed to match
1881 * the range. (At this time, the only use of address
1882 * ranges is for IP multicast routing, for which the
1883 * range is big enough to require all bits set.)
1884 */
1885 hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1886 ifp->if_flags |= IFF_ALLMULTI;
1887 goto chipit;
1888 }
1889
1890 cp = enm->enm_addrlo;
1891 crc = 0xffffffff;
1892 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1893 int octet = *cp++;
1894 int i;
1895
1896 #define MC_POLY_LE 0xedb88320UL /* mcast crc, little endian */
1897 for (i = 0; i < 8; i++) {
1898 if ((crc & 1) ^ (octet & 1)) {
1899 crc >>= 1;
1900 crc ^= MC_POLY_LE;
1901 } else {
1902 crc >>= 1;
1903 }
1904 octet >>= 1;
1905 }
1906 }
1907 /* Just want the 8 most significant bits. */
1908 crc >>= 24;
1909
1910 /* Set the corresponding bit in the filter. */
1911 hash[crc >> 4] |= 1 << (crc & 0xf);
1912
1913 ETHER_NEXT_MULTI(step, enm);
1914 }
1915
1916 ifp->if_flags &= ~IFF_ALLMULTI;
1917
1918 chipit:
1919 /* Now load the hash table into the chip */
1920 bus_space_write_4(t, h, GEM_MAC_HASH0, hash[0]);
1921 bus_space_write_4(t, h, GEM_MAC_HASH1, hash[1]);
1922 bus_space_write_4(t, h, GEM_MAC_HASH2, hash[2]);
1923 bus_space_write_4(t, h, GEM_MAC_HASH3, hash[3]);
1924 bus_space_write_4(t, h, GEM_MAC_HASH4, hash[4]);
1925 bus_space_write_4(t, h, GEM_MAC_HASH5, hash[5]);
1926 bus_space_write_4(t, h, GEM_MAC_HASH6, hash[6]);
1927 bus_space_write_4(t, h, GEM_MAC_HASH7, hash[7]);
1928 bus_space_write_4(t, h, GEM_MAC_HASH8, hash[8]);
1929 bus_space_write_4(t, h, GEM_MAC_HASH9, hash[9]);
1930 bus_space_write_4(t, h, GEM_MAC_HASH10, hash[10]);
1931 bus_space_write_4(t, h, GEM_MAC_HASH11, hash[11]);
1932 bus_space_write_4(t, h, GEM_MAC_HASH12, hash[12]);
1933 bus_space_write_4(t, h, GEM_MAC_HASH13, hash[13]);
1934 bus_space_write_4(t, h, GEM_MAC_HASH14, hash[14]);
1935 bus_space_write_4(t, h, GEM_MAC_HASH15, hash[15]);
1936
1937 bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
1938 }
1939
1940 #if notyet
1941
1942 /*
1943 * gem_power:
1944 *
1945 * Power management (suspend/resume) hook.
1946 */
1947 void
1948 gem_power(why, arg)
1949 int why;
1950 void *arg;
1951 {
1952 struct gem_softc *sc = arg;
1953 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1954 int s;
1955
1956 s = splnet();
1957 switch (why) {
1958 case PWR_SUSPEND:
1959 case PWR_STANDBY:
1960 gem_stop(ifp, 1);
1961 if (sc->sc_power != NULL)
1962 (*sc->sc_power)(sc, why);
1963 break;
1964 case PWR_RESUME:
1965 if (ifp->if_flags & IFF_UP) {
1966 if (sc->sc_power != NULL)
1967 (*sc->sc_power)(sc, why);
1968 gem_init(ifp);
1969 }
1970 break;
1971 case PWR_SOFTSUSPEND:
1972 case PWR_SOFTSTANDBY:
1973 case PWR_SOFTRESUME:
1974 break;
1975 }
1976 splx(s);
1977 }
1978 #endif
1979