if_cs.c revision 179591
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 179591 2008-06-06 04:56:27Z 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>
60156559Sphilip#include <net/if_dl.h>
6137785Smsmith#include <net/if_media.h>
62147256Sbrooks#include <net/if_types.h>
6337785Smsmith#include <net/ethernet.h>
6437785Smsmith#include <net/bpf.h>
6537785Smsmith
6672940Simp#include <dev/cs/if_csvar.h>
6758816Simp#include <dev/cs/if_csreg.h>
6837785Smsmith
6937785Smsmith#ifdef  CS_USE_64K_DMA
7037785Smsmith#define CS_DMA_BUFFER_SIZE 65536
7137785Smsmith#else
7237785Smsmith#define CS_DMA_BUFFER_SIZE 16384
7337785Smsmith#endif
7437785Smsmith
75122024Simpstatic void	cs_init(void *);
76179560Sjhbstatic void	cs_init_locked(struct cs_softc *);
77122024Simpstatic int	cs_ioctl(struct ifnet *, u_long, caddr_t);
78122024Simpstatic void	cs_start(struct ifnet *);
79179560Sjhbstatic void	cs_start_locked(struct ifnet *);
80122024Simpstatic void	cs_stop(struct cs_softc *);
81122024Simpstatic void	cs_reset(struct cs_softc *);
82179560Sjhbstatic void	cs_watchdog(void *);
8338592Smsmith
84122024Simpstatic int	cs_mediachange(struct ifnet *);
85122024Simpstatic void	cs_mediastatus(struct ifnet *, struct ifmediareq *);
86122024Simpstatic int      cs_mediaset(struct cs_softc *, int);
8758816Simp
8837785Smsmithstatic void	cs_write_mbufs(struct cs_softc*, struct mbuf*);
8937785Smsmithstatic void	cs_xmit_buf(struct cs_softc*);
9037785Smsmithstatic int	cs_get_packet(struct cs_softc*);
9137785Smsmithstatic void	cs_setmode(struct cs_softc*);
9237785Smsmith
93122024Simpstatic int	get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *);
94122024Simpstatic int	get_eeprom_cksum(int, int, uint16_t *);
9537785Smsmithstatic int	wait_eeprom_ready( struct cs_softc *);
9637785Smsmithstatic void	control_dc_dc( struct cs_softc *, int );
9737785Smsmithstatic int	send_test_pkt( struct cs_softc * );
9837785Smsmithstatic int	enable_tp(struct cs_softc *);
9937785Smsmithstatic int	enable_aui(struct cs_softc *);
10037785Smsmithstatic int	enable_bnc(struct cs_softc *);
10137785Smsmithstatic int      cs_duplex_auto(struct cs_softc *);
10237785Smsmith
10371316Simpdevclass_t cs_devclass;
10437785Smsmith
105122024Simp/* sysctl vars */
106122024SimpSYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
107122024Simp
108122024Simpint	cs_debug = 0;
109122024SimpTUNABLE_INT("hw.cs.debug", &cs_debug);
110122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, debug, CTLFLAG_RW,
111122024Simp    &cs_debug, 0,
112122024Simp  "cs debug");
113122024Simp
114122024Simpint	cs_ignore_cksum_failure = 0;
115122024SimpTUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure);
116122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW,
117122024Simp    &cs_ignore_cksum_failure, 0,
118122024Simp  "ignore checksum errors in cs card EEPROM");
119122024Simp
120122024Simpstatic int	cs_recv_delay = 570;
121140925SimpTUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay);
122122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, "");
123122024Simp
124179532Simpstatic int cs8900_eeint2irq[16] = {
125179532Simp	 10,  11,  12,   5, 255, 255, 255, 255,
126179532Simp	255, 255, 255, 255, 255, 255, 255, 255
127179532Simp};
128179532Simp
129179532Simpstatic int cs8900_irq2eeint[16] = {
130179532Simp	255, 255, 255, 255, 255,   3, 255, 255,
131179532Simp	255,   0,   1,   2, 255, 255, 255, 255
132179532Simp};
133179532Simp
13437785Smsmithstatic int
135122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
13637785Smsmith{
13737785Smsmith	int i;
13837785Smsmith
13937785Smsmith#ifdef CS_DEBUG
140179591Simp	device_printf(sc->sc_dev, "EEPROM data from %x for %x:\n", off, len);
14137785Smsmith#endif
14237785Smsmith
143122024Simp	for (i=0; i < len; i++) {
144122024Simp		if (wait_eeprom_ready(sc) < 0)
145122024Simp			return (-1);
14637785Smsmith		/* Send command to EEPROM to read */
14772940Simp		cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
148122024Simp		if (wait_eeprom_ready(sc) < 0)
14972940Simp			return (-1);
15072940Simp		buffer[i] = cs_readreg(sc, PP_EEData);
15137785Smsmith
15237785Smsmith#ifdef CS_DEBUG
153179591Simp		printf("%04x ",buffer[i]);
15437785Smsmith#endif
15537785Smsmith	}
15637785Smsmith
15737785Smsmith#ifdef CS_DEBUG
15837785Smsmith	printf("\n");
15937785Smsmith#endif
16072940Simp	return (0);
16137785Smsmith}
16237785Smsmith
16337785Smsmithstatic int
164122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer)
16537785Smsmith{
166122024Simp	int i;
167122024Simp	uint16_t cksum=0;
16837785Smsmith
169122024Simp	for (i = 0; i < len; i++)
170122024Simp		cksum += buffer[i];
17137785Smsmith	cksum &= 0xffff;
172179591Simp	if (cksum == 0 || cs_ignore_cksum_failure)
173122024Simp		return (0);
174122024Simp	return (-1);
17537785Smsmith}
17637785Smsmith
17737785Smsmithstatic int
17837785Smsmithwait_eeprom_ready(struct cs_softc *sc)
17937785Smsmith{
180122024Simp	DELAY(30000);	/* XXX should we do some checks here ? */
181140928Simp	return (0);
18237785Smsmith}
18337785Smsmith
18437785Smsmithstatic void
18537785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off)
18637785Smsmith{
18737785Smsmith	unsigned int self_control = HCB1_ENBL;
18837785Smsmith
18937785Smsmith	if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
19037785Smsmith		self_control |= HCB1;
19137785Smsmith	else
19237785Smsmith		self_control &= ~HCB1;
19372940Simp	cs_writereg(sc, PP_SelfCTL, self_control);
194122024Simp	DELAY(500000);
19537785Smsmith}
19637785Smsmith
19737785Smsmith
19837785Smsmithstatic int
19937785Smsmithcs_duplex_auto(struct cs_softc *sc)
20037785Smsmith{
201140927Simp	int i, error=0;
202140927Simp
20372940Simp	cs_writereg(sc, PP_AutoNegCTL,
20472940Simp	    RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
205140927Simp	for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
206140927Simp		if (i > 40000) {
207162321Sglebius			device_printf(sc->dev,
208140927Simp			    "full/half duplex auto negotiation timeout\n");
20937785Smsmith			error = ETIMEDOUT;
210140927Simp			break;
211140927Simp		}
212140927Simp		DELAY(1000);
213140927Simp	}
214140928Simp	return (error);
21537785Smsmith}
21637785Smsmith
21737785Smsmithstatic int
21837785Smsmithenable_tp(struct cs_softc *sc)
21937785Smsmith{
22037785Smsmith
22172940Simp	cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
22237785Smsmith	control_dc_dc(sc, 0);
223140928Simp	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++)
240147256Sbrooks		ether_address_backup[i] = sc->enaddr[i];
24138305Smsmith
24272940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_TX_ON);
243147256Sbrooks	bcopy(test_packet, sc->enaddr, ETHER_ADDR_LEN);
24472940Simp	bcopy(test_packet+ETHER_ADDR_LEN,
245147256Sbrooks	    sc->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++)
253147256Sbrooks			sc->enaddr[i] = ether_address_backup[i];
254140928Simp		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++)
262147256Sbrooks		sc->enaddr[i] = ether_address_backup[i];
263122024Simp	if ((cs_readreg(sc, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK)
264140928Simp		return (1);
265140928Simp	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
279179553Simp	if (!send_test_pkt(sc))
280140928Simp		return (EINVAL);
281140928Simp	return (0);
28237785Smsmith}
28337785Smsmith
28437785Smsmith/*
28537785Smsmith * XXX This was rewritten from Linux driver without any tests.
286140927Simp */
28737785Smsmithstatic int
28837785Smsmithenable_bnc(struct cs_softc *sc)
28937785Smsmith{
29037785Smsmith
29137785Smsmith	control_dc_dc(sc, 1);
29272940Simp	cs_writereg(sc, PP_LineCTL,
29372940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
29437785Smsmith
295179553Simp	if (!send_test_pkt(sc))
296140928Simp		return (EINVAL);
297140928Simp	return (0);
29837785Smsmith}
29937785Smsmith
30071316Simpint
30158816Simpcs_cs89x0_probe(device_t dev)
30237785Smsmith{
30358816Simp	int i;
30458816Simp	int error;
30558816Simp	u_long irq, junk;
30658816Simp	struct cs_softc *sc = device_get_softc(dev);
30737785Smsmith	unsigned rev_type = 0;
308122024Simp	uint16_t id;
30958816Simp	char chip_revision;
310122024Simp	uint16_t eeprom_buff[CHKSUM_LEN];
31137785Smsmith	int chip_type, pp_isaint, pp_isadma;
31237785Smsmith
31358816Simp	error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
31458816Simp	if (error)
31558816Simp		return (error);
31658816Simp
31772940Simp	sc->nic_addr = rman_get_start(sc->port_res);
31858816Simp
31972940Simp	if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
32037785Smsmith		/* Chip not detected. Let's try to reset it */
32137785Smsmith		if (bootverbose)
32258816Simp			device_printf(dev, "trying to reset the chip.\n");
32372940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
32472940Simp		i = cs_inw(sc, DATA_PORT);
32572940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
32672940Simp		cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
32772940Simp		if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
32858816Simp			return (ENXIO);
32937785Smsmith	}
33037785Smsmith
33172940Simp	for (i = 0; i < 10000; i++) {
33272940Simp		id = cs_readreg(sc, PP_ChipID);
33372940Simp		if (id == CHIP_EISA_ID_SIG)
33472940Simp			break;
33572940Simp	}
33672940Simp	if (i == 10000)
33758816Simp		return (ENXIO);
33837785Smsmith
33972940Simp	rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
34037785Smsmith	chip_type = rev_type & ~REVISON_BITS;
34137785Smsmith	chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
34237785Smsmith
34337785Smsmith	sc->chip_type = chip_type;
34458816Simp
345179532Simp	if (chip_type == CS8900) {
34637785Smsmith		pp_isaint = PP_CS8900_ISAINT;
34737785Smsmith		pp_isadma = PP_CS8900_ISADMA;
34837785Smsmith		sc->send_cmd = TX_CS8900_AFTER_ALL;
34937785Smsmith	} else {
35037785Smsmith		pp_isaint = PP_CS8920_ISAINT;
35137785Smsmith		pp_isadma = PP_CS8920_ISADMA;
35237785Smsmith		sc->send_cmd = TX_CS8920_AFTER_ALL;
35337785Smsmith	}
35437785Smsmith
35537785Smsmith	/*
356140927Simp	 * Clear some fields so that fail of EEPROM will left them clean
357140927Simp	 */
358140927Simp	sc->auto_neg_cnf = 0;
359140927Simp	sc->adapter_cnf  = 0;
360140927Simp	sc->isa_config   = 0;
361140927Simp
362140927Simp	/*
363179532Simp	 * If no interrupt specified, use what the board tells us.
36437785Smsmith	 */
36558816Simp	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
36658816Simp
36758816Simp	/*
36858816Simp	 * Get data from EEPROM
36958816Simp	 */
37072940Simp	if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
37158816Simp		device_printf(dev, "No EEPROM, assuming defaults.\n");
372179532Simp	} else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
373179532Simp		device_printf(dev, "EEPROM read failed, assuming defaults.\n");
374179532Simp	} else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
375179532Simp		device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n");
37637785Smsmith	} else {
377179532Simp		sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
378179532Simp		sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
379179532Simp		sc->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
380179532Simp		for (i=0; i<ETHER_ADDR_LEN/2; i++) {
381179532Simp			sc->enaddr[i*2] = eeprom_buff[i];
382179532Simp			sc->enaddr[i*2+1] = eeprom_buff[i] >> 8;
383179532Simp		}
384179532Simp		/*
385179532Simp		 * If no interrupt specified, use what the
386179532Simp		 * board tells us.
387179532Simp		 */
388179532Simp		if (error) {
389179532Simp			irq = sc->isa_config & INT_NO_MASK;
390179532Simp			error = 0;
391179532Simp			if (chip_type == CS8900) {
392179532Simp				irq = cs8900_eeint2irq[irq];
39337785Smsmith			} else {
394179532Simp				if (irq > CS8920_NO_INTS)
395179532Simp					irq = 255;
39637785Smsmith			}
397179532Simp			if (irq == 255) {
398179532Simp				device_printf(dev, "invalid irq in EEPROM.\n");
399179532Simp				error = EINVAL;
400179532Simp			}
401179532Simp			if (!error)
402179532Simp				bus_set_resource(dev, SYS_RES_IRQ, 0,
403179532Simp				    irq, 1);
404140927Simp		}
405140927Simp	}
40637785Smsmith
407179532Simp	if (!error && !(sc->flags & CS_NO_IRQ)) {
408140927Simp		if (chip_type == CS8900) {
409179532Simp			if (irq >= 0 || irq < 16)
410179532Simp				irq = cs8900_irq2eeint[irq];
411179532Simp			else
412179532Simp				irq = 255;
413140927Simp		} else {
414179532Simp			if (irq > CS8920_NO_INTS)
415179532Simp				irq = 255;
416140927Simp		}
417179532Simp		if (irq == 255)
418179532Simp			error = EINVAL;
41958816Simp	}
42058816Simp
421179507Simp	if (error) {
42258816Simp	       	device_printf(dev, "Unknown or invalid irq\n");
423179507Simp		return (error);
424140927Simp	}
425140927Simp
426179507Simp	if (!(sc->flags & CS_NO_IRQ))
427179507Simp		cs_writereg(sc, pp_isaint, irq);
428179507Simp
429140927Simp	/*
430140927Simp	 * Temporary disabled
431140927Simp	 *
432140927Simp	if (drq>0)
43372940Simp		cs_writereg(sc, pp_isadma, drq);
43437785Smsmith	else {
435104252Sbrooks		device_printf(dev, "incorrect drq\n",);
436140928Simp		return (0);
43737785Smsmith	}
438140927Simp	*/
43937785Smsmith
44037785Smsmith	if (bootverbose)
44158816Simp		 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
442179532Simp			chip_type == CS8900 ? '0' : '2',
443179532Simp			chip_type == CS8920M ? "M" : "",
44437785Smsmith			chip_revision,
44537785Smsmith			(sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
44637785Smsmith			(sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
44758816Simp			(sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
44837785Smsmith
449140927Simp	if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
450140927Simp	    (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
451140927Simp		sc->line_ctl = LOW_RX_SQUELCH;
452140927Simp	else
453140927Simp		sc->line_ctl = 0;
45437785Smsmith
455140928Simp	return (0);
45637785Smsmith}
45737785Smsmith
45837785Smsmith/*
45958816Simp * Allocate a port resource with the given resource id.
46058816Simp */
461179560Sjhbint
462179560Sjhbcs_alloc_port(device_t dev, int rid, int size)
46358816Simp{
464140927Simp	struct cs_softc *sc = device_get_softc(dev);
465140927Simp	struct resource *res;
46658816Simp
467140927Simp	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
468140927Simp	    0ul, ~0ul, size, RF_ACTIVE);
469140928Simp	if (res == NULL)
470140927Simp		return (ENOENT);
471140928Simp	sc->port_rid = rid;
472140928Simp	sc->port_res = res;
473140928Simp	return (0);
47458816Simp}
47558816Simp
47658816Simp/*
47758816Simp * Allocate a memory resource with the given resource id.
47858816Simp */
479179560Sjhbint
480179560Sjhbcs_alloc_memory(device_t dev, int rid, int size)
48158816Simp{
482140927Simp	struct cs_softc *sc = device_get_softc(dev);
483140927Simp	struct resource *res;
48458816Simp
485140927Simp	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
486140927Simp	    0ul, ~0ul, size, RF_ACTIVE);
487140928Simp	if (res == NULL)
488140927Simp		return (ENOENT);
489140928Simp	return (0);
49058816Simp}
49158816Simp
49258816Simp/*
49358816Simp * Allocate an irq resource with the given resource id.
49458816Simp */
495179560Sjhbint
496179560Sjhbcs_alloc_irq(device_t dev, int rid, int flags)
49758816Simp{
498140927Simp	struct cs_softc *sc = device_get_softc(dev);
499140927Simp	struct resource *res;
50058816Simp
501140927Simp	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
502140927Simp	    (RF_ACTIVE | flags));
503140928Simp	if (res == NULL)
504140927Simp		return (ENOENT);
505140928Simp	sc->irq_rid = rid;
506140928Simp	sc->irq_res = res;
507140928Simp	return (0);
50858816Simp}
50958816Simp
51058816Simp/*
51158816Simp * Release all resources
51258816Simp */
513179560Sjhbvoid
514179560Sjhbcs_release_resources(device_t dev)
51558816Simp{
516140927Simp	struct cs_softc *sc = device_get_softc(dev);
51758816Simp
518140927Simp	if (sc->port_res) {
519140927Simp		bus_release_resource(dev, SYS_RES_IOPORT,
520140927Simp		    sc->port_rid, sc->port_res);
521140927Simp		sc->port_res = 0;
522140927Simp	}
523140927Simp	if (sc->irq_res) {
524140927Simp		bus_release_resource(dev, SYS_RES_IRQ,
525140927Simp		    sc->irq_rid, sc->irq_res);
526140927Simp		sc->irq_res = 0;
527140927Simp	}
52858816Simp}
52958816Simp
53058816Simp/*
53137785Smsmith * Install the interface into kernel networking data structures
53237785Smsmith */
53371316Simpint
534121816Sbrookscs_attach(device_t dev)
53537785Smsmith{
536179560Sjhb	int error, media=0;
537121816Sbrooks	struct cs_softc *sc = device_get_softc(dev);;
538147256Sbrooks	struct ifnet *ifp;
53937785Smsmith
540162321Sglebius	sc->dev = dev;
541162321Sglebius
542147256Sbrooks	ifp = sc->ifp = if_alloc(IFT_ETHER);
543147256Sbrooks	if (ifp == NULL) {
544147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
545179560Sjhb		cs_release_resources(dev);
546179560Sjhb		return (ENOMEM);
547147256Sbrooks	}
548147256Sbrooks
549179560Sjhb	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
550179560Sjhb	    MTX_DEF);
551179560Sjhb	callout_init_mtx(&sc->timer, &sc->lock, 0);
55258816Simp
553179560Sjhb	CS_LOCK(sc);
554179560Sjhb	cs_stop(sc);
555179560Sjhb	CS_UNLOCK(sc);
556179560Sjhb
557121752Sbrooks	ifp->if_softc=sc;
558121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
559121752Sbrooks	ifp->if_start=cs_start;
560121752Sbrooks	ifp->if_ioctl=cs_ioctl;
561121752Sbrooks	ifp->if_init=cs_init;
562179560Sjhb	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
563121752Sbrooks	/*
564121752Sbrooks	 *  MIB DATA
565121752Sbrooks	 */
566121752Sbrooks	/*
567121752Sbrooks	ifp->if_linkmib=&sc->mibdata;
568121752Sbrooks	ifp->if_linkmiblen=sizeof sc->mibdata;
569121752Sbrooks	*/
57037785Smsmith
571179560Sjhb	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
57237785Smsmith
573121752Sbrooks	/*
574121752Sbrooks	 * this code still in progress (DMA support)
575121752Sbrooks	 *
57637785Smsmith
577121752Sbrooks	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
578121752Sbrooks	if (sc->recv_ring == NULL) {
579121816Sbrooks		log(LOG_ERR,
580121816Sbrooks		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
581121752Sbrooks		return(0);
582121752Sbrooks	}
583121752Sbrooks	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
584121752Sbrooks	    < (128*1024-CS_DMA_BUFFER_SIZE))
585121752Sbrooks	    sc->recv_ring+=16*1024;
58637785Smsmith
587121752Sbrooks	*/
58837785Smsmith
589121752Sbrooks	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
590121752Sbrooks	if (sc->buffer == NULL) {
591162321Sglebius		device_printf(sc->dev, "Couldn't allocate memory for NIC\n");
592179560Sjhb		if_free(ifp);
593179560Sjhb		mtx_destroy(&sc->lock);
594179560Sjhb		cs_release_resources(dev);
595179560Sjhb		return(ENOMEM);
596121752Sbrooks	}
59737785Smsmith
598121752Sbrooks	/*
599121752Sbrooks	 * Initialize the media structures.
600121752Sbrooks	 */
601121752Sbrooks	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
60237785Smsmith
603121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_T) {
604121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
605121752Sbrooks		if (sc->chip_type != CS8900) {
606121752Sbrooks			ifmedia_add(&sc->media,
607121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
608121752Sbrooks			ifmedia_add(&sc->media,
609121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
610121752Sbrooks		}
611121752Sbrooks	}
61237785Smsmith
613121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_2)
614121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
61537785Smsmith
616121752Sbrooks	if (sc->adapter_cnf & A_CNF_AUI)
617121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
61837785Smsmith
619121752Sbrooks	if (sc->adapter_cnf & A_CNF_MEDIA)
620121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
62137785Smsmith
622121752Sbrooks	/* Set default media from EEPROM */
623121752Sbrooks	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
624121752Sbrooks	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
625121752Sbrooks	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
626121752Sbrooks	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
627121752Sbrooks	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
628122024Simp	default:
629162321Sglebius		device_printf(sc->dev, "no media, assuming 10baseT\n");
630122024Simp		sc->adapter_cnf |= A_CNF_10B_T;
631122024Simp		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
632122024Simp		if (sc->chip_type != CS8900) {
633122024Simp			ifmedia_add(&sc->media,
634122024Simp			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
635122024Simp			ifmedia_add(&sc->media,
636122024Simp			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
637122024Simp		}
638122024Simp		media = IFM_ETHER | IFM_10_T;
639122024Simp		break;
64037785Smsmith	}
641121752Sbrooks	ifmedia_set(&sc->media, media);
642121752Sbrooks	cs_mediaset(sc, media);
64337785Smsmith
644147256Sbrooks	ether_ifattach(ifp, sc->enaddr);
645121752Sbrooks
646179560Sjhb  	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
647179560Sjhb	    NULL, csintr, sc, &sc->irq_handle);
648179560Sjhb	if (error) {
649179560Sjhb		ether_ifdetach(ifp);
650179560Sjhb		free(sc->buffer, M_DEVBUF);
651179560Sjhb		if_free(ifp);
652179560Sjhb		mtx_destroy(&sc->lock);
653179560Sjhb		cs_release_resources(dev);
654179560Sjhb		return (error);
655179560Sjhb	}
656179560Sjhb
65758816Simp	return (0);
65837785Smsmith}
65937785Smsmith
660140888Simpint
661140888Simpcs_detach(device_t dev)
662140888Simp{
663140888Simp	struct cs_softc *sc;
664140888Simp	struct ifnet *ifp;
665140888Simp
666140888Simp	sc = device_get_softc(dev);
667147256Sbrooks	ifp = sc->ifp;
668140888Simp
669179560Sjhb	CS_LOCK(sc);
670140888Simp	cs_stop(sc);
671179560Sjhb	CS_UNLOCK(sc);
672179560Sjhb	callout_drain(&sc->timer);
673140888Simp	ether_ifdetach(ifp);
674179560Sjhb	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
675150306Simp	cs_release_resources(dev);
676179560Sjhb	free(sc->buffer, M_DEVBUF);
677147256Sbrooks	if_free(ifp);
678179560Sjhb	mtx_destroy(&sc->lock);
679140888Simp	return (0);
680140888Simp}
681140888Simp
68237785Smsmith/*
68337785Smsmith * Initialize the board
68437785Smsmith */
68537785Smsmithstatic void
68637785Smsmithcs_init(void *xsc)
68737785Smsmith{
68837785Smsmith	struct cs_softc *sc=(struct cs_softc *)xsc;
689179560Sjhb
690179560Sjhb	CS_LOCK(sc);
691179560Sjhb	cs_init_locked(sc);
692179560Sjhb	CS_UNLOCK(sc);
693179560Sjhb}
694179560Sjhb
695179560Sjhbstatic void
696179560Sjhbcs_init_locked(struct cs_softc *sc)
697179560Sjhb{
698147256Sbrooks	struct ifnet *ifp = sc->ifp;
699179560Sjhb	int i, rx_cfg;
70037785Smsmith
70137785Smsmith	/*
702179560Sjhb	 * reset watchdog timer
70337785Smsmith	 */
704179560Sjhb	sc->tx_timeout = 0;
70537785Smsmith	sc->buf_len = 0;
70637785Smsmith
70737785Smsmith	/*
70837785Smsmith	 * Hardware initialization of cs
70937785Smsmith	 */
71037785Smsmith
71137785Smsmith	/* Enable receiver and transmitter */
71272940Simp	cs_writereg(sc, PP_LineCTL,
71372940Simp		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
71437785Smsmith
71537785Smsmith	/* Configure the receiver mode */
71637785Smsmith	cs_setmode(sc);
71737785Smsmith
71837785Smsmith	/*
71937785Smsmith	 * This defines what type of frames will cause interrupts
72037785Smsmith	 * Bad frames should generate interrupts so that the driver
72137785Smsmith	 * could track statistics of discarded packets
72237785Smsmith	 */
723140927Simp	rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
72437785Smsmith		 RX_EXTRA_DATA_ENBL;
72537785Smsmith	if (sc->isa_config & STREAM_TRANSFER)
72637785Smsmith		rx_cfg |= RX_STREAM_ENBL;
72772940Simp	cs_writereg(sc, PP_RxCFG, rx_cfg);
72872940Simp	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
72937785Smsmith		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
73037785Smsmith		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
73172940Simp	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
73237785Smsmith		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
73337785Smsmith		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
73437785Smsmith
735140927Simp	/* Write MAC address into IA filter */
736140927Simp	for (i=0; i<ETHER_ADDR_LEN/2; i++)
737140927Simp		cs_writereg(sc, PP_IA + i * 2,
738147256Sbrooks		    sc->enaddr[i * 2] |
739147256Sbrooks		    (sc->enaddr[i * 2 + 1] << 8) );
74037785Smsmith
74137785Smsmith	/*
74237785Smsmith	 * Now enable everything
74337785Smsmith	 */
74437785Smsmith/*
74537785Smsmith#ifdef	CS_USE_64K_DMA
74672940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
74772940Simp#else
748140927Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
74937785Smsmith#endif
75037785Smsmith*/
75172940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
75237785Smsmith
75337785Smsmith	/*
75437785Smsmith	 * Set running and clear output active flags
75537785Smsmith	 */
756148887Srwatson	sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
757148887Srwatson	sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
758179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
75937785Smsmith
76037785Smsmith	/*
76137785Smsmith	 * Start sending process
76237785Smsmith	 */
763179560Sjhb	cs_start_locked(ifp);
76437785Smsmith}
76537785Smsmith
76637785Smsmith/*
767106937Ssam * Get the packet from the board and send it to the upper layer.
76837785Smsmith */
76937785Smsmithstatic int
77037785Smsmithcs_get_packet(struct cs_softc *sc)
77137785Smsmith{
772147256Sbrooks	struct ifnet *ifp = sc->ifp;
77337785Smsmith	int iobase = sc->nic_addr, status, length;
77437785Smsmith	struct ether_header *eh;
77537785Smsmith	struct mbuf *m;
77637785Smsmith
77737785Smsmith#ifdef CS_DEBUG
77837785Smsmith	int i;
77937785Smsmith#endif
78037785Smsmith
78172940Simp	status = cs_inw(sc, RX_FRAME_PORT);
78272940Simp	length = cs_inw(sc, RX_FRAME_PORT);
78337785Smsmith
78437785Smsmith#ifdef CS_DEBUG
785162321Sglebius	device_printf(sc->dev, "rcvd: stat %x, len %d\n",
786104252Sbrooks		status, length);
78737785Smsmith#endif
78837785Smsmith
78937785Smsmith	if (!(status & RX_OK)) {
79037785Smsmith#ifdef CS_DEBUG
791162321Sglebius		device_printf(sc->dev, "bad pkt stat %x\n", status);
79237785Smsmith#endif
79337785Smsmith		ifp->if_ierrors++;
794140928Simp		return (-1);
79537785Smsmith	}
79637785Smsmith
797111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
79837785Smsmith	if (m==NULL)
799140928Simp		return (-1);
80037785Smsmith
80137785Smsmith	if (length > MHLEN) {
802111119Simp		MCLGET(m, M_DONTWAIT);
80337785Smsmith		if (!(m->m_flags & M_EXT)) {
80437785Smsmith			m_freem(m);
805140928Simp			return (-1);
80637785Smsmith		}
80737785Smsmith	}
80837785Smsmith
80937785Smsmith	/* Initialize packet's header info */
81037785Smsmith	m->m_pkthdr.rcvif = ifp;
81137785Smsmith	m->m_pkthdr.len = length;
81237785Smsmith	m->m_len = length;
81337785Smsmith
81437785Smsmith	/* Get the data */
81537785Smsmith	insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
81637785Smsmith
81737785Smsmith	eh = mtod(m, struct ether_header *);
81837785Smsmith
81937785Smsmith#ifdef CS_DEBUG
82037785Smsmith	for (i=0;i<length;i++)
82137785Smsmith	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
82237785Smsmith	printf( "\n" );
82337785Smsmith#endif
82437785Smsmith
82537785Smsmith	if (status & (RX_IA | RX_BROADCAST) ||
82637785Smsmith	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
82737785Smsmith		/* Feed the packet to the upper layer */
828106937Ssam		(*ifp->if_input)(ifp, m);
82937785Smsmith		ifp->if_ipackets++;
830122024Simp		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
831140927Simp			DELAY(cs_recv_delay);
83237785Smsmith	} else {
83337785Smsmith		m_freem(m);
83437785Smsmith	}
83537785Smsmith
836140928Simp	return (0);
83737785Smsmith}
83837785Smsmith
83937785Smsmith/*
84058816Simp * Handle interrupts
84137785Smsmith */
84258816Simpvoid
84358816Simpcsintr(void *arg)
84437785Smsmith{
84558816Simp	struct cs_softc *sc = (struct cs_softc*) arg;
846147256Sbrooks	struct ifnet *ifp = sc->ifp;
84741591Sarchie	int status;
84837785Smsmith
84937785Smsmith#ifdef CS_DEBUG
850162321Sglebius	device_printf(sc->dev, "Interrupt.\n");
85137785Smsmith#endif
85237785Smsmith
853179560Sjhb	CS_LOCK(sc);
85472940Simp	while ((status=cs_inw(sc, ISQ_PORT))) {
85537785Smsmith
85637785Smsmith#ifdef CS_DEBUG
857162321Sglebius		device_printf(sc->dev, "from ISQ: %04x\n", status);
85837785Smsmith#endif
85937785Smsmith
86037785Smsmith		switch (status & ISQ_EVENT_MASK) {
861140927Simp		case ISQ_RECEIVER_EVENT:
862140927Simp			cs_get_packet(sc);
863140927Simp			break;
86437785Smsmith
865140927Simp		case ISQ_TRANSMITTER_EVENT:
866140927Simp			if (status & TX_OK)
867140927Simp				ifp->if_opackets++;
868140927Simp			else
869140927Simp				ifp->if_oerrors++;
870148887Srwatson			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
871179560Sjhb			sc->tx_timeout = 0;
872140927Simp			break;
87337785Smsmith
874140927Simp		case ISQ_BUFFER_EVENT:
875140927Simp			if (status & READY_FOR_TX) {
876148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
877179560Sjhb				sc->tx_timeout = 0;
878140927Simp			}
87937785Smsmith
880140927Simp			if (status & TX_UNDERRUN) {
881148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
882179560Sjhb				sc->tx_timeout = 0;
883140927Simp				ifp->if_oerrors++;
884140927Simp			}
885140927Simp			break;
88637785Smsmith
887140927Simp		case ISQ_RX_MISS_EVENT:
888140927Simp			ifp->if_ierrors+=(status>>6);
889140927Simp			break;
89037785Smsmith
891140927Simp		case ISQ_TX_COL_EVENT:
892140927Simp			ifp->if_collisions+=(status>>6);
893140927Simp			break;
894140927Simp		}
895140927Simp	}
89637785Smsmith
897148887Srwatson	if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
898179560Sjhb		cs_start_locked(ifp);
899140927Simp	}
900179560Sjhb	CS_UNLOCK(sc);
90137785Smsmith}
90237785Smsmith
90337785Smsmith/*
90437785Smsmith * Save the data in buffer
90537785Smsmith */
90637785Smsmith
90737785Smsmithstatic void
90837785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
90937785Smsmith{
91037785Smsmith	int len;
91137785Smsmith	struct mbuf *mp;
91237785Smsmith	unsigned char *data, *buf;
91337785Smsmith
91437785Smsmith	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
91537785Smsmith		len = mp->m_len;
91637785Smsmith
91737785Smsmith		/*
91837785Smsmith		 * Ignore empty parts
91937785Smsmith		 */
92037785Smsmith		if (!len)
92137785Smsmith		continue;
92237785Smsmith
92337785Smsmith		/*
92437785Smsmith		 * Find actual data address
92537785Smsmith		 */
92637785Smsmith		data = mtod(mp, caddr_t);
92737785Smsmith
92837785Smsmith		bcopy((caddr_t) data, (caddr_t) buf, len);
92937785Smsmith		buf += len;
93037785Smsmith		sc->buf_len += len;
93137785Smsmith	}
93237785Smsmith}
93337785Smsmith
93437785Smsmith
93537785Smsmithstatic void
93637785Smsmithcs_xmit_buf( struct cs_softc *sc )
93737785Smsmith{
93837785Smsmith	outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
93937785Smsmith	sc->buf_len = 0;
94037785Smsmith}
94137785Smsmith
94237785Smsmithstatic void
94337785Smsmithcs_start(struct ifnet *ifp)
94437785Smsmith{
945179560Sjhb	struct cs_softc *sc = ifp->if_softc;
946179560Sjhb
947179560Sjhb	CS_LOCK(sc);
948179560Sjhb	cs_start_locked(ifp);
949179560Sjhb	CS_UNLOCK(sc);
950179560Sjhb}
951179560Sjhb
952179560Sjhbstatic void
953179560Sjhbcs_start_locked(struct ifnet *ifp)
954179560Sjhb{
955179560Sjhb	int length;
95637785Smsmith	struct mbuf *m, *mp;
95737785Smsmith	struct cs_softc *sc = ifp->if_softc;
95837785Smsmith
95937785Smsmith	for (;;) {
96037785Smsmith		if (sc->buf_len)
96137785Smsmith			length = sc->buf_len;
96237785Smsmith		else {
96337785Smsmith			IF_DEQUEUE( &ifp->if_snd, m );
96437785Smsmith
96537785Smsmith			if (m==NULL) {
96637785Smsmith				return;
96737785Smsmith			}
96837785Smsmith
96937785Smsmith			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
97037785Smsmith				length += mp->m_len;
97137785Smsmith
97237785Smsmith			/* Skip zero-length packets */
97337785Smsmith			if (length == 0) {
97437785Smsmith				m_freem(m);
97537785Smsmith				continue;
97637785Smsmith			}
97737785Smsmith
97837785Smsmith			cs_write_mbufs(sc, m);
97937785Smsmith
980106937Ssam			BPF_MTAP(ifp, m);
98137785Smsmith
98237785Smsmith			m_freem(m);
98337785Smsmith		}
98437785Smsmith
98537785Smsmith		/*
98637785Smsmith		 * Issue a SEND command
98737785Smsmith		 */
98872940Simp		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
98972940Simp		cs_outw(sc, TX_LEN_PORT, length );
99037785Smsmith
99137785Smsmith		/*
99237785Smsmith		 * If there's no free space in the buffer then leave
99337785Smsmith		 * this packet for the next time: indicate output active
99437785Smsmith		 * and return.
99537785Smsmith		 */
99672940Simp		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
997179560Sjhb			sc->tx_timeout = sc->buf_len;
998148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
99937785Smsmith			return;
100037785Smsmith		}
100137785Smsmith
1002140927Simp		cs_xmit_buf(sc);
100337785Smsmith
100437785Smsmith		/*
100537785Smsmith		 * Set the watchdog timer in case we never hear
100637785Smsmith		 * from board again. (I don't know about correct
100737785Smsmith		 * value for this timeout)
100837785Smsmith		 */
1009179560Sjhb		sc->tx_timeout = length;
101037785Smsmith
1011148887Srwatson		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
101237785Smsmith		return;
101337785Smsmith	}
101437785Smsmith}
101537785Smsmith
101637785Smsmith/*
101737785Smsmith * Stop everything on the interface
101837785Smsmith */
101937785Smsmithstatic void
102037785Smsmithcs_stop(struct cs_softc *sc)
102137785Smsmith{
102237785Smsmith
1023179560Sjhb	CS_ASSERT_LOCKED(sc);
102472940Simp	cs_writereg(sc, PP_RxCFG, 0);
102572940Simp	cs_writereg(sc, PP_TxCFG, 0);
102672940Simp	cs_writereg(sc, PP_BufCFG, 0);
102772940Simp	cs_writereg(sc, PP_BusCTL, 0);
102837785Smsmith
1029148887Srwatson	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1030179560Sjhb	sc->tx_timeout = 0;
1031179560Sjhb	callout_stop(&sc->timer);
103237785Smsmith}
103337785Smsmith
103437785Smsmith/*
103537785Smsmith * Reset the interface
103637785Smsmith */
103737785Smsmithstatic void
103837785Smsmithcs_reset(struct cs_softc *sc)
103937785Smsmith{
1040179560Sjhb
1041179560Sjhb	CS_ASSERT_LOCKED(sc);
104237785Smsmith	cs_stop(sc);
1043179560Sjhb	cs_init_locked(sc);
104437785Smsmith}
104537785Smsmith
1046156559Sphilipstatic uint16_t
1047156559Sphilipcs_hash_index(struct sockaddr_dl *addr)
1048156559Sphilip{
1049156559Sphilip	uint32_t crc;
1050156559Sphilip	uint16_t idx;
1051156559Sphilip	caddr_t lla;
1052156559Sphilip
1053156559Sphilip	lla = LLADDR(addr);
1054156559Sphilip	crc = ether_crc32_le(lla, ETHER_ADDR_LEN);
1055156559Sphilip	idx = crc >> 26;
1056156559Sphilip
1057156559Sphilip	return (idx);
1058156559Sphilip}
1059156559Sphilip
106037785Smsmithstatic void
106137785Smsmithcs_setmode(struct cs_softc *sc)
106237785Smsmith{
1063156559Sphilip	int rx_ctl;
1064156559Sphilip	uint16_t af[4];
1065156559Sphilip	uint16_t port, mask, index;
1066147256Sbrooks	struct ifnet *ifp = sc->ifp;
1067156559Sphilip	struct ifmultiaddr *ifma;
106837785Smsmith
106937785Smsmith	/* Stop the receiver while changing filters */
107072940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
107137785Smsmith
107237785Smsmith	if (ifp->if_flags & IFF_PROMISC) {
107337785Smsmith		/* Turn on promiscuous mode. */
107437785Smsmith		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1075156559Sphilip	} else if (ifp->if_flags & IFF_MULTICAST) {
1076156559Sphilip		/* Allow receiving frames with multicast addresses */
1077156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1078156559Sphilip			 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1079156559Sphilip
1080156559Sphilip		/* Start with an empty filter */
1081156559Sphilip		af[0] = af[1] = af[2] = af[3] = 0x0000;
1082156559Sphilip
1083156559Sphilip		if (ifp->if_flags & IFF_ALLMULTI) {
1084156559Sphilip			/* Accept all multicast frames */
1085156559Sphilip			af[0] = af[1] = af[2] = af[3] = 0xffff;
108637785Smsmith		} else {
1087156559Sphilip			/*
1088156559Sphilip			 * Set up the filter to only accept multicast
1089156559Sphilip			 * frames we're interested in.
109037785Smsmith			 */
1091156559Sphilip			IF_ADDR_LOCK(ifp);
1092156559Sphilip			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1093156559Sphilip				struct sockaddr_dl *dl =
1094156559Sphilip				    (struct sockaddr_dl *)ifma->ifma_addr;
1095156559Sphilip
1096156559Sphilip				index = cs_hash_index(dl);
1097156559Sphilip				port = (u_int16_t) (index >> 4);
1098156559Sphilip				mask = (u_int16_t) (1 << (index & 0xf));
1099156559Sphilip				af[port] |= mask;
1100156559Sphilip			}
1101156559Sphilip			IF_ADDR_UNLOCK(ifp);
110237785Smsmith		}
1103156559Sphilip
1104156559Sphilip		cs_writereg(sc, PP_LAF + 0, af[0]);
1105156559Sphilip		cs_writereg(sc, PP_LAF + 2, af[1]);
1106156559Sphilip		cs_writereg(sc, PP_LAF + 4, af[2]);
1107156559Sphilip		cs_writereg(sc, PP_LAF + 6, af[3]);
1108156559Sphilip	} else {
1109156559Sphilip		/*
1110156559Sphilip		 * Receive only good frames addressed for us and
1111156559Sphilip		 * good broadcasts.
1112156559Sphilip		 */
1113156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1114156559Sphilip			 RX_OK_ACCEPT;
111537785Smsmith	}
111637785Smsmith
111737785Smsmith	/* Set up the filter */
111872940Simp	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
111937785Smsmith
112037785Smsmith	/* Turn on receiver */
112172940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
112237785Smsmith}
112337785Smsmith
112437785Smsmithstatic int
112537785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
112637785Smsmith{
112737785Smsmith	struct cs_softc *sc=ifp->if_softc;
112837785Smsmith	struct ifreq *ifr = (struct ifreq *)data;
1129179560Sjhb	int error=0;
113037785Smsmith
113137785Smsmith#ifdef CS_DEBUG
1132162321Sglebius	if_printf(ifp, "%s command=%lx\n", __func__, command);
113337785Smsmith#endif
113437785Smsmith
113537785Smsmith	switch (command) {
113637785Smsmith	case SIOCSIFFLAGS:
113737785Smsmith		/*
113837785Smsmith		 * Switch interface state between "running" and
113937785Smsmith		 * "stopped", reflecting the UP flag.
1140140927Simp		 */
1141179560Sjhb		CS_LOCK(sc);
1142147256Sbrooks		if (sc->ifp->if_flags & IFF_UP) {
1143148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) {
1144179560Sjhb				cs_init_locked(sc);
1145140927Simp			}
1146140927Simp		} else {
1147148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) {
1148140927Simp				cs_stop(sc);
1149140927Simp			}
1150179560Sjhb		}
115137785Smsmith		/*
115237785Smsmith		 * Promiscuous and/or multicast flags may have changed,
115337785Smsmith		 * so reprogram the multicast filter and/or receive mode.
115437785Smsmith		 *
115537785Smsmith		 * See note about multicasts in cs_setmode
115637785Smsmith		 */
115737785Smsmith		cs_setmode(sc);
1158179560Sjhb		CS_UNLOCK(sc);
115937785Smsmith		break;
116037785Smsmith
116137785Smsmith	case SIOCADDMULTI:
116237785Smsmith	case SIOCDELMULTI:
116337785Smsmith	    /*
116437785Smsmith	     * Multicast list has changed; set the hardware filter
116537785Smsmith	     * accordingly.
116637785Smsmith	     *
116737785Smsmith	     * See note about multicasts in cs_setmode
116837785Smsmith	     */
1169179560Sjhb	    CS_LOCK(sc);
117037785Smsmith	    cs_setmode(sc);
1171179560Sjhb	    CS_UNLOCK(sc);
117237785Smsmith	    error = 0;
117337785Smsmith	    break;
117437785Smsmith
1175140927Simp	case SIOCSIFMEDIA:
1176140927Simp	case SIOCGIFMEDIA:
1177140927Simp		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1178140927Simp		break;
117937785Smsmith
1180140927Simp	default:
1181140921Simp		error = ether_ioctl(ifp, command, data);
1182106937Ssam		break;
1183140927Simp	}
118437785Smsmith
1185140928Simp	return (error);
118637785Smsmith}
118737785Smsmith
118837785Smsmith/*
118937785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to
119037785Smsmith * generate an interrupt after a transmit has been started on it.
119137785Smsmith */
119237785Smsmithstatic void
1193179560Sjhbcs_watchdog(void *arg)
119437785Smsmith{
1195179560Sjhb	struct cs_softc *sc = arg;
1196179560Sjhb	struct ifnet *ifp = sc->ifp;
119737785Smsmith
1198179560Sjhb	CS_ASSERT_LOCKED(sc);
1199179560Sjhb	if (sc->tx_timeout && --sc->tx_timeout == 0) {
1200179560Sjhb		ifp->if_oerrors++;
1201179560Sjhb		log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
120237785Smsmith
1203179560Sjhb		/* Reset the interface */
1204179560Sjhb		if (ifp->if_flags & IFF_UP)
1205179560Sjhb			cs_reset(sc);
1206179560Sjhb		else
1207179560Sjhb			cs_stop(sc);
1208179560Sjhb	}
1209179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
121037785Smsmith}
121137785Smsmith
121237785Smsmithstatic int
121337785Smsmithcs_mediachange(struct ifnet *ifp)
121437785Smsmith{
121537785Smsmith	struct cs_softc *sc = ifp->if_softc;
121637785Smsmith	struct ifmedia *ifm = &sc->media;
1217179560Sjhb	int error;
121837785Smsmith
121937785Smsmith	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1220140928Simp		return (EINVAL);
122137785Smsmith
1222179560Sjhb	CS_LOCK(sc);
1223179560Sjhb	error = cs_mediaset(sc, ifm->ifm_media);
1224179560Sjhb	CS_UNLOCK(sc);
1225179560Sjhb	return (error);
122637785Smsmith}
122737785Smsmith
122837785Smsmithstatic void
122937785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
123037785Smsmith{
123137785Smsmith	int line_status;
123237785Smsmith	struct cs_softc *sc = ifp->if_softc;
123337785Smsmith
1234179560Sjhb	CS_LOCK(sc);
123537785Smsmith	ifmr->ifm_active = IFM_ETHER;
123672940Simp	line_status = cs_readreg(sc, PP_LineST);
123737785Smsmith	if (line_status & TENBASET_ON) {
123837785Smsmith		ifmr->ifm_active |= IFM_10_T;
123937785Smsmith		if (sc->chip_type != CS8900) {
124072940Simp			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
124137785Smsmith				ifmr->ifm_active |= IFM_FDX;
124272940Simp			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
124337785Smsmith				ifmr->ifm_active |= IFM_HDX;
124437785Smsmith		}
124537785Smsmith		ifmr->ifm_status = IFM_AVALID;
124637785Smsmith		if (line_status & LINK_OK)
124737785Smsmith			ifmr->ifm_status |= IFM_ACTIVE;
124837785Smsmith	} else {
124938305Smsmith		if (line_status & AUI_ON) {
125072940Simp			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
125172940Simp			    HCB1_ENBL);
125238305Smsmith			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
125372940Simp			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
125438305Smsmith				ifmr->ifm_active |= IFM_10_2;
125538305Smsmith			else
125638305Smsmith				ifmr->ifm_active |= IFM_10_5;
125738305Smsmith		}
125837785Smsmith	}
1259179560Sjhb	CS_UNLOCK(sc);
126037785Smsmith}
126137785Smsmith
126237785Smsmithstatic int
126337785Smsmithcs_mediaset(struct cs_softc *sc, int media)
126437785Smsmith{
1265179532Simp	int error = 0;
126637785Smsmith
126737785Smsmith	/* Stop the receiver & transmitter */
126872940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
126972940Simp	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
127037785Smsmith
127137785Smsmith#ifdef CS_DEBUG
1272162321Sglebius	device_printf(sc->dev, "%s media=%x\n", __func__, media);
127337785Smsmith#endif
127437785Smsmith
127537785Smsmith	switch (IFM_SUBTYPE(media)) {
127637785Smsmith	default:
127737785Smsmith	case IFM_AUTO:
1278179553Simp		/*
1279179553Simp		 * This chip makes it a little hard to support this, so treat
1280179553Simp		 * it as IFM_10_T, auto duplex.
1281179553Simp		 */
1282179553Simp		enable_tp(sc);
1283179553Simp		cs_duplex_auto(sc);
128437785Smsmith		break;
128537785Smsmith	case IFM_10_T:
1286179532Simp		enable_tp(sc);
128737785Smsmith		if (media & IFM_FDX)
128837785Smsmith			cs_duplex_full(sc);
128937785Smsmith		else if (media & IFM_HDX)
129037785Smsmith			cs_duplex_half(sc);
129137785Smsmith		else
129237785Smsmith			error = cs_duplex_auto(sc);
129337785Smsmith		break;
129437785Smsmith	case IFM_10_2:
129537785Smsmith		error = enable_bnc(sc);
129637785Smsmith		break;
129737785Smsmith	case IFM_10_5:
129837785Smsmith		error = enable_aui(sc);
129937785Smsmith		break;
130037785Smsmith	}
130137785Smsmith
130237785Smsmith	/*
130337785Smsmith	 * Turn the transmitter & receiver back on
130437785Smsmith	 */
130572940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
130672940Simp	    SERIAL_RX_ON | SERIAL_TX_ON);
130737785Smsmith
1308140928Simp	return (error);
130937785Smsmith}
1310