if_cs.c revision 179597
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 179597 2008-06-06 05:25:24Z 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	enable_tp(struct cs_softc *);
9837785Smsmithstatic int	enable_aui(struct cs_softc *);
9937785Smsmithstatic int	enable_bnc(struct cs_softc *);
10037785Smsmithstatic int      cs_duplex_auto(struct cs_softc *);
10137785Smsmith
10271316Simpdevclass_t cs_devclass;
103179597Simpdriver_intr_t	csintr;
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++) {
206179597Simp		if (i > 4000) {
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
22637785Smsmithstatic int
22737785Smsmithenable_aui(struct cs_softc *sc)
22837785Smsmith{
22937785Smsmith
23072940Simp	cs_writereg(sc, PP_LineCTL,
23172940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
232179597Simp	control_dc_dc(sc, 0);
233140928Simp	return (0);
23437785Smsmith}
23537785Smsmith
23637785Smsmithstatic int
23737785Smsmithenable_bnc(struct cs_softc *sc)
23837785Smsmith{
23937785Smsmith
24072940Simp	cs_writereg(sc, PP_LineCTL,
24172940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
242179597Simp	control_dc_dc(sc, 1);
243140928Simp	return (0);
24437785Smsmith}
24537785Smsmith
24671316Simpint
24758816Simpcs_cs89x0_probe(device_t dev)
24837785Smsmith{
24958816Simp	int i;
25058816Simp	int error;
25158816Simp	u_long irq, junk;
25258816Simp	struct cs_softc *sc = device_get_softc(dev);
25337785Smsmith	unsigned rev_type = 0;
254122024Simp	uint16_t id;
25558816Simp	char chip_revision;
256122024Simp	uint16_t eeprom_buff[CHKSUM_LEN];
25737785Smsmith	int chip_type, pp_isaint, pp_isadma;
25837785Smsmith
25958816Simp	error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
26058816Simp	if (error)
26158816Simp		return (error);
26258816Simp
26372940Simp	sc->nic_addr = rman_get_start(sc->port_res);
26458816Simp
26572940Simp	if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
26637785Smsmith		/* Chip not detected. Let's try to reset it */
26737785Smsmith		if (bootverbose)
26858816Simp			device_printf(dev, "trying to reset the chip.\n");
26972940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
27072940Simp		i = cs_inw(sc, DATA_PORT);
27172940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
27272940Simp		cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
27372940Simp		if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
27458816Simp			return (ENXIO);
27537785Smsmith	}
27637785Smsmith
27772940Simp	for (i = 0; i < 10000; i++) {
27872940Simp		id = cs_readreg(sc, PP_ChipID);
27972940Simp		if (id == CHIP_EISA_ID_SIG)
28072940Simp			break;
28172940Simp	}
28272940Simp	if (i == 10000)
28358816Simp		return (ENXIO);
28437785Smsmith
28572940Simp	rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
28637785Smsmith	chip_type = rev_type & ~REVISON_BITS;
28737785Smsmith	chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
28837785Smsmith
28937785Smsmith	sc->chip_type = chip_type;
29058816Simp
291179532Simp	if (chip_type == CS8900) {
29237785Smsmith		pp_isaint = PP_CS8900_ISAINT;
29337785Smsmith		pp_isadma = PP_CS8900_ISADMA;
29437785Smsmith		sc->send_cmd = TX_CS8900_AFTER_ALL;
29537785Smsmith	} else {
29637785Smsmith		pp_isaint = PP_CS8920_ISAINT;
29737785Smsmith		pp_isadma = PP_CS8920_ISADMA;
29837785Smsmith		sc->send_cmd = TX_CS8920_AFTER_ALL;
29937785Smsmith	}
30037785Smsmith
30137785Smsmith	/*
302140927Simp	 * Clear some fields so that fail of EEPROM will left them clean
303140927Simp	 */
304140927Simp	sc->auto_neg_cnf = 0;
305140927Simp	sc->adapter_cnf  = 0;
306140927Simp	sc->isa_config   = 0;
307140927Simp
308140927Simp	/*
309179532Simp	 * If no interrupt specified, use what the board tells us.
31037785Smsmith	 */
31158816Simp	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
31258816Simp
31358816Simp	/*
31458816Simp	 * Get data from EEPROM
31558816Simp	 */
31672940Simp	if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
31758816Simp		device_printf(dev, "No EEPROM, assuming defaults.\n");
318179532Simp	} else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
319179532Simp		device_printf(dev, "EEPROM read failed, assuming defaults.\n");
320179532Simp	} else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
321179532Simp		device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n");
32237785Smsmith	} else {
323179532Simp		sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
324179532Simp		sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
325179532Simp		sc->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
326179532Simp		for (i=0; i<ETHER_ADDR_LEN/2; i++) {
327179532Simp			sc->enaddr[i*2] = eeprom_buff[i];
328179532Simp			sc->enaddr[i*2+1] = eeprom_buff[i] >> 8;
329179532Simp		}
330179532Simp		/*
331179532Simp		 * If no interrupt specified, use what the
332179532Simp		 * board tells us.
333179532Simp		 */
334179532Simp		if (error) {
335179532Simp			irq = sc->isa_config & INT_NO_MASK;
336179532Simp			error = 0;
337179532Simp			if (chip_type == CS8900) {
338179532Simp				irq = cs8900_eeint2irq[irq];
33937785Smsmith			} else {
340179532Simp				if (irq > CS8920_NO_INTS)
341179532Simp					irq = 255;
34237785Smsmith			}
343179532Simp			if (irq == 255) {
344179532Simp				device_printf(dev, "invalid irq in EEPROM.\n");
345179532Simp				error = EINVAL;
346179532Simp			}
347179532Simp			if (!error)
348179532Simp				bus_set_resource(dev, SYS_RES_IRQ, 0,
349179532Simp				    irq, 1);
350140927Simp		}
351140927Simp	}
35237785Smsmith
353179532Simp	if (!error && !(sc->flags & CS_NO_IRQ)) {
354140927Simp		if (chip_type == CS8900) {
355179532Simp			if (irq >= 0 || irq < 16)
356179532Simp				irq = cs8900_irq2eeint[irq];
357179532Simp			else
358179532Simp				irq = 255;
359140927Simp		} else {
360179532Simp			if (irq > CS8920_NO_INTS)
361179532Simp				irq = 255;
362140927Simp		}
363179532Simp		if (irq == 255)
364179532Simp			error = EINVAL;
36558816Simp	}
36658816Simp
367179507Simp	if (error) {
36858816Simp	       	device_printf(dev, "Unknown or invalid irq\n");
369179507Simp		return (error);
370140927Simp	}
371140927Simp
372179507Simp	if (!(sc->flags & CS_NO_IRQ))
373179507Simp		cs_writereg(sc, pp_isaint, irq);
374179507Simp
375140927Simp	/*
376140927Simp	 * Temporary disabled
377140927Simp	 *
378140927Simp	if (drq>0)
37972940Simp		cs_writereg(sc, pp_isadma, drq);
38037785Smsmith	else {
381104252Sbrooks		device_printf(dev, "incorrect drq\n",);
382140928Simp		return (0);
38337785Smsmith	}
384140927Simp	*/
38537785Smsmith
38637785Smsmith	if (bootverbose)
38758816Simp		 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
388179532Simp			chip_type == CS8900 ? '0' : '2',
389179532Simp			chip_type == CS8920M ? "M" : "",
39037785Smsmith			chip_revision,
39137785Smsmith			(sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
39237785Smsmith			(sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
39358816Simp			(sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
39437785Smsmith
395140927Simp	if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
396140927Simp	    (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
397140927Simp		sc->line_ctl = LOW_RX_SQUELCH;
398140927Simp	else
399140927Simp		sc->line_ctl = 0;
40037785Smsmith
401140928Simp	return (0);
40237785Smsmith}
40337785Smsmith
40437785Smsmith/*
40558816Simp * Allocate a port resource with the given resource id.
40658816Simp */
407179560Sjhbint
408179560Sjhbcs_alloc_port(device_t dev, int rid, int size)
40958816Simp{
410140927Simp	struct cs_softc *sc = device_get_softc(dev);
411140927Simp	struct resource *res;
41258816Simp
413140927Simp	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
414140927Simp	    0ul, ~0ul, size, RF_ACTIVE);
415140928Simp	if (res == NULL)
416140927Simp		return (ENOENT);
417140928Simp	sc->port_rid = rid;
418140928Simp	sc->port_res = res;
419140928Simp	return (0);
42058816Simp}
42158816Simp
42258816Simp/*
42358816Simp * Allocate an irq resource with the given resource id.
42458816Simp */
425179560Sjhbint
426179597Simpcs_alloc_irq(device_t dev, int rid)
42758816Simp{
428140927Simp	struct cs_softc *sc = device_get_softc(dev);
429140927Simp	struct resource *res;
43058816Simp
431179597Simp	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
432140928Simp	if (res == NULL)
433140927Simp		return (ENOENT);
434140928Simp	sc->irq_rid = rid;
435140928Simp	sc->irq_res = res;
436140928Simp	return (0);
43758816Simp}
43858816Simp
43958816Simp/*
44058816Simp * Release all resources
44158816Simp */
442179560Sjhbvoid
443179560Sjhbcs_release_resources(device_t dev)
44458816Simp{
445140927Simp	struct cs_softc *sc = device_get_softc(dev);
44658816Simp
447140927Simp	if (sc->port_res) {
448140927Simp		bus_release_resource(dev, SYS_RES_IOPORT,
449140927Simp		    sc->port_rid, sc->port_res);
450140927Simp		sc->port_res = 0;
451140927Simp	}
452140927Simp	if (sc->irq_res) {
453140927Simp		bus_release_resource(dev, SYS_RES_IRQ,
454140927Simp		    sc->irq_rid, sc->irq_res);
455140927Simp		sc->irq_res = 0;
456140927Simp	}
45758816Simp}
45858816Simp
45958816Simp/*
46037785Smsmith * Install the interface into kernel networking data structures
46137785Smsmith */
46271316Simpint
463121816Sbrookscs_attach(device_t dev)
46437785Smsmith{
465179560Sjhb	int error, media=0;
466121816Sbrooks	struct cs_softc *sc = device_get_softc(dev);;
467147256Sbrooks	struct ifnet *ifp;
46837785Smsmith
469162321Sglebius	sc->dev = dev;
470162321Sglebius
471147256Sbrooks	ifp = sc->ifp = if_alloc(IFT_ETHER);
472147256Sbrooks	if (ifp == NULL) {
473147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
474179560Sjhb		cs_release_resources(dev);
475179560Sjhb		return (ENOMEM);
476147256Sbrooks	}
477147256Sbrooks
478179560Sjhb	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
479179560Sjhb	    MTX_DEF);
480179560Sjhb	callout_init_mtx(&sc->timer, &sc->lock, 0);
48158816Simp
482179560Sjhb	CS_LOCK(sc);
483179560Sjhb	cs_stop(sc);
484179560Sjhb	CS_UNLOCK(sc);
485179560Sjhb
486121752Sbrooks	ifp->if_softc=sc;
487121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
488121752Sbrooks	ifp->if_start=cs_start;
489121752Sbrooks	ifp->if_ioctl=cs_ioctl;
490121752Sbrooks	ifp->if_init=cs_init;
491179560Sjhb	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
49237785Smsmith
493179560Sjhb	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
49437785Smsmith
495121752Sbrooks	/*
496121752Sbrooks	 * this code still in progress (DMA support)
497121752Sbrooks	 *
49837785Smsmith
499121752Sbrooks	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
500121752Sbrooks	if (sc->recv_ring == NULL) {
501121816Sbrooks		log(LOG_ERR,
502121816Sbrooks		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
503121752Sbrooks		return(0);
504121752Sbrooks	}
505121752Sbrooks	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
506121752Sbrooks	    < (128*1024-CS_DMA_BUFFER_SIZE))
507121752Sbrooks	    sc->recv_ring+=16*1024;
50837785Smsmith
509121752Sbrooks	*/
51037785Smsmith
511121752Sbrooks	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
512121752Sbrooks	if (sc->buffer == NULL) {
513162321Sglebius		device_printf(sc->dev, "Couldn't allocate memory for NIC\n");
514179560Sjhb		if_free(ifp);
515179560Sjhb		mtx_destroy(&sc->lock);
516179560Sjhb		cs_release_resources(dev);
517179560Sjhb		return(ENOMEM);
518121752Sbrooks	}
51937785Smsmith
520121752Sbrooks	/*
521121752Sbrooks	 * Initialize the media structures.
522121752Sbrooks	 */
523121752Sbrooks	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
52437785Smsmith
525121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_T) {
526121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
527121752Sbrooks		if (sc->chip_type != CS8900) {
528121752Sbrooks			ifmedia_add(&sc->media,
529121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
530121752Sbrooks			ifmedia_add(&sc->media,
531121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
532121752Sbrooks		}
533121752Sbrooks	}
53437785Smsmith
535121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_2)
536121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
53737785Smsmith
538121752Sbrooks	if (sc->adapter_cnf & A_CNF_AUI)
539121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
54037785Smsmith
541121752Sbrooks	if (sc->adapter_cnf & A_CNF_MEDIA)
542121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
54337785Smsmith
544121752Sbrooks	/* Set default media from EEPROM */
545121752Sbrooks	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
546121752Sbrooks	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
547121752Sbrooks	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
548121752Sbrooks	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
549121752Sbrooks	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
550122024Simp	default:
551162321Sglebius		device_printf(sc->dev, "no media, assuming 10baseT\n");
552122024Simp		sc->adapter_cnf |= A_CNF_10B_T;
553122024Simp		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
554122024Simp		if (sc->chip_type != CS8900) {
555122024Simp			ifmedia_add(&sc->media,
556122024Simp			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
557122024Simp			ifmedia_add(&sc->media,
558122024Simp			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
559122024Simp		}
560122024Simp		media = IFM_ETHER | IFM_10_T;
561122024Simp		break;
56237785Smsmith	}
563121752Sbrooks	ifmedia_set(&sc->media, media);
564121752Sbrooks	cs_mediaset(sc, media);
56537785Smsmith
566147256Sbrooks	ether_ifattach(ifp, sc->enaddr);
567121752Sbrooks
568179560Sjhb  	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
569179560Sjhb	    NULL, csintr, sc, &sc->irq_handle);
570179560Sjhb	if (error) {
571179560Sjhb		ether_ifdetach(ifp);
572179560Sjhb		free(sc->buffer, M_DEVBUF);
573179560Sjhb		if_free(ifp);
574179560Sjhb		mtx_destroy(&sc->lock);
575179560Sjhb		cs_release_resources(dev);
576179560Sjhb		return (error);
577179560Sjhb	}
578179560Sjhb
57958816Simp	return (0);
58037785Smsmith}
58137785Smsmith
582140888Simpint
583140888Simpcs_detach(device_t dev)
584140888Simp{
585140888Simp	struct cs_softc *sc;
586140888Simp	struct ifnet *ifp;
587140888Simp
588140888Simp	sc = device_get_softc(dev);
589147256Sbrooks	ifp = sc->ifp;
590140888Simp
591179560Sjhb	CS_LOCK(sc);
592140888Simp	cs_stop(sc);
593179560Sjhb	CS_UNLOCK(sc);
594179560Sjhb	callout_drain(&sc->timer);
595140888Simp	ether_ifdetach(ifp);
596179560Sjhb	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
597150306Simp	cs_release_resources(dev);
598179560Sjhb	free(sc->buffer, M_DEVBUF);
599147256Sbrooks	if_free(ifp);
600179560Sjhb	mtx_destroy(&sc->lock);
601140888Simp	return (0);
602140888Simp}
603140888Simp
60437785Smsmith/*
60537785Smsmith * Initialize the board
60637785Smsmith */
60737785Smsmithstatic void
60837785Smsmithcs_init(void *xsc)
60937785Smsmith{
61037785Smsmith	struct cs_softc *sc=(struct cs_softc *)xsc;
611179560Sjhb
612179560Sjhb	CS_LOCK(sc);
613179560Sjhb	cs_init_locked(sc);
614179560Sjhb	CS_UNLOCK(sc);
615179560Sjhb}
616179560Sjhb
617179560Sjhbstatic void
618179560Sjhbcs_init_locked(struct cs_softc *sc)
619179560Sjhb{
620147256Sbrooks	struct ifnet *ifp = sc->ifp;
621179560Sjhb	int i, rx_cfg;
62237785Smsmith
62337785Smsmith	/*
624179560Sjhb	 * reset watchdog timer
62537785Smsmith	 */
626179560Sjhb	sc->tx_timeout = 0;
62737785Smsmith	sc->buf_len = 0;
62837785Smsmith
62937785Smsmith	/*
63037785Smsmith	 * Hardware initialization of cs
63137785Smsmith	 */
63237785Smsmith
63337785Smsmith	/* Enable receiver and transmitter */
63472940Simp	cs_writereg(sc, PP_LineCTL,
63572940Simp		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
63637785Smsmith
63737785Smsmith	/* Configure the receiver mode */
63837785Smsmith	cs_setmode(sc);
63937785Smsmith
64037785Smsmith	/*
64137785Smsmith	 * This defines what type of frames will cause interrupts
64237785Smsmith	 * Bad frames should generate interrupts so that the driver
64337785Smsmith	 * could track statistics of discarded packets
64437785Smsmith	 */
645140927Simp	rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
64637785Smsmith		 RX_EXTRA_DATA_ENBL;
64737785Smsmith	if (sc->isa_config & STREAM_TRANSFER)
64837785Smsmith		rx_cfg |= RX_STREAM_ENBL;
64972940Simp	cs_writereg(sc, PP_RxCFG, rx_cfg);
65072940Simp	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
65137785Smsmith		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
65237785Smsmith		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
65372940Simp	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
65437785Smsmith		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
65537785Smsmith		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
65637785Smsmith
657140927Simp	/* Write MAC address into IA filter */
658140927Simp	for (i=0; i<ETHER_ADDR_LEN/2; i++)
659140927Simp		cs_writereg(sc, PP_IA + i * 2,
660147256Sbrooks		    sc->enaddr[i * 2] |
661147256Sbrooks		    (sc->enaddr[i * 2 + 1] << 8) );
66237785Smsmith
66337785Smsmith	/*
66437785Smsmith	 * Now enable everything
66537785Smsmith	 */
66637785Smsmith/*
66737785Smsmith#ifdef	CS_USE_64K_DMA
66872940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
66972940Simp#else
670140927Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
67137785Smsmith#endif
67237785Smsmith*/
67372940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
67437785Smsmith
67537785Smsmith	/*
67637785Smsmith	 * Set running and clear output active flags
67737785Smsmith	 */
678148887Srwatson	sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
679148887Srwatson	sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
680179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
68137785Smsmith
68237785Smsmith	/*
68337785Smsmith	 * Start sending process
68437785Smsmith	 */
685179560Sjhb	cs_start_locked(ifp);
68637785Smsmith}
68737785Smsmith
68837785Smsmith/*
689106937Ssam * Get the packet from the board and send it to the upper layer.
69037785Smsmith */
69137785Smsmithstatic int
69237785Smsmithcs_get_packet(struct cs_softc *sc)
69337785Smsmith{
694147256Sbrooks	struct ifnet *ifp = sc->ifp;
69537785Smsmith	int iobase = sc->nic_addr, status, length;
69637785Smsmith	struct ether_header *eh;
69737785Smsmith	struct mbuf *m;
69837785Smsmith
69937785Smsmith#ifdef CS_DEBUG
70037785Smsmith	int i;
70137785Smsmith#endif
70237785Smsmith
70372940Simp	status = cs_inw(sc, RX_FRAME_PORT);
70472940Simp	length = cs_inw(sc, RX_FRAME_PORT);
70537785Smsmith
70637785Smsmith#ifdef CS_DEBUG
707162321Sglebius	device_printf(sc->dev, "rcvd: stat %x, len %d\n",
708104252Sbrooks		status, length);
70937785Smsmith#endif
71037785Smsmith
71137785Smsmith	if (!(status & RX_OK)) {
71237785Smsmith#ifdef CS_DEBUG
713162321Sglebius		device_printf(sc->dev, "bad pkt stat %x\n", status);
71437785Smsmith#endif
71537785Smsmith		ifp->if_ierrors++;
716140928Simp		return (-1);
71737785Smsmith	}
71837785Smsmith
719111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
72037785Smsmith	if (m==NULL)
721140928Simp		return (-1);
72237785Smsmith
72337785Smsmith	if (length > MHLEN) {
724111119Simp		MCLGET(m, M_DONTWAIT);
72537785Smsmith		if (!(m->m_flags & M_EXT)) {
72637785Smsmith			m_freem(m);
727140928Simp			return (-1);
72837785Smsmith		}
72937785Smsmith	}
73037785Smsmith
73137785Smsmith	/* Initialize packet's header info */
73237785Smsmith	m->m_pkthdr.rcvif = ifp;
73337785Smsmith	m->m_pkthdr.len = length;
73437785Smsmith	m->m_len = length;
73537785Smsmith
73637785Smsmith	/* Get the data */
73737785Smsmith	insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
73837785Smsmith
73937785Smsmith	eh = mtod(m, struct ether_header *);
74037785Smsmith
74137785Smsmith#ifdef CS_DEBUG
74237785Smsmith	for (i=0;i<length;i++)
74337785Smsmith	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
74437785Smsmith	printf( "\n" );
74537785Smsmith#endif
74637785Smsmith
74737785Smsmith	if (status & (RX_IA | RX_BROADCAST) ||
74837785Smsmith	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
74937785Smsmith		/* Feed the packet to the upper layer */
750106937Ssam		(*ifp->if_input)(ifp, m);
75137785Smsmith		ifp->if_ipackets++;
752122024Simp		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
753140927Simp			DELAY(cs_recv_delay);
75437785Smsmith	} else {
75537785Smsmith		m_freem(m);
75637785Smsmith	}
75737785Smsmith
758140928Simp	return (0);
75937785Smsmith}
76037785Smsmith
76137785Smsmith/*
76258816Simp * Handle interrupts
76337785Smsmith */
76458816Simpvoid
76558816Simpcsintr(void *arg)
76637785Smsmith{
76758816Simp	struct cs_softc *sc = (struct cs_softc*) arg;
768147256Sbrooks	struct ifnet *ifp = sc->ifp;
76941591Sarchie	int status;
77037785Smsmith
77137785Smsmith#ifdef CS_DEBUG
772162321Sglebius	device_printf(sc->dev, "Interrupt.\n");
77337785Smsmith#endif
77437785Smsmith
775179560Sjhb	CS_LOCK(sc);
77672940Simp	while ((status=cs_inw(sc, ISQ_PORT))) {
77737785Smsmith
77837785Smsmith#ifdef CS_DEBUG
779162321Sglebius		device_printf(sc->dev, "from ISQ: %04x\n", status);
78037785Smsmith#endif
78137785Smsmith
78237785Smsmith		switch (status & ISQ_EVENT_MASK) {
783140927Simp		case ISQ_RECEIVER_EVENT:
784140927Simp			cs_get_packet(sc);
785140927Simp			break;
78637785Smsmith
787140927Simp		case ISQ_TRANSMITTER_EVENT:
788140927Simp			if (status & TX_OK)
789140927Simp				ifp->if_opackets++;
790140927Simp			else
791140927Simp				ifp->if_oerrors++;
792148887Srwatson			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
793179560Sjhb			sc->tx_timeout = 0;
794140927Simp			break;
79537785Smsmith
796140927Simp		case ISQ_BUFFER_EVENT:
797140927Simp			if (status & READY_FOR_TX) {
798148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
799179560Sjhb				sc->tx_timeout = 0;
800140927Simp			}
80137785Smsmith
802140927Simp			if (status & TX_UNDERRUN) {
803148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
804179560Sjhb				sc->tx_timeout = 0;
805140927Simp				ifp->if_oerrors++;
806140927Simp			}
807140927Simp			break;
80837785Smsmith
809140927Simp		case ISQ_RX_MISS_EVENT:
810140927Simp			ifp->if_ierrors+=(status>>6);
811140927Simp			break;
81237785Smsmith
813140927Simp		case ISQ_TX_COL_EVENT:
814140927Simp			ifp->if_collisions+=(status>>6);
815140927Simp			break;
816140927Simp		}
817140927Simp	}
81837785Smsmith
819148887Srwatson	if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
820179560Sjhb		cs_start_locked(ifp);
821140927Simp	}
822179560Sjhb	CS_UNLOCK(sc);
82337785Smsmith}
82437785Smsmith
82537785Smsmith/*
82637785Smsmith * Save the data in buffer
82737785Smsmith */
82837785Smsmith
82937785Smsmithstatic void
83037785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
83137785Smsmith{
83237785Smsmith	int len;
83337785Smsmith	struct mbuf *mp;
83437785Smsmith	unsigned char *data, *buf;
83537785Smsmith
83637785Smsmith	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
83737785Smsmith		len = mp->m_len;
83837785Smsmith
83937785Smsmith		/*
84037785Smsmith		 * Ignore empty parts
84137785Smsmith		 */
84237785Smsmith		if (!len)
84337785Smsmith		continue;
84437785Smsmith
84537785Smsmith		/*
84637785Smsmith		 * Find actual data address
84737785Smsmith		 */
84837785Smsmith		data = mtod(mp, caddr_t);
84937785Smsmith
85037785Smsmith		bcopy((caddr_t) data, (caddr_t) buf, len);
85137785Smsmith		buf += len;
85237785Smsmith		sc->buf_len += len;
85337785Smsmith	}
85437785Smsmith}
85537785Smsmith
85637785Smsmith
85737785Smsmithstatic void
85837785Smsmithcs_xmit_buf( struct cs_softc *sc )
85937785Smsmith{
86037785Smsmith	outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
86137785Smsmith	sc->buf_len = 0;
86237785Smsmith}
86337785Smsmith
86437785Smsmithstatic void
86537785Smsmithcs_start(struct ifnet *ifp)
86637785Smsmith{
867179560Sjhb	struct cs_softc *sc = ifp->if_softc;
868179560Sjhb
869179560Sjhb	CS_LOCK(sc);
870179560Sjhb	cs_start_locked(ifp);
871179560Sjhb	CS_UNLOCK(sc);
872179560Sjhb}
873179560Sjhb
874179560Sjhbstatic void
875179560Sjhbcs_start_locked(struct ifnet *ifp)
876179560Sjhb{
877179560Sjhb	int length;
87837785Smsmith	struct mbuf *m, *mp;
87937785Smsmith	struct cs_softc *sc = ifp->if_softc;
88037785Smsmith
88137785Smsmith	for (;;) {
88237785Smsmith		if (sc->buf_len)
88337785Smsmith			length = sc->buf_len;
88437785Smsmith		else {
88537785Smsmith			IF_DEQUEUE( &ifp->if_snd, m );
88637785Smsmith
88737785Smsmith			if (m==NULL) {
88837785Smsmith				return;
88937785Smsmith			}
89037785Smsmith
89137785Smsmith			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
89237785Smsmith				length += mp->m_len;
89337785Smsmith
89437785Smsmith			/* Skip zero-length packets */
89537785Smsmith			if (length == 0) {
89637785Smsmith				m_freem(m);
89737785Smsmith				continue;
89837785Smsmith			}
89937785Smsmith
90037785Smsmith			cs_write_mbufs(sc, m);
90137785Smsmith
902106937Ssam			BPF_MTAP(ifp, m);
90337785Smsmith
90437785Smsmith			m_freem(m);
90537785Smsmith		}
90637785Smsmith
90737785Smsmith		/*
90837785Smsmith		 * Issue a SEND command
90937785Smsmith		 */
91072940Simp		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
91172940Simp		cs_outw(sc, TX_LEN_PORT, length );
91237785Smsmith
91337785Smsmith		/*
91437785Smsmith		 * If there's no free space in the buffer then leave
91537785Smsmith		 * this packet for the next time: indicate output active
91637785Smsmith		 * and return.
91737785Smsmith		 */
91872940Simp		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
919179560Sjhb			sc->tx_timeout = sc->buf_len;
920148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
92137785Smsmith			return;
92237785Smsmith		}
92337785Smsmith
924140927Simp		cs_xmit_buf(sc);
92537785Smsmith
92637785Smsmith		/*
92737785Smsmith		 * Set the watchdog timer in case we never hear
92837785Smsmith		 * from board again. (I don't know about correct
92937785Smsmith		 * value for this timeout)
93037785Smsmith		 */
931179560Sjhb		sc->tx_timeout = length;
93237785Smsmith
933148887Srwatson		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
93437785Smsmith		return;
93537785Smsmith	}
93637785Smsmith}
93737785Smsmith
93837785Smsmith/*
93937785Smsmith * Stop everything on the interface
94037785Smsmith */
94137785Smsmithstatic void
94237785Smsmithcs_stop(struct cs_softc *sc)
94337785Smsmith{
94437785Smsmith
945179560Sjhb	CS_ASSERT_LOCKED(sc);
94672940Simp	cs_writereg(sc, PP_RxCFG, 0);
94772940Simp	cs_writereg(sc, PP_TxCFG, 0);
94872940Simp	cs_writereg(sc, PP_BufCFG, 0);
94972940Simp	cs_writereg(sc, PP_BusCTL, 0);
95037785Smsmith
951148887Srwatson	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
952179560Sjhb	sc->tx_timeout = 0;
953179560Sjhb	callout_stop(&sc->timer);
95437785Smsmith}
95537785Smsmith
95637785Smsmith/*
95737785Smsmith * Reset the interface
95837785Smsmith */
95937785Smsmithstatic void
96037785Smsmithcs_reset(struct cs_softc *sc)
96137785Smsmith{
962179560Sjhb
963179560Sjhb	CS_ASSERT_LOCKED(sc);
96437785Smsmith	cs_stop(sc);
965179560Sjhb	cs_init_locked(sc);
96637785Smsmith}
96737785Smsmith
968156559Sphilipstatic uint16_t
969156559Sphilipcs_hash_index(struct sockaddr_dl *addr)
970156559Sphilip{
971156559Sphilip	uint32_t crc;
972156559Sphilip	uint16_t idx;
973156559Sphilip	caddr_t lla;
974156559Sphilip
975156559Sphilip	lla = LLADDR(addr);
976156559Sphilip	crc = ether_crc32_le(lla, ETHER_ADDR_LEN);
977156559Sphilip	idx = crc >> 26;
978156559Sphilip
979156559Sphilip	return (idx);
980156559Sphilip}
981156559Sphilip
98237785Smsmithstatic void
98337785Smsmithcs_setmode(struct cs_softc *sc)
98437785Smsmith{
985156559Sphilip	int rx_ctl;
986156559Sphilip	uint16_t af[4];
987156559Sphilip	uint16_t port, mask, index;
988147256Sbrooks	struct ifnet *ifp = sc->ifp;
989156559Sphilip	struct ifmultiaddr *ifma;
99037785Smsmith
99137785Smsmith	/* Stop the receiver while changing filters */
99272940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
99337785Smsmith
99437785Smsmith	if (ifp->if_flags & IFF_PROMISC) {
99537785Smsmith		/* Turn on promiscuous mode. */
99637785Smsmith		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
997156559Sphilip	} else if (ifp->if_flags & IFF_MULTICAST) {
998156559Sphilip		/* Allow receiving frames with multicast addresses */
999156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1000156559Sphilip			 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1001156559Sphilip
1002156559Sphilip		/* Start with an empty filter */
1003156559Sphilip		af[0] = af[1] = af[2] = af[3] = 0x0000;
1004156559Sphilip
1005156559Sphilip		if (ifp->if_flags & IFF_ALLMULTI) {
1006156559Sphilip			/* Accept all multicast frames */
1007156559Sphilip			af[0] = af[1] = af[2] = af[3] = 0xffff;
100837785Smsmith		} else {
1009156559Sphilip			/*
1010156559Sphilip			 * Set up the filter to only accept multicast
1011156559Sphilip			 * frames we're interested in.
101237785Smsmith			 */
1013156559Sphilip			IF_ADDR_LOCK(ifp);
1014156559Sphilip			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1015156559Sphilip				struct sockaddr_dl *dl =
1016156559Sphilip				    (struct sockaddr_dl *)ifma->ifma_addr;
1017156559Sphilip
1018156559Sphilip				index = cs_hash_index(dl);
1019156559Sphilip				port = (u_int16_t) (index >> 4);
1020156559Sphilip				mask = (u_int16_t) (1 << (index & 0xf));
1021156559Sphilip				af[port] |= mask;
1022156559Sphilip			}
1023156559Sphilip			IF_ADDR_UNLOCK(ifp);
102437785Smsmith		}
1025156559Sphilip
1026156559Sphilip		cs_writereg(sc, PP_LAF + 0, af[0]);
1027156559Sphilip		cs_writereg(sc, PP_LAF + 2, af[1]);
1028156559Sphilip		cs_writereg(sc, PP_LAF + 4, af[2]);
1029156559Sphilip		cs_writereg(sc, PP_LAF + 6, af[3]);
1030156559Sphilip	} else {
1031156559Sphilip		/*
1032156559Sphilip		 * Receive only good frames addressed for us and
1033156559Sphilip		 * good broadcasts.
1034156559Sphilip		 */
1035156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1036156559Sphilip			 RX_OK_ACCEPT;
103737785Smsmith	}
103837785Smsmith
103937785Smsmith	/* Set up the filter */
104072940Simp	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
104137785Smsmith
104237785Smsmith	/* Turn on receiver */
104372940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
104437785Smsmith}
104537785Smsmith
104637785Smsmithstatic int
104737785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
104837785Smsmith{
104937785Smsmith	struct cs_softc *sc=ifp->if_softc;
105037785Smsmith	struct ifreq *ifr = (struct ifreq *)data;
1051179560Sjhb	int error=0;
105237785Smsmith
105337785Smsmith#ifdef CS_DEBUG
1054162321Sglebius	if_printf(ifp, "%s command=%lx\n", __func__, command);
105537785Smsmith#endif
105637785Smsmith
105737785Smsmith	switch (command) {
105837785Smsmith	case SIOCSIFFLAGS:
105937785Smsmith		/*
106037785Smsmith		 * Switch interface state between "running" and
106137785Smsmith		 * "stopped", reflecting the UP flag.
1062140927Simp		 */
1063179560Sjhb		CS_LOCK(sc);
1064147256Sbrooks		if (sc->ifp->if_flags & IFF_UP) {
1065148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) {
1066179560Sjhb				cs_init_locked(sc);
1067140927Simp			}
1068140927Simp		} else {
1069148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) {
1070140927Simp				cs_stop(sc);
1071140927Simp			}
1072179560Sjhb		}
107337785Smsmith		/*
107437785Smsmith		 * Promiscuous and/or multicast flags may have changed,
107537785Smsmith		 * so reprogram the multicast filter and/or receive mode.
107637785Smsmith		 *
107737785Smsmith		 * See note about multicasts in cs_setmode
107837785Smsmith		 */
107937785Smsmith		cs_setmode(sc);
1080179560Sjhb		CS_UNLOCK(sc);
108137785Smsmith		break;
108237785Smsmith
108337785Smsmith	case SIOCADDMULTI:
108437785Smsmith	case SIOCDELMULTI:
108537785Smsmith	    /*
108637785Smsmith	     * Multicast list has changed; set the hardware filter
108737785Smsmith	     * accordingly.
108837785Smsmith	     *
108937785Smsmith	     * See note about multicasts in cs_setmode
109037785Smsmith	     */
1091179560Sjhb	    CS_LOCK(sc);
109237785Smsmith	    cs_setmode(sc);
1093179560Sjhb	    CS_UNLOCK(sc);
109437785Smsmith	    error = 0;
109537785Smsmith	    break;
109637785Smsmith
1097140927Simp	case SIOCSIFMEDIA:
1098140927Simp	case SIOCGIFMEDIA:
1099140927Simp		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1100140927Simp		break;
110137785Smsmith
1102140927Simp	default:
1103140921Simp		error = ether_ioctl(ifp, command, data);
1104106937Ssam		break;
1105140927Simp	}
110637785Smsmith
1107140928Simp	return (error);
110837785Smsmith}
110937785Smsmith
111037785Smsmith/*
111137785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to
111237785Smsmith * generate an interrupt after a transmit has been started on it.
111337785Smsmith */
111437785Smsmithstatic void
1115179560Sjhbcs_watchdog(void *arg)
111637785Smsmith{
1117179560Sjhb	struct cs_softc *sc = arg;
1118179560Sjhb	struct ifnet *ifp = sc->ifp;
111937785Smsmith
1120179560Sjhb	CS_ASSERT_LOCKED(sc);
1121179560Sjhb	if (sc->tx_timeout && --sc->tx_timeout == 0) {
1122179560Sjhb		ifp->if_oerrors++;
1123179560Sjhb		log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
112437785Smsmith
1125179560Sjhb		/* Reset the interface */
1126179560Sjhb		if (ifp->if_flags & IFF_UP)
1127179560Sjhb			cs_reset(sc);
1128179560Sjhb		else
1129179560Sjhb			cs_stop(sc);
1130179560Sjhb	}
1131179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
113237785Smsmith}
113337785Smsmith
113437785Smsmithstatic int
113537785Smsmithcs_mediachange(struct ifnet *ifp)
113637785Smsmith{
113737785Smsmith	struct cs_softc *sc = ifp->if_softc;
113837785Smsmith	struct ifmedia *ifm = &sc->media;
1139179560Sjhb	int error;
114037785Smsmith
114137785Smsmith	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1142140928Simp		return (EINVAL);
114337785Smsmith
1144179560Sjhb	CS_LOCK(sc);
1145179560Sjhb	error = cs_mediaset(sc, ifm->ifm_media);
1146179560Sjhb	CS_UNLOCK(sc);
1147179560Sjhb	return (error);
114837785Smsmith}
114937785Smsmith
115037785Smsmithstatic void
115137785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
115237785Smsmith{
115337785Smsmith	int line_status;
115437785Smsmith	struct cs_softc *sc = ifp->if_softc;
115537785Smsmith
1156179560Sjhb	CS_LOCK(sc);
115737785Smsmith	ifmr->ifm_active = IFM_ETHER;
115872940Simp	line_status = cs_readreg(sc, PP_LineST);
115937785Smsmith	if (line_status & TENBASET_ON) {
116037785Smsmith		ifmr->ifm_active |= IFM_10_T;
116137785Smsmith		if (sc->chip_type != CS8900) {
116272940Simp			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
116337785Smsmith				ifmr->ifm_active |= IFM_FDX;
116472940Simp			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
116537785Smsmith				ifmr->ifm_active |= IFM_HDX;
116637785Smsmith		}
116737785Smsmith		ifmr->ifm_status = IFM_AVALID;
116837785Smsmith		if (line_status & LINK_OK)
116937785Smsmith			ifmr->ifm_status |= IFM_ACTIVE;
117037785Smsmith	} else {
117138305Smsmith		if (line_status & AUI_ON) {
117272940Simp			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
117372940Simp			    HCB1_ENBL);
117438305Smsmith			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
117572940Simp			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
117638305Smsmith				ifmr->ifm_active |= IFM_10_2;
117738305Smsmith			else
117838305Smsmith				ifmr->ifm_active |= IFM_10_5;
117938305Smsmith		}
118037785Smsmith	}
1181179560Sjhb	CS_UNLOCK(sc);
118237785Smsmith}
118337785Smsmith
118437785Smsmithstatic int
118537785Smsmithcs_mediaset(struct cs_softc *sc, int media)
118637785Smsmith{
1187179532Simp	int error = 0;
118837785Smsmith
118937785Smsmith	/* Stop the receiver & transmitter */
119072940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
119172940Simp	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
119237785Smsmith
119337785Smsmith#ifdef CS_DEBUG
1194162321Sglebius	device_printf(sc->dev, "%s media=%x\n", __func__, media);
119537785Smsmith#endif
119637785Smsmith
119737785Smsmith	switch (IFM_SUBTYPE(media)) {
119837785Smsmith	default:
119937785Smsmith	case IFM_AUTO:
1200179553Simp		/*
1201179553Simp		 * This chip makes it a little hard to support this, so treat
1202179553Simp		 * it as IFM_10_T, auto duplex.
1203179553Simp		 */
1204179553Simp		enable_tp(sc);
1205179553Simp		cs_duplex_auto(sc);
120637785Smsmith		break;
120737785Smsmith	case IFM_10_T:
1208179532Simp		enable_tp(sc);
120937785Smsmith		if (media & IFM_FDX)
121037785Smsmith			cs_duplex_full(sc);
121137785Smsmith		else if (media & IFM_HDX)
121237785Smsmith			cs_duplex_half(sc);
121337785Smsmith		else
121437785Smsmith			error = cs_duplex_auto(sc);
121537785Smsmith		break;
121637785Smsmith	case IFM_10_2:
121737785Smsmith		error = enable_bnc(sc);
121837785Smsmith		break;
121937785Smsmith	case IFM_10_5:
122037785Smsmith		error = enable_aui(sc);
122137785Smsmith		break;
122237785Smsmith	}
122337785Smsmith
122437785Smsmith	/*
122537785Smsmith	 * Turn the transmitter & receiver back on
122637785Smsmith	 */
122772940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
122872940Simp	    SERIAL_RX_ON | SERIAL_TX_ON);
122937785Smsmith
1230140928Simp	return (error);
123137785Smsmith}
1232