if_sf.c revision 117208
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/*
34 * Adaptec AIC-6915 "Starfire" PCI fast ethernet driver for FreeBSD.
35 * Programming manual is available from:
36 * ftp.adaptec.com:/pub/BBS/userguides/aic6915_pg.pdf.
37 *
38 * Written by Bill Paul <wpaul@ctr.columbia.edu>
39 * Department of Electical Engineering
40 * Columbia University, New York City
41 */
42
43/*
44 * The Adaptec AIC-6915 "Starfire" is a 64-bit 10/100 PCI ethernet
45 * controller designed with flexibility and reducing CPU load in mind.
46 * The Starfire offers high and low priority buffer queues, a
47 * producer/consumer index mechanism and several different buffer
48 * queue and completion queue descriptor types. Any one of a number
49 * of different driver designs can be used, depending on system and
50 * OS requirements. This driver makes use of type0 transmit frame
51 * descriptors (since BSD fragments packets across an mbuf chain)
52 * and two RX buffer queues prioritized on size (one queue for small
53 * frames that will fit into a single mbuf, another with full size
54 * mbuf clusters for everything else). The producer/consumer indexes
55 * and completion queues are also used.
56 *
57 * One downside to the Starfire has to do with alignment: buffer
58 * queues must be aligned on 256-byte boundaries, and receive buffers
59 * must be aligned on longword boundaries. The receive buffer alignment
60 * causes problems on the Alpha platform, where the packet payload
61 * should be longword aligned. There is no simple way around this.
62 *
63 * For receive filtering, the Starfire offers 16 perfect filter slots
64 * and a 512-bit hash table.
65 *
66 * The Starfire has no internal transceiver, relying instead on an
67 * external MII-based transceiver. Accessing registers on external
68 * PHYs is done through a special register map rather than with the
69 * usual bitbang MDIO method.
70 *
71 * Acesssing the registers on the Starfire is a little tricky. The
72 * Starfire has a 512K internal register space. When programmed for
73 * PCI memory mapped mode, the entire register space can be accessed
74 * directly. However in I/O space mode, only 256 bytes are directly
75 * mapped into PCI I/O space. The other registers can be accessed
76 * indirectly using the SF_INDIRECTIO_ADDR and SF_INDIRECTIO_DATA
77 * registers inside the 256-byte I/O window.
78 */
79
80#include <sys/cdefs.h>
81__FBSDID("$FreeBSD: head/sys/dev/sf/if_sf.c 117208 2003-07-03 21:39:53Z imp $");
82
83#include <sys/param.h>
84#include <sys/systm.h>
85#include <sys/sockio.h>
86#include <sys/mbuf.h>
87#include <sys/malloc.h>
88#include <sys/kernel.h>
89#include <sys/socket.h>
90
91#include <net/if.h>
92#include <net/if_arp.h>
93#include <net/ethernet.h>
94#include <net/if_dl.h>
95#include <net/if_media.h>
96
97#include <net/bpf.h>
98
99#include <vm/vm.h>              /* for vtophys */
100#include <vm/pmap.h>            /* for vtophys */
101#include <machine/bus_pio.h>
102#include <machine/bus_memio.h>
103#include <machine/bus.h>
104#include <machine/resource.h>
105#include <sys/bus.h>
106#include <sys/rman.h>
107
108#include <dev/mii/mii.h>
109#include <dev/mii/miivar.h>
110
111/* "controller miibus0" required.  See GENERIC if you get errors here. */
112#include "miibus_if.h"
113
114#include <pci/pcireg.h>
115#include <pci/pcivar.h>
116
117#define SF_USEIOSPACE
118
119#include <pci/if_sfreg.h>
120
121MODULE_DEPEND(sf, pci, 1, 1, 1);
122MODULE_DEPEND(sf, ether, 1, 1, 1);
123MODULE_DEPEND(sf, miibus, 1, 1, 1);
124
125static struct sf_type sf_devs[] = {
126	{ AD_VENDORID, AD_DEVICEID_STARFIRE,
127		"Adaptec AIC-6915 10/100BaseTX" },
128	{ 0, 0, NULL }
129};
130
131static int sf_probe		(device_t);
132static int sf_attach		(device_t);
133static int sf_detach		(device_t);
134static void sf_intr		(void *);
135static void sf_stats_update	(void *);
136static void sf_rxeof		(struct sf_softc *);
137static void sf_txeof		(struct sf_softc *);
138static int sf_encap		(struct sf_softc *,
139					struct sf_tx_bufdesc_type0 *,
140					struct mbuf *);
141static void sf_start		(struct ifnet *);
142static int sf_ioctl		(struct ifnet *, u_long, caddr_t);
143static void sf_init		(void *);
144static void sf_stop		(struct sf_softc *);
145static void sf_watchdog		(struct ifnet *);
146static void sf_shutdown		(device_t);
147static int sf_ifmedia_upd	(struct ifnet *);
148static void sf_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
149static void sf_reset		(struct sf_softc *);
150static int sf_init_rx_ring	(struct sf_softc *);
151static void sf_init_tx_ring	(struct sf_softc *);
152static int sf_newbuf		(struct sf_softc *,
153					struct sf_rx_bufdesc_type0 *,
154					struct mbuf *);
155static void sf_setmulti		(struct sf_softc *);
156static int sf_setperf		(struct sf_softc *, int, caddr_t);
157static int sf_sethash		(struct sf_softc *, caddr_t, int);
158#ifdef notdef
159static int sf_setvlan		(struct sf_softc *, int, u_int32_t);
160#endif
161
162static u_int8_t sf_read_eeprom	(struct sf_softc *, int);
163static u_int32_t sf_calchash	(caddr_t);
164
165static int sf_miibus_readreg	(device_t, int, int);
166static int sf_miibus_writereg	(device_t, int, int, int);
167static void sf_miibus_statchg	(device_t);
168
169static u_int32_t csr_read_4	(struct sf_softc *, int);
170static void csr_write_4		(struct sf_softc *, int, u_int32_t);
171static void sf_txthresh_adjust	(struct sf_softc *);
172
173#ifdef SF_USEIOSPACE
174#define SF_RES			SYS_RES_IOPORT
175#define SF_RID			SF_PCI_LOIO
176#else
177#define SF_RES			SYS_RES_MEMORY
178#define SF_RID			SF_PCI_LOMEM
179#endif
180
181static device_method_t sf_methods[] = {
182	/* Device interface */
183	DEVMETHOD(device_probe,		sf_probe),
184	DEVMETHOD(device_attach,	sf_attach),
185	DEVMETHOD(device_detach,	sf_detach),
186	DEVMETHOD(device_shutdown,	sf_shutdown),
187
188	/* bus interface */
189	DEVMETHOD(bus_print_child,	bus_generic_print_child),
190	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
191
192	/* MII interface */
193	DEVMETHOD(miibus_readreg,	sf_miibus_readreg),
194	DEVMETHOD(miibus_writereg,	sf_miibus_writereg),
195	DEVMETHOD(miibus_statchg,	sf_miibus_statchg),
196
197	{ 0, 0 }
198};
199
200static driver_t sf_driver = {
201	"sf",
202	sf_methods,
203	sizeof(struct sf_softc),
204};
205
206static devclass_t sf_devclass;
207
208DRIVER_MODULE(sf, pci, sf_driver, sf_devclass, 0, 0);
209DRIVER_MODULE(miibus, sf, miibus_driver, miibus_devclass, 0, 0);
210
211#define SF_SETBIT(sc, reg, x)	\
212	csr_write_4(sc, reg, csr_read_4(sc, reg) | (x))
213
214#define SF_CLRBIT(sc, reg, x)				\
215	csr_write_4(sc, reg, csr_read_4(sc, reg) & ~(x))
216
217static u_int32_t
218csr_read_4(sc, reg)
219	struct sf_softc		*sc;
220	int			reg;
221{
222	u_int32_t		val;
223
224#ifdef SF_USEIOSPACE
225	CSR_WRITE_4(sc, SF_INDIRECTIO_ADDR, reg + SF_RMAP_INTREG_BASE);
226	val = CSR_READ_4(sc, SF_INDIRECTIO_DATA);
227#else
228	val = CSR_READ_4(sc, (reg + SF_RMAP_INTREG_BASE));
229#endif
230
231	return(val);
232}
233
234static u_int8_t
235sf_read_eeprom(sc, reg)
236	struct sf_softc		*sc;
237	int			reg;
238{
239	u_int8_t		val;
240
241	val = (csr_read_4(sc, SF_EEADDR_BASE +
242	    (reg & 0xFFFFFFFC)) >> (8 * (reg & 3))) & 0xFF;
243
244	return(val);
245}
246
247static void
248csr_write_4(sc, reg, val)
249	struct sf_softc		*sc;
250	int			reg;
251	u_int32_t		val;
252{
253#ifdef SF_USEIOSPACE
254	CSR_WRITE_4(sc, SF_INDIRECTIO_ADDR, reg + SF_RMAP_INTREG_BASE);
255	CSR_WRITE_4(sc, SF_INDIRECTIO_DATA, val);
256#else
257	CSR_WRITE_4(sc, (reg + SF_RMAP_INTREG_BASE), val);
258#endif
259	return;
260}
261
262static u_int32_t
263sf_calchash(addr)
264	caddr_t			addr;
265{
266	u_int32_t		crc, carry;
267	int			i, j;
268	u_int8_t		c;
269
270	/* Compute CRC for the address value. */
271	crc = 0xFFFFFFFF; /* initial value */
272
273	for (i = 0; i < 6; i++) {
274		c = *(addr + i);
275		for (j = 0; j < 8; j++) {
276			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
277			crc <<= 1;
278			c >>= 1;
279			if (carry)
280				crc = (crc ^ 0x04c11db6) | carry;
281		}
282	}
283
284	/* return the filter bit position */
285	return(crc >> 23 & 0x1FF);
286}
287
288/*
289 * Copy the address 'mac' into the perfect RX filter entry at
290 * offset 'idx.' The perfect filter only has 16 entries so do
291 * some sanity tests.
292 */
293static int
294sf_setperf(sc, idx, mac)
295	struct sf_softc		*sc;
296	int			idx;
297	caddr_t			mac;
298{
299	u_int16_t		*p;
300
301	if (idx < 0 || idx > SF_RXFILT_PERFECT_CNT)
302		return(EINVAL);
303
304	if (mac == NULL)
305		return(EINVAL);
306
307	p = (u_int16_t *)mac;
308
309	csr_write_4(sc, SF_RXFILT_PERFECT_BASE +
310	    (idx * SF_RXFILT_PERFECT_SKIP), htons(p[2]));
311	csr_write_4(sc, SF_RXFILT_PERFECT_BASE +
312	    (idx * SF_RXFILT_PERFECT_SKIP) + 4, htons(p[1]));
313	csr_write_4(sc, SF_RXFILT_PERFECT_BASE +
314	    (idx * SF_RXFILT_PERFECT_SKIP) + 8, htons(p[0]));
315
316	return(0);
317}
318
319/*
320 * Set the bit in the 512-bit hash table that corresponds to the
321 * specified mac address 'mac.' If 'prio' is nonzero, update the
322 * priority hash table instead of the filter hash table.
323 */
324static int
325sf_sethash(sc, mac, prio)
326	struct sf_softc		*sc;
327	caddr_t			mac;
328	int			prio;
329{
330	u_int32_t		h = 0;
331
332	if (mac == NULL)
333		return(EINVAL);
334
335	h = sf_calchash(mac);
336
337	if (prio) {
338		SF_SETBIT(sc, SF_RXFILT_HASH_BASE + SF_RXFILT_HASH_PRIOOFF +
339		    (SF_RXFILT_HASH_SKIP * (h >> 4)), (1 << (h & 0xF)));
340	} else {
341		SF_SETBIT(sc, SF_RXFILT_HASH_BASE + SF_RXFILT_HASH_ADDROFF +
342		    (SF_RXFILT_HASH_SKIP * (h >> 4)), (1 << (h & 0xF)));
343	}
344
345	return(0);
346}
347
348#ifdef notdef
349/*
350 * Set a VLAN tag in the receive filter.
351 */
352static int
353sf_setvlan(sc, idx, vlan)
354	struct sf_softc		*sc;
355	int			idx;
356	u_int32_t		vlan;
357{
358	if (idx < 0 || idx >> SF_RXFILT_HASH_CNT)
359		return(EINVAL);
360
361	csr_write_4(sc, SF_RXFILT_HASH_BASE +
362	    (idx * SF_RXFILT_HASH_SKIP) + SF_RXFILT_HASH_VLANOFF, vlan);
363
364	return(0);
365}
366#endif
367
368static int
369sf_miibus_readreg(dev, phy, reg)
370	device_t		dev;
371	int			phy, reg;
372{
373	struct sf_softc		*sc;
374	int			i;
375	u_int32_t		val = 0;
376
377	sc = device_get_softc(dev);
378
379	for (i = 0; i < SF_TIMEOUT; i++) {
380		val = csr_read_4(sc, SF_PHY_REG(phy, reg));
381		if (val & SF_MII_DATAVALID)
382			break;
383	}
384
385	if (i == SF_TIMEOUT)
386		return(0);
387
388	if ((val & 0x0000FFFF) == 0xFFFF)
389		return(0);
390
391	return(val & 0x0000FFFF);
392}
393
394static int
395sf_miibus_writereg(dev, phy, reg, val)
396	device_t		dev;
397	int			phy, reg, val;
398{
399	struct sf_softc		*sc;
400	int			i;
401	int			busy;
402
403	sc = device_get_softc(dev);
404
405	csr_write_4(sc, SF_PHY_REG(phy, reg), val);
406
407	for (i = 0; i < SF_TIMEOUT; i++) {
408		busy = csr_read_4(sc, SF_PHY_REG(phy, reg));
409		if (!(busy & SF_MII_BUSY))
410			break;
411	}
412
413	return(0);
414}
415
416static void
417sf_miibus_statchg(dev)
418	device_t		dev;
419{
420	struct sf_softc		*sc;
421	struct mii_data		*mii;
422
423	sc = device_get_softc(dev);
424	mii = device_get_softc(sc->sf_miibus);
425
426	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
427		SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_FULLDUPLEX);
428		csr_write_4(sc, SF_BKTOBKIPG, SF_IPGT_FDX);
429	} else {
430		SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_FULLDUPLEX);
431		csr_write_4(sc, SF_BKTOBKIPG, SF_IPGT_HDX);
432	}
433
434	return;
435}
436
437static void
438sf_setmulti(sc)
439	struct sf_softc		*sc;
440{
441	struct ifnet		*ifp;
442	int			i;
443	struct ifmultiaddr	*ifma;
444	u_int8_t		dummy[] = { 0, 0, 0, 0, 0, 0 };
445
446	ifp = &sc->arpcom.ac_if;
447
448	/* First zot all the existing filters. */
449	for (i = 1; i < SF_RXFILT_PERFECT_CNT; i++)
450		sf_setperf(sc, i, (char *)&dummy);
451	for (i = SF_RXFILT_HASH_BASE;
452	    i < (SF_RXFILT_HASH_MAX + 1); i += 4)
453		csr_write_4(sc, i, 0);
454	SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_ALLMULTI);
455
456	/* Now program new ones. */
457	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
458		SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_ALLMULTI);
459	} else {
460		i = 1;
461		TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) {
462			if (ifma->ifma_addr->sa_family != AF_LINK)
463				continue;
464			/*
465			 * Program the first 15 multicast groups
466			 * into the perfect filter. For all others,
467			 * use the hash table.
468			 */
469			if (i < SF_RXFILT_PERFECT_CNT) {
470				sf_setperf(sc, i,
471			LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
472				i++;
473				continue;
474			}
475
476			sf_sethash(sc,
477			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 0);
478		}
479	}
480
481	return;
482}
483
484/*
485 * Set media options.
486 */
487static int
488sf_ifmedia_upd(ifp)
489	struct ifnet		*ifp;
490{
491	struct sf_softc		*sc;
492	struct mii_data		*mii;
493
494	sc = ifp->if_softc;
495	mii = device_get_softc(sc->sf_miibus);
496	sc->sf_link = 0;
497	if (mii->mii_instance) {
498		struct mii_softc        *miisc;
499		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
500			mii_phy_reset(miisc);
501	}
502	mii_mediachg(mii);
503
504	return(0);
505}
506
507/*
508 * Report current media status.
509 */
510static void
511sf_ifmedia_sts(ifp, ifmr)
512	struct ifnet		*ifp;
513	struct ifmediareq	*ifmr;
514{
515	struct sf_softc		*sc;
516	struct mii_data		*mii;
517
518	sc = ifp->if_softc;
519	mii = device_get_softc(sc->sf_miibus);
520
521	mii_pollstat(mii);
522	ifmr->ifm_active = mii->mii_media_active;
523	ifmr->ifm_status = mii->mii_media_status;
524
525	return;
526}
527
528static int
529sf_ioctl(ifp, command, data)
530	struct ifnet		*ifp;
531	u_long			command;
532	caddr_t			data;
533{
534	struct sf_softc		*sc = ifp->if_softc;
535	struct ifreq		*ifr = (struct ifreq *) data;
536	struct mii_data		*mii;
537	int			error = 0;
538
539	SF_LOCK(sc);
540
541	switch(command) {
542	case SIOCSIFFLAGS:
543		if (ifp->if_flags & IFF_UP) {
544			if (ifp->if_flags & IFF_RUNNING &&
545			    ifp->if_flags & IFF_PROMISC &&
546			    !(sc->sf_if_flags & IFF_PROMISC)) {
547				SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC);
548			} else if (ifp->if_flags & IFF_RUNNING &&
549			    !(ifp->if_flags & IFF_PROMISC) &&
550			    sc->sf_if_flags & IFF_PROMISC) {
551				SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC);
552			} else if (!(ifp->if_flags & IFF_RUNNING))
553				sf_init(sc);
554		} else {
555			if (ifp->if_flags & IFF_RUNNING)
556				sf_stop(sc);
557		}
558		sc->sf_if_flags = ifp->if_flags;
559		error = 0;
560		break;
561	case SIOCADDMULTI:
562	case SIOCDELMULTI:
563		sf_setmulti(sc);
564		error = 0;
565		break;
566	case SIOCGIFMEDIA:
567	case SIOCSIFMEDIA:
568		mii = device_get_softc(sc->sf_miibus);
569		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
570		break;
571	default:
572		error = ether_ioctl(ifp, command, data);
573		break;
574	}
575
576	SF_UNLOCK(sc);
577
578	return(error);
579}
580
581static void
582sf_reset(sc)
583	struct sf_softc		*sc;
584{
585	register int		i;
586
587	csr_write_4(sc, SF_GEN_ETH_CTL, 0);
588	SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
589	DELAY(1000);
590	SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET);
591
592	SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_RESET);
593
594	for (i = 0; i < SF_TIMEOUT; i++) {
595		DELAY(10);
596		if (!(csr_read_4(sc, SF_PCI_DEVCFG) & SF_PCIDEVCFG_RESET))
597			break;
598	}
599
600	if (i == SF_TIMEOUT)
601		printf("sf%d: reset never completed!\n", sc->sf_unit);
602
603	/* Wait a little while for the chip to get its brains in order. */
604	DELAY(1000);
605	return;
606}
607
608/*
609 * Probe for an Adaptec AIC-6915 chip. Check the PCI vendor and device
610 * IDs against our list and return a device name if we find a match.
611 * We also check the subsystem ID so that we can identify exactly which
612 * NIC has been found, if possible.
613 */
614static int
615sf_probe(dev)
616	device_t		dev;
617{
618	struct sf_type		*t;
619
620	t = sf_devs;
621
622	while(t->sf_name != NULL) {
623		if ((pci_get_vendor(dev) == t->sf_vid) &&
624		    (pci_get_device(dev) == t->sf_did)) {
625			switch((pci_read_config(dev,
626			    SF_PCI_SUBVEN_ID, 4) >> 16) & 0xFFFF) {
627			case AD_SUBSYSID_62011_REV0:
628			case AD_SUBSYSID_62011_REV1:
629				device_set_desc(dev,
630				    "Adaptec ANA-62011 10/100BaseTX");
631				return(0);
632			case AD_SUBSYSID_62022:
633				device_set_desc(dev,
634				    "Adaptec ANA-62022 10/100BaseTX");
635				return(0);
636			case AD_SUBSYSID_62044_REV0:
637			case AD_SUBSYSID_62044_REV1:
638				device_set_desc(dev,
639				    "Adaptec ANA-62044 10/100BaseTX");
640				return(0);
641			case AD_SUBSYSID_62020:
642				device_set_desc(dev,
643				    "Adaptec ANA-62020 10/100BaseFX");
644				return(0);
645			case AD_SUBSYSID_69011:
646				device_set_desc(dev,
647				    "Adaptec ANA-69011 10/100BaseTX");
648				return(0);
649			default:
650				device_set_desc(dev, t->sf_name);
651				return(0);
652				break;
653			}
654		}
655		t++;
656	}
657
658	return(ENXIO);
659}
660
661/*
662 * Attach the interface. Allocate softc structures, do ifmedia
663 * setup and ethernet/BPF attach.
664 */
665static int
666sf_attach(dev)
667	device_t		dev;
668{
669	int			i;
670	struct sf_softc		*sc;
671	struct ifnet		*ifp;
672	int			unit, rid, error = 0;
673
674	sc = device_get_softc(dev);
675	unit = device_get_unit(dev);
676
677	mtx_init(&sc->sf_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
678	    MTX_DEF | MTX_RECURSE);
679#ifndef BURN_BRIDGES
680	/*
681	 * Handle power management nonsense.
682	 */
683	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
684		u_int32_t		iobase, membase, irq;
685
686		/* Save important PCI config data. */
687		iobase = pci_read_config(dev, SF_PCI_LOIO, 4);
688		membase = pci_read_config(dev, SF_PCI_LOMEM, 4);
689		irq = pci_read_config(dev, SF_PCI_INTLINE, 4);
690
691		/* Reset the power state. */
692		printf("sf%d: chip is in D%d power mode "
693		    "-- setting to D0\n", unit,
694		    pci_get_powerstate(dev));
695		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
696
697		/* Restore PCI config data. */
698		pci_write_config(dev, SF_PCI_LOIO, iobase, 4);
699		pci_write_config(dev, SF_PCI_LOMEM, membase, 4);
700		pci_write_config(dev, SF_PCI_INTLINE, irq, 4);
701	}
702#endif
703	/*
704	 * Map control/status registers.
705	 */
706	pci_enable_busmaster(dev);
707
708	rid = SF_RID;
709	sc->sf_res = bus_alloc_resource(dev, SF_RES, &rid,
710	    0, ~0, 1, RF_ACTIVE);
711
712	if (sc->sf_res == NULL) {
713		printf ("sf%d: couldn't map ports\n", unit);
714		error = ENXIO;
715		goto fail;
716	}
717
718	sc->sf_btag = rman_get_bustag(sc->sf_res);
719	sc->sf_bhandle = rman_get_bushandle(sc->sf_res);
720
721	/* Allocate interrupt */
722	rid = 0;
723	sc->sf_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
724	    RF_SHAREABLE | RF_ACTIVE);
725
726	if (sc->sf_irq == NULL) {
727		printf("sf%d: couldn't map interrupt\n", unit);
728		error = ENXIO;
729		goto fail;
730	}
731
732	callout_handle_init(&sc->sf_stat_ch);
733	/* Reset the adapter. */
734	sf_reset(sc);
735
736	/*
737	 * Get station address from the EEPROM.
738	 */
739	for (i = 0; i < ETHER_ADDR_LEN; i++)
740		sc->arpcom.ac_enaddr[i] =
741		    sf_read_eeprom(sc, SF_EE_NODEADDR + ETHER_ADDR_LEN - i);
742
743	/*
744	 * An Adaptec chip was detected. Inform the world.
745	 */
746	printf("sf%d: Ethernet address: %6D\n", unit,
747	    sc->arpcom.ac_enaddr, ":");
748
749	sc->sf_unit = unit;
750
751	/* Allocate the descriptor queues. */
752	sc->sf_ldata = contigmalloc(sizeof(struct sf_list_data), M_DEVBUF,
753	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
754
755	if (sc->sf_ldata == NULL) {
756		printf("sf%d: no memory for list buffers!\n", unit);
757		error = ENXIO;
758		goto fail;
759	}
760
761	bzero(sc->sf_ldata, sizeof(struct sf_list_data));
762
763	/* Do MII setup. */
764	if (mii_phy_probe(dev, &sc->sf_miibus,
765	    sf_ifmedia_upd, sf_ifmedia_sts)) {
766		printf("sf%d: MII without any phy!\n", sc->sf_unit);
767		error = ENXIO;
768		goto fail;
769	}
770
771	ifp = &sc->arpcom.ac_if;
772	ifp->if_softc = sc;
773	ifp->if_unit = unit;
774	ifp->if_name = "sf";
775	ifp->if_mtu = ETHERMTU;
776	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
777	ifp->if_ioctl = sf_ioctl;
778	ifp->if_output = ether_output;
779	ifp->if_start = sf_start;
780	ifp->if_watchdog = sf_watchdog;
781	ifp->if_init = sf_init;
782	ifp->if_baudrate = 10000000;
783	ifp->if_snd.ifq_maxlen = SF_TX_DLIST_CNT - 1;
784
785	/*
786	 * Call MI attach routine.
787	 */
788	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
789
790	/* Hook interrupt last to avoid having to lock softc */
791	error = bus_setup_intr(dev, sc->sf_irq, INTR_TYPE_NET,
792	    sf_intr, sc, &sc->sf_intrhand);
793
794	if (error) {
795		printf("sf%d: couldn't set up irq\n", unit);
796		ether_ifdetach(ifp);
797		goto fail;
798	}
799
800fail:
801	if (error)
802		sf_detach(dev);
803
804	return(error);
805}
806
807/*
808 * Shutdown hardware and free up resources. This can be called any
809 * time after the mutex has been initialized. It is called in both
810 * the error case in attach and the normal detach case so it needs
811 * to be careful about only freeing resources that have actually been
812 * allocated.
813 */
814static int
815sf_detach(dev)
816	device_t		dev;
817{
818	struct sf_softc		*sc;
819	struct ifnet		*ifp;
820
821	sc = device_get_softc(dev);
822	KASSERT(mtx_initialized(&sc->sf_mtx), ("sf mutex not initialized"));
823	SF_LOCK(sc);
824	ifp = &sc->arpcom.ac_if;
825
826	/* These should only be active if attach succeeded */
827	if (device_is_attached(dev)) {
828		sf_stop(sc);
829		ether_ifdetach(ifp);
830	}
831	if (sc->sf_miibus)
832		device_delete_child(dev, sc->sf_miibus);
833	bus_generic_detach(dev);
834
835	if (sc->sf_intrhand)
836		bus_teardown_intr(dev, sc->sf_irq, sc->sf_intrhand);
837	if (sc->sf_irq)
838		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sf_irq);
839	if (sc->sf_res)
840		bus_release_resource(dev, SF_RES, SF_RID, sc->sf_res);
841
842	if (sc->sf_ldata)
843		contigfree(sc->sf_ldata, sizeof(struct sf_list_data), M_DEVBUF);
844
845	SF_UNLOCK(sc);
846	mtx_destroy(&sc->sf_mtx);
847
848	return(0);
849}
850
851static int
852sf_init_rx_ring(sc)
853	struct sf_softc		*sc;
854{
855	struct sf_list_data	*ld;
856	int			i;
857
858	ld = sc->sf_ldata;
859
860	bzero((char *)ld->sf_rx_dlist_big,
861	    sizeof(struct sf_rx_bufdesc_type0) * SF_RX_DLIST_CNT);
862	bzero((char *)ld->sf_rx_clist,
863	    sizeof(struct sf_rx_cmpdesc_type3) * SF_RX_CLIST_CNT);
864
865	for (i = 0; i < SF_RX_DLIST_CNT; i++) {
866		if (sf_newbuf(sc, &ld->sf_rx_dlist_big[i], NULL) == ENOBUFS)
867			return(ENOBUFS);
868	}
869
870	return(0);
871}
872
873static void
874sf_init_tx_ring(sc)
875	struct sf_softc		*sc;
876{
877	struct sf_list_data	*ld;
878	int			i;
879
880	ld = sc->sf_ldata;
881
882	bzero((char *)ld->sf_tx_dlist,
883	    sizeof(struct sf_tx_bufdesc_type0) * SF_TX_DLIST_CNT);
884	bzero((char *)ld->sf_tx_clist,
885	    sizeof(struct sf_tx_cmpdesc_type0) * SF_TX_CLIST_CNT);
886
887	for (i = 0; i < SF_TX_DLIST_CNT; i++)
888		ld->sf_tx_dlist[i].sf_id = SF_TX_BUFDESC_ID;
889	for (i = 0; i < SF_TX_CLIST_CNT; i++)
890		ld->sf_tx_clist[i].sf_type = SF_TXCMPTYPE_TX;
891
892	ld->sf_tx_dlist[SF_TX_DLIST_CNT - 1].sf_end = 1;
893	sc->sf_tx_cnt = 0;
894
895	return;
896}
897
898static int
899sf_newbuf(sc, c, m)
900	struct sf_softc		*sc;
901	struct sf_rx_bufdesc_type0	*c;
902	struct mbuf		*m;
903{
904	struct mbuf		*m_new = NULL;
905
906	if (m == NULL) {
907		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
908		if (m_new == NULL)
909			return(ENOBUFS);
910
911		MCLGET(m_new, M_DONTWAIT);
912		if (!(m_new->m_flags & M_EXT)) {
913			m_freem(m_new);
914			return(ENOBUFS);
915		}
916		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
917	} else {
918		m_new = m;
919		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
920		m_new->m_data = m_new->m_ext.ext_buf;
921	}
922
923	m_adj(m_new, sizeof(u_int64_t));
924
925	c->sf_mbuf = m_new;
926	c->sf_addrlo = SF_RX_HOSTADDR(vtophys(mtod(m_new, caddr_t)));
927	c->sf_valid = 1;
928
929	return(0);
930}
931
932/*
933 * The starfire is programmed to use 'normal' mode for packet reception,
934 * which means we use the consumer/producer model for both the buffer
935 * descriptor queue and the completion descriptor queue. The only problem
936 * with this is that it involves a lot of register accesses: we have to
937 * read the RX completion consumer and producer indexes and the RX buffer
938 * producer index, plus the RX completion consumer and RX buffer producer
939 * indexes have to be updated. It would have been easier if Adaptec had
940 * put each index in a separate register, especially given that the damn
941 * NIC has a 512K register space.
942 *
943 * In spite of all the lovely features that Adaptec crammed into the 6915,
944 * it is marred by one truly stupid design flaw, which is that receive
945 * buffer addresses must be aligned on a longword boundary. This forces
946 * the packet payload to be unaligned, which is suboptimal on the x86 and
947 * completely unuseable on the Alpha. Our only recourse is to copy received
948 * packets into properly aligned buffers before handing them off.
949 */
950
951static void
952sf_rxeof(sc)
953	struct sf_softc		*sc;
954{
955	struct mbuf		*m;
956	struct ifnet		*ifp;
957	struct sf_rx_bufdesc_type0	*desc;
958	struct sf_rx_cmpdesc_type3	*cur_rx;
959	u_int32_t		rxcons, rxprod;
960	int			cmpprodidx, cmpconsidx, bufprodidx;
961
962	ifp = &sc->arpcom.ac_if;
963
964	rxcons = csr_read_4(sc, SF_CQ_CONSIDX);
965	rxprod = csr_read_4(sc, SF_RXDQ_PTR_Q1);
966	cmpprodidx = SF_IDX_LO(csr_read_4(sc, SF_CQ_PRODIDX));
967	cmpconsidx = SF_IDX_LO(rxcons);
968	bufprodidx = SF_IDX_LO(rxprod);
969
970	while (cmpconsidx != cmpprodidx) {
971		struct mbuf		*m0;
972
973		cur_rx = &sc->sf_ldata->sf_rx_clist[cmpconsidx];
974		desc = &sc->sf_ldata->sf_rx_dlist_big[cur_rx->sf_endidx];
975		m = desc->sf_mbuf;
976		SF_INC(cmpconsidx, SF_RX_CLIST_CNT);
977		SF_INC(bufprodidx, SF_RX_DLIST_CNT);
978
979		if (!(cur_rx->sf_status1 & SF_RXSTAT1_OK)) {
980			ifp->if_ierrors++;
981			sf_newbuf(sc, desc, m);
982			continue;
983		}
984
985		m0 = m_devget(mtod(m, char *), cur_rx->sf_len, ETHER_ALIGN,
986		    ifp, NULL);
987		sf_newbuf(sc, desc, m);
988		if (m0 == NULL) {
989			ifp->if_ierrors++;
990			continue;
991		}
992		m = m0;
993
994		ifp->if_ipackets++;
995		(*ifp->if_input)(ifp, m);
996	}
997
998	csr_write_4(sc, SF_CQ_CONSIDX,
999	    (rxcons & ~SF_CQ_CONSIDX_RXQ1) | cmpconsidx);
1000	csr_write_4(sc, SF_RXDQ_PTR_Q1,
1001	    (rxprod & ~SF_RXDQ_PRODIDX) | bufprodidx);
1002
1003	return;
1004}
1005
1006/*
1007 * Read the transmit status from the completion queue and release
1008 * mbufs. Note that the buffer descriptor index in the completion
1009 * descriptor is an offset from the start of the transmit buffer
1010 * descriptor list in bytes. This is important because the manual
1011 * gives the impression that it should match the producer/consumer
1012 * index, which is the offset in 8 byte blocks.
1013 */
1014static void
1015sf_txeof(sc)
1016	struct sf_softc		*sc;
1017{
1018	int			txcons, cmpprodidx, cmpconsidx;
1019	struct sf_tx_cmpdesc_type1 *cur_cmp;
1020	struct sf_tx_bufdesc_type0 *cur_tx;
1021	struct ifnet		*ifp;
1022
1023	ifp = &sc->arpcom.ac_if;
1024
1025	txcons = csr_read_4(sc, SF_CQ_CONSIDX);
1026	cmpprodidx = SF_IDX_HI(csr_read_4(sc, SF_CQ_PRODIDX));
1027	cmpconsidx = SF_IDX_HI(txcons);
1028
1029	while (cmpconsidx != cmpprodidx) {
1030		cur_cmp = &sc->sf_ldata->sf_tx_clist[cmpconsidx];
1031		cur_tx = &sc->sf_ldata->sf_tx_dlist[cur_cmp->sf_index >> 7];
1032
1033		if (cur_cmp->sf_txstat & SF_TXSTAT_TX_OK)
1034			ifp->if_opackets++;
1035		else {
1036			if (cur_cmp->sf_txstat & SF_TXSTAT_TX_UNDERRUN)
1037				sf_txthresh_adjust(sc);
1038			ifp->if_oerrors++;
1039		}
1040
1041		sc->sf_tx_cnt--;
1042		if (cur_tx->sf_mbuf != NULL) {
1043			m_freem(cur_tx->sf_mbuf);
1044			cur_tx->sf_mbuf = NULL;
1045		} else
1046			break;
1047		SF_INC(cmpconsidx, SF_TX_CLIST_CNT);
1048	}
1049
1050	ifp->if_timer = 0;
1051	ifp->if_flags &= ~IFF_OACTIVE;
1052
1053	csr_write_4(sc, SF_CQ_CONSIDX,
1054	    (txcons & ~SF_CQ_CONSIDX_TXQ) |
1055	    ((cmpconsidx << 16) & 0xFFFF0000));
1056
1057	return;
1058}
1059
1060static void
1061sf_txthresh_adjust(sc)
1062	struct sf_softc		*sc;
1063{
1064	u_int32_t		txfctl;
1065	u_int8_t		txthresh;
1066
1067	txfctl = csr_read_4(sc, SF_TX_FRAMCTL);
1068	txthresh = txfctl & SF_TXFRMCTL_TXTHRESH;
1069	if (txthresh < 0xFF) {
1070		txthresh++;
1071		txfctl &= ~SF_TXFRMCTL_TXTHRESH;
1072		txfctl |= txthresh;
1073#ifdef DIAGNOSTIC
1074		printf("sf%d: tx underrun, increasing "
1075		    "tx threshold to %d bytes\n",
1076		    sc->sf_unit, txthresh * 4);
1077#endif
1078		csr_write_4(sc, SF_TX_FRAMCTL, txfctl);
1079	}
1080
1081	return;
1082}
1083
1084static void
1085sf_intr(arg)
1086	void			*arg;
1087{
1088	struct sf_softc		*sc;
1089	struct ifnet		*ifp;
1090	u_int32_t		status;
1091
1092	sc = arg;
1093	SF_LOCK(sc);
1094
1095	ifp = &sc->arpcom.ac_if;
1096
1097	if (!(csr_read_4(sc, SF_ISR_SHADOW) & SF_ISR_PCIINT_ASSERTED)) {
1098		SF_UNLOCK(sc);
1099		return;
1100	}
1101
1102	/* Disable interrupts. */
1103	csr_write_4(sc, SF_IMR, 0x00000000);
1104
1105	for (;;) {
1106		status = csr_read_4(sc, SF_ISR);
1107		if (status)
1108			csr_write_4(sc, SF_ISR, status);
1109
1110		if (!(status & SF_INTRS))
1111			break;
1112
1113		if (status & SF_ISR_RXDQ1_DMADONE)
1114			sf_rxeof(sc);
1115
1116		if (status & SF_ISR_TX_TXDONE ||
1117		    status & SF_ISR_TX_DMADONE ||
1118		    status & SF_ISR_TX_QUEUEDONE)
1119			sf_txeof(sc);
1120
1121		if (status & SF_ISR_TX_LOFIFO)
1122			sf_txthresh_adjust(sc);
1123
1124		if (status & SF_ISR_ABNORMALINTR) {
1125			if (status & SF_ISR_STATSOFLOW) {
1126				untimeout(sf_stats_update, sc,
1127				    sc->sf_stat_ch);
1128				sf_stats_update(sc);
1129			} else
1130				sf_init(sc);
1131		}
1132	}
1133
1134	/* Re-enable interrupts. */
1135	csr_write_4(sc, SF_IMR, SF_INTRS);
1136
1137	if (ifp->if_snd.ifq_head != NULL)
1138		sf_start(ifp);
1139
1140	SF_UNLOCK(sc);
1141	return;
1142}
1143
1144static void
1145sf_init(xsc)
1146	void			*xsc;
1147{
1148	struct sf_softc		*sc;
1149	struct ifnet		*ifp;
1150	struct mii_data		*mii;
1151	int			i;
1152
1153	sc = xsc;
1154	SF_LOCK(sc);
1155	ifp = &sc->arpcom.ac_if;
1156	mii = device_get_softc(sc->sf_miibus);
1157
1158	sf_stop(sc);
1159	sf_reset(sc);
1160
1161	/* Init all the receive filter registers */
1162	for (i = SF_RXFILT_PERFECT_BASE;
1163	    i < (SF_RXFILT_HASH_MAX + 1); i += 4)
1164		csr_write_4(sc, i, 0);
1165
1166	/* Empty stats counter registers. */
1167	for (i = 0; i < sizeof(struct sf_stats)/sizeof(u_int32_t); i++)
1168		csr_write_4(sc, SF_STATS_BASE +
1169		    (i + sizeof(u_int32_t)), 0);
1170
1171	/* Init our MAC address */
1172	csr_write_4(sc, SF_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
1173	csr_write_4(sc, SF_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
1174	sf_setperf(sc, 0, (caddr_t)&sc->arpcom.ac_enaddr);
1175
1176	if (sf_init_rx_ring(sc) == ENOBUFS) {
1177		printf("sf%d: initialization failed: no "
1178		    "memory for rx buffers\n", sc->sf_unit);
1179		SF_UNLOCK(sc);
1180		return;
1181	}
1182
1183	sf_init_tx_ring(sc);
1184
1185	csr_write_4(sc, SF_RXFILT, SF_PERFMODE_NORMAL|SF_HASHMODE_WITHVLAN);
1186
1187	/* If we want promiscuous mode, set the allframes bit. */
1188	if (ifp->if_flags & IFF_PROMISC) {
1189		SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC);
1190	} else {
1191		SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC);
1192	}
1193
1194	if (ifp->if_flags & IFF_BROADCAST) {
1195		SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_BROAD);
1196	} else {
1197		SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_BROAD);
1198	}
1199
1200	/*
1201	 * Load the multicast filter.
1202	 */
1203	sf_setmulti(sc);
1204
1205	/* Init the completion queue indexes */
1206	csr_write_4(sc, SF_CQ_CONSIDX, 0);
1207	csr_write_4(sc, SF_CQ_PRODIDX, 0);
1208
1209	/* Init the RX completion queue */
1210	csr_write_4(sc, SF_RXCQ_CTL_1,
1211	    vtophys(sc->sf_ldata->sf_rx_clist) & SF_RXCQ_ADDR);
1212	SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQTYPE_3);
1213
1214	/* Init RX DMA control. */
1215	SF_SETBIT(sc, SF_RXDMA_CTL, SF_RXDMA_REPORTBADPKTS);
1216
1217	/* Init the RX buffer descriptor queue. */
1218	csr_write_4(sc, SF_RXDQ_ADDR_Q1,
1219	    vtophys(sc->sf_ldata->sf_rx_dlist_big));
1220	csr_write_4(sc, SF_RXDQ_CTL_1, (MCLBYTES << 16) | SF_DESCSPACE_16BYTES);
1221	csr_write_4(sc, SF_RXDQ_PTR_Q1, SF_RX_DLIST_CNT - 1);
1222
1223	/* Init the TX completion queue */
1224	csr_write_4(sc, SF_TXCQ_CTL,
1225	    vtophys(sc->sf_ldata->sf_tx_clist) & SF_RXCQ_ADDR);
1226
1227	/* Init the TX buffer descriptor queue. */
1228	csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO,
1229		vtophys(sc->sf_ldata->sf_tx_dlist));
1230	SF_SETBIT(sc, SF_TX_FRAMCTL, SF_TXFRMCTL_CPLAFTERTX);
1231	csr_write_4(sc, SF_TXDQ_CTL,
1232	    SF_TXBUFDESC_TYPE0|SF_TXMINSPACE_128BYTES|SF_TXSKIPLEN_8BYTES);
1233	SF_SETBIT(sc, SF_TXDQ_CTL, SF_TXDQCTL_NODMACMP);
1234
1235	/* Enable autopadding of short TX frames. */
1236	SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_AUTOPAD);
1237
1238	/* Enable interrupts. */
1239	csr_write_4(sc, SF_IMR, SF_INTRS);
1240	SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_INTR_ENB);
1241
1242	/* Enable the RX and TX engines. */
1243	SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RX_ENB|SF_ETHCTL_RXDMA_ENB);
1244	SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TX_ENB|SF_ETHCTL_TXDMA_ENB);
1245
1246	/*mii_mediachg(mii);*/
1247	sf_ifmedia_upd(ifp);
1248
1249	ifp->if_flags |= IFF_RUNNING;
1250	ifp->if_flags &= ~IFF_OACTIVE;
1251
1252	sc->sf_stat_ch = timeout(sf_stats_update, sc, hz);
1253
1254	SF_UNLOCK(sc);
1255
1256	return;
1257}
1258
1259static int
1260sf_encap(sc, c, m_head)
1261	struct sf_softc		*sc;
1262	struct sf_tx_bufdesc_type0 *c;
1263	struct mbuf		*m_head;
1264{
1265	int			frag = 0;
1266	struct sf_frag		*f = NULL;
1267	struct mbuf		*m;
1268
1269	m = m_head;
1270
1271	for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1272		if (m->m_len != 0) {
1273			if (frag == SF_MAXFRAGS)
1274				break;
1275			f = &c->sf_frags[frag];
1276			if (frag == 0)
1277				f->sf_pktlen = m_head->m_pkthdr.len;
1278			f->sf_fraglen = m->m_len;
1279			f->sf_addr = vtophys(mtod(m, vm_offset_t));
1280			frag++;
1281		}
1282	}
1283
1284	if (m != NULL) {
1285		struct mbuf		*m_new = NULL;
1286
1287		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1288		if (m_new == NULL) {
1289			printf("sf%d: no memory for tx list\n", sc->sf_unit);
1290			return(1);
1291		}
1292
1293		if (m_head->m_pkthdr.len > MHLEN) {
1294			MCLGET(m_new, M_DONTWAIT);
1295			if (!(m_new->m_flags & M_EXT)) {
1296				m_freem(m_new);
1297				printf("sf%d: no memory for tx list\n",
1298				    sc->sf_unit);
1299				return(1);
1300			}
1301		}
1302		m_copydata(m_head, 0, m_head->m_pkthdr.len,
1303		    mtod(m_new, caddr_t));
1304		m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1305		m_freem(m_head);
1306		m_head = m_new;
1307		f = &c->sf_frags[0];
1308		f->sf_fraglen = f->sf_pktlen = m_head->m_pkthdr.len;
1309		f->sf_addr = vtophys(mtod(m_head, caddr_t));
1310		frag = 1;
1311	}
1312
1313	c->sf_mbuf = m_head;
1314	c->sf_id = SF_TX_BUFDESC_ID;
1315	c->sf_fragcnt = frag;
1316	c->sf_intr = 1;
1317	c->sf_caltcp = 0;
1318	c->sf_crcen = 1;
1319
1320	return(0);
1321}
1322
1323static void
1324sf_start(ifp)
1325	struct ifnet		*ifp;
1326{
1327	struct sf_softc		*sc;
1328	struct sf_tx_bufdesc_type0 *cur_tx = NULL;
1329	struct mbuf		*m_head = NULL;
1330	int			i, txprod;
1331
1332	sc = ifp->if_softc;
1333	SF_LOCK(sc);
1334
1335	if (!sc->sf_link && ifp->if_snd.ifq_len < 10) {
1336		SF_UNLOCK(sc);
1337		return;
1338	}
1339
1340	if (ifp->if_flags & IFF_OACTIVE) {
1341		SF_UNLOCK(sc);
1342		return;
1343	}
1344
1345	txprod = csr_read_4(sc, SF_TXDQ_PRODIDX);
1346	i = SF_IDX_HI(txprod) >> 4;
1347
1348	if (sc->sf_ldata->sf_tx_dlist[i].sf_mbuf != NULL) {
1349		printf("sf%d: TX ring full, resetting\n", sc->sf_unit);
1350		sf_init(sc);
1351		txprod = csr_read_4(sc, SF_TXDQ_PRODIDX);
1352		i = SF_IDX_HI(txprod) >> 4;
1353	}
1354
1355	while(sc->sf_ldata->sf_tx_dlist[i].sf_mbuf == NULL) {
1356		if (sc->sf_tx_cnt >= (SF_TX_DLIST_CNT - 5)) {
1357			ifp->if_flags |= IFF_OACTIVE;
1358			cur_tx = NULL;
1359			break;
1360		}
1361		IF_DEQUEUE(&ifp->if_snd, m_head);
1362		if (m_head == NULL)
1363			break;
1364
1365		cur_tx = &sc->sf_ldata->sf_tx_dlist[i];
1366		if (sf_encap(sc, cur_tx, m_head)) {
1367			IF_PREPEND(&ifp->if_snd, m_head);
1368			ifp->if_flags |= IFF_OACTIVE;
1369			cur_tx = NULL;
1370			break;
1371		}
1372
1373		/*
1374		 * If there's a BPF listener, bounce a copy of this frame
1375		 * to him.
1376		 */
1377		BPF_MTAP(ifp, m_head);
1378
1379		SF_INC(i, SF_TX_DLIST_CNT);
1380		sc->sf_tx_cnt++;
1381		/*
1382		 * Don't get the TX DMA queue get too full.
1383		 */
1384		if (sc->sf_tx_cnt > 64)
1385			break;
1386	}
1387
1388	if (cur_tx == NULL) {
1389		SF_UNLOCK(sc);
1390		return;
1391	}
1392
1393	/* Transmit */
1394	csr_write_4(sc, SF_TXDQ_PRODIDX,
1395	    (txprod & ~SF_TXDQ_PRODIDX_HIPRIO) |
1396	    ((i << 20) & 0xFFFF0000));
1397
1398	ifp->if_timer = 5;
1399
1400	SF_UNLOCK(sc);
1401
1402	return;
1403}
1404
1405static void
1406sf_stop(sc)
1407	struct sf_softc		*sc;
1408{
1409	int			i;
1410	struct ifnet		*ifp;
1411
1412	SF_LOCK(sc);
1413
1414	ifp = &sc->arpcom.ac_if;
1415
1416	untimeout(sf_stats_update, sc, sc->sf_stat_ch);
1417
1418	csr_write_4(sc, SF_GEN_ETH_CTL, 0);
1419	csr_write_4(sc, SF_CQ_CONSIDX, 0);
1420	csr_write_4(sc, SF_CQ_PRODIDX, 0);
1421	csr_write_4(sc, SF_RXDQ_ADDR_Q1, 0);
1422	csr_write_4(sc, SF_RXDQ_CTL_1, 0);
1423	csr_write_4(sc, SF_RXDQ_PTR_Q1, 0);
1424	csr_write_4(sc, SF_TXCQ_CTL, 0);
1425	csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0);
1426	csr_write_4(sc, SF_TXDQ_CTL, 0);
1427	sf_reset(sc);
1428
1429	sc->sf_link = 0;
1430
1431	for (i = 0; i < SF_RX_DLIST_CNT; i++) {
1432		if (sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf != NULL) {
1433			m_freem(sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf);
1434			sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf = NULL;
1435		}
1436	}
1437
1438	for (i = 0; i < SF_TX_DLIST_CNT; i++) {
1439		if (sc->sf_ldata->sf_tx_dlist[i].sf_mbuf != NULL) {
1440			m_freem(sc->sf_ldata->sf_tx_dlist[i].sf_mbuf);
1441			sc->sf_ldata->sf_tx_dlist[i].sf_mbuf = NULL;
1442		}
1443	}
1444
1445	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1446	SF_UNLOCK(sc);
1447
1448	return;
1449}
1450
1451/*
1452 * Note: it is important that this function not be interrupted. We
1453 * use a two-stage register access scheme: if we are interrupted in
1454 * between setting the indirect address register and reading from the
1455 * indirect data register, the contents of the address register could
1456 * be changed out from under us.
1457 */
1458static void
1459sf_stats_update(xsc)
1460	void			*xsc;
1461{
1462	struct sf_softc		*sc;
1463	struct ifnet		*ifp;
1464	struct mii_data		*mii;
1465	struct sf_stats		stats;
1466	u_int32_t		*ptr;
1467	int			i;
1468
1469	sc = xsc;
1470	SF_LOCK(sc);
1471	ifp = &sc->arpcom.ac_if;
1472	mii = device_get_softc(sc->sf_miibus);
1473
1474	ptr = (u_int32_t *)&stats;
1475	for (i = 0; i < sizeof(stats)/sizeof(u_int32_t); i++)
1476		ptr[i] = csr_read_4(sc, SF_STATS_BASE +
1477		    (i + sizeof(u_int32_t)));
1478
1479	for (i = 0; i < sizeof(stats)/sizeof(u_int32_t); i++)
1480		csr_write_4(sc, SF_STATS_BASE +
1481		    (i + sizeof(u_int32_t)), 0);
1482
1483	ifp->if_collisions += stats.sf_tx_single_colls +
1484	    stats.sf_tx_multi_colls + stats.sf_tx_excess_colls;
1485
1486	mii_tick(mii);
1487
1488	if (!sc->sf_link && mii->mii_media_status & IFM_ACTIVE &&
1489	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1490		sc->sf_link++;
1491		if (ifp->if_snd.ifq_head != NULL)
1492			sf_start(ifp);
1493	}
1494
1495	sc->sf_stat_ch = timeout(sf_stats_update, sc, hz);
1496
1497	SF_UNLOCK(sc);
1498
1499	return;
1500}
1501
1502static void
1503sf_watchdog(ifp)
1504	struct ifnet		*ifp;
1505{
1506	struct sf_softc		*sc;
1507
1508	sc = ifp->if_softc;
1509
1510	SF_LOCK(sc);
1511
1512	ifp->if_oerrors++;
1513	printf("sf%d: watchdog timeout\n", sc->sf_unit);
1514
1515	sf_stop(sc);
1516	sf_reset(sc);
1517	sf_init(sc);
1518
1519	if (ifp->if_snd.ifq_head != NULL)
1520		sf_start(ifp);
1521
1522	SF_UNLOCK(sc);
1523
1524	return;
1525}
1526
1527static void
1528sf_shutdown(dev)
1529	device_t		dev;
1530{
1531	struct sf_softc		*sc;
1532
1533	sc = device_get_softc(dev);
1534
1535	sf_stop(sc);
1536
1537	return;
1538}
1539