if_cs.c revision 243857
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 243857 2012-12-04 09:32:43Z glebius $");
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 */
106227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
107122024Simp
108122024Simpint	cs_ignore_cksum_failure = 0;
109122024SimpTUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure);
110122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW,
111122024Simp    &cs_ignore_cksum_failure, 0,
112122024Simp  "ignore checksum errors in cs card EEPROM");
113122024Simp
114122024Simpstatic int	cs_recv_delay = 570;
115140925SimpTUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay);
116122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, "");
117122024Simp
118179532Simpstatic int cs8900_eeint2irq[16] = {
119179532Simp	 10,  11,  12,   5, 255, 255, 255, 255,
120179532Simp	255, 255, 255, 255, 255, 255, 255, 255
121179532Simp};
122179532Simp
123179532Simpstatic int cs8900_irq2eeint[16] = {
124179532Simp	255, 255, 255, 255, 255,   3, 255, 255,
125179532Simp	255,   0,   1,   2, 255, 255, 255, 255
126179532Simp};
127179532Simp
12837785Smsmithstatic int
129122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
13037785Smsmith{
13137785Smsmith	int i;
13237785Smsmith
13337785Smsmith#ifdef CS_DEBUG
134179620Simp	device_printf(sc->dev, "EEPROM data from %x for %x:\n", off, len);
13537785Smsmith#endif
136122024Simp	for (i=0; i < len; i++) {
137122024Simp		if (wait_eeprom_ready(sc) < 0)
138122024Simp			return (-1);
13937785Smsmith		/* Send command to EEPROM to read */
14072940Simp		cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
141122024Simp		if (wait_eeprom_ready(sc) < 0)
14272940Simp			return (-1);
14372940Simp		buffer[i] = cs_readreg(sc, PP_EEData);
14437785Smsmith
14537785Smsmith#ifdef CS_DEBUG
146179591Simp		printf("%04x ",buffer[i]);
14737785Smsmith#endif
14837785Smsmith	}
14937785Smsmith
15037785Smsmith#ifdef CS_DEBUG
15137785Smsmith	printf("\n");
15237785Smsmith#endif
15372940Simp	return (0);
15437785Smsmith}
15537785Smsmith
15637785Smsmithstatic int
157122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer)
15837785Smsmith{
159122024Simp	int i;
160122024Simp	uint16_t cksum=0;
16137785Smsmith
162122024Simp	for (i = 0; i < len; i++)
163122024Simp		cksum += buffer[i];
16437785Smsmith	cksum &= 0xffff;
165179591Simp	if (cksum == 0 || cs_ignore_cksum_failure)
166122024Simp		return (0);
167122024Simp	return (-1);
16837785Smsmith}
16937785Smsmith
17037785Smsmithstatic int
17137785Smsmithwait_eeprom_ready(struct cs_softc *sc)
17237785Smsmith{
173179620Simp	int i;
174179620Simp
175179620Simp	/*
176179620Simp	 * From the CS8900A datasheet, section 3.5.2:
177179620Simp	 * "Before issuing any command to the EEPROM, the host must wait
178179620Simp	 * for the SIBUSY bit (Register 16, SelfST, bit 8) to clear.  After
179179620Simp	 * each command has been issued, the host must wait again for SIBUSY
180179620Simp	 * to clear."
181179620Simp	 *
182179620Simp	 * Before we issue the command, we should be !busy, so that will
183179620Simp	 * be fast.  The datasheet suggests that clock out from the part
184179620Simp	 * per word will be on the order of 25us, which is consistant with
185179620Simp	 * the 1MHz serial clock and 16bits...  We should never hit 100,
186179621Simp	 * let alone 15,000 here.  The original code did an unconditional
187179621Simp	 * 30ms DELAY here.  Bad Kharma.  cs_readreg takes ~2us.
188179620Simp	 */
189179621Simp	for (i = 0; i < 15000; i++)	/* 30ms max */
190179620Simp		if (!(cs_readreg(sc, PP_SelfST) & SI_BUSY))
191179620Simp			return (0);
192179620Simp	return (1);
19337785Smsmith}
19437785Smsmith
19537785Smsmithstatic void
19637785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off)
19737785Smsmith{
19837785Smsmith	unsigned int self_control = HCB1_ENBL;
19937785Smsmith
20037785Smsmith	if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
20137785Smsmith		self_control |= HCB1;
20237785Smsmith	else
20337785Smsmith		self_control &= ~HCB1;
20472940Simp	cs_writereg(sc, PP_SelfCTL, self_control);
205180388Simp	DELAY(500000);	/* Bad! */
20637785Smsmith}
20737785Smsmith
20837785Smsmith
20937785Smsmithstatic int
21037785Smsmithcs_duplex_auto(struct cs_softc *sc)
21137785Smsmith{
212140927Simp	int i, error=0;
213140927Simp
21472940Simp	cs_writereg(sc, PP_AutoNegCTL,
21572940Simp	    RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
216140927Simp	for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
217179597Simp		if (i > 4000) {
218162321Sglebius			device_printf(sc->dev,
219140927Simp			    "full/half duplex auto negotiation timeout\n");
22037785Smsmith			error = ETIMEDOUT;
221140927Simp			break;
222140927Simp		}
223140927Simp		DELAY(1000);
224140927Simp	}
225140928Simp	return (error);
22637785Smsmith}
22737785Smsmith
22837785Smsmithstatic int
22937785Smsmithenable_tp(struct cs_softc *sc)
23037785Smsmith{
23137785Smsmith
23272940Simp	cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
23337785Smsmith	control_dc_dc(sc, 0);
234140928Simp	return (0);
23537785Smsmith}
23637785Smsmith
23737785Smsmithstatic int
23837785Smsmithenable_aui(struct cs_softc *sc)
23937785Smsmith{
24037785Smsmith
24172940Simp	cs_writereg(sc, PP_LineCTL,
24272940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
243179597Simp	control_dc_dc(sc, 0);
244140928Simp	return (0);
24537785Smsmith}
24637785Smsmith
24737785Smsmithstatic int
24837785Smsmithenable_bnc(struct cs_softc *sc)
24937785Smsmith{
25037785Smsmith
25172940Simp	cs_writereg(sc, PP_LineCTL,
25272940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
253179597Simp	control_dc_dc(sc, 1);
254140928Simp	return (0);
25537785Smsmith}
25637785Smsmith
25771316Simpint
25858816Simpcs_cs89x0_probe(device_t dev)
25937785Smsmith{
26058816Simp	int i;
26158816Simp	int error;
26258816Simp	u_long irq, junk;
26358816Simp	struct cs_softc *sc = device_get_softc(dev);
26437785Smsmith	unsigned rev_type = 0;
265122024Simp	uint16_t id;
26658816Simp	char chip_revision;
267122024Simp	uint16_t eeprom_buff[CHKSUM_LEN];
26837785Smsmith	int chip_type, pp_isaint, pp_isadma;
26937785Smsmith
270179620Simp	sc->dev = dev;
27158816Simp	error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
27258816Simp	if (error)
27358816Simp		return (error);
27458816Simp
27572940Simp	if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
27637785Smsmith		/* Chip not detected. Let's try to reset it */
27737785Smsmith		if (bootverbose)
27858816Simp			device_printf(dev, "trying to reset the chip.\n");
27972940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
28072940Simp		i = cs_inw(sc, DATA_PORT);
28172940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
28272940Simp		cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
28372940Simp		if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
28458816Simp			return (ENXIO);
28537785Smsmith	}
28637785Smsmith
28772940Simp	for (i = 0; i < 10000; i++) {
28872940Simp		id = cs_readreg(sc, PP_ChipID);
28972940Simp		if (id == CHIP_EISA_ID_SIG)
29072940Simp			break;
29172940Simp	}
29272940Simp	if (i == 10000)
29358816Simp		return (ENXIO);
29437785Smsmith
29572940Simp	rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
29637785Smsmith	chip_type = rev_type & ~REVISON_BITS;
29737785Smsmith	chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
29837785Smsmith
29937785Smsmith	sc->chip_type = chip_type;
30058816Simp
301179532Simp	if (chip_type == CS8900) {
30237785Smsmith		pp_isaint = PP_CS8900_ISAINT;
30337785Smsmith		pp_isadma = PP_CS8900_ISADMA;
30437785Smsmith		sc->send_cmd = TX_CS8900_AFTER_ALL;
30537785Smsmith	} else {
30637785Smsmith		pp_isaint = PP_CS8920_ISAINT;
30737785Smsmith		pp_isadma = PP_CS8920_ISADMA;
30837785Smsmith		sc->send_cmd = TX_CS8920_AFTER_ALL;
30937785Smsmith	}
31037785Smsmith
31137785Smsmith	/*
312140927Simp	 * Clear some fields so that fail of EEPROM will left them clean
313140927Simp	 */
314140927Simp	sc->auto_neg_cnf = 0;
315140927Simp	sc->adapter_cnf  = 0;
316140927Simp	sc->isa_config   = 0;
317140927Simp
318140927Simp	/*
319179532Simp	 * If no interrupt specified, use what the board tells us.
32037785Smsmith	 */
32158816Simp	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
32258816Simp
32358816Simp	/*
32458816Simp	 * Get data from EEPROM
32558816Simp	 */
32672940Simp	if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
32758816Simp		device_printf(dev, "No EEPROM, assuming defaults.\n");
328179532Simp	} else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
329179532Simp		device_printf(dev, "EEPROM read failed, assuming defaults.\n");
330179532Simp	} else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
331179532Simp		device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n");
33237785Smsmith	} else {
333180388Simp		sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET];
334180388Simp		sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET];
335180388Simp		sc->isa_config = eeprom_buff[ISA_CNF_OFFSET];
336179532Simp		for (i=0; i<ETHER_ADDR_LEN/2; i++) {
337179532Simp			sc->enaddr[i*2] = eeprom_buff[i];
338179532Simp			sc->enaddr[i*2+1] = eeprom_buff[i] >> 8;
339179532Simp		}
340179532Simp		/*
341179532Simp		 * If no interrupt specified, use what the
342179532Simp		 * board tells us.
343179532Simp		 */
344179532Simp		if (error) {
345179532Simp			irq = sc->isa_config & INT_NO_MASK;
346179532Simp			error = 0;
347179532Simp			if (chip_type == CS8900) {
348179532Simp				irq = cs8900_eeint2irq[irq];
34937785Smsmith			} else {
350179532Simp				if (irq > CS8920_NO_INTS)
351179532Simp					irq = 255;
35237785Smsmith			}
353179532Simp			if (irq == 255) {
354179532Simp				device_printf(dev, "invalid irq in EEPROM.\n");
355179532Simp				error = EINVAL;
356179532Simp			}
357179532Simp			if (!error)
358179532Simp				bus_set_resource(dev, SYS_RES_IRQ, 0,
359179532Simp				    irq, 1);
360140927Simp		}
361140927Simp	}
36237785Smsmith
363179532Simp	if (!error && !(sc->flags & CS_NO_IRQ)) {
364140927Simp		if (chip_type == CS8900) {
365217538Sjhb			if (irq < 16)
366179532Simp				irq = cs8900_irq2eeint[irq];
367179532Simp			else
368179532Simp				irq = 255;
369140927Simp		} else {
370179532Simp			if (irq > CS8920_NO_INTS)
371179532Simp				irq = 255;
372140927Simp		}
373179532Simp		if (irq == 255)
374179532Simp			error = EINVAL;
37558816Simp	}
37658816Simp
377179507Simp	if (error) {
37858816Simp	       	device_printf(dev, "Unknown or invalid irq\n");
379179507Simp		return (error);
380140927Simp	}
381140927Simp
382179507Simp	if (!(sc->flags & CS_NO_IRQ))
383179507Simp		cs_writereg(sc, pp_isaint, irq);
384179507Simp
385140927Simp	/*
386140927Simp	 * Temporary disabled
387140927Simp	 *
388140927Simp	if (drq>0)
38972940Simp		cs_writereg(sc, pp_isadma, drq);
39037785Smsmith	else {
391104252Sbrooks		device_printf(dev, "incorrect drq\n",);
392140928Simp		return (0);
39337785Smsmith	}
394140927Simp	*/
39537785Smsmith
39637785Smsmith	if (bootverbose)
39758816Simp		 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
398179532Simp			chip_type == CS8900 ? '0' : '2',
399179532Simp			chip_type == CS8920M ? "M" : "",
40037785Smsmith			chip_revision,
40137785Smsmith			(sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
40237785Smsmith			(sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
40358816Simp			(sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
40437785Smsmith
405140927Simp	if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
406140927Simp	    (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
407140927Simp		sc->line_ctl = LOW_RX_SQUELCH;
408140927Simp	else
409140927Simp		sc->line_ctl = 0;
41037785Smsmith
411140928Simp	return (0);
41237785Smsmith}
41337785Smsmith
41437785Smsmith/*
41558816Simp * Allocate a port resource with the given resource id.
41658816Simp */
417179560Sjhbint
418179560Sjhbcs_alloc_port(device_t dev, int rid, int size)
41958816Simp{
420140927Simp	struct cs_softc *sc = device_get_softc(dev);
421140927Simp	struct resource *res;
42258816Simp
423140927Simp	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
424140927Simp	    0ul, ~0ul, size, RF_ACTIVE);
425140928Simp	if (res == NULL)
426140927Simp		return (ENOENT);
427140928Simp	sc->port_rid = rid;
428140928Simp	sc->port_res = res;
429140928Simp	return (0);
43058816Simp}
43158816Simp
43258816Simp/*
43358816Simp * Allocate an irq resource with the given resource id.
43458816Simp */
435179560Sjhbint
436179597Simpcs_alloc_irq(device_t dev, int rid)
43758816Simp{
438140927Simp	struct cs_softc *sc = device_get_softc(dev);
439140927Simp	struct resource *res;
44058816Simp
441179597Simp	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
442140928Simp	if (res == NULL)
443140927Simp		return (ENOENT);
444140928Simp	sc->irq_rid = rid;
445140928Simp	sc->irq_res = res;
446140928Simp	return (0);
44758816Simp}
44858816Simp
44958816Simp/*
45058816Simp * Release all resources
45158816Simp */
452179560Sjhbvoid
453179560Sjhbcs_release_resources(device_t dev)
45458816Simp{
455140927Simp	struct cs_softc *sc = device_get_softc(dev);
45658816Simp
457140927Simp	if (sc->port_res) {
458140927Simp		bus_release_resource(dev, SYS_RES_IOPORT,
459140927Simp		    sc->port_rid, sc->port_res);
460140927Simp		sc->port_res = 0;
461140927Simp	}
462140927Simp	if (sc->irq_res) {
463140927Simp		bus_release_resource(dev, SYS_RES_IRQ,
464140927Simp		    sc->irq_rid, sc->irq_res);
465140927Simp		sc->irq_res = 0;
466140927Simp	}
46758816Simp}
46858816Simp
46958816Simp/*
47037785Smsmith * Install the interface into kernel networking data structures
47137785Smsmith */
47271316Simpint
473121816Sbrookscs_attach(device_t dev)
47437785Smsmith{
475179560Sjhb	int error, media=0;
476201758Smbr	struct cs_softc *sc = device_get_softc(dev);
477147256Sbrooks	struct ifnet *ifp;
47837785Smsmith
479162321Sglebius	sc->dev = dev;
480162321Sglebius
481147256Sbrooks	ifp = sc->ifp = if_alloc(IFT_ETHER);
482147256Sbrooks	if (ifp == NULL) {
483147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
484179560Sjhb		cs_release_resources(dev);
485179560Sjhb		return (ENOMEM);
486147256Sbrooks	}
487147256Sbrooks
488179560Sjhb	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
489179560Sjhb	    MTX_DEF);
490179560Sjhb	callout_init_mtx(&sc->timer, &sc->lock, 0);
49158816Simp
492179560Sjhb	CS_LOCK(sc);
493179560Sjhb	cs_stop(sc);
494179560Sjhb	CS_UNLOCK(sc);
495179560Sjhb
496121752Sbrooks	ifp->if_softc=sc;
497121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
498121752Sbrooks	ifp->if_start=cs_start;
499121752Sbrooks	ifp->if_ioctl=cs_ioctl;
500121752Sbrooks	ifp->if_init=cs_init;
501207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
50237785Smsmith
503179560Sjhb	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
50437785Smsmith
505121752Sbrooks	/*
506121752Sbrooks	 * this code still in progress (DMA support)
507121752Sbrooks	 *
50837785Smsmith
509121752Sbrooks	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
510121752Sbrooks	if (sc->recv_ring == NULL) {
511121816Sbrooks		log(LOG_ERR,
512121816Sbrooks		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
513121752Sbrooks		return(0);
514121752Sbrooks	}
515121752Sbrooks	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
516121752Sbrooks	    < (128*1024-CS_DMA_BUFFER_SIZE))
517121752Sbrooks	    sc->recv_ring+=16*1024;
51837785Smsmith
519121752Sbrooks	*/
52037785Smsmith
521121752Sbrooks	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
522121752Sbrooks	if (sc->buffer == NULL) {
523162321Sglebius		device_printf(sc->dev, "Couldn't allocate memory for NIC\n");
524179560Sjhb		if_free(ifp);
525179560Sjhb		mtx_destroy(&sc->lock);
526179560Sjhb		cs_release_resources(dev);
527179560Sjhb		return(ENOMEM);
528121752Sbrooks	}
52937785Smsmith
530121752Sbrooks	/*
531121752Sbrooks	 * Initialize the media structures.
532121752Sbrooks	 */
533121752Sbrooks	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
53437785Smsmith
535121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_T) {
536121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
537121752Sbrooks		if (sc->chip_type != CS8900) {
538121752Sbrooks			ifmedia_add(&sc->media,
539121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
540121752Sbrooks			ifmedia_add(&sc->media,
541121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
542121752Sbrooks		}
543121752Sbrooks	}
54437785Smsmith
545121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_2)
546121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
54737785Smsmith
548121752Sbrooks	if (sc->adapter_cnf & A_CNF_AUI)
549121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
55037785Smsmith
551121752Sbrooks	if (sc->adapter_cnf & A_CNF_MEDIA)
552121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
55337785Smsmith
554121752Sbrooks	/* Set default media from EEPROM */
555121752Sbrooks	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
556121752Sbrooks	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
557121752Sbrooks	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
558121752Sbrooks	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
559121752Sbrooks	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
560122024Simp	default:
561162321Sglebius		device_printf(sc->dev, "no media, assuming 10baseT\n");
562122024Simp		sc->adapter_cnf |= A_CNF_10B_T;
563122024Simp		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
564122024Simp		if (sc->chip_type != CS8900) {
565122024Simp			ifmedia_add(&sc->media,
566122024Simp			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
567122024Simp			ifmedia_add(&sc->media,
568122024Simp			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
569122024Simp		}
570122024Simp		media = IFM_ETHER | IFM_10_T;
571122024Simp		break;
57237785Smsmith	}
573121752Sbrooks	ifmedia_set(&sc->media, media);
574121752Sbrooks	cs_mediaset(sc, media);
57537785Smsmith
576147256Sbrooks	ether_ifattach(ifp, sc->enaddr);
577121752Sbrooks
578179560Sjhb  	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
579179560Sjhb	    NULL, csintr, sc, &sc->irq_handle);
580179560Sjhb	if (error) {
581179560Sjhb		ether_ifdetach(ifp);
582179560Sjhb		free(sc->buffer, M_DEVBUF);
583179560Sjhb		if_free(ifp);
584179560Sjhb		mtx_destroy(&sc->lock);
585179560Sjhb		cs_release_resources(dev);
586179560Sjhb		return (error);
587179560Sjhb	}
588179560Sjhb
58958816Simp	return (0);
59037785Smsmith}
59137785Smsmith
592140888Simpint
593140888Simpcs_detach(device_t dev)
594140888Simp{
595140888Simp	struct cs_softc *sc;
596140888Simp	struct ifnet *ifp;
597140888Simp
598140888Simp	sc = device_get_softc(dev);
599147256Sbrooks	ifp = sc->ifp;
600140888Simp
601179560Sjhb	CS_LOCK(sc);
602140888Simp	cs_stop(sc);
603179560Sjhb	CS_UNLOCK(sc);
604179560Sjhb	callout_drain(&sc->timer);
605140888Simp	ether_ifdetach(ifp);
606179560Sjhb	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
607150306Simp	cs_release_resources(dev);
608179560Sjhb	free(sc->buffer, M_DEVBUF);
609147256Sbrooks	if_free(ifp);
610179560Sjhb	mtx_destroy(&sc->lock);
611140888Simp	return (0);
612140888Simp}
613140888Simp
61437785Smsmith/*
61537785Smsmith * Initialize the board
61637785Smsmith */
61737785Smsmithstatic void
61837785Smsmithcs_init(void *xsc)
61937785Smsmith{
62037785Smsmith	struct cs_softc *sc=(struct cs_softc *)xsc;
621179560Sjhb
622179560Sjhb	CS_LOCK(sc);
623179560Sjhb	cs_init_locked(sc);
624179560Sjhb	CS_UNLOCK(sc);
625179560Sjhb}
626179560Sjhb
627179560Sjhbstatic void
628179560Sjhbcs_init_locked(struct cs_softc *sc)
629179560Sjhb{
630147256Sbrooks	struct ifnet *ifp = sc->ifp;
631179560Sjhb	int i, rx_cfg;
63237785Smsmith
63337785Smsmith	/*
634179560Sjhb	 * reset watchdog timer
63537785Smsmith	 */
636179560Sjhb	sc->tx_timeout = 0;
63737785Smsmith	sc->buf_len = 0;
63837785Smsmith
63937785Smsmith	/*
64037785Smsmith	 * Hardware initialization of cs
64137785Smsmith	 */
64237785Smsmith
64337785Smsmith	/* Enable receiver and transmitter */
64472940Simp	cs_writereg(sc, PP_LineCTL,
64572940Simp		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
64637785Smsmith
64737785Smsmith	/* Configure the receiver mode */
64837785Smsmith	cs_setmode(sc);
64937785Smsmith
65037785Smsmith	/*
65137785Smsmith	 * This defines what type of frames will cause interrupts
65237785Smsmith	 * Bad frames should generate interrupts so that the driver
65337785Smsmith	 * could track statistics of discarded packets
65437785Smsmith	 */
655140927Simp	rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
65637785Smsmith		 RX_EXTRA_DATA_ENBL;
65737785Smsmith	if (sc->isa_config & STREAM_TRANSFER)
65837785Smsmith		rx_cfg |= RX_STREAM_ENBL;
65972940Simp	cs_writereg(sc, PP_RxCFG, rx_cfg);
66072940Simp	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
66137785Smsmith		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
66237785Smsmith		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
66372940Simp	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
66437785Smsmith		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
66537785Smsmith		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
66637785Smsmith
667140927Simp	/* Write MAC address into IA filter */
668140927Simp	for (i=0; i<ETHER_ADDR_LEN/2; i++)
669140927Simp		cs_writereg(sc, PP_IA + i * 2,
670147256Sbrooks		    sc->enaddr[i * 2] |
671147256Sbrooks		    (sc->enaddr[i * 2 + 1] << 8) );
67237785Smsmith
67337785Smsmith	/*
67437785Smsmith	 * Now enable everything
67537785Smsmith	 */
67637785Smsmith/*
67737785Smsmith#ifdef	CS_USE_64K_DMA
67872940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
67972940Simp#else
680140927Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
68137785Smsmith#endif
68237785Smsmith*/
68372940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
68437785Smsmith
68537785Smsmith	/*
68637785Smsmith	 * Set running and clear output active flags
68737785Smsmith	 */
688148887Srwatson	sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
689148887Srwatson	sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
690179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
69137785Smsmith
69237785Smsmith	/*
69337785Smsmith	 * Start sending process
69437785Smsmith	 */
695179560Sjhb	cs_start_locked(ifp);
69637785Smsmith}
69737785Smsmith
69837785Smsmith/*
699106937Ssam * Get the packet from the board and send it to the upper layer.
70037785Smsmith */
70137785Smsmithstatic int
70237785Smsmithcs_get_packet(struct cs_softc *sc)
70337785Smsmith{
704147256Sbrooks	struct ifnet *ifp = sc->ifp;
705218055Sandrew	int status, length;
70637785Smsmith	struct ether_header *eh;
70737785Smsmith	struct mbuf *m;
70837785Smsmith
70937785Smsmith#ifdef CS_DEBUG
71037785Smsmith	int i;
71137785Smsmith#endif
71237785Smsmith
71372940Simp	status = cs_inw(sc, RX_FRAME_PORT);
71472940Simp	length = cs_inw(sc, RX_FRAME_PORT);
71537785Smsmith
71637785Smsmith#ifdef CS_DEBUG
717162321Sglebius	device_printf(sc->dev, "rcvd: stat %x, len %d\n",
718104252Sbrooks		status, length);
71937785Smsmith#endif
72037785Smsmith
72137785Smsmith	if (!(status & RX_OK)) {
72237785Smsmith#ifdef CS_DEBUG
723162321Sglebius		device_printf(sc->dev, "bad pkt stat %x\n", status);
72437785Smsmith#endif
72537785Smsmith		ifp->if_ierrors++;
726140928Simp		return (-1);
72737785Smsmith	}
72837785Smsmith
729243857Sglebius	MGETHDR(m, M_NOWAIT, MT_DATA);
73037785Smsmith	if (m==NULL)
731140928Simp		return (-1);
73237785Smsmith
73337785Smsmith	if (length > MHLEN) {
734243857Sglebius		MCLGET(m, M_NOWAIT);
73537785Smsmith		if (!(m->m_flags & M_EXT)) {
73637785Smsmith			m_freem(m);
737140928Simp			return (-1);
73837785Smsmith		}
73937785Smsmith	}
74037785Smsmith
74137785Smsmith	/* Initialize packet's header info */
74237785Smsmith	m->m_pkthdr.rcvif = ifp;
74337785Smsmith	m->m_pkthdr.len = length;
74437785Smsmith	m->m_len = length;
74537785Smsmith
74637785Smsmith	/* Get the data */
747218055Sandrew	bus_read_multi_2(sc->port_res, RX_FRAME_PORT, mtod(m, uint16_t *),
748218055Sandrew	    (length + 1) >> 1);
74937785Smsmith
75037785Smsmith	eh = mtod(m, struct ether_header *);
75137785Smsmith
75237785Smsmith#ifdef CS_DEBUG
75337785Smsmith	for (i=0;i<length;i++)
75437785Smsmith	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
75537785Smsmith	printf( "\n" );
75637785Smsmith#endif
75737785Smsmith
75837785Smsmith	if (status & (RX_IA | RX_BROADCAST) ||
75937785Smsmith	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
76037785Smsmith		/* Feed the packet to the upper layer */
761106937Ssam		(*ifp->if_input)(ifp, m);
76237785Smsmith		ifp->if_ipackets++;
763122024Simp		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
764140927Simp			DELAY(cs_recv_delay);
76537785Smsmith	} else {
76637785Smsmith		m_freem(m);
76737785Smsmith	}
76837785Smsmith
769140928Simp	return (0);
77037785Smsmith}
77137785Smsmith
77237785Smsmith/*
77358816Simp * Handle interrupts
77437785Smsmith */
77558816Simpvoid
77658816Simpcsintr(void *arg)
77737785Smsmith{
77858816Simp	struct cs_softc *sc = (struct cs_softc*) arg;
779147256Sbrooks	struct ifnet *ifp = sc->ifp;
78041591Sarchie	int status;
78137785Smsmith
78237785Smsmith#ifdef CS_DEBUG
783162321Sglebius	device_printf(sc->dev, "Interrupt.\n");
78437785Smsmith#endif
78537785Smsmith
786179560Sjhb	CS_LOCK(sc);
78772940Simp	while ((status=cs_inw(sc, ISQ_PORT))) {
78837785Smsmith
78937785Smsmith#ifdef CS_DEBUG
790162321Sglebius		device_printf(sc->dev, "from ISQ: %04x\n", status);
79137785Smsmith#endif
79237785Smsmith
79337785Smsmith		switch (status & ISQ_EVENT_MASK) {
794140927Simp		case ISQ_RECEIVER_EVENT:
795140927Simp			cs_get_packet(sc);
796140927Simp			break;
79737785Smsmith
798140927Simp		case ISQ_TRANSMITTER_EVENT:
799140927Simp			if (status & TX_OK)
800140927Simp				ifp->if_opackets++;
801140927Simp			else
802140927Simp				ifp->if_oerrors++;
803148887Srwatson			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
804179560Sjhb			sc->tx_timeout = 0;
805140927Simp			break;
80637785Smsmith
807140927Simp		case ISQ_BUFFER_EVENT:
808140927Simp			if (status & READY_FOR_TX) {
809148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
810179560Sjhb				sc->tx_timeout = 0;
811140927Simp			}
81237785Smsmith
813140927Simp			if (status & TX_UNDERRUN) {
814148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
815179560Sjhb				sc->tx_timeout = 0;
816140927Simp				ifp->if_oerrors++;
817140927Simp			}
818140927Simp			break;
81937785Smsmith
820140927Simp		case ISQ_RX_MISS_EVENT:
821140927Simp			ifp->if_ierrors+=(status>>6);
822140927Simp			break;
82337785Smsmith
824140927Simp		case ISQ_TX_COL_EVENT:
825140927Simp			ifp->if_collisions+=(status>>6);
826140927Simp			break;
827140927Simp		}
828140927Simp	}
82937785Smsmith
830148887Srwatson	if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
831179560Sjhb		cs_start_locked(ifp);
832140927Simp	}
833179560Sjhb	CS_UNLOCK(sc);
83437785Smsmith}
83537785Smsmith
83637785Smsmith/*
83737785Smsmith * Save the data in buffer
83837785Smsmith */
83937785Smsmith
84037785Smsmithstatic void
84137785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
84237785Smsmith{
84337785Smsmith	int len;
84437785Smsmith	struct mbuf *mp;
84537785Smsmith	unsigned char *data, *buf;
84637785Smsmith
84737785Smsmith	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
84837785Smsmith		len = mp->m_len;
84937785Smsmith
85037785Smsmith		/*
85137785Smsmith		 * Ignore empty parts
85237785Smsmith		 */
85337785Smsmith		if (!len)
854202580Simp			continue;
85537785Smsmith
85637785Smsmith		/*
85737785Smsmith		 * Find actual data address
85837785Smsmith		 */
85937785Smsmith		data = mtod(mp, caddr_t);
86037785Smsmith
86137785Smsmith		bcopy((caddr_t) data, (caddr_t) buf, len);
86237785Smsmith		buf += len;
86337785Smsmith		sc->buf_len += len;
86437785Smsmith	}
86537785Smsmith}
86637785Smsmith
86737785Smsmith
86837785Smsmithstatic void
86937785Smsmithcs_xmit_buf( struct cs_softc *sc )
87037785Smsmith{
871218055Sandrew	bus_write_multi_2(sc->port_res, TX_FRAME_PORT, (uint16_t *)sc->buffer,
872218055Sandrew	    (sc->buf_len + 1) >> 1);
87337785Smsmith	sc->buf_len = 0;
87437785Smsmith}
87537785Smsmith
87637785Smsmithstatic void
87737785Smsmithcs_start(struct ifnet *ifp)
87837785Smsmith{
879179560Sjhb	struct cs_softc *sc = ifp->if_softc;
880179560Sjhb
881179560Sjhb	CS_LOCK(sc);
882179560Sjhb	cs_start_locked(ifp);
883179560Sjhb	CS_UNLOCK(sc);
884179560Sjhb}
885179560Sjhb
886179560Sjhbstatic void
887179560Sjhbcs_start_locked(struct ifnet *ifp)
888179560Sjhb{
889179560Sjhb	int length;
89037785Smsmith	struct mbuf *m, *mp;
89137785Smsmith	struct cs_softc *sc = ifp->if_softc;
89237785Smsmith
89337785Smsmith	for (;;) {
89437785Smsmith		if (sc->buf_len)
89537785Smsmith			length = sc->buf_len;
89637785Smsmith		else {
89737785Smsmith			IF_DEQUEUE( &ifp->if_snd, m );
89837785Smsmith
89937785Smsmith			if (m==NULL) {
90037785Smsmith				return;
90137785Smsmith			}
90237785Smsmith
90337785Smsmith			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
90437785Smsmith				length += mp->m_len;
90537785Smsmith
90637785Smsmith			/* Skip zero-length packets */
90737785Smsmith			if (length == 0) {
90837785Smsmith				m_freem(m);
90937785Smsmith				continue;
91037785Smsmith			}
91137785Smsmith
91237785Smsmith			cs_write_mbufs(sc, m);
91337785Smsmith
914106937Ssam			BPF_MTAP(ifp, m);
91537785Smsmith
91637785Smsmith			m_freem(m);
91737785Smsmith		}
91837785Smsmith
91937785Smsmith		/*
92037785Smsmith		 * Issue a SEND command
92137785Smsmith		 */
92272940Simp		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
92372940Simp		cs_outw(sc, TX_LEN_PORT, length );
92437785Smsmith
92537785Smsmith		/*
92637785Smsmith		 * If there's no free space in the buffer then leave
92737785Smsmith		 * this packet for the next time: indicate output active
92837785Smsmith		 * and return.
92937785Smsmith		 */
93072940Simp		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
931179560Sjhb			sc->tx_timeout = sc->buf_len;
932148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
93337785Smsmith			return;
93437785Smsmith		}
93537785Smsmith
936140927Simp		cs_xmit_buf(sc);
93737785Smsmith
93837785Smsmith		/*
93937785Smsmith		 * Set the watchdog timer in case we never hear
94037785Smsmith		 * from board again. (I don't know about correct
94137785Smsmith		 * value for this timeout)
94237785Smsmith		 */
943179560Sjhb		sc->tx_timeout = length;
94437785Smsmith
945148887Srwatson		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
94637785Smsmith		return;
94737785Smsmith	}
94837785Smsmith}
94937785Smsmith
95037785Smsmith/*
95137785Smsmith * Stop everything on the interface
95237785Smsmith */
95337785Smsmithstatic void
95437785Smsmithcs_stop(struct cs_softc *sc)
95537785Smsmith{
95637785Smsmith
957179560Sjhb	CS_ASSERT_LOCKED(sc);
95872940Simp	cs_writereg(sc, PP_RxCFG, 0);
95972940Simp	cs_writereg(sc, PP_TxCFG, 0);
96072940Simp	cs_writereg(sc, PP_BufCFG, 0);
96172940Simp	cs_writereg(sc, PP_BusCTL, 0);
96237785Smsmith
963148887Srwatson	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
964179560Sjhb	sc->tx_timeout = 0;
965179560Sjhb	callout_stop(&sc->timer);
96637785Smsmith}
96737785Smsmith
96837785Smsmith/*
96937785Smsmith * Reset the interface
97037785Smsmith */
97137785Smsmithstatic void
97237785Smsmithcs_reset(struct cs_softc *sc)
97337785Smsmith{
974179560Sjhb
975179560Sjhb	CS_ASSERT_LOCKED(sc);
97637785Smsmith	cs_stop(sc);
977179560Sjhb	cs_init_locked(sc);
97837785Smsmith}
97937785Smsmith
980156559Sphilipstatic uint16_t
981156559Sphilipcs_hash_index(struct sockaddr_dl *addr)
982156559Sphilip{
983156559Sphilip	uint32_t crc;
984156559Sphilip	uint16_t idx;
985156559Sphilip	caddr_t lla;
986156559Sphilip
987156559Sphilip	lla = LLADDR(addr);
988156559Sphilip	crc = ether_crc32_le(lla, ETHER_ADDR_LEN);
989156559Sphilip	idx = crc >> 26;
990156559Sphilip
991156559Sphilip	return (idx);
992156559Sphilip}
993156559Sphilip
99437785Smsmithstatic void
99537785Smsmithcs_setmode(struct cs_softc *sc)
99637785Smsmith{
997156559Sphilip	int rx_ctl;
998156559Sphilip	uint16_t af[4];
999156559Sphilip	uint16_t port, mask, index;
1000147256Sbrooks	struct ifnet *ifp = sc->ifp;
1001156559Sphilip	struct ifmultiaddr *ifma;
100237785Smsmith
100337785Smsmith	/* Stop the receiver while changing filters */
100472940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
100537785Smsmith
100637785Smsmith	if (ifp->if_flags & IFF_PROMISC) {
100737785Smsmith		/* Turn on promiscuous mode. */
100837785Smsmith		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1009156559Sphilip	} else if (ifp->if_flags & IFF_MULTICAST) {
1010156559Sphilip		/* Allow receiving frames with multicast addresses */
1011156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1012156559Sphilip			 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1013156559Sphilip
1014156559Sphilip		/* Start with an empty filter */
1015156559Sphilip		af[0] = af[1] = af[2] = af[3] = 0x0000;
1016156559Sphilip
1017156559Sphilip		if (ifp->if_flags & IFF_ALLMULTI) {
1018156559Sphilip			/* Accept all multicast frames */
1019156559Sphilip			af[0] = af[1] = af[2] = af[3] = 0xffff;
102037785Smsmith		} else {
1021156559Sphilip			/*
1022156559Sphilip			 * Set up the filter to only accept multicast
1023156559Sphilip			 * frames we're interested in.
102437785Smsmith			 */
1025195049Srwatson			if_maddr_rlock(ifp);
1026156559Sphilip			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1027156559Sphilip				struct sockaddr_dl *dl =
1028156559Sphilip				    (struct sockaddr_dl *)ifma->ifma_addr;
1029156559Sphilip
1030156559Sphilip				index = cs_hash_index(dl);
1031156559Sphilip				port = (u_int16_t) (index >> 4);
1032156559Sphilip				mask = (u_int16_t) (1 << (index & 0xf));
1033156559Sphilip				af[port] |= mask;
1034156559Sphilip			}
1035195049Srwatson			if_maddr_runlock(ifp);
103637785Smsmith		}
1037156559Sphilip
1038156559Sphilip		cs_writereg(sc, PP_LAF + 0, af[0]);
1039156559Sphilip		cs_writereg(sc, PP_LAF + 2, af[1]);
1040156559Sphilip		cs_writereg(sc, PP_LAF + 4, af[2]);
1041156559Sphilip		cs_writereg(sc, PP_LAF + 6, af[3]);
1042156559Sphilip	} else {
1043156559Sphilip		/*
1044156559Sphilip		 * Receive only good frames addressed for us and
1045156559Sphilip		 * good broadcasts.
1046156559Sphilip		 */
1047156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1048156559Sphilip			 RX_OK_ACCEPT;
104937785Smsmith	}
105037785Smsmith
105137785Smsmith	/* Set up the filter */
105272940Simp	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
105337785Smsmith
105437785Smsmith	/* Turn on receiver */
105572940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
105637785Smsmith}
105737785Smsmith
105837785Smsmithstatic int
105937785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
106037785Smsmith{
106137785Smsmith	struct cs_softc *sc=ifp->if_softc;
106237785Smsmith	struct ifreq *ifr = (struct ifreq *)data;
1063179560Sjhb	int error=0;
106437785Smsmith
106537785Smsmith#ifdef CS_DEBUG
1066162321Sglebius	if_printf(ifp, "%s command=%lx\n", __func__, command);
106737785Smsmith#endif
106837785Smsmith
106937785Smsmith	switch (command) {
107037785Smsmith	case SIOCSIFFLAGS:
107137785Smsmith		/*
107237785Smsmith		 * Switch interface state between "running" and
107337785Smsmith		 * "stopped", reflecting the UP flag.
1074140927Simp		 */
1075179560Sjhb		CS_LOCK(sc);
1076147256Sbrooks		if (sc->ifp->if_flags & IFF_UP) {
1077148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) {
1078179560Sjhb				cs_init_locked(sc);
1079140927Simp			}
1080140927Simp		} else {
1081148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) {
1082140927Simp				cs_stop(sc);
1083140927Simp			}
1084179560Sjhb		}
108537785Smsmith		/*
108637785Smsmith		 * Promiscuous and/or multicast flags may have changed,
108737785Smsmith		 * so reprogram the multicast filter and/or receive mode.
108837785Smsmith		 *
108937785Smsmith		 * See note about multicasts in cs_setmode
109037785Smsmith		 */
109137785Smsmith		cs_setmode(sc);
1092179560Sjhb		CS_UNLOCK(sc);
109337785Smsmith		break;
109437785Smsmith
109537785Smsmith	case SIOCADDMULTI:
109637785Smsmith	case SIOCDELMULTI:
109737785Smsmith	    /*
109837785Smsmith	     * Multicast list has changed; set the hardware filter
109937785Smsmith	     * accordingly.
110037785Smsmith	     *
110137785Smsmith	     * See note about multicasts in cs_setmode
110237785Smsmith	     */
1103179560Sjhb	    CS_LOCK(sc);
110437785Smsmith	    cs_setmode(sc);
1105179560Sjhb	    CS_UNLOCK(sc);
110637785Smsmith	    error = 0;
110737785Smsmith	    break;
110837785Smsmith
1109140927Simp	case SIOCSIFMEDIA:
1110140927Simp	case SIOCGIFMEDIA:
1111140927Simp		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1112140927Simp		break;
111337785Smsmith
1114140927Simp	default:
1115140921Simp		error = ether_ioctl(ifp, command, data);
1116106937Ssam		break;
1117140927Simp	}
111837785Smsmith
1119140928Simp	return (error);
112037785Smsmith}
112137785Smsmith
112237785Smsmith/*
112337785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to
112437785Smsmith * generate an interrupt after a transmit has been started on it.
112537785Smsmith */
112637785Smsmithstatic void
1127179560Sjhbcs_watchdog(void *arg)
112837785Smsmith{
1129179560Sjhb	struct cs_softc *sc = arg;
1130179560Sjhb	struct ifnet *ifp = sc->ifp;
113137785Smsmith
1132179560Sjhb	CS_ASSERT_LOCKED(sc);
1133179560Sjhb	if (sc->tx_timeout && --sc->tx_timeout == 0) {
1134179560Sjhb		ifp->if_oerrors++;
1135179560Sjhb		log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
113637785Smsmith
1137179560Sjhb		/* Reset the interface */
1138179560Sjhb		if (ifp->if_flags & IFF_UP)
1139179560Sjhb			cs_reset(sc);
1140179560Sjhb		else
1141179560Sjhb			cs_stop(sc);
1142179560Sjhb	}
1143179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
114437785Smsmith}
114537785Smsmith
114637785Smsmithstatic int
114737785Smsmithcs_mediachange(struct ifnet *ifp)
114837785Smsmith{
114937785Smsmith	struct cs_softc *sc = ifp->if_softc;
115037785Smsmith	struct ifmedia *ifm = &sc->media;
1151179560Sjhb	int error;
115237785Smsmith
115337785Smsmith	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1154140928Simp		return (EINVAL);
115537785Smsmith
1156179560Sjhb	CS_LOCK(sc);
1157179560Sjhb	error = cs_mediaset(sc, ifm->ifm_media);
1158179560Sjhb	CS_UNLOCK(sc);
1159179560Sjhb	return (error);
116037785Smsmith}
116137785Smsmith
116237785Smsmithstatic void
116337785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
116437785Smsmith{
116537785Smsmith	int line_status;
116637785Smsmith	struct cs_softc *sc = ifp->if_softc;
116737785Smsmith
1168179560Sjhb	CS_LOCK(sc);
116937785Smsmith	ifmr->ifm_active = IFM_ETHER;
117072940Simp	line_status = cs_readreg(sc, PP_LineST);
117137785Smsmith	if (line_status & TENBASET_ON) {
117237785Smsmith		ifmr->ifm_active |= IFM_10_T;
117337785Smsmith		if (sc->chip_type != CS8900) {
117472940Simp			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
117537785Smsmith				ifmr->ifm_active |= IFM_FDX;
117672940Simp			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
117737785Smsmith				ifmr->ifm_active |= IFM_HDX;
117837785Smsmith		}
117937785Smsmith		ifmr->ifm_status = IFM_AVALID;
118037785Smsmith		if (line_status & LINK_OK)
118137785Smsmith			ifmr->ifm_status |= IFM_ACTIVE;
118237785Smsmith	} else {
118338305Smsmith		if (line_status & AUI_ON) {
118472940Simp			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
118572940Simp			    HCB1_ENBL);
118638305Smsmith			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
118772940Simp			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
118838305Smsmith				ifmr->ifm_active |= IFM_10_2;
118938305Smsmith			else
119038305Smsmith				ifmr->ifm_active |= IFM_10_5;
119138305Smsmith		}
119237785Smsmith	}
1193179560Sjhb	CS_UNLOCK(sc);
119437785Smsmith}
119537785Smsmith
119637785Smsmithstatic int
119737785Smsmithcs_mediaset(struct cs_softc *sc, int media)
119837785Smsmith{
1199179532Simp	int error = 0;
120037785Smsmith
120137785Smsmith	/* Stop the receiver & transmitter */
120272940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
120372940Simp	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
120437785Smsmith
120537785Smsmith#ifdef CS_DEBUG
1206162321Sglebius	device_printf(sc->dev, "%s media=%x\n", __func__, media);
120737785Smsmith#endif
120837785Smsmith
120937785Smsmith	switch (IFM_SUBTYPE(media)) {
121037785Smsmith	default:
121137785Smsmith	case IFM_AUTO:
1212179553Simp		/*
1213179553Simp		 * This chip makes it a little hard to support this, so treat
1214179553Simp		 * it as IFM_10_T, auto duplex.
1215179553Simp		 */
1216179553Simp		enable_tp(sc);
1217179553Simp		cs_duplex_auto(sc);
121837785Smsmith		break;
121937785Smsmith	case IFM_10_T:
1220179532Simp		enable_tp(sc);
122137785Smsmith		if (media & IFM_FDX)
122237785Smsmith			cs_duplex_full(sc);
122337785Smsmith		else if (media & IFM_HDX)
122437785Smsmith			cs_duplex_half(sc);
122537785Smsmith		else
122637785Smsmith			error = cs_duplex_auto(sc);
122737785Smsmith		break;
122837785Smsmith	case IFM_10_2:
1229180388Simp		enable_bnc(sc);
123037785Smsmith		break;
123137785Smsmith	case IFM_10_5:
1232180388Simp		enable_aui(sc);
123337785Smsmith		break;
123437785Smsmith	}
123537785Smsmith
123637785Smsmith	/*
123737785Smsmith	 * Turn the transmitter & receiver back on
123837785Smsmith	 */
123972940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
124072940Simp	    SERIAL_RX_ON | SERIAL_TX_ON);
124137785Smsmith
1242140928Simp	return (error);
124337785Smsmith}
1244