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