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