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: stable/11/sys/dev/cs/if_cs.c 347962 2019-05-18 20:43:13Z brooks $");
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>
59257176Sglebius#include <net/if_var.h>
6037785Smsmith#include <net/if_arp.h>
61156559Sphilip#include <net/if_dl.h>
6237785Smsmith#include <net/if_media.h>
63147256Sbrooks#include <net/if_types.h>
6437785Smsmith#include <net/ethernet.h>
6537785Smsmith#include <net/bpf.h>
6637785Smsmith
6772940Simp#include <dev/cs/if_csvar.h>
6858816Simp#include <dev/cs/if_csreg.h>
6937785Smsmith
7037785Smsmith#ifdef  CS_USE_64K_DMA
7137785Smsmith#define CS_DMA_BUFFER_SIZE 65536
7237785Smsmith#else
7337785Smsmith#define CS_DMA_BUFFER_SIZE 16384
7437785Smsmith#endif
7537785Smsmith
76122024Simpstatic void	cs_init(void *);
77179560Sjhbstatic void	cs_init_locked(struct cs_softc *);
78122024Simpstatic int	cs_ioctl(struct ifnet *, u_long, caddr_t);
79122024Simpstatic void	cs_start(struct ifnet *);
80179560Sjhbstatic void	cs_start_locked(struct ifnet *);
81122024Simpstatic void	cs_stop(struct cs_softc *);
82122024Simpstatic void	cs_reset(struct cs_softc *);
83179560Sjhbstatic void	cs_watchdog(void *);
8438592Smsmith
85122024Simpstatic int	cs_mediachange(struct ifnet *);
86122024Simpstatic void	cs_mediastatus(struct ifnet *, struct ifmediareq *);
87122024Simpstatic int      cs_mediaset(struct cs_softc *, int);
8858816Simp
8937785Smsmithstatic void	cs_write_mbufs(struct cs_softc*, struct mbuf*);
9037785Smsmithstatic void	cs_xmit_buf(struct cs_softc*);
9137785Smsmithstatic int	cs_get_packet(struct cs_softc*);
9237785Smsmithstatic void	cs_setmode(struct cs_softc*);
9337785Smsmith
94122024Simpstatic int	get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *);
95122024Simpstatic int	get_eeprom_cksum(int, int, uint16_t *);
9637785Smsmithstatic int	wait_eeprom_ready( struct cs_softc *);
9737785Smsmithstatic void	control_dc_dc( struct cs_softc *, int );
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;
104179597Simpdriver_intr_t	csintr;
10537785Smsmith
106122024Simp/* sysctl vars */
107227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
108122024Simp
109122024Simpint	cs_ignore_cksum_failure = 0;
110267992ShselaskySYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RWTUN,
111122024Simp    &cs_ignore_cksum_failure, 0,
112122024Simp  "ignore checksum errors in cs card EEPROM");
113122024Simp
114122024Simpstatic int	cs_recv_delay = 570;
115267992ShselaskySYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RWTUN, &cs_recv_delay, 570, "");
116122024Simp
117179532Simpstatic int cs8900_eeint2irq[16] = {
118179532Simp	 10,  11,  12,   5, 255, 255, 255, 255,
119179532Simp	255, 255, 255, 255, 255, 255, 255, 255
120179532Simp};
121179532Simp
122179532Simpstatic int cs8900_irq2eeint[16] = {
123179532Simp	255, 255, 255, 255, 255,   3, 255, 255,
124179532Simp	255,   0,   1,   2, 255, 255, 255, 255
125179532Simp};
126179532Simp
12737785Smsmithstatic int
128122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
12937785Smsmith{
13037785Smsmith	int i;
13137785Smsmith
13237785Smsmith#ifdef CS_DEBUG
133179620Simp	device_printf(sc->dev, "EEPROM data from %x for %x:\n", off, len);
13437785Smsmith#endif
135122024Simp	for (i=0; i < len; i++) {
136122024Simp		if (wait_eeprom_ready(sc) < 0)
137122024Simp			return (-1);
13837785Smsmith		/* Send command to EEPROM to read */
13972940Simp		cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
140122024Simp		if (wait_eeprom_ready(sc) < 0)
14172940Simp			return (-1);
14272940Simp		buffer[i] = cs_readreg(sc, PP_EEData);
14337785Smsmith
14437785Smsmith#ifdef CS_DEBUG
145179591Simp		printf("%04x ",buffer[i]);
14637785Smsmith#endif
14737785Smsmith	}
14837785Smsmith
14937785Smsmith#ifdef CS_DEBUG
15037785Smsmith	printf("\n");
15137785Smsmith#endif
15272940Simp	return (0);
15337785Smsmith}
15437785Smsmith
15537785Smsmithstatic int
156122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer)
15737785Smsmith{
158122024Simp	int i;
159122024Simp	uint16_t cksum=0;
16037785Smsmith
161122024Simp	for (i = 0; i < len; i++)
162122024Simp		cksum += buffer[i];
16337785Smsmith	cksum &= 0xffff;
164179591Simp	if (cksum == 0 || cs_ignore_cksum_failure)
165122024Simp		return (0);
166122024Simp	return (-1);
16737785Smsmith}
16837785Smsmith
16937785Smsmithstatic int
17037785Smsmithwait_eeprom_ready(struct cs_softc *sc)
17137785Smsmith{
172179620Simp	int i;
173179620Simp
174179620Simp	/*
175179620Simp	 * From the CS8900A datasheet, section 3.5.2:
176179620Simp	 * "Before issuing any command to the EEPROM, the host must wait
177179620Simp	 * for the SIBUSY bit (Register 16, SelfST, bit 8) to clear.  After
178179620Simp	 * each command has been issued, the host must wait again for SIBUSY
179179620Simp	 * to clear."
180179620Simp	 *
181179620Simp	 * Before we issue the command, we should be !busy, so that will
182179620Simp	 * be fast.  The datasheet suggests that clock out from the part
183298955Spfg	 * per word will be on the order of 25us, which is consistent with
184179620Simp	 * the 1MHz serial clock and 16bits...  We should never hit 100,
185179621Simp	 * let alone 15,000 here.  The original code did an unconditional
186179621Simp	 * 30ms DELAY here.  Bad Kharma.  cs_readreg takes ~2us.
187179620Simp	 */
188179621Simp	for (i = 0; i < 15000; i++)	/* 30ms max */
189179620Simp		if (!(cs_readreg(sc, PP_SelfST) & SI_BUSY))
190179620Simp			return (0);
191179620Simp	return (1);
19237785Smsmith}
19337785Smsmith
19437785Smsmithstatic void
19537785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off)
19637785Smsmith{
19737785Smsmith	unsigned int self_control = HCB1_ENBL;
19837785Smsmith
19937785Smsmith	if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
20037785Smsmith		self_control |= HCB1;
20137785Smsmith	else
20237785Smsmith		self_control &= ~HCB1;
20372940Simp	cs_writereg(sc, PP_SelfCTL, self_control);
204180388Simp	DELAY(500000);	/* Bad! */
20537785Smsmith}
20637785Smsmith
20737785Smsmith
20837785Smsmithstatic int
20937785Smsmithcs_duplex_auto(struct cs_softc *sc)
21037785Smsmith{
211140927Simp	int i, error=0;
212140927Simp
21372940Simp	cs_writereg(sc, PP_AutoNegCTL,
21472940Simp	    RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
215140927Simp	for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
216179597Simp		if (i > 4000) {
217162321Sglebius			device_printf(sc->dev,
218140927Simp			    "full/half duplex auto negotiation timeout\n");
21937785Smsmith			error = ETIMEDOUT;
220140927Simp			break;
221140927Simp		}
222140927Simp		DELAY(1000);
223140927Simp	}
224140928Simp	return (error);
22537785Smsmith}
22637785Smsmith
22737785Smsmithstatic int
22837785Smsmithenable_tp(struct cs_softc *sc)
22937785Smsmith{
23037785Smsmith
23172940Simp	cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
23237785Smsmith	control_dc_dc(sc, 0);
233140928Simp	return (0);
23437785Smsmith}
23537785Smsmith
23637785Smsmithstatic int
23737785Smsmithenable_aui(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, 0);
243140928Simp	return (0);
24437785Smsmith}
24537785Smsmith
24637785Smsmithstatic int
24737785Smsmithenable_bnc(struct cs_softc *sc)
24837785Smsmith{
24937785Smsmith
25072940Simp	cs_writereg(sc, PP_LineCTL,
25172940Simp	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
252179597Simp	control_dc_dc(sc, 1);
253140928Simp	return (0);
25437785Smsmith}
25537785Smsmith
25671316Simpint
25758816Simpcs_cs89x0_probe(device_t dev)
25837785Smsmith{
25958816Simp	int i;
26058816Simp	int error;
261294883Sjhibbits	rman_res_t irq, junk;
26258816Simp	struct cs_softc *sc = device_get_softc(dev);
26337785Smsmith	unsigned rev_type = 0;
264122024Simp	uint16_t id;
26558816Simp	char chip_revision;
266122024Simp	uint16_t eeprom_buff[CHKSUM_LEN];
267269980Simp	int chip_type, pp_isaint;
26837785Smsmith
269179620Simp	sc->dev = dev;
27058816Simp	error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
27158816Simp	if (error)
27258816Simp		return (error);
27358816Simp
27472940Simp	if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
27537785Smsmith		/* Chip not detected. Let's try to reset it */
27637785Smsmith		if (bootverbose)
27758816Simp			device_printf(dev, "trying to reset the chip.\n");
27872940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
27972940Simp		i = cs_inw(sc, DATA_PORT);
28072940Simp		cs_outw(sc, ADD_PORT, PP_SelfCTL);
28172940Simp		cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
28272940Simp		if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
28358816Simp			return (ENXIO);
28437785Smsmith	}
28537785Smsmith
28672940Simp	for (i = 0; i < 10000; i++) {
28772940Simp		id = cs_readreg(sc, PP_ChipID);
28872940Simp		if (id == CHIP_EISA_ID_SIG)
28972940Simp			break;
29072940Simp	}
29172940Simp	if (i == 10000)
29258816Simp		return (ENXIO);
29337785Smsmith
29472940Simp	rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
29537785Smsmith	chip_type = rev_type & ~REVISON_BITS;
29637785Smsmith	chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
29737785Smsmith
29837785Smsmith	sc->chip_type = chip_type;
29958816Simp
300179532Simp	if (chip_type == CS8900) {
30137785Smsmith		pp_isaint = PP_CS8900_ISAINT;
30237785Smsmith		sc->send_cmd = TX_CS8900_AFTER_ALL;
30337785Smsmith	} else {
30437785Smsmith		pp_isaint = PP_CS8920_ISAINT;
30537785Smsmith		sc->send_cmd = TX_CS8920_AFTER_ALL;
30637785Smsmith	}
30737785Smsmith
30837785Smsmith	/*
309140927Simp	 * Clear some fields so that fail of EEPROM will left them clean
310140927Simp	 */
311140927Simp	sc->auto_neg_cnf = 0;
312140927Simp	sc->adapter_cnf  = 0;
313140927Simp	sc->isa_config   = 0;
314140927Simp
315140927Simp	/*
316179532Simp	 * If no interrupt specified, use what the board tells us.
31737785Smsmith	 */
31858816Simp	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
31958816Simp
32058816Simp	/*
32158816Simp	 * Get data from EEPROM
32258816Simp	 */
32372940Simp	if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
32458816Simp		device_printf(dev, "No EEPROM, assuming defaults.\n");
325179532Simp	} else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
326179532Simp		device_printf(dev, "EEPROM read failed, assuming defaults.\n");
327179532Simp	} else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
328179532Simp		device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n");
32937785Smsmith	} else {
330180388Simp		sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET];
331180388Simp		sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET];
332180388Simp		sc->isa_config = eeprom_buff[ISA_CNF_OFFSET];
333179532Simp		for (i=0; i<ETHER_ADDR_LEN/2; i++) {
334179532Simp			sc->enaddr[i*2] = eeprom_buff[i];
335179532Simp			sc->enaddr[i*2+1] = eeprom_buff[i] >> 8;
336179532Simp		}
337179532Simp		/*
338179532Simp		 * If no interrupt specified, use what the
339179532Simp		 * board tells us.
340179532Simp		 */
341179532Simp		if (error) {
342179532Simp			irq = sc->isa_config & INT_NO_MASK;
343179532Simp			error = 0;
344179532Simp			if (chip_type == CS8900) {
345179532Simp				irq = cs8900_eeint2irq[irq];
34637785Smsmith			} else {
347179532Simp				if (irq > CS8920_NO_INTS)
348179532Simp					irq = 255;
34937785Smsmith			}
350179532Simp			if (irq == 255) {
351179532Simp				device_printf(dev, "invalid irq in EEPROM.\n");
352179532Simp				error = EINVAL;
353179532Simp			}
354179532Simp			if (!error)
355179532Simp				bus_set_resource(dev, SYS_RES_IRQ, 0,
356179532Simp				    irq, 1);
357140927Simp		}
358140927Simp	}
35937785Smsmith
360179532Simp	if (!error && !(sc->flags & CS_NO_IRQ)) {
361140927Simp		if (chip_type == CS8900) {
362217538Sjhb			if (irq < 16)
363179532Simp				irq = cs8900_irq2eeint[irq];
364179532Simp			else
365179532Simp				irq = 255;
366140927Simp		} else {
367179532Simp			if (irq > CS8920_NO_INTS)
368179532Simp				irq = 255;
369140927Simp		}
370179532Simp		if (irq == 255)
371179532Simp			error = EINVAL;
37258816Simp	}
37358816Simp
374179507Simp	if (error) {
37558816Simp	       	device_printf(dev, "Unknown or invalid irq\n");
376179507Simp		return (error);
377140927Simp	}
378140927Simp
379179507Simp	if (!(sc->flags & CS_NO_IRQ))
380179507Simp		cs_writereg(sc, pp_isaint, irq);
381179507Simp
38237785Smsmith	if (bootverbose)
38358816Simp		 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
384179532Simp			chip_type == CS8900 ? '0' : '2',
385179532Simp			chip_type == CS8920M ? "M" : "",
38637785Smsmith			chip_revision,
38737785Smsmith			(sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
38837785Smsmith			(sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
38958816Simp			(sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
39037785Smsmith
391140927Simp	if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
392140927Simp	    (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
393140927Simp		sc->line_ctl = LOW_RX_SQUELCH;
394140927Simp	else
395140927Simp		sc->line_ctl = 0;
39637785Smsmith
397140928Simp	return (0);
39837785Smsmith}
39937785Smsmith
40037785Smsmith/*
40158816Simp * Allocate a port resource with the given resource id.
40258816Simp */
403179560Sjhbint
404179560Sjhbcs_alloc_port(device_t dev, int rid, int size)
40558816Simp{
406140927Simp	struct cs_softc *sc = device_get_softc(dev);
407140927Simp	struct resource *res;
40858816Simp
409296137Sjhibbits	res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
410296137Sjhibbits	    size, RF_ACTIVE);
411140928Simp	if (res == NULL)
412140927Simp		return (ENOENT);
413140928Simp	sc->port_rid = rid;
414140928Simp	sc->port_res = res;
415140928Simp	return (0);
41658816Simp}
41758816Simp
41858816Simp/*
41958816Simp * Allocate an irq resource with the given resource id.
42058816Simp */
421179560Sjhbint
422179597Simpcs_alloc_irq(device_t dev, int rid)
42358816Simp{
424140927Simp	struct cs_softc *sc = device_get_softc(dev);
425140927Simp	struct resource *res;
42658816Simp
427179597Simp	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
428140928Simp	if (res == NULL)
429140927Simp		return (ENOENT);
430140928Simp	sc->irq_rid = rid;
431140928Simp	sc->irq_res = res;
432140928Simp	return (0);
43358816Simp}
43458816Simp
43558816Simp/*
43658816Simp * Release all resources
43758816Simp */
438179560Sjhbvoid
439179560Sjhbcs_release_resources(device_t dev)
44058816Simp{
441140927Simp	struct cs_softc *sc = device_get_softc(dev);
44258816Simp
443140927Simp	if (sc->port_res) {
444140927Simp		bus_release_resource(dev, SYS_RES_IOPORT,
445140927Simp		    sc->port_rid, sc->port_res);
446140927Simp		sc->port_res = 0;
447140927Simp	}
448140927Simp	if (sc->irq_res) {
449140927Simp		bus_release_resource(dev, SYS_RES_IRQ,
450140927Simp		    sc->irq_rid, sc->irq_res);
451140927Simp		sc->irq_res = 0;
452140927Simp	}
45358816Simp}
45458816Simp
45558816Simp/*
45637785Smsmith * Install the interface into kernel networking data structures
45737785Smsmith */
45871316Simpint
459121816Sbrookscs_attach(device_t dev)
46037785Smsmith{
461179560Sjhb	int error, media=0;
462201758Smbr	struct cs_softc *sc = device_get_softc(dev);
463147256Sbrooks	struct ifnet *ifp;
46437785Smsmith
465162321Sglebius	sc->dev = dev;
466162321Sglebius
467147256Sbrooks	ifp = sc->ifp = if_alloc(IFT_ETHER);
468147256Sbrooks	if (ifp == NULL) {
469147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
470179560Sjhb		cs_release_resources(dev);
471179560Sjhb		return (ENOMEM);
472147256Sbrooks	}
473147256Sbrooks
474179560Sjhb	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
475179560Sjhb	    MTX_DEF);
476179560Sjhb	callout_init_mtx(&sc->timer, &sc->lock, 0);
47758816Simp
478179560Sjhb	CS_LOCK(sc);
479179560Sjhb	cs_stop(sc);
480179560Sjhb	CS_UNLOCK(sc);
481179560Sjhb
482121752Sbrooks	ifp->if_softc=sc;
483121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
484121752Sbrooks	ifp->if_start=cs_start;
485121752Sbrooks	ifp->if_ioctl=cs_ioctl;
486121752Sbrooks	ifp->if_init=cs_init;
487207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
48837785Smsmith
489179560Sjhb	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
49037785Smsmith
491121752Sbrooks	/*
492121752Sbrooks	 * this code still in progress (DMA support)
493121752Sbrooks	 *
49437785Smsmith
495121752Sbrooks	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
496121752Sbrooks	if (sc->recv_ring == NULL) {
497121816Sbrooks		log(LOG_ERR,
498121816Sbrooks		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
499121752Sbrooks		return(0);
500121752Sbrooks	}
501121752Sbrooks	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
502121752Sbrooks	    < (128*1024-CS_DMA_BUFFER_SIZE))
503121752Sbrooks	    sc->recv_ring+=16*1024;
50437785Smsmith
505121752Sbrooks	*/
50637785Smsmith
507121752Sbrooks	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
508121752Sbrooks	if (sc->buffer == NULL) {
509162321Sglebius		device_printf(sc->dev, "Couldn't allocate memory for NIC\n");
510179560Sjhb		if_free(ifp);
511179560Sjhb		mtx_destroy(&sc->lock);
512179560Sjhb		cs_release_resources(dev);
513179560Sjhb		return(ENOMEM);
514121752Sbrooks	}
51537785Smsmith
516121752Sbrooks	/*
517121752Sbrooks	 * Initialize the media structures.
518121752Sbrooks	 */
519121752Sbrooks	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
52037785Smsmith
521121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_T) {
522121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
523121752Sbrooks		if (sc->chip_type != CS8900) {
524121752Sbrooks			ifmedia_add(&sc->media,
525121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
526121752Sbrooks			ifmedia_add(&sc->media,
527121752Sbrooks				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
528121752Sbrooks		}
529121752Sbrooks	}
53037785Smsmith
531121752Sbrooks	if (sc->adapter_cnf & A_CNF_10B_2)
532121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
53337785Smsmith
534121752Sbrooks	if (sc->adapter_cnf & A_CNF_AUI)
535121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
53637785Smsmith
537121752Sbrooks	if (sc->adapter_cnf & A_CNF_MEDIA)
538121752Sbrooks		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
53937785Smsmith
540121752Sbrooks	/* Set default media from EEPROM */
541121752Sbrooks	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
542121752Sbrooks	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
543121752Sbrooks	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
544121752Sbrooks	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
545121752Sbrooks	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
546122024Simp	default:
547162321Sglebius		device_printf(sc->dev, "no media, assuming 10baseT\n");
548122024Simp		sc->adapter_cnf |= A_CNF_10B_T;
549122024Simp		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
550122024Simp		if (sc->chip_type != CS8900) {
551122024Simp			ifmedia_add(&sc->media,
552122024Simp			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
553122024Simp			ifmedia_add(&sc->media,
554122024Simp			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
555122024Simp		}
556122024Simp		media = IFM_ETHER | IFM_10_T;
557122024Simp		break;
55837785Smsmith	}
559121752Sbrooks	ifmedia_set(&sc->media, media);
560121752Sbrooks	cs_mediaset(sc, media);
56137785Smsmith
562147256Sbrooks	ether_ifattach(ifp, sc->enaddr);
563121752Sbrooks
564179560Sjhb  	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
565179560Sjhb	    NULL, csintr, sc, &sc->irq_handle);
566179560Sjhb	if (error) {
567179560Sjhb		ether_ifdetach(ifp);
568179560Sjhb		free(sc->buffer, M_DEVBUF);
569179560Sjhb		if_free(ifp);
570179560Sjhb		mtx_destroy(&sc->lock);
571179560Sjhb		cs_release_resources(dev);
572179560Sjhb		return (error);
573179560Sjhb	}
574179560Sjhb
575347962Sbrooks	gone_by_fcp101_dev(dev);
576347962Sbrooks
57758816Simp	return (0);
57837785Smsmith}
57937785Smsmith
580140888Simpint
581140888Simpcs_detach(device_t dev)
582140888Simp{
583140888Simp	struct cs_softc *sc;
584140888Simp	struct ifnet *ifp;
585140888Simp
586140888Simp	sc = device_get_softc(dev);
587147256Sbrooks	ifp = sc->ifp;
588140888Simp
589179560Sjhb	CS_LOCK(sc);
590140888Simp	cs_stop(sc);
591179560Sjhb	CS_UNLOCK(sc);
592179560Sjhb	callout_drain(&sc->timer);
593140888Simp	ether_ifdetach(ifp);
594179560Sjhb	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
595150306Simp	cs_release_resources(dev);
596179560Sjhb	free(sc->buffer, M_DEVBUF);
597147256Sbrooks	if_free(ifp);
598179560Sjhb	mtx_destroy(&sc->lock);
599140888Simp	return (0);
600140888Simp}
601140888Simp
60237785Smsmith/*
60337785Smsmith * Initialize the board
60437785Smsmith */
60537785Smsmithstatic void
60637785Smsmithcs_init(void *xsc)
60737785Smsmith{
60837785Smsmith	struct cs_softc *sc=(struct cs_softc *)xsc;
609179560Sjhb
610179560Sjhb	CS_LOCK(sc);
611179560Sjhb	cs_init_locked(sc);
612179560Sjhb	CS_UNLOCK(sc);
613179560Sjhb}
614179560Sjhb
615179560Sjhbstatic void
616179560Sjhbcs_init_locked(struct cs_softc *sc)
617179560Sjhb{
618147256Sbrooks	struct ifnet *ifp = sc->ifp;
619179560Sjhb	int i, rx_cfg;
62037785Smsmith
62137785Smsmith	/*
622179560Sjhb	 * reset watchdog timer
62337785Smsmith	 */
624179560Sjhb	sc->tx_timeout = 0;
62537785Smsmith	sc->buf_len = 0;
62637785Smsmith
62737785Smsmith	/*
62837785Smsmith	 * Hardware initialization of cs
62937785Smsmith	 */
63037785Smsmith
63137785Smsmith	/* Enable receiver and transmitter */
63272940Simp	cs_writereg(sc, PP_LineCTL,
63372940Simp		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
63437785Smsmith
63537785Smsmith	/* Configure the receiver mode */
63637785Smsmith	cs_setmode(sc);
63737785Smsmith
63837785Smsmith	/*
63937785Smsmith	 * This defines what type of frames will cause interrupts
64037785Smsmith	 * Bad frames should generate interrupts so that the driver
64137785Smsmith	 * could track statistics of discarded packets
64237785Smsmith	 */
643140927Simp	rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
64437785Smsmith		 RX_EXTRA_DATA_ENBL;
64537785Smsmith	if (sc->isa_config & STREAM_TRANSFER)
64637785Smsmith		rx_cfg |= RX_STREAM_ENBL;
64772940Simp	cs_writereg(sc, PP_RxCFG, rx_cfg);
64872940Simp	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
64937785Smsmith		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
65037785Smsmith		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
65172940Simp	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
65237785Smsmith		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
65337785Smsmith		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
65437785Smsmith
655140927Simp	/* Write MAC address into IA filter */
656140927Simp	for (i=0; i<ETHER_ADDR_LEN/2; i++)
657140927Simp		cs_writereg(sc, PP_IA + i * 2,
658147256Sbrooks		    sc->enaddr[i * 2] |
659147256Sbrooks		    (sc->enaddr[i * 2 + 1] << 8) );
66037785Smsmith
66137785Smsmith	/*
66237785Smsmith	 * Now enable everything
66337785Smsmith	 */
66437785Smsmith/*
66537785Smsmith#ifdef	CS_USE_64K_DMA
66672940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
66772940Simp#else
668140927Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
66937785Smsmith#endif
67037785Smsmith*/
67172940Simp	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
67237785Smsmith
67337785Smsmith	/*
67437785Smsmith	 * Set running and clear output active flags
67537785Smsmith	 */
676148887Srwatson	sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
677148887Srwatson	sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
678179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
67937785Smsmith
68037785Smsmith	/*
68137785Smsmith	 * Start sending process
68237785Smsmith	 */
683179560Sjhb	cs_start_locked(ifp);
68437785Smsmith}
68537785Smsmith
68637785Smsmith/*
687106937Ssam * Get the packet from the board and send it to the upper layer.
68837785Smsmith */
68937785Smsmithstatic int
69037785Smsmithcs_get_packet(struct cs_softc *sc)
69137785Smsmith{
692147256Sbrooks	struct ifnet *ifp = sc->ifp;
693218055Sandrew	int status, length;
69437785Smsmith	struct mbuf *m;
69537785Smsmith
69637785Smsmith#ifdef CS_DEBUG
69737785Smsmith	int i;
69837785Smsmith#endif
69937785Smsmith
70072940Simp	status = cs_inw(sc, RX_FRAME_PORT);
70172940Simp	length = cs_inw(sc, RX_FRAME_PORT);
70237785Smsmith
70337785Smsmith#ifdef CS_DEBUG
704162321Sglebius	device_printf(sc->dev, "rcvd: stat %x, len %d\n",
705104252Sbrooks		status, length);
70637785Smsmith#endif
70737785Smsmith
70837785Smsmith	if (!(status & RX_OK)) {
70937785Smsmith#ifdef CS_DEBUG
710162321Sglebius		device_printf(sc->dev, "bad pkt stat %x\n", status);
71137785Smsmith#endif
712271836Sglebius		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
713140928Simp		return (-1);
71437785Smsmith	}
71537785Smsmith
716243857Sglebius	MGETHDR(m, M_NOWAIT, MT_DATA);
71737785Smsmith	if (m==NULL)
718140928Simp		return (-1);
71937785Smsmith
72037785Smsmith	if (length > MHLEN) {
721276750Srwatson		if (!(MCLGET(m, M_NOWAIT))) {
72237785Smsmith			m_freem(m);
723140928Simp			return (-1);
72437785Smsmith		}
72537785Smsmith	}
72637785Smsmith
72737785Smsmith	/* Initialize packet's header info */
72837785Smsmith	m->m_pkthdr.rcvif = ifp;
72937785Smsmith	m->m_pkthdr.len = length;
73037785Smsmith	m->m_len = length;
73137785Smsmith
73237785Smsmith	/* Get the data */
733218055Sandrew	bus_read_multi_2(sc->port_res, RX_FRAME_PORT, mtod(m, uint16_t *),
734218055Sandrew	    (length + 1) >> 1);
73537785Smsmith
73637785Smsmith#ifdef CS_DEBUG
73737785Smsmith	for (i=0;i<length;i++)
73837785Smsmith	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
73937785Smsmith	printf( "\n" );
74037785Smsmith#endif
74137785Smsmith
74237785Smsmith	if (status & (RX_IA | RX_BROADCAST) ||
74337785Smsmith	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
74437785Smsmith		/* Feed the packet to the upper layer */
745106937Ssam		(*ifp->if_input)(ifp, m);
746271836Sglebius		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
747122024Simp		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
748140927Simp			DELAY(cs_recv_delay);
74937785Smsmith	} else {
75037785Smsmith		m_freem(m);
75137785Smsmith	}
75237785Smsmith
753140928Simp	return (0);
75437785Smsmith}
75537785Smsmith
75637785Smsmith/*
75758816Simp * Handle interrupts
75837785Smsmith */
75958816Simpvoid
76058816Simpcsintr(void *arg)
76137785Smsmith{
76258816Simp	struct cs_softc *sc = (struct cs_softc*) arg;
763147256Sbrooks	struct ifnet *ifp = sc->ifp;
76441591Sarchie	int status;
76537785Smsmith
76637785Smsmith#ifdef CS_DEBUG
767162321Sglebius	device_printf(sc->dev, "Interrupt.\n");
76837785Smsmith#endif
76937785Smsmith
770179560Sjhb	CS_LOCK(sc);
77172940Simp	while ((status=cs_inw(sc, ISQ_PORT))) {
77237785Smsmith
77337785Smsmith#ifdef CS_DEBUG
774162321Sglebius		device_printf(sc->dev, "from ISQ: %04x\n", status);
77537785Smsmith#endif
77637785Smsmith
77737785Smsmith		switch (status & ISQ_EVENT_MASK) {
778140927Simp		case ISQ_RECEIVER_EVENT:
779140927Simp			cs_get_packet(sc);
780140927Simp			break;
78137785Smsmith
782140927Simp		case ISQ_TRANSMITTER_EVENT:
783140927Simp			if (status & TX_OK)
784271836Sglebius				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
785140927Simp			else
786271836Sglebius				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
787148887Srwatson			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
788179560Sjhb			sc->tx_timeout = 0;
789140927Simp			break;
79037785Smsmith
791140927Simp		case ISQ_BUFFER_EVENT:
792140927Simp			if (status & READY_FOR_TX) {
793148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
794179560Sjhb				sc->tx_timeout = 0;
795140927Simp			}
79637785Smsmith
797140927Simp			if (status & TX_UNDERRUN) {
798148887Srwatson				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
799179560Sjhb				sc->tx_timeout = 0;
800271836Sglebius				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
801140927Simp			}
802140927Simp			break;
80337785Smsmith
804140927Simp		case ISQ_RX_MISS_EVENT:
805271836Sglebius			if_inc_counter(ifp, IFCOUNTER_IERRORS, status >> 6);
806140927Simp			break;
80737785Smsmith
808140927Simp		case ISQ_TX_COL_EVENT:
809271836Sglebius			if_inc_counter(ifp, IFCOUNTER_COLLISIONS, status >> 6);
810140927Simp			break;
811140927Simp		}
812140927Simp	}
81337785Smsmith
814148887Srwatson	if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
815179560Sjhb		cs_start_locked(ifp);
816140927Simp	}
817179560Sjhb	CS_UNLOCK(sc);
81837785Smsmith}
81937785Smsmith
82037785Smsmith/*
82137785Smsmith * Save the data in buffer
82237785Smsmith */
82337785Smsmith
82437785Smsmithstatic void
82537785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
82637785Smsmith{
82737785Smsmith	int len;
82837785Smsmith	struct mbuf *mp;
82937785Smsmith	unsigned char *data, *buf;
83037785Smsmith
83137785Smsmith	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
83237785Smsmith		len = mp->m_len;
83337785Smsmith
83437785Smsmith		/*
83537785Smsmith		 * Ignore empty parts
83637785Smsmith		 */
83737785Smsmith		if (!len)
838202580Simp			continue;
83937785Smsmith
84037785Smsmith		/*
84137785Smsmith		 * Find actual data address
84237785Smsmith		 */
84337785Smsmith		data = mtod(mp, caddr_t);
84437785Smsmith
84537785Smsmith		bcopy((caddr_t) data, (caddr_t) buf, len);
84637785Smsmith		buf += len;
84737785Smsmith		sc->buf_len += len;
84837785Smsmith	}
84937785Smsmith}
85037785Smsmith
85137785Smsmith
85237785Smsmithstatic void
85337785Smsmithcs_xmit_buf( struct cs_softc *sc )
85437785Smsmith{
855218055Sandrew	bus_write_multi_2(sc->port_res, TX_FRAME_PORT, (uint16_t *)sc->buffer,
856218055Sandrew	    (sc->buf_len + 1) >> 1);
85737785Smsmith	sc->buf_len = 0;
85837785Smsmith}
85937785Smsmith
86037785Smsmithstatic void
86137785Smsmithcs_start(struct ifnet *ifp)
86237785Smsmith{
863179560Sjhb	struct cs_softc *sc = ifp->if_softc;
864179560Sjhb
865179560Sjhb	CS_LOCK(sc);
866179560Sjhb	cs_start_locked(ifp);
867179560Sjhb	CS_UNLOCK(sc);
868179560Sjhb}
869179560Sjhb
870179560Sjhbstatic void
871179560Sjhbcs_start_locked(struct ifnet *ifp)
872179560Sjhb{
873179560Sjhb	int length;
87437785Smsmith	struct mbuf *m, *mp;
87537785Smsmith	struct cs_softc *sc = ifp->if_softc;
87637785Smsmith
87737785Smsmith	for (;;) {
87837785Smsmith		if (sc->buf_len)
87937785Smsmith			length = sc->buf_len;
88037785Smsmith		else {
88137785Smsmith			IF_DEQUEUE( &ifp->if_snd, m );
88237785Smsmith
88337785Smsmith			if (m==NULL) {
88437785Smsmith				return;
88537785Smsmith			}
88637785Smsmith
88737785Smsmith			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
88837785Smsmith				length += mp->m_len;
88937785Smsmith
89037785Smsmith			/* Skip zero-length packets */
89137785Smsmith			if (length == 0) {
89237785Smsmith				m_freem(m);
89337785Smsmith				continue;
89437785Smsmith			}
89537785Smsmith
89637785Smsmith			cs_write_mbufs(sc, m);
89737785Smsmith
898106937Ssam			BPF_MTAP(ifp, m);
89937785Smsmith
90037785Smsmith			m_freem(m);
90137785Smsmith		}
90237785Smsmith
90337785Smsmith		/*
90437785Smsmith		 * Issue a SEND command
90537785Smsmith		 */
90672940Simp		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
90772940Simp		cs_outw(sc, TX_LEN_PORT, length );
90837785Smsmith
90937785Smsmith		/*
91037785Smsmith		 * If there's no free space in the buffer then leave
91137785Smsmith		 * this packet for the next time: indicate output active
91237785Smsmith		 * and return.
91337785Smsmith		 */
91472940Simp		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
915179560Sjhb			sc->tx_timeout = sc->buf_len;
916148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
91737785Smsmith			return;
91837785Smsmith		}
91937785Smsmith
920140927Simp		cs_xmit_buf(sc);
92137785Smsmith
92237785Smsmith		/*
92337785Smsmith		 * Set the watchdog timer in case we never hear
92437785Smsmith		 * from board again. (I don't know about correct
92537785Smsmith		 * value for this timeout)
92637785Smsmith		 */
927179560Sjhb		sc->tx_timeout = length;
92837785Smsmith
929148887Srwatson		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
93037785Smsmith		return;
93137785Smsmith	}
93237785Smsmith}
93337785Smsmith
93437785Smsmith/*
93537785Smsmith * Stop everything on the interface
93637785Smsmith */
93737785Smsmithstatic void
93837785Smsmithcs_stop(struct cs_softc *sc)
93937785Smsmith{
94037785Smsmith
941179560Sjhb	CS_ASSERT_LOCKED(sc);
94272940Simp	cs_writereg(sc, PP_RxCFG, 0);
94372940Simp	cs_writereg(sc, PP_TxCFG, 0);
94472940Simp	cs_writereg(sc, PP_BufCFG, 0);
94572940Simp	cs_writereg(sc, PP_BusCTL, 0);
94637785Smsmith
947148887Srwatson	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
948179560Sjhb	sc->tx_timeout = 0;
949179560Sjhb	callout_stop(&sc->timer);
95037785Smsmith}
95137785Smsmith
95237785Smsmith/*
95337785Smsmith * Reset the interface
95437785Smsmith */
95537785Smsmithstatic void
95637785Smsmithcs_reset(struct cs_softc *sc)
95737785Smsmith{
958179560Sjhb
959179560Sjhb	CS_ASSERT_LOCKED(sc);
96037785Smsmith	cs_stop(sc);
961179560Sjhb	cs_init_locked(sc);
96237785Smsmith}
96337785Smsmith
964156559Sphilipstatic uint16_t
965156559Sphilipcs_hash_index(struct sockaddr_dl *addr)
966156559Sphilip{
967156559Sphilip	uint32_t crc;
968156559Sphilip	uint16_t idx;
969156559Sphilip	caddr_t lla;
970156559Sphilip
971156559Sphilip	lla = LLADDR(addr);
972156559Sphilip	crc = ether_crc32_le(lla, ETHER_ADDR_LEN);
973156559Sphilip	idx = crc >> 26;
974156559Sphilip
975156559Sphilip	return (idx);
976156559Sphilip}
977156559Sphilip
97837785Smsmithstatic void
97937785Smsmithcs_setmode(struct cs_softc *sc)
98037785Smsmith{
981156559Sphilip	int rx_ctl;
982156559Sphilip	uint16_t af[4];
983156559Sphilip	uint16_t port, mask, index;
984147256Sbrooks	struct ifnet *ifp = sc->ifp;
985156559Sphilip	struct ifmultiaddr *ifma;
98637785Smsmith
98737785Smsmith	/* Stop the receiver while changing filters */
98872940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
98937785Smsmith
99037785Smsmith	if (ifp->if_flags & IFF_PROMISC) {
99137785Smsmith		/* Turn on promiscuous mode. */
99237785Smsmith		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
993156559Sphilip	} else if (ifp->if_flags & IFF_MULTICAST) {
994156559Sphilip		/* Allow receiving frames with multicast addresses */
995156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
996156559Sphilip			 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
997156559Sphilip
998156559Sphilip		/* Start with an empty filter */
999156559Sphilip		af[0] = af[1] = af[2] = af[3] = 0x0000;
1000156559Sphilip
1001156559Sphilip		if (ifp->if_flags & IFF_ALLMULTI) {
1002156559Sphilip			/* Accept all multicast frames */
1003156559Sphilip			af[0] = af[1] = af[2] = af[3] = 0xffff;
100437785Smsmith		} else {
1005156559Sphilip			/*
1006156559Sphilip			 * Set up the filter to only accept multicast
1007156559Sphilip			 * frames we're interested in.
100837785Smsmith			 */
1009195049Srwatson			if_maddr_rlock(ifp);
1010156559Sphilip			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1011156559Sphilip				struct sockaddr_dl *dl =
1012156559Sphilip				    (struct sockaddr_dl *)ifma->ifma_addr;
1013156559Sphilip
1014156559Sphilip				index = cs_hash_index(dl);
1015156559Sphilip				port = (u_int16_t) (index >> 4);
1016156559Sphilip				mask = (u_int16_t) (1 << (index & 0xf));
1017156559Sphilip				af[port] |= mask;
1018156559Sphilip			}
1019195049Srwatson			if_maddr_runlock(ifp);
102037785Smsmith		}
1021156559Sphilip
1022156559Sphilip		cs_writereg(sc, PP_LAF + 0, af[0]);
1023156559Sphilip		cs_writereg(sc, PP_LAF + 2, af[1]);
1024156559Sphilip		cs_writereg(sc, PP_LAF + 4, af[2]);
1025156559Sphilip		cs_writereg(sc, PP_LAF + 6, af[3]);
1026156559Sphilip	} else {
1027156559Sphilip		/*
1028156559Sphilip		 * Receive only good frames addressed for us and
1029156559Sphilip		 * good broadcasts.
1030156559Sphilip		 */
1031156559Sphilip		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1032156559Sphilip			 RX_OK_ACCEPT;
103337785Smsmith	}
103437785Smsmith
103537785Smsmith	/* Set up the filter */
103672940Simp	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
103737785Smsmith
103837785Smsmith	/* Turn on receiver */
103972940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
104037785Smsmith}
104137785Smsmith
104237785Smsmithstatic int
1043331643Sdimcs_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
104437785Smsmith{
104537785Smsmith	struct cs_softc *sc=ifp->if_softc;
104637785Smsmith	struct ifreq *ifr = (struct ifreq *)data;
1047179560Sjhb	int error=0;
104837785Smsmith
104937785Smsmith#ifdef CS_DEBUG
1050162321Sglebius	if_printf(ifp, "%s command=%lx\n", __func__, command);
105137785Smsmith#endif
105237785Smsmith
105337785Smsmith	switch (command) {
105437785Smsmith	case SIOCSIFFLAGS:
105537785Smsmith		/*
105637785Smsmith		 * Switch interface state between "running" and
105737785Smsmith		 * "stopped", reflecting the UP flag.
1058140927Simp		 */
1059179560Sjhb		CS_LOCK(sc);
1060147256Sbrooks		if (sc->ifp->if_flags & IFF_UP) {
1061148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) {
1062179560Sjhb				cs_init_locked(sc);
1063140927Simp			}
1064140927Simp		} else {
1065148887Srwatson			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) {
1066140927Simp				cs_stop(sc);
1067140927Simp			}
1068179560Sjhb		}
106937785Smsmith		/*
107037785Smsmith		 * Promiscuous and/or multicast flags may have changed,
107137785Smsmith		 * so reprogram the multicast filter and/or receive mode.
107237785Smsmith		 *
107337785Smsmith		 * See note about multicasts in cs_setmode
107437785Smsmith		 */
107537785Smsmith		cs_setmode(sc);
1076179560Sjhb		CS_UNLOCK(sc);
107737785Smsmith		break;
107837785Smsmith
107937785Smsmith	case SIOCADDMULTI:
108037785Smsmith	case SIOCDELMULTI:
108137785Smsmith	    /*
108237785Smsmith	     * Multicast list has changed; set the hardware filter
108337785Smsmith	     * accordingly.
108437785Smsmith	     *
108537785Smsmith	     * See note about multicasts in cs_setmode
108637785Smsmith	     */
1087179560Sjhb	    CS_LOCK(sc);
108837785Smsmith	    cs_setmode(sc);
1089179560Sjhb	    CS_UNLOCK(sc);
109037785Smsmith	    error = 0;
109137785Smsmith	    break;
109237785Smsmith
1093140927Simp	case SIOCSIFMEDIA:
1094140927Simp	case SIOCGIFMEDIA:
1095140927Simp		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1096140927Simp		break;
109737785Smsmith
1098140927Simp	default:
1099140921Simp		error = ether_ioctl(ifp, command, data);
1100106937Ssam		break;
1101140927Simp	}
110237785Smsmith
1103140928Simp	return (error);
110437785Smsmith}
110537785Smsmith
110637785Smsmith/*
110737785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to
110837785Smsmith * generate an interrupt after a transmit has been started on it.
110937785Smsmith */
111037785Smsmithstatic void
1111179560Sjhbcs_watchdog(void *arg)
111237785Smsmith{
1113179560Sjhb	struct cs_softc *sc = arg;
1114179560Sjhb	struct ifnet *ifp = sc->ifp;
111537785Smsmith
1116179560Sjhb	CS_ASSERT_LOCKED(sc);
1117179560Sjhb	if (sc->tx_timeout && --sc->tx_timeout == 0) {
1118271836Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1119179560Sjhb		log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
112037785Smsmith
1121179560Sjhb		/* Reset the interface */
1122179560Sjhb		if (ifp->if_flags & IFF_UP)
1123179560Sjhb			cs_reset(sc);
1124179560Sjhb		else
1125179560Sjhb			cs_stop(sc);
1126179560Sjhb	}
1127179560Sjhb	callout_reset(&sc->timer, hz, cs_watchdog, sc);
112837785Smsmith}
112937785Smsmith
113037785Smsmithstatic int
113137785Smsmithcs_mediachange(struct ifnet *ifp)
113237785Smsmith{
113337785Smsmith	struct cs_softc *sc = ifp->if_softc;
113437785Smsmith	struct ifmedia *ifm = &sc->media;
1135179560Sjhb	int error;
113637785Smsmith
113737785Smsmith	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1138140928Simp		return (EINVAL);
113937785Smsmith
1140179560Sjhb	CS_LOCK(sc);
1141179560Sjhb	error = cs_mediaset(sc, ifm->ifm_media);
1142179560Sjhb	CS_UNLOCK(sc);
1143179560Sjhb	return (error);
114437785Smsmith}
114537785Smsmith
114637785Smsmithstatic void
114737785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
114837785Smsmith{
114937785Smsmith	int line_status;
115037785Smsmith	struct cs_softc *sc = ifp->if_softc;
115137785Smsmith
1152179560Sjhb	CS_LOCK(sc);
115337785Smsmith	ifmr->ifm_active = IFM_ETHER;
115472940Simp	line_status = cs_readreg(sc, PP_LineST);
115537785Smsmith	if (line_status & TENBASET_ON) {
115637785Smsmith		ifmr->ifm_active |= IFM_10_T;
115737785Smsmith		if (sc->chip_type != CS8900) {
115872940Simp			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
115937785Smsmith				ifmr->ifm_active |= IFM_FDX;
116072940Simp			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
116137785Smsmith				ifmr->ifm_active |= IFM_HDX;
116237785Smsmith		}
116337785Smsmith		ifmr->ifm_status = IFM_AVALID;
116437785Smsmith		if (line_status & LINK_OK)
116537785Smsmith			ifmr->ifm_status |= IFM_ACTIVE;
116637785Smsmith	} else {
116738305Smsmith		if (line_status & AUI_ON) {
116872940Simp			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
116972940Simp			    HCB1_ENBL);
117038305Smsmith			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
117172940Simp			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
117238305Smsmith				ifmr->ifm_active |= IFM_10_2;
117338305Smsmith			else
117438305Smsmith				ifmr->ifm_active |= IFM_10_5;
117538305Smsmith		}
117637785Smsmith	}
1177179560Sjhb	CS_UNLOCK(sc);
117837785Smsmith}
117937785Smsmith
118037785Smsmithstatic int
118137785Smsmithcs_mediaset(struct cs_softc *sc, int media)
118237785Smsmith{
1183179532Simp	int error = 0;
118437785Smsmith
118537785Smsmith	/* Stop the receiver & transmitter */
118672940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
118772940Simp	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
118837785Smsmith
118937785Smsmith#ifdef CS_DEBUG
1190162321Sglebius	device_printf(sc->dev, "%s media=%x\n", __func__, media);
119137785Smsmith#endif
119237785Smsmith
119337785Smsmith	switch (IFM_SUBTYPE(media)) {
119437785Smsmith	default:
119537785Smsmith	case IFM_AUTO:
1196179553Simp		/*
1197179553Simp		 * This chip makes it a little hard to support this, so treat
1198179553Simp		 * it as IFM_10_T, auto duplex.
1199179553Simp		 */
1200179553Simp		enable_tp(sc);
1201179553Simp		cs_duplex_auto(sc);
120237785Smsmith		break;
120337785Smsmith	case IFM_10_T:
1204179532Simp		enable_tp(sc);
120537785Smsmith		if (media & IFM_FDX)
120637785Smsmith			cs_duplex_full(sc);
120737785Smsmith		else if (media & IFM_HDX)
120837785Smsmith			cs_duplex_half(sc);
120937785Smsmith		else
121037785Smsmith			error = cs_duplex_auto(sc);
121137785Smsmith		break;
121237785Smsmith	case IFM_10_2:
1213180388Simp		enable_bnc(sc);
121437785Smsmith		break;
121537785Smsmith	case IFM_10_5:
1216180388Simp		enable_aui(sc);
121737785Smsmith		break;
121837785Smsmith	}
121937785Smsmith
122037785Smsmith	/*
122137785Smsmith	 * Turn the transmitter & receiver back on
122237785Smsmith	 */
122372940Simp	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
122472940Simp	    SERIAL_RX_ON | SERIAL_TX_ON);
122537785Smsmith
1226140928Simp	return (error);
122737785Smsmith}
1228