if_cs.c revision 140926
1139749Simp/*-
237785Smsmith * Copyright (c) 1997,1998 Maxim Bolotin and Oleg Sharoiko.
337785Smsmith * All rights reserved.
437785Smsmith *
537785Smsmith * Redistribution and use in source and binary forms, with or without
637785Smsmith * modification, are permitted provided that the following conditions
737785Smsmith * are met:
837785Smsmith * 1. Redistributions of source code must retain the above copyright
937785Smsmith *    notice unmodified, this list of conditions, and the following
1037785Smsmith *    disclaimer.
1137785Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1237785Smsmith *    notice, this list of conditions and the following disclaimer in the
1337785Smsmith *    documentation and/or other materials provided with the distribution.
1437785Smsmith *
1537785Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1637785Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1737785Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1837785Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1937785Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2037785Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2137785Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2237785Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2337785Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2437785Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2537785Smsmith * SUCH DAMAGE.
2637785Smsmith *
2737785Smsmith */
2837785Smsmith
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/cs/if_cs.c 140926 2005-01-28 06:35:39Z imp $");
31119418Sobrien
3237785Smsmith/*
3337785Smsmith *
3437785Smsmith * Device driver for Crystal Semiconductor CS8920 based ethernet
3537785Smsmith *   adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
3637785Smsmith */
3737785Smsmith
3858816Simp/*
3958816Simp#define	 CS_DEBUG
4058816Simp */
4137785Smsmith
4237785Smsmith#include <sys/param.h>
4337785Smsmith#include <sys/systm.h>
4437785Smsmith#include <sys/malloc.h>
4558816Simp#include <sys/mbuf.h>
4658816Simp#include <sys/socket.h>
4737785Smsmith#include <sys/sockio.h>
4837785Smsmith#include <sys/kernel.h>
4938592Smsmith#include <sys/sysctl.h>
5037785Smsmith#include <sys/syslog.h>
5137785Smsmith
5258816Simp#include <sys/module.h>
5358816Simp#include <sys/bus.h>
5458816Simp#include <machine/bus.h>
5558816Simp#include <sys/rman.h>
5658816Simp#include <machine/resource.h>
5758816Simp
5837785Smsmith#include <net/if.h>
5937785Smsmith#include <net/if_arp.h>
6037785Smsmith#include <net/if_media.h>
6137785Smsmith#include <net/ethernet.h>
6237785Smsmith#include <net/bpf.h>
6337785Smsmith
6472940Simp#include <dev/cs/if_csvar.h>
6558816Simp#include <dev/cs/if_csreg.h>
6637785Smsmith
6737785Smsmith#ifdef  CS_USE_64K_DMA
6837785Smsmith#define CS_DMA_BUFFER_SIZE 65536
6937785Smsmith#else
7037785Smsmith#define CS_DMA_BUFFER_SIZE 16384
7137785Smsmith#endif
7237785Smsmith
73122024Simpstatic void	cs_init(void *);
74122024Simpstatic int	cs_ioctl(struct ifnet *, u_long, caddr_t);
75122024Simpstatic void	cs_start(struct ifnet *);
76122024Simpstatic void	cs_stop(struct cs_softc *);
77122024Simpstatic void	cs_reset(struct cs_softc *);
78122024Simpstatic void	cs_watchdog(struct ifnet *);
7938592Smsmith
80122024Simpstatic int	cs_mediachange(struct ifnet *);
81122024Simpstatic void	cs_mediastatus(struct ifnet *, struct ifmediareq *);
82122024Simpstatic int      cs_mediaset(struct cs_softc *, int);
8358816Simp
8437785Smsmithstatic void	cs_write_mbufs(struct cs_softc*, struct mbuf*);
8537785Smsmithstatic void	cs_xmit_buf(struct cs_softc*);
8637785Smsmithstatic int	cs_get_packet(struct cs_softc*);
8737785Smsmithstatic void	cs_setmode(struct cs_softc*);
8837785Smsmith
89122024Simpstatic int	get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *);
90122024Simpstatic int	get_eeprom_cksum(int, int, uint16_t *);
9137785Smsmithstatic int	wait_eeprom_ready( struct cs_softc *);
9237785Smsmithstatic void	control_dc_dc( struct cs_softc *, int );
9337785Smsmithstatic int	send_test_pkt( struct cs_softc * );
9437785Smsmithstatic int	enable_tp(struct cs_softc *);
9537785Smsmithstatic int	enable_aui(struct cs_softc *);
9637785Smsmithstatic int	enable_bnc(struct cs_softc *);
9737785Smsmithstatic int      cs_duplex_auto(struct cs_softc *);
9837785Smsmith
9971316Simpdevclass_t cs_devclass;
10037785Smsmith
101122024Simp/* sysctl vars */
102122024SimpSYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
103122024Simp
104122024Simpint	cs_debug = 0;
105122024SimpTUNABLE_INT("hw.cs.debug", &cs_debug);
106122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, debug, CTLFLAG_RW,
107122024Simp    &cs_debug, 0,
108122024Simp  "cs debug");
109122024Simp
110122024Simpint	cs_ignore_cksum_failure = 0;
111122024SimpTUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure);
112122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW,
113122024Simp    &cs_ignore_cksum_failure, 0,
114122024Simp  "ignore checksum errors in cs card EEPROM");
115122024Simp
116122024Simpstatic int	cs_recv_delay = 570;
117140925SimpTUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay);
118122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, "");
119122024Simp
12037785Smsmithstatic int
121122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
12237785Smsmith{
12337785Smsmith	int i;
12437785Smsmith
12537785Smsmith#ifdef CS_DEBUG
126122024Simp	printf(CS_NAME":EEPROM data from %x for %x:\n", off, len);
12737785Smsmith#endif
12837785Smsmith
129122024Simp	for (i=0; i < len; i++) {
130122024Simp		if (wait_eeprom_ready(sc) < 0)
131122024Simp			return (-1);
13237785Smsmith		/* Send command to EEPROM to read */
13372940Simp		cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
134122024Simp		if (wait_eeprom_ready(sc) < 0)
13572940Simp			return (-1);
13672940Simp		buffer[i] = cs_readreg(sc, PP_EEData);
13737785Smsmith
13837785Smsmith#ifdef CS_DEBUG
13937785Smsmith		printf("%02x %02x ",(unsigned char)buffer[i],
140122024Simp		    (unsigned char)buffer[i] >> 8);
14137785Smsmith#endif
14237785Smsmith	}
14337785Smsmith
14437785Smsmith#ifdef CS_DEBUG
14537785Smsmith	printf("\n");
14637785Smsmith#endif
14772940Simp	return (0);
14837785Smsmith}
14937785Smsmith
15037785Smsmithstatic int
151122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer)
15237785Smsmith{
153122024Simp	int i;
154122024Simp	uint16_t cksum=0;
15537785Smsmith
156122024Simp	for (i = 0; i < len; i++)
157122024Simp		cksum += buffer[i];
15837785Smsmith	cksum &= 0xffff;
15937785Smsmith	if (cksum==0)
160122024Simp		return (0);
161122024Simp	if (cs_ignore_cksum_failure) {
162122024Simp		printf(CS_NAME": checksum mismatch, ignoring\n");
163122024Simp		return (0);
164122024Simp	}
165122024Simp	return (-1);
16637785Smsmith}
16737785Smsmith
16837785Smsmithstatic int
16937785Smsmithwait_eeprom_ready(struct cs_softc *sc)
17037785Smsmith{
171122024Simp	DELAY(30000);	/* XXX should we do some checks here ? */
17237785Smsmith	return 0;
17337785Smsmith}
17437785Smsmith
17537785Smsmithstatic void
17637785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off)
17737785Smsmith{
17837785Smsmith	unsigned int self_control = HCB1_ENBL;
17937785Smsmith
18037785Smsmith	if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
18137785Smsmith		self_control |= HCB1;
18237785Smsmith	else
18337785Smsmith		self_control &= ~HCB1;
18472940Simp	cs_writereg(sc, PP_SelfCTL, self_control);
18537785Smsmith
186122024Simp	DELAY(500000);
18737785Smsmith}
18837785Smsmith
18937785Smsmith
19037785Smsmithstatic int
19137785Smsmithcs_duplex_auto(struct cs_softc *sc)
19237785Smsmith{
193104252Sbrooks        int i, error=0;
19437785Smsmith
19572940Simp	cs_writereg(sc, PP_AutoNegCTL,
19672940Simp	    RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
19772940Simp        for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
19837785Smsmith                if (i > 40000) {
199104252Sbrooks                        if_printf(&sc->arpcom.ac_if,
200104252Sbrooks                        	"full/half duplex auto negotiation timeout\n");
20137785Smsmith			error = ETIMEDOUT;
20237785Smsmith                        break;
20337785Smsmith                }
20437785Smsmith                DELAY(1000);
20537785Smsmith        }
20637785Smsmith        DELAY( 1000000 );
20737785Smsmith	return error;
20837785Smsmith}
20937785Smsmith
21037785Smsmithstatic int
21137785Smsmithenable_tp(struct cs_softc *sc)
21237785Smsmith{
21337785Smsmith
21472940Simp	cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
21537785Smsmith	control_dc_dc(sc, 0);
21637785Smsmith	DELAY( 150000 );
21737785Smsmith
21872940Simp	if ((cs_readreg(sc, PP_LineST) & LINK_OK)==0) {
219104252Sbrooks		if_printf(&sc->arpcom.ac_if, "failed to enable TP\n");
22037785Smsmith                return EINVAL;
22137785Smsmith	}
22237785Smsmith
22337785Smsmith	return 0;
22437785Smsmith}
22537785Smsmith
22637785Smsmith/*
22737785Smsmith * XXX This was rewritten from Linux driver without any tests.
22837785Smsmith */
22937785Smsmithstatic int
23037785Smsmithsend_test_pkt(struct cs_softc *sc)
23137785Smsmith{
23237785Smsmith	char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
23337785Smsmith				0, 46,  /* A 46 in network order */
23437785Smsmith				0, 0,   /* DSAP=0 & SSAP=0 fields */
23537785Smsmith				0xf3, 0 /* Control (Test Req + P bit set) */ };
23638305Smsmith	int i;
23738305Smsmith	u_char ether_address_backup[ETHER_ADDR_LEN];
23837785Smsmith
239122024Simp	for (i = 0; i < ETHER_ADDR_LEN; i++)
24038305Smsmith		ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
24138305Smsmith
24272940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_TX_ON);
24372940Simp	bcopy(test_packet, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
24472940Simp	bcopy(test_packet+ETHER_ADDR_LEN,
24572940Simp	    sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
24672940Simp	cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
24772940Simp	cs_outw(sc, TX_LEN_PORT, sizeof(test_packet));
24837785Smsmith
24937785Smsmith	/* Wait for chip to allocate memory */
25037785Smsmith	DELAY(50000);
25172940Simp	if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
252122024Simp		for (i = 0; i < ETHER_ADDR_LEN; i++)
25338305Smsmith			sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
25437785Smsmith		return 0;
25538305Smsmith	}
25637785Smsmith
25737785Smsmith	outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
25837785Smsmith
25937785Smsmith	DELAY(30000);
26037785Smsmith
261122024Simp	for (i = 0; i < ETHER_ADDR_LEN; i++)
262122024Simp		sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
263122024Simp	if ((cs_readreg(sc, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK)
26437785Smsmith		return 1;
26537785Smsmith	return 0;
26637785Smsmith}
26737785Smsmith
26837785Smsmith/*
26937785Smsmith * XXX This was rewritten from Linux driver without any tests.
27037785Smsmith */
27137785Smsmithstatic int
27237785Smsmithenable_aui(struct cs_softc *sc)
27337785Smsmith{
27437785Smsmith
27537785Smsmith	control_dc_dc(sc, 0);
27672940Simp	cs_writereg(sc, PP_LineCTL,
27772940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
27837785Smsmith
27937785Smsmith	if (!send_test_pkt(sc)) {
280104252Sbrooks		if_printf(&sc->arpcom.ac_if, "failed to enable AUI\n");
28137785Smsmith		return EINVAL;
28237785Smsmith        }
28337785Smsmith        return 0;
28437785Smsmith}
28537785Smsmith
28637785Smsmith/*
28737785Smsmith * XXX This was rewritten from Linux driver without any tests.
28837785Smsmith */
28937785Smsmithstatic int
29037785Smsmithenable_bnc(struct cs_softc *sc)
29137785Smsmith{
29237785Smsmith
29337785Smsmith	control_dc_dc(sc, 1);
29472940Simp	cs_writereg(sc, PP_LineCTL,
29572940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
29637785Smsmith
29737785Smsmith	if (!send_test_pkt(sc)) {
298104252Sbrooks		if_printf(&sc->arpcom.ac_if, "failed to enable BNC\n");
29937785Smsmith		return EINVAL;
30037785Smsmith        }
30137785Smsmith        return 0;
30237785Smsmith}
30337785Smsmith
30471316Simpint
30558816Simpcs_cs89x0_probe(device_t dev)
30637785Smsmith{
30758816Simp	int i;
30858816Simp	int error;
30958816Simp	u_long irq, junk;
31058816Simp	struct cs_softc *sc = device_get_softc(dev);
31137785Smsmith	unsigned rev_type = 0;
312122024Simp	uint16_t id;
31358816Simp	char chip_revision;
314122024Simp	uint16_t eeprom_buff[CHKSUM_LEN];
31537785Smsmith	int chip_type, pp_isaint, pp_isadma;
31637785Smsmith
31758816Simp	error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
31858816Simp	if (error)
31958816Simp		return (error);
32058816Simp
32172940Simp	sc->nic_addr = rman_get_start(sc->port_res);
32258816Simp
32372940Simp	if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
32437785Smsmith		/* Chip not detected. Let's try to reset it */
32537785Smsmith		if (bootverbose)
32658816Simp			device_printf(dev, "trying to reset the chip.\n");
32772940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
32872940Simp		i = cs_inw(sc, DATA_PORT);
32972940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
33072940Simp		cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
33172940Simp		if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
33258816Simp			return (ENXIO);
33337785Smsmith	}
33437785Smsmith
33572940Simp	for (i = 0; i < 10000; i++) {
33672940Simp		id = cs_readreg(sc, PP_ChipID);
33772940Simp		if (id == CHIP_EISA_ID_SIG)
33872940Simp			break;
33972940Simp	}
34072940Simp	if (i == 10000)
34158816Simp		return (ENXIO);
34237785Smsmith
34372940Simp	rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
34437785Smsmith	chip_type = rev_type & ~REVISON_BITS;
34537785Smsmith	chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
34637785Smsmith
34737785Smsmith	sc->chip_type = chip_type;
34858816Simp
34937785Smsmith	if(chip_type==CS8900) {
35037785Smsmith		pp_isaint = PP_CS8900_ISAINT;
35137785Smsmith		pp_isadma = PP_CS8900_ISADMA;
35237785Smsmith		sc->send_cmd = TX_CS8900_AFTER_ALL;
35337785Smsmith	} else {
35437785Smsmith		pp_isaint = PP_CS8920_ISAINT;
35537785Smsmith		pp_isadma = PP_CS8920_ISADMA;
35637785Smsmith		sc->send_cmd = TX_CS8920_AFTER_ALL;
35737785Smsmith	}
35837785Smsmith
35937785Smsmith        /*
36037785Smsmith         * Clear some fields so that fail of EEPROM will left them clean
36137785Smsmith         */
36237785Smsmith        sc->auto_neg_cnf = 0;
36337785Smsmith        sc->adapter_cnf  = 0;
36437785Smsmith        sc->isa_config   = 0;
36537785Smsmith
36637785Smsmith	/*
36758816Simp	 * If no interrupt specified (or "?"), use what the board tells us.
36837785Smsmith	 */
36958816Simp	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
37058816Simp
37158816Simp	/*
37258816Simp	 * Get data from EEPROM
37358816Simp	 */
37472940Simp	if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
37558816Simp		device_printf(dev, "No EEPROM, assuming defaults.\n");
37637785Smsmith	} else {
37737785Smsmith		if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
37858816Simp			device_printf(dev, "EEPROM read failed, "
37958816Simp				"assuming defaults.\n");
38037785Smsmith		} else {
38137785Smsmith			if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
38258816Simp				device_printf(dev, "EEPROM cheksum bad, "
38358816Simp					"assuming defaults.\n");
38437785Smsmith			} else {
38537785Smsmith                                sc->auto_neg_cnf =
38637785Smsmith                                        eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
38737785Smsmith                                sc->adapter_cnf =
38837785Smsmith                                        eeprom_buff[ADAPTER_CNF_OFFSET/2];
38937785Smsmith                                sc->isa_config =
39037785Smsmith                                        eeprom_buff[ISA_CNF_OFFSET/2];
39137785Smsmith
39237785Smsmith                                for (i=0; i<ETHER_ADDR_LEN/2; i++) {
39337785Smsmith                                        sc->arpcom.ac_enaddr[i*2]=
39437785Smsmith                                                eeprom_buff[i];
39537785Smsmith                                        sc->arpcom.ac_enaddr[i*2+1]=
39637785Smsmith                                                eeprom_buff[i] >> 8;
39737785Smsmith                                }
39837785Smsmith
39937785Smsmith                                /*
40037785Smsmith                                 * If no interrupt specified (or "?"),
40137785Smsmith                                 * use what the board tells us.
40237785Smsmith                                 */
40358816Simp				if (error) {
40458816Simp					irq = sc->isa_config & INT_NO_MASK;
40537785Smsmith                                        if (chip_type==CS8900) {
40658816Simp                                                switch(irq) {
40758816Simp						 case 0:
40858816Simp							irq=10;
40958816Simp							error=0;
41058816Simp							break;
41158816Simp						 case 1:
41258816Simp							irq=11;
41358816Simp							error=0;
41458816Simp							break;
41558816Simp						 case 2:
41658816Simp							irq=12;
41758816Simp							error=0;
41858816Simp							break;
41958816Simp						 case 3:
42058816Simp							irq=5;
42158816Simp							error=0;
42258816Simp							break;
42358816Simp						 default:
42458816Simp							device_printf(dev, "invalid irq in EEPROM.\n");
42558816Simp							error=EINVAL;
42658816Simp                                                }
42758816Simp					} else {
42858816Simp						if (irq>CS8920_NO_INTS) {
42958816Simp							device_printf(dev, "invalid irq in EEPROM.\n");
43058816Simp							error=EINVAL;
43158816Simp						} else {
43258816Simp							error=0;
43337785Smsmith						}
43437785Smsmith					}
43558816Simp
43658816Simp					if (!error)
43758816Simp						bus_set_resource(dev, SYS_RES_IRQ, 0,
43858816Simp								irq, 1);
43958816Simp				}
44037785Smsmith			}
44137785Smsmith                }
44237785Smsmith        }
44337785Smsmith
44458816Simp	if (!error) {
44537785Smsmith                if (chip_type == CS8900) {
44637785Smsmith			switch(irq) {
44758816Simp				case  5:
44858816Simp					irq = 3;
44958816Simp					break;
45058816Simp				case 10:
45158816Simp					irq = 0;
45258816Simp					break;
45358816Simp				case 11:
45458816Simp					irq = 1;
45558816Simp					break;
45658816Simp				case 12:
45758816Simp					irq = 2;
45858816Simp					break;
45958816Simp				default:
46058816Simp					error=EINVAL;
46137785Smsmith			}
46237785Smsmith                } else {
46337785Smsmith                        if (irq > CS8920_NO_INTS) {
46458816Simp                                error = EINVAL;
46537785Smsmith                        }
46637785Smsmith                }
46758816Simp	}
46858816Simp
46958816Simp	if (!error) {
470140926Simp		if (!(sc->flags & CS_NO_IRQ))
471140926Simp			cs_writereg(sc, pp_isaint, irq);
47237785Smsmith	} else {
47358816Simp	       	device_printf(dev, "Unknown or invalid irq\n");
47458816Simp                return (ENXIO);
47537785Smsmith        }
47637785Smsmith
47737785Smsmith        /*
47837785Smsmith         * Temporary disabled
47937785Smsmith         *
48037785Smsmith        if (drq>0)
48172940Simp		cs_writereg(sc, pp_isadma, drq);
48237785Smsmith	else {
483104252Sbrooks		device_printf(dev, "incorrect drq\n",);
48437785Smsmith		return 0;
48537785Smsmith	}
48637785Smsmith        */
48737785Smsmith
48837785Smsmith	if (bootverbose)
48958816Simp		 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
49037785Smsmith			chip_type==CS8900 ? '0' : '2',
49137785Smsmith			chip_type==CS8920M ? "M" : "",
49237785Smsmith			chip_revision,
49337785Smsmith			(sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
49437785Smsmith			(sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
49558816Simp			(sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
49637785Smsmith
49737785Smsmith        if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
49837785Smsmith            (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
49937785Smsmith                sc->line_ctl = LOW_RX_SQUELCH;
50037785Smsmith        else
50137785Smsmith                sc->line_ctl = 0;
50237785Smsmith
50337785Smsmith
50458816Simp	return 0;
50537785Smsmith}
50637785Smsmith
50737785Smsmith/*
50858816Simp * Allocate a port resource with the given resource id.
50958816Simp */
51058816Simpint cs_alloc_port(device_t dev, int rid, int size)
51158816Simp{
51258816Simp        struct cs_softc *sc = device_get_softc(dev);
51358816Simp        struct resource *res;
51458816Simp
51558816Simp        res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
51658816Simp                                 0ul, ~0ul, size, RF_ACTIVE);
51758816Simp        if (res) {
51858816Simp                sc->port_rid = rid;
51958816Simp                sc->port_res = res;
52058816Simp                sc->port_used = size;
52158816Simp                return (0);
52258816Simp        } else {
52358816Simp                return (ENOENT);
52458816Simp        }
52558816Simp}
52658816Simp
52758816Simp/*
52858816Simp * Allocate a memory resource with the given resource id.
52958816Simp */
53058816Simpint cs_alloc_memory(device_t dev, int rid, int size)
53158816Simp{
53258816Simp        struct cs_softc *sc = device_get_softc(dev);
53358816Simp        struct resource *res;
53458816Simp
53558816Simp        res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
53658816Simp                                 0ul, ~0ul, size, RF_ACTIVE);
53758816Simp        if (res) {
53858816Simp                sc->mem_rid = rid;
53958816Simp                sc->mem_res = res;
54058816Simp                sc->mem_used = size;
54158816Simp                return (0);
54258816Simp        } else {
54358816Simp                return (ENOENT);
54458816Simp        }
54558816Simp}
54658816Simp
54758816Simp/*
54858816Simp * Allocate an irq resource with the given resource id.
54958816Simp */
55058816Simpint cs_alloc_irq(device_t dev, int rid, int flags)
55158816Simp{
55258816Simp        struct cs_softc *sc = device_get_softc(dev);
55358816Simp        struct resource *res;
55458816Simp
555127135Snjl        res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
556127135Snjl                                     (RF_ACTIVE | flags));
55758816Simp        if (res) {
55858816Simp                sc->irq_rid = rid;
55958816Simp                sc->irq_res = res;
56058816Simp                return (0);
56158816Simp        } else {
56258816Simp                return (ENOENT);
56358816Simp        }
56458816Simp}
56558816Simp
56658816Simp/*
56758816Simp * Release all resources
56858816Simp */
56958816Simpvoid cs_release_resources(device_t dev)
57058816Simp{
57158816Simp        struct cs_softc *sc = device_get_softc(dev);
57258816Simp
57358816Simp        if (sc->port_res) {
57458816Simp                bus_release_resource(dev, SYS_RES_IOPORT,
57558816Simp                                     sc->port_rid, sc->port_res);
57658816Simp                sc->port_res = 0;
57758816Simp        }
57858816Simp        if (sc->mem_res) {
57958816Simp                bus_release_resource(dev, SYS_RES_MEMORY,
58058816Simp                                     sc->mem_rid, sc->mem_res);
58158816Simp                sc->mem_res = 0;
58258816Simp        }
58358816Simp        if (sc->irq_res) {
58458816Simp                bus_release_resource(dev, SYS_RES_IRQ,
58558816Simp                                     sc->irq_rid, sc->irq_res);
58658816Simp                sc->irq_res = 0;
58758816Simp        }
58858816Simp}
58958816Simp
59058816Simp/*
59137785Smsmith * Install the interface into kernel networking data structures
59237785Smsmith */
59371316Simpint
594121816Sbrookscs_attach(device_t dev)
59537785Smsmith{
59637785Smsmith        int media=0;
597121816Sbrooks	struct cs_softc *sc = device_get_softc(dev);;
59837785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
59937785Smsmith
60058816Simp	cs_stop( sc );
60158816Simp
602121752Sbrooks	ifp->if_softc=sc;
603121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
604121752Sbrooks	ifp->if_start=cs_start;
605121752Sbrooks	ifp->if_ioctl=cs_ioctl;
606121752Sbrooks	ifp->if_watchdog=cs_watchdog;
607121752Sbrooks	ifp->if_init=cs_init;
608121752Sbrooks	ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
609121752Sbrooks	/*
610121752Sbrooks	 *  MIB DATA
611121752Sbrooks	 */
612121752Sbrooks	/*
613121752Sbrooks	ifp->if_linkmib=&sc->mibdata;
614121752Sbrooks	ifp->if_linkmiblen=sizeof sc->mibdata;
615121752Sbrooks	*/
61637785Smsmith
617133681Srwatson	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
618133681Srwatson	    IFF_NEEDSGIANT);
61937785Smsmith
620121752Sbrooks	/*
621121752Sbrooks	 * this code still in progress (DMA support)
622121752Sbrooks	 *
62337785Smsmith
624121752Sbrooks	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
625121752Sbrooks	if (sc->recv_ring == NULL) {
626121816Sbrooks		log(LOG_ERR,
627121816Sbrooks		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
628121752Sbrooks		return(0);
629121752Sbrooks	}
630121752Sbrooks	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
631121752Sbrooks	    < (128*1024-CS_DMA_BUFFER_SIZE))
632121752Sbrooks	    sc->recv_ring+=16*1024;
63337785Smsmith
634121752Sbrooks	*/
63537785Smsmith
636121752Sbrooks	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
637121752Sbrooks	if (sc->buffer == NULL) {
638121752Sbrooks		if_printf(ifp, "Couldn't allocate memory for NIC\n");
639121752Sbrooks		return(0);
640121752Sbrooks	}
64137785Smsmith
642121752Sbrooks	/*
643121752Sbrooks	 * Initialize the media structures.
644121752Sbrooks	 */
645121752Sbrooks	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
64637785Smsmith
647121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_T) {
648121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
649121752Sbrooks		if (sc->chip_type != CS8900) {
650121752Sbrooks			ifmedia_add(&sc->media,
651121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
652121752Sbrooks			ifmedia_add(&sc->media,
653121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
654121752Sbrooks		}
655121752Sbrooks	}
65637785Smsmith
657121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_2)
658121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
65937785Smsmith
660121752Sbrooks	if (sc->adapter_cnf & A_CNF_AUI)
661121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
66237785Smsmith
663121752Sbrooks	if (sc->adapter_cnf & A_CNF_MEDIA)
664121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
66537785Smsmith
666121752Sbrooks	/* Set default media from EEPROM */
667121752Sbrooks	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
668121752Sbrooks	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
669121752Sbrooks	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
670121752Sbrooks	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
671121752Sbrooks	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
672122024Simp	default:
673122024Simp		if_printf(ifp, "no media, assuming 10baseT\n");
674122024Simp		sc->adapter_cnf |= A_CNF_10B_T;
675122024Simp		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
676122024Simp		if (sc->chip_type != CS8900) {
677122024Simp			ifmedia_add(&sc->media,
678122024Simp			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
679122024Simp			ifmedia_add(&sc->media,
680122024Simp			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
681122024Simp		}
682122024Simp		media = IFM_ETHER | IFM_10_T;
683122024Simp		break;
68437785Smsmith	}
685121752Sbrooks	ifmedia_set(&sc->media, media);
686121752Sbrooks	cs_mediaset(sc, media);
68737785Smsmith
688121752Sbrooks	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
689121752Sbrooks
69058816Simp	return (0);
69137785Smsmith}
69237785Smsmith
693140888Simpint
694140888Simpcs_detach(device_t dev)
695140888Simp{
696140888Simp	struct cs_softc *sc;
697140888Simp	struct ifnet *ifp;
698140888Simp
699140888Simp	sc = device_get_softc(dev);
700140888Simp	ifp = &sc->arpcom.ac_if;
701140888Simp
702140888Simp	cs_stop(sc);
703140888Simp	ifp->if_flags &= ~IFF_RUNNING;
704140888Simp	ether_ifdetach(ifp);
705140888Simp	cs_release_resources(dev);
706140888Simp	return (0);
707140888Simp}
708140888Simp
70937785Smsmith/*
71037785Smsmith * Initialize the board
71137785Smsmith */
71237785Smsmithstatic void
71337785Smsmithcs_init(void *xsc)
71437785Smsmith{
71537785Smsmith	struct cs_softc *sc=(struct cs_softc *)xsc;
71637785Smsmith	struct ifnet *ifp = &sc->arpcom.ac_if;
71741591Sarchie	int i, s, rx_cfg;
71837785Smsmith
71937785Smsmith	/*
72037785Smsmith	 * reset whatchdog timer
72137785Smsmith	 */
72237785Smsmith	ifp->if_timer=0;
72337785Smsmith	sc->buf_len = 0;
72437785Smsmith
72537785Smsmith	s=splimp();
72637785Smsmith
72737785Smsmith	/*
72837785Smsmith	 * Hardware initialization of cs
72937785Smsmith	 */
73037785Smsmith
73137785Smsmith	/* Enable receiver and transmitter */
73272940Simp	cs_writereg(sc, PP_LineCTL,
73372940Simp		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
73437785Smsmith
73537785Smsmith	/* Configure the receiver mode */
73637785Smsmith	cs_setmode(sc);
73737785Smsmith
73837785Smsmith	/*
73937785Smsmith	 * This defines what type of frames will cause interrupts
74037785Smsmith	 * Bad frames should generate interrupts so that the driver
74137785Smsmith	 * could track statistics of discarded packets
74237785Smsmith	 */
74337785Smsmith        rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
74437785Smsmith		 RX_EXTRA_DATA_ENBL;
74537785Smsmith	if (sc->isa_config & STREAM_TRANSFER)
74637785Smsmith		rx_cfg |= RX_STREAM_ENBL;
74772940Simp	cs_writereg(sc, PP_RxCFG, rx_cfg);
74872940Simp	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
74937785Smsmith		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
75037785Smsmith		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
75172940Simp	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
75237785Smsmith		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
75337785Smsmith		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
75437785Smsmith
75537785Smsmith        /* Write MAC address into IA filter */
75637785Smsmith        for (i=0; i<ETHER_ADDR_LEN/2; i++)
75772940Simp                cs_writereg(sc, PP_IA + i * 2,
75872940Simp		    sc->arpcom.ac_enaddr[i * 2] |
75972940Simp		    (sc->arpcom.ac_enaddr[i * 2 + 1] << 8) );
76037785Smsmith
76137785Smsmith	/*
76237785Smsmith	 * Now enable everything
76337785Smsmith	 */
76437785Smsmith/*
76537785Smsmith#ifdef	CS_USE_64K_DMA
76672940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
76772940Simp#else
76872940Simp        cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
76937785Smsmith#endif
77037785Smsmith*/
77172940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
77237785Smsmith
77337785Smsmith	/*
77437785Smsmith	 * Set running and clear output active flags
77537785Smsmith	 */
77637785Smsmith	sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
77737785Smsmith	sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
77837785Smsmith
77937785Smsmith	/*
78037785Smsmith	 * Start sending process
78137785Smsmith	 */
78237785Smsmith	cs_start(ifp);
78337785Smsmith
78437785Smsmith	(void) splx(s);
78537785Smsmith}
78637785Smsmith
78737785Smsmith/*
788106937Ssam * Get the packet from the board and send it to the upper layer.
78937785Smsmith */
79037785Smsmithstatic int
79137785Smsmithcs_get_packet(struct cs_softc *sc)
79237785Smsmith{
79337785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
79437785Smsmith	int iobase = sc->nic_addr, status, length;
79537785Smsmith	struct ether_header *eh;
79637785Smsmith	struct mbuf *m;
79737785Smsmith
79837785Smsmith#ifdef CS_DEBUG
79937785Smsmith	int i;
80037785Smsmith#endif
80137785Smsmith
80272940Simp	status = cs_inw(sc, RX_FRAME_PORT);
80372940Simp	length = cs_inw(sc, RX_FRAME_PORT);
80437785Smsmith
80537785Smsmith#ifdef CS_DEBUG
806104252Sbrooks	if_printf(ifp, "rcvd: stat %x, len %d\n",
807104252Sbrooks		status, length);
80837785Smsmith#endif
80937785Smsmith
81037785Smsmith	if (!(status & RX_OK)) {
81137785Smsmith#ifdef CS_DEBUG
812104252Sbrooks		if_printf(ifp, "bad pkt stat %x\n", status);
81337785Smsmith#endif
81437785Smsmith		ifp->if_ierrors++;
81537785Smsmith		return -1;
81637785Smsmith	}
81737785Smsmith
818111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
81937785Smsmith	if (m==NULL)
82037785Smsmith		return -1;
82137785Smsmith
82237785Smsmith	if (length > MHLEN) {
823111119Simp		MCLGET(m, M_DONTWAIT);
82437785Smsmith		if (!(m->m_flags & M_EXT)) {
82537785Smsmith			m_freem(m);
82637785Smsmith			return -1;
82737785Smsmith		}
82837785Smsmith	}
82937785Smsmith
83037785Smsmith	/* Initialize packet's header info */
83137785Smsmith	m->m_pkthdr.rcvif = ifp;
83237785Smsmith	m->m_pkthdr.len = length;
83337785Smsmith	m->m_len = length;
83437785Smsmith
83537785Smsmith	/* Get the data */
83637785Smsmith	insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
83737785Smsmith
83837785Smsmith	eh = mtod(m, struct ether_header *);
83937785Smsmith
84037785Smsmith#ifdef CS_DEBUG
84137785Smsmith	for (i=0;i<length;i++)
84237785Smsmith	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
84337785Smsmith	printf( "\n" );
84437785Smsmith#endif
84537785Smsmith
84637785Smsmith	if (status & (RX_IA | RX_BROADCAST) ||
84737785Smsmith	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
84837785Smsmith		/* Feed the packet to the upper layer */
849106937Ssam		(*ifp->if_input)(ifp, m);
85037785Smsmith		ifp->if_ipackets++;
851122024Simp		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
852122024Simp                        DELAY(cs_recv_delay);
85337785Smsmith	} else {
85437785Smsmith		m_freem(m);
85537785Smsmith	}
85637785Smsmith
85737785Smsmith	return 0;
85837785Smsmith}
85937785Smsmith
86037785Smsmith/*
86158816Simp * Handle interrupts
86237785Smsmith */
86358816Simpvoid
86458816Simpcsintr(void *arg)
86537785Smsmith{
86658816Simp	struct cs_softc *sc = (struct cs_softc*) arg;
86737785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
86841591Sarchie	int status;
86937785Smsmith
87037785Smsmith#ifdef CS_DEBUG
871104252Sbrooks	if_printf(ifp, "Interrupt.\n");
87237785Smsmith#endif
87337785Smsmith
87472940Simp	while ((status=cs_inw(sc, ISQ_PORT))) {
87537785Smsmith
87637785Smsmith#ifdef CS_DEBUG
877104252Sbrooks		if_printf(ifp, "from ISQ: %04x\n", status);
87837785Smsmith#endif
87937785Smsmith
88037785Smsmith		switch (status & ISQ_EVENT_MASK) {
88137785Smsmith                case ISQ_RECEIVER_EVENT:
88237785Smsmith                        cs_get_packet(sc);
88337785Smsmith                        break;
88437785Smsmith
88537785Smsmith                case ISQ_TRANSMITTER_EVENT:
88637785Smsmith                        if (status & TX_OK)
88737785Smsmith                                ifp->if_opackets++;
88837785Smsmith                        else
88937785Smsmith                                ifp->if_oerrors++;
89037785Smsmith                        ifp->if_flags &= ~IFF_OACTIVE;
89137785Smsmith                        ifp->if_timer = 0;
89237785Smsmith                        break;
89337785Smsmith
89437785Smsmith                case ISQ_BUFFER_EVENT:
89537785Smsmith                        if (status & READY_FOR_TX) {
89637785Smsmith                                ifp->if_flags &= ~IFF_OACTIVE;
89737785Smsmith                                ifp->if_timer = 0;
89837785Smsmith                        }
89937785Smsmith
90037785Smsmith                        if (status & TX_UNDERRUN) {
90137785Smsmith                                ifp->if_flags &= ~IFF_OACTIVE;
90237785Smsmith                                ifp->if_timer = 0;
90337785Smsmith                                ifp->if_oerrors++;
90437785Smsmith                        }
90537785Smsmith                        break;
90637785Smsmith
90737785Smsmith                case ISQ_RX_MISS_EVENT:
90837785Smsmith                        ifp->if_ierrors+=(status>>6);
90937785Smsmith                        break;
91037785Smsmith
91137785Smsmith                case ISQ_TX_COL_EVENT:
91237785Smsmith                        ifp->if_collisions+=(status>>6);
91337785Smsmith                        break;
91437785Smsmith                }
91537785Smsmith        }
91637785Smsmith
91737785Smsmith        if (!(ifp->if_flags & IFF_OACTIVE)) {
91837785Smsmith                cs_start(ifp);
91937785Smsmith        }
92037785Smsmith}
92137785Smsmith
92237785Smsmith/*
92337785Smsmith * Save the data in buffer
92437785Smsmith */
92537785Smsmith
92637785Smsmithstatic void
92737785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
92837785Smsmith{
92937785Smsmith	int len;
93037785Smsmith	struct mbuf *mp;
93137785Smsmith	unsigned char *data, *buf;
93237785Smsmith
93337785Smsmith	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
93437785Smsmith		len = mp->m_len;
93537785Smsmith
93637785Smsmith		/*
93737785Smsmith		 * Ignore empty parts
93837785Smsmith		 */
93937785Smsmith		if (!len)
94037785Smsmith		continue;
94137785Smsmith
94237785Smsmith		/*
94337785Smsmith		 * Find actual data address
94437785Smsmith		 */
94537785Smsmith		data = mtod(mp, caddr_t);
94637785Smsmith
94737785Smsmith		bcopy((caddr_t) data, (caddr_t) buf, len);
94837785Smsmith		buf += len;
94937785Smsmith		sc->buf_len += len;
95037785Smsmith	}
95137785Smsmith}
95237785Smsmith
95337785Smsmith
95437785Smsmithstatic void
95537785Smsmithcs_xmit_buf( struct cs_softc *sc )
95637785Smsmith{
95737785Smsmith	outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
95837785Smsmith	sc->buf_len = 0;
95937785Smsmith}
96037785Smsmith
96137785Smsmithstatic void
96237785Smsmithcs_start(struct ifnet *ifp)
96337785Smsmith{
96437785Smsmith	int s, length;
96537785Smsmith	struct mbuf *m, *mp;
96637785Smsmith	struct cs_softc *sc = ifp->if_softc;
96737785Smsmith
96837785Smsmith	s = splimp();
96937785Smsmith
97037785Smsmith	for (;;) {
97137785Smsmith		if (sc->buf_len)
97237785Smsmith			length = sc->buf_len;
97337785Smsmith		else {
97437785Smsmith			IF_DEQUEUE( &ifp->if_snd, m );
97537785Smsmith
97637785Smsmith			if (m==NULL) {
97737785Smsmith				(void) splx(s);
97837785Smsmith				return;
97937785Smsmith			}
98037785Smsmith
98137785Smsmith			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
98237785Smsmith				length += mp->m_len;
98337785Smsmith
98437785Smsmith			/* Skip zero-length packets */
98537785Smsmith			if (length == 0) {
98637785Smsmith				m_freem(m);
98737785Smsmith				continue;
98837785Smsmith			}
98937785Smsmith
99037785Smsmith			cs_write_mbufs(sc, m);
99137785Smsmith
992106937Ssam			BPF_MTAP(ifp, m);
99337785Smsmith
99437785Smsmith			m_freem(m);
99537785Smsmith		}
99637785Smsmith
99737785Smsmith		/*
99837785Smsmith		 * Issue a SEND command
99937785Smsmith		 */
100072940Simp		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
100172940Simp		cs_outw(sc, TX_LEN_PORT, length );
100237785Smsmith
100337785Smsmith		/*
100437785Smsmith		 * If there's no free space in the buffer then leave
100537785Smsmith		 * this packet for the next time: indicate output active
100637785Smsmith		 * and return.
100737785Smsmith		 */
100872940Simp		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
100937785Smsmith			ifp->if_timer = sc->buf_len;
101037785Smsmith			(void) splx(s);
101137785Smsmith			ifp->if_flags |= IFF_OACTIVE;
101237785Smsmith			return;
101337785Smsmith		}
101437785Smsmith
101537785Smsmith               	cs_xmit_buf(sc);
101637785Smsmith
101737785Smsmith		/*
101837785Smsmith		 * Set the watchdog timer in case we never hear
101937785Smsmith		 * from board again. (I don't know about correct
102037785Smsmith		 * value for this timeout)
102137785Smsmith		 */
102237785Smsmith		ifp->if_timer = length;
102337785Smsmith
102437785Smsmith		(void) splx(s);
102537785Smsmith		ifp->if_flags |= IFF_OACTIVE;
102637785Smsmith		return;
102737785Smsmith	}
102837785Smsmith}
102937785Smsmith
103037785Smsmith/*
103137785Smsmith * Stop everything on the interface
103237785Smsmith */
103337785Smsmithstatic void
103437785Smsmithcs_stop(struct cs_softc *sc)
103537785Smsmith{
103637785Smsmith	int s = splimp();
103737785Smsmith
103872940Simp	cs_writereg(sc, PP_RxCFG, 0);
103972940Simp	cs_writereg(sc, PP_TxCFG, 0);
104072940Simp	cs_writereg(sc, PP_BufCFG, 0);
104172940Simp	cs_writereg(sc, PP_BusCTL, 0);
104237785Smsmith
104337785Smsmith	sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
104437785Smsmith	sc->arpcom.ac_if.if_timer = 0;
104537785Smsmith
104637785Smsmith	(void) splx(s);
104737785Smsmith}
104837785Smsmith
104937785Smsmith/*
105037785Smsmith * Reset the interface
105137785Smsmith */
105237785Smsmithstatic void
105337785Smsmithcs_reset(struct cs_softc *sc)
105437785Smsmith{
105537785Smsmith	cs_stop(sc);
105637785Smsmith	cs_init(sc);
105737785Smsmith}
105837785Smsmith
105937785Smsmithstatic void
106037785Smsmithcs_setmode(struct cs_softc *sc)
106137785Smsmith{
106237785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
106337785Smsmith	int rx_ctl;
106437785Smsmith
106537785Smsmith	/* Stop the receiver while changing filters */
106672940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
106737785Smsmith
106837785Smsmith	if (ifp->if_flags & IFF_PROMISC) {
106937785Smsmith		/* Turn on promiscuous mode. */
107037785Smsmith		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
107137785Smsmith	} else {
107237785Smsmith		if (ifp->if_flags & IFF_MULTICAST) {
107337785Smsmith			/* Allow receiving frames with multicast addresses */
107437785Smsmith			rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
107537785Smsmith				 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
107637785Smsmith			/*
107737785Smsmith			 * Here the reconfiguration of chip's multicast
107837785Smsmith			 * filters should be done but I've no idea about
107937785Smsmith			 * hash transformation in this chip. If you can
108037785Smsmith			 * add this code or describe me the transformation
108137785Smsmith			 * I'd be very glad.
108237785Smsmith			 */
108337785Smsmith		} else {
108437785Smsmith			/*
108537785Smsmith			 * Receive only good frames addressed for us and
108637785Smsmith			 * good broadcasts.
108737785Smsmith			 */
108837785Smsmith			rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
108937785Smsmith				 RX_OK_ACCEPT;
109037785Smsmith		}
109137785Smsmith	}
109237785Smsmith
109337785Smsmith	/* Set up the filter */
109472940Simp	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
109537785Smsmith
109637785Smsmith	/* Turn on receiver */
109772940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
109837785Smsmith}
109937785Smsmith
110037785Smsmithstatic int
110137785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
110237785Smsmith{
110337785Smsmith	struct cs_softc *sc=ifp->if_softc;
110437785Smsmith	struct ifreq *ifr = (struct ifreq *)data;
110537785Smsmith	int s,error=0;
110637785Smsmith
110737785Smsmith#ifdef CS_DEBUG
1108104252Sbrooks	if_printf(ifp, "ioctl(%lx)\n", command);
110937785Smsmith#endif
111037785Smsmith
111137785Smsmith	s=splimp();
111237785Smsmith
111337785Smsmith	switch (command) {
111437785Smsmith	case SIOCSIFFLAGS:
111537785Smsmith		/*
111637785Smsmith		 * Switch interface state between "running" and
111737785Smsmith		 * "stopped", reflecting the UP flag.
111837785Smsmith                 */
111937785Smsmith                if (sc->arpcom.ac_if.if_flags & IFF_UP) {
112037785Smsmith                        if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
112137785Smsmith                                cs_init(sc);
112237785Smsmith                        }
112337785Smsmith                } else {
112437785Smsmith                        if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
112537785Smsmith                                cs_stop(sc);
112637785Smsmith                        }
112737785Smsmith		}
112837785Smsmith		/*
112937785Smsmith		 * Promiscuous and/or multicast flags may have changed,
113037785Smsmith		 * so reprogram the multicast filter and/or receive mode.
113137785Smsmith		 *
113237785Smsmith		 * See note about multicasts in cs_setmode
113337785Smsmith		 */
113437785Smsmith		cs_setmode(sc);
113537785Smsmith		break;
113637785Smsmith
113737785Smsmith	case SIOCADDMULTI:
113837785Smsmith	case SIOCDELMULTI:
113937785Smsmith	    /*
114037785Smsmith	     * Multicast list has changed; set the hardware filter
114137785Smsmith	     * accordingly.
114237785Smsmith	     *
114337785Smsmith	     * See note about multicasts in cs_setmode
114437785Smsmith	     */
114537785Smsmith	    cs_setmode(sc);
114637785Smsmith	    error = 0;
114737785Smsmith	    break;
114837785Smsmith
114937785Smsmith        case SIOCSIFMEDIA:
115037785Smsmith        case SIOCGIFMEDIA:
115137785Smsmith                error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
115237785Smsmith                break;
115337785Smsmith
115437785Smsmith        default:
1155140921Simp		error = ether_ioctl(ifp, command, data);
1156106937Ssam		break;
115737785Smsmith        }
115837785Smsmith
115937785Smsmith	(void) splx(s);
116037785Smsmith	return error;
116137785Smsmith}
116237785Smsmith
116337785Smsmith/*
116437785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to
116537785Smsmith * generate an interrupt after a transmit has been started on it.
116637785Smsmith */
116737785Smsmithstatic void
116837785Smsmithcs_watchdog(struct ifnet *ifp)
116937785Smsmith{
117058816Simp	struct cs_softc *sc = ifp->if_softc;
117137785Smsmith
117237785Smsmith	ifp->if_oerrors++;
1173121816Sbrooks	log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
117437785Smsmith
117537785Smsmith	/* Reset the interface */
117637785Smsmith	if (ifp->if_flags & IFF_UP)
117737785Smsmith		cs_reset(sc);
117837785Smsmith	else
117937785Smsmith		cs_stop(sc);
118037785Smsmith}
118137785Smsmith
118237785Smsmithstatic int
118337785Smsmithcs_mediachange(struct ifnet *ifp)
118437785Smsmith{
118537785Smsmith	struct cs_softc *sc = ifp->if_softc;
118637785Smsmith	struct ifmedia *ifm = &sc->media;
118737785Smsmith
118837785Smsmith	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
118937785Smsmith		return EINVAL;
119037785Smsmith
119137785Smsmith	return cs_mediaset(sc, ifm->ifm_media);
119237785Smsmith}
119337785Smsmith
119437785Smsmithstatic void
119537785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
119637785Smsmith{
119737785Smsmith	int line_status;
119837785Smsmith	struct cs_softc *sc = ifp->if_softc;
119937785Smsmith
120037785Smsmith	ifmr->ifm_active = IFM_ETHER;
120172940Simp	line_status = cs_readreg(sc, PP_LineST);
120237785Smsmith	if (line_status & TENBASET_ON) {
120337785Smsmith		ifmr->ifm_active |= IFM_10_T;
120437785Smsmith		if (sc->chip_type != CS8900) {
120572940Simp			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
120637785Smsmith				ifmr->ifm_active |= IFM_FDX;
120772940Simp			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
120837785Smsmith				ifmr->ifm_active |= IFM_HDX;
120937785Smsmith		}
121037785Smsmith		ifmr->ifm_status = IFM_AVALID;
121137785Smsmith		if (line_status & LINK_OK)
121237785Smsmith			ifmr->ifm_status |= IFM_ACTIVE;
121337785Smsmith	} else {
121438305Smsmith		if (line_status & AUI_ON) {
121572940Simp			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
121672940Simp			    HCB1_ENBL);
121738305Smsmith			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
121872940Simp			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
121938305Smsmith				ifmr->ifm_active |= IFM_10_2;
122038305Smsmith			else
122138305Smsmith				ifmr->ifm_active |= IFM_10_5;
122238305Smsmith		}
122337785Smsmith	}
122437785Smsmith}
122537785Smsmith
122637785Smsmithstatic int
122737785Smsmithcs_mediaset(struct cs_softc *sc, int media)
122837785Smsmith{
122937785Smsmith        int error;
123037785Smsmith
123137785Smsmith	/* Stop the receiver & transmitter */
123272940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
123372940Simp	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
123437785Smsmith
123537785Smsmith#ifdef CS_DEBUG
1236104252Sbrooks	if_printf(&sc->arpcom.ac_if, "cs_setmedia(%x)\n", media);
123737785Smsmith#endif
123837785Smsmith
123937785Smsmith	switch (IFM_SUBTYPE(media)) {
124037785Smsmith	default:
124137785Smsmith	case IFM_AUTO:
124238592Smsmith		if ((error=enable_tp(sc))==0)
124338592Smsmith			error = cs_duplex_auto(sc);
124443314Sdillon		else if ((error=enable_bnc(sc)) != 0)
124538592Smsmith			error = enable_aui(sc);
124637785Smsmith		break;
124737785Smsmith	case IFM_10_T:
124843314Sdillon		if ((error=enable_tp(sc)) != 0)
124937785Smsmith			break;
125037785Smsmith		if (media & IFM_FDX)
125137785Smsmith			cs_duplex_full(sc);
125237785Smsmith		else if (media & IFM_HDX)
125337785Smsmith			cs_duplex_half(sc);
125437785Smsmith		else
125537785Smsmith			error = cs_duplex_auto(sc);
125637785Smsmith		break;
125737785Smsmith	case IFM_10_2:
125837785Smsmith		error = enable_bnc(sc);
125937785Smsmith		break;
126037785Smsmith	case IFM_10_5:
126137785Smsmith		error = enable_aui(sc);
126237785Smsmith		break;
126337785Smsmith	}
126437785Smsmith
126537785Smsmith	/*
126637785Smsmith	 * Turn the transmitter & receiver back on
126737785Smsmith	 */
126872940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
126972940Simp	    SERIAL_RX_ON | SERIAL_TX_ON);
127037785Smsmith
127137785Smsmith	return error;
127237785Smsmith}
1273