]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/stge/if_stge.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / stge / if_stge.c
1 /*      $NetBSD: if_stge.c,v 1.32 2005/12/11 12:22:49 christos Exp $    */
2
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*
33  * Device driver for the Sundance Tech. TC9021 10/100/1000
34  * Ethernet controller.
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #ifdef HAVE_KERNEL_OPTION_HEADERS
41 #include "opt_device_polling.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/endian.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/taskqueue.h>
55
56 #include <net/bpf.h>
57 #include <net/ethernet.h>
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_types.h>
62 #include <net/if_vlan_var.h>
63
64 #include <machine/bus.h>
65 #include <machine/resource.h>
66 #include <sys/bus.h>
67 #include <sys/rman.h>
68
69 #include <dev/mii/mii.h>
70 #include <dev/mii/mii_bitbang.h>
71 #include <dev/mii/miivar.h>
72
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcivar.h>
75
76 #include <dev/stge/if_stgereg.h>
77
78 #define STGE_CSUM_FEATURES      (CSUM_IP | CSUM_TCP | CSUM_UDP)
79
80 MODULE_DEPEND(stge, pci, 1, 1, 1);
81 MODULE_DEPEND(stge, ether, 1, 1, 1);
82 MODULE_DEPEND(stge, miibus, 1, 1, 1);
83
84 /* "device miibus" required.  See GENERIC if you get errors here. */
85 #include "miibus_if.h"
86
87 /*
88  * Devices supported by this driver.
89  */
90 static const struct stge_product {
91         uint16_t        stge_vendorid;
92         uint16_t        stge_deviceid;
93         const char      *stge_name;
94 } const stge_products[] = {
95         { VENDOR_SUNDANCETI,    DEVICEID_SUNDANCETI_ST1023,
96           "Sundance ST-1023 Gigabit Ethernet" },
97
98         { VENDOR_SUNDANCETI,    DEVICEID_SUNDANCETI_ST2021,
99           "Sundance ST-2021 Gigabit Ethernet" },
100
101         { VENDOR_TAMARACK,      DEVICEID_TAMARACK_TC9021,
102           "Tamarack TC9021 Gigabit Ethernet" },
103
104         { VENDOR_TAMARACK,      DEVICEID_TAMARACK_TC9021_ALT,
105           "Tamarack TC9021 Gigabit Ethernet" },
106
107         /*
108          * The Sundance sample boards use the Sundance vendor ID,
109          * but the Tamarack product ID.
110          */
111         { VENDOR_SUNDANCETI,    DEVICEID_TAMARACK_TC9021,
112           "Sundance TC9021 Gigabit Ethernet" },
113
114         { VENDOR_SUNDANCETI,    DEVICEID_TAMARACK_TC9021_ALT,
115           "Sundance TC9021 Gigabit Ethernet" },
116
117         { VENDOR_DLINK,         DEVICEID_DLINK_DL4000,
118           "D-Link DL-4000 Gigabit Ethernet" },
119
120         { VENDOR_ANTARES,       DEVICEID_ANTARES_TC9021,
121           "Antares Gigabit Ethernet" }
122 };
123
124 static int      stge_probe(device_t);
125 static int      stge_attach(device_t);
126 static int      stge_detach(device_t);
127 static int      stge_shutdown(device_t);
128 static int      stge_suspend(device_t);
129 static int      stge_resume(device_t);
130
131 static int      stge_encap(struct stge_softc *, struct mbuf **);
132 static void     stge_start(struct ifnet *);
133 static void     stge_start_locked(struct ifnet *);
134 static void     stge_watchdog(struct stge_softc *);
135 static int      stge_ioctl(struct ifnet *, u_long, caddr_t);
136 static void     stge_init(void *);
137 static void     stge_init_locked(struct stge_softc *);
138 static void     stge_vlan_setup(struct stge_softc *);
139 static void     stge_stop(struct stge_softc *);
140 static void     stge_start_tx(struct stge_softc *);
141 static void     stge_start_rx(struct stge_softc *);
142 static void     stge_stop_tx(struct stge_softc *);
143 static void     stge_stop_rx(struct stge_softc *);
144
145 static void     stge_reset(struct stge_softc *, uint32_t);
146 static int      stge_eeprom_wait(struct stge_softc *);
147 static void     stge_read_eeprom(struct stge_softc *, int, uint16_t *);
148 static void     stge_tick(void *);
149 static void     stge_stats_update(struct stge_softc *);
150 static void     stge_set_filter(struct stge_softc *);
151 static void     stge_set_multi(struct stge_softc *);
152
153 static void     stge_link_task(void *, int);
154 static void     stge_intr(void *);
155 static __inline int stge_tx_error(struct stge_softc *);
156 static void     stge_txeof(struct stge_softc *);
157 static int      stge_rxeof(struct stge_softc *);
158 static __inline void stge_discard_rxbuf(struct stge_softc *, int);
159 static int      stge_newbuf(struct stge_softc *, int);
160 #ifndef __NO_STRICT_ALIGNMENT
161 static __inline struct mbuf *stge_fixup_rx(struct stge_softc *, struct mbuf *);
162 #endif
163
164 static int      stge_miibus_readreg(device_t, int, int);
165 static int      stge_miibus_writereg(device_t, int, int, int);
166 static void     stge_miibus_statchg(device_t);
167 static int      stge_mediachange(struct ifnet *);
168 static void     stge_mediastatus(struct ifnet *, struct ifmediareq *);
169
170 static void     stge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
171 static int      stge_dma_alloc(struct stge_softc *);
172 static void     stge_dma_free(struct stge_softc *);
173 static void     stge_dma_wait(struct stge_softc *);
174 static void     stge_init_tx_ring(struct stge_softc *);
175 static int      stge_init_rx_ring(struct stge_softc *);
176 #ifdef DEVICE_POLLING
177 static int      stge_poll(struct ifnet *, enum poll_cmd, int);
178 #endif
179
180 static void     stge_setwol(struct stge_softc *);
181 static int      sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
182 static int      sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS);
183 static int      sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS);
184
185 /*
186  * MII bit-bang glue
187  */
188 static uint32_t stge_mii_bitbang_read(device_t);
189 static void     stge_mii_bitbang_write(device_t, uint32_t);
190
191 static const struct mii_bitbang_ops stge_mii_bitbang_ops = {
192         stge_mii_bitbang_read,
193         stge_mii_bitbang_write,
194         {
195                 PC_MgmtData,            /* MII_BIT_MDO */
196                 PC_MgmtData,            /* MII_BIT_MDI */
197                 PC_MgmtClk,             /* MII_BIT_MDC */
198                 PC_MgmtDir,             /* MII_BIT_DIR_HOST_PHY */
199                 0,                      /* MII_BIT_DIR_PHY_HOST */
200         }
201 };
202
203 static device_method_t stge_methods[] = {
204         /* Device interface */
205         DEVMETHOD(device_probe,         stge_probe),
206         DEVMETHOD(device_attach,        stge_attach),
207         DEVMETHOD(device_detach,        stge_detach),
208         DEVMETHOD(device_shutdown,      stge_shutdown),
209         DEVMETHOD(device_suspend,       stge_suspend),
210         DEVMETHOD(device_resume,        stge_resume),
211
212         /* MII interface */
213         DEVMETHOD(miibus_readreg,       stge_miibus_readreg),
214         DEVMETHOD(miibus_writereg,      stge_miibus_writereg),
215         DEVMETHOD(miibus_statchg,       stge_miibus_statchg),
216
217         { 0, 0 }
218
219 };
220
221 static driver_t stge_driver = {
222         "stge",
223         stge_methods,
224         sizeof(struct stge_softc)
225 };
226
227 static devclass_t stge_devclass;
228
229 DRIVER_MODULE(stge, pci, stge_driver, stge_devclass, 0, 0);
230 DRIVER_MODULE(miibus, stge, miibus_driver, miibus_devclass, 0, 0);
231
232 static struct resource_spec stge_res_spec_io[] = {
233         { SYS_RES_IOPORT,       PCIR_BAR(0),    RF_ACTIVE },
234         { SYS_RES_IRQ,          0,              RF_ACTIVE | RF_SHAREABLE },
235         { -1,                   0,              0 }
236 };
237
238 static struct resource_spec stge_res_spec_mem[] = {
239         { SYS_RES_MEMORY,       PCIR_BAR(1),    RF_ACTIVE },
240         { SYS_RES_IRQ,          0,              RF_ACTIVE | RF_SHAREABLE },
241         { -1,                   0,              0 }
242 };
243
244 /*
245  * stge_mii_bitbang_read: [mii bit-bang interface function]
246  *
247  *      Read the MII serial port for the MII bit-bang module.
248  */
249 static uint32_t
250 stge_mii_bitbang_read(device_t dev)
251 {
252         struct stge_softc *sc;
253         uint32_t val;
254
255         sc = device_get_softc(dev);
256
257         val = CSR_READ_1(sc, STGE_PhyCtrl);
258         CSR_BARRIER(sc, STGE_PhyCtrl, 1,
259             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
260         return (val);
261 }
262
263 /*
264  * stge_mii_bitbang_write: [mii big-bang interface function]
265  *
266  *      Write the MII serial port for the MII bit-bang module.
267  */
268 static void
269 stge_mii_bitbang_write(device_t dev, uint32_t val)
270 {
271         struct stge_softc *sc;
272
273         sc = device_get_softc(dev);
274
275         CSR_WRITE_1(sc, STGE_PhyCtrl, val);
276         CSR_BARRIER(sc, STGE_PhyCtrl, 1,
277             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
278 }
279
280 /*
281  * sc_miibus_readreg:   [mii interface function]
282  *
283  *      Read a PHY register on the MII of the TC9021.
284  */
285 static int
286 stge_miibus_readreg(device_t dev, int phy, int reg)
287 {
288         struct stge_softc *sc;
289         int error, val;
290
291         sc = device_get_softc(dev);
292
293         if (reg == STGE_PhyCtrl) {
294                 /* XXX allow ip1000phy read STGE_PhyCtrl register. */
295                 STGE_MII_LOCK(sc);
296                 error = CSR_READ_1(sc, STGE_PhyCtrl);
297                 STGE_MII_UNLOCK(sc);
298                 return (error);
299         }
300
301         STGE_MII_LOCK(sc);
302         val = mii_bitbang_readreg(dev, &stge_mii_bitbang_ops, phy, reg);
303         STGE_MII_UNLOCK(sc);
304         return (val);
305 }
306
307 /*
308  * stge_miibus_writereg:        [mii interface function]
309  *
310  *      Write a PHY register on the MII of the TC9021.
311  */
312 static int
313 stge_miibus_writereg(device_t dev, int phy, int reg, int val)
314 {
315         struct stge_softc *sc;
316
317         sc = device_get_softc(dev);
318
319         STGE_MII_LOCK(sc);
320         mii_bitbang_writereg(dev, &stge_mii_bitbang_ops, phy, reg, val);
321         STGE_MII_UNLOCK(sc);
322         return (0);
323 }
324
325 /*
326  * stge_miibus_statchg: [mii interface function]
327  *
328  *      Callback from MII layer when media changes.
329  */
330 static void
331 stge_miibus_statchg(device_t dev)
332 {
333         struct stge_softc *sc;
334
335         sc = device_get_softc(dev);
336         taskqueue_enqueue(taskqueue_swi, &sc->sc_link_task);
337 }
338
339 /*
340  * stge_mediastatus:    [ifmedia interface function]
341  *
342  *      Get the current interface media status.
343  */
344 static void
345 stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
346 {
347         struct stge_softc *sc;
348         struct mii_data *mii;
349
350         sc = ifp->if_softc;
351         mii = device_get_softc(sc->sc_miibus);
352
353         mii_pollstat(mii);
354         ifmr->ifm_status = mii->mii_media_status;
355         ifmr->ifm_active = mii->mii_media_active;
356 }
357
358 /*
359  * stge_mediachange:    [ifmedia interface function]
360  *
361  *      Set hardware to newly-selected media.
362  */
363 static int
364 stge_mediachange(struct ifnet *ifp)
365 {
366         struct stge_softc *sc;
367         struct mii_data *mii;
368
369         sc = ifp->if_softc;
370         mii = device_get_softc(sc->sc_miibus);
371         mii_mediachg(mii);
372
373         return (0);
374 }
375
376 static int
377 stge_eeprom_wait(struct stge_softc *sc)
378 {
379         int i;
380
381         for (i = 0; i < STGE_TIMEOUT; i++) {
382                 DELAY(1000);
383                 if ((CSR_READ_2(sc, STGE_EepromCtrl) & EC_EepromBusy) == 0)
384                         return (0);
385         }
386         return (1);
387 }
388
389 /*
390  * stge_read_eeprom:
391  *
392  *      Read data from the serial EEPROM.
393  */
394 static void
395 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
396 {
397
398         if (stge_eeprom_wait(sc))
399                 device_printf(sc->sc_dev, "EEPROM failed to come ready\n");
400
401         CSR_WRITE_2(sc, STGE_EepromCtrl,
402             EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
403         if (stge_eeprom_wait(sc))
404                 device_printf(sc->sc_dev, "EEPROM read timed out\n");
405         *data = CSR_READ_2(sc, STGE_EepromData);
406 }
407
408
409 static int
410 stge_probe(device_t dev)
411 {
412         const struct stge_product *sp;
413         int i;
414         uint16_t vendor, devid;
415
416         vendor = pci_get_vendor(dev);
417         devid = pci_get_device(dev);
418         sp = stge_products;
419         for (i = 0; i < sizeof(stge_products)/sizeof(stge_products[0]);
420             i++, sp++) {
421                 if (vendor == sp->stge_vendorid &&
422                     devid == sp->stge_deviceid) {
423                         device_set_desc(dev, sp->stge_name);
424                         return (BUS_PROBE_DEFAULT);
425                 }
426         }
427
428         return (ENXIO);
429 }
430
431 static int
432 stge_attach(device_t dev)
433 {
434         struct stge_softc *sc;
435         struct ifnet *ifp;
436         uint8_t enaddr[ETHER_ADDR_LEN];
437         int error, flags, i;
438         uint16_t cmd;
439         uint32_t val;
440
441         error = 0;
442         sc = device_get_softc(dev);
443         sc->sc_dev = dev;
444
445         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
446             MTX_DEF);
447         mtx_init(&sc->sc_mii_mtx, "stge_mii_mutex", NULL, MTX_DEF);
448         callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
449         TASK_INIT(&sc->sc_link_task, 0, stge_link_task, sc);
450
451         /*
452          * Map the device.
453          */
454         pci_enable_busmaster(dev);
455         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
456         val = pci_read_config(dev, PCIR_BAR(1), 4);
457         if ((val & 0x01) != 0)
458                 sc->sc_spec = stge_res_spec_mem;
459         else {
460                 val = pci_read_config(dev, PCIR_BAR(0), 4);
461                 if ((val & 0x01) == 0) {
462                         device_printf(sc->sc_dev, "couldn't locate IO BAR\n");
463                         error = ENXIO;
464                         goto fail;
465                 }
466                 sc->sc_spec = stge_res_spec_io;
467         }
468         error = bus_alloc_resources(dev, sc->sc_spec, sc->sc_res);
469         if (error != 0) {
470                 device_printf(dev, "couldn't allocate %s resources\n",
471                     sc->sc_spec == stge_res_spec_mem ? "memory" : "I/O");
472                 goto fail;
473         }
474         sc->sc_rev = pci_get_revid(dev);
475
476         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
477             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
478             "rxint_nframe", CTLTYPE_INT|CTLFLAG_RW, &sc->sc_rxint_nframe, 0,
479             sysctl_hw_stge_rxint_nframe, "I", "stge rx interrupt nframe");
480
481         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
482             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
483             "rxint_dmawait", CTLTYPE_INT|CTLFLAG_RW, &sc->sc_rxint_dmawait, 0,
484             sysctl_hw_stge_rxint_dmawait, "I", "stge rx interrupt dmawait");
485
486         /* Pull in device tunables. */
487         sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
488         error = resource_int_value(device_get_name(dev), device_get_unit(dev),
489             "rxint_nframe", &sc->sc_rxint_nframe);
490         if (error == 0) {
491                 if (sc->sc_rxint_nframe < STGE_RXINT_NFRAME_MIN ||
492                     sc->sc_rxint_nframe > STGE_RXINT_NFRAME_MAX) {
493                         device_printf(dev, "rxint_nframe value out of range; "
494                             "using default: %d\n", STGE_RXINT_NFRAME_DEFAULT);
495                         sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
496                 }
497         }
498
499         sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
500         error = resource_int_value(device_get_name(dev), device_get_unit(dev),
501             "rxint_dmawait", &sc->sc_rxint_dmawait);
502         if (error == 0) {
503                 if (sc->sc_rxint_dmawait < STGE_RXINT_DMAWAIT_MIN ||
504                     sc->sc_rxint_dmawait > STGE_RXINT_DMAWAIT_MAX) {
505                         device_printf(dev, "rxint_dmawait value out of range; "
506                             "using default: %d\n", STGE_RXINT_DMAWAIT_DEFAULT);
507                         sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
508                 }
509         }
510
511         if ((error = stge_dma_alloc(sc) != 0))
512                 goto fail;
513
514         /*
515          * Determine if we're copper or fiber.  It affects how we
516          * reset the card.
517          */
518         if (CSR_READ_4(sc, STGE_AsicCtrl) & AC_PhyMedia)
519                 sc->sc_usefiber = 1;
520         else
521                 sc->sc_usefiber = 0;
522
523         /* Load LED configuration from EEPROM. */
524         stge_read_eeprom(sc, STGE_EEPROM_LEDMode, &sc->sc_led);
525
526         /*
527          * Reset the chip to a known state.
528          */
529         STGE_LOCK(sc);
530         stge_reset(sc, STGE_RESET_FULL);
531         STGE_UNLOCK(sc);
532
533         /*
534          * Reading the station address from the EEPROM doesn't seem
535          * to work, at least on my sample boards.  Instead, since
536          * the reset sequence does AutoInit, read it from the station
537          * address registers. For Sundance 1023 you can only read it
538          * from EEPROM.
539          */
540         if (pci_get_device(dev) != DEVICEID_SUNDANCETI_ST1023) {
541                 uint16_t v;
542
543                 v = CSR_READ_2(sc, STGE_StationAddress0);
544                 enaddr[0] = v & 0xff;
545                 enaddr[1] = v >> 8;
546                 v = CSR_READ_2(sc, STGE_StationAddress1);
547                 enaddr[2] = v & 0xff;
548                 enaddr[3] = v >> 8;
549                 v = CSR_READ_2(sc, STGE_StationAddress2);
550                 enaddr[4] = v & 0xff;
551                 enaddr[5] = v >> 8;
552                 sc->sc_stge1023 = 0;
553         } else {
554                 uint16_t myaddr[ETHER_ADDR_LEN / 2];
555                 for (i = 0; i <ETHER_ADDR_LEN / 2; i++) {
556                         stge_read_eeprom(sc, STGE_EEPROM_StationAddress0 + i,
557                             &myaddr[i]);
558                         myaddr[i] = le16toh(myaddr[i]);
559                 }
560                 bcopy(myaddr, enaddr, sizeof(enaddr));
561                 sc->sc_stge1023 = 1;
562         }
563
564         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
565         if (ifp == NULL) {
566                 device_printf(sc->sc_dev, "failed to if_alloc()\n");
567                 error = ENXIO;
568                 goto fail;
569         }
570
571         ifp->if_softc = sc;
572         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
573         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
574         ifp->if_ioctl = stge_ioctl;
575         ifp->if_start = stge_start;
576         ifp->if_init = stge_init;
577         ifp->if_mtu = ETHERMTU;
578         ifp->if_snd.ifq_drv_maxlen = STGE_TX_RING_CNT - 1;
579         IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
580         IFQ_SET_READY(&ifp->if_snd);
581         /* Revision B3 and earlier chips have checksum bug. */
582         if (sc->sc_rev >= 0x0c) {
583                 ifp->if_hwassist = STGE_CSUM_FEATURES;
584                 ifp->if_capabilities = IFCAP_HWCSUM;
585         } else {
586                 ifp->if_hwassist = 0;
587                 ifp->if_capabilities = 0;
588         }
589         ifp->if_capabilities |= IFCAP_WOL_MAGIC;
590         ifp->if_capenable = ifp->if_capabilities;
591
592         /*
593          * Read some important bits from the PhyCtrl register.
594          */
595         sc->sc_PhyCtrl = CSR_READ_1(sc, STGE_PhyCtrl) &
596             (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
597
598         /* Set up MII bus. */
599         flags = MIIF_DOPAUSE;
600         if (sc->sc_rev >= 0x40 && sc->sc_rev <= 0x4e)
601                 flags |= MIIF_MACPRIV0;
602         error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp, stge_mediachange,
603             stge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
604             flags);
605         if (error != 0) {
606                 device_printf(sc->sc_dev, "attaching PHYs failed\n");
607                 goto fail;
608         }
609
610         ether_ifattach(ifp, enaddr);
611
612         /* VLAN capability setup */
613         ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
614         if (sc->sc_rev >= 0x0c)
615                 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
616         ifp->if_capenable = ifp->if_capabilities;
617 #ifdef DEVICE_POLLING
618         ifp->if_capabilities |= IFCAP_POLLING;
619 #endif
620         /*
621          * Tell the upper layer(s) we support long frames.
622          * Must appear after the call to ether_ifattach() because
623          * ether_ifattach() sets ifi_hdrlen to the default value.
624          */
625         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
626
627         /*
628          * The manual recommends disabling early transmit, so we
629          * do.  It's disabled anyway, if using IP checksumming,
630          * since the entire packet must be in the FIFO in order
631          * for the chip to perform the checksum.
632          */
633         sc->sc_txthresh = 0x0fff;
634
635         /*
636          * Disable MWI if the PCI layer tells us to.
637          */
638         sc->sc_DMACtrl = 0;
639         if ((cmd & PCIM_CMD_MWRICEN) == 0)
640                 sc->sc_DMACtrl |= DMAC_MWIDisable;
641
642         /*
643          * Hookup IRQ
644          */
645         error = bus_setup_intr(dev, sc->sc_res[1], INTR_TYPE_NET | INTR_MPSAFE,
646             NULL, stge_intr, sc, &sc->sc_ih);
647         if (error != 0) {
648                 ether_ifdetach(ifp);
649                 device_printf(sc->sc_dev, "couldn't set up IRQ\n");
650                 sc->sc_ifp = NULL;
651                 goto fail;
652         }
653
654 fail:
655         if (error != 0)
656                 stge_detach(dev);
657
658         return (error);
659 }
660
661 static int
662 stge_detach(device_t dev)
663 {
664         struct stge_softc *sc;
665         struct ifnet *ifp;
666
667         sc = device_get_softc(dev);
668
669         ifp = sc->sc_ifp;
670 #ifdef DEVICE_POLLING
671         if (ifp && ifp->if_capenable & IFCAP_POLLING)
672                 ether_poll_deregister(ifp);
673 #endif
674         if (device_is_attached(dev)) {
675                 STGE_LOCK(sc);
676                 /* XXX */
677                 sc->sc_detach = 1;
678                 stge_stop(sc);
679                 STGE_UNLOCK(sc);
680                 callout_drain(&sc->sc_tick_ch);
681                 taskqueue_drain(taskqueue_swi, &sc->sc_link_task);
682                 ether_ifdetach(ifp);
683         }
684
685         if (sc->sc_miibus != NULL) {
686                 device_delete_child(dev, sc->sc_miibus);
687                 sc->sc_miibus = NULL;
688         }
689         bus_generic_detach(dev);
690         stge_dma_free(sc);
691
692         if (ifp != NULL) {
693                 if_free(ifp);
694                 sc->sc_ifp = NULL;
695         }
696
697         if (sc->sc_ih) {
698                 bus_teardown_intr(dev, sc->sc_res[1], sc->sc_ih);
699                 sc->sc_ih = NULL;
700         }
701         bus_release_resources(dev, sc->sc_spec, sc->sc_res);
702
703         mtx_destroy(&sc->sc_mii_mtx);
704         mtx_destroy(&sc->sc_mtx);
705
706         return (0);
707 }
708
709 struct stge_dmamap_arg {
710         bus_addr_t      stge_busaddr;
711 };
712
713 static void
714 stge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
715 {
716         struct stge_dmamap_arg *ctx;
717
718         if (error != 0)
719                 return;
720
721         ctx = (struct stge_dmamap_arg *)arg;
722         ctx->stge_busaddr = segs[0].ds_addr;
723 }
724
725 static int
726 stge_dma_alloc(struct stge_softc *sc)
727 {
728         struct stge_dmamap_arg ctx;
729         struct stge_txdesc *txd;
730         struct stge_rxdesc *rxd;
731         int error, i;
732
733         /* create parent tag. */
734         error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),/* parent */
735                     1, 0,                       /* algnmnt, boundary */
736                     STGE_DMA_MAXADDR,           /* lowaddr */
737                     BUS_SPACE_MAXADDR,          /* highaddr */
738                     NULL, NULL,                 /* filter, filterarg */
739                     BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
740                     0,                          /* nsegments */
741                     BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
742                     0,                          /* flags */
743                     NULL, NULL,                 /* lockfunc, lockarg */
744                     &sc->sc_cdata.stge_parent_tag);
745         if (error != 0) {
746                 device_printf(sc->sc_dev, "failed to create parent DMA tag\n");
747                 goto fail;
748         }
749         /* create tag for Tx ring. */
750         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
751                     STGE_RING_ALIGN, 0,         /* algnmnt, boundary */
752                     BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
753                     BUS_SPACE_MAXADDR,          /* highaddr */
754                     NULL, NULL,                 /* filter, filterarg */
755                     STGE_TX_RING_SZ,            /* maxsize */
756                     1,                          /* nsegments */
757                     STGE_TX_RING_SZ,            /* maxsegsize */
758                     0,                          /* flags */
759                     NULL, NULL,                 /* lockfunc, lockarg */
760                     &sc->sc_cdata.stge_tx_ring_tag);
761         if (error != 0) {
762                 device_printf(sc->sc_dev,
763                     "failed to allocate Tx ring DMA tag\n");
764                 goto fail;
765         }
766
767         /* create tag for Rx ring. */
768         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
769                     STGE_RING_ALIGN, 0,         /* algnmnt, boundary */
770                     BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
771                     BUS_SPACE_MAXADDR,          /* highaddr */
772                     NULL, NULL,                 /* filter, filterarg */
773                     STGE_RX_RING_SZ,            /* maxsize */
774                     1,                          /* nsegments */
775                     STGE_RX_RING_SZ,            /* maxsegsize */
776                     0,                          /* flags */
777                     NULL, NULL,                 /* lockfunc, lockarg */
778                     &sc->sc_cdata.stge_rx_ring_tag);
779         if (error != 0) {
780                 device_printf(sc->sc_dev,
781                     "failed to allocate Rx ring DMA tag\n");
782                 goto fail;
783         }
784
785         /* create tag for Tx buffers. */
786         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
787                     1, 0,                       /* algnmnt, boundary */
788                     BUS_SPACE_MAXADDR,          /* lowaddr */
789                     BUS_SPACE_MAXADDR,          /* highaddr */
790                     NULL, NULL,                 /* filter, filterarg */
791                     MCLBYTES * STGE_MAXTXSEGS,  /* maxsize */
792                     STGE_MAXTXSEGS,             /* nsegments */
793                     MCLBYTES,                   /* maxsegsize */
794                     0,                          /* flags */
795                     NULL, NULL,                 /* lockfunc, lockarg */
796                     &sc->sc_cdata.stge_tx_tag);
797         if (error != 0) {
798                 device_printf(sc->sc_dev, "failed to allocate Tx DMA tag\n");
799                 goto fail;
800         }
801
802         /* create tag for Rx buffers. */
803         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
804                     1, 0,                       /* algnmnt, boundary */
805                     BUS_SPACE_MAXADDR,          /* lowaddr */
806                     BUS_SPACE_MAXADDR,          /* highaddr */
807                     NULL, NULL,                 /* filter, filterarg */
808                     MCLBYTES,                   /* maxsize */
809                     1,                          /* nsegments */
810                     MCLBYTES,                   /* maxsegsize */
811                     0,                          /* flags */
812                     NULL, NULL,                 /* lockfunc, lockarg */
813                     &sc->sc_cdata.stge_rx_tag);
814         if (error != 0) {
815                 device_printf(sc->sc_dev, "failed to allocate Rx DMA tag\n");
816                 goto fail;
817         }
818
819         /* allocate DMA'able memory and load the DMA map for Tx ring. */
820         error = bus_dmamem_alloc(sc->sc_cdata.stge_tx_ring_tag,
821             (void **)&sc->sc_rdata.stge_tx_ring, BUS_DMA_NOWAIT |
822             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sc_cdata.stge_tx_ring_map);
823         if (error != 0) {
824                 device_printf(sc->sc_dev,
825                     "failed to allocate DMA'able memory for Tx ring\n");
826                 goto fail;
827         }
828
829         ctx.stge_busaddr = 0;
830         error = bus_dmamap_load(sc->sc_cdata.stge_tx_ring_tag,
831             sc->sc_cdata.stge_tx_ring_map, sc->sc_rdata.stge_tx_ring,
832             STGE_TX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
833         if (error != 0 || ctx.stge_busaddr == 0) {
834                 device_printf(sc->sc_dev,
835                     "failed to load DMA'able memory for Tx ring\n");
836                 goto fail;
837         }
838         sc->sc_rdata.stge_tx_ring_paddr = ctx.stge_busaddr;
839
840         /* allocate DMA'able memory and load the DMA map for Rx ring. */
841         error = bus_dmamem_alloc(sc->sc_cdata.stge_rx_ring_tag,
842             (void **)&sc->sc_rdata.stge_rx_ring, BUS_DMA_NOWAIT |
843             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sc_cdata.stge_rx_ring_map);
844         if (error != 0) {
845                 device_printf(sc->sc_dev,
846                     "failed to allocate DMA'able memory for Rx ring\n");
847                 goto fail;
848         }
849
850         ctx.stge_busaddr = 0;
851         error = bus_dmamap_load(sc->sc_cdata.stge_rx_ring_tag,
852             sc->sc_cdata.stge_rx_ring_map, sc->sc_rdata.stge_rx_ring,
853             STGE_RX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
854         if (error != 0 || ctx.stge_busaddr == 0) {
855                 device_printf(sc->sc_dev,
856                     "failed to load DMA'able memory for Rx ring\n");
857                 goto fail;
858         }
859         sc->sc_rdata.stge_rx_ring_paddr = ctx.stge_busaddr;
860
861         /* create DMA maps for Tx buffers. */
862         for (i = 0; i < STGE_TX_RING_CNT; i++) {
863                 txd = &sc->sc_cdata.stge_txdesc[i];
864                 txd->tx_m = NULL;
865                 txd->tx_dmamap = 0;
866                 error = bus_dmamap_create(sc->sc_cdata.stge_tx_tag, 0,
867                     &txd->tx_dmamap);
868                 if (error != 0) {
869                         device_printf(sc->sc_dev,
870                             "failed to create Tx dmamap\n");
871                         goto fail;
872                 }
873         }
874         /* create DMA maps for Rx buffers. */
875         if ((error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
876             &sc->sc_cdata.stge_rx_sparemap)) != 0) {
877                 device_printf(sc->sc_dev, "failed to create spare Rx dmamap\n");
878                 goto fail;
879         }
880         for (i = 0; i < STGE_RX_RING_CNT; i++) {
881                 rxd = &sc->sc_cdata.stge_rxdesc[i];
882                 rxd->rx_m = NULL;
883                 rxd->rx_dmamap = 0;
884                 error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
885                     &rxd->rx_dmamap);
886                 if (error != 0) {
887                         device_printf(sc->sc_dev,
888                             "failed to create Rx dmamap\n");
889                         goto fail;
890                 }
891         }
892
893 fail:
894         return (error);
895 }
896
897 static void
898 stge_dma_free(struct stge_softc *sc)
899 {
900         struct stge_txdesc *txd;
901         struct stge_rxdesc *rxd;
902         int i;
903
904         /* Tx ring */
905         if (sc->sc_cdata.stge_tx_ring_tag) {
906                 if (sc->sc_cdata.stge_tx_ring_map)
907                         bus_dmamap_unload(sc->sc_cdata.stge_tx_ring_tag,
908                             sc->sc_cdata.stge_tx_ring_map);
909                 if (sc->sc_cdata.stge_tx_ring_map &&
910                     sc->sc_rdata.stge_tx_ring)
911                         bus_dmamem_free(sc->sc_cdata.stge_tx_ring_tag,
912                             sc->sc_rdata.stge_tx_ring,
913                             sc->sc_cdata.stge_tx_ring_map);
914                 sc->sc_rdata.stge_tx_ring = NULL;
915                 sc->sc_cdata.stge_tx_ring_map = 0;
916                 bus_dma_tag_destroy(sc->sc_cdata.stge_tx_ring_tag);
917                 sc->sc_cdata.stge_tx_ring_tag = NULL;
918         }
919         /* Rx ring */
920         if (sc->sc_cdata.stge_rx_ring_tag) {
921                 if (sc->sc_cdata.stge_rx_ring_map)
922                         bus_dmamap_unload(sc->sc_cdata.stge_rx_ring_tag,
923                             sc->sc_cdata.stge_rx_ring_map);
924                 if (sc->sc_cdata.stge_rx_ring_map &&
925                     sc->sc_rdata.stge_rx_ring)
926                         bus_dmamem_free(sc->sc_cdata.stge_rx_ring_tag,
927                             sc->sc_rdata.stge_rx_ring,
928                             sc->sc_cdata.stge_rx_ring_map);
929                 sc->sc_rdata.stge_rx_ring = NULL;
930                 sc->sc_cdata.stge_rx_ring_map = 0;
931                 bus_dma_tag_destroy(sc->sc_cdata.stge_rx_ring_tag);
932                 sc->sc_cdata.stge_rx_ring_tag = NULL;
933         }
934         /* Tx buffers */
935         if (sc->sc_cdata.stge_tx_tag) {
936                 for (i = 0; i < STGE_TX_RING_CNT; i++) {
937                         txd = &sc->sc_cdata.stge_txdesc[i];
938                         if (txd->tx_dmamap) {
939                                 bus_dmamap_destroy(sc->sc_cdata.stge_tx_tag,
940                                     txd->tx_dmamap);
941                                 txd->tx_dmamap = 0;
942                         }
943                 }
944                 bus_dma_tag_destroy(sc->sc_cdata.stge_tx_tag);
945                 sc->sc_cdata.stge_tx_tag = NULL;
946         }
947         /* Rx buffers */
948         if (sc->sc_cdata.stge_rx_tag) {
949                 for (i = 0; i < STGE_RX_RING_CNT; i++) {
950                         rxd = &sc->sc_cdata.stge_rxdesc[i];
951                         if (rxd->rx_dmamap) {
952                                 bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
953                                     rxd->rx_dmamap);
954                                 rxd->rx_dmamap = 0;
955                         }
956                 }
957                 if (sc->sc_cdata.stge_rx_sparemap) {
958                         bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
959                             sc->sc_cdata.stge_rx_sparemap);
960                         sc->sc_cdata.stge_rx_sparemap = 0;
961                 }
962                 bus_dma_tag_destroy(sc->sc_cdata.stge_rx_tag);
963                 sc->sc_cdata.stge_rx_tag = NULL;
964         }
965
966         if (sc->sc_cdata.stge_parent_tag) {
967                 bus_dma_tag_destroy(sc->sc_cdata.stge_parent_tag);
968                 sc->sc_cdata.stge_parent_tag = NULL;
969         }
970 }
971
972 /*
973  * stge_shutdown:
974  *
975  *      Make sure the interface is stopped at reboot time.
976  */
977 static int
978 stge_shutdown(device_t dev)
979 {
980
981         return (stge_suspend(dev));
982 }
983
984 static void
985 stge_setwol(struct stge_softc *sc)
986 {
987         struct ifnet *ifp;
988         uint8_t v;
989
990         STGE_LOCK_ASSERT(sc);
991
992         ifp = sc->sc_ifp;
993         v = CSR_READ_1(sc, STGE_WakeEvent);
994         /* Disable all WOL bits. */
995         v &= ~(WE_WakePktEnable | WE_MagicPktEnable | WE_LinkEventEnable |
996             WE_WakeOnLanEnable);
997         if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
998                 v |= WE_MagicPktEnable | WE_WakeOnLanEnable;
999         CSR_WRITE_1(sc, STGE_WakeEvent, v);
1000         /* Reset Tx and prevent transmission. */
1001         CSR_WRITE_4(sc, STGE_AsicCtrl,
1002             CSR_READ_4(sc, STGE_AsicCtrl) | AC_TxReset);
1003         /*
1004          * TC9021 automatically reset link speed to 100Mbps when it's put
1005          * into sleep so there is no need to try to resetting link speed.
1006          */
1007 }
1008
1009 static int
1010 stge_suspend(device_t dev)
1011 {
1012         struct stge_softc *sc;
1013
1014         sc = device_get_softc(dev);
1015
1016         STGE_LOCK(sc);
1017         stge_stop(sc);
1018         sc->sc_suspended = 1;
1019         stge_setwol(sc);
1020         STGE_UNLOCK(sc);
1021
1022         return (0);
1023 }
1024
1025 static int
1026 stge_resume(device_t dev)
1027 {
1028         struct stge_softc *sc;
1029         struct ifnet *ifp;
1030         uint8_t v;
1031
1032         sc = device_get_softc(dev);
1033
1034         STGE_LOCK(sc);
1035         /*
1036          * Clear WOL bits, so special frames wouldn't interfere
1037          * normal Rx operation anymore.
1038          */
1039         v = CSR_READ_1(sc, STGE_WakeEvent);
1040         v &= ~(WE_WakePktEnable | WE_MagicPktEnable | WE_LinkEventEnable |
1041             WE_WakeOnLanEnable);
1042         CSR_WRITE_1(sc, STGE_WakeEvent, v);
1043         ifp = sc->sc_ifp;
1044         if (ifp->if_flags & IFF_UP)
1045                 stge_init_locked(sc);
1046
1047         sc->sc_suspended = 0;
1048         STGE_UNLOCK(sc);
1049
1050         return (0);
1051 }
1052
1053 static void
1054 stge_dma_wait(struct stge_softc *sc)
1055 {
1056         int i;
1057
1058         for (i = 0; i < STGE_TIMEOUT; i++) {
1059                 DELAY(2);
1060                 if ((CSR_READ_4(sc, STGE_DMACtrl) & DMAC_TxDMAInProg) == 0)
1061                         break;
1062         }
1063
1064         if (i == STGE_TIMEOUT)
1065                 device_printf(sc->sc_dev, "DMA wait timed out\n");
1066 }
1067
1068 static int
1069 stge_encap(struct stge_softc *sc, struct mbuf **m_head)
1070 {
1071         struct stge_txdesc *txd;
1072         struct stge_tfd *tfd;
1073         struct mbuf *m;
1074         bus_dma_segment_t txsegs[STGE_MAXTXSEGS];
1075         int error, i, nsegs, si;
1076         uint64_t csum_flags, tfc;
1077
1078         STGE_LOCK_ASSERT(sc);
1079
1080         if ((txd = STAILQ_FIRST(&sc->sc_cdata.stge_txfreeq)) == NULL)
1081                 return (ENOBUFS);
1082
1083         error =  bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_tx_tag,
1084             txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1085         if (error == EFBIG) {
1086                 m = m_collapse(*m_head, M_DONTWAIT, STGE_MAXTXSEGS);
1087                 if (m == NULL) {
1088                         m_freem(*m_head);
1089                         *m_head = NULL;
1090                         return (ENOMEM);
1091                 }
1092                 *m_head = m;
1093                 error = bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_tx_tag,
1094                     txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1095                 if (error != 0) {
1096                         m_freem(*m_head);
1097                         *m_head = NULL;
1098                         return (error);
1099                 }
1100         } else if (error != 0)
1101                 return (error);
1102         if (nsegs == 0) {
1103                 m_freem(*m_head);
1104                 *m_head = NULL;
1105                 return (EIO);
1106         }
1107
1108         m = *m_head;
1109         csum_flags = 0;
1110         if ((m->m_pkthdr.csum_flags & STGE_CSUM_FEATURES) != 0) {
1111                 if (m->m_pkthdr.csum_flags & CSUM_IP)
1112                         csum_flags |= TFD_IPChecksumEnable;
1113                 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1114                         csum_flags |= TFD_TCPChecksumEnable;
1115                 else if (m->m_pkthdr.csum_flags & CSUM_UDP)
1116                         csum_flags |= TFD_UDPChecksumEnable;
1117         }
1118
1119         si = sc->sc_cdata.stge_tx_prod;
1120         tfd = &sc->sc_rdata.stge_tx_ring[si];
1121         for (i = 0; i < nsegs; i++)
1122                 tfd->tfd_frags[i].frag_word0 =
1123                     htole64(FRAG_ADDR(txsegs[i].ds_addr) |
1124                     FRAG_LEN(txsegs[i].ds_len));
1125         sc->sc_cdata.stge_tx_cnt++;
1126
1127         tfc = TFD_FrameId(si) | TFD_WordAlign(TFD_WordAlign_disable) |
1128             TFD_FragCount(nsegs) | csum_flags;
1129         if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT)
1130                 tfc |= TFD_TxDMAIndicate;
1131
1132         /* Update producer index. */
1133         sc->sc_cdata.stge_tx_prod = (si + 1) % STGE_TX_RING_CNT;
1134
1135         /* Check if we have a VLAN tag to insert. */
1136         if (m->m_flags & M_VLANTAG)
1137                 tfc |= (TFD_VLANTagInsert | TFD_VID(m->m_pkthdr.ether_vtag));
1138         tfd->tfd_control = htole64(tfc);
1139
1140         /* Update Tx Queue. */
1141         STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txfreeq, tx_q);
1142         STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txbusyq, txd, tx_q);
1143         txd->tx_m = m;
1144
1145         /* Sync descriptors. */
1146         bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1147             BUS_DMASYNC_PREWRITE);
1148         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1149             sc->sc_cdata.stge_tx_ring_map,
1150             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1151
1152         return (0);
1153 }
1154
1155 /*
1156  * stge_start:          [ifnet interface function]
1157  *
1158  *      Start packet transmission on the interface.
1159  */
1160 static void
1161 stge_start(struct ifnet *ifp)
1162 {
1163         struct stge_softc *sc;
1164
1165         sc = ifp->if_softc;
1166         STGE_LOCK(sc);
1167         stge_start_locked(ifp);
1168         STGE_UNLOCK(sc);
1169 }
1170
1171 static void
1172 stge_start_locked(struct ifnet *ifp)
1173 {
1174         struct stge_softc *sc;
1175         struct mbuf *m_head;
1176         int enq;
1177
1178         sc = ifp->if_softc;
1179
1180         STGE_LOCK_ASSERT(sc);
1181
1182         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
1183             IFF_DRV_RUNNING || sc->sc_link == 0)
1184                 return;
1185
1186         for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
1187                 if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT) {
1188                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1189                         break;
1190                 }
1191
1192                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1193                 if (m_head == NULL)
1194                         break;
1195                 /*
1196                  * Pack the data into the transmit ring. If we
1197                  * don't have room, set the OACTIVE flag and wait
1198                  * for the NIC to drain the ring.
1199                  */
1200                 if (stge_encap(sc, &m_head)) {
1201                         if (m_head == NULL)
1202                                 break;
1203                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1204                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1205                         break;
1206                 }
1207
1208                 enq++;
1209                 /*
1210                  * If there's a BPF listener, bounce a copy of this frame
1211                  * to him.
1212                  */
1213                 ETHER_BPF_MTAP(ifp, m_head);
1214         }
1215
1216         if (enq > 0) {
1217                 /* Transmit */
1218                 CSR_WRITE_4(sc, STGE_DMACtrl, DMAC_TxDMAPollNow);
1219
1220                 /* Set a timeout in case the chip goes out to lunch. */
1221                 sc->sc_watchdog_timer = 5;
1222         }
1223 }
1224
1225 /*
1226  * stge_watchdog:
1227  *
1228  *      Watchdog timer handler.
1229  */
1230 static void
1231 stge_watchdog(struct stge_softc *sc)
1232 {
1233         struct ifnet *ifp;
1234
1235         STGE_LOCK_ASSERT(sc);
1236
1237         if (sc->sc_watchdog_timer == 0 || --sc->sc_watchdog_timer)
1238                 return;
1239
1240         ifp = sc->sc_ifp;
1241         if_printf(sc->sc_ifp, "device timeout\n");
1242         ifp->if_oerrors++;
1243         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1244         stge_init_locked(sc);
1245         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1246                 stge_start_locked(ifp);
1247 }
1248
1249 /*
1250  * stge_ioctl:          [ifnet interface function]
1251  *
1252  *      Handle control requests from the operator.
1253  */
1254 static int
1255 stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1256 {
1257         struct stge_softc *sc;
1258         struct ifreq *ifr;
1259         struct mii_data *mii;
1260         int error, mask;
1261
1262         sc = ifp->if_softc;
1263         ifr = (struct ifreq *)data;
1264         error = 0;
1265         switch (cmd) {
1266         case SIOCSIFMTU:
1267                 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > STGE_JUMBO_MTU)
1268                         error = EINVAL;
1269                 else if (ifp->if_mtu != ifr->ifr_mtu) {
1270                         ifp->if_mtu = ifr->ifr_mtu;
1271                         STGE_LOCK(sc);
1272                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1273                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1274                                 stge_init_locked(sc);
1275                         }
1276                         STGE_UNLOCK(sc);
1277                 }
1278                 break;
1279         case SIOCSIFFLAGS:
1280                 STGE_LOCK(sc);
1281                 if ((ifp->if_flags & IFF_UP) != 0) {
1282                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1283                                 if (((ifp->if_flags ^ sc->sc_if_flags)
1284                                     & IFF_PROMISC) != 0)
1285                                         stge_set_filter(sc);
1286                         } else {
1287                                 if (sc->sc_detach == 0)
1288                                         stge_init_locked(sc);
1289                         }
1290                 } else {
1291                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1292                                 stge_stop(sc);
1293                 }
1294                 sc->sc_if_flags = ifp->if_flags;
1295                 STGE_UNLOCK(sc);
1296                 break;
1297         case SIOCADDMULTI:
1298         case SIOCDELMULTI:
1299                 STGE_LOCK(sc);
1300                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1301                         stge_set_multi(sc);
1302                 STGE_UNLOCK(sc);
1303                 break;
1304         case SIOCSIFMEDIA:
1305         case SIOCGIFMEDIA:
1306                 mii = device_get_softc(sc->sc_miibus);
1307                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1308                 break;
1309         case SIOCSIFCAP:
1310                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1311 #ifdef DEVICE_POLLING
1312                 if ((mask & IFCAP_POLLING) != 0) {
1313                         if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1314                                 error = ether_poll_register(stge_poll, ifp);
1315                                 if (error != 0)
1316                                         break;
1317                                 STGE_LOCK(sc);
1318                                 CSR_WRITE_2(sc, STGE_IntEnable, 0);
1319                                 ifp->if_capenable |= IFCAP_POLLING;
1320                                 STGE_UNLOCK(sc);
1321                         } else {
1322                                 error = ether_poll_deregister(ifp);
1323                                 if (error != 0)
1324                                         break;
1325                                 STGE_LOCK(sc);
1326                                 CSR_WRITE_2(sc, STGE_IntEnable,
1327                                     sc->sc_IntEnable);
1328                                 ifp->if_capenable &= ~IFCAP_POLLING;
1329                                 STGE_UNLOCK(sc);
1330                         }
1331                 }
1332 #endif
1333                 if ((mask & IFCAP_HWCSUM) != 0) {
1334                         ifp->if_capenable ^= IFCAP_HWCSUM;
1335                         if ((IFCAP_HWCSUM & ifp->if_capenable) != 0 &&
1336                             (IFCAP_HWCSUM & ifp->if_capabilities) != 0)
1337                                 ifp->if_hwassist = STGE_CSUM_FEATURES;
1338                         else
1339                                 ifp->if_hwassist = 0;
1340                 }
1341                 if ((mask & IFCAP_WOL) != 0 &&
1342                     (ifp->if_capabilities & IFCAP_WOL) != 0) {
1343                         if ((mask & IFCAP_WOL_MAGIC) != 0)
1344                                 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
1345                 }
1346                 if ((mask & IFCAP_VLAN_HWTAGGING) != 0) {
1347                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1348                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1349                                 STGE_LOCK(sc);
1350                                 stge_vlan_setup(sc);
1351                                 STGE_UNLOCK(sc);
1352                         }
1353                 }
1354                 VLAN_CAPABILITIES(ifp);
1355                 break;
1356         default:
1357                 error = ether_ioctl(ifp, cmd, data);
1358                 break;
1359         }
1360
1361         return (error);
1362 }
1363
1364 static void
1365 stge_link_task(void *arg, int pending)
1366 {
1367         struct stge_softc *sc;
1368         struct mii_data *mii;
1369         uint32_t v, ac;
1370         int i;
1371
1372         sc = (struct stge_softc *)arg;
1373         STGE_LOCK(sc);
1374
1375         mii = device_get_softc(sc->sc_miibus);
1376         if (mii->mii_media_status & IFM_ACTIVE) {
1377                 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
1378                         sc->sc_link = 1;
1379         } else
1380                 sc->sc_link = 0;
1381
1382         sc->sc_MACCtrl = 0;
1383         if (((mii->mii_media_active & IFM_GMASK) & IFM_FDX) != 0)
1384                 sc->sc_MACCtrl |= MC_DuplexSelect;
1385         if (((mii->mii_media_active & IFM_GMASK) & IFM_ETH_RXPAUSE) != 0)
1386                 sc->sc_MACCtrl |= MC_RxFlowControlEnable;
1387         if (((mii->mii_media_active & IFM_GMASK) & IFM_ETH_TXPAUSE) != 0)
1388                 sc->sc_MACCtrl |= MC_TxFlowControlEnable;
1389         /*
1390          * Update STGE_MACCtrl register depending on link status.
1391          * (duplex, flow control etc)
1392          */
1393         v = ac = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
1394         v &= ~(MC_DuplexSelect|MC_RxFlowControlEnable|MC_TxFlowControlEnable);
1395         v |= sc->sc_MACCtrl;
1396         CSR_WRITE_4(sc, STGE_MACCtrl, v);
1397         if (((ac ^ sc->sc_MACCtrl) & MC_DuplexSelect) != 0) {
1398                 /* Duplex setting changed, reset Tx/Rx functions. */
1399                 ac = CSR_READ_4(sc, STGE_AsicCtrl);
1400                 ac |= AC_TxReset | AC_RxReset;
1401                 CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1402                 for (i = 0; i < STGE_TIMEOUT; i++) {
1403                         DELAY(100);
1404                         if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1405                                 break;
1406                 }
1407                 if (i == STGE_TIMEOUT)
1408                         device_printf(sc->sc_dev, "reset failed to complete\n");
1409         }
1410         STGE_UNLOCK(sc);
1411 }
1412
1413 static __inline int
1414 stge_tx_error(struct stge_softc *sc)
1415 {
1416         uint32_t txstat;
1417         int error;
1418
1419         for (error = 0;;) {
1420                 txstat = CSR_READ_4(sc, STGE_TxStatus);
1421                 if ((txstat & TS_TxComplete) == 0)
1422                         break;
1423                 /* Tx underrun */
1424                 if ((txstat & TS_TxUnderrun) != 0) {
1425                         /*
1426                          * XXX
1427                          * There should be a more better way to recover
1428                          * from Tx underrun instead of a full reset.
1429                          */
1430                         if (sc->sc_nerr++ < STGE_MAXERR)
1431                                 device_printf(sc->sc_dev, "Tx underrun, "
1432                                     "resetting...\n");
1433                         if (sc->sc_nerr == STGE_MAXERR)
1434                                 device_printf(sc->sc_dev, "too many errors; "
1435                                     "not reporting any more\n");
1436                         error = -1;
1437                         break;
1438                 }
1439                 /* Maximum/Late collisions, Re-enable Tx MAC. */
1440                 if ((txstat & (TS_MaxCollisions|TS_LateCollision)) != 0)
1441                         CSR_WRITE_4(sc, STGE_MACCtrl,
1442                             (CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK) |
1443                             MC_TxEnable);
1444         }
1445
1446         return (error);
1447 }
1448
1449 /*
1450  * stge_intr:
1451  *
1452  *      Interrupt service routine.
1453  */
1454 static void
1455 stge_intr(void *arg)
1456 {
1457         struct stge_softc *sc;
1458         struct ifnet *ifp;
1459         int reinit;
1460         uint16_t status;
1461
1462         sc = (struct stge_softc *)arg;
1463         ifp = sc->sc_ifp;
1464
1465         STGE_LOCK(sc);
1466
1467 #ifdef DEVICE_POLLING
1468         if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1469                 goto done_locked;
1470 #endif
1471         status = CSR_READ_2(sc, STGE_IntStatus);
1472         if (sc->sc_suspended || (status & IS_InterruptStatus) == 0)
1473                 goto done_locked;
1474
1475         /* Disable interrupts. */
1476         for (reinit = 0;;) {
1477                 status = CSR_READ_2(sc, STGE_IntStatusAck);
1478                 status &= sc->sc_IntEnable;
1479                 if (status == 0)
1480                         break;
1481                 /* Host interface errors. */
1482                 if ((status & IS_HostError) != 0) {
1483                         device_printf(sc->sc_dev,
1484                             "Host interface error, resetting...\n");
1485                         reinit = 1;
1486                         goto force_init;
1487                 }
1488
1489                 /* Receive interrupts. */
1490                 if ((status & IS_RxDMAComplete) != 0) {
1491                         stge_rxeof(sc);
1492                         if ((status & IS_RFDListEnd) != 0)
1493                                 CSR_WRITE_4(sc, STGE_DMACtrl,
1494                                     DMAC_RxDMAPollNow);
1495                 }
1496
1497                 /* Transmit interrupts. */
1498                 if ((status & (IS_TxDMAComplete | IS_TxComplete)) != 0)
1499                         stge_txeof(sc);
1500
1501                 /* Transmission errors.*/
1502                 if ((status & IS_TxComplete) != 0) {
1503                         if ((reinit = stge_tx_error(sc)) != 0)
1504                                 break;
1505                 }
1506         }
1507
1508 force_init:
1509         if (reinit != 0) {
1510                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1511                 stge_init_locked(sc);
1512         }
1513
1514         /* Re-enable interrupts. */
1515         CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
1516
1517         /* Try to get more packets going. */
1518         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1519                 stge_start_locked(ifp);
1520
1521 done_locked:
1522         STGE_UNLOCK(sc);
1523 }
1524
1525 /*
1526  * stge_txeof:
1527  *
1528  *      Helper; handle transmit interrupts.
1529  */
1530 static void
1531 stge_txeof(struct stge_softc *sc)
1532 {
1533         struct ifnet *ifp;
1534         struct stge_txdesc *txd;
1535         uint64_t control;
1536         int cons;
1537
1538         STGE_LOCK_ASSERT(sc);
1539
1540         ifp = sc->sc_ifp;
1541
1542         txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1543         if (txd == NULL)
1544                 return;
1545         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1546             sc->sc_cdata.stge_tx_ring_map, BUS_DMASYNC_POSTREAD);
1547
1548         /*
1549          * Go through our Tx list and free mbufs for those
1550          * frames which have been transmitted.
1551          */
1552         for (cons = sc->sc_cdata.stge_tx_cons;;
1553             cons = (cons + 1) % STGE_TX_RING_CNT) {
1554                 if (sc->sc_cdata.stge_tx_cnt <= 0)
1555                         break;
1556                 control = le64toh(sc->sc_rdata.stge_tx_ring[cons].tfd_control);
1557                 if ((control & TFD_TFDDone) == 0)
1558                         break;
1559                 sc->sc_cdata.stge_tx_cnt--;
1560                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1561
1562                 bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1563                     BUS_DMASYNC_POSTWRITE);
1564                 bus_dmamap_unload(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap);
1565
1566                 /* Output counter is updated with statistics register */
1567                 m_freem(txd->tx_m);
1568                 txd->tx_m = NULL;
1569                 STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txbusyq, tx_q);
1570                 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
1571                 txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1572         }
1573         sc->sc_cdata.stge_tx_cons = cons;
1574         if (sc->sc_cdata.stge_tx_cnt == 0)
1575                 sc->sc_watchdog_timer = 0;
1576
1577         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1578             sc->sc_cdata.stge_tx_ring_map,
1579             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1580 }
1581
1582 static __inline void
1583 stge_discard_rxbuf(struct stge_softc *sc, int idx)
1584 {
1585         struct stge_rfd *rfd;
1586
1587         rfd = &sc->sc_rdata.stge_rx_ring[idx];
1588         rfd->rfd_status = 0;
1589 }
1590
1591 #ifndef __NO_STRICT_ALIGNMENT
1592 /*
1593  * It seems that TC9021's DMA engine has alignment restrictions in
1594  * DMA scatter operations. The first DMA segment has no address
1595  * alignment restrictins but the rest should be aligned on 4(?) bytes
1596  * boundary. Otherwise it would corrupt random memory. Since we don't
1597  * know which one is used for the first segment in advance we simply
1598  * don't align at all.
1599  * To avoid copying over an entire frame to align, we allocate a new
1600  * mbuf and copy ethernet header to the new mbuf. The new mbuf is
1601  * prepended into the existing mbuf chain.
1602  */
1603 static __inline struct mbuf *
1604 stge_fixup_rx(struct stge_softc *sc, struct mbuf *m)
1605 {
1606         struct mbuf *n;
1607
1608         n = NULL;
1609         if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
1610                 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
1611                 m->m_data += ETHER_HDR_LEN;
1612                 n = m;
1613         } else {
1614                 MGETHDR(n, M_DONTWAIT, MT_DATA);
1615                 if (n != NULL) {
1616                         bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
1617                         m->m_data += ETHER_HDR_LEN;
1618                         m->m_len -= ETHER_HDR_LEN;
1619                         n->m_len = ETHER_HDR_LEN;
1620                         M_MOVE_PKTHDR(n, m);
1621                         n->m_next = m;
1622                 } else
1623                         m_freem(m);
1624         }
1625
1626         return (n);
1627 }
1628 #endif
1629
1630 /*
1631  * stge_rxeof:
1632  *
1633  *      Helper; handle receive interrupts.
1634  */
1635 static int
1636 stge_rxeof(struct stge_softc *sc)
1637 {
1638         struct ifnet *ifp;
1639         struct stge_rxdesc *rxd;
1640         struct mbuf *mp, *m;
1641         uint64_t status64;
1642         uint32_t status;
1643         int cons, prog, rx_npkts;
1644
1645         STGE_LOCK_ASSERT(sc);
1646
1647         rx_npkts = 0;
1648         ifp = sc->sc_ifp;
1649
1650         bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1651             sc->sc_cdata.stge_rx_ring_map, BUS_DMASYNC_POSTREAD);
1652
1653         prog = 0;
1654         for (cons = sc->sc_cdata.stge_rx_cons; prog < STGE_RX_RING_CNT;
1655             prog++, cons = (cons + 1) % STGE_RX_RING_CNT) {
1656                 status64 = le64toh(sc->sc_rdata.stge_rx_ring[cons].rfd_status);
1657                 status = RFD_RxStatus(status64);
1658                 if ((status & RFD_RFDDone) == 0)
1659                         break;
1660 #ifdef DEVICE_POLLING
1661                 if (ifp->if_capenable & IFCAP_POLLING) {
1662                         if (sc->sc_cdata.stge_rxcycles <= 0)
1663                                 break;
1664                         sc->sc_cdata.stge_rxcycles--;
1665                 }
1666 #endif
1667                 prog++;
1668                 rxd = &sc->sc_cdata.stge_rxdesc[cons];
1669                 mp = rxd->rx_m;
1670
1671                 /*
1672                  * If the packet had an error, drop it.  Note we count
1673                  * the error later in the periodic stats update.
1674                  */
1675                 if ((status & RFD_FrameEnd) != 0 && (status &
1676                     (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
1677                     RFD_RxAlignmentError | RFD_RxFCSError |
1678                     RFD_RxLengthError)) != 0) {
1679                         stge_discard_rxbuf(sc, cons);
1680                         if (sc->sc_cdata.stge_rxhead != NULL) {
1681                                 m_freem(sc->sc_cdata.stge_rxhead);
1682                                 STGE_RXCHAIN_RESET(sc);
1683                         }
1684                         continue;
1685                 }
1686                 /*
1687                  * Add a new receive buffer to the ring.
1688                  */
1689                 if (stge_newbuf(sc, cons) != 0) {
1690                         ifp->if_iqdrops++;
1691                         stge_discard_rxbuf(sc, cons);
1692                         if (sc->sc_cdata.stge_rxhead != NULL) {
1693                                 m_freem(sc->sc_cdata.stge_rxhead);
1694                                 STGE_RXCHAIN_RESET(sc);
1695                         }
1696                         continue;
1697                 }
1698
1699                 if ((status & RFD_FrameEnd) != 0)
1700                         mp->m_len = RFD_RxDMAFrameLen(status) -
1701                             sc->sc_cdata.stge_rxlen;
1702                 sc->sc_cdata.stge_rxlen += mp->m_len;
1703
1704                 /* Chain mbufs. */
1705                 if (sc->sc_cdata.stge_rxhead == NULL) {
1706                         sc->sc_cdata.stge_rxhead = mp;
1707                         sc->sc_cdata.stge_rxtail = mp;
1708                 } else {
1709                         mp->m_flags &= ~M_PKTHDR;
1710                         sc->sc_cdata.stge_rxtail->m_next = mp;
1711                         sc->sc_cdata.stge_rxtail = mp;
1712                 }
1713
1714                 if ((status & RFD_FrameEnd) != 0) {
1715                         m = sc->sc_cdata.stge_rxhead;
1716                         m->m_pkthdr.rcvif = ifp;
1717                         m->m_pkthdr.len = sc->sc_cdata.stge_rxlen;
1718
1719                         if (m->m_pkthdr.len > sc->sc_if_framesize) {
1720                                 m_freem(m);
1721                                 STGE_RXCHAIN_RESET(sc);
1722                                 continue;
1723                         }
1724                         /*
1725                          * Set the incoming checksum information for
1726                          * the packet.
1727                          */
1728                         if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1729                                 if ((status & RFD_IPDetected) != 0) {
1730                                         m->m_pkthdr.csum_flags |=
1731                                                 CSUM_IP_CHECKED;
1732                                         if ((status & RFD_IPError) == 0)
1733                                                 m->m_pkthdr.csum_flags |=
1734                                                     CSUM_IP_VALID;
1735                                 }
1736                                 if (((status & RFD_TCPDetected) != 0 &&
1737                                     (status & RFD_TCPError) == 0) ||
1738                                     ((status & RFD_UDPDetected) != 0 &&
1739                                     (status & RFD_UDPError) == 0)) {
1740                                         m->m_pkthdr.csum_flags |=
1741                                             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1742                                         m->m_pkthdr.csum_data = 0xffff;
1743                                 }
1744                         }
1745
1746 #ifndef __NO_STRICT_ALIGNMENT
1747                         if (sc->sc_if_framesize > (MCLBYTES - ETHER_ALIGN)) {
1748                                 if ((m = stge_fixup_rx(sc, m)) == NULL) {
1749                                         STGE_RXCHAIN_RESET(sc);
1750                                         continue;
1751                                 }
1752                         }
1753 #endif
1754                         /* Check for VLAN tagged packets. */
1755                         if ((status & RFD_VLANDetected) != 0 &&
1756                             (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1757                                 m->m_pkthdr.ether_vtag = RFD_TCI(status64);
1758                                 m->m_flags |= M_VLANTAG;
1759                         }
1760
1761                         STGE_UNLOCK(sc);
1762                         /* Pass it on. */
1763                         (*ifp->if_input)(ifp, m);
1764                         STGE_LOCK(sc);
1765                         rx_npkts++;
1766
1767                         STGE_RXCHAIN_RESET(sc);
1768                 }
1769         }
1770
1771         if (prog > 0) {
1772                 /* Update the consumer index. */
1773                 sc->sc_cdata.stge_rx_cons = cons;
1774                 bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1775                     sc->sc_cdata.stge_rx_ring_map,
1776                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1777         }
1778         return (rx_npkts);
1779 }
1780
1781 #ifdef DEVICE_POLLING
1782 static int
1783 stge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1784 {
1785         struct stge_softc *sc;
1786         uint16_t status;
1787         int rx_npkts;
1788
1789         rx_npkts = 0;
1790         sc = ifp->if_softc;
1791         STGE_LOCK(sc);
1792         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1793                 STGE_UNLOCK(sc);
1794                 return (rx_npkts);
1795         }
1796
1797         sc->sc_cdata.stge_rxcycles = count;
1798         rx_npkts = stge_rxeof(sc);
1799         stge_txeof(sc);
1800
1801         if (cmd == POLL_AND_CHECK_STATUS) {
1802                 status = CSR_READ_2(sc, STGE_IntStatus);
1803                 status &= sc->sc_IntEnable;
1804                 if (status != 0) {
1805                         if ((status & IS_HostError) != 0) {
1806                                 device_printf(sc->sc_dev,
1807                                     "Host interface error, resetting...\n");
1808                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1809                                 stge_init_locked(sc);
1810                         }
1811                         if ((status & IS_TxComplete) != 0) {
1812                                 if (stge_tx_error(sc) != 0) {
1813                                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1814                                         stge_init_locked(sc);
1815                                 }
1816                         }
1817                 }
1818
1819         }
1820
1821         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1822                 stge_start_locked(ifp);
1823
1824         STGE_UNLOCK(sc);
1825         return (rx_npkts);
1826 }
1827 #endif  /* DEVICE_POLLING */
1828
1829 /*
1830  * stge_tick:
1831  *
1832  *      One second timer, used to tick the MII.
1833  */
1834 static void
1835 stge_tick(void *arg)
1836 {
1837         struct stge_softc *sc;
1838         struct mii_data *mii;
1839
1840         sc = (struct stge_softc *)arg;
1841
1842         STGE_LOCK_ASSERT(sc);
1843
1844         mii = device_get_softc(sc->sc_miibus);
1845         mii_tick(mii);
1846
1847         /* Update statistics counters. */
1848         stge_stats_update(sc);
1849
1850         /*
1851          * Relcaim any pending Tx descriptors to release mbufs in a
1852          * timely manner as we don't generate Tx completion interrupts
1853          * for every frame. This limits the delay to a maximum of one
1854          * second.
1855          */
1856         if (sc->sc_cdata.stge_tx_cnt != 0)
1857                 stge_txeof(sc);
1858
1859         stge_watchdog(sc);
1860
1861         callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1862 }
1863
1864 /*
1865  * stge_stats_update:
1866  *
1867  *      Read the TC9021 statistics counters.
1868  */
1869 static void
1870 stge_stats_update(struct stge_softc *sc)
1871 {
1872         struct ifnet *ifp;
1873
1874         STGE_LOCK_ASSERT(sc);
1875
1876         ifp = sc->sc_ifp;
1877
1878         CSR_READ_4(sc,STGE_OctetRcvOk);
1879
1880         ifp->if_ipackets += CSR_READ_4(sc, STGE_FramesRcvdOk);
1881
1882         ifp->if_ierrors += CSR_READ_2(sc, STGE_FramesLostRxErrors);
1883
1884         CSR_READ_4(sc, STGE_OctetXmtdOk);
1885
1886         ifp->if_opackets += CSR_READ_4(sc, STGE_FramesXmtdOk);
1887
1888         ifp->if_collisions +=
1889             CSR_READ_4(sc, STGE_LateCollisions) +
1890             CSR_READ_4(sc, STGE_MultiColFrames) +
1891             CSR_READ_4(sc, STGE_SingleColFrames);
1892
1893         ifp->if_oerrors +=
1894             CSR_READ_2(sc, STGE_FramesAbortXSColls) +
1895             CSR_READ_2(sc, STGE_FramesWEXDeferal);
1896 }
1897
1898 /*
1899  * stge_reset:
1900  *
1901  *      Perform a soft reset on the TC9021.
1902  */
1903 static void
1904 stge_reset(struct stge_softc *sc, uint32_t how)
1905 {
1906         uint32_t ac;
1907         uint8_t v;
1908         int i, dv;
1909
1910         STGE_LOCK_ASSERT(sc);
1911
1912         dv = 5000;
1913         ac = CSR_READ_4(sc, STGE_AsicCtrl);
1914         switch (how) {
1915         case STGE_RESET_TX:
1916                 ac |= AC_TxReset | AC_FIFO;
1917                 dv = 100;
1918                 break;
1919         case STGE_RESET_RX:
1920                 ac |= AC_RxReset | AC_FIFO;
1921                 dv = 100;
1922                 break;
1923         case STGE_RESET_FULL:
1924         default:
1925                 /*
1926                  * Only assert RstOut if we're fiber.  We need GMII clocks
1927                  * to be present in order for the reset to complete on fiber
1928                  * cards.
1929                  */
1930                 ac |= AC_GlobalReset | AC_RxReset | AC_TxReset |
1931                     AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
1932                     (sc->sc_usefiber ? AC_RstOut : 0);
1933                 break;
1934         }
1935
1936         CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1937
1938         /* Account for reset problem at 10Mbps. */
1939         DELAY(dv);
1940
1941         for (i = 0; i < STGE_TIMEOUT; i++) {
1942                 if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1943                         break;
1944                 DELAY(dv);
1945         }
1946
1947         if (i == STGE_TIMEOUT)
1948                 device_printf(sc->sc_dev, "reset failed to complete\n");
1949
1950         /* Set LED, from Linux IPG driver. */
1951         ac = CSR_READ_4(sc, STGE_AsicCtrl);
1952         ac &= ~(AC_LEDMode | AC_LEDSpeed | AC_LEDModeBit1);
1953         if ((sc->sc_led & 0x01) != 0)
1954                 ac |= AC_LEDMode;
1955         if ((sc->sc_led & 0x03) != 0)
1956                 ac |= AC_LEDModeBit1;
1957         if ((sc->sc_led & 0x08) != 0)
1958                 ac |= AC_LEDSpeed;
1959         CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1960
1961         /* Set PHY, from Linux IPG driver */
1962         v = CSR_READ_1(sc, STGE_PhySet);
1963         v &= ~(PS_MemLenb9b | PS_MemLen | PS_NonCompdet);
1964         v |= ((sc->sc_led & 0x70) >> 4);
1965         CSR_WRITE_1(sc, STGE_PhySet, v);
1966 }
1967
1968 /*
1969  * stge_init:           [ ifnet interface function ]
1970  *
1971  *      Initialize the interface.
1972  */
1973 static void
1974 stge_init(void *xsc)
1975 {
1976         struct stge_softc *sc;
1977
1978         sc = (struct stge_softc *)xsc;
1979         STGE_LOCK(sc);
1980         stge_init_locked(sc);
1981         STGE_UNLOCK(sc);
1982 }
1983
1984 static void
1985 stge_init_locked(struct stge_softc *sc)
1986 {
1987         struct ifnet *ifp;
1988         struct mii_data *mii;
1989         uint16_t eaddr[3];
1990         uint32_t v;
1991         int error;
1992
1993         STGE_LOCK_ASSERT(sc);
1994
1995         ifp = sc->sc_ifp;
1996         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1997                 return;
1998         mii = device_get_softc(sc->sc_miibus);
1999
2000         /*
2001          * Cancel any pending I/O.
2002          */
2003         stge_stop(sc);
2004
2005         /*
2006          * Reset the chip to a known state.
2007          */
2008         stge_reset(sc, STGE_RESET_FULL);
2009
2010         /* Init descriptors. */
2011         error = stge_init_rx_ring(sc);
2012         if (error != 0) {
2013                 device_printf(sc->sc_dev,
2014                     "initialization failed: no memory for rx buffers\n");
2015                 stge_stop(sc);
2016                 goto out;
2017         }
2018         stge_init_tx_ring(sc);
2019
2020         /* Set the station address. */
2021         bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2022         CSR_WRITE_2(sc, STGE_StationAddress0, htole16(eaddr[0]));
2023         CSR_WRITE_2(sc, STGE_StationAddress1, htole16(eaddr[1]));
2024         CSR_WRITE_2(sc, STGE_StationAddress2, htole16(eaddr[2]));
2025
2026         /*
2027          * Set the statistics masks.  Disable all the RMON stats,
2028          * and disable selected stats in the non-RMON stats registers.
2029          */
2030         CSR_WRITE_4(sc, STGE_RMONStatisticsMask, 0xffffffff);
2031         CSR_WRITE_4(sc, STGE_StatisticsMask,
2032             (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
2033             (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
2034             (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
2035             (1U << 21));
2036
2037         /* Set up the receive filter. */
2038         stge_set_filter(sc);
2039         /* Program multicast filter. */
2040         stge_set_multi(sc);
2041
2042         /*
2043          * Give the transmit and receive ring to the chip.
2044          */
2045         CSR_WRITE_4(sc, STGE_TFDListPtrHi,
2046             STGE_ADDR_HI(STGE_TX_RING_ADDR(sc, 0)));
2047         CSR_WRITE_4(sc, STGE_TFDListPtrLo,
2048             STGE_ADDR_LO(STGE_TX_RING_ADDR(sc, 0)));
2049
2050         CSR_WRITE_4(sc, STGE_RFDListPtrHi,
2051             STGE_ADDR_HI(STGE_RX_RING_ADDR(sc, 0)));
2052         CSR_WRITE_4(sc, STGE_RFDListPtrLo,
2053             STGE_ADDR_LO(STGE_RX_RING_ADDR(sc, 0)));
2054
2055         /*
2056          * Initialize the Tx auto-poll period.  It's OK to make this number
2057          * large (255 is the max, but we use 127) -- we explicitly kick the
2058          * transmit engine when there's actually a packet.
2059          */
2060         CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2061
2062         /* ..and the Rx auto-poll period. */
2063         CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2064
2065         /* Initialize the Tx start threshold. */
2066         CSR_WRITE_2(sc, STGE_TxStartThresh, sc->sc_txthresh);
2067
2068         /* Rx DMA thresholds, from Linux */
2069         CSR_WRITE_1(sc, STGE_RxDMABurstThresh, 0x30);
2070         CSR_WRITE_1(sc, STGE_RxDMAUrgentThresh, 0x30);
2071
2072         /* Rx early threhold, from Linux */
2073         CSR_WRITE_2(sc, STGE_RxEarlyThresh, 0x7ff);
2074
2075         /* Tx DMA thresholds, from Linux */
2076         CSR_WRITE_1(sc, STGE_TxDMABurstThresh, 0x30);
2077         CSR_WRITE_1(sc, STGE_TxDMAUrgentThresh, 0x04);
2078
2079         /*
2080          * Initialize the Rx DMA interrupt control register.  We
2081          * request an interrupt after every incoming packet, but
2082          * defer it for sc_rxint_dmawait us. When the number of
2083          * interrupts pending reaches STGE_RXINT_NFRAME, we stop
2084          * deferring the interrupt, and signal it immediately.
2085          */
2086         CSR_WRITE_4(sc, STGE_RxDMAIntCtrl,
2087             RDIC_RxFrameCount(sc->sc_rxint_nframe) |
2088             RDIC_RxDMAWaitTime(STGE_RXINT_USECS2TICK(sc->sc_rxint_dmawait)));
2089
2090         /*
2091          * Initialize the interrupt mask.
2092          */
2093         sc->sc_IntEnable = IS_HostError | IS_TxComplete |
2094             IS_TxDMAComplete | IS_RxDMAComplete | IS_RFDListEnd;
2095 #ifdef DEVICE_POLLING
2096         /* Disable interrupts if we are polling. */
2097         if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2098                 CSR_WRITE_2(sc, STGE_IntEnable, 0);
2099         else
2100 #endif
2101         CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
2102
2103         /*
2104          * Configure the DMA engine.
2105          * XXX Should auto-tune TxBurstLimit.
2106          */
2107         CSR_WRITE_4(sc, STGE_DMACtrl, sc->sc_DMACtrl | DMAC_TxBurstLimit(3));
2108
2109         /*
2110          * Send a PAUSE frame when we reach 29,696 bytes in the Rx
2111          * FIFO, and send an un-PAUSE frame when we reach 3056 bytes
2112          * in the Rx FIFO.
2113          */
2114         CSR_WRITE_2(sc, STGE_FlowOnTresh, 29696 / 16);
2115         CSR_WRITE_2(sc, STGE_FlowOffThresh, 3056 / 16);
2116
2117         /*
2118          * Set the maximum frame size.
2119          */
2120         sc->sc_if_framesize = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2121         CSR_WRITE_2(sc, STGE_MaxFrameSize, sc->sc_if_framesize);
2122
2123         /*
2124          * Initialize MacCtrl -- do it before setting the media,
2125          * as setting the media will actually program the register.
2126          *
2127          * Note: We have to poke the IFS value before poking
2128          * anything else.
2129          */
2130         /* Tx/Rx MAC should be disabled before programming IFS.*/
2131         CSR_WRITE_4(sc, STGE_MACCtrl, MC_IFSSelect(MC_IFS96bit));
2132
2133         stge_vlan_setup(sc);
2134
2135         if (sc->sc_rev >= 6) {          /* >= B.2 */
2136                 /* Multi-frag frame bug work-around. */
2137                 CSR_WRITE_2(sc, STGE_DebugCtrl,
2138                     CSR_READ_2(sc, STGE_DebugCtrl) | 0x0200);
2139
2140                 /* Tx Poll Now bug work-around. */
2141                 CSR_WRITE_2(sc, STGE_DebugCtrl,
2142                     CSR_READ_2(sc, STGE_DebugCtrl) | 0x0010);
2143                 /* Tx Poll Now bug work-around. */
2144                 CSR_WRITE_2(sc, STGE_DebugCtrl,
2145                     CSR_READ_2(sc, STGE_DebugCtrl) | 0x0020);
2146         }
2147
2148         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2149         v |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
2150         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2151         /*
2152          * It seems that transmitting frames without checking the state of
2153          * Rx/Tx MAC wedge the hardware.
2154          */
2155         stge_start_tx(sc);
2156         stge_start_rx(sc);
2157
2158         sc->sc_link = 0;
2159         /*
2160          * Set the current media.
2161          */
2162         mii_mediachg(mii);
2163
2164         /*
2165          * Start the one second MII clock.
2166          */
2167         callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
2168
2169         /*
2170          * ...all done!
2171          */
2172         ifp->if_drv_flags |= IFF_DRV_RUNNING;
2173         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2174
2175  out:
2176         if (error != 0)
2177                 device_printf(sc->sc_dev, "interface not running\n");
2178 }
2179
2180 static void
2181 stge_vlan_setup(struct stge_softc *sc)
2182 {
2183         struct ifnet *ifp;
2184         uint32_t v;
2185
2186         ifp = sc->sc_ifp;
2187         /*
2188          * The NIC always copy a VLAN tag regardless of STGE_MACCtrl
2189          * MC_AutoVLANuntagging bit.
2190          * MC_AutoVLANtagging bit selects which VLAN source to use
2191          * between STGE_VLANTag and TFC. However TFC TFD_VLANTagInsert
2192          * bit has priority over MC_AutoVLANtagging bit. So we always
2193          * use TFC instead of STGE_VLANTag register.
2194          */
2195         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2196         if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2197                 v |= MC_AutoVLANuntagging;
2198         else
2199                 v &= ~MC_AutoVLANuntagging;
2200         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2201 }
2202
2203 /*
2204  *      Stop transmission on the interface.
2205  */
2206 static void
2207 stge_stop(struct stge_softc *sc)
2208 {
2209         struct ifnet *ifp;
2210         struct stge_txdesc *txd;
2211         struct stge_rxdesc *rxd;
2212         uint32_t v;
2213         int i;
2214
2215         STGE_LOCK_ASSERT(sc);
2216         /*
2217          * Stop the one second clock.
2218          */
2219         callout_stop(&sc->sc_tick_ch);
2220         sc->sc_watchdog_timer = 0;
2221
2222         /*
2223          * Disable interrupts.
2224          */
2225         CSR_WRITE_2(sc, STGE_IntEnable, 0);
2226
2227         /*
2228          * Stop receiver, transmitter, and stats update.
2229          */
2230         stge_stop_rx(sc);
2231         stge_stop_tx(sc);
2232         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2233         v |= MC_StatisticsDisable;
2234         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2235
2236         /*
2237          * Stop the transmit and receive DMA.
2238          */
2239         stge_dma_wait(sc);
2240         CSR_WRITE_4(sc, STGE_TFDListPtrHi, 0);
2241         CSR_WRITE_4(sc, STGE_TFDListPtrLo, 0);
2242         CSR_WRITE_4(sc, STGE_RFDListPtrHi, 0);
2243         CSR_WRITE_4(sc, STGE_RFDListPtrLo, 0);
2244
2245         /*
2246          * Free RX and TX mbufs still in the queues.
2247          */
2248         for (i = 0; i < STGE_RX_RING_CNT; i++) {
2249                 rxd = &sc->sc_cdata.stge_rxdesc[i];
2250                 if (rxd->rx_m != NULL) {
2251                         bus_dmamap_sync(sc->sc_cdata.stge_rx_tag,
2252                             rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2253                         bus_dmamap_unload(sc->sc_cdata.stge_rx_tag,
2254                             rxd->rx_dmamap);
2255                         m_freem(rxd->rx_m);
2256                         rxd->rx_m = NULL;
2257                 }
2258         }
2259         for (i = 0; i < STGE_TX_RING_CNT; i++) {
2260                 txd = &sc->sc_cdata.stge_txdesc[i];
2261                 if (txd->tx_m != NULL) {
2262                         bus_dmamap_sync(sc->sc_cdata.stge_tx_tag,
2263                             txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2264                         bus_dmamap_unload(sc->sc_cdata.stge_tx_tag,
2265                             txd->tx_dmamap);
2266                         m_freem(txd->tx_m);
2267                         txd->tx_m = NULL;
2268                 }
2269         }
2270
2271         /*
2272          * Mark the interface down and cancel the watchdog timer.
2273          */
2274         ifp = sc->sc_ifp;
2275         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2276         sc->sc_link = 0;
2277 }
2278
2279 static void
2280 stge_start_tx(struct stge_softc *sc)
2281 {
2282         uint32_t v;
2283         int i;
2284
2285         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2286         if ((v & MC_TxEnabled) != 0)
2287                 return;
2288         v |= MC_TxEnable;
2289         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2290         CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2291         for (i = STGE_TIMEOUT; i > 0; i--) {
2292                 DELAY(10);
2293                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2294                 if ((v & MC_TxEnabled) != 0)
2295                         break;
2296         }
2297         if (i == 0)
2298                 device_printf(sc->sc_dev, "Starting Tx MAC timed out\n");
2299 }
2300
2301 static void
2302 stge_start_rx(struct stge_softc *sc)
2303 {
2304         uint32_t v;
2305         int i;
2306
2307         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2308         if ((v & MC_RxEnabled) != 0)
2309                 return;
2310         v |= MC_RxEnable;
2311         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2312         CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2313         for (i = STGE_TIMEOUT; i > 0; i--) {
2314                 DELAY(10);
2315                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2316                 if ((v & MC_RxEnabled) != 0)
2317                         break;
2318         }
2319         if (i == 0)
2320                 device_printf(sc->sc_dev, "Starting Rx MAC timed out\n");
2321 }
2322
2323 static void
2324 stge_stop_tx(struct stge_softc *sc)
2325 {
2326         uint32_t v;
2327         int i;
2328
2329         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2330         if ((v & MC_TxEnabled) == 0)
2331                 return;
2332         v |= MC_TxDisable;
2333         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2334         for (i = STGE_TIMEOUT; i > 0; i--) {
2335                 DELAY(10);
2336                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2337                 if ((v & MC_TxEnabled) == 0)
2338                         break;
2339         }
2340         if (i == 0)
2341                 device_printf(sc->sc_dev, "Stopping Tx MAC timed out\n");
2342 }
2343
2344 static void
2345 stge_stop_rx(struct stge_softc *sc)
2346 {
2347         uint32_t v;
2348         int i;
2349
2350         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2351         if ((v & MC_RxEnabled) == 0)
2352                 return;
2353         v |= MC_RxDisable;
2354         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2355         for (i = STGE_TIMEOUT; i > 0; i--) {
2356                 DELAY(10);
2357                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2358                 if ((v & MC_RxEnabled) == 0)
2359                         break;
2360         }
2361         if (i == 0)
2362                 device_printf(sc->sc_dev, "Stopping Rx MAC timed out\n");
2363 }
2364
2365 static void
2366 stge_init_tx_ring(struct stge_softc *sc)
2367 {
2368         struct stge_ring_data *rd;
2369         struct stge_txdesc *txd;
2370         bus_addr_t addr;
2371         int i;
2372
2373         STAILQ_INIT(&sc->sc_cdata.stge_txfreeq);
2374         STAILQ_INIT(&sc->sc_cdata.stge_txbusyq);
2375
2376         sc->sc_cdata.stge_tx_prod = 0;
2377         sc->sc_cdata.stge_tx_cons = 0;
2378         sc->sc_cdata.stge_tx_cnt = 0;
2379
2380         rd = &sc->sc_rdata;
2381         bzero(rd->stge_tx_ring, STGE_TX_RING_SZ);
2382         for (i = 0; i < STGE_TX_RING_CNT; i++) {
2383                 if (i == (STGE_TX_RING_CNT - 1))
2384                         addr = STGE_TX_RING_ADDR(sc, 0);
2385                 else
2386                         addr = STGE_TX_RING_ADDR(sc, i + 1);
2387                 rd->stge_tx_ring[i].tfd_next = htole64(addr);
2388                 rd->stge_tx_ring[i].tfd_control = htole64(TFD_TFDDone);
2389                 txd = &sc->sc_cdata.stge_txdesc[i];
2390                 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
2391         }
2392
2393         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
2394             sc->sc_cdata.stge_tx_ring_map,
2395             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2396
2397 }
2398
2399 static int
2400 stge_init_rx_ring(struct stge_softc *sc)
2401 {
2402         struct stge_ring_data *rd;
2403         bus_addr_t addr;
2404         int i;
2405
2406         sc->sc_cdata.stge_rx_cons = 0;
2407         STGE_RXCHAIN_RESET(sc);
2408
2409         rd = &sc->sc_rdata;
2410         bzero(rd->stge_rx_ring, STGE_RX_RING_SZ);
2411         for (i = 0; i < STGE_RX_RING_CNT; i++) {
2412                 if (stge_newbuf(sc, i) != 0)
2413                         return (ENOBUFS);
2414                 if (i == (STGE_RX_RING_CNT - 1))
2415                         addr = STGE_RX_RING_ADDR(sc, 0);
2416                 else
2417                         addr = STGE_RX_RING_ADDR(sc, i + 1);
2418                 rd->stge_rx_ring[i].rfd_next = htole64(addr);
2419                 rd->stge_rx_ring[i].rfd_status = 0;
2420         }
2421
2422         bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
2423             sc->sc_cdata.stge_rx_ring_map,
2424             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2425
2426         return (0);
2427 }
2428
2429 /*
2430  * stge_newbuf:
2431  *
2432  *      Add a receive buffer to the indicated descriptor.
2433  */
2434 static int
2435 stge_newbuf(struct stge_softc *sc, int idx)
2436 {
2437         struct stge_rxdesc *rxd;
2438         struct stge_rfd *rfd;
2439         struct mbuf *m;
2440         bus_dma_segment_t segs[1];
2441         bus_dmamap_t map;
2442         int nsegs;
2443
2444         m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2445         if (m == NULL)
2446                 return (ENOBUFS);
2447         m->m_len = m->m_pkthdr.len = MCLBYTES;
2448         /*
2449          * The hardware requires 4bytes aligned DMA address when JUMBO
2450          * frame is used.
2451          */
2452         if (sc->sc_if_framesize <= (MCLBYTES - ETHER_ALIGN))
2453                 m_adj(m, ETHER_ALIGN);
2454
2455         if (bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_rx_tag,
2456             sc->sc_cdata.stge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
2457                 m_freem(m);
2458                 return (ENOBUFS);
2459         }
2460         KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2461
2462         rxd = &sc->sc_cdata.stge_rxdesc[idx];
2463         if (rxd->rx_m != NULL) {
2464                 bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2465                     BUS_DMASYNC_POSTREAD);
2466                 bus_dmamap_unload(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap);
2467         }
2468         map = rxd->rx_dmamap;
2469         rxd->rx_dmamap = sc->sc_cdata.stge_rx_sparemap;
2470         sc->sc_cdata.stge_rx_sparemap = map;
2471         bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2472             BUS_DMASYNC_PREREAD);
2473         rxd->rx_m = m;
2474
2475         rfd = &sc->sc_rdata.stge_rx_ring[idx];
2476         rfd->rfd_frag.frag_word0 =
2477             htole64(FRAG_ADDR(segs[0].ds_addr) | FRAG_LEN(segs[0].ds_len));
2478         rfd->rfd_status = 0;
2479
2480         return (0);
2481 }
2482
2483 /*
2484  * stge_set_filter:
2485  *
2486  *      Set up the receive filter.
2487  */
2488 static void
2489 stge_set_filter(struct stge_softc *sc)
2490 {
2491         struct ifnet *ifp;
2492         uint16_t mode;
2493
2494         STGE_LOCK_ASSERT(sc);
2495
2496         ifp = sc->sc_ifp;
2497
2498         mode = CSR_READ_2(sc, STGE_ReceiveMode);
2499         mode |= RM_ReceiveUnicast;
2500         if ((ifp->if_flags & IFF_BROADCAST) != 0)
2501                 mode |= RM_ReceiveBroadcast;
2502         else
2503                 mode &= ~RM_ReceiveBroadcast;
2504         if ((ifp->if_flags & IFF_PROMISC) != 0)
2505                 mode |= RM_ReceiveAllFrames;
2506         else
2507                 mode &= ~RM_ReceiveAllFrames;
2508
2509         CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2510 }
2511
2512 static void
2513 stge_set_multi(struct stge_softc *sc)
2514 {
2515         struct ifnet *ifp;
2516         struct ifmultiaddr *ifma;
2517         uint32_t crc;
2518         uint32_t mchash[2];
2519         uint16_t mode;
2520         int count;
2521
2522         STGE_LOCK_ASSERT(sc);
2523
2524         ifp = sc->sc_ifp;
2525
2526         mode = CSR_READ_2(sc, STGE_ReceiveMode);
2527         if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2528                 if ((ifp->if_flags & IFF_PROMISC) != 0)
2529                         mode |= RM_ReceiveAllFrames;
2530                 else if ((ifp->if_flags & IFF_ALLMULTI) != 0)
2531                         mode |= RM_ReceiveMulticast;
2532                 CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2533                 return;
2534         }
2535
2536         /* clear existing filters. */
2537         CSR_WRITE_4(sc, STGE_HashTable0, 0);
2538         CSR_WRITE_4(sc, STGE_HashTable1, 0);
2539
2540         /*
2541          * Set up the multicast address filter by passing all multicast
2542          * addresses through a CRC generator, and then using the low-order
2543          * 6 bits as an index into the 64 bit multicast hash table.  The
2544          * high order bits select the register, while the rest of the bits
2545          * select the bit within the register.
2546          */
2547
2548         bzero(mchash, sizeof(mchash));
2549
2550         count = 0;
2551         if_maddr_rlock(sc->sc_ifp);
2552         TAILQ_FOREACH(ifma, &sc->sc_ifp->if_multiaddrs, ifma_link) {
2553                 if (ifma->ifma_addr->sa_family != AF_LINK)
2554                         continue;
2555                 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2556                     ifma->ifma_addr), ETHER_ADDR_LEN);
2557
2558                 /* Just want the 6 least significant bits. */
2559                 crc &= 0x3f;
2560
2561                 /* Set the corresponding bit in the hash table. */
2562                 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2563                 count++;
2564         }
2565         if_maddr_runlock(ifp);
2566
2567         mode &= ~(RM_ReceiveMulticast | RM_ReceiveAllFrames);
2568         if (count > 0)
2569                 mode |= RM_ReceiveMulticastHash;
2570         else
2571                 mode &= ~RM_ReceiveMulticastHash;
2572
2573         CSR_WRITE_4(sc, STGE_HashTable0, mchash[0]);
2574         CSR_WRITE_4(sc, STGE_HashTable1, mchash[1]);
2575         CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2576 }
2577
2578 static int
2579 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2580 {
2581         int error, value;
2582
2583         if (!arg1)
2584                 return (EINVAL);
2585         value = *(int *)arg1;
2586         error = sysctl_handle_int(oidp, &value, 0, req);
2587         if (error || !req->newptr)
2588                 return (error);
2589         if (value < low || value > high)
2590                 return (EINVAL);
2591         *(int *)arg1 = value;
2592
2593         return (0);
2594 }
2595
2596 static int
2597 sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS)
2598 {
2599         return (sysctl_int_range(oidp, arg1, arg2, req,
2600             STGE_RXINT_NFRAME_MIN, STGE_RXINT_NFRAME_MAX));
2601 }
2602
2603 static int
2604 sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS)
2605 {
2606         return (sysctl_int_range(oidp, arg1, arg2, req,
2607             STGE_RXINT_DMAWAIT_MIN, STGE_RXINT_DMAWAIT_MAX));
2608 }