]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/sf/if_sf.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / sf / if_sf.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * Adaptec AIC-6915 "Starfire" PCI fast ethernet driver for FreeBSD.
38  * Programming manual is available from:
39  * http://download.adaptec.com/pdfs/user_guides/aic6915_pg.pdf.
40  *
41  * Written by Bill Paul <wpaul@ctr.columbia.edu>
42  * Department of Electical Engineering
43  * Columbia University, New York City
44  */
45 /*
46  * The Adaptec AIC-6915 "Starfire" is a 64-bit 10/100 PCI ethernet
47  * controller designed with flexibility and reducing CPU load in mind.
48  * The Starfire offers high and low priority buffer queues, a
49  * producer/consumer index mechanism and several different buffer
50  * queue and completion queue descriptor types. Any one of a number
51  * of different driver designs can be used, depending on system and
52  * OS requirements. This driver makes use of type2 transmit frame
53  * descriptors to take full advantage of fragmented packets buffers
54  * and two RX buffer queues prioritized on size (one queue for small
55  * frames that will fit into a single mbuf, another with full size
56  * mbuf clusters for everything else). The producer/consumer indexes
57  * and completion queues are also used.
58  *
59  * One downside to the Starfire has to do with alignment: buffer
60  * queues must be aligned on 256-byte boundaries, and receive buffers
61  * must be aligned on longword boundaries. The receive buffer alignment
62  * causes problems on the strict alignment architecture, where the
63  * packet payload should be longword aligned. There is no simple way
64  * around this.
65  *
66  * For receive filtering, the Starfire offers 16 perfect filter slots
67  * and a 512-bit hash table.
68  *
69  * The Starfire has no internal transceiver, relying instead on an
70  * external MII-based transceiver. Accessing registers on external
71  * PHYs is done through a special register map rather than with the
72  * usual bitbang MDIO method.
73  *
74  * Acesssing the registers on the Starfire is a little tricky. The
75  * Starfire has a 512K internal register space. When programmed for
76  * PCI memory mapped mode, the entire register space can be accessed
77  * directly. However in I/O space mode, only 256 bytes are directly
78  * mapped into PCI I/O space. The other registers can be accessed
79  * indirectly using the SF_INDIRECTIO_ADDR and SF_INDIRECTIO_DATA
80  * registers inside the 256-byte I/O window.
81  */
82
83 #ifdef HAVE_KERNEL_OPTION_HEADERS
84 #include "opt_device_polling.h"
85 #endif
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/bus.h>
90 #include <sys/endian.h>
91 #include <sys/kernel.h>
92 #include <sys/malloc.h>
93 #include <sys/mbuf.h>
94 #include <sys/rman.h>
95 #include <sys/module.h>
96 #include <sys/socket.h>
97 #include <sys/sockio.h>
98 #include <sys/sysctl.h>
99 #include <sys/taskqueue.h>
100
101 #include <net/bpf.h>
102 #include <net/if.h>
103 #include <net/if_arp.h>
104 #include <net/ethernet.h>
105 #include <net/if_dl.h>
106 #include <net/if_media.h>
107 #include <net/if_types.h>
108 #include <net/if_vlan_var.h>
109
110 #include <dev/mii/mii.h>
111 #include <dev/mii/miivar.h>
112
113 #include <dev/pci/pcireg.h>
114 #include <dev/pci/pcivar.h>
115
116 #include <machine/bus.h>
117
118 #include <dev/sf/if_sfreg.h>
119 #include <dev/sf/starfire_rx.h>
120 #include <dev/sf/starfire_tx.h>
121
122 /* "device miibus" required.  See GENERIC if you get errors here. */
123 #include "miibus_if.h"
124
125 MODULE_DEPEND(sf, pci, 1, 1, 1);
126 MODULE_DEPEND(sf, ether, 1, 1, 1);
127 MODULE_DEPEND(sf, miibus, 1, 1, 1);
128
129 #undef  SF_GFP_DEBUG
130 #define SF_CSUM_FEATURES        (CSUM_TCP | CSUM_UDP)
131 /* Define this to activate partial TCP/UDP checksum offload. */
132 #undef  SF_PARTIAL_CSUM_SUPPORT
133
134 static struct sf_type sf_devs[] = {
135         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
136             AD_SUBSYSID_62011_REV0, "Adaptec ANA-62011 (rev 0) 10/100BaseTX" },
137         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
138             AD_SUBSYSID_62011_REV1, "Adaptec ANA-62011 (rev 1) 10/100BaseTX" },
139         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
140             AD_SUBSYSID_62022, "Adaptec ANA-62022 10/100BaseTX" },
141         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
142             AD_SUBSYSID_62044_REV0, "Adaptec ANA-62044 (rev 0) 10/100BaseTX" },
143         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
144             AD_SUBSYSID_62044_REV1, "Adaptec ANA-62044 (rev 1) 10/100BaseTX" },
145         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
146             AD_SUBSYSID_62020, "Adaptec ANA-62020 10/100BaseFX" },
147         { AD_VENDORID, AD_DEVICEID_STARFIRE, "Adaptec AIC-6915 10/100BaseTX",
148             AD_SUBSYSID_69011, "Adaptec ANA-69011 10/100BaseTX" },
149 };
150
151 static int sf_probe(device_t);
152 static int sf_attach(device_t);
153 static int sf_detach(device_t);
154 static int sf_shutdown(device_t);
155 static int sf_suspend(device_t);
156 static int sf_resume(device_t);
157 static void sf_intr(void *);
158 static void sf_tick(void *);
159 static void sf_stats_update(struct sf_softc *);
160 #ifndef __NO_STRICT_ALIGNMENT
161 static __inline void sf_fixup_rx(struct mbuf *);
162 #endif
163 static int sf_rxeof(struct sf_softc *);
164 static void sf_txeof(struct sf_softc *);
165 static int sf_encap(struct sf_softc *, struct mbuf **);
166 static void sf_start(struct ifnet *);
167 static void sf_start_locked(struct ifnet *);
168 static int sf_ioctl(struct ifnet *, u_long, caddr_t);
169 static void sf_download_fw(struct sf_softc *);
170 static void sf_init(void *);
171 static void sf_init_locked(struct sf_softc *);
172 static void sf_stop(struct sf_softc *);
173 static void sf_watchdog(struct sf_softc *);
174 static int sf_ifmedia_upd(struct ifnet *);
175 static void sf_ifmedia_sts(struct ifnet *, struct ifmediareq *);
176 static void sf_reset(struct sf_softc *);
177 static int sf_dma_alloc(struct sf_softc *);
178 static void sf_dma_free(struct sf_softc *);
179 static int sf_init_rx_ring(struct sf_softc *);
180 static void sf_init_tx_ring(struct sf_softc *);
181 static int sf_newbuf(struct sf_softc *, int);
182 static void sf_rxfilter(struct sf_softc *);
183 static int sf_setperf(struct sf_softc *, int, uint8_t *);
184 static int sf_sethash(struct sf_softc *, caddr_t, int);
185 #ifdef notdef
186 static int sf_setvlan(struct sf_softc *, int, uint32_t);
187 #endif
188
189 static uint8_t sf_read_eeprom(struct sf_softc *, int);
190
191 static int sf_miibus_readreg(device_t, int, int);
192 static int sf_miibus_writereg(device_t, int, int, int);
193 static void sf_miibus_statchg(device_t);
194 static void sf_link_task(void *, int);
195 #ifdef DEVICE_POLLING
196 static int sf_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
197 #endif
198
199 static uint32_t csr_read_4(struct sf_softc *, int);
200 static void csr_write_4(struct sf_softc *, int, uint32_t);
201 static void sf_txthresh_adjust(struct sf_softc *);
202 static int sf_sysctl_stats(SYSCTL_HANDLER_ARGS);
203 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
204 static int sysctl_hw_sf_int_mod(SYSCTL_HANDLER_ARGS);
205
206 static device_method_t sf_methods[] = {
207         /* Device interface */
208         DEVMETHOD(device_probe,         sf_probe),
209         DEVMETHOD(device_attach,        sf_attach),
210         DEVMETHOD(device_detach,        sf_detach),
211         DEVMETHOD(device_shutdown,      sf_shutdown),
212         DEVMETHOD(device_suspend,       sf_suspend),
213         DEVMETHOD(device_resume,        sf_resume),
214
215         /* bus interface */
216         DEVMETHOD(bus_print_child,      bus_generic_print_child),
217         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
218
219         /* MII interface */
220         DEVMETHOD(miibus_readreg,       sf_miibus_readreg),
221         DEVMETHOD(miibus_writereg,      sf_miibus_writereg),
222         DEVMETHOD(miibus_statchg,       sf_miibus_statchg),
223
224         { NULL, NULL }
225 };
226
227 static driver_t sf_driver = {
228         "sf",
229         sf_methods,
230         sizeof(struct sf_softc),
231 };
232
233 static devclass_t sf_devclass;
234
235 DRIVER_MODULE(sf, pci, sf_driver, sf_devclass, 0, 0);
236 DRIVER_MODULE(miibus, sf, miibus_driver, miibus_devclass, 0, 0);
237
238 #define SF_SETBIT(sc, reg, x)   \
239         csr_write_4(sc, reg, csr_read_4(sc, reg) | (x))
240
241 #define SF_CLRBIT(sc, reg, x)                           \
242         csr_write_4(sc, reg, csr_read_4(sc, reg) & ~(x))
243
244 static uint32_t
245 csr_read_4(struct sf_softc *sc, int reg)
246 {
247         uint32_t                val;
248
249         if (sc->sf_restype == SYS_RES_MEMORY)
250                 val = CSR_READ_4(sc, (reg + SF_RMAP_INTREG_BASE));
251         else {
252                 CSR_WRITE_4(sc, SF_INDIRECTIO_ADDR, reg + SF_RMAP_INTREG_BASE);
253                 val = CSR_READ_4(sc, SF_INDIRECTIO_DATA);
254         }
255
256         return (val);
257 }
258
259 static uint8_t
260 sf_read_eeprom(struct sf_softc *sc, int reg)
261 {
262         uint8_t         val;
263
264         val = (csr_read_4(sc, SF_EEADDR_BASE +
265             (reg & 0xFFFFFFFC)) >> (8 * (reg & 3))) & 0xFF;
266
267         return (val);
268 }
269
270 static void
271 csr_write_4(struct sf_softc *sc, int reg, uint32_t val)
272 {
273
274         if (sc->sf_restype == SYS_RES_MEMORY)
275                 CSR_WRITE_4(sc, (reg + SF_RMAP_INTREG_BASE), val);
276         else {
277                 CSR_WRITE_4(sc, SF_INDIRECTIO_ADDR, reg + SF_RMAP_INTREG_BASE);
278                 CSR_WRITE_4(sc, SF_INDIRECTIO_DATA, val);
279         }
280 }
281
282 /*
283  * Copy the address 'mac' into the perfect RX filter entry at
284  * offset 'idx.' The perfect filter only has 16 entries so do
285  * some sanity tests.
286  */
287 static int
288 sf_setperf(struct sf_softc *sc, int idx, uint8_t *mac)
289 {
290
291         if (idx < 0 || idx > SF_RXFILT_PERFECT_CNT)
292                 return (EINVAL);
293
294         if (mac == NULL)
295                 return (EINVAL);
296
297         csr_write_4(sc, SF_RXFILT_PERFECT_BASE +
298             (idx * SF_RXFILT_PERFECT_SKIP) + 0, mac[5] | (mac[4] << 8));
299         csr_write_4(sc, SF_RXFILT_PERFECT_BASE +
300             (idx * SF_RXFILT_PERFECT_SKIP) + 4, mac[3] | (mac[2] << 8));
301         csr_write_4(sc, SF_RXFILT_PERFECT_BASE +
302             (idx * SF_RXFILT_PERFECT_SKIP) + 8, mac[1] | (mac[0] << 8));
303
304         return (0);
305 }
306
307 /*
308  * Set the bit in the 512-bit hash table that corresponds to the
309  * specified mac address 'mac.' If 'prio' is nonzero, update the
310  * priority hash table instead of the filter hash table.
311  */
312 static int
313 sf_sethash(struct sf_softc *sc, caddr_t mac, int prio)
314 {
315         uint32_t                h;
316
317         if (mac == NULL)
318                 return (EINVAL);
319
320         h = ether_crc32_be(mac, ETHER_ADDR_LEN) >> 23;
321
322         if (prio) {
323                 SF_SETBIT(sc, SF_RXFILT_HASH_BASE + SF_RXFILT_HASH_PRIOOFF +
324                     (SF_RXFILT_HASH_SKIP * (h >> 4)), (1 << (h & 0xF)));
325         } else {
326                 SF_SETBIT(sc, SF_RXFILT_HASH_BASE + SF_RXFILT_HASH_ADDROFF +
327                     (SF_RXFILT_HASH_SKIP * (h >> 4)), (1 << (h & 0xF)));
328         }
329
330         return (0);
331 }
332
333 #ifdef notdef
334 /*
335  * Set a VLAN tag in the receive filter.
336  */
337 static int
338 sf_setvlan(struct sf_softc *sc, int idx, uint32_t vlan)
339 {
340
341         if (idx < 0 || idx >> SF_RXFILT_HASH_CNT)
342                 return (EINVAL);
343
344         csr_write_4(sc, SF_RXFILT_HASH_BASE +
345             (idx * SF_RXFILT_HASH_SKIP) + SF_RXFILT_HASH_VLANOFF, vlan);
346
347         return (0);
348 }
349 #endif
350
351 static int
352 sf_miibus_readreg(device_t dev, int phy, int reg)
353 {
354         struct sf_softc         *sc;
355         int                     i;
356         uint32_t                val = 0;
357
358         sc = device_get_softc(dev);
359
360         for (i = 0; i < SF_TIMEOUT; i++) {
361                 val = csr_read_4(sc, SF_PHY_REG(phy, reg));
362                 if ((val & SF_MII_DATAVALID) != 0)
363                         break;
364         }
365
366         if (i == SF_TIMEOUT)
367                 return (0);
368
369         val &= SF_MII_DATAPORT;
370         if (val == 0xffff)
371                 return (0);
372
373         return (val);
374 }
375
376 static int
377 sf_miibus_writereg(device_t dev, int phy, int reg, int val)
378 {
379         struct sf_softc         *sc;
380         int                     i;
381         int                     busy;
382
383         sc = device_get_softc(dev);
384
385         csr_write_4(sc, SF_PHY_REG(phy, reg), val);
386
387         for (i = 0; i < SF_TIMEOUT; i++) {
388                 busy = csr_read_4(sc, SF_PHY_REG(phy, reg));
389                 if ((busy & SF_MII_BUSY) == 0)
390                         break;
391         }
392
393         return (0);
394 }
395
396 static void
397 sf_miibus_statchg(device_t dev)
398 {
399         struct sf_softc         *sc;
400
401         sc = device_get_softc(dev);
402         taskqueue_enqueue(taskqueue_swi, &sc->sf_link_task);
403 }
404
405 static void
406 sf_link_task(void *arg, int pending)
407 {
408         struct sf_softc         *sc;
409         struct mii_data         *mii;
410         struct ifnet            *ifp;
411         uint32_t                val;
412
413         sc = (struct sf_softc *)arg;
414
415         SF_LOCK(sc);
416
417         mii = device_get_softc(sc->sf_miibus);
418         ifp = sc->sf_ifp;
419         if (mii == NULL || ifp == NULL ||
420             (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
421                 SF_UNLOCK(sc);
422                 return;
423         }
424
425         if (mii->mii_media_status & IFM_ACTIVE) {
426                 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
427                         sc->sf_link = 1;
428         } else
429                 sc->sf_link = 0;
430
431         val = csr_read_4(sc, SF_MACCFG_1);
432         val &= ~SF_MACCFG1_FULLDUPLEX;
433         val &= ~(SF_MACCFG1_RX_FLOWENB | SF_MACCFG1_TX_FLOWENB);
434         if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
435                 val |= SF_MACCFG1_FULLDUPLEX;
436                 csr_write_4(sc, SF_BKTOBKIPG, SF_IPGT_FDX);
437 #ifdef notyet
438                 /* Configure flow-control bits. */
439                 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
440                     IFM_ETH_RXPAUSE) != 0)
441                         val |= SF_MACCFG1_RX_FLOWENB;
442                 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
443                     IFM_ETH_TXPAUSE) != 0)
444                         val |= SF_MACCFG1_TX_FLOWENB;
445 #endif
446         } else
447                 csr_write_4(sc, SF_BKTOBKIPG, SF_IPGT_HDX);
448
449         /* Make sure to reset MAC to take changes effect. */
450         csr_write_4(sc, SF_MACCFG_1, val | SF_MACCFG1_SOFTRESET);
451         DELAY(1000);
452         csr_write_4(sc, SF_MACCFG_1, val);
453
454         val = csr_read_4(sc, SF_TIMER_CTL);
455         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
456                 val |= SF_TIMER_TIMES_TEN;
457         else
458                 val &= ~SF_TIMER_TIMES_TEN;
459         csr_write_4(sc, SF_TIMER_CTL, val);
460
461         SF_UNLOCK(sc);
462 }
463
464 static void
465 sf_rxfilter(struct sf_softc *sc)
466 {
467         struct ifnet            *ifp;
468         int                     i;
469         struct ifmultiaddr      *ifma;
470         uint8_t                 dummy[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
471         uint32_t                rxfilt;
472
473         ifp = sc->sf_ifp;
474
475         /* First zot all the existing filters. */
476         for (i = 1; i < SF_RXFILT_PERFECT_CNT; i++)
477                 sf_setperf(sc, i, dummy);
478         for (i = SF_RXFILT_HASH_BASE; i < (SF_RXFILT_HASH_MAX + 1);
479             i += sizeof(uint32_t))
480                 csr_write_4(sc, i, 0);
481
482         rxfilt = csr_read_4(sc, SF_RXFILT);
483         rxfilt &= ~(SF_RXFILT_PROMISC | SF_RXFILT_ALLMULTI | SF_RXFILT_BROAD);
484         if ((ifp->if_flags & IFF_BROADCAST) != 0)
485                 rxfilt |= SF_RXFILT_BROAD;
486         if ((ifp->if_flags & IFF_ALLMULTI) != 0 ||
487             (ifp->if_flags & IFF_PROMISC) != 0) {
488                 if ((ifp->if_flags & IFF_PROMISC) != 0)
489                         rxfilt |= SF_RXFILT_PROMISC;
490                 if ((ifp->if_flags & IFF_ALLMULTI) != 0)
491                         rxfilt |= SF_RXFILT_ALLMULTI;
492                 goto done;
493         }
494
495         /* Now program new ones. */
496         i = 1;
497         if_maddr_rlock(ifp);
498         TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead,
499             ifma_link) {
500                 if (ifma->ifma_addr->sa_family != AF_LINK)
501                         continue;
502                 /*
503                  * Program the first 15 multicast groups
504                  * into the perfect filter. For all others,
505                  * use the hash table.
506                  */
507                 if (i < SF_RXFILT_PERFECT_CNT) {
508                         sf_setperf(sc, i,
509                             LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
510                         i++;
511                         continue;
512                 }
513
514                 sf_sethash(sc,
515                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 0);
516         }
517         if_maddr_runlock(ifp);
518
519 done:
520         csr_write_4(sc, SF_RXFILT, rxfilt);
521 }
522
523 /*
524  * Set media options.
525  */
526 static int
527 sf_ifmedia_upd(struct ifnet *ifp)
528 {
529         struct sf_softc         *sc;
530         struct mii_data         *mii;
531         int                     error;
532
533         sc = ifp->if_softc;
534         SF_LOCK(sc);
535
536         mii = device_get_softc(sc->sf_miibus);
537         if (mii->mii_instance) {
538                 struct mii_softc        *miisc;
539                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
540                         mii_phy_reset(miisc);
541         }
542         error = mii_mediachg(mii);
543         SF_UNLOCK(sc);
544
545         return (error);
546 }
547
548 /*
549  * Report current media status.
550  */
551 static void
552 sf_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
553 {
554         struct sf_softc         *sc;
555         struct mii_data         *mii;
556
557         sc = ifp->if_softc;
558         SF_LOCK(sc);
559         mii = device_get_softc(sc->sf_miibus);
560
561         mii_pollstat(mii);
562         ifmr->ifm_active = mii->mii_media_active;
563         ifmr->ifm_status = mii->mii_media_status;
564         SF_UNLOCK(sc);
565 }
566
567 static int
568 sf_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
569 {
570         struct sf_softc         *sc;
571         struct ifreq            *ifr;
572         struct mii_data         *mii;
573         int                     error, mask;
574
575         sc = ifp->if_softc;
576         ifr = (struct ifreq *)data;
577         error = 0;
578
579         switch (command) {
580         case SIOCSIFFLAGS:
581                 SF_LOCK(sc);
582                 if (ifp->if_flags & IFF_UP) {
583                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
584                                 if ((ifp->if_flags ^ sc->sf_if_flags) &
585                                     (IFF_PROMISC | IFF_ALLMULTI))
586                                         sf_rxfilter(sc);
587                         } else {
588                                 if (sc->sf_detach == 0)
589                                         sf_init_locked(sc);
590                         }
591                 } else {
592                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
593                                 sf_stop(sc);
594                 }
595                 sc->sf_if_flags = ifp->if_flags;
596                 SF_UNLOCK(sc);
597                 break;
598         case SIOCADDMULTI:
599         case SIOCDELMULTI:
600                 SF_LOCK(sc);
601                 sf_rxfilter(sc);
602                 SF_UNLOCK(sc);
603                 break;
604         case SIOCGIFMEDIA:
605         case SIOCSIFMEDIA:
606                 mii = device_get_softc(sc->sf_miibus);
607                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
608                 break;
609         case SIOCSIFCAP:
610                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
611 #ifdef DEVICE_POLLING
612                 if ((mask & IFCAP_POLLING) != 0) {
613                         if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
614                                 error = ether_poll_register(sf_poll, ifp);
615                                 if (error != 0)
616                                         break;
617                                 SF_LOCK(sc);
618                                 /* Disable interrupts. */
619                                 csr_write_4(sc, SF_IMR, 0);
620                                 ifp->if_capenable |= IFCAP_POLLING;
621                                 SF_UNLOCK(sc);
622                         } else {
623                                 error = ether_poll_deregister(ifp);
624                                 /* Enable interrupts. */
625                                 SF_LOCK(sc);
626                                 csr_write_4(sc, SF_IMR, SF_INTRS);
627                                 ifp->if_capenable &= ~IFCAP_POLLING;
628                                 SF_UNLOCK(sc);
629                         }
630                 }
631 #endif /* DEVICE_POLLING */
632                 if ((mask & IFCAP_TXCSUM) != 0) {
633                         if ((IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
634                                 SF_LOCK(sc);
635                                 ifp->if_capenable ^= IFCAP_TXCSUM;
636                                 if ((IFCAP_TXCSUM & ifp->if_capenable) != 0) {
637                                         ifp->if_hwassist |= SF_CSUM_FEATURES;
638                                         SF_SETBIT(sc, SF_GEN_ETH_CTL,
639                                             SF_ETHCTL_TXGFP_ENB);
640                                 } else {
641                                         ifp->if_hwassist &= ~SF_CSUM_FEATURES;
642                                         SF_CLRBIT(sc, SF_GEN_ETH_CTL,
643                                             SF_ETHCTL_TXGFP_ENB);
644                                 }
645                                 SF_UNLOCK(sc);
646                         }
647                 }
648                 if ((mask & IFCAP_RXCSUM) != 0) {
649                         if ((IFCAP_RXCSUM & ifp->if_capabilities) != 0) {
650                                 SF_LOCK(sc);
651                                 ifp->if_capenable ^= IFCAP_RXCSUM;
652                                 if ((IFCAP_RXCSUM & ifp->if_capenable) != 0)
653                                         SF_SETBIT(sc, SF_GEN_ETH_CTL,
654                                             SF_ETHCTL_RXGFP_ENB);
655                                 else
656                                         SF_CLRBIT(sc, SF_GEN_ETH_CTL,
657                                             SF_ETHCTL_RXGFP_ENB);
658                                 SF_UNLOCK(sc);
659                         }
660                 }
661                 break;
662         default:
663                 error = ether_ioctl(ifp, command, data);
664                 break;
665         }
666
667         return (error);
668 }
669
670 static void
671 sf_reset(struct sf_softc *sc)
672 {
673         int             i;
674
675         csr_write_4(sc, SF_GEN_ETH_CTL, 0);
676         SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
677         DELAY(1000);
678         SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
679
680         SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_RESET);
681
682         for (i = 0; i < SF_TIMEOUT; i++) {
683                 DELAY(10);
684                 if (!(csr_read_4(sc, SF_PCI_DEVCFG) & SF_PCIDEVCFG_RESET))
685                         break;
686         }
687
688         if (i == SF_TIMEOUT)
689                 device_printf(sc->sf_dev, "reset never completed!\n");
690
691         /* Wait a little while for the chip to get its brains in order. */
692         DELAY(1000);
693 }
694
695 /*
696  * Probe for an Adaptec AIC-6915 chip. Check the PCI vendor and device
697  * IDs against our list and return a device name if we find a match.
698  * We also check the subsystem ID so that we can identify exactly which
699  * NIC has been found, if possible.
700  */
701 static int
702 sf_probe(device_t dev)
703 {
704         struct sf_type          *t;
705         uint16_t                vid;
706         uint16_t                did;
707         uint16_t                sdid;
708         int                     i;
709
710         vid = pci_get_vendor(dev);
711         did = pci_get_device(dev);
712         sdid = pci_get_subdevice(dev);
713
714         t = sf_devs;
715         for (i = 0; i < sizeof(sf_devs) / sizeof(sf_devs[0]); i++, t++) {
716                 if (vid == t->sf_vid && did == t->sf_did) {
717                         if (sdid == t->sf_sdid) {
718                                 device_set_desc(dev, t->sf_sname);
719                                 return (BUS_PROBE_DEFAULT);
720                         }
721                 }
722         }
723
724         if (vid == AD_VENDORID && did == AD_DEVICEID_STARFIRE) {
725                 /* unkown subdevice */
726                 device_set_desc(dev, sf_devs[0].sf_name);
727                 return (BUS_PROBE_DEFAULT);
728         }
729
730         return (ENXIO);
731 }
732
733 /*
734  * Attach the interface. Allocate softc structures, do ifmedia
735  * setup and ethernet/BPF attach.
736  */
737 static int
738 sf_attach(device_t dev)
739 {
740         int                     i;
741         struct sf_softc         *sc;
742         struct ifnet            *ifp;
743         uint32_t                reg;
744         int                     rid, error = 0;
745         uint8_t                 eaddr[ETHER_ADDR_LEN];
746
747         sc = device_get_softc(dev);
748         sc->sf_dev = dev;
749
750         mtx_init(&sc->sf_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
751             MTX_DEF);
752         callout_init_mtx(&sc->sf_co, &sc->sf_mtx, 0);
753         TASK_INIT(&sc->sf_link_task, 0, sf_link_task, sc);
754
755         /*
756          * Map control/status registers.
757          */
758         pci_enable_busmaster(dev);
759
760         /*
761          * Prefer memory space register mapping over I/O space as the
762          * hardware requires lots of register access to get various
763          * producer/consumer index during Tx/Rx operation. However this
764          * requires large memory space(512K) to map the entire register
765          * space.
766          */
767         sc->sf_rid = PCIR_BAR(0);
768         sc->sf_restype = SYS_RES_MEMORY;
769         sc->sf_res = bus_alloc_resource_any(dev, sc->sf_restype, &sc->sf_rid,
770             RF_ACTIVE);
771         if (sc->sf_res == NULL) {
772                 reg = pci_read_config(dev, PCIR_BAR(0), 4);
773                 if ((reg & PCIM_BAR_MEM_64) == PCIM_BAR_MEM_64)
774                         sc->sf_rid = PCIR_BAR(2);
775                 else
776                         sc->sf_rid = PCIR_BAR(1);
777                 sc->sf_restype = SYS_RES_IOPORT;
778                 sc->sf_res = bus_alloc_resource_any(dev, sc->sf_restype,
779                     &sc->sf_rid, RF_ACTIVE);
780                 if (sc->sf_res == NULL) {
781                         device_printf(dev, "couldn't allocate resources\n");
782                         mtx_destroy(&sc->sf_mtx);
783                         return (ENXIO);
784                 }
785         }
786         if (bootverbose)
787                 device_printf(dev, "using %s space register mapping\n",
788                     sc->sf_restype == SYS_RES_MEMORY ? "memory" : "I/O");
789
790         reg = pci_read_config(dev, PCIR_CACHELNSZ, 1);
791         if (reg == 0) {
792                 /*
793                  * If cache line size is 0, MWI is not used at all, so set
794                  * reasonable default. AIC-6915 supports 0, 4, 8, 16, 32
795                  * and 64.
796                  */
797                 reg = 16;
798                 device_printf(dev, "setting PCI cache line size to %u\n", reg);
799                 pci_write_config(dev, PCIR_CACHELNSZ, reg, 1);
800         } else {
801                 if (bootverbose)
802                         device_printf(dev, "PCI cache line size : %u\n", reg);
803         }
804         /* Enable MWI. */
805         reg = pci_read_config(dev, PCIR_COMMAND, 2);
806         reg |= PCIM_CMD_MWRICEN;
807         pci_write_config(dev, PCIR_COMMAND, reg, 2);
808
809         /* Allocate interrupt. */
810         rid = 0;
811         sc->sf_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
812             RF_SHAREABLE | RF_ACTIVE);
813
814         if (sc->sf_irq == NULL) {
815                 device_printf(dev, "couldn't map interrupt\n");
816                 error = ENXIO;
817                 goto fail;
818         }
819
820         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
821             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
822             OID_AUTO, "stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
823             sf_sysctl_stats, "I", "Statistics");
824
825         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
826                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
827                 OID_AUTO, "int_mod", CTLTYPE_INT | CTLFLAG_RW,
828                 &sc->sf_int_mod, 0, sysctl_hw_sf_int_mod, "I",
829                 "sf interrupt moderation");
830         /* Pull in device tunables. */
831         sc->sf_int_mod = SF_IM_DEFAULT;
832         error = resource_int_value(device_get_name(dev), device_get_unit(dev),
833             "int_mod", &sc->sf_int_mod);
834         if (error == 0) {
835                 if (sc->sf_int_mod < SF_IM_MIN ||
836                     sc->sf_int_mod > SF_IM_MAX) {
837                         device_printf(dev, "int_mod value out of range; "
838                             "using default: %d\n", SF_IM_DEFAULT);
839                         sc->sf_int_mod = SF_IM_DEFAULT;
840                 }
841         }
842
843         /* Reset the adapter. */
844         sf_reset(sc);
845
846         /*
847          * Get station address from the EEPROM.
848          */
849         for (i = 0; i < ETHER_ADDR_LEN; i++)
850                 eaddr[i] =
851                     sf_read_eeprom(sc, SF_EE_NODEADDR + ETHER_ADDR_LEN - i);
852
853         /* Allocate DMA resources. */
854         if (sf_dma_alloc(sc) != 0) {
855                 error = ENOSPC;
856                 goto fail;
857         }
858
859         sc->sf_txthresh = SF_MIN_TX_THRESHOLD;
860
861         ifp = sc->sf_ifp = if_alloc(IFT_ETHER);
862         if (ifp == NULL) {
863                 device_printf(dev, "can not allocate ifnet structure\n");
864                 error = ENOSPC;
865                 goto fail;
866         }
867
868         /* Do MII setup. */
869         if (mii_phy_probe(dev, &sc->sf_miibus, sf_ifmedia_upd,
870             sf_ifmedia_sts)) {
871                 device_printf(dev, "MII without any phy!\n");
872                 error = ENXIO;
873                 goto fail;
874         }
875
876         ifp->if_softc = sc;
877         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
878         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
879         ifp->if_ioctl = sf_ioctl;
880         ifp->if_start = sf_start;
881         ifp->if_init = sf_init;
882         IFQ_SET_MAXLEN(&ifp->if_snd, SF_TX_DLIST_CNT - 1);
883         ifp->if_snd.ifq_drv_maxlen = SF_TX_DLIST_CNT - 1;
884         IFQ_SET_READY(&ifp->if_snd);
885         /*
886          * With the help of firmware, AIC-6915 supports
887          * Tx/Rx TCP/UDP checksum offload.
888          */
889         ifp->if_hwassist = SF_CSUM_FEATURES;
890         ifp->if_capabilities = IFCAP_HWCSUM;
891
892         /*
893          * Call MI attach routine.
894          */
895         ether_ifattach(ifp, eaddr);
896
897         /* VLAN capability setup. */
898         ifp->if_capabilities |= IFCAP_VLAN_MTU;
899         ifp->if_capenable = ifp->if_capabilities;
900 #ifdef DEVICE_POLLING
901         ifp->if_capabilities |= IFCAP_POLLING;
902 #endif
903         /*
904          * Tell the upper layer(s) we support long frames.
905          * Must appear after the call to ether_ifattach() because
906          * ether_ifattach() sets ifi_hdrlen to the default value.
907          */
908         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
909
910         /* Hook interrupt last to avoid having to lock softc */
911         error = bus_setup_intr(dev, sc->sf_irq, INTR_TYPE_NET | INTR_MPSAFE,
912             NULL, sf_intr, sc, &sc->sf_intrhand);
913
914         if (error) {
915                 device_printf(dev, "couldn't set up irq\n");
916                 ether_ifdetach(ifp);
917                 goto fail;
918         }
919
920 fail:
921         if (error)
922                 sf_detach(dev);
923
924         return (error);
925 }
926
927 /*
928  * Shutdown hardware and free up resources. This can be called any
929  * time after the mutex has been initialized. It is called in both
930  * the error case in attach and the normal detach case so it needs
931  * to be careful about only freeing resources that have actually been
932  * allocated.
933  */
934 static int
935 sf_detach(device_t dev)
936 {
937         struct sf_softc         *sc;
938         struct ifnet            *ifp;
939
940         sc = device_get_softc(dev);
941         ifp = sc->sf_ifp;
942
943 #ifdef DEVICE_POLLING
944         if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
945                 ether_poll_deregister(ifp);
946 #endif
947
948         /* These should only be active if attach succeeded */
949         if (device_is_attached(dev)) {
950                 SF_LOCK(sc);
951                 sc->sf_detach = 1;
952                 sf_stop(sc);
953                 SF_UNLOCK(sc);
954                 callout_drain(&sc->sf_co);
955                 taskqueue_drain(taskqueue_swi, &sc->sf_link_task);
956                 if (ifp != NULL)
957                         ether_ifdetach(ifp);
958         }
959         if (sc->sf_miibus) {
960                 device_delete_child(dev, sc->sf_miibus);
961                 sc->sf_miibus = NULL;
962         }
963         bus_generic_detach(dev);
964
965         if (sc->sf_intrhand != NULL)
966                 bus_teardown_intr(dev, sc->sf_irq, sc->sf_intrhand);
967         if (sc->sf_irq != NULL)
968                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sf_irq);
969         if (sc->sf_res != NULL)
970                 bus_release_resource(dev, sc->sf_restype, sc->sf_rid,
971                     sc->sf_res);
972
973         sf_dma_free(sc);
974         if (ifp != NULL)
975                 if_free(ifp);
976
977         mtx_destroy(&sc->sf_mtx);
978
979         return (0);
980 }
981
982 struct sf_dmamap_arg {
983         bus_addr_t              sf_busaddr;
984 };
985
986 static void
987 sf_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
988 {
989         struct sf_dmamap_arg    *ctx;
990
991         if (error != 0)
992                 return;
993         ctx = arg;
994         ctx->sf_busaddr = segs[0].ds_addr;
995 }
996
997 static int
998 sf_dma_alloc(struct sf_softc *sc)
999 {
1000         struct sf_dmamap_arg    ctx;
1001         struct sf_txdesc        *txd;
1002         struct sf_rxdesc        *rxd;
1003         bus_addr_t              lowaddr;
1004         bus_addr_t              rx_ring_end, rx_cring_end;
1005         bus_addr_t              tx_ring_end, tx_cring_end;
1006         int                     error, i;
1007
1008         lowaddr = BUS_SPACE_MAXADDR;
1009
1010 again:
1011         /* Create parent DMA tag. */
1012         error = bus_dma_tag_create(
1013             bus_get_dma_tag(sc->sf_dev),        /* parent */
1014             1, 0,                       /* alignment, boundary */
1015             lowaddr,                    /* lowaddr */
1016             BUS_SPACE_MAXADDR,          /* highaddr */
1017             NULL, NULL,                 /* filter, filterarg */
1018             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
1019             0,                          /* nsegments */
1020             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1021             0,                          /* flags */
1022             NULL, NULL,                 /* lockfunc, lockarg */
1023             &sc->sf_cdata.sf_parent_tag);
1024         if (error != 0) {
1025                 device_printf(sc->sf_dev, "failed to create parent DMA tag\n");
1026                 goto fail;
1027         }
1028         /* Create tag for Tx ring. */
1029         error = bus_dma_tag_create(sc->sf_cdata.sf_parent_tag,/* parent */
1030             SF_RING_ALIGN, 0,           /* alignment, boundary */
1031             BUS_SPACE_MAXADDR,          /* lowaddr */
1032             BUS_SPACE_MAXADDR,          /* highaddr */
1033             NULL, NULL,                 /* filter, filterarg */
1034             SF_TX_DLIST_SIZE,           /* maxsize */
1035             1,                          /* nsegments */
1036             SF_TX_DLIST_SIZE,           /* maxsegsize */
1037             0,                          /* flags */
1038             NULL, NULL,                 /* lockfunc, lockarg */
1039             &sc->sf_cdata.sf_tx_ring_tag);
1040         if (error != 0) {
1041                 device_printf(sc->sf_dev, "failed to create Tx ring DMA tag\n");
1042                 goto fail;
1043         }
1044
1045         /* Create tag for Tx completion ring. */
1046         error = bus_dma_tag_create(sc->sf_cdata.sf_parent_tag,/* parent */
1047             SF_RING_ALIGN, 0,           /* alignment, boundary */
1048             BUS_SPACE_MAXADDR,          /* lowaddr */
1049             BUS_SPACE_MAXADDR,          /* highaddr */
1050             NULL, NULL,                 /* filter, filterarg */
1051             SF_TX_CLIST_SIZE,           /* maxsize */
1052             1,                          /* nsegments */
1053             SF_TX_CLIST_SIZE,           /* maxsegsize */
1054             0,                          /* flags */
1055             NULL, NULL,                 /* lockfunc, lockarg */
1056             &sc->sf_cdata.sf_tx_cring_tag);
1057         if (error != 0) {
1058                 device_printf(sc->sf_dev,
1059                     "failed to create Tx completion ring DMA tag\n");
1060                 goto fail;
1061         }
1062
1063         /* Create tag for Rx ring. */
1064         error = bus_dma_tag_create(sc->sf_cdata.sf_parent_tag,/* parent */
1065             SF_RING_ALIGN, 0,           /* alignment, boundary */
1066             BUS_SPACE_MAXADDR,          /* lowaddr */
1067             BUS_SPACE_MAXADDR,          /* highaddr */
1068             NULL, NULL,                 /* filter, filterarg */
1069             SF_RX_DLIST_SIZE,           /* maxsize */
1070             1,                          /* nsegments */
1071             SF_RX_DLIST_SIZE,           /* maxsegsize */
1072             0,                          /* flags */
1073             NULL, NULL,                 /* lockfunc, lockarg */
1074             &sc->sf_cdata.sf_rx_ring_tag);
1075         if (error != 0) {
1076                 device_printf(sc->sf_dev,
1077                     "failed to create Rx ring DMA tag\n");
1078                 goto fail;
1079         }
1080
1081         /* Create tag for Rx completion ring. */
1082         error = bus_dma_tag_create(sc->sf_cdata.sf_parent_tag,/* parent */
1083             SF_RING_ALIGN, 0,           /* alignment, boundary */
1084             BUS_SPACE_MAXADDR,          /* lowaddr */
1085             BUS_SPACE_MAXADDR,          /* highaddr */
1086             NULL, NULL,                 /* filter, filterarg */
1087             SF_RX_CLIST_SIZE,           /* maxsize */
1088             1,                          /* nsegments */
1089             SF_RX_CLIST_SIZE,           /* maxsegsize */
1090             0,                          /* flags */
1091             NULL, NULL,                 /* lockfunc, lockarg */
1092             &sc->sf_cdata.sf_rx_cring_tag);
1093         if (error != 0) {
1094                 device_printf(sc->sf_dev,
1095                     "failed to create Rx completion ring DMA tag\n");
1096                 goto fail;
1097         }
1098
1099         /* Create tag for Tx buffers. */
1100         error = bus_dma_tag_create(sc->sf_cdata.sf_parent_tag,/* parent */
1101             1, 0,                       /* alignment, boundary */
1102             BUS_SPACE_MAXADDR,          /* lowaddr */
1103             BUS_SPACE_MAXADDR,          /* highaddr */
1104             NULL, NULL,                 /* filter, filterarg */
1105             MCLBYTES * SF_MAXTXSEGS,    /* maxsize */
1106             SF_MAXTXSEGS,               /* nsegments */
1107             MCLBYTES,                   /* maxsegsize */
1108             0,                          /* flags */
1109             NULL, NULL,                 /* lockfunc, lockarg */
1110             &sc->sf_cdata.sf_tx_tag);
1111         if (error != 0) {
1112                 device_printf(sc->sf_dev, "failed to create Tx DMA tag\n");
1113                 goto fail;
1114         }
1115
1116         /* Create tag for Rx buffers. */
1117         error = bus_dma_tag_create(sc->sf_cdata.sf_parent_tag,/* parent */
1118             SF_RX_ALIGN, 0,             /* alignment, boundary */
1119             BUS_SPACE_MAXADDR,          /* lowaddr */
1120             BUS_SPACE_MAXADDR,          /* highaddr */
1121             NULL, NULL,                 /* filter, filterarg */
1122             MCLBYTES,                   /* maxsize */
1123             1,                          /* nsegments */
1124             MCLBYTES,                   /* maxsegsize */
1125             0,                          /* flags */
1126             NULL, NULL,                 /* lockfunc, lockarg */
1127             &sc->sf_cdata.sf_rx_tag);
1128         if (error != 0) {
1129                 device_printf(sc->sf_dev, "failed to create Rx DMA tag\n");
1130                 goto fail;
1131         }
1132
1133         /* Allocate DMA'able memory and load the DMA map for Tx ring. */
1134         error = bus_dmamem_alloc(sc->sf_cdata.sf_tx_ring_tag,
1135             (void **)&sc->sf_rdata.sf_tx_ring, BUS_DMA_WAITOK |
1136             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sf_cdata.sf_tx_ring_map);
1137         if (error != 0) {
1138                 device_printf(sc->sf_dev,
1139                     "failed to allocate DMA'able memory for Tx ring\n");
1140                 goto fail;
1141         }
1142
1143         ctx.sf_busaddr = 0;
1144         error = bus_dmamap_load(sc->sf_cdata.sf_tx_ring_tag,
1145             sc->sf_cdata.sf_tx_ring_map, sc->sf_rdata.sf_tx_ring,
1146             SF_TX_DLIST_SIZE, sf_dmamap_cb, &ctx, 0);
1147         if (error != 0 || ctx.sf_busaddr == 0) {
1148                 device_printf(sc->sf_dev,
1149                     "failed to load DMA'able memory for Tx ring\n");
1150                 goto fail;
1151         }
1152         sc->sf_rdata.sf_tx_ring_paddr = ctx.sf_busaddr;
1153
1154         /*
1155          * Allocate DMA'able memory and load the DMA map for Tx completion ring.
1156          */
1157         error = bus_dmamem_alloc(sc->sf_cdata.sf_tx_cring_tag,
1158             (void **)&sc->sf_rdata.sf_tx_cring, BUS_DMA_WAITOK |
1159             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sf_cdata.sf_tx_cring_map);
1160         if (error != 0) {
1161                 device_printf(sc->sf_dev,
1162                     "failed to allocate DMA'able memory for "
1163                     "Tx completion ring\n");
1164                 goto fail;
1165         }
1166
1167         ctx.sf_busaddr = 0;
1168         error = bus_dmamap_load(sc->sf_cdata.sf_tx_cring_tag,
1169             sc->sf_cdata.sf_tx_cring_map, sc->sf_rdata.sf_tx_cring,
1170             SF_TX_CLIST_SIZE, sf_dmamap_cb, &ctx, 0);
1171         if (error != 0 || ctx.sf_busaddr == 0) {
1172                 device_printf(sc->sf_dev,
1173                     "failed to load DMA'able memory for Tx completion ring\n");
1174                 goto fail;
1175         }
1176         sc->sf_rdata.sf_tx_cring_paddr = ctx.sf_busaddr;
1177
1178         /* Allocate DMA'able memory and load the DMA map for Rx ring. */
1179         error = bus_dmamem_alloc(sc->sf_cdata.sf_rx_ring_tag,
1180             (void **)&sc->sf_rdata.sf_rx_ring, BUS_DMA_WAITOK |
1181             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sf_cdata.sf_rx_ring_map);
1182         if (error != 0) {
1183                 device_printf(sc->sf_dev,
1184                     "failed to allocate DMA'able memory for Rx ring\n");
1185                 goto fail;
1186         }
1187
1188         ctx.sf_busaddr = 0;
1189         error = bus_dmamap_load(sc->sf_cdata.sf_rx_ring_tag,
1190             sc->sf_cdata.sf_rx_ring_map, sc->sf_rdata.sf_rx_ring,
1191             SF_RX_DLIST_SIZE, sf_dmamap_cb, &ctx, 0);
1192         if (error != 0 || ctx.sf_busaddr == 0) {
1193                 device_printf(sc->sf_dev,
1194                     "failed to load DMA'able memory for Rx ring\n");
1195                 goto fail;
1196         }
1197         sc->sf_rdata.sf_rx_ring_paddr = ctx.sf_busaddr;
1198
1199         /*
1200          * Allocate DMA'able memory and load the DMA map for Rx completion ring.
1201          */
1202         error = bus_dmamem_alloc(sc->sf_cdata.sf_rx_cring_tag,
1203             (void **)&sc->sf_rdata.sf_rx_cring, BUS_DMA_WAITOK |
1204             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sf_cdata.sf_rx_cring_map);
1205         if (error != 0) {
1206                 device_printf(sc->sf_dev,
1207                     "failed to allocate DMA'able memory for "
1208                     "Rx completion ring\n");
1209                 goto fail;
1210         }
1211
1212         ctx.sf_busaddr = 0;
1213         error = bus_dmamap_load(sc->sf_cdata.sf_rx_cring_tag,
1214             sc->sf_cdata.sf_rx_cring_map, sc->sf_rdata.sf_rx_cring,
1215             SF_RX_CLIST_SIZE, sf_dmamap_cb, &ctx, 0);
1216         if (error != 0 || ctx.sf_busaddr == 0) {
1217                 device_printf(sc->sf_dev,
1218                     "failed to load DMA'able memory for Rx completion ring\n");
1219                 goto fail;
1220         }
1221         sc->sf_rdata.sf_rx_cring_paddr = ctx.sf_busaddr;
1222
1223         /*
1224          * Tx desciptor ring and Tx completion ring should be addressed in
1225          * the same 4GB space. The same rule applys to Rx ring and Rx
1226          * completion ring. Unfortunately there is no way to specify this
1227          * boundary restriction with bus_dma(9). So just try to allocate
1228          * without the restriction and check the restriction was satisfied.
1229          * If not, fall back to 32bit dma addressing mode which always
1230          * guarantees the restriction.
1231          */
1232         tx_ring_end = sc->sf_rdata.sf_tx_ring_paddr + SF_TX_DLIST_SIZE;
1233         tx_cring_end = sc->sf_rdata.sf_tx_cring_paddr + SF_TX_CLIST_SIZE;
1234         rx_ring_end = sc->sf_rdata.sf_rx_ring_paddr + SF_RX_DLIST_SIZE;
1235         rx_cring_end = sc->sf_rdata.sf_rx_cring_paddr + SF_RX_CLIST_SIZE;
1236         if ((SF_ADDR_HI(sc->sf_rdata.sf_tx_ring_paddr) !=
1237             SF_ADDR_HI(tx_cring_end)) ||
1238             (SF_ADDR_HI(sc->sf_rdata.sf_tx_cring_paddr) !=
1239             SF_ADDR_HI(tx_ring_end)) ||
1240             (SF_ADDR_HI(sc->sf_rdata.sf_rx_ring_paddr) !=
1241             SF_ADDR_HI(rx_cring_end)) ||
1242             (SF_ADDR_HI(sc->sf_rdata.sf_rx_cring_paddr) !=
1243             SF_ADDR_HI(rx_ring_end))) {
1244                 device_printf(sc->sf_dev,
1245                     "switching to 32bit DMA mode\n");
1246                 sf_dma_free(sc);
1247                 /* Limit DMA address space to 32bit and try again. */
1248                 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1249                 goto again;
1250         }
1251
1252         /* Create DMA maps for Tx buffers. */
1253         for (i = 0; i < SF_TX_DLIST_CNT; i++) {
1254                 txd = &sc->sf_cdata.sf_txdesc[i];
1255                 txd->tx_m = NULL;
1256                 txd->ndesc = 0;
1257                 txd->tx_dmamap = NULL;
1258                 error = bus_dmamap_create(sc->sf_cdata.sf_tx_tag, 0,
1259                     &txd->tx_dmamap);
1260                 if (error != 0) {
1261                         device_printf(sc->sf_dev,
1262                             "failed to create Tx dmamap\n");
1263                         goto fail;
1264                 }
1265         }
1266         /* Create DMA maps for Rx buffers. */
1267         if ((error = bus_dmamap_create(sc->sf_cdata.sf_rx_tag, 0,
1268             &sc->sf_cdata.sf_rx_sparemap)) != 0) {
1269                 device_printf(sc->sf_dev,
1270                     "failed to create spare Rx dmamap\n");
1271                 goto fail;
1272         }
1273         for (i = 0; i < SF_RX_DLIST_CNT; i++) {
1274                 rxd = &sc->sf_cdata.sf_rxdesc[i];
1275                 rxd->rx_m = NULL;
1276                 rxd->rx_dmamap = NULL;
1277                 error = bus_dmamap_create(sc->sf_cdata.sf_rx_tag, 0,
1278                     &rxd->rx_dmamap);
1279                 if (error != 0) {
1280                         device_printf(sc->sf_dev,
1281                             "failed to create Rx dmamap\n");
1282                         goto fail;
1283                 }
1284         }
1285
1286 fail:
1287         return (error);
1288 }
1289
1290 static void
1291 sf_dma_free(struct sf_softc *sc)
1292 {
1293         struct sf_txdesc        *txd;
1294         struct sf_rxdesc        *rxd;
1295         int                     i;
1296
1297         /* Tx ring. */
1298         if (sc->sf_cdata.sf_tx_ring_tag) {
1299                 if (sc->sf_cdata.sf_tx_ring_map)
1300                         bus_dmamap_unload(sc->sf_cdata.sf_tx_ring_tag,
1301                             sc->sf_cdata.sf_tx_ring_map);
1302                 if (sc->sf_cdata.sf_tx_ring_map &&
1303                     sc->sf_rdata.sf_tx_ring)
1304                         bus_dmamem_free(sc->sf_cdata.sf_tx_ring_tag,
1305                             sc->sf_rdata.sf_tx_ring,
1306                             sc->sf_cdata.sf_tx_ring_map);
1307                 sc->sf_rdata.sf_tx_ring = NULL;
1308                 sc->sf_cdata.sf_tx_ring_map = NULL;
1309                 bus_dma_tag_destroy(sc->sf_cdata.sf_tx_ring_tag);
1310                 sc->sf_cdata.sf_tx_ring_tag = NULL;
1311         }
1312         /* Tx completion ring. */
1313         if (sc->sf_cdata.sf_tx_cring_tag) {
1314                 if (sc->sf_cdata.sf_tx_cring_map)
1315                         bus_dmamap_unload(sc->sf_cdata.sf_tx_cring_tag,
1316                             sc->sf_cdata.sf_tx_cring_map);
1317                 if (sc->sf_cdata.sf_tx_cring_map &&
1318                     sc->sf_rdata.sf_tx_cring)
1319                         bus_dmamem_free(sc->sf_cdata.sf_tx_cring_tag,
1320                             sc->sf_rdata.sf_tx_cring,
1321                             sc->sf_cdata.sf_tx_cring_map);
1322                 sc->sf_rdata.sf_tx_cring = NULL;
1323                 sc->sf_cdata.sf_tx_cring_map = NULL;
1324                 bus_dma_tag_destroy(sc->sf_cdata.sf_tx_cring_tag);
1325                 sc->sf_cdata.sf_tx_cring_tag = NULL;
1326         }
1327         /* Rx ring. */
1328         if (sc->sf_cdata.sf_rx_ring_tag) {
1329                 if (sc->sf_cdata.sf_rx_ring_map)
1330                         bus_dmamap_unload(sc->sf_cdata.sf_rx_ring_tag,
1331                             sc->sf_cdata.sf_rx_ring_map);
1332                 if (sc->sf_cdata.sf_rx_ring_map &&
1333                     sc->sf_rdata.sf_rx_ring)
1334                         bus_dmamem_free(sc->sf_cdata.sf_rx_ring_tag,
1335                             sc->sf_rdata.sf_rx_ring,
1336                             sc->sf_cdata.sf_rx_ring_map);
1337                 sc->sf_rdata.sf_rx_ring = NULL;
1338                 sc->sf_cdata.sf_rx_ring_map = NULL;
1339                 bus_dma_tag_destroy(sc->sf_cdata.sf_rx_ring_tag);
1340                 sc->sf_cdata.sf_rx_ring_tag = NULL;
1341         }
1342         /* Rx completion ring. */
1343         if (sc->sf_cdata.sf_rx_cring_tag) {
1344                 if (sc->sf_cdata.sf_rx_cring_map)
1345                         bus_dmamap_unload(sc->sf_cdata.sf_rx_cring_tag,
1346                             sc->sf_cdata.sf_rx_cring_map);
1347                 if (sc->sf_cdata.sf_rx_cring_map &&
1348                     sc->sf_rdata.sf_rx_cring)
1349                         bus_dmamem_free(sc->sf_cdata.sf_rx_cring_tag,
1350                             sc->sf_rdata.sf_rx_cring,
1351                             sc->sf_cdata.sf_rx_cring_map);
1352                 sc->sf_rdata.sf_rx_cring = NULL;
1353                 sc->sf_cdata.sf_rx_cring_map = NULL;
1354                 bus_dma_tag_destroy(sc->sf_cdata.sf_rx_cring_tag);
1355                 sc->sf_cdata.sf_rx_cring_tag = NULL;
1356         }
1357         /* Tx buffers. */
1358         if (sc->sf_cdata.sf_tx_tag) {
1359                 for (i = 0; i < SF_TX_DLIST_CNT; i++) {
1360                         txd = &sc->sf_cdata.sf_txdesc[i];
1361                         if (txd->tx_dmamap) {
1362                                 bus_dmamap_destroy(sc->sf_cdata.sf_tx_tag,
1363                                     txd->tx_dmamap);
1364                                 txd->tx_dmamap = NULL;
1365                         }
1366                 }
1367                 bus_dma_tag_destroy(sc->sf_cdata.sf_tx_tag);
1368                 sc->sf_cdata.sf_tx_tag = NULL;
1369         }
1370         /* Rx buffers. */
1371         if (sc->sf_cdata.sf_rx_tag) {
1372                 for (i = 0; i < SF_RX_DLIST_CNT; i++) {
1373                         rxd = &sc->sf_cdata.sf_rxdesc[i];
1374                         if (rxd->rx_dmamap) {
1375                                 bus_dmamap_destroy(sc->sf_cdata.sf_rx_tag,
1376                                     rxd->rx_dmamap);
1377                                 rxd->rx_dmamap = NULL;
1378                         }
1379                 }
1380                 if (sc->sf_cdata.sf_rx_sparemap) {
1381                         bus_dmamap_destroy(sc->sf_cdata.sf_rx_tag,
1382                             sc->sf_cdata.sf_rx_sparemap);
1383                         sc->sf_cdata.sf_rx_sparemap = 0;
1384                 }
1385                 bus_dma_tag_destroy(sc->sf_cdata.sf_rx_tag);
1386                 sc->sf_cdata.sf_rx_tag = NULL;
1387         }
1388
1389         if (sc->sf_cdata.sf_parent_tag) {
1390                 bus_dma_tag_destroy(sc->sf_cdata.sf_parent_tag);
1391                 sc->sf_cdata.sf_parent_tag = NULL;
1392         }
1393 }
1394
1395 static int
1396 sf_init_rx_ring(struct sf_softc *sc)
1397 {
1398         struct sf_ring_data     *rd;
1399         int                     i;
1400
1401         sc->sf_cdata.sf_rxc_cons = 0;
1402
1403         rd = &sc->sf_rdata;
1404         bzero(rd->sf_rx_ring, SF_RX_DLIST_SIZE);
1405         bzero(rd->sf_rx_cring, SF_RX_CLIST_SIZE);
1406
1407         for (i = 0; i < SF_RX_DLIST_CNT; i++) {
1408                 if (sf_newbuf(sc, i) != 0)
1409                         return (ENOBUFS);
1410         }
1411
1412         bus_dmamap_sync(sc->sf_cdata.sf_rx_cring_tag,
1413             sc->sf_cdata.sf_rx_cring_map,
1414             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1415         bus_dmamap_sync(sc->sf_cdata.sf_rx_ring_tag,
1416             sc->sf_cdata.sf_rx_ring_map,
1417             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1418
1419         return (0);
1420 }
1421
1422 static void
1423 sf_init_tx_ring(struct sf_softc *sc)
1424 {
1425         struct sf_ring_data     *rd;
1426         int                     i;
1427
1428         sc->sf_cdata.sf_tx_prod = 0;
1429         sc->sf_cdata.sf_tx_cnt = 0;
1430         sc->sf_cdata.sf_txc_cons = 0;
1431
1432         rd = &sc->sf_rdata;
1433         bzero(rd->sf_tx_ring, SF_TX_DLIST_SIZE);
1434         bzero(rd->sf_tx_cring, SF_TX_CLIST_SIZE);
1435         for (i = 0; i < SF_TX_DLIST_CNT; i++) {
1436                 rd->sf_tx_ring[i].sf_tx_ctrl = htole32(SF_TX_DESC_ID);
1437                 sc->sf_cdata.sf_txdesc[i].tx_m = NULL;
1438                 sc->sf_cdata.sf_txdesc[i].ndesc = 0;
1439         }
1440         rd->sf_tx_ring[i].sf_tx_ctrl |= htole32(SF_TX_DESC_END);
1441
1442         bus_dmamap_sync(sc->sf_cdata.sf_tx_ring_tag,
1443             sc->sf_cdata.sf_tx_ring_map,
1444             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1445         bus_dmamap_sync(sc->sf_cdata.sf_tx_cring_tag,
1446             sc->sf_cdata.sf_tx_cring_map,
1447             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1448 }
1449
1450 /*
1451  * Initialize an RX descriptor and attach an MBUF cluster.
1452  */
1453 static int
1454 sf_newbuf(struct sf_softc *sc, int idx)
1455 {
1456         struct sf_rx_rdesc      *desc;
1457         struct sf_rxdesc        *rxd;
1458         struct mbuf             *m;
1459         bus_dma_segment_t       segs[1];
1460         bus_dmamap_t            map;
1461         int                     nsegs;
1462
1463         m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1464         if (m == NULL)
1465                 return (ENOBUFS);
1466         m->m_len = m->m_pkthdr.len = MCLBYTES;
1467         m_adj(m, sizeof(uint32_t));
1468
1469         if (bus_dmamap_load_mbuf_sg(sc->sf_cdata.sf_rx_tag,
1470             sc->sf_cdata.sf_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1471                 m_freem(m);
1472                 return (ENOBUFS);
1473         }
1474         KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1475
1476         rxd = &sc->sf_cdata.sf_rxdesc[idx];
1477         if (rxd->rx_m != NULL) {
1478                 bus_dmamap_sync(sc->sf_cdata.sf_rx_tag, rxd->rx_dmamap,
1479                     BUS_DMASYNC_POSTREAD);
1480                 bus_dmamap_unload(sc->sf_cdata.sf_rx_tag, rxd->rx_dmamap);
1481         }
1482         map = rxd->rx_dmamap;
1483         rxd->rx_dmamap = sc->sf_cdata.sf_rx_sparemap;
1484         sc->sf_cdata.sf_rx_sparemap = map;
1485         bus_dmamap_sync(sc->sf_cdata.sf_rx_tag, rxd->rx_dmamap,
1486             BUS_DMASYNC_PREREAD);
1487         rxd->rx_m = m;
1488         desc = &sc->sf_rdata.sf_rx_ring[idx];
1489         desc->sf_addr = htole64(segs[0].ds_addr);
1490
1491         return (0);
1492 }
1493
1494 #ifndef __NO_STRICT_ALIGNMENT
1495 static __inline void
1496 sf_fixup_rx(struct mbuf *m)
1497 {
1498         int                     i;
1499         uint16_t                *src, *dst;
1500
1501         src = mtod(m, uint16_t *);
1502         dst = src - 1;
1503
1504         for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1505                 *dst++ = *src++;
1506
1507         m->m_data -= ETHER_ALIGN;
1508 }
1509 #endif
1510
1511 /*
1512  * The starfire is programmed to use 'normal' mode for packet reception,
1513  * which means we use the consumer/producer model for both the buffer
1514  * descriptor queue and the completion descriptor queue. The only problem
1515  * with this is that it involves a lot of register accesses: we have to
1516  * read the RX completion consumer and producer indexes and the RX buffer
1517  * producer index, plus the RX completion consumer and RX buffer producer
1518  * indexes have to be updated. It would have been easier if Adaptec had
1519  * put each index in a separate register, especially given that the damn
1520  * NIC has a 512K register space.
1521  *
1522  * In spite of all the lovely features that Adaptec crammed into the 6915,
1523  * it is marred by one truly stupid design flaw, which is that receive
1524  * buffer addresses must be aligned on a longword boundary. This forces
1525  * the packet payload to be unaligned, which is suboptimal on the x86 and
1526  * completely unuseable on the Alpha. Our only recourse is to copy received
1527  * packets into properly aligned buffers before handing them off.
1528  */
1529 static int
1530 sf_rxeof(struct sf_softc *sc)
1531 {
1532         struct mbuf             *m;
1533         struct ifnet            *ifp;
1534         struct sf_rxdesc        *rxd;
1535         struct sf_rx_rcdesc     *cur_cmp;
1536         int                     cons, eidx, prog, rx_npkts;
1537         uint32_t                status, status2;
1538
1539         SF_LOCK_ASSERT(sc);
1540
1541         ifp = sc->sf_ifp;
1542         rx_npkts = 0;
1543
1544         bus_dmamap_sync(sc->sf_cdata.sf_rx_ring_tag,
1545             sc->sf_cdata.sf_rx_ring_map,
1546             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1547         bus_dmamap_sync(sc->sf_cdata.sf_rx_cring_tag,
1548             sc->sf_cdata.sf_rx_cring_map,
1549             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1550
1551         /*
1552          * To reduce register access, directly read Receive completion
1553          * queue entry.
1554          */
1555         eidx = 0;
1556         prog = 0;
1557         for (cons = sc->sf_cdata.sf_rxc_cons; ; SF_INC(cons, SF_RX_CLIST_CNT)) {
1558                 cur_cmp = &sc->sf_rdata.sf_rx_cring[cons];
1559                 status = le32toh(cur_cmp->sf_rx_status1);
1560                 if (status == 0)
1561                         break;
1562 #ifdef DEVICE_POLLING
1563                 if ((ifp->if_capenable & IFCAP_POLLING) != 0) {
1564                         if (sc->rxcycles <= 0)
1565                                 break;
1566                         sc->rxcycles--;
1567                 }
1568 #endif
1569                 prog++;
1570                 eidx = (status & SF_RX_CMPDESC_EIDX) >> 16;
1571                 rxd = &sc->sf_cdata.sf_rxdesc[eidx];
1572                 m = rxd->rx_m;
1573
1574                 /*
1575                  * Note, if_ipackets and if_ierrors counters
1576                  * are handled in sf_stats_update().
1577                  */
1578                 if ((status & SF_RXSTAT1_OK) == 0) {
1579                         cur_cmp->sf_rx_status1 = 0;
1580                         continue;
1581                 }
1582
1583                 if (sf_newbuf(sc, eidx) != 0) {
1584                         ifp->if_iqdrops++;
1585                         cur_cmp->sf_rx_status1 = 0;
1586                         continue;
1587                 }
1588
1589                 /* AIC-6915 supports TCP/UDP checksum offload. */
1590                 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1591                         status2 = le32toh(cur_cmp->sf_rx_status2);
1592                         /*
1593                          * Sometimes AIC-6915 generates an interrupt to
1594                          * warn RxGFP stall with bad checksum bit set
1595                          * in status word. I'm not sure what conditioan
1596                          * triggers it but recevied packet's checksum
1597                          * was correct even though AIC-6915 does not
1598                          * agree on this. This may be an indication of
1599                          * firmware bug. To fix the issue, do not rely
1600                          * on bad checksum bit in status word and let
1601                          * upper layer verify integrity of received
1602                          * frame.
1603                          * Another nice feature of AIC-6915 is hardware
1604                          * assistance of checksum calculation by
1605                          * providing partial checksum value for received
1606                          * frame. The partial checksum value can be used
1607                          * to accelerate checksum computation for
1608                          * fragmented TCP/UDP packets. Upper network
1609                          * stack already takes advantage of the partial
1610                          * checksum value in IP reassembly stage. But
1611                          * I'm not sure the correctness of the partial
1612                          * hardware checksum assistance as frequent
1613                          * RxGFP stalls are seen on non-fragmented
1614                          * frames. Due to the nature of the complexity
1615                          * of checksum computation code in firmware it's
1616                          * possible to see another bug in RxGFP so
1617                          * ignore checksum assistance for fragmented
1618                          * frames. This can be changed in future.
1619                          */
1620                         if ((status2 & SF_RXSTAT2_FRAG) == 0) {
1621                                 if ((status2 & (SF_RXSTAT2_TCP |
1622                                     SF_RXSTAT2_UDP)) != 0) {
1623                                         if ((status2 & SF_RXSTAT2_CSUM_OK)) {
1624                                                 m->m_pkthdr.csum_flags =
1625                                                     CSUM_DATA_VALID |
1626                                                     CSUM_PSEUDO_HDR;
1627                                                 m->m_pkthdr.csum_data = 0xffff;
1628                                         }
1629                                 }
1630                         }
1631 #ifdef SF_PARTIAL_CSUM_SUPPORT
1632                         else if ((status2 & SF_RXSTAT2_FRAG) != 0) {
1633                                 if ((status2 & (SF_RXSTAT2_TCP |
1634                                     SF_RXSTAT2_UDP)) != 0) {
1635                                         if ((status2 & SF_RXSTAT2_PCSUM_OK)) {
1636                                                 m->m_pkthdr.csum_flags =
1637                                                     CSUM_DATA_VALID;
1638                                                 m->m_pkthdr.csum_data =
1639                                                     (status &
1640                                                     SF_RX_CMPDESC_CSUM2);
1641                                         }
1642                                 }
1643                         }
1644 #endif
1645                 }
1646
1647                 m->m_pkthdr.len = m->m_len = status & SF_RX_CMPDESC_LEN;
1648 #ifndef __NO_STRICT_ALIGNMENT
1649                 sf_fixup_rx(m);
1650 #endif
1651                 m->m_pkthdr.rcvif = ifp;
1652
1653                 SF_UNLOCK(sc);
1654                 (*ifp->if_input)(ifp, m);
1655                 SF_LOCK(sc);
1656                 rx_npkts++;
1657
1658                 /* Clear completion status. */
1659                 cur_cmp->sf_rx_status1 = 0;
1660         }
1661
1662         if (prog > 0) {
1663                 sc->sf_cdata.sf_rxc_cons = cons;
1664                 bus_dmamap_sync(sc->sf_cdata.sf_rx_ring_tag,
1665                     sc->sf_cdata.sf_rx_ring_map,
1666                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1667                 bus_dmamap_sync(sc->sf_cdata.sf_rx_cring_tag,
1668                     sc->sf_cdata.sf_rx_cring_map,
1669                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1670
1671                 /* Update Rx completion Q1 consumer index. */
1672                 csr_write_4(sc, SF_CQ_CONSIDX,
1673                     (csr_read_4(sc, SF_CQ_CONSIDX) & ~SF_CQ_CONSIDX_RXQ1) |
1674                     (cons & SF_CQ_CONSIDX_RXQ1));
1675                 /* Update Rx descriptor Q1 ptr. */
1676                 csr_write_4(sc, SF_RXDQ_PTR_Q1,
1677                     (csr_read_4(sc, SF_RXDQ_PTR_Q1) & ~SF_RXDQ_PRODIDX) |
1678                     (eidx & SF_RXDQ_PRODIDX));
1679         }
1680         return (rx_npkts);
1681 }
1682
1683 /*
1684  * Read the transmit status from the completion queue and release
1685  * mbufs. Note that the buffer descriptor index in the completion
1686  * descriptor is an offset from the start of the transmit buffer
1687  * descriptor list in bytes. This is important because the manual
1688  * gives the impression that it should match the producer/consumer
1689  * index, which is the offset in 8 byte blocks.
1690  */
1691 static void
1692 sf_txeof(struct sf_softc *sc)
1693 {
1694         struct sf_txdesc        *txd;
1695         struct sf_tx_rcdesc     *cur_cmp;
1696         struct ifnet            *ifp;
1697         uint32_t                status;
1698         int                     cons, idx, prod;
1699
1700         SF_LOCK_ASSERT(sc);
1701
1702         ifp = sc->sf_ifp;
1703
1704         bus_dmamap_sync(sc->sf_cdata.sf_tx_cring_tag,
1705             sc->sf_cdata.sf_tx_cring_map,
1706             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1707
1708         cons = sc->sf_cdata.sf_txc_cons;
1709         prod = (csr_read_4(sc, SF_CQ_PRODIDX) & SF_TXDQ_PRODIDX_HIPRIO) >> 16;
1710         if (prod == cons)
1711                 return;
1712
1713         for (; cons != prod; SF_INC(cons, SF_TX_CLIST_CNT)) {
1714                 cur_cmp = &sc->sf_rdata.sf_tx_cring[cons];
1715                 status = le32toh(cur_cmp->sf_tx_status1);
1716                 if (status == 0)
1717                         break;
1718                 switch (status & SF_TX_CMPDESC_TYPE) {
1719                 case SF_TXCMPTYPE_TX:
1720                         /* Tx complete entry. */
1721                         break;
1722                 case SF_TXCMPTYPE_DMA:
1723                         /* DMA complete entry. */
1724                         idx = status & SF_TX_CMPDESC_IDX;
1725                         idx = idx / sizeof(struct sf_tx_rdesc);
1726                         /*
1727                          * We don't need to check Tx status here.
1728                          * SF_ISR_TX_LOFIFO intr would handle this.
1729                          * Note, if_opackets, if_collisions and if_oerrors
1730                          * counters are handled in sf_stats_update().
1731                          */
1732                         txd = &sc->sf_cdata.sf_txdesc[idx];
1733                         if (txd->tx_m != NULL) {
1734                                 bus_dmamap_sync(sc->sf_cdata.sf_tx_tag,
1735                                     txd->tx_dmamap,
1736                                     BUS_DMASYNC_POSTWRITE);
1737                                 bus_dmamap_unload(sc->sf_cdata.sf_tx_tag,
1738                                     txd->tx_dmamap);
1739                                 m_freem(txd->tx_m);
1740                                 txd->tx_m = NULL;
1741                         }
1742                         sc->sf_cdata.sf_tx_cnt -= txd->ndesc;
1743                         KASSERT(sc->sf_cdata.sf_tx_cnt >= 0,
1744                             ("%s: Active Tx desc counter was garbled\n",
1745                             __func__));
1746                         txd->ndesc = 0;
1747                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1748                         break;
1749                 default:
1750                         /* It should not happen. */
1751                         device_printf(sc->sf_dev,
1752                             "unknown Tx completion type : 0x%08x : %d : %d\n",
1753                             status, cons, prod);
1754                         break;
1755                 }
1756                 cur_cmp->sf_tx_status1 = 0;
1757         }
1758
1759         sc->sf_cdata.sf_txc_cons = cons;
1760         bus_dmamap_sync(sc->sf_cdata.sf_tx_cring_tag,
1761             sc->sf_cdata.sf_tx_cring_map,
1762             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1763
1764         if (sc->sf_cdata.sf_tx_cnt == 0)
1765                 sc->sf_watchdog_timer = 0;
1766
1767         /* Update Tx completion consumer index. */
1768         csr_write_4(sc, SF_CQ_CONSIDX,
1769             (csr_read_4(sc, SF_CQ_CONSIDX) & 0xffff) |
1770             ((cons << 16) & 0xffff0000));
1771 }
1772
1773 static void
1774 sf_txthresh_adjust(struct sf_softc *sc)
1775 {
1776         uint32_t                txfctl;
1777
1778         device_printf(sc->sf_dev, "Tx underrun -- ");
1779         if (sc->sf_txthresh < SF_MAX_TX_THRESHOLD) {
1780                 txfctl = csr_read_4(sc, SF_TX_FRAMCTL);
1781                 /* Increase Tx threshold 256 bytes. */
1782                 sc->sf_txthresh += 16;
1783                 if (sc->sf_txthresh > SF_MAX_TX_THRESHOLD)
1784                         sc->sf_txthresh = SF_MAX_TX_THRESHOLD;
1785                 txfctl &= ~SF_TXFRMCTL_TXTHRESH;
1786                 txfctl |= sc->sf_txthresh;
1787                 printf("increasing Tx threshold to %d bytes\n",
1788                     sc->sf_txthresh * SF_TX_THRESHOLD_UNIT);
1789                 csr_write_4(sc, SF_TX_FRAMCTL, txfctl);
1790         } else
1791                 printf("\n");
1792 }
1793
1794 #ifdef DEVICE_POLLING
1795 static int
1796 sf_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1797 {
1798         struct sf_softc         *sc;
1799         uint32_t                status;
1800         int                     rx_npkts;
1801
1802         sc = ifp->if_softc;
1803         rx_npkts = 0;
1804         SF_LOCK(sc);
1805
1806         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1807                 SF_UNLOCK(sc);
1808                 return (rx_npkts);
1809         }
1810
1811         sc->rxcycles = count;
1812         rx_npkts = sf_rxeof(sc);
1813         sf_txeof(sc);
1814         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1815                 sf_start_locked(ifp);
1816
1817         if (cmd == POLL_AND_CHECK_STATUS) {
1818                 /* Reading the ISR register clears all interrrupts. */
1819                 status = csr_read_4(sc, SF_ISR);
1820
1821                 if ((status & SF_ISR_ABNORMALINTR) != 0) {
1822                         if ((status & SF_ISR_STATSOFLOW) != 0)
1823                                 sf_stats_update(sc);
1824                         else if ((status & SF_ISR_TX_LOFIFO) != 0)
1825                                 sf_txthresh_adjust(sc);
1826                         else if ((status & SF_ISR_DMAERR) != 0) {
1827                                 device_printf(sc->sf_dev,
1828                                     "DMA error, resetting\n");
1829                                 sf_init_locked(sc);
1830                         } else if ((status & SF_ISR_NO_TX_CSUM) != 0) {
1831                                 sc->sf_statistics.sf_tx_gfp_stall++;
1832 #ifdef  SF_GFP_DEBUG
1833                                 device_printf(sc->sf_dev,
1834                                     "TxGFP is not responding!\n");
1835 #endif
1836                         } else if ((status & SF_ISR_RXGFP_NORESP) != 0) {
1837                                 sc->sf_statistics.sf_rx_gfp_stall++;
1838 #ifdef  SF_GFP_DEBUG
1839                                 device_printf(sc->sf_dev,
1840                                     "RxGFP is not responding!\n");
1841 #endif
1842                         }
1843                 }
1844         }
1845
1846         SF_UNLOCK(sc);
1847         return (rx_npkts);
1848 }
1849 #endif /* DEVICE_POLLING */
1850
1851 static void
1852 sf_intr(void *arg)
1853 {
1854         struct sf_softc         *sc;
1855         struct ifnet            *ifp;
1856         uint32_t                status;
1857
1858         sc = (struct sf_softc *)arg;
1859         SF_LOCK(sc);
1860
1861         if (sc->sf_suspended != 0)
1862                 goto done_locked;
1863
1864         /* Reading the ISR register clears all interrrupts. */
1865         status = csr_read_4(sc, SF_ISR);
1866         if (status == 0 || status == 0xffffffff ||
1867             (status & SF_ISR_PCIINT_ASSERTED) == 0)
1868                 goto done_locked;
1869
1870         ifp = sc->sf_ifp;
1871 #ifdef DEVICE_POLLING
1872         if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1873                 goto done_locked;
1874 #endif
1875         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1876                 goto done_locked;
1877
1878         /* Disable interrupts. */
1879         csr_write_4(sc, SF_IMR, 0x00000000);
1880
1881         for (; (status & SF_INTRS) != 0;) {
1882                 if ((status & SF_ISR_RXDQ1_DMADONE) != 0)
1883                         sf_rxeof(sc);
1884
1885                 if ((status & (SF_ISR_TX_TXDONE | SF_ISR_TX_DMADONE |
1886                     SF_ISR_TX_QUEUEDONE)) != 0)
1887                         sf_txeof(sc);
1888
1889                 if ((status & SF_ISR_ABNORMALINTR) != 0) {
1890                         if ((status & SF_ISR_STATSOFLOW) != 0)
1891                                 sf_stats_update(sc);
1892                         else if ((status & SF_ISR_TX_LOFIFO) != 0)
1893                                 sf_txthresh_adjust(sc);
1894                         else if ((status & SF_ISR_DMAERR) != 0) {
1895                                 device_printf(sc->sf_dev,
1896                                     "DMA error, resetting\n");
1897                                 sf_init_locked(sc);
1898                                 break;
1899                         } else if ((status & SF_ISR_NO_TX_CSUM) != 0) {
1900                                 sc->sf_statistics.sf_tx_gfp_stall++;
1901 #ifdef  SF_GFP_DEBUG
1902                                 device_printf(sc->sf_dev,
1903                                     "TxGFP is not responding!\n");
1904 #endif
1905                         }
1906                         else if ((status & SF_ISR_RXGFP_NORESP) != 0) {
1907                                 sc->sf_statistics.sf_rx_gfp_stall++;
1908 #ifdef  SF_GFP_DEBUG
1909                                 device_printf(sc->sf_dev,
1910                                     "RxGFP is not responding!\n");
1911 #endif
1912                         }
1913                 }
1914                 /* Reading the ISR register clears all interrrupts. */
1915                 status = csr_read_4(sc, SF_ISR);
1916         }
1917
1918         /* Re-enable interrupts. */
1919         csr_write_4(sc, SF_IMR, SF_INTRS);
1920
1921         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1922                 sf_start_locked(ifp);
1923 done_locked:
1924         SF_UNLOCK(sc);
1925 }
1926
1927 static void
1928 sf_download_fw(struct sf_softc *sc)
1929 {
1930         uint32_t gfpinst;
1931         int i, ndx;
1932         uint8_t *p;
1933
1934         /*
1935          * A FP instruction is composed of 48bits so we have to
1936          * write it with two parts.
1937          */
1938         p = txfwdata;
1939         ndx = 0;
1940         for (i = 0; i < sizeof(txfwdata) / SF_GFP_INST_BYTES; i++) {
1941                 gfpinst = p[2] << 24 | p[3] << 16 | p[4] << 8 | p[5];
1942                 csr_write_4(sc, SF_TXGFP_MEM_BASE + ndx * 4, gfpinst);
1943                 gfpinst = p[0] << 8 | p[1];
1944                 csr_write_4(sc, SF_TXGFP_MEM_BASE + (ndx + 1) * 4, gfpinst);
1945                 p += SF_GFP_INST_BYTES;
1946                 ndx += 2;
1947         }
1948         if (bootverbose)
1949                 device_printf(sc->sf_dev, "%d Tx instructions downloaded\n", i);
1950
1951         p = rxfwdata;
1952         ndx = 0;
1953         for (i = 0; i < sizeof(rxfwdata) / SF_GFP_INST_BYTES; i++) {
1954                 gfpinst = p[2] << 24 | p[3] << 16 | p[4] << 8 | p[5];
1955                 csr_write_4(sc, SF_RXGFP_MEM_BASE + (ndx * 4), gfpinst);
1956                 gfpinst = p[0] << 8 | p[1];
1957                 csr_write_4(sc, SF_RXGFP_MEM_BASE + (ndx + 1) * 4, gfpinst);
1958                 p += SF_GFP_INST_BYTES;
1959                 ndx += 2;
1960         }
1961         if (bootverbose)
1962                 device_printf(sc->sf_dev, "%d Rx instructions downloaded\n", i);
1963 }
1964
1965 static void
1966 sf_init(void *xsc)
1967 {
1968         struct sf_softc         *sc;
1969
1970         sc = (struct sf_softc *)xsc;
1971         SF_LOCK(sc);
1972         sf_init_locked(sc);
1973         SF_UNLOCK(sc);
1974 }
1975
1976 static void
1977 sf_init_locked(struct sf_softc *sc)
1978 {
1979         struct ifnet            *ifp;
1980         struct mii_data         *mii;
1981         uint8_t                 eaddr[ETHER_ADDR_LEN];
1982         bus_addr_t              addr;
1983         int                     i;
1984
1985         SF_LOCK_ASSERT(sc);
1986         ifp = sc->sf_ifp;
1987         mii = device_get_softc(sc->sf_miibus);
1988
1989         sf_stop(sc);
1990         /* Reset the hardware to a known state. */
1991         sf_reset(sc);
1992
1993         /* Init all the receive filter registers */
1994         for (i = SF_RXFILT_PERFECT_BASE;
1995             i < (SF_RXFILT_HASH_MAX + 1); i += sizeof(uint32_t))
1996                 csr_write_4(sc, i, 0);
1997
1998         /* Empty stats counter registers. */
1999         for (i = SF_STATS_BASE; i < (SF_STATS_END + 1); i += sizeof(uint32_t))
2000                 csr_write_4(sc, i, 0);
2001
2002         /* Init our MAC address. */
2003         bcopy(IF_LLADDR(sc->sf_ifp), eaddr, sizeof(eaddr));
2004         csr_write_4(sc, SF_PAR0,
2005             eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
2006         csr_write_4(sc, SF_PAR1, eaddr[0] << 8 | eaddr[1]);
2007         sf_setperf(sc, 0, eaddr);
2008
2009         if (sf_init_rx_ring(sc) == ENOBUFS) {
2010                 device_printf(sc->sf_dev,
2011                     "initialization failed: no memory for rx buffers\n");
2012                 return;
2013         }
2014
2015         sf_init_tx_ring(sc);
2016
2017         /*
2018          * 16 perfect address filtering.
2019          * Hash only multicast destination address, Accept matching
2020          * frames regardless of VLAN ID.
2021          */
2022         csr_write_4(sc, SF_RXFILT, SF_PERFMODE_NORMAL | SF_HASHMODE_ANYVLAN);
2023
2024         /*
2025          * Set Rx filter.
2026          */
2027         sf_rxfilter(sc);
2028
2029         /* Init the completion queue indexes. */
2030         csr_write_4(sc, SF_CQ_CONSIDX, 0);
2031         csr_write_4(sc, SF_CQ_PRODIDX, 0);
2032
2033         /* Init the RX completion queue. */
2034         addr = sc->sf_rdata.sf_rx_cring_paddr;
2035         csr_write_4(sc, SF_CQ_ADDR_HI, SF_ADDR_HI(addr));
2036         csr_write_4(sc, SF_RXCQ_CTL_1, SF_ADDR_LO(addr) & SF_RXCQ_ADDR);
2037         if (SF_ADDR_HI(addr) != 0)
2038                 SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQ_USE_64BIT);
2039         /* Set RX completion queue type 2. */
2040         SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQTYPE_2);
2041         csr_write_4(sc, SF_RXCQ_CTL_2, 0);
2042
2043         /*
2044          * Init RX DMA control.
2045          * default RxHighPriority Threshold,
2046          * default RxBurstSize, 128bytes.
2047          */
2048         SF_SETBIT(sc, SF_RXDMA_CTL,
2049             SF_RXDMA_REPORTBADPKTS |
2050             (SF_RXDMA_HIGHPRIO_THRESH << 8) |
2051             SF_RXDMA_BURST);
2052
2053         /* Init the RX buffer descriptor queue. */
2054         addr = sc->sf_rdata.sf_rx_ring_paddr;
2055         csr_write_4(sc, SF_RXDQ_ADDR_HI, SF_ADDR_HI(addr));
2056         csr_write_4(sc, SF_RXDQ_ADDR_Q1, SF_ADDR_LO(addr));
2057
2058         /* Set RX queue buffer length. */
2059         csr_write_4(sc, SF_RXDQ_CTL_1,
2060             ((MCLBYTES  - sizeof(uint32_t)) << 16) |
2061             SF_RXDQCTL_64BITBADDR | SF_RXDQCTL_VARIABLE);
2062
2063         if (SF_ADDR_HI(addr) != 0)
2064                 SF_SETBIT(sc, SF_RXDQ_CTL_1, SF_RXDQCTL_64BITDADDR);
2065         csr_write_4(sc, SF_RXDQ_PTR_Q1, SF_RX_DLIST_CNT - 1);
2066         csr_write_4(sc, SF_RXDQ_CTL_2, 0);
2067
2068         /* Init the TX completion queue */
2069         addr = sc->sf_rdata.sf_tx_cring_paddr;
2070         csr_write_4(sc, SF_TXCQ_CTL, SF_ADDR_LO(addr) & SF_TXCQ_ADDR);
2071         if (SF_ADDR_HI(addr) != 0)
2072                 SF_SETBIT(sc, SF_TXCQ_CTL, SF_TXCQ_USE_64BIT);
2073
2074         /* Init the TX buffer descriptor queue. */
2075         addr = sc->sf_rdata.sf_tx_ring_paddr;
2076         csr_write_4(sc, SF_TXDQ_ADDR_HI, SF_ADDR_HI(addr));
2077         csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0);
2078         csr_write_4(sc, SF_TXDQ_ADDR_LOPRIO, SF_ADDR_LO(addr));
2079         csr_write_4(sc, SF_TX_FRAMCTL,
2080             SF_TXFRMCTL_CPLAFTERTX | sc->sf_txthresh);
2081         csr_write_4(sc, SF_TXDQ_CTL,
2082             SF_TXDMA_HIPRIO_THRESH << 24 |
2083             SF_TXSKIPLEN_0BYTES << 16 |
2084             SF_TXDDMA_BURST << 8 |
2085             SF_TXBUFDESC_TYPE2 | SF_TXMINSPACE_UNLIMIT);
2086         if (SF_ADDR_HI(addr) != 0)
2087                 SF_SETBIT(sc, SF_TXDQ_CTL, SF_TXDQCTL_64BITADDR);
2088
2089         /* Set VLAN Type register. */
2090         csr_write_4(sc, SF_VLANTYPE, ETHERTYPE_VLAN);
2091
2092         /* Set TxPause Timer. */
2093         csr_write_4(sc, SF_TXPAUSETIMER, 0xffff);
2094
2095         /* Enable autopadding of short TX frames. */
2096         SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_AUTOPAD);
2097         SF_SETBIT(sc, SF_MACCFG_2, SF_MACCFG2_AUTOVLANPAD);
2098         /* Make sure to reset MAC to take changes effect. */
2099         SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
2100         DELAY(1000);
2101         SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
2102
2103         /* Enable PCI bus master. */
2104         SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_PCIMEN);
2105
2106         /* Load StarFire firmware. */
2107         sf_download_fw(sc);
2108
2109         /* Intialize interrupt moderation. */
2110         csr_write_4(sc, SF_TIMER_CTL, SF_TIMER_IMASK_MODE | SF_TIMER_TIMES_TEN |
2111             (sc->sf_int_mod & SF_TIMER_IMASK_INTERVAL));
2112
2113 #ifdef DEVICE_POLLING
2114         /* Disable interrupts if we are polling. */
2115         if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2116                 csr_write_4(sc, SF_IMR, 0x00000000);
2117         else
2118 #endif
2119         /* Enable interrupts. */
2120         csr_write_4(sc, SF_IMR, SF_INTRS);
2121         SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_INTR_ENB);
2122
2123         /* Enable the RX and TX engines. */
2124         csr_write_4(sc, SF_GEN_ETH_CTL,
2125             SF_ETHCTL_RX_ENB | SF_ETHCTL_RXDMA_ENB |
2126             SF_ETHCTL_TX_ENB | SF_ETHCTL_TXDMA_ENB);
2127
2128         if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
2129                 SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TXGFP_ENB);
2130         else
2131                 SF_CLRBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TXGFP_ENB);
2132         if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
2133                 SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RXGFP_ENB);
2134         else
2135                 SF_CLRBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RXGFP_ENB);
2136
2137         sc->sf_link = 0;
2138         mii_mediachg(mii);
2139
2140         ifp->if_drv_flags |= IFF_DRV_RUNNING;
2141         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2142
2143         callout_reset(&sc->sf_co, hz, sf_tick, sc);
2144 }
2145
2146 static int
2147 sf_encap(struct sf_softc *sc, struct mbuf **m_head)
2148 {
2149         struct sf_txdesc        *txd;
2150         struct sf_tx_rdesc      *desc;
2151         struct mbuf             *m;
2152         bus_dmamap_t            map;
2153         bus_dma_segment_t       txsegs[SF_MAXTXSEGS];
2154         int                     error, i, nsegs, prod, si;
2155         int                     avail, nskip;
2156
2157         SF_LOCK_ASSERT(sc);
2158
2159         m = *m_head;
2160         prod = sc->sf_cdata.sf_tx_prod;
2161         txd = &sc->sf_cdata.sf_txdesc[prod];
2162         map = txd->tx_dmamap;
2163         error = bus_dmamap_load_mbuf_sg(sc->sf_cdata.sf_tx_tag, map,
2164             *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
2165         if (error == EFBIG) {
2166                 m = m_collapse(*m_head, M_DONTWAIT, SF_MAXTXSEGS);
2167                 if (m == NULL) {
2168                         m_freem(*m_head);
2169                         *m_head = NULL;
2170                         return (ENOBUFS);
2171                 }
2172                 *m_head = m;
2173                 error = bus_dmamap_load_mbuf_sg(sc->sf_cdata.sf_tx_tag,
2174                     map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
2175                 if (error != 0) {
2176                         m_freem(*m_head);
2177                         *m_head = NULL;
2178                         return (error);
2179                 }
2180         } else if (error != 0)
2181                 return (error);
2182         if (nsegs == 0) {
2183                 m_freem(*m_head);
2184                 *m_head = NULL;
2185                 return (EIO);
2186         }
2187
2188         /* Check number of available descriptors. */
2189         avail = (SF_TX_DLIST_CNT - 1) - sc->sf_cdata.sf_tx_cnt;
2190         if (avail < nsegs) {
2191                 bus_dmamap_unload(sc->sf_cdata.sf_tx_tag, map);
2192                 return (ENOBUFS);
2193         }
2194         nskip = 0;
2195         if (prod + nsegs >= SF_TX_DLIST_CNT) {
2196                 nskip = SF_TX_DLIST_CNT - prod - 1;
2197                 if (avail < nsegs + nskip) {
2198                         bus_dmamap_unload(sc->sf_cdata.sf_tx_tag, map);
2199                         return (ENOBUFS);
2200                 }
2201         }
2202
2203         bus_dmamap_sync(sc->sf_cdata.sf_tx_tag, map, BUS_DMASYNC_PREWRITE);
2204
2205         si = prod;
2206         for (i = 0; i < nsegs; i++) {
2207                 desc = &sc->sf_rdata.sf_tx_ring[prod];
2208                 desc->sf_tx_ctrl = htole32(SF_TX_DESC_ID |
2209                     (txsegs[i].ds_len & SF_TX_DESC_FRAGLEN));
2210                 desc->sf_tx_reserved = 0;
2211                 desc->sf_addr = htole64(txsegs[i].ds_addr);
2212                 if (i == 0 && prod + nsegs >= SF_TX_DLIST_CNT) {
2213                         /* Queue wraps! */
2214                         desc->sf_tx_ctrl |= htole32(SF_TX_DESC_END);
2215                         prod = 0;
2216                 } else
2217                         SF_INC(prod, SF_TX_DLIST_CNT);
2218         }
2219         /* Update producer index. */
2220         sc->sf_cdata.sf_tx_prod = prod;
2221         sc->sf_cdata.sf_tx_cnt += nsegs + nskip;
2222
2223         desc = &sc->sf_rdata.sf_tx_ring[si];
2224         /* Check TDP/UDP checksum offload request. */
2225         if ((m->m_pkthdr.csum_flags & SF_CSUM_FEATURES) != 0)
2226                 desc->sf_tx_ctrl |= htole32(SF_TX_DESC_CALTCP);
2227         desc->sf_tx_ctrl |=
2228             htole32(SF_TX_DESC_CRCEN | SF_TX_DESC_INTR | (nsegs << 16));
2229
2230         txd->tx_dmamap = map;
2231         txd->tx_m = m;
2232         txd->ndesc = nsegs + nskip;
2233
2234         return (0);
2235 }
2236
2237 static void
2238 sf_start(struct ifnet *ifp)
2239 {
2240         struct sf_softc         *sc;
2241
2242         sc = ifp->if_softc;
2243         SF_LOCK(sc);
2244         sf_start_locked(ifp);
2245         SF_UNLOCK(sc);
2246 }
2247
2248 static void
2249 sf_start_locked(struct ifnet *ifp)
2250 {
2251         struct sf_softc         *sc;
2252         struct mbuf             *m_head;
2253         int                     enq;
2254
2255         sc = ifp->if_softc;
2256         SF_LOCK_ASSERT(sc);
2257
2258         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2259             IFF_DRV_RUNNING || sc->sf_link == 0)
2260                 return;
2261
2262         /*
2263          * Since we don't know when descriptor wrap occurrs in advance
2264          * limit available number of active Tx descriptor counter to be
2265          * higher than maximum number of DMA segments allowed in driver.
2266          */
2267         for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2268             sc->sf_cdata.sf_tx_cnt < SF_TX_DLIST_CNT - SF_MAXTXSEGS; ) {
2269                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2270                 if (m_head == NULL)
2271                         break;
2272                 /*
2273                  * Pack the data into the transmit ring. If we
2274                  * don't have room, set the OACTIVE flag and wait
2275                  * for the NIC to drain the ring.
2276                  */
2277                 if (sf_encap(sc, &m_head)) {
2278                         if (m_head == NULL)
2279                                 break;
2280                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2281                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2282                         break;
2283                 }
2284
2285                 enq++;
2286                 /*
2287                  * If there's a BPF listener, bounce a copy of this frame
2288                  * to him.
2289                  */
2290                 ETHER_BPF_MTAP(ifp, m_head);
2291         }
2292
2293         if (enq > 0) {
2294                 bus_dmamap_sync(sc->sf_cdata.sf_tx_ring_tag,
2295                     sc->sf_cdata.sf_tx_ring_map,
2296                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2297                 /* Kick transmit. */
2298                 csr_write_4(sc, SF_TXDQ_PRODIDX,
2299                     sc->sf_cdata.sf_tx_prod * (sizeof(struct sf_tx_rdesc) / 8));
2300
2301                 /* Set a timeout in case the chip goes out to lunch. */
2302                 sc->sf_watchdog_timer = 5;
2303         }
2304 }
2305
2306 static void
2307 sf_stop(struct sf_softc *sc)
2308 {
2309         struct sf_txdesc        *txd;
2310         struct sf_rxdesc        *rxd;
2311         struct ifnet            *ifp;
2312         int                     i;
2313
2314         SF_LOCK_ASSERT(sc);
2315
2316         ifp = sc->sf_ifp;
2317
2318         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2319         sc->sf_link = 0;
2320         callout_stop(&sc->sf_co);
2321         sc->sf_watchdog_timer = 0;
2322
2323         /* Reading the ISR register clears all interrrupts. */
2324         csr_read_4(sc, SF_ISR);
2325         /* Disable further interrupts. */
2326         csr_write_4(sc, SF_IMR, 0);
2327
2328         /* Disable Tx/Rx egine. */
2329         csr_write_4(sc, SF_GEN_ETH_CTL, 0);
2330
2331         csr_write_4(sc, SF_CQ_CONSIDX, 0);
2332         csr_write_4(sc, SF_CQ_PRODIDX, 0);
2333         csr_write_4(sc, SF_RXDQ_ADDR_Q1, 0);
2334         csr_write_4(sc, SF_RXDQ_CTL_1, 0);
2335         csr_write_4(sc, SF_RXDQ_PTR_Q1, 0);
2336         csr_write_4(sc, SF_TXCQ_CTL, 0);
2337         csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0);
2338         csr_write_4(sc, SF_TXDQ_CTL, 0);
2339
2340         /*
2341          * Free RX and TX mbufs still in the queues.
2342          */
2343         for (i = 0; i < SF_RX_DLIST_CNT; i++) {
2344                 rxd = &sc->sf_cdata.sf_rxdesc[i];
2345                 if (rxd->rx_m != NULL) {
2346                         bus_dmamap_sync(sc->sf_cdata.sf_rx_tag,
2347                             rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2348                         bus_dmamap_unload(sc->sf_cdata.sf_rx_tag,
2349                             rxd->rx_dmamap);
2350                         m_freem(rxd->rx_m);
2351                         rxd->rx_m = NULL;
2352                 }
2353         }
2354         for (i = 0; i < SF_TX_DLIST_CNT; i++) {
2355                 txd = &sc->sf_cdata.sf_txdesc[i];
2356                 if (txd->tx_m != NULL) {
2357                         bus_dmamap_sync(sc->sf_cdata.sf_tx_tag,
2358                             txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2359                         bus_dmamap_unload(sc->sf_cdata.sf_tx_tag,
2360                             txd->tx_dmamap);
2361                         m_freem(txd->tx_m);
2362                         txd->tx_m = NULL;
2363                         txd->ndesc = 0;
2364                 }
2365         }
2366 }
2367
2368 static void
2369 sf_tick(void *xsc)
2370 {
2371         struct sf_softc         *sc;
2372         struct mii_data         *mii;
2373
2374         sc = xsc;
2375         SF_LOCK_ASSERT(sc);
2376         mii = device_get_softc(sc->sf_miibus);
2377         mii_tick(mii);
2378         sf_stats_update(sc);
2379         sf_watchdog(sc);
2380         callout_reset(&sc->sf_co, hz, sf_tick, sc);
2381 }
2382
2383 /*
2384  * Note: it is important that this function not be interrupted. We
2385  * use a two-stage register access scheme: if we are interrupted in
2386  * between setting the indirect address register and reading from the
2387  * indirect data register, the contents of the address register could
2388  * be changed out from under us.
2389  */
2390 static void
2391 sf_stats_update(struct sf_softc *sc)
2392 {
2393         struct ifnet            *ifp;
2394         struct sf_stats         now, *stats, *nstats;
2395         int                     i;
2396
2397         SF_LOCK_ASSERT(sc);
2398
2399         ifp = sc->sf_ifp;
2400         stats = &now;
2401
2402         stats->sf_tx_frames =
2403             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_FRAMES);
2404         stats->sf_tx_single_colls =
2405             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_SINGLE_COL);
2406         stats->sf_tx_multi_colls =
2407             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_MULTI_COL);
2408         stats->sf_tx_crcerrs =
2409             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_CRC_ERRS);
2410         stats->sf_tx_bytes =
2411             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_BYTES);
2412         stats->sf_tx_deferred =
2413             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_DEFERRED);
2414         stats->sf_tx_late_colls =
2415             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_LATE_COL);
2416         stats->sf_tx_pause_frames =
2417             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_PAUSE);
2418         stats->sf_tx_control_frames =
2419             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_CTL_FRAME);
2420         stats->sf_tx_excess_colls =
2421             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_EXCESS_COL);
2422         stats->sf_tx_excess_defer =
2423             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_EXCESS_DEF);
2424         stats->sf_tx_mcast_frames =
2425             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_MULTI);
2426         stats->sf_tx_bcast_frames =
2427             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_BCAST);
2428         stats->sf_tx_frames_lost =
2429             csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_FRAME_LOST);
2430         stats->sf_rx_frames =
2431             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_FRAMES);
2432         stats->sf_rx_crcerrs =
2433             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_CRC_ERRS);
2434         stats->sf_rx_alignerrs =
2435             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_ALIGN_ERRS);
2436         stats->sf_rx_bytes =
2437             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_BYTES);
2438         stats->sf_rx_pause_frames =
2439             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_PAUSE);
2440         stats->sf_rx_control_frames =
2441             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_CTL_FRAME);
2442         stats->sf_rx_unsup_control_frames =
2443             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_UNSUP_FRAME);
2444         stats->sf_rx_giants =
2445             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_GIANTS);
2446         stats->sf_rx_runts =
2447             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_RUNTS);
2448         stats->sf_rx_jabbererrs =
2449             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_JABBER);
2450         stats->sf_rx_fragments =
2451             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_FRAGMENTS);
2452         stats->sf_rx_pkts_64 =
2453             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_64);
2454         stats->sf_rx_pkts_65_127 =
2455             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_65_127);
2456         stats->sf_rx_pkts_128_255 =
2457             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_128_255);
2458         stats->sf_rx_pkts_256_511 =
2459             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_256_511);
2460         stats->sf_rx_pkts_512_1023 =
2461             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_512_1023);
2462         stats->sf_rx_pkts_1024_1518 =
2463             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_1024_1518);
2464         stats->sf_rx_frames_lost =
2465             csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_FRAME_LOST);
2466         /* Lower 16bits are valid. */
2467         stats->sf_tx_underruns =
2468             (csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_UNDERRUN) & 0xffff);
2469
2470         /* Empty stats counter registers. */
2471         for (i = SF_STATS_BASE; i < (SF_STATS_END + 1); i += sizeof(uint32_t))
2472                 csr_write_4(sc, i, 0);
2473
2474         ifp->if_opackets += (u_long)stats->sf_tx_frames;
2475
2476         ifp->if_collisions += (u_long)stats->sf_tx_single_colls +
2477             (u_long)stats->sf_tx_multi_colls;
2478
2479         ifp->if_oerrors += (u_long)stats->sf_tx_excess_colls +
2480             (u_long)stats->sf_tx_excess_defer +
2481             (u_long)stats->sf_tx_frames_lost;
2482
2483         ifp->if_ipackets += (u_long)stats->sf_rx_frames;
2484
2485         ifp->if_ierrors += (u_long)stats->sf_rx_crcerrs +
2486             (u_long)stats->sf_rx_alignerrs +
2487             (u_long)stats->sf_rx_giants +
2488             (u_long)stats->sf_rx_runts +
2489             (u_long)stats->sf_rx_jabbererrs +
2490             (u_long)stats->sf_rx_frames_lost;
2491
2492         nstats = &sc->sf_statistics;
2493
2494         nstats->sf_tx_frames += stats->sf_tx_frames;
2495         nstats->sf_tx_single_colls += stats->sf_tx_single_colls;
2496         nstats->sf_tx_multi_colls += stats->sf_tx_multi_colls;
2497         nstats->sf_tx_crcerrs += stats->sf_tx_crcerrs;
2498         nstats->sf_tx_bytes += stats->sf_tx_bytes;
2499         nstats->sf_tx_deferred += stats->sf_tx_deferred;
2500         nstats->sf_tx_late_colls += stats->sf_tx_late_colls;
2501         nstats->sf_tx_pause_frames += stats->sf_tx_pause_frames;
2502         nstats->sf_tx_control_frames += stats->sf_tx_control_frames;
2503         nstats->sf_tx_excess_colls += stats->sf_tx_excess_colls;
2504         nstats->sf_tx_excess_defer += stats->sf_tx_excess_defer;
2505         nstats->sf_tx_mcast_frames += stats->sf_tx_mcast_frames;
2506         nstats->sf_tx_bcast_frames += stats->sf_tx_bcast_frames;
2507         nstats->sf_tx_frames_lost += stats->sf_tx_frames_lost;
2508         nstats->sf_rx_frames += stats->sf_rx_frames;
2509         nstats->sf_rx_crcerrs += stats->sf_rx_crcerrs;
2510         nstats->sf_rx_alignerrs += stats->sf_rx_alignerrs;
2511         nstats->sf_rx_bytes += stats->sf_rx_bytes;
2512         nstats->sf_rx_pause_frames += stats->sf_rx_pause_frames;
2513         nstats->sf_rx_control_frames += stats->sf_rx_control_frames;
2514         nstats->sf_rx_unsup_control_frames += stats->sf_rx_unsup_control_frames;
2515         nstats->sf_rx_giants += stats->sf_rx_giants;
2516         nstats->sf_rx_runts += stats->sf_rx_runts;
2517         nstats->sf_rx_jabbererrs += stats->sf_rx_jabbererrs;
2518         nstats->sf_rx_fragments += stats->sf_rx_fragments;
2519         nstats->sf_rx_pkts_64 += stats->sf_rx_pkts_64;
2520         nstats->sf_rx_pkts_65_127 += stats->sf_rx_pkts_65_127;
2521         nstats->sf_rx_pkts_128_255 += stats->sf_rx_pkts_128_255;
2522         nstats->sf_rx_pkts_256_511 += stats->sf_rx_pkts_256_511;
2523         nstats->sf_rx_pkts_512_1023 += stats->sf_rx_pkts_512_1023;
2524         nstats->sf_rx_pkts_1024_1518 += stats->sf_rx_pkts_1024_1518;
2525         nstats->sf_rx_frames_lost += stats->sf_rx_frames_lost;
2526         nstats->sf_tx_underruns += stats->sf_tx_underruns;
2527 }
2528
2529 static void
2530 sf_watchdog(struct sf_softc *sc)
2531 {
2532         struct ifnet            *ifp;
2533
2534         SF_LOCK_ASSERT(sc);
2535
2536         if (sc->sf_watchdog_timer == 0 || --sc->sf_watchdog_timer)
2537                 return;
2538
2539         ifp = sc->sf_ifp;
2540
2541         ifp->if_oerrors++;
2542         if (sc->sf_link == 0) {
2543                 if (bootverbose)
2544                         if_printf(sc->sf_ifp, "watchdog timeout "
2545                            "(missed link)\n");
2546         } else
2547                 if_printf(ifp, "watchdog timeout, %d Tx descs are active\n",
2548                     sc->sf_cdata.sf_tx_cnt);
2549
2550         sf_init_locked(sc);
2551
2552         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2553                 sf_start_locked(ifp);
2554 }
2555
2556 static int
2557 sf_shutdown(device_t dev)
2558 {
2559         struct sf_softc         *sc;
2560
2561         sc = device_get_softc(dev);
2562
2563         SF_LOCK(sc);
2564         sf_stop(sc);
2565         SF_UNLOCK(sc);
2566
2567         return (0);
2568 }
2569
2570 static int
2571 sf_suspend(device_t dev)
2572 {
2573         struct sf_softc         *sc;
2574
2575         sc = device_get_softc(dev);
2576
2577         SF_LOCK(sc);
2578         sf_stop(sc);
2579         sc->sf_suspended = 1;
2580         bus_generic_suspend(dev);
2581         SF_UNLOCK(sc);
2582
2583         return (0);
2584 }
2585
2586 static int
2587 sf_resume(device_t dev)
2588 {
2589         struct sf_softc         *sc;
2590         struct ifnet            *ifp;
2591
2592         sc = device_get_softc(dev);
2593
2594         SF_LOCK(sc);
2595         bus_generic_resume(dev);
2596         ifp = sc->sf_ifp;
2597         if ((ifp->if_flags & IFF_UP) != 0)
2598                 sf_init_locked(sc);
2599
2600         sc->sf_suspended = 0;
2601         SF_UNLOCK(sc);
2602
2603         return (0);
2604 }
2605
2606 static int
2607 sf_sysctl_stats(SYSCTL_HANDLER_ARGS)
2608 {
2609         struct sf_softc         *sc;
2610         struct sf_stats         *stats;
2611         int                     error;
2612         int                     result;
2613
2614         result = -1;
2615         error = sysctl_handle_int(oidp, &result, 0, req);
2616
2617         if (error != 0 || req->newptr == NULL)
2618                 return (error);
2619
2620         if (result != 1)
2621                 return (error);
2622
2623         sc = (struct sf_softc *)arg1;
2624         stats = &sc->sf_statistics;
2625
2626         printf("%s statistics:\n", device_get_nameunit(sc->sf_dev));
2627         printf("Transmit good frames : %ju\n",
2628             (uintmax_t)stats->sf_tx_frames);
2629         printf("Transmit good octets : %ju\n",
2630             (uintmax_t)stats->sf_tx_bytes);
2631         printf("Transmit single collisions : %u\n",
2632             stats->sf_tx_single_colls);
2633         printf("Transmit multiple collisions : %u\n",
2634             stats->sf_tx_multi_colls);
2635         printf("Transmit late collisions : %u\n",
2636             stats->sf_tx_late_colls);
2637         printf("Transmit abort due to excessive collisions : %u\n",
2638             stats->sf_tx_excess_colls);
2639         printf("Transmit CRC errors : %u\n",
2640             stats->sf_tx_crcerrs);
2641         printf("Transmit deferrals : %u\n",
2642             stats->sf_tx_deferred);
2643         printf("Transmit abort due to excessive deferrals : %u\n",
2644             stats->sf_tx_excess_defer);
2645         printf("Transmit pause control frames : %u\n",
2646             stats->sf_tx_pause_frames);
2647         printf("Transmit control frames : %u\n",
2648             stats->sf_tx_control_frames);
2649         printf("Transmit good multicast frames : %u\n",
2650             stats->sf_tx_mcast_frames);
2651         printf("Transmit good broadcast frames : %u\n",
2652             stats->sf_tx_bcast_frames);
2653         printf("Transmit frames lost due to internal transmit errors : %u\n",
2654             stats->sf_tx_frames_lost);
2655         printf("Transmit FIFO underflows : %u\n",
2656             stats->sf_tx_underruns);
2657         printf("Transmit GFP stalls : %u\n", stats->sf_tx_gfp_stall);
2658         printf("Receive good frames : %ju\n",
2659             (uint64_t)stats->sf_rx_frames);
2660         printf("Receive good octets : %ju\n",
2661             (uint64_t)stats->sf_rx_bytes);
2662         printf("Receive CRC errors : %u\n",
2663             stats->sf_rx_crcerrs);
2664         printf("Receive alignment errors : %u\n",
2665             stats->sf_rx_alignerrs);
2666         printf("Receive pause frames : %u\n",
2667             stats->sf_rx_pause_frames);
2668         printf("Receive control frames : %u\n",
2669             stats->sf_rx_control_frames);
2670         printf("Receive control frames with unsupported opcode : %u\n",
2671             stats->sf_rx_unsup_control_frames);
2672         printf("Receive frames too long : %u\n",
2673             stats->sf_rx_giants);
2674         printf("Receive frames too short : %u\n",
2675             stats->sf_rx_runts);
2676         printf("Receive frames jabber errors : %u\n",
2677             stats->sf_rx_jabbererrs);
2678         printf("Receive frames fragments : %u\n",
2679             stats->sf_rx_fragments);
2680         printf("Receive packets 64 bytes : %ju\n",
2681             (uint64_t)stats->sf_rx_pkts_64);
2682         printf("Receive packets 65 to 127 bytes : %ju\n",
2683             (uint64_t)stats->sf_rx_pkts_65_127);
2684         printf("Receive packets 128 to 255 bytes : %ju\n",
2685             (uint64_t)stats->sf_rx_pkts_128_255);
2686         printf("Receive packets 256 to 511 bytes : %ju\n",
2687             (uint64_t)stats->sf_rx_pkts_256_511);
2688         printf("Receive packets 512 to 1023 bytes : %ju\n",
2689             (uint64_t)stats->sf_rx_pkts_512_1023);
2690         printf("Receive packets 1024 to 1518 bytes : %ju\n",
2691             (uint64_t)stats->sf_rx_pkts_1024_1518);
2692         printf("Receive frames lost due to internal receive errors : %u\n",
2693             stats->sf_rx_frames_lost);
2694         printf("Receive GFP stalls : %u\n", stats->sf_rx_gfp_stall);
2695
2696         return (error);
2697 }
2698
2699 static int
2700 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2701 {
2702         int error, value;
2703
2704         if (!arg1)
2705                 return (EINVAL);
2706         value = *(int *)arg1;
2707         error = sysctl_handle_int(oidp, &value, 0, req);
2708         if (error || !req->newptr)
2709                 return (error);
2710         if (value < low || value > high)
2711                 return (EINVAL);
2712         *(int *)arg1 = value;
2713
2714         return (0);
2715 }
2716
2717 static int
2718 sysctl_hw_sf_int_mod(SYSCTL_HANDLER_ARGS)
2719 {
2720
2721         return (sysctl_int_range(oidp, arg1, arg2, req, SF_IM_MIN, SF_IM_MAX));
2722 }