if_ste.c revision 50548
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 * $FreeBSD: head/sys/pci/if_ste.c 50548 1999-08-29 09:03:58Z bde $
33 */
34
35
36#include "bpf.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/sockio.h>
41#include <sys/mbuf.h>
42#include <sys/malloc.h>
43#include <sys/kernel.h>
44#include <sys/socket.h>
45
46#include <net/if.h>
47#include <net/if_arp.h>
48#include <net/ethernet.h>
49#include <net/if_dl.h>
50#include <net/if_media.h>
51
52#if NBPF > 0
53#include <net/bpf.h>
54#endif
55
56#include "opt_bdg.h"
57#ifdef BRIDGE
58#include <net/bridge.h>
59#endif
60
61#include <vm/vm.h>              /* for vtophys */
62#include <vm/pmap.h>            /* for vtophys */
63#include <machine/clock.h>      /* for DELAY */
64#include <machine/bus_memio.h>
65#include <machine/bus_pio.h>
66#include <machine/bus.h>
67#include <machine/resource.h>
68#include <sys/bus.h>
69#include <sys/rman.h>
70
71#include <dev/mii/mii.h>
72#include <dev/mii/miivar.h>
73
74#include <pci/pcireg.h>
75#include <pci/pcivar.h>
76
77#include "miibus_if.h"
78
79#define STE_USEIOSPACE
80
81#include <pci/if_stereg.h>
82
83#if !defined(lint)
84static const char rcsid[] =
85  "$FreeBSD: head/sys/pci/if_ste.c 50548 1999-08-29 09:03:58Z bde $";
86#endif
87
88/*
89 * Various supported device vendors/types and their names.
90 */
91static struct ste_type ste_devs[] = {
92	{ ST_VENDORID, ST_DEVICEID_ST201, "Sundance ST201 10/100BaseTX" },
93	{ DL_VENDORID, DL_DEVICEID_550TX, "D-Link DFE-550TX 10/100BaseTX" },
94	{ 0, 0, NULL }
95};
96
97static int ste_probe		__P((device_t));
98static int ste_attach		__P((device_t));
99static int ste_detach		__P((device_t));
100static void ste_init		__P((void *));
101static void ste_intr		__P((void *));
102static void ste_rxeof		__P((struct ste_softc *));
103static void ste_txeoc		__P((struct ste_softc *));
104static void ste_txeof		__P((struct ste_softc *));
105static void ste_stats_update	__P((void *));
106static void ste_stop		__P((struct ste_softc *));
107static void ste_reset		__P((struct ste_softc *));
108static int ste_ioctl		__P((struct ifnet *, u_long, caddr_t));
109static int ste_encap		__P((struct ste_softc *, struct ste_chain *,
110					struct mbuf *));
111static void ste_start		__P((struct ifnet *));
112static void ste_watchdog	__P((struct ifnet *));
113static void ste_shutdown	__P((device_t));
114static int ste_newbuf		__P((struct ste_softc *,
115					struct ste_chain_onefrag *,
116					struct mbuf *));
117static int ste_ifmedia_upd	__P((struct ifnet *));
118static void ste_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
119
120static void ste_mii_sync	__P((struct ste_softc *));
121static void ste_mii_send	__P((struct ste_softc *, u_int32_t, int));
122static int ste_mii_readreg	__P((struct ste_softc *,
123					struct ste_mii_frame *));
124static int ste_mii_writereg	__P((struct ste_softc *,
125					struct ste_mii_frame *));
126static int ste_miibus_readreg	__P((device_t, int, int));
127static int ste_miibus_writereg	__P((device_t, int, int, int));
128static void ste_miibus_statchg	__P((device_t));
129
130static int ste_eeprom_wait	__P((struct ste_softc *));
131static int ste_read_eeprom	__P((struct ste_softc *, caddr_t, int,
132							int, int));
133static void ste_wait		__P((struct ste_softc *));
134static u_int8_t ste_calchash	__P((caddr_t));
135static void ste_setmulti	__P((struct ste_softc *));
136static int ste_init_rx_list	__P((struct ste_softc *));
137static void ste_init_tx_list	__P((struct ste_softc *));
138
139#ifdef STE_USEIOSPACE
140#define STE_RES			SYS_RES_IOPORT
141#define STE_RID			STE_PCI_LOIO
142#else
143#define STE_RES			SYS_RES_MEMORY
144#define STE_RID			STE_PCI_LOMEM
145#endif
146
147static device_method_t ste_methods[] = {
148	/* Device interface */
149	DEVMETHOD(device_probe,		ste_probe),
150	DEVMETHOD(device_attach,	ste_attach),
151	DEVMETHOD(device_detach,	ste_detach),
152	DEVMETHOD(device_shutdown,	ste_shutdown),
153
154	/* bus interface */
155	DEVMETHOD(bus_print_child,	bus_generic_print_child),
156	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
157
158	/* MII interface */
159	DEVMETHOD(miibus_readreg,	ste_miibus_readreg),
160	DEVMETHOD(miibus_writereg,	ste_miibus_writereg),
161	DEVMETHOD(miibus_statchg,	ste_miibus_statchg),
162
163	{ 0, 0 }
164};
165
166static driver_t ste_driver = {
167	"ste",
168	ste_methods,
169	sizeof(struct ste_softc)
170};
171
172static devclass_t ste_devclass;
173
174DRIVER_MODULE(ste, pci, ste_driver, ste_devclass, 0, 0);
175DRIVER_MODULE(miibus, ste, miibus_driver, miibus_devclass, 0, 0);
176
177#define STE_SETBIT4(sc, reg, x)				\
178	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
179
180#define STE_CLRBIT4(sc, reg, x)				\
181	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
182
183#define STE_SETBIT2(sc, reg, x)				\
184	CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | x)
185
186#define STE_CLRBIT2(sc, reg, x)				\
187	CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~x)
188
189#define STE_SETBIT1(sc, reg, x)				\
190	CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | x)
191
192#define STE_CLRBIT1(sc, reg, x)				\
193	CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~x)
194
195
196#define MII_SET(x)		STE_SETBIT1(sc, STE_PHYCTL, x)
197#define MII_CLR(x)		STE_CLRBIT1(sc, STE_PHYCTL, x)
198
199/*
200 * Sync the PHYs by setting data bit and strobing the clock 32 times.
201 */
202static void ste_mii_sync(sc)
203	struct ste_softc		*sc;
204{
205	register int		i;
206
207	MII_SET(STE_PHYCTL_MDIR|STE_PHYCTL_MDATA);
208
209	for (i = 0; i < 32; i++) {
210		MII_SET(STE_PHYCTL_MCLK);
211		DELAY(1);
212		MII_CLR(STE_PHYCTL_MCLK);
213		DELAY(1);
214	}
215
216	return;
217}
218
219/*
220 * Clock a series of bits through the MII.
221 */
222static void ste_mii_send(sc, bits, cnt)
223	struct ste_softc		*sc;
224	u_int32_t		bits;
225	int			cnt;
226{
227	int			i;
228
229	MII_CLR(STE_PHYCTL_MCLK);
230
231	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
232                if (bits & i) {
233			MII_SET(STE_PHYCTL_MDATA);
234                } else {
235			MII_CLR(STE_PHYCTL_MDATA);
236                }
237		DELAY(1);
238		MII_CLR(STE_PHYCTL_MCLK);
239		DELAY(1);
240		MII_SET(STE_PHYCTL_MCLK);
241	}
242}
243
244/*
245 * Read an PHY register through the MII.
246 */
247static int ste_mii_readreg(sc, frame)
248	struct ste_softc		*sc;
249	struct ste_mii_frame	*frame;
250
251{
252	int			i, ack, s;
253
254	s = splimp();
255
256	/*
257	 * Set up frame for RX.
258	 */
259	frame->mii_stdelim = STE_MII_STARTDELIM;
260	frame->mii_opcode = STE_MII_READOP;
261	frame->mii_turnaround = 0;
262	frame->mii_data = 0;
263
264	CSR_WRITE_2(sc, STE_PHYCTL, 0);
265	/*
266 	 * Turn on data xmit.
267	 */
268	MII_SET(STE_PHYCTL_MDIR);
269
270	ste_mii_sync(sc);
271
272	/*
273	 * Send command/address info.
274	 */
275	ste_mii_send(sc, frame->mii_stdelim, 2);
276	ste_mii_send(sc, frame->mii_opcode, 2);
277	ste_mii_send(sc, frame->mii_phyaddr, 5);
278	ste_mii_send(sc, frame->mii_regaddr, 5);
279
280	/* Turn off xmit. */
281	MII_CLR(STE_PHYCTL_MDIR);
282
283	/* Idle bit */
284	MII_CLR((STE_PHYCTL_MCLK|STE_PHYCTL_MDATA));
285	DELAY(1);
286	MII_SET(STE_PHYCTL_MCLK);
287	DELAY(1);
288
289	/* Check for ack */
290	MII_CLR(STE_PHYCTL_MCLK);
291	DELAY(1);
292	MII_SET(STE_PHYCTL_MCLK);
293	DELAY(1);
294	ack = CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA;
295
296	/*
297	 * Now try reading data bits. If the ack failed, we still
298	 * need to clock through 16 cycles to keep the PHY(s) in sync.
299	 */
300	if (ack) {
301		for(i = 0; i < 16; i++) {
302			MII_CLR(STE_PHYCTL_MCLK);
303			DELAY(1);
304			MII_SET(STE_PHYCTL_MCLK);
305			DELAY(1);
306		}
307		goto fail;
308	}
309
310	for (i = 0x8000; i; i >>= 1) {
311		MII_CLR(STE_PHYCTL_MCLK);
312		DELAY(1);
313		if (!ack) {
314			if (CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA)
315				frame->mii_data |= i;
316			DELAY(1);
317		}
318		MII_SET(STE_PHYCTL_MCLK);
319		DELAY(1);
320	}
321
322fail:
323
324	MII_CLR(STE_PHYCTL_MCLK);
325	DELAY(1);
326	MII_SET(STE_PHYCTL_MCLK);
327	DELAY(1);
328
329	splx(s);
330
331	if (ack)
332		return(1);
333	return(0);
334}
335
336/*
337 * Write to a PHY register through the MII.
338 */
339static int ste_mii_writereg(sc, frame)
340	struct ste_softc		*sc;
341	struct ste_mii_frame	*frame;
342
343{
344	int			s;
345
346	s = splimp();
347	/*
348	 * Set up frame for TX.
349	 */
350
351	frame->mii_stdelim = STE_MII_STARTDELIM;
352	frame->mii_opcode = STE_MII_WRITEOP;
353	frame->mii_turnaround = STE_MII_TURNAROUND;
354
355	/*
356 	 * Turn on data output.
357	 */
358	MII_SET(STE_PHYCTL_MDIR);
359
360	ste_mii_sync(sc);
361
362	ste_mii_send(sc, frame->mii_stdelim, 2);
363	ste_mii_send(sc, frame->mii_opcode, 2);
364	ste_mii_send(sc, frame->mii_phyaddr, 5);
365	ste_mii_send(sc, frame->mii_regaddr, 5);
366	ste_mii_send(sc, frame->mii_turnaround, 2);
367	ste_mii_send(sc, frame->mii_data, 16);
368
369	/* Idle bit. */
370	MII_SET(STE_PHYCTL_MCLK);
371	DELAY(1);
372	MII_CLR(STE_PHYCTL_MCLK);
373	DELAY(1);
374
375	/*
376	 * Turn off xmit.
377	 */
378	MII_CLR(STE_PHYCTL_MDIR);
379
380	splx(s);
381
382	return(0);
383}
384
385static int ste_miibus_readreg(dev, phy, reg)
386	device_t		dev;
387	int			phy, reg;
388{
389	struct ste_softc	*sc;
390	struct ste_mii_frame	frame;
391
392	sc = device_get_softc(dev);
393
394	bzero((char *)&frame, sizeof(frame));
395
396	frame.mii_phyaddr = phy;
397	frame.mii_regaddr = reg;
398	ste_mii_readreg(sc, &frame);
399
400	return(frame.mii_data);
401}
402
403static int ste_miibus_writereg(dev, phy, reg, data)
404	device_t		dev;
405	int			phy, reg, data;
406{
407	struct ste_softc	*sc;
408	struct ste_mii_frame	frame;
409
410	sc = device_get_softc(dev);
411	bzero((char *)&frame, sizeof(frame));
412
413	frame.mii_phyaddr = phy;
414	frame.mii_regaddr = reg;
415	frame.mii_data = data;
416
417	ste_mii_writereg(sc, &frame);
418
419	return(0);
420}
421
422static void ste_miibus_statchg(dev)
423	device_t		dev;
424{
425	struct ste_softc	*sc;
426	struct mii_data		*mii;
427
428	sc = device_get_softc(dev);
429	mii = device_get_softc(sc->ste_miibus);
430
431	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
432		STE_SETBIT2(sc, STE_MACCTL0, STE_MACCTL0_FULLDUPLEX);
433	} else {
434		STE_CLRBIT2(sc, STE_MACCTL0, STE_MACCTL0_FULLDUPLEX);
435	}
436
437	return;
438}
439
440static int ste_ifmedia_upd(ifp)
441	struct ifnet		*ifp;
442{
443	struct ste_softc	*sc;
444	struct mii_data		*mii;
445
446	sc = ifp->if_softc;
447	mii = device_get_softc(sc->ste_miibus);
448	mii_mediachg(mii);
449
450	return(0);
451}
452
453static void ste_ifmedia_sts(ifp, ifmr)
454	struct ifnet		*ifp;
455	struct ifmediareq	*ifmr;
456{
457	struct ste_softc	*sc;
458	struct mii_data		*mii;
459
460	sc = ifp->if_softc;
461	mii = device_get_softc(sc->ste_miibus);
462
463	mii_pollstat(mii);
464	ifmr->ifm_active = mii->mii_media_active;
465	ifmr->ifm_status = mii->mii_media_status;
466
467	return;
468}
469
470static void ste_wait(sc)
471	struct ste_softc		*sc;
472{
473	register int		i;
474
475	for (i = 0; i < STE_TIMEOUT; i++) {
476		if (!(CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_DMA_HALTINPROG))
477			break;
478	}
479
480	if (i == STE_TIMEOUT)
481		printf("ste%d: command never completed!\n", sc->ste_unit);
482
483	return;
484}
485
486/*
487 * The EEPROM is slow: give it time to come ready after issuing
488 * it a command.
489 */
490static int ste_eeprom_wait(sc)
491	struct ste_softc		*sc;
492{
493	int			i;
494
495	DELAY(1000);
496
497	for (i = 0; i < 100; i++) {
498		if (CSR_READ_2(sc, STE_EEPROM_CTL) & STE_EECTL_BUSY)
499			DELAY(1000);
500		else
501			break;
502	}
503
504	if (i == 100) {
505		printf("ste%d: eeprom failed to come ready\n", sc->ste_unit);
506		return(1);
507	}
508
509	return(0);
510}
511
512/*
513 * Read a sequence of words from the EEPROM. Note that ethernet address
514 * data is stored in the EEPROM in network byte order.
515 */
516static int ste_read_eeprom(sc, dest, off, cnt, swap)
517	struct ste_softc		*sc;
518	caddr_t			dest;
519	int			off;
520	int			cnt;
521	int			swap;
522{
523	int			err = 0, i;
524	u_int16_t		word = 0, *ptr;
525
526	if (ste_eeprom_wait(sc))
527		return(1);
528
529	for (i = 0; i < cnt; i++) {
530		CSR_WRITE_2(sc, STE_EEPROM_CTL, STE_EEOPCODE_READ | (off + i));
531		err = ste_eeprom_wait(sc);
532		if (err)
533			break;
534		word = CSR_READ_2(sc, STE_EEPROM_DATA);
535		ptr = (u_int16_t *)(dest + (i * 2));
536		if (swap)
537			*ptr = ntohs(word);
538		else
539			*ptr = word;
540	}
541
542	return(err ? 1 : 0);
543}
544
545static u_int8_t ste_calchash(addr)
546	caddr_t			addr;
547{
548
549	u_int32_t		crc, carry;
550	int			i, j;
551	u_int8_t		c;
552
553	/* Compute CRC for the address value. */
554	crc = 0xFFFFFFFF; /* initial value */
555
556	for (i = 0; i < 6; i++) {
557		c = *(addr + i);
558		for (j = 0; j < 8; j++) {
559			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
560			crc <<= 1;
561			c >>= 1;
562			if (carry)
563				crc = (crc ^ 0x04c11db6) | carry;
564		}
565	}
566
567	/* return the filter bit position */
568	return(crc & 0x0000003F);
569}
570
571static void ste_setmulti(sc)
572	struct ste_softc	*sc;
573{
574	struct ifnet		*ifp;
575	int			h = 0;
576	u_int32_t		hashes[2] = { 0, 0 };
577	struct ifmultiaddr	*ifma;
578
579	ifp = &sc->arpcom.ac_if;
580	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
581		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI);
582		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH);
583		return;
584	}
585
586	/* first, zot all the existing hash bits */
587	CSR_WRITE_4(sc, STE_MAR0, 0);
588	CSR_WRITE_4(sc, STE_MAR1, 0);
589
590	/* now program new ones */
591	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
592	    ifma = ifma->ifma_link.le_next) {
593		if (ifma->ifma_addr->sa_family != AF_LINK)
594			continue;
595		h = ste_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
596		if (h < 32)
597			hashes[0] |= (1 << h);
598		else
599			hashes[1] |= (1 << (h - 32));
600	}
601
602	CSR_WRITE_4(sc, STE_MAR0, hashes[0]);
603	CSR_WRITE_4(sc, STE_MAR1, hashes[1]);
604	STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI);
605	STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH);
606
607	return;
608}
609
610static void ste_intr(xsc)
611	void			*xsc;
612{
613	struct ste_softc	*sc;
614	struct ifnet		*ifp;
615	u_int16_t		status;
616
617	sc = xsc;
618	ifp = &sc->arpcom.ac_if;
619
620	/* See if this is really our interrupt. */
621	if (!(CSR_READ_2(sc, STE_ISR) & STE_ISR_INTLATCH))
622		return;
623
624	for (;;) {
625		status = CSR_READ_2(sc, STE_ISR_ACK);
626
627		if (!(status & STE_INTRS))
628			break;
629
630		if (status & STE_ISR_RX_DMADONE)
631			ste_rxeof(sc);
632
633		if (status & STE_ISR_TX_DMADONE)
634			ste_txeof(sc);
635
636		if (status & STE_ISR_TX_DONE)
637			ste_txeoc(sc);
638
639		if (status & STE_ISR_STATS_OFLOW) {
640			untimeout(ste_stats_update, sc, sc->ste_stat_ch);
641			ste_stats_update(sc);
642		}
643
644		if (status & STE_ISR_HOSTERR) {
645			ste_reset(sc);
646			ste_init(sc);
647		}
648	}
649
650	/* Re-enable interrupts */
651	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
652
653	if (ifp->if_snd.ifq_head != NULL)
654		ste_start(ifp);
655
656	return;
657}
658
659/*
660 * A frame has been uploaded: pass the resulting mbuf chain up to
661 * the higher level protocols.
662 */
663static void ste_rxeof(sc)
664	struct ste_softc		*sc;
665{
666        struct ether_header	*eh;
667        struct mbuf		*m;
668        struct ifnet		*ifp;
669	struct ste_chain_onefrag	*cur_rx;
670	int			total_len = 0;
671	u_int32_t		rxstat;
672
673	ifp = &sc->arpcom.ac_if;
674
675again:
676
677	while((rxstat = sc->ste_cdata.ste_rx_head->ste_ptr->ste_status)) {
678		cur_rx = sc->ste_cdata.ste_rx_head;
679		sc->ste_cdata.ste_rx_head = cur_rx->ste_next;
680
681		/*
682		 * If an error occurs, update stats, clear the
683		 * status word and leave the mbuf cluster in place:
684		 * it should simply get re-used next time this descriptor
685	 	 * comes up in the ring.
686		 */
687		if (rxstat & STE_RXSTAT_FRAME_ERR) {
688			ifp->if_ierrors++;
689			cur_rx->ste_ptr->ste_status = 0;
690			continue;
691		}
692
693		/*
694		 * If there error bit was not set, the upload complete
695		 * bit should be set which means we have a valid packet.
696		 * If not, something truly strange has happened.
697		 */
698		if (!(rxstat & STE_RXSTAT_DMADONE)) {
699			printf("ste%d: bad receive status -- packet dropped",
700							sc->ste_unit);
701			ifp->if_ierrors++;
702			cur_rx->ste_ptr->ste_status = 0;
703			continue;
704		}
705
706		/* No errors; receive the packet. */
707		m = cur_rx->ste_mbuf;
708		total_len = cur_rx->ste_ptr->ste_status & STE_RXSTAT_FRAMELEN;
709
710		/*
711		 * Try to conjure up a new mbuf cluster. If that
712		 * fails, it means we have an out of memory condition and
713		 * should leave the buffer in place and continue. This will
714		 * result in a lost packet, but there's little else we
715		 * can do in this situation.
716		 */
717		if (ste_newbuf(sc, cur_rx, NULL) == ENOBUFS) {
718			ifp->if_ierrors++;
719			cur_rx->ste_ptr->ste_status = 0;
720			continue;
721		}
722
723		ifp->if_ipackets++;
724		eh = mtod(m, struct ether_header *);
725		m->m_pkthdr.rcvif = ifp;
726		m->m_pkthdr.len = m->m_len = total_len;
727
728#if NBPF > 0
729		/* Handle BPF listeners. Let the BPF user see the packet. */
730		if (ifp->if_bpf)
731			bpf_mtap(ifp, m);
732#endif
733
734#ifdef BRIDGE
735		if (do_bridge) {
736			struct ifnet *bdg_ifp ;
737			bdg_ifp = bridge_in(m);
738			if (bdg_ifp != BDG_LOCAL && bdg_ifp != BDG_DROP)
739				bdg_forward(&m, bdg_ifp);
740			if (((bdg_ifp != BDG_LOCAL) && (bdg_ifp != BDG_BCAST) &&
741			    (bdg_ifp != BDG_MCAST)) || bdg_ifp == BDG_DROP) {
742				m_freem(m);
743				continue;
744			}
745		}
746#endif
747
748#if NBPF > 0
749		/*
750		 * Don't pass packet up to the ether_input() layer unless it's
751		 * a broadcast packet, multicast packet, matches our ethernet
752		 * address or the interface is in promiscuous mode.
753		 */
754		if (ifp->if_bpf) {
755			if (ifp->if_flags & IFF_PROMISC &&
756			    (bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
757			    ETHER_ADDR_LEN) && (eh->ether_dhost[0] & 1) == 0)){
758				m_freem(m);
759				continue;
760			}
761		}
762#endif
763
764		/* Remove header from mbuf and pass it on. */
765		m_adj(m, sizeof(struct ether_header));
766		ether_input(ifp, eh, m);
767	}
768
769	/*
770	 * Handle the 'end of channel' condition. When the upload
771	 * engine hits the end of the RX ring, it will stall. This
772	 * is our cue to flush the RX ring, reload the uplist pointer
773	 * register and unstall the engine.
774	 * XXX This is actually a little goofy. With the ThunderLAN
775	 * chip, you get an interrupt when the receiver hits the end
776	 * of the receive ring, which tells you exactly when you
777	 * you need to reload the ring pointer. Here we have to
778	 * fake it. I'm mad at myself for not being clever enough
779	 * to avoid the use of a goto here.
780	 */
781	if (CSR_READ_4(sc, STE_RX_DMALIST_PTR) == 0 ||
782		CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_RXDMA_STOPPED) {
783		STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
784		ste_wait(sc);
785		CSR_WRITE_4(sc, STE_RX_DMALIST_PTR,
786			vtophys(&sc->ste_ldata->ste_rx_list[0]));
787		sc->ste_cdata.ste_rx_head = &sc->ste_cdata.ste_rx_chain[0];
788		STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
789		goto again;
790	}
791
792	return;
793}
794
795static void ste_txeoc(sc)
796	struct ste_softc	*sc;
797{
798	u_int8_t		txstat;
799	struct ifnet		*ifp;
800
801	ifp = &sc->arpcom.ac_if;
802
803	while ((txstat = CSR_READ_1(sc, STE_TX_STATUS)) &
804	    STE_TXSTATUS_TXDONE) {
805		if (txstat & STE_TXSTATUS_UNDERRUN ||
806		    txstat & STE_TXSTATUS_EXCESSCOLLS ||
807		    txstat & STE_TXSTATUS_RECLAIMERR) {
808			ifp->if_oerrors++;
809			printf("ste%d: transmission error: %x\n",
810			    sc->ste_unit, txstat);
811			STE_SETBIT4(sc, STE_ASICCTL, STE_ASICCTL_TX_RESET);
812
813			if (sc->ste_cdata.ste_tx_head != NULL)
814				CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
815				    vtophys(sc->ste_cdata.ste_tx_head->ste_ptr));
816			if (txstat & STE_TXSTATUS_UNDERRUN &&
817			    sc->ste_tx_thresh < STE_PACKET_SIZE) {
818				sc->ste_tx_thresh += STE_MIN_FRAMELEN;
819				printf("ste%d: tx underrun, increasing tx"
820				    " start threshold to %d bytes\n",
821				    sc->ste_unit, sc->ste_tx_thresh);
822			}
823			CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
824			CSR_WRITE_2(sc, STE_TX_RECLAIM_THRESH,
825			    (STE_PACKET_SIZE >> 4));
826		}
827		ste_init(sc);
828		CSR_WRITE_2(sc, STE_TX_STATUS, txstat);
829	}
830
831	return;
832}
833
834static void ste_txeof(sc)
835	struct ste_softc	*sc;
836{
837	struct ste_chain	*cur_tx;
838	struct ifnet		*ifp;
839
840	ifp = &sc->arpcom.ac_if;
841
842	/* Clear the timeout timer. */
843	ifp->if_timer = 0;
844
845	while(sc->ste_cdata.ste_tx_head != NULL) {
846		cur_tx = sc->ste_cdata.ste_tx_head;
847		if (!(cur_tx->ste_ptr->ste_ctl & STE_TXCTL_DMADONE))
848			break;
849		sc->ste_cdata.ste_tx_head = cur_tx->ste_next;
850
851		m_freem(cur_tx->ste_mbuf);
852		cur_tx->ste_mbuf = NULL;
853		ifp->if_opackets++;
854
855		cur_tx->ste_next = sc->ste_cdata.ste_tx_free;
856		sc->ste_cdata.ste_tx_free = cur_tx;
857	}
858
859	if (sc->ste_cdata.ste_tx_head == NULL) {
860		ifp->if_flags &= ~IFF_OACTIVE;
861		sc->ste_cdata.ste_tx_tail = NULL;
862	} else {
863		if (CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_TXDMA_STOPPED ||
864		    !CSR_READ_4(sc, STE_TX_DMALIST_PTR)) {
865			CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
866			    vtophys(sc->ste_cdata.ste_tx_head->ste_ptr));
867			CSR_WRITE_4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
868		}
869	}
870
871	return;
872}
873
874static void ste_stats_update(xsc)
875	void			*xsc;
876{
877	struct ste_softc	*sc;
878	struct ste_stats	stats;
879	struct ifnet		*ifp;
880	struct mii_data		*mii;
881	int			i, s;
882	u_int8_t		*p;
883
884	s = splimp();
885
886	sc = xsc;
887	ifp = &sc->arpcom.ac_if;
888	mii = device_get_softc(sc->ste_miibus);
889
890	p = (u_int8_t *)&stats;
891
892	for (i = 0; i < sizeof(stats); i++) {
893		*p = CSR_READ_1(sc, STE_STATS + i);
894		p++;
895	}
896
897	ifp->if_collisions += stats.ste_single_colls +
898	    stats.ste_multi_colls + stats.ste_late_colls;
899
900	mii_tick(mii);
901
902	sc->ste_stat_ch = timeout(ste_stats_update, sc, hz);
903	splx(s);
904
905	return;
906}
907
908
909/*
910 * Probe for a Sundance ST201 chip. Check the PCI vendor and device
911 * IDs against our list and return a device name if we find a match.
912 */
913static int ste_probe(dev)
914	device_t		dev;
915{
916	struct ste_type		*t;
917
918	t = ste_devs;
919
920	while(t->ste_name != NULL) {
921		if ((pci_get_vendor(dev) == t->ste_vid) &&
922		    (pci_get_device(dev) == t->ste_did)) {
923			device_set_desc(dev, t->ste_name);
924			return(0);
925		}
926		t++;
927	}
928
929	return(ENXIO);
930}
931
932/*
933 * Attach the interface. Allocate softc structures, do ifmedia
934 * setup and ethernet/BPF attach.
935 */
936static int ste_attach(dev)
937	device_t		dev;
938{
939	int			s;
940	u_int32_t		command;
941	struct ste_softc	*sc;
942	struct ifnet		*ifp;
943	int			unit, error = 0, rid;
944
945	s = splimp();
946
947	sc = device_get_softc(dev);
948	unit = device_get_unit(dev);
949	bzero(sc, sizeof(struct ste_softc));
950
951	/*
952	 * Handle power management nonsense.
953	 */
954	command = pci_read_config(dev, STE_PCI_CAPID, 4) & 0x000000FF;
955	if (command == 0x01) {
956
957		command = pci_read_config(dev, STE_PCI_PWRMGMTCTRL, 4);
958		if (command & STE_PSTATE_MASK) {
959			u_int32_t		iobase, membase, irq;
960
961			/* Save important PCI config data. */
962			iobase = pci_read_config(dev, STE_PCI_LOIO, 4);
963			membase = pci_read_config(dev, STE_PCI_LOMEM, 4);
964			irq = pci_read_config(dev, STE_PCI_INTLINE, 4);
965
966			/* Reset the power state. */
967			printf("ste%d: chip is in D%d power mode "
968			"-- setting to D0\n", unit, command & STE_PSTATE_MASK);
969			command &= 0xFFFFFFFC;
970			pci_write_config(dev, STE_PCI_PWRMGMTCTRL, command, 4);
971
972			/* Restore PCI config data. */
973			pci_write_config(dev, STE_PCI_LOIO, iobase, 4);
974			pci_write_config(dev, STE_PCI_LOMEM, membase, 4);
975			pci_write_config(dev, STE_PCI_INTLINE, irq, 4);
976		}
977	}
978
979	/*
980	 * Map control/status registers.
981	 */
982	command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4);
983	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
984	pci_write_config(dev, PCI_COMMAND_STATUS_REG, command, 4);
985	command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4);
986
987#ifdef STE_USEIOSPACE
988	if (!(command & PCIM_CMD_PORTEN)) {
989		printf("ste%d: failed to enable I/O ports!\n", unit);
990		error = ENXIO;
991		goto fail;
992	}
993#else
994	if (!(command & PCIM_CMD_MEMEN)) {
995		printf("ste%d: failed to enable memory mapping!\n", unit);
996		error = ENXIO;
997		goto fail;
998	}
999#endif
1000
1001	rid = STE_RID;
1002	sc->ste_res = bus_alloc_resource(dev, STE_RES, &rid,
1003	    0, ~0, 1, RF_ACTIVE);
1004
1005	if (sc->ste_res == NULL) {
1006		printf ("ste%d: couldn't map ports/memory\n", unit);
1007		error = ENXIO;
1008		goto fail;
1009	}
1010
1011	sc->ste_btag = rman_get_bustag(sc->ste_res);
1012	sc->ste_bhandle = rman_get_bushandle(sc->ste_res);
1013
1014	rid = 0;
1015	sc->ste_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1016	    RF_SHAREABLE | RF_ACTIVE);
1017
1018	if (sc->ste_irq == NULL) {
1019		printf("ste%d: couldn't map interrupt\n", unit);
1020		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1021		error = ENXIO;
1022		goto fail;
1023	}
1024
1025	error = bus_setup_intr(dev, sc->ste_irq, INTR_TYPE_NET,
1026	    ste_intr, sc, &sc->ste_intrhand);
1027
1028	if (error) {
1029		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1030		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1031		printf("ste%d: couldn't set up irq\n", unit);
1032		goto fail;
1033	}
1034
1035	callout_handle_init(&sc->ste_stat_ch);
1036
1037	/* Reset the adapter. */
1038	ste_reset(sc);
1039
1040	/*
1041	 * Get station address from the EEPROM.
1042	 */
1043	if (ste_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
1044	    STE_EEADDR_NODE0, 3, 0)) {
1045		printf("ste%d: failed to read station address\n", unit);
1046		free(sc, M_DEVBUF);
1047		goto fail;
1048	}
1049
1050	/*
1051	 * A Sundance chip was detected. Inform the world.
1052	 */
1053	printf("ste%d: Ethernet address: %6D\n", unit,
1054	    sc->arpcom.ac_enaddr, ":");
1055
1056	sc->ste_unit = unit;
1057
1058	/* Allocate the descriptor queues. */
1059	sc->ste_ldata = contigmalloc(sizeof(struct ste_list_data), M_DEVBUF,
1060	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1061
1062	if (sc->ste_ldata == NULL) {
1063		free(sc, M_DEVBUF);
1064		printf("ste%d: no memory for list buffers!\n", unit);
1065		goto fail;
1066	}
1067
1068	bzero(sc->ste_ldata, sizeof(struct ste_list_data));
1069
1070	/* Do MII setup. */
1071	if (mii_phy_probe(dev, &sc->ste_miibus,
1072		ste_ifmedia_upd, ste_ifmedia_sts)) {
1073		printf("ste%d: MII without any phy!\n", sc->ste_unit);
1074		bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1075		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1076		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1077		free(sc->ste_ldata, M_DEVBUF);
1078		error = ENXIO;
1079		goto fail;
1080	}
1081
1082	ifp = &sc->arpcom.ac_if;
1083	ifp->if_softc = sc;
1084	ifp->if_unit = unit;
1085	ifp->if_name = "ste";
1086	ifp->if_mtu = ETHERMTU;
1087	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1088	ifp->if_ioctl = ste_ioctl;
1089	ifp->if_output = ether_output;
1090	ifp->if_start = ste_start;
1091	ifp->if_watchdog = ste_watchdog;
1092	ifp->if_init = ste_init;
1093	ifp->if_baudrate = 10000000;
1094	ifp->if_snd.ifq_maxlen = STE_TX_LIST_CNT - 1;
1095
1096	/*
1097	 * Call MI attach routines.
1098	 */
1099
1100	if_attach(ifp);
1101	ether_ifattach(ifp);
1102
1103#if NBPF > 0
1104	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1105#endif
1106
1107fail:
1108	splx(s);
1109	return(error);
1110}
1111
1112static int ste_detach(dev)
1113	device_t		dev;
1114{
1115	struct ste_softc	*sc;
1116	struct ifnet		*ifp;
1117	int			s;
1118
1119	s = splimp();
1120
1121	sc = device_get_softc(dev);
1122	ifp = &sc->arpcom.ac_if;
1123
1124	ste_stop(sc);
1125	if_detach(ifp);
1126
1127	bus_generic_detach(dev);
1128	device_delete_child(dev, sc->ste_miibus);
1129
1130	bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1131	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1132	bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1133
1134	free(sc->ste_ldata, M_DEVBUF);
1135
1136	splx(s);
1137
1138	return(0);
1139}
1140
1141static int ste_newbuf(sc, c, m)
1142	struct ste_softc	*sc;
1143	struct ste_chain_onefrag	*c;
1144	struct mbuf		*m;
1145{
1146	struct mbuf		*m_new = NULL;
1147
1148	if (m == NULL) {
1149		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1150		if (m_new == NULL) {
1151			printf("ste%d: no memory for rx list -- "
1152			    "packet dropped\n", sc->ste_unit);
1153			return(ENOBUFS);
1154		}
1155		MCLGET(m_new, M_DONTWAIT);
1156		if (!(m_new->m_flags & M_EXT)) {
1157			printf("ste%d: no memory for rx list -- "
1158			    "packet dropped\n", sc->ste_unit);
1159			m_freem(m_new);
1160			return(ENOBUFS);
1161		}
1162		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1163	} else {
1164		m_new = m;
1165		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1166		m_new->m_data = m_new->m_ext.ext_buf;
1167	}
1168
1169	m_adj(m_new, ETHER_ALIGN);
1170
1171	c->ste_mbuf = m_new;
1172	c->ste_ptr->ste_status = 0;
1173	c->ste_ptr->ste_frag.ste_addr = vtophys(mtod(m_new, caddr_t));
1174	c->ste_ptr->ste_frag.ste_len = 1536 | STE_FRAG_LAST;
1175
1176	return(0);
1177}
1178
1179static int ste_init_rx_list(sc)
1180	struct ste_softc	*sc;
1181{
1182	struct ste_chain_data	*cd;
1183	struct ste_list_data	*ld;
1184	int			i;
1185
1186	cd = &sc->ste_cdata;
1187	ld = sc->ste_ldata;
1188
1189	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1190		cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i];
1191		if (ste_newbuf(sc, &cd->ste_rx_chain[i], NULL) == ENOBUFS)
1192			return(ENOBUFS);
1193		if (i == (STE_RX_LIST_CNT - 1)) {
1194			cd->ste_rx_chain[i].ste_next =
1195			    &cd->ste_rx_chain[0];
1196			ld->ste_rx_list[i].ste_next =
1197			    vtophys(&ld->ste_rx_list[0]);
1198		} else {
1199			cd->ste_rx_chain[i].ste_next =
1200			    &cd->ste_rx_chain[i + 1];
1201			ld->ste_rx_list[i].ste_next =
1202			    vtophys(&ld->ste_rx_list[i + 1]);
1203		}
1204
1205	}
1206
1207	cd->ste_rx_head = &cd->ste_rx_chain[0];
1208
1209	return(0);
1210}
1211
1212static void ste_init_tx_list(sc)
1213	struct ste_softc	*sc;
1214{
1215	struct ste_chain_data	*cd;
1216	struct ste_list_data	*ld;
1217	int			i;
1218
1219	cd = &sc->ste_cdata;
1220	ld = sc->ste_ldata;
1221	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1222		cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i];
1223		if (i == (STE_TX_LIST_CNT - 1))
1224			cd->ste_tx_chain[i].ste_next = NULL;
1225		else
1226			cd->ste_tx_chain[i].ste_next =
1227			    &cd->ste_tx_chain[i + 1];
1228	}
1229
1230	cd->ste_tx_free = &cd->ste_tx_chain[0];
1231	cd->ste_tx_tail = cd->ste_tx_head = NULL;
1232
1233	return;
1234}
1235
1236static void ste_init(xsc)
1237	void			*xsc;
1238{
1239	struct ste_softc	*sc;
1240	int			i, s;
1241	struct ifnet		*ifp;
1242	struct mii_data		*mii;
1243
1244	s = splimp();
1245
1246	sc = xsc;
1247	ifp = &sc->arpcom.ac_if;
1248	mii = device_get_softc(sc->ste_miibus);
1249
1250	ste_stop(sc);
1251
1252	/* Init our MAC address */
1253	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1254		CSR_WRITE_1(sc, STE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1255	}
1256
1257	/* Init RX list */
1258	if (ste_init_rx_list(sc) == ENOBUFS) {
1259		printf("ste%d: initialization failed: no "
1260		    "memory for RX buffers\n", sc->ste_unit);
1261		ste_stop(sc);
1262		splx(s);
1263		return;
1264	}
1265
1266	/* Init TX descriptors */
1267	ste_init_tx_list(sc);
1268
1269	/* Set the TX freethresh value */
1270	CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8);
1271
1272	/* Set the TX start threshold for best performance. */
1273	sc->ste_tx_thresh = STE_MIN_FRAMELEN;
1274	CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
1275
1276	/* Set the TX reclaim threshold. */
1277	CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4));
1278
1279	/* Set up the RX filter. */
1280	CSR_WRITE_1(sc, STE_RX_MODE, STE_RXMODE_UNICAST);
1281
1282	/* If we want promiscuous mode, set the allframes bit. */
1283	if (ifp->if_flags & IFF_PROMISC) {
1284		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1285	} else {
1286		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1287	}
1288
1289	/* Set capture broadcast bit to accept broadcast frames. */
1290	if (ifp->if_flags & IFF_BROADCAST) {
1291		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1292	} else {
1293		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1294	}
1295
1296	ste_setmulti(sc);
1297
1298	/* Load the address of the RX list. */
1299	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1300	ste_wait(sc);
1301	CSR_WRITE_4(sc, STE_RX_DMALIST_PTR,
1302	    vtophys(&sc->ste_ldata->ste_rx_list[0]));
1303	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1304	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1305
1306	/* Enable receiver and transmitter */
1307	CSR_WRITE_2(sc, STE_MACCTL0, 0);
1308	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE);
1309	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE);
1310
1311	/* Enable stats counters. */
1312	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE);
1313
1314	/* Enable interrupts. */
1315	CSR_WRITE_2(sc, STE_ISR, 0xFFFF);
1316	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1317
1318	mii_mediachg(mii);
1319
1320	ifp->if_flags |= IFF_RUNNING;
1321	ifp->if_flags &= ~IFF_OACTIVE;
1322
1323	splx(s);
1324
1325	sc->ste_stat_ch = timeout(ste_stats_update, sc, hz);
1326
1327	return;
1328}
1329
1330static void ste_stop(sc)
1331	struct ste_softc	*sc;
1332{
1333	int			i;
1334	struct ifnet		*ifp;
1335
1336	ifp = &sc->arpcom.ac_if;
1337
1338	untimeout(ste_stats_update, sc, sc->ste_stat_ch);
1339
1340	CSR_WRITE_2(sc, STE_IMR, 0);
1341	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_DISABLE);
1342	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_DISABLE);
1343	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_DISABLE);
1344	STE_SETBIT2(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1345	STE_SETBIT2(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1346	ste_wait(sc);
1347
1348	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1349		if (sc->ste_cdata.ste_rx_chain[i].ste_mbuf != NULL) {
1350			m_freem(sc->ste_cdata.ste_rx_chain[i].ste_mbuf);
1351			sc->ste_cdata.ste_rx_chain[i].ste_mbuf = NULL;
1352		}
1353	}
1354
1355	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1356		if (sc->ste_cdata.ste_tx_chain[i].ste_mbuf != NULL) {
1357			m_freem(sc->ste_cdata.ste_tx_chain[i].ste_mbuf);
1358			sc->ste_cdata.ste_tx_chain[i].ste_mbuf = NULL;
1359		}
1360	}
1361
1362	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1363
1364	return;
1365}
1366
1367static void ste_reset(sc)
1368	struct ste_softc	*sc;
1369{
1370	int			i;
1371
1372	STE_SETBIT4(sc, STE_ASICCTL,
1373	    STE_ASICCTL_GLOBAL_RESET|STE_ASICCTL_RX_RESET|
1374	    STE_ASICCTL_TX_RESET|STE_ASICCTL_DMA_RESET|
1375	    STE_ASICCTL_FIFO_RESET|STE_ASICCTL_NETWORK_RESET|
1376	    STE_ASICCTL_AUTOINIT_RESET|STE_ASICCTL_HOST_RESET|
1377	    STE_ASICCTL_EXTRESET_RESET);
1378
1379	DELAY(100000);
1380
1381	for (i = 0; i < STE_TIMEOUT; i++) {
1382		if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY))
1383			break;
1384	}
1385
1386	if (i == STE_TIMEOUT)
1387		printf("ste%d: global reset never completed\n", sc->ste_unit);
1388
1389#ifdef foo
1390	STE_SETBIT4(sc, STE_ASICCTL, STE_ASICCTL_RX_RESET);
1391	for (i = 0; i < STE_TIMEOUT; i++) {
1392		if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RX_RESET))
1393			break;
1394	}
1395
1396	if (i == STE_TIMEOUT)
1397		printf("ste%d: RX reset never completed\n", sc->ste_unit);
1398
1399	DELAY(100000);
1400
1401	STE_SETBIT4(sc, STE_ASICCTL, STE_ASICCTL_TX_RESET);
1402	for (i = 0; i < STE_TIMEOUT; i++) {
1403		if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_TX_RESET))
1404			break;
1405	}
1406
1407	if (i == STE_TIMEOUT)
1408		printf("ste%d: TX reset never completed\n", sc->ste_unit);
1409
1410	DELAY(100000);
1411#endif
1412
1413	return;
1414}
1415
1416static int ste_ioctl(ifp, command, data)
1417	struct ifnet		*ifp;
1418	u_long			command;
1419	caddr_t			data;
1420{
1421	struct ste_softc	*sc;
1422	struct ifreq		*ifr;
1423	struct mii_data		*mii;
1424	int			error = 0, s;
1425
1426	s = splimp();
1427
1428	sc = ifp->if_softc;
1429	ifr = (struct ifreq *)data;
1430
1431	switch(command) {
1432	case SIOCSIFADDR:
1433	case SIOCGIFADDR:
1434	case SIOCSIFMTU:
1435		error = ether_ioctl(ifp, command, data);
1436		break;
1437	case SIOCSIFFLAGS:
1438		if (ifp->if_flags & IFF_UP) {
1439			ste_init(sc);
1440		} else {
1441			if (ifp->if_flags & IFF_RUNNING)
1442				ste_stop(sc);
1443		}
1444		error = 0;
1445		break;
1446	case SIOCADDMULTI:
1447	case SIOCDELMULTI:
1448		ste_setmulti(sc);
1449		error = 0;
1450		break;
1451	case SIOCGIFMEDIA:
1452	case SIOCSIFMEDIA:
1453		mii = device_get_softc(sc->ste_miibus);
1454		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1455		break;
1456	default:
1457		error = EINVAL;
1458		break;
1459	}
1460
1461	splx(s);
1462
1463	return(error);
1464}
1465
1466static int ste_encap(sc, c, m_head)
1467	struct ste_softc	*sc;
1468	struct ste_chain	*c;
1469	struct mbuf		*m_head;
1470{
1471	int			frag = 0;
1472	struct ste_frag		*f = NULL;
1473	int			total_len;
1474	struct mbuf		*m;
1475
1476	m = m_head;
1477	total_len = 0;
1478
1479	for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1480		if (m->m_len != 0) {
1481			if (frag == STE_MAXFRAGS)
1482				break;
1483			total_len += m->m_len;
1484			f = &c->ste_ptr->ste_frags[frag];
1485			f->ste_addr = vtophys(mtod(m, vm_offset_t));
1486			f->ste_len = m->m_len;
1487			frag++;
1488		}
1489	}
1490
1491	if (m != NULL) {
1492		struct mbuf		*m_new = NULL;
1493
1494		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1495		if (m_new == NULL) {
1496			printf("ste%d: no memory for "
1497			   "tx list", sc->ste_unit);
1498			return(1);
1499		}
1500		if (m_head->m_pkthdr.len > MHLEN) {
1501			MCLGET(m_new, M_DONTWAIT);
1502			if (!(m_new->m_flags & M_EXT)) {
1503				m_freem(m_new);
1504				printf("ste%d: no memory for "
1505			   	    "tx list", sc->ste_unit);
1506				return(1);
1507			}
1508		}
1509		m_copydata(m_head, 0, m_head->m_pkthdr.len,
1510		    mtod(m_new, caddr_t));
1511		m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1512		m_freem(m_head);
1513		m_head = m_new;
1514		f = &c->ste_ptr->ste_frags[0];
1515		f->ste_addr = vtophys(mtod(m_new, caddr_t));
1516		f->ste_len = total_len = m_new->m_len;
1517		frag = 1;
1518	}
1519
1520	c->ste_mbuf = m_head;
1521	c->ste_ptr->ste_frags[frag - 1].ste_len |= STE_FRAG_LAST;
1522	c->ste_ptr->ste_ctl = total_len;
1523	c->ste_ptr->ste_next = 0;
1524
1525	return(0);
1526}
1527
1528static void ste_start(ifp)
1529	struct ifnet		*ifp;
1530{
1531	struct ste_softc	*sc;
1532	struct mbuf		*m_head = NULL;
1533	struct ste_chain	*prev = NULL, *cur_tx = NULL, *start_tx;
1534
1535	sc = ifp->if_softc;
1536
1537	if (sc->ste_cdata.ste_tx_free == NULL) {
1538		ifp->if_flags |= IFF_OACTIVE;
1539		return;
1540	}
1541
1542	start_tx = sc->ste_cdata.ste_tx_free;
1543
1544	while(sc->ste_cdata.ste_tx_free != NULL) {
1545		IF_DEQUEUE(&ifp->if_snd, m_head);
1546		if (m_head == NULL)
1547			break;
1548
1549		cur_tx = sc->ste_cdata.ste_tx_free;
1550		sc->ste_cdata.ste_tx_free = cur_tx->ste_next;
1551
1552		cur_tx->ste_next = NULL;
1553
1554		ste_encap(sc, cur_tx, m_head);
1555
1556		if (prev != NULL) {
1557			prev->ste_next = cur_tx;
1558			prev->ste_ptr->ste_next = vtophys(cur_tx->ste_ptr);
1559		}
1560		prev = cur_tx;
1561
1562#if NBPF > 0
1563		/*
1564		 * If there's a BPF listener, bounce a copt of this frame
1565		 * to him.
1566	 	 */
1567		if (ifp->if_bpf)
1568			bpf_mtap(ifp, cur_tx->ste_mbuf);
1569#endif
1570	}
1571
1572	if (cur_tx == NULL)
1573		return;
1574
1575	cur_tx->ste_ptr->ste_ctl |= STE_TXCTL_DMAINTR;
1576
1577	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1578	ste_wait(sc);
1579
1580	if (sc->ste_cdata.ste_tx_head != NULL) {
1581		sc->ste_cdata.ste_tx_tail->ste_next = start_tx;
1582		sc->ste_cdata.ste_tx_tail->ste_ptr->ste_next =
1583		    vtophys(start_tx->ste_ptr);
1584		sc->ste_cdata.ste_tx_tail->ste_ptr->ste_ctl &=
1585		    ~STE_TXCTL_DMAINTR;
1586		sc->ste_cdata.ste_tx_tail = cur_tx;
1587	} else {
1588		sc->ste_cdata.ste_tx_head = start_tx;
1589		sc->ste_cdata.ste_tx_tail = cur_tx;
1590	}
1591
1592	if (!CSR_READ_4(sc, STE_TX_DMALIST_PTR))
1593		CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
1594		    vtophys(start_tx->ste_ptr));
1595	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1596
1597	ifp->if_timer = 5;
1598
1599	return;
1600}
1601
1602static void ste_watchdog(ifp)
1603	struct ifnet		*ifp;
1604{
1605	struct ste_softc	*sc;
1606
1607	sc = ifp->if_softc;
1608
1609	ifp->if_oerrors++;
1610	printf("ste%d: watchdog timeout\n", sc->ste_unit);
1611
1612#ifdef foo
1613	if (sc->ste_pinfo != NULL) {
1614		if (!(ste_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
1615			printf("ste%d: no carrier - transceiver "
1616			    "cable problem?\n", sc->ste_unit);
1617	}
1618#endif
1619
1620	ste_txeoc(sc);
1621	ste_txeof(sc);
1622	ste_rxeof(sc);
1623	ste_reset(sc);
1624	ste_init(sc);
1625
1626	if (ifp->if_snd.ifq_head != NULL)
1627		ste_start(ifp);
1628
1629	return;
1630}
1631
1632static void ste_shutdown(dev)
1633	device_t		dev;
1634{
1635	struct ste_softc	*sc;
1636
1637	sc = device_get_softc(dev);
1638
1639	ste_stop(sc);
1640
1641	return;
1642}
1643