xref: /freebsd-12.1/sys/dev/sf/if_sf.c (revision dfd2f2d4)
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <[email protected]>.  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 <[email protected]>
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 	error = mii_attach(dev, &sc->sf_miibus, ifp, sf_ifmedia_upd,
870 	    sf_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
871 	if (error != 0) {
872 		device_printf(dev, "attaching PHYs failed\n");
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 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1830 				sf_init_locked(sc);
1831 				SF_UNLOCK(sc);
1832 				return (rx_npkts);
1833 			} else if ((status & SF_ISR_NO_TX_CSUM) != 0) {
1834 				sc->sf_statistics.sf_tx_gfp_stall++;
1835 #ifdef	SF_GFP_DEBUG
1836 				device_printf(sc->sf_dev,
1837 				    "TxGFP is not responding!\n");
1838 #endif
1839 			} else if ((status & SF_ISR_RXGFP_NORESP) != 0) {
1840 				sc->sf_statistics.sf_rx_gfp_stall++;
1841 #ifdef	SF_GFP_DEBUG
1842 				device_printf(sc->sf_dev,
1843 				    "RxGFP is not responding!\n");
1844 #endif
1845 			}
1846 		}
1847 	}
1848 
1849 	SF_UNLOCK(sc);
1850 	return (rx_npkts);
1851 }
1852 #endif /* DEVICE_POLLING */
1853 
1854 static void
1855 sf_intr(void *arg)
1856 {
1857 	struct sf_softc		*sc;
1858 	struct ifnet		*ifp;
1859 	uint32_t		status;
1860 
1861 	sc = (struct sf_softc *)arg;
1862 	SF_LOCK(sc);
1863 
1864 	if (sc->sf_suspended != 0)
1865 		goto done_locked;
1866 
1867 	/* Reading the ISR register clears all interrrupts. */
1868 	status = csr_read_4(sc, SF_ISR);
1869 	if (status == 0 || status == 0xffffffff ||
1870 	    (status & SF_ISR_PCIINT_ASSERTED) == 0)
1871 		goto done_locked;
1872 
1873 	ifp = sc->sf_ifp;
1874 #ifdef DEVICE_POLLING
1875 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1876 		goto done_locked;
1877 #endif
1878 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1879 		goto done_locked;
1880 
1881 	/* Disable interrupts. */
1882 	csr_write_4(sc, SF_IMR, 0x00000000);
1883 
1884 	for (; (status & SF_INTRS) != 0;) {
1885 		if ((status & SF_ISR_RXDQ1_DMADONE) != 0)
1886 			sf_rxeof(sc);
1887 
1888 		if ((status & (SF_ISR_TX_TXDONE | SF_ISR_TX_DMADONE |
1889 		    SF_ISR_TX_QUEUEDONE)) != 0)
1890 			sf_txeof(sc);
1891 
1892 		if ((status & SF_ISR_ABNORMALINTR) != 0) {
1893 			if ((status & SF_ISR_STATSOFLOW) != 0)
1894 				sf_stats_update(sc);
1895 			else if ((status & SF_ISR_TX_LOFIFO) != 0)
1896 				sf_txthresh_adjust(sc);
1897 			else if ((status & SF_ISR_DMAERR) != 0) {
1898 				device_printf(sc->sf_dev,
1899 				    "DMA error, resetting\n");
1900 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1901 				sf_init_locked(sc);
1902 				SF_UNLOCK(sc);
1903 				return;
1904 			} else if ((status & SF_ISR_NO_TX_CSUM) != 0) {
1905 				sc->sf_statistics.sf_tx_gfp_stall++;
1906 #ifdef	SF_GFP_DEBUG
1907 				device_printf(sc->sf_dev,
1908 				    "TxGFP is not responding!\n");
1909 #endif
1910 			}
1911 			else if ((status & SF_ISR_RXGFP_NORESP) != 0) {
1912 				sc->sf_statistics.sf_rx_gfp_stall++;
1913 #ifdef	SF_GFP_DEBUG
1914 				device_printf(sc->sf_dev,
1915 				    "RxGFP is not responding!\n");
1916 #endif
1917 			}
1918 		}
1919 		/* Reading the ISR register clears all interrrupts. */
1920 		status = csr_read_4(sc, SF_ISR);
1921 	}
1922 
1923 	/* Re-enable interrupts. */
1924 	csr_write_4(sc, SF_IMR, SF_INTRS);
1925 
1926 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1927 		sf_start_locked(ifp);
1928 done_locked:
1929 	SF_UNLOCK(sc);
1930 }
1931 
1932 static void
1933 sf_download_fw(struct sf_softc *sc)
1934 {
1935 	uint32_t gfpinst;
1936 	int i, ndx;
1937 	uint8_t *p;
1938 
1939 	/*
1940 	 * A FP instruction is composed of 48bits so we have to
1941 	 * write it with two parts.
1942 	 */
1943 	p = txfwdata;
1944 	ndx = 0;
1945 	for (i = 0; i < sizeof(txfwdata) / SF_GFP_INST_BYTES; i++) {
1946 		gfpinst = p[2] << 24 | p[3] << 16 | p[4] << 8 | p[5];
1947 		csr_write_4(sc, SF_TXGFP_MEM_BASE + ndx * 4, gfpinst);
1948 		gfpinst = p[0] << 8 | p[1];
1949 		csr_write_4(sc, SF_TXGFP_MEM_BASE + (ndx + 1) * 4, gfpinst);
1950 		p += SF_GFP_INST_BYTES;
1951 		ndx += 2;
1952 	}
1953 	if (bootverbose)
1954 		device_printf(sc->sf_dev, "%d Tx instructions downloaded\n", i);
1955 
1956 	p = rxfwdata;
1957 	ndx = 0;
1958 	for (i = 0; i < sizeof(rxfwdata) / SF_GFP_INST_BYTES; i++) {
1959 		gfpinst = p[2] << 24 | p[3] << 16 | p[4] << 8 | p[5];
1960 		csr_write_4(sc, SF_RXGFP_MEM_BASE + (ndx * 4), gfpinst);
1961 		gfpinst = p[0] << 8 | p[1];
1962 		csr_write_4(sc, SF_RXGFP_MEM_BASE + (ndx + 1) * 4, gfpinst);
1963 		p += SF_GFP_INST_BYTES;
1964 		ndx += 2;
1965 	}
1966 	if (bootverbose)
1967 		device_printf(sc->sf_dev, "%d Rx instructions downloaded\n", i);
1968 }
1969 
1970 static void
1971 sf_init(void *xsc)
1972 {
1973 	struct sf_softc		*sc;
1974 
1975 	sc = (struct sf_softc *)xsc;
1976 	SF_LOCK(sc);
1977 	sf_init_locked(sc);
1978 	SF_UNLOCK(sc);
1979 }
1980 
1981 static void
1982 sf_init_locked(struct sf_softc *sc)
1983 {
1984 	struct ifnet		*ifp;
1985 	struct mii_data		*mii;
1986 	uint8_t			eaddr[ETHER_ADDR_LEN];
1987 	bus_addr_t		addr;
1988 	int			i;
1989 
1990 	SF_LOCK_ASSERT(sc);
1991 	ifp = sc->sf_ifp;
1992 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1993 		return;
1994 	mii = device_get_softc(sc->sf_miibus);
1995 
1996 	sf_stop(sc);
1997 	/* Reset the hardware to a known state. */
1998 	sf_reset(sc);
1999 
2000 	/* Init all the receive filter registers */
2001 	for (i = SF_RXFILT_PERFECT_BASE;
2002 	    i < (SF_RXFILT_HASH_MAX + 1); i += sizeof(uint32_t))
2003 		csr_write_4(sc, i, 0);
2004 
2005 	/* Empty stats counter registers. */
2006 	for (i = SF_STATS_BASE; i < (SF_STATS_END + 1); i += sizeof(uint32_t))
2007 		csr_write_4(sc, i, 0);
2008 
2009 	/* Init our MAC address. */
2010 	bcopy(IF_LLADDR(sc->sf_ifp), eaddr, sizeof(eaddr));
2011 	csr_write_4(sc, SF_PAR0,
2012 	    eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
2013 	csr_write_4(sc, SF_PAR1, eaddr[0] << 8 | eaddr[1]);
2014 	sf_setperf(sc, 0, eaddr);
2015 
2016 	if (sf_init_rx_ring(sc) == ENOBUFS) {
2017 		device_printf(sc->sf_dev,
2018 		    "initialization failed: no memory for rx buffers\n");
2019 		return;
2020 	}
2021 
2022 	sf_init_tx_ring(sc);
2023 
2024 	/*
2025 	 * 16 perfect address filtering.
2026 	 * Hash only multicast destination address, Accept matching
2027 	 * frames regardless of VLAN ID.
2028 	 */
2029 	csr_write_4(sc, SF_RXFILT, SF_PERFMODE_NORMAL | SF_HASHMODE_ANYVLAN);
2030 
2031 	/*
2032 	 * Set Rx filter.
2033 	 */
2034 	sf_rxfilter(sc);
2035 
2036 	/* Init the completion queue indexes. */
2037 	csr_write_4(sc, SF_CQ_CONSIDX, 0);
2038 	csr_write_4(sc, SF_CQ_PRODIDX, 0);
2039 
2040 	/* Init the RX completion queue. */
2041 	addr = sc->sf_rdata.sf_rx_cring_paddr;
2042 	csr_write_4(sc, SF_CQ_ADDR_HI, SF_ADDR_HI(addr));
2043 	csr_write_4(sc, SF_RXCQ_CTL_1, SF_ADDR_LO(addr) & SF_RXCQ_ADDR);
2044 	if (SF_ADDR_HI(addr) != 0)
2045 		SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQ_USE_64BIT);
2046 	/* Set RX completion queue type 2. */
2047 	SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQTYPE_2);
2048 	csr_write_4(sc, SF_RXCQ_CTL_2, 0);
2049 
2050 	/*
2051 	 * Init RX DMA control.
2052 	 * default RxHighPriority Threshold,
2053 	 * default RxBurstSize, 128bytes.
2054 	 */
2055 	SF_SETBIT(sc, SF_RXDMA_CTL,
2056 	    SF_RXDMA_REPORTBADPKTS |
2057 	    (SF_RXDMA_HIGHPRIO_THRESH << 8) |
2058 	    SF_RXDMA_BURST);
2059 
2060 	/* Init the RX buffer descriptor queue. */
2061 	addr = sc->sf_rdata.sf_rx_ring_paddr;
2062 	csr_write_4(sc, SF_RXDQ_ADDR_HI, SF_ADDR_HI(addr));
2063 	csr_write_4(sc, SF_RXDQ_ADDR_Q1, SF_ADDR_LO(addr));
2064 
2065 	/* Set RX queue buffer length. */
2066 	csr_write_4(sc, SF_RXDQ_CTL_1,
2067 	    ((MCLBYTES  - sizeof(uint32_t)) << 16) |
2068 	    SF_RXDQCTL_64BITBADDR | SF_RXDQCTL_VARIABLE);
2069 
2070 	if (SF_ADDR_HI(addr) != 0)
2071 		SF_SETBIT(sc, SF_RXDQ_CTL_1, SF_RXDQCTL_64BITDADDR);
2072 	csr_write_4(sc, SF_RXDQ_PTR_Q1, SF_RX_DLIST_CNT - 1);
2073 	csr_write_4(sc, SF_RXDQ_CTL_2, 0);
2074 
2075 	/* Init the TX completion queue */
2076 	addr = sc->sf_rdata.sf_tx_cring_paddr;
2077 	csr_write_4(sc, SF_TXCQ_CTL, SF_ADDR_LO(addr) & SF_TXCQ_ADDR);
2078 	if (SF_ADDR_HI(addr) != 0)
2079 		SF_SETBIT(sc, SF_TXCQ_CTL, SF_TXCQ_USE_64BIT);
2080 
2081 	/* Init the TX buffer descriptor queue. */
2082 	addr = sc->sf_rdata.sf_tx_ring_paddr;
2083 	csr_write_4(sc, SF_TXDQ_ADDR_HI, SF_ADDR_HI(addr));
2084 	csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0);
2085 	csr_write_4(sc, SF_TXDQ_ADDR_LOPRIO, SF_ADDR_LO(addr));
2086 	csr_write_4(sc, SF_TX_FRAMCTL,
2087 	    SF_TXFRMCTL_CPLAFTERTX | sc->sf_txthresh);
2088 	csr_write_4(sc, SF_TXDQ_CTL,
2089 	    SF_TXDMA_HIPRIO_THRESH << 24 |
2090 	    SF_TXSKIPLEN_0BYTES << 16 |
2091 	    SF_TXDDMA_BURST << 8 |
2092 	    SF_TXBUFDESC_TYPE2 | SF_TXMINSPACE_UNLIMIT);
2093 	if (SF_ADDR_HI(addr) != 0)
2094 		SF_SETBIT(sc, SF_TXDQ_CTL, SF_TXDQCTL_64BITADDR);
2095 
2096 	/* Set VLAN Type register. */
2097 	csr_write_4(sc, SF_VLANTYPE, ETHERTYPE_VLAN);
2098 
2099 	/* Set TxPause Timer. */
2100 	csr_write_4(sc, SF_TXPAUSETIMER, 0xffff);
2101 
2102 	/* Enable autopadding of short TX frames. */
2103 	SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_AUTOPAD);
2104 	SF_SETBIT(sc, SF_MACCFG_2, SF_MACCFG2_AUTOVLANPAD);
2105 	/* Make sure to reset MAC to take changes effect. */
2106 	SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
2107 	DELAY(1000);
2108 	SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
2109 
2110 	/* Enable PCI bus master. */
2111 	SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_PCIMEN);
2112 
2113 	/* Load StarFire firmware. */
2114 	sf_download_fw(sc);
2115 
2116 	/* Intialize interrupt moderation. */
2117 	csr_write_4(sc, SF_TIMER_CTL, SF_TIMER_IMASK_MODE | SF_TIMER_TIMES_TEN |
2118 	    (sc->sf_int_mod & SF_TIMER_IMASK_INTERVAL));
2119 
2120 #ifdef DEVICE_POLLING
2121 	/* Disable interrupts if we are polling. */
2122 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2123 		csr_write_4(sc, SF_IMR, 0x00000000);
2124 	else
2125 #endif
2126 	/* Enable interrupts. */
2127 	csr_write_4(sc, SF_IMR, SF_INTRS);
2128 	SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_INTR_ENB);
2129 
2130 	/* Enable the RX and TX engines. */
2131 	csr_write_4(sc, SF_GEN_ETH_CTL,
2132 	    SF_ETHCTL_RX_ENB | SF_ETHCTL_RXDMA_ENB |
2133 	    SF_ETHCTL_TX_ENB | SF_ETHCTL_TXDMA_ENB);
2134 
2135 	if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
2136 		SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TXGFP_ENB);
2137 	else
2138 		SF_CLRBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TXGFP_ENB);
2139 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
2140 		SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RXGFP_ENB);
2141 	else
2142 		SF_CLRBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RXGFP_ENB);
2143 
2144 	sc->sf_link = 0;
2145 	mii_mediachg(mii);
2146 
2147 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2148 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2149 
2150 	callout_reset(&sc->sf_co, hz, sf_tick, sc);
2151 }
2152 
2153 static int
2154 sf_encap(struct sf_softc *sc, struct mbuf **m_head)
2155 {
2156 	struct sf_txdesc	*txd;
2157 	struct sf_tx_rdesc	*desc;
2158 	struct mbuf		*m;
2159 	bus_dmamap_t		map;
2160 	bus_dma_segment_t	txsegs[SF_MAXTXSEGS];
2161 	int			error, i, nsegs, prod, si;
2162 	int			avail, nskip;
2163 
2164 	SF_LOCK_ASSERT(sc);
2165 
2166 	m = *m_head;
2167 	prod = sc->sf_cdata.sf_tx_prod;
2168 	txd = &sc->sf_cdata.sf_txdesc[prod];
2169 	map = txd->tx_dmamap;
2170 	error = bus_dmamap_load_mbuf_sg(sc->sf_cdata.sf_tx_tag, map,
2171 	    *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
2172 	if (error == EFBIG) {
2173 		m = m_collapse(*m_head, M_DONTWAIT, SF_MAXTXSEGS);
2174 		if (m == NULL) {
2175 			m_freem(*m_head);
2176 			*m_head = NULL;
2177 			return (ENOBUFS);
2178 		}
2179 		*m_head = m;
2180 		error = bus_dmamap_load_mbuf_sg(sc->sf_cdata.sf_tx_tag,
2181 		    map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
2182 		if (error != 0) {
2183 			m_freem(*m_head);
2184 			*m_head = NULL;
2185 			return (error);
2186 		}
2187 	} else if (error != 0)
2188 		return (error);
2189 	if (nsegs == 0) {
2190 		m_freem(*m_head);
2191 		*m_head = NULL;
2192 		return (EIO);
2193 	}
2194 
2195 	/* Check number of available descriptors. */
2196 	avail = (SF_TX_DLIST_CNT - 1) - sc->sf_cdata.sf_tx_cnt;
2197 	if (avail < nsegs) {
2198 		bus_dmamap_unload(sc->sf_cdata.sf_tx_tag, map);
2199 		return (ENOBUFS);
2200 	}
2201 	nskip = 0;
2202 	if (prod + nsegs >= SF_TX_DLIST_CNT) {
2203 		nskip = SF_TX_DLIST_CNT - prod - 1;
2204 		if (avail < nsegs + nskip) {
2205 			bus_dmamap_unload(sc->sf_cdata.sf_tx_tag, map);
2206 			return (ENOBUFS);
2207 		}
2208 	}
2209 
2210 	bus_dmamap_sync(sc->sf_cdata.sf_tx_tag, map, BUS_DMASYNC_PREWRITE);
2211 
2212 	si = prod;
2213 	for (i = 0; i < nsegs; i++) {
2214 		desc = &sc->sf_rdata.sf_tx_ring[prod];
2215 		desc->sf_tx_ctrl = htole32(SF_TX_DESC_ID |
2216 		    (txsegs[i].ds_len & SF_TX_DESC_FRAGLEN));
2217 		desc->sf_tx_reserved = 0;
2218 		desc->sf_addr = htole64(txsegs[i].ds_addr);
2219 		if (i == 0 && prod + nsegs >= SF_TX_DLIST_CNT) {
2220 			/* Queue wraps! */
2221 			desc->sf_tx_ctrl |= htole32(SF_TX_DESC_END);
2222 			prod = 0;
2223 		} else
2224 			SF_INC(prod, SF_TX_DLIST_CNT);
2225 	}
2226 	/* Update producer index. */
2227 	sc->sf_cdata.sf_tx_prod = prod;
2228 	sc->sf_cdata.sf_tx_cnt += nsegs + nskip;
2229 
2230 	desc = &sc->sf_rdata.sf_tx_ring[si];
2231 	/* Check TDP/UDP checksum offload request. */
2232 	if ((m->m_pkthdr.csum_flags & SF_CSUM_FEATURES) != 0)
2233 		desc->sf_tx_ctrl |= htole32(SF_TX_DESC_CALTCP);
2234 	desc->sf_tx_ctrl |=
2235 	    htole32(SF_TX_DESC_CRCEN | SF_TX_DESC_INTR | (nsegs << 16));
2236 
2237 	txd->tx_dmamap = map;
2238 	txd->tx_m = m;
2239 	txd->ndesc = nsegs + nskip;
2240 
2241 	return (0);
2242 }
2243 
2244 static void
2245 sf_start(struct ifnet *ifp)
2246 {
2247 	struct sf_softc		*sc;
2248 
2249 	sc = ifp->if_softc;
2250 	SF_LOCK(sc);
2251 	sf_start_locked(ifp);
2252 	SF_UNLOCK(sc);
2253 }
2254 
2255 static void
2256 sf_start_locked(struct ifnet *ifp)
2257 {
2258 	struct sf_softc		*sc;
2259 	struct mbuf		*m_head;
2260 	int			enq;
2261 
2262 	sc = ifp->if_softc;
2263 	SF_LOCK_ASSERT(sc);
2264 
2265 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2266 	    IFF_DRV_RUNNING || sc->sf_link == 0)
2267 		return;
2268 
2269 	/*
2270 	 * Since we don't know when descriptor wrap occurrs in advance
2271 	 * limit available number of active Tx descriptor counter to be
2272 	 * higher than maximum number of DMA segments allowed in driver.
2273 	 */
2274 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2275 	    sc->sf_cdata.sf_tx_cnt < SF_TX_DLIST_CNT - SF_MAXTXSEGS; ) {
2276 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2277 		if (m_head == NULL)
2278 			break;
2279 		/*
2280 		 * Pack the data into the transmit ring. If we
2281 		 * don't have room, set the OACTIVE flag and wait
2282 		 * for the NIC to drain the ring.
2283 		 */
2284 		if (sf_encap(sc, &m_head)) {
2285 			if (m_head == NULL)
2286 				break;
2287 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2288 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2289 			break;
2290 		}
2291 
2292 		enq++;
2293 		/*
2294 		 * If there's a BPF listener, bounce a copy of this frame
2295 		 * to him.
2296 		 */
2297 		ETHER_BPF_MTAP(ifp, m_head);
2298 	}
2299 
2300 	if (enq > 0) {
2301 		bus_dmamap_sync(sc->sf_cdata.sf_tx_ring_tag,
2302 		    sc->sf_cdata.sf_tx_ring_map,
2303 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2304 		/* Kick transmit. */
2305 		csr_write_4(sc, SF_TXDQ_PRODIDX,
2306 		    sc->sf_cdata.sf_tx_prod * (sizeof(struct sf_tx_rdesc) / 8));
2307 
2308 		/* Set a timeout in case the chip goes out to lunch. */
2309 		sc->sf_watchdog_timer = 5;
2310 	}
2311 }
2312 
2313 static void
2314 sf_stop(struct sf_softc *sc)
2315 {
2316 	struct sf_txdesc	*txd;
2317 	struct sf_rxdesc	*rxd;
2318 	struct ifnet		*ifp;
2319 	int			i;
2320 
2321 	SF_LOCK_ASSERT(sc);
2322 
2323 	ifp = sc->sf_ifp;
2324 
2325 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2326 	sc->sf_link = 0;
2327 	callout_stop(&sc->sf_co);
2328 	sc->sf_watchdog_timer = 0;
2329 
2330 	/* Reading the ISR register clears all interrrupts. */
2331 	csr_read_4(sc, SF_ISR);
2332 	/* Disable further interrupts. */
2333 	csr_write_4(sc, SF_IMR, 0);
2334 
2335 	/* Disable Tx/Rx egine. */
2336 	csr_write_4(sc, SF_GEN_ETH_CTL, 0);
2337 
2338 	csr_write_4(sc, SF_CQ_CONSIDX, 0);
2339 	csr_write_4(sc, SF_CQ_PRODIDX, 0);
2340 	csr_write_4(sc, SF_RXDQ_ADDR_Q1, 0);
2341 	csr_write_4(sc, SF_RXDQ_CTL_1, 0);
2342 	csr_write_4(sc, SF_RXDQ_PTR_Q1, 0);
2343 	csr_write_4(sc, SF_TXCQ_CTL, 0);
2344 	csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0);
2345 	csr_write_4(sc, SF_TXDQ_CTL, 0);
2346 
2347 	/*
2348 	 * Free RX and TX mbufs still in the queues.
2349 	 */
2350 	for (i = 0; i < SF_RX_DLIST_CNT; i++) {
2351 		rxd = &sc->sf_cdata.sf_rxdesc[i];
2352 		if (rxd->rx_m != NULL) {
2353 			bus_dmamap_sync(sc->sf_cdata.sf_rx_tag,
2354 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2355 			bus_dmamap_unload(sc->sf_cdata.sf_rx_tag,
2356 			    rxd->rx_dmamap);
2357 			m_freem(rxd->rx_m);
2358 			rxd->rx_m = NULL;
2359 		}
2360         }
2361 	for (i = 0; i < SF_TX_DLIST_CNT; i++) {
2362 		txd = &sc->sf_cdata.sf_txdesc[i];
2363 		if (txd->tx_m != NULL) {
2364 			bus_dmamap_sync(sc->sf_cdata.sf_tx_tag,
2365 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2366 			bus_dmamap_unload(sc->sf_cdata.sf_tx_tag,
2367 			    txd->tx_dmamap);
2368 			m_freem(txd->tx_m);
2369 			txd->tx_m = NULL;
2370 			txd->ndesc = 0;
2371 		}
2372         }
2373 }
2374 
2375 static void
2376 sf_tick(void *xsc)
2377 {
2378 	struct sf_softc		*sc;
2379 	struct mii_data		*mii;
2380 
2381 	sc = xsc;
2382 	SF_LOCK_ASSERT(sc);
2383 	mii = device_get_softc(sc->sf_miibus);
2384 	mii_tick(mii);
2385 	sf_stats_update(sc);
2386 	sf_watchdog(sc);
2387 	callout_reset(&sc->sf_co, hz, sf_tick, sc);
2388 }
2389 
2390 /*
2391  * Note: it is important that this function not be interrupted. We
2392  * use a two-stage register access scheme: if we are interrupted in
2393  * between setting the indirect address register and reading from the
2394  * indirect data register, the contents of the address register could
2395  * be changed out from under us.
2396  */
2397 static void
2398 sf_stats_update(struct sf_softc *sc)
2399 {
2400 	struct ifnet		*ifp;
2401 	struct sf_stats		now, *stats, *nstats;
2402 	int			i;
2403 
2404 	SF_LOCK_ASSERT(sc);
2405 
2406 	ifp = sc->sf_ifp;
2407 	stats = &now;
2408 
2409 	stats->sf_tx_frames =
2410 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_FRAMES);
2411 	stats->sf_tx_single_colls =
2412 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_SINGLE_COL);
2413 	stats->sf_tx_multi_colls =
2414 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_MULTI_COL);
2415 	stats->sf_tx_crcerrs =
2416 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_CRC_ERRS);
2417 	stats->sf_tx_bytes =
2418 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_BYTES);
2419 	stats->sf_tx_deferred =
2420 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_DEFERRED);
2421 	stats->sf_tx_late_colls =
2422 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_LATE_COL);
2423 	stats->sf_tx_pause_frames =
2424 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_PAUSE);
2425 	stats->sf_tx_control_frames =
2426 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_CTL_FRAME);
2427 	stats->sf_tx_excess_colls =
2428 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_EXCESS_COL);
2429 	stats->sf_tx_excess_defer =
2430 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_EXCESS_DEF);
2431 	stats->sf_tx_mcast_frames =
2432 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_MULTI);
2433 	stats->sf_tx_bcast_frames =
2434 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_BCAST);
2435 	stats->sf_tx_frames_lost =
2436 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_FRAME_LOST);
2437 	stats->sf_rx_frames =
2438 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_FRAMES);
2439 	stats->sf_rx_crcerrs =
2440 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_CRC_ERRS);
2441 	stats->sf_rx_alignerrs =
2442 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_ALIGN_ERRS);
2443 	stats->sf_rx_bytes =
2444 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_BYTES);
2445 	stats->sf_rx_pause_frames =
2446 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_PAUSE);
2447 	stats->sf_rx_control_frames =
2448 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_CTL_FRAME);
2449 	stats->sf_rx_unsup_control_frames =
2450 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_UNSUP_FRAME);
2451 	stats->sf_rx_giants =
2452 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_GIANTS);
2453 	stats->sf_rx_runts =
2454 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_RUNTS);
2455 	stats->sf_rx_jabbererrs =
2456 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_JABBER);
2457 	stats->sf_rx_fragments =
2458 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_FRAGMENTS);
2459 	stats->sf_rx_pkts_64 =
2460 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_64);
2461 	stats->sf_rx_pkts_65_127 =
2462 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_65_127);
2463 	stats->sf_rx_pkts_128_255 =
2464 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_128_255);
2465 	stats->sf_rx_pkts_256_511 =
2466 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_256_511);
2467 	stats->sf_rx_pkts_512_1023 =
2468 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_512_1023);
2469 	stats->sf_rx_pkts_1024_1518 =
2470 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_1024_1518);
2471 	stats->sf_rx_frames_lost =
2472 	    csr_read_4(sc, SF_STATS_BASE + SF_STATS_RX_FRAME_LOST);
2473 	/* Lower 16bits are valid. */
2474 	stats->sf_tx_underruns =
2475 	    (csr_read_4(sc, SF_STATS_BASE + SF_STATS_TX_UNDERRUN) & 0xffff);
2476 
2477 	/* Empty stats counter registers. */
2478 	for (i = SF_STATS_BASE; i < (SF_STATS_END + 1); i += sizeof(uint32_t))
2479 		csr_write_4(sc, i, 0);
2480 
2481 	ifp->if_opackets += (u_long)stats->sf_tx_frames;
2482 
2483 	ifp->if_collisions += (u_long)stats->sf_tx_single_colls +
2484 	    (u_long)stats->sf_tx_multi_colls;
2485 
2486 	ifp->if_oerrors += (u_long)stats->sf_tx_excess_colls +
2487 	    (u_long)stats->sf_tx_excess_defer +
2488 	    (u_long)stats->sf_tx_frames_lost;
2489 
2490 	ifp->if_ipackets += (u_long)stats->sf_rx_frames;
2491 
2492 	ifp->if_ierrors += (u_long)stats->sf_rx_crcerrs +
2493 	    (u_long)stats->sf_rx_alignerrs +
2494 	    (u_long)stats->sf_rx_giants +
2495 	    (u_long)stats->sf_rx_runts +
2496 	    (u_long)stats->sf_rx_jabbererrs +
2497 	    (u_long)stats->sf_rx_frames_lost;
2498 
2499 	nstats = &sc->sf_statistics;
2500 
2501 	nstats->sf_tx_frames += stats->sf_tx_frames;
2502 	nstats->sf_tx_single_colls += stats->sf_tx_single_colls;
2503 	nstats->sf_tx_multi_colls += stats->sf_tx_multi_colls;
2504 	nstats->sf_tx_crcerrs += stats->sf_tx_crcerrs;
2505 	nstats->sf_tx_bytes += stats->sf_tx_bytes;
2506 	nstats->sf_tx_deferred += stats->sf_tx_deferred;
2507 	nstats->sf_tx_late_colls += stats->sf_tx_late_colls;
2508 	nstats->sf_tx_pause_frames += stats->sf_tx_pause_frames;
2509 	nstats->sf_tx_control_frames += stats->sf_tx_control_frames;
2510 	nstats->sf_tx_excess_colls += stats->sf_tx_excess_colls;
2511 	nstats->sf_tx_excess_defer += stats->sf_tx_excess_defer;
2512 	nstats->sf_tx_mcast_frames += stats->sf_tx_mcast_frames;
2513 	nstats->sf_tx_bcast_frames += stats->sf_tx_bcast_frames;
2514 	nstats->sf_tx_frames_lost += stats->sf_tx_frames_lost;
2515 	nstats->sf_rx_frames += stats->sf_rx_frames;
2516 	nstats->sf_rx_crcerrs += stats->sf_rx_crcerrs;
2517 	nstats->sf_rx_alignerrs += stats->sf_rx_alignerrs;
2518 	nstats->sf_rx_bytes += stats->sf_rx_bytes;
2519 	nstats->sf_rx_pause_frames += stats->sf_rx_pause_frames;
2520 	nstats->sf_rx_control_frames += stats->sf_rx_control_frames;
2521 	nstats->sf_rx_unsup_control_frames += stats->sf_rx_unsup_control_frames;
2522 	nstats->sf_rx_giants += stats->sf_rx_giants;
2523 	nstats->sf_rx_runts += stats->sf_rx_runts;
2524 	nstats->sf_rx_jabbererrs += stats->sf_rx_jabbererrs;
2525 	nstats->sf_rx_fragments += stats->sf_rx_fragments;
2526 	nstats->sf_rx_pkts_64 += stats->sf_rx_pkts_64;
2527 	nstats->sf_rx_pkts_65_127 += stats->sf_rx_pkts_65_127;
2528 	nstats->sf_rx_pkts_128_255 += stats->sf_rx_pkts_128_255;
2529 	nstats->sf_rx_pkts_256_511 += stats->sf_rx_pkts_256_511;
2530 	nstats->sf_rx_pkts_512_1023 += stats->sf_rx_pkts_512_1023;
2531 	nstats->sf_rx_pkts_1024_1518 += stats->sf_rx_pkts_1024_1518;
2532 	nstats->sf_rx_frames_lost += stats->sf_rx_frames_lost;
2533 	nstats->sf_tx_underruns += stats->sf_tx_underruns;
2534 }
2535 
2536 static void
2537 sf_watchdog(struct sf_softc *sc)
2538 {
2539 	struct ifnet		*ifp;
2540 
2541 	SF_LOCK_ASSERT(sc);
2542 
2543 	if (sc->sf_watchdog_timer == 0 || --sc->sf_watchdog_timer)
2544 		return;
2545 
2546 	ifp = sc->sf_ifp;
2547 
2548 	ifp->if_oerrors++;
2549 	if (sc->sf_link == 0) {
2550 		if (bootverbose)
2551 			if_printf(sc->sf_ifp, "watchdog timeout "
2552 			   "(missed link)\n");
2553 	} else
2554 		if_printf(ifp, "watchdog timeout, %d Tx descs are active\n",
2555 		    sc->sf_cdata.sf_tx_cnt);
2556 
2557 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2558 	sf_init_locked(sc);
2559 
2560 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2561 		sf_start_locked(ifp);
2562 }
2563 
2564 static int
2565 sf_shutdown(device_t dev)
2566 {
2567 	struct sf_softc		*sc;
2568 
2569 	sc = device_get_softc(dev);
2570 
2571 	SF_LOCK(sc);
2572 	sf_stop(sc);
2573 	SF_UNLOCK(sc);
2574 
2575 	return (0);
2576 }
2577 
2578 static int
2579 sf_suspend(device_t dev)
2580 {
2581 	struct sf_softc		*sc;
2582 
2583 	sc = device_get_softc(dev);
2584 
2585 	SF_LOCK(sc);
2586 	sf_stop(sc);
2587 	sc->sf_suspended = 1;
2588 	bus_generic_suspend(dev);
2589 	SF_UNLOCK(sc);
2590 
2591 	return (0);
2592 }
2593 
2594 static int
2595 sf_resume(device_t dev)
2596 {
2597 	struct sf_softc		*sc;
2598 	struct ifnet		*ifp;
2599 
2600 	sc = device_get_softc(dev);
2601 
2602 	SF_LOCK(sc);
2603 	bus_generic_resume(dev);
2604 	ifp = sc->sf_ifp;
2605 	if ((ifp->if_flags & IFF_UP) != 0)
2606 		sf_init_locked(sc);
2607 
2608 	sc->sf_suspended = 0;
2609 	SF_UNLOCK(sc);
2610 
2611 	return (0);
2612 }
2613 
2614 static int
2615 sf_sysctl_stats(SYSCTL_HANDLER_ARGS)
2616 {
2617 	struct sf_softc		*sc;
2618 	struct sf_stats		*stats;
2619 	int			error;
2620 	int			result;
2621 
2622 	result = -1;
2623 	error = sysctl_handle_int(oidp, &result, 0, req);
2624 
2625 	if (error != 0 || req->newptr == NULL)
2626 		return (error);
2627 
2628 	if (result != 1)
2629 		return (error);
2630 
2631 	sc = (struct sf_softc *)arg1;
2632 	stats = &sc->sf_statistics;
2633 
2634 	printf("%s statistics:\n", device_get_nameunit(sc->sf_dev));
2635 	printf("Transmit good frames : %ju\n",
2636 	    (uintmax_t)stats->sf_tx_frames);
2637 	printf("Transmit good octets : %ju\n",
2638 	    (uintmax_t)stats->sf_tx_bytes);
2639 	printf("Transmit single collisions : %u\n",
2640 	    stats->sf_tx_single_colls);
2641 	printf("Transmit multiple collisions : %u\n",
2642 	    stats->sf_tx_multi_colls);
2643 	printf("Transmit late collisions : %u\n",
2644 	    stats->sf_tx_late_colls);
2645 	printf("Transmit abort due to excessive collisions : %u\n",
2646 	    stats->sf_tx_excess_colls);
2647 	printf("Transmit CRC errors : %u\n",
2648 	    stats->sf_tx_crcerrs);
2649 	printf("Transmit deferrals : %u\n",
2650 	    stats->sf_tx_deferred);
2651 	printf("Transmit abort due to excessive deferrals : %u\n",
2652 	    stats->sf_tx_excess_defer);
2653 	printf("Transmit pause control frames : %u\n",
2654 	    stats->sf_tx_pause_frames);
2655 	printf("Transmit control frames : %u\n",
2656 	    stats->sf_tx_control_frames);
2657 	printf("Transmit good multicast frames : %u\n",
2658 	    stats->sf_tx_mcast_frames);
2659 	printf("Transmit good broadcast frames : %u\n",
2660 	    stats->sf_tx_bcast_frames);
2661 	printf("Transmit frames lost due to internal transmit errors : %u\n",
2662 	    stats->sf_tx_frames_lost);
2663 	printf("Transmit FIFO underflows : %u\n",
2664 	    stats->sf_tx_underruns);
2665 	printf("Transmit GFP stalls : %u\n", stats->sf_tx_gfp_stall);
2666 	printf("Receive good frames : %ju\n",
2667 	    (uint64_t)stats->sf_rx_frames);
2668 	printf("Receive good octets : %ju\n",
2669 	    (uint64_t)stats->sf_rx_bytes);
2670 	printf("Receive CRC errors : %u\n",
2671 	    stats->sf_rx_crcerrs);
2672 	printf("Receive alignment errors : %u\n",
2673 	    stats->sf_rx_alignerrs);
2674 	printf("Receive pause frames : %u\n",
2675 	    stats->sf_rx_pause_frames);
2676 	printf("Receive control frames : %u\n",
2677 	    stats->sf_rx_control_frames);
2678 	printf("Receive control frames with unsupported opcode : %u\n",
2679 	    stats->sf_rx_unsup_control_frames);
2680 	printf("Receive frames too long : %u\n",
2681 	    stats->sf_rx_giants);
2682 	printf("Receive frames too short : %u\n",
2683 	    stats->sf_rx_runts);
2684 	printf("Receive frames jabber errors : %u\n",
2685 	    stats->sf_rx_jabbererrs);
2686 	printf("Receive frames fragments : %u\n",
2687 	    stats->sf_rx_fragments);
2688 	printf("Receive packets 64 bytes : %ju\n",
2689 	    (uint64_t)stats->sf_rx_pkts_64);
2690 	printf("Receive packets 65 to 127 bytes : %ju\n",
2691 	    (uint64_t)stats->sf_rx_pkts_65_127);
2692 	printf("Receive packets 128 to 255 bytes : %ju\n",
2693 	    (uint64_t)stats->sf_rx_pkts_128_255);
2694 	printf("Receive packets 256 to 511 bytes : %ju\n",
2695 	    (uint64_t)stats->sf_rx_pkts_256_511);
2696 	printf("Receive packets 512 to 1023 bytes : %ju\n",
2697 	    (uint64_t)stats->sf_rx_pkts_512_1023);
2698 	printf("Receive packets 1024 to 1518 bytes : %ju\n",
2699 	    (uint64_t)stats->sf_rx_pkts_1024_1518);
2700 	printf("Receive frames lost due to internal receive errors : %u\n",
2701 	    stats->sf_rx_frames_lost);
2702 	printf("Receive GFP stalls : %u\n", stats->sf_rx_gfp_stall);
2703 
2704 	return (error);
2705 }
2706 
2707 static int
2708 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2709 {
2710 	int error, value;
2711 
2712 	if (!arg1)
2713 		return (EINVAL);
2714 	value = *(int *)arg1;
2715 	error = sysctl_handle_int(oidp, &value, 0, req);
2716 	if (error || !req->newptr)
2717 		return (error);
2718 	if (value < low || value > high)
2719 		return (EINVAL);
2720 	*(int *)arg1 = value;
2721 
2722 	return (0);
2723 }
2724 
2725 static int
2726 sysctl_hw_sf_int_mod(SYSCTL_HANDLER_ARGS)
2727 {
2728 
2729 	return (sysctl_int_range(oidp, arg1, arg2, req, SF_IM_MIN, SF_IM_MAX));
2730 }
2731