if_ste.c revision 106696
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 106696 2002-11-09 12:55:07Z alfred $
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 106696 2002-11-09 12:55:07Z alfred $";
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 one PHY since this chip reports multiple
930	 * Note on the DFE-550 the PHY is at 1 on the DFE-580
931	 * it is at 0 & 1.  It is rev 0x12.
932	 */
933	if (pci_get_vendor(dev) == DL_VENDORID &&
934	    pci_get_device(dev) == DL_DEVICEID_550TX &&
935	    pci_get_revid(dev) == 0x12 )
936		sc->ste_one_phy = 1;
937
938	mtx_init(&sc->ste_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
939	    MTX_DEF | MTX_RECURSE);
940	STE_LOCK(sc);
941
942	/*
943	 * Handle power management nonsense.
944	 */
945	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
946		u_int32_t		iobase, membase, irq;
947
948		/* Save important PCI config data. */
949		iobase = pci_read_config(dev, STE_PCI_LOIO, 4);
950		membase = pci_read_config(dev, STE_PCI_LOMEM, 4);
951		irq = pci_read_config(dev, STE_PCI_INTLINE, 4);
952
953		/* Reset the power state. */
954		printf("ste%d: chip is in D%d power mode "
955		    "-- setting to D0\n", unit,
956		    pci_get_powerstate(dev));
957		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
958
959		/* Restore PCI config data. */
960		pci_write_config(dev, STE_PCI_LOIO, iobase, 4);
961		pci_write_config(dev, STE_PCI_LOMEM, membase, 4);
962		pci_write_config(dev, STE_PCI_INTLINE, irq, 4);
963	}
964
965	/*
966	 * Map control/status registers.
967	 */
968	pci_enable_busmaster(dev);
969	pci_enable_io(dev, SYS_RES_IOPORT);
970	pci_enable_io(dev, SYS_RES_MEMORY);
971	command = pci_read_config(dev, PCIR_COMMAND, 4);
972
973#ifdef STE_USEIOSPACE
974	if (!(command & PCIM_CMD_PORTEN)) {
975		printf("ste%d: failed to enable I/O ports!\n", unit);
976		error = ENXIO;
977		goto fail;
978	}
979#else
980	if (!(command & PCIM_CMD_MEMEN)) {
981		printf("ste%d: failed to enable memory mapping!\n", unit);
982		error = ENXIO;
983		goto fail;
984	}
985#endif
986
987	rid = STE_RID;
988	sc->ste_res = bus_alloc_resource(dev, STE_RES, &rid,
989	    0, ~0, 1, RF_ACTIVE);
990
991	if (sc->ste_res == NULL) {
992		printf ("ste%d: couldn't map ports/memory\n", unit);
993		error = ENXIO;
994		goto fail;
995	}
996
997	sc->ste_btag = rman_get_bustag(sc->ste_res);
998	sc->ste_bhandle = rman_get_bushandle(sc->ste_res);
999
1000	rid = 0;
1001	sc->ste_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1002	    RF_SHAREABLE | RF_ACTIVE);
1003
1004	if (sc->ste_irq == NULL) {
1005		printf("ste%d: couldn't map interrupt\n", unit);
1006		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1007		error = ENXIO;
1008		goto fail;
1009	}
1010
1011	error = bus_setup_intr(dev, sc->ste_irq, INTR_TYPE_NET,
1012	    ste_intr, sc, &sc->ste_intrhand);
1013
1014	if (error) {
1015		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1016		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1017		printf("ste%d: couldn't set up irq\n", unit);
1018		goto fail;
1019	}
1020
1021	callout_handle_init(&sc->ste_stat_ch);
1022
1023	/* Reset the adapter. */
1024	ste_reset(sc);
1025
1026	/*
1027	 * Get station address from the EEPROM.
1028	 */
1029	if (ste_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
1030	    STE_EEADDR_NODE0, 3, 0)) {
1031		printf("ste%d: failed to read station address\n", unit);
1032		bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1033		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1034		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1035		error = ENXIO;;
1036		goto fail;
1037	}
1038
1039	/*
1040	 * A Sundance chip was detected. Inform the world.
1041	 */
1042	printf("ste%d: Ethernet address: %6D\n", unit,
1043	    sc->arpcom.ac_enaddr, ":");
1044
1045	sc->ste_unit = unit;
1046
1047	/* Allocate the descriptor queues. */
1048	sc->ste_ldata = contigmalloc(sizeof(struct ste_list_data), M_DEVBUF,
1049	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1050
1051	if (sc->ste_ldata == NULL) {
1052		printf("ste%d: no memory for list buffers!\n", unit);
1053		bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1054		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1055		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1056		error = ENXIO;
1057		goto fail;
1058	}
1059
1060	bzero(sc->ste_ldata, sizeof(struct ste_list_data));
1061
1062	/* Do MII setup. */
1063	if (mii_phy_probe(dev, &sc->ste_miibus,
1064		ste_ifmedia_upd, ste_ifmedia_sts)) {
1065		printf("ste%d: MII without any phy!\n", sc->ste_unit);
1066		bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1067		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1068		bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1069		contigfree(sc->ste_ldata,
1070		    sizeof(struct ste_list_data), M_DEVBUF);
1071		error = ENXIO;
1072		goto fail;
1073	}
1074
1075	ifp = &sc->arpcom.ac_if;
1076	ifp->if_softc = sc;
1077	ifp->if_unit = unit;
1078	ifp->if_name = "ste";
1079	ifp->if_mtu = ETHERMTU;
1080	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1081	ifp->if_ioctl = ste_ioctl;
1082	ifp->if_output = ether_output;
1083	ifp->if_start = ste_start;
1084	ifp->if_watchdog = ste_watchdog;
1085	ifp->if_init = ste_init;
1086	ifp->if_baudrate = 10000000;
1087	ifp->if_snd.ifq_maxlen = STE_TX_LIST_CNT - 1;
1088
1089	sc->ste_tx_thresh = STE_TXSTART_THRESH;
1090
1091	/*
1092	 * Call MI attach routine.
1093	 */
1094	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1095
1096	/*
1097	 * Tell the upper layer(s) we support long frames.
1098	 */
1099	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1100
1101	STE_UNLOCK(sc);
1102	return(0);
1103
1104fail:
1105	STE_UNLOCK(sc);
1106	mtx_destroy(&sc->ste_mtx);
1107	return(error);
1108}
1109
1110static int
1111ste_detach(dev)
1112	device_t		dev;
1113{
1114	struct ste_softc	*sc;
1115	struct ifnet		*ifp;
1116
1117	sc = device_get_softc(dev);
1118	STE_LOCK(sc);
1119	ifp = &sc->arpcom.ac_if;
1120
1121	ste_stop(sc);
1122	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1123
1124	bus_generic_detach(dev);
1125	device_delete_child(dev, sc->ste_miibus);
1126
1127	bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1128	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1129	bus_release_resource(dev, STE_RES, STE_RID, sc->ste_res);
1130
1131	contigfree(sc->ste_ldata, sizeof(struct ste_list_data), M_DEVBUF);
1132
1133	STE_UNLOCK(sc);
1134	mtx_destroy(&sc->ste_mtx);
1135
1136	return(0);
1137}
1138
1139static int
1140ste_newbuf(sc, c, m)
1141	struct ste_softc	*sc;
1142	struct ste_chain_onefrag	*c;
1143	struct mbuf		*m;
1144{
1145	struct mbuf		*m_new = NULL;
1146
1147	if (m == NULL) {
1148		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1149		if (m_new == NULL)
1150			return(ENOBUFS);
1151		MCLGET(m_new, M_DONTWAIT);
1152		if (!(m_new->m_flags & M_EXT)) {
1153			m_freem(m_new);
1154			return(ENOBUFS);
1155		}
1156		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1157	} else {
1158		m_new = m;
1159		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1160		m_new->m_data = m_new->m_ext.ext_buf;
1161	}
1162
1163	m_adj(m_new, ETHER_ALIGN);
1164
1165	c->ste_mbuf = m_new;
1166	c->ste_ptr->ste_status = 0;
1167	c->ste_ptr->ste_frag.ste_addr = vtophys(mtod(m_new, caddr_t));
1168	c->ste_ptr->ste_frag.ste_len = (1536 + EVL_ENCAPLEN) | STE_FRAG_LAST;
1169
1170	return(0);
1171}
1172
1173static int
1174ste_init_rx_list(sc)
1175	struct ste_softc	*sc;
1176{
1177	struct ste_chain_data	*cd;
1178	struct ste_list_data	*ld;
1179	int			i;
1180
1181	cd = &sc->ste_cdata;
1182	ld = sc->ste_ldata;
1183
1184	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1185		cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i];
1186		if (ste_newbuf(sc, &cd->ste_rx_chain[i], NULL) == ENOBUFS)
1187			return(ENOBUFS);
1188		if (i == (STE_RX_LIST_CNT - 1)) {
1189			cd->ste_rx_chain[i].ste_next =
1190			    &cd->ste_rx_chain[0];
1191			ld->ste_rx_list[i].ste_next =
1192			    vtophys(&ld->ste_rx_list[0]);
1193		} else {
1194			cd->ste_rx_chain[i].ste_next =
1195			    &cd->ste_rx_chain[i + 1];
1196			ld->ste_rx_list[i].ste_next =
1197			    vtophys(&ld->ste_rx_list[i + 1]);
1198		}
1199		ld->ste_rx_list[i].ste_status = 0;
1200	}
1201
1202	cd->ste_rx_head = &cd->ste_rx_chain[0];
1203
1204	return(0);
1205}
1206
1207static void
1208ste_init_tx_list(sc)
1209	struct ste_softc	*sc;
1210{
1211	struct ste_chain_data	*cd;
1212	struct ste_list_data	*ld;
1213	int			i;
1214
1215	cd = &sc->ste_cdata;
1216	ld = sc->ste_ldata;
1217	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1218		cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i];
1219		cd->ste_tx_chain[i].ste_ptr->ste_next = 0;
1220		cd->ste_tx_chain[i].ste_ptr->ste_ctl  = 0;
1221		cd->ste_tx_chain[i].ste_phys = vtophys(&ld->ste_tx_list[i]);
1222		if (i == (STE_TX_LIST_CNT - 1))
1223			cd->ste_tx_chain[i].ste_next =
1224			    &cd->ste_tx_chain[0];
1225		else
1226			cd->ste_tx_chain[i].ste_next =
1227			    &cd->ste_tx_chain[i + 1];
1228		if (i == 0)
1229			cd->ste_tx_chain[i].ste_prev =
1230			     &cd->ste_tx_chain[STE_TX_LIST_CNT - 1];
1231		else
1232			cd->ste_tx_chain[i].ste_prev =
1233			     &cd->ste_tx_chain[i - 1];
1234	}
1235
1236	cd->ste_tx_prod = 0;
1237	cd->ste_tx_cons = 0;
1238	cd->ste_tx_cnt = 0;
1239
1240	return;
1241}
1242
1243static void
1244ste_init(xsc)
1245	void			*xsc;
1246{
1247	struct ste_softc	*sc;
1248	int			i;
1249	struct ifnet		*ifp;
1250	struct mii_data		*mii;
1251
1252	sc = xsc;
1253	STE_LOCK(sc);
1254	ifp = &sc->arpcom.ac_if;
1255	mii = device_get_softc(sc->ste_miibus);
1256
1257	ste_stop(sc);
1258
1259	/* Init our MAC address */
1260	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1261		CSR_WRITE_1(sc, STE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1262	}
1263
1264	/* Init RX list */
1265	if (ste_init_rx_list(sc) == ENOBUFS) {
1266		printf("ste%d: initialization failed: no "
1267		    "memory for RX buffers\n", sc->ste_unit);
1268		ste_stop(sc);
1269		STE_UNLOCK(sc);
1270		return;
1271	}
1272
1273	/* Set RX polling interval */
1274	CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 1);
1275
1276	/* Init TX descriptors */
1277	ste_init_tx_list(sc);
1278
1279	/* Set the TX freethresh value */
1280	CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8);
1281
1282	/* Set the TX start threshold for best performance. */
1283	CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
1284
1285	/* Set the TX reclaim threshold. */
1286	CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4));
1287
1288	/* Set up the RX filter. */
1289	CSR_WRITE_1(sc, STE_RX_MODE, STE_RXMODE_UNICAST);
1290
1291	/* If we want promiscuous mode, set the allframes bit. */
1292	if (ifp->if_flags & IFF_PROMISC) {
1293		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1294	} else {
1295		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1296	}
1297
1298	/* Set capture broadcast bit to accept broadcast frames. */
1299	if (ifp->if_flags & IFF_BROADCAST) {
1300		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1301	} else {
1302		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1303	}
1304
1305	ste_setmulti(sc);
1306
1307	/* Load the address of the RX list. */
1308	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1309	ste_wait(sc);
1310	CSR_WRITE_4(sc, STE_RX_DMALIST_PTR,
1311	    vtophys(&sc->ste_ldata->ste_rx_list[0]));
1312	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1313	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1314
1315	/* Set TX polling interval (defer until we TX first packet */
1316	CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
1317
1318	/* Load address of the TX list */
1319	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1320	ste_wait(sc);
1321	CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
1322	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1323	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1324	ste_wait(sc);
1325	sc->ste_tx_prev_idx=-1;
1326
1327	/* Enable receiver and transmitter */
1328	CSR_WRITE_2(sc, STE_MACCTL0, 0);
1329	CSR_WRITE_2(sc, STE_MACCTL1, 0);
1330	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE);
1331	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE);
1332
1333	/* Enable stats counters. */
1334	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE);
1335
1336	/* Enable interrupts. */
1337	CSR_WRITE_2(sc, STE_ISR, 0xFFFF);
1338	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1339
1340	/* Accept VLAN length packets */
1341	CSR_WRITE_2(sc, STE_MAX_FRAMELEN, ETHER_MAX_LEN + EVL_ENCAPLEN);
1342
1343	ste_ifmedia_upd(ifp);
1344
1345	ifp->if_flags |= IFF_RUNNING;
1346	ifp->if_flags &= ~IFF_OACTIVE;
1347
1348	sc->ste_stat_ch = timeout(ste_stats_update, sc, hz);
1349	STE_UNLOCK(sc);
1350
1351	return;
1352}
1353
1354static void
1355ste_stop(sc)
1356	struct ste_softc	*sc;
1357{
1358	int			i;
1359	struct ifnet		*ifp;
1360
1361	STE_LOCK(sc);
1362	ifp = &sc->arpcom.ac_if;
1363
1364	untimeout(ste_stats_update, sc, sc->ste_stat_ch);
1365
1366	CSR_WRITE_2(sc, STE_IMR, 0);
1367	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_DISABLE);
1368	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_DISABLE);
1369	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_DISABLE);
1370	STE_SETBIT2(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1371	STE_SETBIT2(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1372	ste_wait(sc);
1373	/*
1374	 * Try really hard to stop the RX engine or under heavy RX
1375	 * data chip will write into de-allocated memory.
1376	 */
1377	ste_reset(sc);
1378
1379	sc->ste_link = 0;
1380
1381	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1382		if (sc->ste_cdata.ste_rx_chain[i].ste_mbuf != NULL) {
1383			m_freem(sc->ste_cdata.ste_rx_chain[i].ste_mbuf);
1384			sc->ste_cdata.ste_rx_chain[i].ste_mbuf = NULL;
1385		}
1386	}
1387
1388	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1389		if (sc->ste_cdata.ste_tx_chain[i].ste_mbuf != NULL) {
1390			m_freem(sc->ste_cdata.ste_tx_chain[i].ste_mbuf);
1391			sc->ste_cdata.ste_tx_chain[i].ste_mbuf = NULL;
1392		}
1393	}
1394
1395	bzero(sc->ste_ldata, sizeof(struct ste_list_data));
1396
1397	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1398	STE_UNLOCK(sc);
1399
1400	return;
1401}
1402
1403static void
1404ste_reset(sc)
1405	struct ste_softc	*sc;
1406{
1407	int			i;
1408
1409	STE_SETBIT4(sc, STE_ASICCTL,
1410	    STE_ASICCTL_GLOBAL_RESET|STE_ASICCTL_RX_RESET|
1411	    STE_ASICCTL_TX_RESET|STE_ASICCTL_DMA_RESET|
1412	    STE_ASICCTL_FIFO_RESET|STE_ASICCTL_NETWORK_RESET|
1413	    STE_ASICCTL_AUTOINIT_RESET|STE_ASICCTL_HOST_RESET|
1414	    STE_ASICCTL_EXTRESET_RESET);
1415
1416	DELAY(100000);
1417
1418	for (i = 0; i < STE_TIMEOUT; i++) {
1419		if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY))
1420			break;
1421	}
1422
1423	if (i == STE_TIMEOUT)
1424		printf("ste%d: global reset never completed\n", sc->ste_unit);
1425
1426	return;
1427}
1428
1429static int
1430ste_ioctl(ifp, command, data)
1431	struct ifnet		*ifp;
1432	u_long			command;
1433	caddr_t			data;
1434{
1435	struct ste_softc	*sc;
1436	struct ifreq		*ifr;
1437	struct mii_data		*mii;
1438	int			error = 0;
1439
1440	sc = ifp->if_softc;
1441	STE_LOCK(sc);
1442	ifr = (struct ifreq *)data;
1443
1444	switch(command) {
1445	case SIOCSIFADDR:
1446	case SIOCGIFADDR:
1447	case SIOCSIFMTU:
1448		error = ether_ioctl(ifp, command, data);
1449		break;
1450	case SIOCSIFFLAGS:
1451		if (ifp->if_flags & IFF_UP) {
1452			if (ifp->if_flags & IFF_RUNNING &&
1453			    ifp->if_flags & IFF_PROMISC &&
1454			    !(sc->ste_if_flags & IFF_PROMISC)) {
1455				STE_SETBIT1(sc, STE_RX_MODE,
1456				    STE_RXMODE_PROMISC);
1457			} else if (ifp->if_flags & IFF_RUNNING &&
1458			    !(ifp->if_flags & IFF_PROMISC) &&
1459			    sc->ste_if_flags & IFF_PROMISC) {
1460				STE_CLRBIT1(sc, STE_RX_MODE,
1461				    STE_RXMODE_PROMISC);
1462			}
1463			if (!(ifp->if_flags & IFF_RUNNING)) {
1464				sc->ste_tx_thresh = STE_TXSTART_THRESH;
1465				ste_init(sc);
1466			}
1467		} else {
1468			if (ifp->if_flags & IFF_RUNNING)
1469				ste_stop(sc);
1470		}
1471		sc->ste_if_flags = ifp->if_flags;
1472		error = 0;
1473		break;
1474	case SIOCADDMULTI:
1475	case SIOCDELMULTI:
1476		ste_setmulti(sc);
1477		error = 0;
1478		break;
1479	case SIOCGIFMEDIA:
1480	case SIOCSIFMEDIA:
1481		mii = device_get_softc(sc->ste_miibus);
1482		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1483		break;
1484	default:
1485		error = EINVAL;
1486		break;
1487	}
1488
1489	STE_UNLOCK(sc);
1490
1491	return(error);
1492}
1493
1494static int
1495ste_encap(sc, c, m_head)
1496	struct ste_softc	*sc;
1497	struct ste_chain	*c;
1498	struct mbuf		*m_head;
1499{
1500	int			frag = 0;
1501	struct ste_frag		*f = NULL;
1502	struct mbuf		*m;
1503	struct ste_desc		*d;
1504	int			total_len = 0;
1505
1506	d = c->ste_ptr;
1507	d->ste_ctl = 0;
1508
1509encap_retry:
1510	for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1511		if (m->m_len != 0) {
1512			if (frag == STE_MAXFRAGS)
1513				break;
1514			total_len += m->m_len;
1515			f = &d->ste_frags[frag];
1516			f->ste_addr = vtophys(mtod(m, vm_offset_t));
1517			f->ste_len = m->m_len;
1518			frag++;
1519		}
1520	}
1521
1522	if (m != NULL) {
1523		struct mbuf *mn;
1524
1525		/*
1526		 * We ran out of segments. We have to recopy this
1527		 * mbuf chain first. Bail out if we can't get the
1528		 * new buffers. Code borrowed from if_fxp.c
1529		 */
1530		MGETHDR(mn, M_DONTWAIT, MT_DATA);
1531		if (mn == NULL) {
1532			m_freem(m_head);
1533			return ENOMEM;
1534		}
1535		if (m_head->m_pkthdr.len > MHLEN) {
1536			MCLGET(mn, M_DONTWAIT);
1537			if ((mn->m_flags & M_EXT) == 0) {
1538				m_freem(mn);
1539				m_freem(m_head);
1540				return ENOMEM;
1541			}
1542		}
1543		m_copydata(m_head, 0, m_head->m_pkthdr.len,
1544		    mtod(mn, caddr_t));
1545		mn->m_pkthdr.len = mn->m_len = m_head->m_pkthdr.len;
1546		m_freem(m_head);
1547		m_head = mn;
1548		goto encap_retry;
1549	}
1550
1551	c->ste_mbuf = m_head;
1552	d->ste_frags[frag - 1].ste_len |= STE_FRAG_LAST;
1553	d->ste_ctl = 1;
1554
1555	return(0);
1556}
1557
1558static void
1559ste_start(ifp)
1560	struct ifnet		*ifp;
1561{
1562	struct ste_softc	*sc;
1563	struct mbuf		*m_head = NULL;
1564	struct ste_chain	*cur_tx = NULL;
1565	int			idx;
1566
1567	sc = ifp->if_softc;
1568	STE_LOCK(sc);
1569
1570	if (!sc->ste_link) {
1571		STE_UNLOCK(sc);
1572		return;
1573	}
1574
1575	if (ifp->if_flags & IFF_OACTIVE) {
1576		STE_UNLOCK(sc);
1577		return;
1578	}
1579
1580	idx = sc->ste_cdata.ste_tx_prod;
1581
1582	while(sc->ste_cdata.ste_tx_chain[idx].ste_mbuf == NULL) {
1583
1584		if ((STE_TX_LIST_CNT - sc->ste_cdata.ste_tx_cnt) < 3) {
1585			ifp->if_flags |= IFF_OACTIVE;
1586			break;
1587		}
1588
1589		IF_DEQUEUE(&ifp->if_snd, m_head);
1590		if (m_head == NULL)
1591			break;
1592
1593		cur_tx = &sc->ste_cdata.ste_tx_chain[idx];
1594
1595		if (ste_encap(sc, cur_tx, m_head) != 0)
1596			break;
1597
1598		cur_tx->ste_ptr->ste_next = 0;
1599
1600		if(sc->ste_tx_prev_idx < 0){
1601			cur_tx->ste_ptr->ste_ctl = STE_TXCTL_DMAINTR | 1;
1602			/* Load address of the TX list */
1603			STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1604			ste_wait(sc);
1605
1606			CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
1607			    vtophys(&sc->ste_ldata->ste_tx_list[0]));
1608
1609			/* Set TX polling interval to start TX engine */
1610			CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 64);
1611
1612			STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1613			ste_wait(sc);
1614		}else{
1615			cur_tx->ste_ptr->ste_ctl = STE_TXCTL_DMAINTR | 1;
1616			sc->ste_cdata.ste_tx_chain[
1617			    sc->ste_tx_prev_idx].ste_ptr->ste_next
1618				= cur_tx->ste_phys;
1619		}
1620
1621		sc->ste_tx_prev_idx=idx;
1622
1623		/*
1624		 * If there's a BPF listener, bounce a copy of this frame
1625		 * to him.
1626	 	 */
1627		if (ifp->if_bpf)
1628			bpf_mtap(ifp, cur_tx->ste_mbuf);
1629
1630		STE_INC(idx, STE_TX_LIST_CNT);
1631		sc->ste_cdata.ste_tx_cnt++;
1632		ifp->if_timer = 5;
1633		sc->ste_cdata.ste_tx_prod = idx;
1634	}
1635
1636	STE_UNLOCK(sc);
1637
1638	return;
1639}
1640
1641static void
1642ste_watchdog(ifp)
1643	struct ifnet		*ifp;
1644{
1645	struct ste_softc	*sc;
1646
1647	sc = ifp->if_softc;
1648	STE_LOCK(sc);
1649
1650	ifp->if_oerrors++;
1651	printf("ste%d: watchdog timeout\n", sc->ste_unit);
1652
1653	ste_txeoc(sc);
1654	ste_txeof(sc);
1655	ste_rxeof(sc);
1656	ste_reset(sc);
1657	ste_init(sc);
1658
1659	if (ifp->if_snd.ifq_head != NULL)
1660		ste_start(ifp);
1661	STE_UNLOCK(sc);
1662
1663	return;
1664}
1665
1666static void
1667ste_shutdown(dev)
1668	device_t		dev;
1669{
1670	struct ste_softc	*sc;
1671
1672	sc = device_get_softc(dev);
1673
1674	ste_stop(sc);
1675
1676	return;
1677}
1678