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