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