if_cs.c revision 140927
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 140927 2005-01-28 06:45:42Z 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{
193140927Simp	int i, error=0;
194140927Simp
19572940Simp	cs_writereg(sc, PP_AutoNegCTL,
19672940Simp	    RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
197140927Simp	for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
198140927Simp		if (i > 40000) {
199140927Simp			if_printf(&sc->arpcom.ac_if,
200140927Simp			    "full/half duplex auto negotiation timeout\n");
20137785Smsmith			error = ETIMEDOUT;
202140927Simp			break;
203140927Simp		}
204140927Simp		DELAY(1000);
205140927Simp	}
206140927Simp	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");
220140927Simp		return EINVAL;
22137785Smsmith	}
22237785Smsmith
22337785Smsmith	return 0;
22437785Smsmith}
22537785Smsmith
22637785Smsmith/*
22737785Smsmith * XXX This was rewritten from Linux driver without any tests.
228140927Simp */
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;
282140927Simp	}
283140927Simp	return 0;
28437785Smsmith}
28537785Smsmith
28637785Smsmith/*
28737785Smsmith * XXX This was rewritten from Linux driver without any tests.
288140927Simp */
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;
300140927Simp	}
301140927Simp	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	/*
360140927Simp	 * Clear some fields so that fail of EEPROM will left them clean
361140927Simp	 */
362140927Simp	sc->auto_neg_cnf = 0;
363140927Simp	sc->adapter_cnf  = 0;
364140927Simp	sc->isa_config   = 0;
365140927Simp
366140927Simp	/*
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 {
385140927Simp				sc->auto_neg_cnf =
386140927Simp				    eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
387140927Simp				sc->adapter_cnf =
388140927Simp				    eeprom_buff[ADAPTER_CNF_OFFSET/2];
389140927Simp				sc->isa_config =
390140927Simp				    eeprom_buff[ISA_CNF_OFFSET/2];
391140927Simp
392140927Simp				for (i=0; i<ETHER_ADDR_LEN/2; i++) {
393140927Simp					sc->arpcom.ac_enaddr[i*2]=
394140927Simp					    eeprom_buff[i];
395140927Simp					sc->arpcom.ac_enaddr[i*2+1]=
396140927Simp					    eeprom_buff[i] >> 8;
397140927Simp				}
39837785Smsmith
399140927Simp				/*
400140927Simp				 * If no interrupt specified,
401140927Simp				 * use what the board tells us.
402140927Simp				 */
40358816Simp				if (error) {
40458816Simp					irq = sc->isa_config & INT_NO_MASK;
405140927Simp					if (chip_type==CS8900) {
406140927Simp						switch(irq) {
407140927Simp						case 0:
40858816Simp							irq=10;
40958816Simp							error=0;
41058816Simp							break;
411140927Simp						case 1:
41258816Simp							irq=11;
41358816Simp							error=0;
41458816Simp							break;
415140927Simp						case 2:
41658816Simp							irq=12;
41758816Simp							error=0;
41858816Simp							break;
419140927Simp						case 3:
42058816Simp							irq=5;
42158816Simp							error=0;
42258816Simp							break;
42358816Simp						 default:
42458816Simp							device_printf(dev, "invalid irq in EEPROM.\n");
42558816Simp							error=EINVAL;
426140927Simp						}
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			}
441140927Simp		}
442140927Simp	}
44337785Smsmith
44458816Simp	if (!error) {
445140927Simp		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			}
462140927Simp		} else {
463140927Simp			if (irq > CS8920_NO_INTS) {
464140927Simp				error = EINVAL;
465140927Simp			}
466140927Simp		}
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");
474140927Simp		return (ENXIO);
475140927Simp	}
476140927Simp
477140927Simp	/*
478140927Simp	 * Temporary disabled
479140927Simp	 *
480140927Simp	if (drq>0)
48172940Simp		cs_writereg(sc, pp_isadma, drq);
48237785Smsmith	else {
483104252Sbrooks		device_printf(dev, "incorrect drq\n",);
48437785Smsmith		return 0;
48537785Smsmith	}
486140927Simp	*/
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
497140927Simp	if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
498140927Simp	    (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
499140927Simp		sc->line_ctl = LOW_RX_SQUELCH;
500140927Simp	else
501140927Simp		sc->line_ctl = 0;
50237785Smsmith
50358816Simp	return 0;
50437785Smsmith}
50537785Smsmith
50637785Smsmith/*
50758816Simp * Allocate a port resource with the given resource id.
50858816Simp */
50958816Simpint cs_alloc_port(device_t dev, int rid, int size)
51058816Simp{
511140927Simp	struct cs_softc *sc = device_get_softc(dev);
512140927Simp	struct resource *res;
51358816Simp
514140927Simp	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
515140927Simp	    0ul, ~0ul, size, RF_ACTIVE);
516140927Simp	if (res) {
517140927Simp		sc->port_rid = rid;
518140927Simp		sc->port_res = res;
519140927Simp		sc->port_used = size;
520140927Simp		return (0);
521140927Simp	} else {
522140927Simp		return (ENOENT);
523140927Simp	}
52458816Simp}
52558816Simp
52658816Simp/*
52758816Simp * Allocate a memory resource with the given resource id.
52858816Simp */
52958816Simpint cs_alloc_memory(device_t dev, int rid, int size)
53058816Simp{
531140927Simp	struct cs_softc *sc = device_get_softc(dev);
532140927Simp	struct resource *res;
53358816Simp
534140927Simp	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
535140927Simp	    0ul, ~0ul, size, RF_ACTIVE);
536140927Simp	if (res) {
537140927Simp		sc->mem_rid = rid;
538140927Simp		sc->mem_res = res;
539140927Simp		sc->mem_used = size;
540140927Simp		return (0);
541140927Simp	} else {
542140927Simp		return (ENOENT);
543140927Simp	}
54458816Simp}
54558816Simp
54658816Simp/*
54758816Simp * Allocate an irq resource with the given resource id.
54858816Simp */
54958816Simpint cs_alloc_irq(device_t dev, int rid, int flags)
55058816Simp{
551140927Simp	struct cs_softc *sc = device_get_softc(dev);
552140927Simp	struct resource *res;
55358816Simp
554140927Simp	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
555140927Simp	    (RF_ACTIVE | flags));
556140927Simp	if (res) {
557140927Simp		sc->irq_rid = rid;
558140927Simp		sc->irq_res = res;
559140927Simp		return (0);
560140927Simp	} else {
561140927Simp		return (ENOENT);
562140927Simp	}
56358816Simp}
56458816Simp
56558816Simp/*
56658816Simp * Release all resources
56758816Simp */
56858816Simpvoid cs_release_resources(device_t dev)
56958816Simp{
570140927Simp	struct cs_softc *sc = device_get_softc(dev);
57158816Simp
572140927Simp	if (sc->port_res) {
573140927Simp		bus_release_resource(dev, SYS_RES_IOPORT,
574140927Simp		    sc->port_rid, sc->port_res);
575140927Simp		sc->port_res = 0;
576140927Simp	}
577140927Simp	if (sc->mem_res) {
578140927Simp		bus_release_resource(dev, SYS_RES_MEMORY,
579140927Simp		    sc->mem_rid, sc->mem_res);
580140927Simp		sc->mem_res = 0;
581140927Simp	}
582140927Simp	if (sc->irq_res) {
583140927Simp		bus_release_resource(dev, SYS_RES_IRQ,
584140927Simp		    sc->irq_rid, sc->irq_res);
585140927Simp		sc->irq_res = 0;
586140927Simp	}
58758816Simp}
58858816Simp
58958816Simp/*
59037785Smsmith * Install the interface into kernel networking data structures
59137785Smsmith */
59271316Simpint
593121816Sbrookscs_attach(device_t dev)
59437785Smsmith{
595140927Simp	int media=0;
596121816Sbrooks	struct cs_softc *sc = device_get_softc(dev);;
59737785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
59837785Smsmith
59958816Simp	cs_stop( sc );
60058816Simp
601121752Sbrooks	ifp->if_softc=sc;
602121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
603121752Sbrooks	ifp->if_start=cs_start;
604121752Sbrooks	ifp->if_ioctl=cs_ioctl;
605121752Sbrooks	ifp->if_watchdog=cs_watchdog;
606121752Sbrooks	ifp->if_init=cs_init;
607121752Sbrooks	ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
608121752Sbrooks	/*
609121752Sbrooks	 *  MIB DATA
610121752Sbrooks	 */
611121752Sbrooks	/*
612121752Sbrooks	ifp->if_linkmib=&sc->mibdata;
613121752Sbrooks	ifp->if_linkmiblen=sizeof sc->mibdata;
614121752Sbrooks	*/
61537785Smsmith
616133681Srwatson	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
617133681Srwatson	    IFF_NEEDSGIANT);
61837785Smsmith
619121752Sbrooks	/*
620121752Sbrooks	 * this code still in progress (DMA support)
621121752Sbrooks	 *
62237785Smsmith
623121752Sbrooks	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
624121752Sbrooks	if (sc->recv_ring == NULL) {
625121816Sbrooks		log(LOG_ERR,
626121816Sbrooks		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
627121752Sbrooks		return(0);
628121752Sbrooks	}
629121752Sbrooks	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
630121752Sbrooks	    < (128*1024-CS_DMA_BUFFER_SIZE))
631121752Sbrooks	    sc->recv_ring+=16*1024;
63237785Smsmith
633121752Sbrooks	*/
63437785Smsmith
635121752Sbrooks	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
636121752Sbrooks	if (sc->buffer == NULL) {
637121752Sbrooks		if_printf(ifp, "Couldn't allocate memory for NIC\n");
638121752Sbrooks		return(0);
639121752Sbrooks	}
64037785Smsmith
641121752Sbrooks	/*
642121752Sbrooks	 * Initialize the media structures.
643121752Sbrooks	 */
644121752Sbrooks	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
64537785Smsmith
646121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_T) {
647121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
648121752Sbrooks		if (sc->chip_type != CS8900) {
649121752Sbrooks			ifmedia_add(&sc->media,
650121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
651121752Sbrooks			ifmedia_add(&sc->media,
652121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
653121752Sbrooks		}
654121752Sbrooks	}
65537785Smsmith
656121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_2)
657121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
65837785Smsmith
659121752Sbrooks	if (sc->adapter_cnf & A_CNF_AUI)
660121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
66137785Smsmith
662121752Sbrooks	if (sc->adapter_cnf & A_CNF_MEDIA)
663121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
66437785Smsmith
665121752Sbrooks	/* Set default media from EEPROM */
666121752Sbrooks	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
667121752Sbrooks	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
668121752Sbrooks	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
669121752Sbrooks	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
670121752Sbrooks	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
671122024Simp	default:
672122024Simp		if_printf(ifp, "no media, assuming 10baseT\n");
673122024Simp		sc->adapter_cnf |= A_CNF_10B_T;
674122024Simp		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
675122024Simp		if (sc->chip_type != CS8900) {
676122024Simp			ifmedia_add(&sc->media,
677122024Simp			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
678122024Simp			ifmedia_add(&sc->media,
679122024Simp			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
680122024Simp		}
681122024Simp		media = IFM_ETHER | IFM_10_T;
682122024Simp		break;
68337785Smsmith	}
684121752Sbrooks	ifmedia_set(&sc->media, media);
685121752Sbrooks	cs_mediaset(sc, media);
68637785Smsmith
687121752Sbrooks	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
688121752Sbrooks
68958816Simp	return (0);
69037785Smsmith}
69137785Smsmith
692140888Simpint
693140888Simpcs_detach(device_t dev)
694140888Simp{
695140888Simp	struct cs_softc *sc;
696140888Simp	struct ifnet *ifp;
697140888Simp
698140888Simp	sc = device_get_softc(dev);
699140888Simp	ifp = &sc->arpcom.ac_if;
700140888Simp
701140888Simp	cs_stop(sc);
702140888Simp	ifp->if_flags &= ~IFF_RUNNING;
703140888Simp	ether_ifdetach(ifp);
704140888Simp	cs_release_resources(dev);
705140888Simp	return (0);
706140888Simp}
707140888Simp
70837785Smsmith/*
70937785Smsmith * Initialize the board
71037785Smsmith */
71137785Smsmithstatic void
71237785Smsmithcs_init(void *xsc)
71337785Smsmith{
71437785Smsmith	struct cs_softc *sc=(struct cs_softc *)xsc;
71537785Smsmith	struct ifnet *ifp = &sc->arpcom.ac_if;
71641591Sarchie	int i, s, rx_cfg;
71737785Smsmith
71837785Smsmith	/*
71937785Smsmith	 * reset whatchdog timer
72037785Smsmith	 */
72137785Smsmith	ifp->if_timer=0;
72237785Smsmith	sc->buf_len = 0;
72337785Smsmith
72437785Smsmith	s=splimp();
72537785Smsmith
72637785Smsmith	/*
72737785Smsmith	 * Hardware initialization of cs
72837785Smsmith	 */
72937785Smsmith
73037785Smsmith	/* Enable receiver and transmitter */
73172940Simp	cs_writereg(sc, PP_LineCTL,
73272940Simp		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
73337785Smsmith
73437785Smsmith	/* Configure the receiver mode */
73537785Smsmith	cs_setmode(sc);
73637785Smsmith
73737785Smsmith	/*
73837785Smsmith	 * This defines what type of frames will cause interrupts
73937785Smsmith	 * Bad frames should generate interrupts so that the driver
74037785Smsmith	 * could track statistics of discarded packets
74137785Smsmith	 */
742140927Simp	rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
74337785Smsmith		 RX_EXTRA_DATA_ENBL;
74437785Smsmith	if (sc->isa_config & STREAM_TRANSFER)
74537785Smsmith		rx_cfg |= RX_STREAM_ENBL;
74672940Simp	cs_writereg(sc, PP_RxCFG, rx_cfg);
74772940Simp	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
74837785Smsmith		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
74937785Smsmith		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
75072940Simp	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
75137785Smsmith		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
75237785Smsmith		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
75337785Smsmith
754140927Simp	/* Write MAC address into IA filter */
755140927Simp	for (i=0; i<ETHER_ADDR_LEN/2; i++)
756140927Simp		cs_writereg(sc, PP_IA + i * 2,
75772940Simp		    sc->arpcom.ac_enaddr[i * 2] |
75872940Simp		    (sc->arpcom.ac_enaddr[i * 2 + 1] << 8) );
75937785Smsmith
76037785Smsmith	/*
76137785Smsmith	 * Now enable everything
76237785Smsmith	 */
76337785Smsmith/*
76437785Smsmith#ifdef	CS_USE_64K_DMA
76572940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
76672940Simp#else
767140927Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
76837785Smsmith#endif
76937785Smsmith*/
77072940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
77137785Smsmith
77237785Smsmith	/*
77337785Smsmith	 * Set running and clear output active flags
77437785Smsmith	 */
77537785Smsmith	sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
77637785Smsmith	sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
77737785Smsmith
77837785Smsmith	/*
77937785Smsmith	 * Start sending process
78037785Smsmith	 */
78137785Smsmith	cs_start(ifp);
78237785Smsmith
78337785Smsmith	(void) splx(s);
78437785Smsmith}
78537785Smsmith
78637785Smsmith/*
787106937Ssam * Get the packet from the board and send it to the upper layer.
78837785Smsmith */
78937785Smsmithstatic int
79037785Smsmithcs_get_packet(struct cs_softc *sc)
79137785Smsmith{
79237785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
79337785Smsmith	int iobase = sc->nic_addr, status, length;
79437785Smsmith	struct ether_header *eh;
79537785Smsmith	struct mbuf *m;
79637785Smsmith
79737785Smsmith#ifdef CS_DEBUG
79837785Smsmith	int i;
79937785Smsmith#endif
80037785Smsmith
80172940Simp	status = cs_inw(sc, RX_FRAME_PORT);
80272940Simp	length = cs_inw(sc, RX_FRAME_PORT);
80337785Smsmith
80437785Smsmith#ifdef CS_DEBUG
805104252Sbrooks	if_printf(ifp, "rcvd: stat %x, len %d\n",
806104252Sbrooks		status, length);
80737785Smsmith#endif
80837785Smsmith
80937785Smsmith	if (!(status & RX_OK)) {
81037785Smsmith#ifdef CS_DEBUG
811104252Sbrooks		if_printf(ifp, "bad pkt stat %x\n", status);
81237785Smsmith#endif
81337785Smsmith		ifp->if_ierrors++;
81437785Smsmith		return -1;
81537785Smsmith	}
81637785Smsmith
817111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
81837785Smsmith	if (m==NULL)
81937785Smsmith		return -1;
82037785Smsmith
82137785Smsmith	if (length > MHLEN) {
822111119Simp		MCLGET(m, M_DONTWAIT);
82337785Smsmith		if (!(m->m_flags & M_EXT)) {
82437785Smsmith			m_freem(m);
82537785Smsmith			return -1;
82637785Smsmith		}
82737785Smsmith	}
82837785Smsmith
82937785Smsmith	/* Initialize packet's header info */
83037785Smsmith	m->m_pkthdr.rcvif = ifp;
83137785Smsmith	m->m_pkthdr.len = length;
83237785Smsmith	m->m_len = length;
83337785Smsmith
83437785Smsmith	/* Get the data */
83537785Smsmith	insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
83637785Smsmith
83737785Smsmith	eh = mtod(m, struct ether_header *);
83837785Smsmith
83937785Smsmith#ifdef CS_DEBUG
84037785Smsmith	for (i=0;i<length;i++)
84137785Smsmith	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
84237785Smsmith	printf( "\n" );
84337785Smsmith#endif
84437785Smsmith
84537785Smsmith	if (status & (RX_IA | RX_BROADCAST) ||
84637785Smsmith	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
84737785Smsmith		/* Feed the packet to the upper layer */
848106937Ssam		(*ifp->if_input)(ifp, m);
84937785Smsmith		ifp->if_ipackets++;
850122024Simp		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
851140927Simp			DELAY(cs_recv_delay);
85237785Smsmith	} else {
85337785Smsmith		m_freem(m);
85437785Smsmith	}
85537785Smsmith
85637785Smsmith	return 0;
85737785Smsmith}
85837785Smsmith
85937785Smsmith/*
86058816Simp * Handle interrupts
86137785Smsmith */
86258816Simpvoid
86358816Simpcsintr(void *arg)
86437785Smsmith{
86558816Simp	struct cs_softc *sc = (struct cs_softc*) arg;
86637785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
86741591Sarchie	int status;
86837785Smsmith
86937785Smsmith#ifdef CS_DEBUG
870104252Sbrooks	if_printf(ifp, "Interrupt.\n");
87137785Smsmith#endif
87237785Smsmith
87372940Simp	while ((status=cs_inw(sc, ISQ_PORT))) {
87437785Smsmith
87537785Smsmith#ifdef CS_DEBUG
876104252Sbrooks		if_printf(ifp, "from ISQ: %04x\n", status);
87737785Smsmith#endif
87837785Smsmith
87937785Smsmith		switch (status & ISQ_EVENT_MASK) {
880140927Simp		case ISQ_RECEIVER_EVENT:
881140927Simp			cs_get_packet(sc);
882140927Simp			break;
88337785Smsmith
884140927Simp		case ISQ_TRANSMITTER_EVENT:
885140927Simp			if (status & TX_OK)
886140927Simp				ifp->if_opackets++;
887140927Simp			else
888140927Simp				ifp->if_oerrors++;
889140927Simp			ifp->if_flags &= ~IFF_OACTIVE;
890140927Simp			ifp->if_timer = 0;
891140927Simp			break;
89237785Smsmith
893140927Simp		case ISQ_BUFFER_EVENT:
894140927Simp			if (status & READY_FOR_TX) {
895140927Simp				ifp->if_flags &= ~IFF_OACTIVE;
896140927Simp				ifp->if_timer = 0;
897140927Simp			}
89837785Smsmith
899140927Simp			if (status & TX_UNDERRUN) {
900140927Simp				ifp->if_flags &= ~IFF_OACTIVE;
901140927Simp				ifp->if_timer = 0;
902140927Simp				ifp->if_oerrors++;
903140927Simp			}
904140927Simp			break;
90537785Smsmith
906140927Simp		case ISQ_RX_MISS_EVENT:
907140927Simp			ifp->if_ierrors+=(status>>6);
908140927Simp			break;
90937785Smsmith
910140927Simp		case ISQ_TX_COL_EVENT:
911140927Simp			ifp->if_collisions+=(status>>6);
912140927Simp			break;
913140927Simp		}
914140927Simp	}
91537785Smsmith
916140927Simp	if (!(ifp->if_flags & IFF_OACTIVE)) {
917140927Simp		cs_start(ifp);
918140927Simp	}
91937785Smsmith}
92037785Smsmith
92137785Smsmith/*
92237785Smsmith * Save the data in buffer
92337785Smsmith */
92437785Smsmith
92537785Smsmithstatic void
92637785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
92737785Smsmith{
92837785Smsmith	int len;
92937785Smsmith	struct mbuf *mp;
93037785Smsmith	unsigned char *data, *buf;
93137785Smsmith
93237785Smsmith	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
93337785Smsmith		len = mp->m_len;
93437785Smsmith
93537785Smsmith		/*
93637785Smsmith		 * Ignore empty parts
93737785Smsmith		 */
93837785Smsmith		if (!len)
93937785Smsmith		continue;
94037785Smsmith
94137785Smsmith		/*
94237785Smsmith		 * Find actual data address
94337785Smsmith		 */
94437785Smsmith		data = mtod(mp, caddr_t);
94537785Smsmith
94637785Smsmith		bcopy((caddr_t) data, (caddr_t) buf, len);
94737785Smsmith		buf += len;
94837785Smsmith		sc->buf_len += len;
94937785Smsmith	}
95037785Smsmith}
95137785Smsmith
95237785Smsmith
95337785Smsmithstatic void
95437785Smsmithcs_xmit_buf( struct cs_softc *sc )
95537785Smsmith{
95637785Smsmith	outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
95737785Smsmith	sc->buf_len = 0;
95837785Smsmith}
95937785Smsmith
96037785Smsmithstatic void
96137785Smsmithcs_start(struct ifnet *ifp)
96237785Smsmith{
96337785Smsmith	int s, length;
96437785Smsmith	struct mbuf *m, *mp;
96537785Smsmith	struct cs_softc *sc = ifp->if_softc;
96637785Smsmith
96737785Smsmith	s = splimp();
96837785Smsmith
96937785Smsmith	for (;;) {
97037785Smsmith		if (sc->buf_len)
97137785Smsmith			length = sc->buf_len;
97237785Smsmith		else {
97337785Smsmith			IF_DEQUEUE( &ifp->if_snd, m );
97437785Smsmith
97537785Smsmith			if (m==NULL) {
97637785Smsmith				(void) splx(s);
97737785Smsmith				return;
97837785Smsmith			}
97937785Smsmith
98037785Smsmith			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
98137785Smsmith				length += mp->m_len;
98237785Smsmith
98337785Smsmith			/* Skip zero-length packets */
98437785Smsmith			if (length == 0) {
98537785Smsmith				m_freem(m);
98637785Smsmith				continue;
98737785Smsmith			}
98837785Smsmith
98937785Smsmith			cs_write_mbufs(sc, m);
99037785Smsmith
991106937Ssam			BPF_MTAP(ifp, m);
99237785Smsmith
99337785Smsmith			m_freem(m);
99437785Smsmith		}
99537785Smsmith
99637785Smsmith		/*
99737785Smsmith		 * Issue a SEND command
99837785Smsmith		 */
99972940Simp		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
100072940Simp		cs_outw(sc, TX_LEN_PORT, length );
100137785Smsmith
100237785Smsmith		/*
100337785Smsmith		 * If there's no free space in the buffer then leave
100437785Smsmith		 * this packet for the next time: indicate output active
100537785Smsmith		 * and return.
100637785Smsmith		 */
100772940Simp		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
100837785Smsmith			ifp->if_timer = sc->buf_len;
100937785Smsmith			(void) splx(s);
101037785Smsmith			ifp->if_flags |= IFF_OACTIVE;
101137785Smsmith			return;
101237785Smsmith		}
101337785Smsmith
1014140927Simp		cs_xmit_buf(sc);
101537785Smsmith
101637785Smsmith		/*
101737785Smsmith		 * Set the watchdog timer in case we never hear
101837785Smsmith		 * from board again. (I don't know about correct
101937785Smsmith		 * value for this timeout)
102037785Smsmith		 */
102137785Smsmith		ifp->if_timer = length;
102237785Smsmith
102337785Smsmith		(void) splx(s);
102437785Smsmith		ifp->if_flags |= IFF_OACTIVE;
102537785Smsmith		return;
102637785Smsmith	}
102737785Smsmith}
102837785Smsmith
102937785Smsmith/*
103037785Smsmith * Stop everything on the interface
103137785Smsmith */
103237785Smsmithstatic void
103337785Smsmithcs_stop(struct cs_softc *sc)
103437785Smsmith{
103537785Smsmith	int s = splimp();
103637785Smsmith
103772940Simp	cs_writereg(sc, PP_RxCFG, 0);
103872940Simp	cs_writereg(sc, PP_TxCFG, 0);
103972940Simp	cs_writereg(sc, PP_BufCFG, 0);
104072940Simp	cs_writereg(sc, PP_BusCTL, 0);
104137785Smsmith
104237785Smsmith	sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
104337785Smsmith	sc->arpcom.ac_if.if_timer = 0;
104437785Smsmith
104537785Smsmith	(void) splx(s);
104637785Smsmith}
104737785Smsmith
104837785Smsmith/*
104937785Smsmith * Reset the interface
105037785Smsmith */
105137785Smsmithstatic void
105237785Smsmithcs_reset(struct cs_softc *sc)
105337785Smsmith{
105437785Smsmith	cs_stop(sc);
105537785Smsmith	cs_init(sc);
105637785Smsmith}
105737785Smsmith
105837785Smsmithstatic void
105937785Smsmithcs_setmode(struct cs_softc *sc)
106037785Smsmith{
106137785Smsmith	struct ifnet *ifp = &(sc->arpcom.ac_if);
106237785Smsmith	int rx_ctl;
106337785Smsmith
106437785Smsmith	/* Stop the receiver while changing filters */
106572940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
106637785Smsmith
106737785Smsmith	if (ifp->if_flags & IFF_PROMISC) {
106837785Smsmith		/* Turn on promiscuous mode. */
106937785Smsmith		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
107037785Smsmith	} else {
107137785Smsmith		if (ifp->if_flags & IFF_MULTICAST) {
107237785Smsmith			/* Allow receiving frames with multicast addresses */
107337785Smsmith			rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
107437785Smsmith				 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
107537785Smsmith			/*
107637785Smsmith			 * Here the reconfiguration of chip's multicast
107737785Smsmith			 * filters should be done but I've no idea about
107837785Smsmith			 * hash transformation in this chip. If you can
107937785Smsmith			 * add this code or describe me the transformation
108037785Smsmith			 * I'd be very glad.
108137785Smsmith			 */
108237785Smsmith		} else {
108337785Smsmith			/*
108437785Smsmith			 * Receive only good frames addressed for us and
108537785Smsmith			 * good broadcasts.
108637785Smsmith			 */
108737785Smsmith			rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
108837785Smsmith				 RX_OK_ACCEPT;
108937785Smsmith		}
109037785Smsmith	}
109137785Smsmith
109237785Smsmith	/* Set up the filter */
109372940Simp	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
109437785Smsmith
109537785Smsmith	/* Turn on receiver */
109672940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
109737785Smsmith}
109837785Smsmith
109937785Smsmithstatic int
110037785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
110137785Smsmith{
110237785Smsmith	struct cs_softc *sc=ifp->if_softc;
110337785Smsmith	struct ifreq *ifr = (struct ifreq *)data;
110437785Smsmith	int s,error=0;
110537785Smsmith
110637785Smsmith#ifdef CS_DEBUG
1107104252Sbrooks	if_printf(ifp, "ioctl(%lx)\n", command);
110837785Smsmith#endif
110937785Smsmith
111037785Smsmith	s=splimp();
111137785Smsmith
111237785Smsmith	switch (command) {
111337785Smsmith	case SIOCSIFFLAGS:
111437785Smsmith		/*
111537785Smsmith		 * Switch interface state between "running" and
111637785Smsmith		 * "stopped", reflecting the UP flag.
1117140927Simp		 */
1118140927Simp		if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1119140927Simp			if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1120140927Simp				cs_init(sc);
1121140927Simp			}
1122140927Simp		} else {
1123140927Simp			if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1124140927Simp				cs_stop(sc);
1125140927Simp			}
112637785Smsmith		}
112737785Smsmith		/*
112837785Smsmith		 * Promiscuous and/or multicast flags may have changed,
112937785Smsmith		 * so reprogram the multicast filter and/or receive mode.
113037785Smsmith		 *
113137785Smsmith		 * See note about multicasts in cs_setmode
113237785Smsmith		 */
113337785Smsmith		cs_setmode(sc);
113437785Smsmith		break;
113537785Smsmith
113637785Smsmith	case SIOCADDMULTI:
113737785Smsmith	case SIOCDELMULTI:
113837785Smsmith	    /*
113937785Smsmith	     * Multicast list has changed; set the hardware filter
114037785Smsmith	     * accordingly.
114137785Smsmith	     *
114237785Smsmith	     * See note about multicasts in cs_setmode
114337785Smsmith	     */
114437785Smsmith	    cs_setmode(sc);
114537785Smsmith	    error = 0;
114637785Smsmith	    break;
114737785Smsmith
1148140927Simp	case SIOCSIFMEDIA:
1149140927Simp	case SIOCGIFMEDIA:
1150140927Simp		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1151140927Simp		break;
115237785Smsmith
1153140927Simp	default:
1154140921Simp		error = ether_ioctl(ifp, command, data);
1155106937Ssam		break;
1156140927Simp	}
115737785Smsmith
115837785Smsmith	(void) splx(s);
115937785Smsmith	return error;
116037785Smsmith}
116137785Smsmith
116237785Smsmith/*
116337785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to
116437785Smsmith * generate an interrupt after a transmit has been started on it.
116537785Smsmith */
116637785Smsmithstatic void
116737785Smsmithcs_watchdog(struct ifnet *ifp)
116837785Smsmith{
116958816Simp	struct cs_softc *sc = ifp->if_softc;
117037785Smsmith
117137785Smsmith	ifp->if_oerrors++;
1172121816Sbrooks	log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
117337785Smsmith
117437785Smsmith	/* Reset the interface */
117537785Smsmith	if (ifp->if_flags & IFF_UP)
117637785Smsmith		cs_reset(sc);
117737785Smsmith	else
117837785Smsmith		cs_stop(sc);
117937785Smsmith}
118037785Smsmith
118137785Smsmithstatic int
118237785Smsmithcs_mediachange(struct ifnet *ifp)
118337785Smsmith{
118437785Smsmith	struct cs_softc *sc = ifp->if_softc;
118537785Smsmith	struct ifmedia *ifm = &sc->media;
118637785Smsmith
118737785Smsmith	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
118837785Smsmith		return EINVAL;
118937785Smsmith
119037785Smsmith	return cs_mediaset(sc, ifm->ifm_media);
119137785Smsmith}
119237785Smsmith
119337785Smsmithstatic void
119437785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
119537785Smsmith{
119637785Smsmith	int line_status;
119737785Smsmith	struct cs_softc *sc = ifp->if_softc;
119837785Smsmith
119937785Smsmith	ifmr->ifm_active = IFM_ETHER;
120072940Simp	line_status = cs_readreg(sc, PP_LineST);
120137785Smsmith	if (line_status & TENBASET_ON) {
120237785Smsmith		ifmr->ifm_active |= IFM_10_T;
120337785Smsmith		if (sc->chip_type != CS8900) {
120472940Simp			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
120537785Smsmith				ifmr->ifm_active |= IFM_FDX;
120672940Simp			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
120737785Smsmith				ifmr->ifm_active |= IFM_HDX;
120837785Smsmith		}
120937785Smsmith		ifmr->ifm_status = IFM_AVALID;
121037785Smsmith		if (line_status & LINK_OK)
121137785Smsmith			ifmr->ifm_status |= IFM_ACTIVE;
121237785Smsmith	} else {
121338305Smsmith		if (line_status & AUI_ON) {
121472940Simp			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
121572940Simp			    HCB1_ENBL);
121638305Smsmith			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
121772940Simp			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
121838305Smsmith				ifmr->ifm_active |= IFM_10_2;
121938305Smsmith			else
122038305Smsmith				ifmr->ifm_active |= IFM_10_5;
122138305Smsmith		}
122237785Smsmith	}
122337785Smsmith}
122437785Smsmith
122537785Smsmithstatic int
122637785Smsmithcs_mediaset(struct cs_softc *sc, int media)
122737785Smsmith{
1228140927Simp	int error;
122937785Smsmith
123037785Smsmith	/* Stop the receiver & transmitter */
123172940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
123272940Simp	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
123337785Smsmith
123437785Smsmith#ifdef CS_DEBUG
1235104252Sbrooks	if_printf(&sc->arpcom.ac_if, "cs_setmedia(%x)\n", media);
123637785Smsmith#endif
123737785Smsmith
123837785Smsmith	switch (IFM_SUBTYPE(media)) {
123937785Smsmith	default:
124037785Smsmith	case IFM_AUTO:
124138592Smsmith		if ((error=enable_tp(sc))==0)
124238592Smsmith			error = cs_duplex_auto(sc);
124343314Sdillon		else if ((error=enable_bnc(sc)) != 0)
124438592Smsmith			error = enable_aui(sc);
124537785Smsmith		break;
124637785Smsmith	case IFM_10_T:
124743314Sdillon		if ((error=enable_tp(sc)) != 0)
124837785Smsmith			break;
124937785Smsmith		if (media & IFM_FDX)
125037785Smsmith			cs_duplex_full(sc);
125137785Smsmith		else if (media & IFM_HDX)
125237785Smsmith			cs_duplex_half(sc);
125337785Smsmith		else
125437785Smsmith			error = cs_duplex_auto(sc);
125537785Smsmith		break;
125637785Smsmith	case IFM_10_2:
125737785Smsmith		error = enable_bnc(sc);
125837785Smsmith		break;
125937785Smsmith	case IFM_10_5:
126037785Smsmith		error = enable_aui(sc);
126137785Smsmith		break;
126237785Smsmith	}
126337785Smsmith
126437785Smsmith	/*
126537785Smsmith	 * Turn the transmitter & receiver back on
126637785Smsmith	 */
126772940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
126872940Simp	    SERIAL_RX_ON | SERIAL_TX_ON);
126937785Smsmith
127037785Smsmith	return error;
127137785Smsmith}
1272