1/*-
2 * Copyright (c) 1997,1998 Maxim Bolotin and Oleg Sharoiko.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32/*
33 *
34 * Device driver for Crystal Semiconductor CS8920 based ethernet
35 *   adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
36 */
37
38/*
39#define	 CS_DEBUG
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/sockio.h>
48#include <sys/kernel.h>
49#include <sys/sysctl.h>
50#include <sys/syslog.h>
51
52#include <sys/module.h>
53#include <sys/bus.h>
54#include <machine/bus.h>
55#include <sys/rman.h>
56#include <machine/resource.h>
57
58#include <net/if.h>
59#include <net/if_arp.h>
60#include <net/if_dl.h>
61#include <net/if_media.h>
62#include <net/if_types.h>
63#include <net/ethernet.h>
64#include <net/bpf.h>
65
66#include <dev/cs/if_csvar.h>
67#include <dev/cs/if_csreg.h>
68
69#ifdef  CS_USE_64K_DMA
70#define CS_DMA_BUFFER_SIZE 65536
71#else
72#define CS_DMA_BUFFER_SIZE 16384
73#endif
74
75static void	cs_init(void *);
76static void	cs_init_locked(struct cs_softc *);
77static int	cs_ioctl(struct ifnet *, u_long, caddr_t);
78static void	cs_start(struct ifnet *);
79static void	cs_start_locked(struct ifnet *);
80static void	cs_stop(struct cs_softc *);
81static void	cs_reset(struct cs_softc *);
82static void	cs_watchdog(void *);
83
84static int	cs_mediachange(struct ifnet *);
85static void	cs_mediastatus(struct ifnet *, struct ifmediareq *);
86static int      cs_mediaset(struct cs_softc *, int);
87
88static void	cs_write_mbufs(struct cs_softc*, struct mbuf*);
89static void	cs_xmit_buf(struct cs_softc*);
90static int	cs_get_packet(struct cs_softc*);
91static void	cs_setmode(struct cs_softc*);
92
93static int	get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *);
94static int	get_eeprom_cksum(int, int, uint16_t *);
95static int	wait_eeprom_ready( struct cs_softc *);
96static void	control_dc_dc( struct cs_softc *, int );
97static int	enable_tp(struct cs_softc *);
98static int	enable_aui(struct cs_softc *);
99static int	enable_bnc(struct cs_softc *);
100static int      cs_duplex_auto(struct cs_softc *);
101
102devclass_t cs_devclass;
103driver_intr_t	csintr;
104
105/* sysctl vars */
106static SYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
107
108int	cs_ignore_cksum_failure = 0;
109TUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure);
110SYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW,
111    &cs_ignore_cksum_failure, 0,
112  "ignore checksum errors in cs card EEPROM");
113
114static int	cs_recv_delay = 570;
115TUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay);
116SYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, "");
117
118static int cs8900_eeint2irq[16] = {
119	 10,  11,  12,   5, 255, 255, 255, 255,
120	255, 255, 255, 255, 255, 255, 255, 255
121};
122
123static int cs8900_irq2eeint[16] = {
124	255, 255, 255, 255, 255,   3, 255, 255,
125	255,   0,   1,   2, 255, 255, 255, 255
126};
127
128static int
129get_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
130{
131	int i;
132
133#ifdef CS_DEBUG
134	device_printf(sc->dev, "EEPROM data from %x for %x:\n", off, len);
135#endif
136	for (i=0; i < len; i++) {
137		if (wait_eeprom_ready(sc) < 0)
138			return (-1);
139		/* Send command to EEPROM to read */
140		cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
141		if (wait_eeprom_ready(sc) < 0)
142			return (-1);
143		buffer[i] = cs_readreg(sc, PP_EEData);
144
145#ifdef CS_DEBUG
146		printf("%04x ",buffer[i]);
147#endif
148	}
149
150#ifdef CS_DEBUG
151	printf("\n");
152#endif
153	return (0);
154}
155
156static int
157get_eeprom_cksum(int off, int len, uint16_t *buffer)
158{
159	int i;
160	uint16_t cksum=0;
161
162	for (i = 0; i < len; i++)
163		cksum += buffer[i];
164	cksum &= 0xffff;
165	if (cksum == 0 || cs_ignore_cksum_failure)
166		return (0);
167	return (-1);
168}
169
170static int
171wait_eeprom_ready(struct cs_softc *sc)
172{
173	int i;
174
175	/*
176	 * From the CS8900A datasheet, section 3.5.2:
177	 * "Before issuing any command to the EEPROM, the host must wait
178	 * for the SIBUSY bit (Register 16, SelfST, bit 8) to clear.  After
179	 * each command has been issued, the host must wait again for SIBUSY
180	 * to clear."
181	 *
182	 * Before we issue the command, we should be !busy, so that will
183	 * be fast.  The datasheet suggests that clock out from the part
184	 * per word will be on the order of 25us, which is consistant with
185	 * the 1MHz serial clock and 16bits...  We should never hit 100,
186	 * let alone 15,000 here.  The original code did an unconditional
187	 * 30ms DELAY here.  Bad Kharma.  cs_readreg takes ~2us.
188	 */
189	for (i = 0; i < 15000; i++)	/* 30ms max */
190		if (!(cs_readreg(sc, PP_SelfST) & SI_BUSY))
191			return (0);
192	return (1);
193}
194
195static void
196control_dc_dc(struct cs_softc *sc, int on_not_off)
197{
198	unsigned int self_control = HCB1_ENBL;
199
200	if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
201		self_control |= HCB1;
202	else
203		self_control &= ~HCB1;
204	cs_writereg(sc, PP_SelfCTL, self_control);
205	DELAY(500000);	/* Bad! */
206}
207
208
209static int
210cs_duplex_auto(struct cs_softc *sc)
211{
212	int i, error=0;
213
214	cs_writereg(sc, PP_AutoNegCTL,
215	    RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
216	for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
217		if (i > 4000) {
218			device_printf(sc->dev,
219			    "full/half duplex auto negotiation timeout\n");
220			error = ETIMEDOUT;
221			break;
222		}
223		DELAY(1000);
224	}
225	return (error);
226}
227
228static int
229enable_tp(struct cs_softc *sc)
230{
231
232	cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
233	control_dc_dc(sc, 0);
234	return (0);
235}
236
237static int
238enable_aui(struct cs_softc *sc)
239{
240
241	cs_writereg(sc, PP_LineCTL,
242	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
243	control_dc_dc(sc, 0);
244	return (0);
245}
246
247static int
248enable_bnc(struct cs_softc *sc)
249{
250
251	cs_writereg(sc, PP_LineCTL,
252	    (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
253	control_dc_dc(sc, 1);
254	return (0);
255}
256
257int
258cs_cs89x0_probe(device_t dev)
259{
260	int i;
261	int error;
262	u_long irq, junk;
263	struct cs_softc *sc = device_get_softc(dev);
264	unsigned rev_type = 0;
265	uint16_t id;
266	char chip_revision;
267	uint16_t eeprom_buff[CHKSUM_LEN];
268	int chip_type, pp_isaint, pp_isadma;
269
270	sc->dev = dev;
271	error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
272	if (error)
273		return (error);
274
275	if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
276		/* Chip not detected. Let's try to reset it */
277		if (bootverbose)
278			device_printf(dev, "trying to reset the chip.\n");
279		cs_outw(sc, ADD_PORT, PP_SelfCTL);
280		i = cs_inw(sc, DATA_PORT);
281		cs_outw(sc, ADD_PORT, PP_SelfCTL);
282		cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
283		if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
284			return (ENXIO);
285	}
286
287	for (i = 0; i < 10000; i++) {
288		id = cs_readreg(sc, PP_ChipID);
289		if (id == CHIP_EISA_ID_SIG)
290			break;
291	}
292	if (i == 10000)
293		return (ENXIO);
294
295	rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
296	chip_type = rev_type & ~REVISON_BITS;
297	chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
298
299	sc->chip_type = chip_type;
300
301	if (chip_type == CS8900) {
302		pp_isaint = PP_CS8900_ISAINT;
303		pp_isadma = PP_CS8900_ISADMA;
304		sc->send_cmd = TX_CS8900_AFTER_ALL;
305	} else {
306		pp_isaint = PP_CS8920_ISAINT;
307		pp_isadma = PP_CS8920_ISADMA;
308		sc->send_cmd = TX_CS8920_AFTER_ALL;
309	}
310
311	/*
312	 * Clear some fields so that fail of EEPROM will left them clean
313	 */
314	sc->auto_neg_cnf = 0;
315	sc->adapter_cnf  = 0;
316	sc->isa_config   = 0;
317
318	/*
319	 * If no interrupt specified, use what the board tells us.
320	 */
321	error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
322
323	/*
324	 * Get data from EEPROM
325	 */
326	if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
327		device_printf(dev, "No EEPROM, assuming defaults.\n");
328	} else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
329		device_printf(dev, "EEPROM read failed, assuming defaults.\n");
330	} else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
331		device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n");
332	} else {
333		sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET];
334		sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET];
335		sc->isa_config = eeprom_buff[ISA_CNF_OFFSET];
336		for (i=0; i<ETHER_ADDR_LEN/2; i++) {
337			sc->enaddr[i*2] = eeprom_buff[i];
338			sc->enaddr[i*2+1] = eeprom_buff[i] >> 8;
339		}
340		/*
341		 * If no interrupt specified, use what the
342		 * board tells us.
343		 */
344		if (error) {
345			irq = sc->isa_config & INT_NO_MASK;
346			error = 0;
347			if (chip_type == CS8900) {
348				irq = cs8900_eeint2irq[irq];
349			} else {
350				if (irq > CS8920_NO_INTS)
351					irq = 255;
352			}
353			if (irq == 255) {
354				device_printf(dev, "invalid irq in EEPROM.\n");
355				error = EINVAL;
356			}
357			if (!error)
358				bus_set_resource(dev, SYS_RES_IRQ, 0,
359				    irq, 1);
360		}
361	}
362
363	if (!error && !(sc->flags & CS_NO_IRQ)) {
364		if (chip_type == CS8900) {
365			if (irq < 16)
366				irq = cs8900_irq2eeint[irq];
367			else
368				irq = 255;
369		} else {
370			if (irq > CS8920_NO_INTS)
371				irq = 255;
372		}
373		if (irq == 255)
374			error = EINVAL;
375	}
376
377	if (error) {
378	       	device_printf(dev, "Unknown or invalid irq\n");
379		return (error);
380	}
381
382	if (!(sc->flags & CS_NO_IRQ))
383		cs_writereg(sc, pp_isaint, irq);
384
385	/*
386	 * Temporary disabled
387	 *
388	if (drq>0)
389		cs_writereg(sc, pp_isadma, drq);
390	else {
391		device_printf(dev, "incorrect drq\n",);
392		return (0);
393	}
394	*/
395
396	if (bootverbose)
397		 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
398			chip_type == CS8900 ? '0' : '2',
399			chip_type == CS8920M ? "M" : "",
400			chip_revision,
401			(sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
402			(sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
403			(sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
404
405	if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
406	    (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
407		sc->line_ctl = LOW_RX_SQUELCH;
408	else
409		sc->line_ctl = 0;
410
411	return (0);
412}
413
414/*
415 * Allocate a port resource with the given resource id.
416 */
417int
418cs_alloc_port(device_t dev, int rid, int size)
419{
420	struct cs_softc *sc = device_get_softc(dev);
421	struct resource *res;
422
423	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
424	    0ul, ~0ul, size, RF_ACTIVE);
425	if (res == NULL)
426		return (ENOENT);
427	sc->port_rid = rid;
428	sc->port_res = res;
429	return (0);
430}
431
432/*
433 * Allocate an irq resource with the given resource id.
434 */
435int
436cs_alloc_irq(device_t dev, int rid)
437{
438	struct cs_softc *sc = device_get_softc(dev);
439	struct resource *res;
440
441	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
442	if (res == NULL)
443		return (ENOENT);
444	sc->irq_rid = rid;
445	sc->irq_res = res;
446	return (0);
447}
448
449/*
450 * Release all resources
451 */
452void
453cs_release_resources(device_t dev)
454{
455	struct cs_softc *sc = device_get_softc(dev);
456
457	if (sc->port_res) {
458		bus_release_resource(dev, SYS_RES_IOPORT,
459		    sc->port_rid, sc->port_res);
460		sc->port_res = 0;
461	}
462	if (sc->irq_res) {
463		bus_release_resource(dev, SYS_RES_IRQ,
464		    sc->irq_rid, sc->irq_res);
465		sc->irq_res = 0;
466	}
467}
468
469/*
470 * Install the interface into kernel networking data structures
471 */
472int
473cs_attach(device_t dev)
474{
475	int error, media=0;
476	struct cs_softc *sc = device_get_softc(dev);
477	struct ifnet *ifp;
478
479	sc->dev = dev;
480
481	ifp = sc->ifp = if_alloc(IFT_ETHER);
482	if (ifp == NULL) {
483		device_printf(dev, "can not if_alloc()\n");
484		cs_release_resources(dev);
485		return (ENOMEM);
486	}
487
488	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
489	    MTX_DEF);
490	callout_init_mtx(&sc->timer, &sc->lock, 0);
491
492	CS_LOCK(sc);
493	cs_stop(sc);
494	CS_UNLOCK(sc);
495
496	ifp->if_softc=sc;
497	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
498	ifp->if_start=cs_start;
499	ifp->if_ioctl=cs_ioctl;
500	ifp->if_init=cs_init;
501	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
502
503	ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
504
505	/*
506	 * this code still in progress (DMA support)
507	 *
508
509	sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
510	if (sc->recv_ring == NULL) {
511		log(LOG_ERR,
512		"%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
513		return(0);
514	}
515	if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
516	    < (128*1024-CS_DMA_BUFFER_SIZE))
517	    sc->recv_ring+=16*1024;
518
519	*/
520
521	sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
522	if (sc->buffer == NULL) {
523		device_printf(sc->dev, "Couldn't allocate memory for NIC\n");
524		if_free(ifp);
525		mtx_destroy(&sc->lock);
526		cs_release_resources(dev);
527		return(ENOMEM);
528	}
529
530	/*
531	 * Initialize the media structures.
532	 */
533	ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
534
535	if (sc->adapter_cnf & A_CNF_10B_T) {
536		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
537		if (sc->chip_type != CS8900) {
538			ifmedia_add(&sc->media,
539				IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
540			ifmedia_add(&sc->media,
541				IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
542		}
543	}
544
545	if (sc->adapter_cnf & A_CNF_10B_2)
546		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
547
548	if (sc->adapter_cnf & A_CNF_AUI)
549		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
550
551	if (sc->adapter_cnf & A_CNF_MEDIA)
552		ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
553
554	/* Set default media from EEPROM */
555	switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
556	case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
557	case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
558	case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
559	case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
560	default:
561		device_printf(sc->dev, "no media, assuming 10baseT\n");
562		sc->adapter_cnf |= A_CNF_10B_T;
563		ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
564		if (sc->chip_type != CS8900) {
565			ifmedia_add(&sc->media,
566			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
567			ifmedia_add(&sc->media,
568			    IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
569		}
570		media = IFM_ETHER | IFM_10_T;
571		break;
572	}
573	ifmedia_set(&sc->media, media);
574	cs_mediaset(sc, media);
575
576	ether_ifattach(ifp, sc->enaddr);
577
578  	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
579	    NULL, csintr, sc, &sc->irq_handle);
580	if (error) {
581		ether_ifdetach(ifp);
582		free(sc->buffer, M_DEVBUF);
583		if_free(ifp);
584		mtx_destroy(&sc->lock);
585		cs_release_resources(dev);
586		return (error);
587	}
588
589	return (0);
590}
591
592int
593cs_detach(device_t dev)
594{
595	struct cs_softc *sc;
596	struct ifnet *ifp;
597
598	sc = device_get_softc(dev);
599	ifp = sc->ifp;
600
601	CS_LOCK(sc);
602	cs_stop(sc);
603	CS_UNLOCK(sc);
604	callout_drain(&sc->timer);
605	ether_ifdetach(ifp);
606	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
607	cs_release_resources(dev);
608	free(sc->buffer, M_DEVBUF);
609	if_free(ifp);
610	mtx_destroy(&sc->lock);
611	return (0);
612}
613
614/*
615 * Initialize the board
616 */
617static void
618cs_init(void *xsc)
619{
620	struct cs_softc *sc=(struct cs_softc *)xsc;
621
622	CS_LOCK(sc);
623	cs_init_locked(sc);
624	CS_UNLOCK(sc);
625}
626
627static void
628cs_init_locked(struct cs_softc *sc)
629{
630	struct ifnet *ifp = sc->ifp;
631	int i, rx_cfg;
632
633	/*
634	 * reset watchdog timer
635	 */
636	sc->tx_timeout = 0;
637	sc->buf_len = 0;
638
639	/*
640	 * Hardware initialization of cs
641	 */
642
643	/* Enable receiver and transmitter */
644	cs_writereg(sc, PP_LineCTL,
645		cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
646
647	/* Configure the receiver mode */
648	cs_setmode(sc);
649
650	/*
651	 * This defines what type of frames will cause interrupts
652	 * Bad frames should generate interrupts so that the driver
653	 * could track statistics of discarded packets
654	 */
655	rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
656		 RX_EXTRA_DATA_ENBL;
657	if (sc->isa_config & STREAM_TRANSFER)
658		rx_cfg |= RX_STREAM_ENBL;
659	cs_writereg(sc, PP_RxCFG, rx_cfg);
660	cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
661		    TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
662		    TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
663	cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
664		    RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
665		    TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
666
667	/* Write MAC address into IA filter */
668	for (i=0; i<ETHER_ADDR_LEN/2; i++)
669		cs_writereg(sc, PP_IA + i * 2,
670		    sc->enaddr[i * 2] |
671		    (sc->enaddr[i * 2 + 1] << 8) );
672
673	/*
674	 * Now enable everything
675	 */
676/*
677#ifdef	CS_USE_64K_DMA
678	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
679#else
680	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
681#endif
682*/
683	cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
684
685	/*
686	 * Set running and clear output active flags
687	 */
688	sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
689	sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
690	callout_reset(&sc->timer, hz, cs_watchdog, sc);
691
692	/*
693	 * Start sending process
694	 */
695	cs_start_locked(ifp);
696}
697
698/*
699 * Get the packet from the board and send it to the upper layer.
700 */
701static int
702cs_get_packet(struct cs_softc *sc)
703{
704	struct ifnet *ifp = sc->ifp;
705	int status, length;
706	struct ether_header *eh;
707	struct mbuf *m;
708
709#ifdef CS_DEBUG
710	int i;
711#endif
712
713	status = cs_inw(sc, RX_FRAME_PORT);
714	length = cs_inw(sc, RX_FRAME_PORT);
715
716#ifdef CS_DEBUG
717	device_printf(sc->dev, "rcvd: stat %x, len %d\n",
718		status, length);
719#endif
720
721	if (!(status & RX_OK)) {
722#ifdef CS_DEBUG
723		device_printf(sc->dev, "bad pkt stat %x\n", status);
724#endif
725		ifp->if_ierrors++;
726		return (-1);
727	}
728
729	MGETHDR(m, M_NOWAIT, MT_DATA);
730	if (m==NULL)
731		return (-1);
732
733	if (length > MHLEN) {
734		MCLGET(m, M_NOWAIT);
735		if (!(m->m_flags & M_EXT)) {
736			m_freem(m);
737			return (-1);
738		}
739	}
740
741	/* Initialize packet's header info */
742	m->m_pkthdr.rcvif = ifp;
743	m->m_pkthdr.len = length;
744	m->m_len = length;
745
746	/* Get the data */
747	bus_read_multi_2(sc->port_res, RX_FRAME_PORT, mtod(m, uint16_t *),
748	    (length + 1) >> 1);
749
750	eh = mtod(m, struct ether_header *);
751
752#ifdef CS_DEBUG
753	for (i=0;i<length;i++)
754	     printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
755	printf( "\n" );
756#endif
757
758	if (status & (RX_IA | RX_BROADCAST) ||
759	    (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
760		/* Feed the packet to the upper layer */
761		(*ifp->if_input)(ifp, m);
762		ifp->if_ipackets++;
763		if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
764			DELAY(cs_recv_delay);
765	} else {
766		m_freem(m);
767	}
768
769	return (0);
770}
771
772/*
773 * Handle interrupts
774 */
775void
776csintr(void *arg)
777{
778	struct cs_softc *sc = (struct cs_softc*) arg;
779	struct ifnet *ifp = sc->ifp;
780	int status;
781
782#ifdef CS_DEBUG
783	device_printf(sc->dev, "Interrupt.\n");
784#endif
785
786	CS_LOCK(sc);
787	while ((status=cs_inw(sc, ISQ_PORT))) {
788
789#ifdef CS_DEBUG
790		device_printf(sc->dev, "from ISQ: %04x\n", status);
791#endif
792
793		switch (status & ISQ_EVENT_MASK) {
794		case ISQ_RECEIVER_EVENT:
795			cs_get_packet(sc);
796			break;
797
798		case ISQ_TRANSMITTER_EVENT:
799			if (status & TX_OK)
800				ifp->if_opackets++;
801			else
802				ifp->if_oerrors++;
803			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
804			sc->tx_timeout = 0;
805			break;
806
807		case ISQ_BUFFER_EVENT:
808			if (status & READY_FOR_TX) {
809				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
810				sc->tx_timeout = 0;
811			}
812
813			if (status & TX_UNDERRUN) {
814				ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
815				sc->tx_timeout = 0;
816				ifp->if_oerrors++;
817			}
818			break;
819
820		case ISQ_RX_MISS_EVENT:
821			ifp->if_ierrors+=(status>>6);
822			break;
823
824		case ISQ_TX_COL_EVENT:
825			ifp->if_collisions+=(status>>6);
826			break;
827		}
828	}
829
830	if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
831		cs_start_locked(ifp);
832	}
833	CS_UNLOCK(sc);
834}
835
836/*
837 * Save the data in buffer
838 */
839
840static void
841cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
842{
843	int len;
844	struct mbuf *mp;
845	unsigned char *data, *buf;
846
847	for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
848		len = mp->m_len;
849
850		/*
851		 * Ignore empty parts
852		 */
853		if (!len)
854			continue;
855
856		/*
857		 * Find actual data address
858		 */
859		data = mtod(mp, caddr_t);
860
861		bcopy((caddr_t) data, (caddr_t) buf, len);
862		buf += len;
863		sc->buf_len += len;
864	}
865}
866
867
868static void
869cs_xmit_buf( struct cs_softc *sc )
870{
871	bus_write_multi_2(sc->port_res, TX_FRAME_PORT, (uint16_t *)sc->buffer,
872	    (sc->buf_len + 1) >> 1);
873	sc->buf_len = 0;
874}
875
876static void
877cs_start(struct ifnet *ifp)
878{
879	struct cs_softc *sc = ifp->if_softc;
880
881	CS_LOCK(sc);
882	cs_start_locked(ifp);
883	CS_UNLOCK(sc);
884}
885
886static void
887cs_start_locked(struct ifnet *ifp)
888{
889	int length;
890	struct mbuf *m, *mp;
891	struct cs_softc *sc = ifp->if_softc;
892
893	for (;;) {
894		if (sc->buf_len)
895			length = sc->buf_len;
896		else {
897			IF_DEQUEUE( &ifp->if_snd, m );
898
899			if (m==NULL) {
900				return;
901			}
902
903			for (length=0, mp=m; mp != NULL; mp=mp->m_next)
904				length += mp->m_len;
905
906			/* Skip zero-length packets */
907			if (length == 0) {
908				m_freem(m);
909				continue;
910			}
911
912			cs_write_mbufs(sc, m);
913
914			BPF_MTAP(ifp, m);
915
916			m_freem(m);
917		}
918
919		/*
920		 * Issue a SEND command
921		 */
922		cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
923		cs_outw(sc, TX_LEN_PORT, length );
924
925		/*
926		 * If there's no free space in the buffer then leave
927		 * this packet for the next time: indicate output active
928		 * and return.
929		 */
930		if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
931			sc->tx_timeout = sc->buf_len;
932			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
933			return;
934		}
935
936		cs_xmit_buf(sc);
937
938		/*
939		 * Set the watchdog timer in case we never hear
940		 * from board again. (I don't know about correct
941		 * value for this timeout)
942		 */
943		sc->tx_timeout = length;
944
945		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
946		return;
947	}
948}
949
950/*
951 * Stop everything on the interface
952 */
953static void
954cs_stop(struct cs_softc *sc)
955{
956
957	CS_ASSERT_LOCKED(sc);
958	cs_writereg(sc, PP_RxCFG, 0);
959	cs_writereg(sc, PP_TxCFG, 0);
960	cs_writereg(sc, PP_BufCFG, 0);
961	cs_writereg(sc, PP_BusCTL, 0);
962
963	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
964	sc->tx_timeout = 0;
965	callout_stop(&sc->timer);
966}
967
968/*
969 * Reset the interface
970 */
971static void
972cs_reset(struct cs_softc *sc)
973{
974
975	CS_ASSERT_LOCKED(sc);
976	cs_stop(sc);
977	cs_init_locked(sc);
978}
979
980static uint16_t
981cs_hash_index(struct sockaddr_dl *addr)
982{
983	uint32_t crc;
984	uint16_t idx;
985	caddr_t lla;
986
987	lla = LLADDR(addr);
988	crc = ether_crc32_le(lla, ETHER_ADDR_LEN);
989	idx = crc >> 26;
990
991	return (idx);
992}
993
994static void
995cs_setmode(struct cs_softc *sc)
996{
997	int rx_ctl;
998	uint16_t af[4];
999	uint16_t port, mask, index;
1000	struct ifnet *ifp = sc->ifp;
1001	struct ifmultiaddr *ifma;
1002
1003	/* Stop the receiver while changing filters */
1004	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
1005
1006	if (ifp->if_flags & IFF_PROMISC) {
1007		/* Turn on promiscuous mode. */
1008		rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1009	} else if (ifp->if_flags & IFF_MULTICAST) {
1010		/* Allow receiving frames with multicast addresses */
1011		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1012			 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1013
1014		/* Start with an empty filter */
1015		af[0] = af[1] = af[2] = af[3] = 0x0000;
1016
1017		if (ifp->if_flags & IFF_ALLMULTI) {
1018			/* Accept all multicast frames */
1019			af[0] = af[1] = af[2] = af[3] = 0xffff;
1020		} else {
1021			/*
1022			 * Set up the filter to only accept multicast
1023			 * frames we're interested in.
1024			 */
1025			if_maddr_rlock(ifp);
1026			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1027				struct sockaddr_dl *dl =
1028				    (struct sockaddr_dl *)ifma->ifma_addr;
1029
1030				index = cs_hash_index(dl);
1031				port = (u_int16_t) (index >> 4);
1032				mask = (u_int16_t) (1 << (index & 0xf));
1033				af[port] |= mask;
1034			}
1035			if_maddr_runlock(ifp);
1036		}
1037
1038		cs_writereg(sc, PP_LAF + 0, af[0]);
1039		cs_writereg(sc, PP_LAF + 2, af[1]);
1040		cs_writereg(sc, PP_LAF + 4, af[2]);
1041		cs_writereg(sc, PP_LAF + 6, af[3]);
1042	} else {
1043		/*
1044		 * Receive only good frames addressed for us and
1045		 * good broadcasts.
1046		 */
1047		rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1048			 RX_OK_ACCEPT;
1049	}
1050
1051	/* Set up the filter */
1052	cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1053
1054	/* Turn on receiver */
1055	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
1056}
1057
1058static int
1059cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
1060{
1061	struct cs_softc *sc=ifp->if_softc;
1062	struct ifreq *ifr = (struct ifreq *)data;
1063	int error=0;
1064
1065#ifdef CS_DEBUG
1066	if_printf(ifp, "%s command=%lx\n", __func__, command);
1067#endif
1068
1069	switch (command) {
1070	case SIOCSIFFLAGS:
1071		/*
1072		 * Switch interface state between "running" and
1073		 * "stopped", reflecting the UP flag.
1074		 */
1075		CS_LOCK(sc);
1076		if (sc->ifp->if_flags & IFF_UP) {
1077			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) {
1078				cs_init_locked(sc);
1079			}
1080		} else {
1081			if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) {
1082				cs_stop(sc);
1083			}
1084		}
1085		/*
1086		 * Promiscuous and/or multicast flags may have changed,
1087		 * so reprogram the multicast filter and/or receive mode.
1088		 *
1089		 * See note about multicasts in cs_setmode
1090		 */
1091		cs_setmode(sc);
1092		CS_UNLOCK(sc);
1093		break;
1094
1095	case SIOCADDMULTI:
1096	case SIOCDELMULTI:
1097	    /*
1098	     * Multicast list has changed; set the hardware filter
1099	     * accordingly.
1100	     *
1101	     * See note about multicasts in cs_setmode
1102	     */
1103	    CS_LOCK(sc);
1104	    cs_setmode(sc);
1105	    CS_UNLOCK(sc);
1106	    error = 0;
1107	    break;
1108
1109	case SIOCSIFMEDIA:
1110	case SIOCGIFMEDIA:
1111		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1112		break;
1113
1114	default:
1115		error = ether_ioctl(ifp, command, data);
1116		break;
1117	}
1118
1119	return (error);
1120}
1121
1122/*
1123 * Device timeout/watchdog routine. Entered if the device neglects to
1124 * generate an interrupt after a transmit has been started on it.
1125 */
1126static void
1127cs_watchdog(void *arg)
1128{
1129	struct cs_softc *sc = arg;
1130	struct ifnet *ifp = sc->ifp;
1131
1132	CS_ASSERT_LOCKED(sc);
1133	if (sc->tx_timeout && --sc->tx_timeout == 0) {
1134		ifp->if_oerrors++;
1135		log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
1136
1137		/* Reset the interface */
1138		if (ifp->if_flags & IFF_UP)
1139			cs_reset(sc);
1140		else
1141			cs_stop(sc);
1142	}
1143	callout_reset(&sc->timer, hz, cs_watchdog, sc);
1144}
1145
1146static int
1147cs_mediachange(struct ifnet *ifp)
1148{
1149	struct cs_softc *sc = ifp->if_softc;
1150	struct ifmedia *ifm = &sc->media;
1151	int error;
1152
1153	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1154		return (EINVAL);
1155
1156	CS_LOCK(sc);
1157	error = cs_mediaset(sc, ifm->ifm_media);
1158	CS_UNLOCK(sc);
1159	return (error);
1160}
1161
1162static void
1163cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1164{
1165	int line_status;
1166	struct cs_softc *sc = ifp->if_softc;
1167
1168	CS_LOCK(sc);
1169	ifmr->ifm_active = IFM_ETHER;
1170	line_status = cs_readreg(sc, PP_LineST);
1171	if (line_status & TENBASET_ON) {
1172		ifmr->ifm_active |= IFM_10_T;
1173		if (sc->chip_type != CS8900) {
1174			if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
1175				ifmr->ifm_active |= IFM_FDX;
1176			if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
1177				ifmr->ifm_active |= IFM_HDX;
1178		}
1179		ifmr->ifm_status = IFM_AVALID;
1180		if (line_status & LINK_OK)
1181			ifmr->ifm_status |= IFM_ACTIVE;
1182	} else {
1183		if (line_status & AUI_ON) {
1184			cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
1185			    HCB1_ENBL);
1186			if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1187			    (cs_readreg(sc, PP_SelfCTL) & HCB1))
1188				ifmr->ifm_active |= IFM_10_2;
1189			else
1190				ifmr->ifm_active |= IFM_10_5;
1191		}
1192	}
1193	CS_UNLOCK(sc);
1194}
1195
1196static int
1197cs_mediaset(struct cs_softc *sc, int media)
1198{
1199	int error = 0;
1200
1201	/* Stop the receiver & transmitter */
1202	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
1203	    ~(SERIAL_RX_ON | SERIAL_TX_ON));
1204
1205#ifdef CS_DEBUG
1206	device_printf(sc->dev, "%s media=%x\n", __func__, media);
1207#endif
1208
1209	switch (IFM_SUBTYPE(media)) {
1210	default:
1211	case IFM_AUTO:
1212		/*
1213		 * This chip makes it a little hard to support this, so treat
1214		 * it as IFM_10_T, auto duplex.
1215		 */
1216		enable_tp(sc);
1217		cs_duplex_auto(sc);
1218		break;
1219	case IFM_10_T:
1220		enable_tp(sc);
1221		if (media & IFM_FDX)
1222			cs_duplex_full(sc);
1223		else if (media & IFM_HDX)
1224			cs_duplex_half(sc);
1225		else
1226			error = cs_duplex_auto(sc);
1227		break;
1228	case IFM_10_2:
1229		enable_bnc(sc);
1230		break;
1231	case IFM_10_5:
1232		enable_aui(sc);
1233		break;
1234	}
1235
1236	/*
1237	 * Turn the transmitter & receiver back on
1238	 */
1239	cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
1240	    SERIAL_RX_ON | SERIAL_TX_ON);
1241
1242	return (error);
1243}
1244