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