2 * Copyright (c) 1997,1998 Maxim Bolotin and Oleg Sharoiko.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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
27 * $FreeBSD: src/sys/dev/cs/if_cs.c,v 1.19.2.1 2001/01/25 20:13:48 imp Exp $
31 * Device driver for Crystal Semiconductor CS8920 based ethernet
32 * adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/interrupt.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/module.h>
52 #include <sys/thread2.h>
53 #include <sys/machintr.h>
55 #include <machine/clock.h>
58 #include <net/ifq_var.h>
59 #include <net/if_arp.h>
60 #include <net/if_media.h>
61 #include <net/ethernet.h>
68 #define CS_DMA_BUFFER_SIZE 65536
70 #define CS_DMA_BUFFER_SIZE 16384
73 static int cs_recv_delay = 570;
74 SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
76 static void cs_init(void *);
77 static int cs_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
78 static void cs_start(struct ifnet *);
79 static void cs_stop(struct cs_softc *);
80 static void cs_reset(struct cs_softc *);
81 static void cs_watchdog(struct ifnet *);
82 static void csintr(void *);
84 static int cs_mediachange(struct ifnet *);
85 static void cs_mediastatus(struct ifnet *, struct ifmediareq *);
86 static int cs_mediaset(struct cs_softc *, int);
88 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
89 static void cs_xmit_buf(struct cs_softc*);
90 static int cs_get_packet(struct cs_softc*);
91 static void cs_setmode(struct cs_softc*);
93 static int get_eeprom_data(device_t, int, int, int *);
94 static int get_eeprom_cksum(int, int, int *);
95 static int wait_eeprom_ready( struct cs_softc *);
96 static void control_dc_dc(struct cs_softc *, int );
97 static int send_test_pkt(struct cs_softc * );
98 static int enable_tp(struct cs_softc *);
99 static int enable_aui(struct cs_softc *);
100 static int enable_bnc(struct cs_softc *);
101 static int cs_duplex_auto(struct cs_softc *);
103 devclass_t cs_devclass;
105 DECLARE_DUMMY_MODULE(if_cs);
108 get_eeprom_data(device_t dev, int off, int len, int *buffer)
113 sc = device_get_softc(dev);
116 device_printf(dev, "EEPROM data from %x for %x:\n", off, len);
119 for (i=0;i<len;i++) {
120 if (wait_eeprom_ready(sc) < 0) return -1;
121 /* Send command to EEPROM to read */
122 cs_writereg(sc->nic_addr, PP_EECMD, (off+i)|EEPROM_READ_CMD );
123 if (wait_eeprom_ready(sc)<0)
125 buffer[i] = cs_readreg (sc->nic_addr, PP_EEData);
128 kprintf("%02x %02x ",(unsigned char)buffer[i],
129 (unsigned char)buffer[i+1]);
141 get_eeprom_cksum(int off, int len, int *buffer)
154 wait_eeprom_ready(struct cs_softc *sc)
156 DELAY ( 30000 ); /* XXX should we do some checks here ? */
161 control_dc_dc(struct cs_softc *sc, int on_not_off)
163 unsigned int self_control = HCB1_ENBL;
165 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
166 self_control |= HCB1;
168 self_control &= ~HCB1;
169 cs_writereg( sc->nic_addr, PP_SelfCTL, self_control );
176 cs_duplex_auto(struct cs_softc *sc)
180 cs_writereg(sc->nic_addr, PP_AutoNegCTL,
181 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE );
182 for (i=0; cs_readreg(sc->nic_addr,PP_AutoNegST)&AUTO_NEG_BUSY; i++) {
184 if_printf(&sc->arpcom.ac_if, "full/half duplex "
185 "auto negotiation timeout\n");
196 enable_tp(struct cs_softc *sc)
198 cs_writereg(sc->nic_addr, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
199 control_dc_dc(sc, 0);
202 if ((cs_readreg(sc->nic_addr, PP_LineST) & LINK_OK)==0) {
203 if_printf(&sc->arpcom.ac_if, "failed to enable TP\n");
211 * XXX This was rewritten from Linux driver without any tests.
214 send_test_pkt(struct cs_softc *sc)
216 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
217 0, 46, /* A 46 in network order */
218 0, 0, /* DSAP=0 & SSAP=0 fields */
219 0xf3, 0 /* Control (Test Req + P bit set) */ };
221 u_char ether_address_backup[ETHER_ADDR_LEN];
223 for (i = 0; i < ETHER_ADDR_LEN; i++) {
224 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
227 cs_writereg(sc->nic_addr, PP_LineCTL,
228 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_TX_ON );
230 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
231 bcopy(test_packet+ETHER_ADDR_LEN,
232 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
233 outw(sc->nic_addr + TX_CMD_PORT, sc->send_cmd);
234 outw(sc->nic_addr + TX_LEN_PORT, sizeof(test_packet));
236 /* Wait for chip to allocate memory */
238 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
239 for (i = 0; i < ETHER_ADDR_LEN; i++) {
240 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
245 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
249 if ((cs_readreg(sc->nic_addr,PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
250 for (i = 0; i < ETHER_ADDR_LEN; i++) {
251 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
255 for (i = 0; i < ETHER_ADDR_LEN; i++) {
256 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
262 * XXX This was rewritten from Linux driver without any tests.
265 enable_aui(struct cs_softc *sc)
267 control_dc_dc(sc, 0);
268 cs_writereg(sc->nic_addr, PP_LineCTL,
269 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
271 if (!send_test_pkt(sc)) {
272 if_printf(&sc->arpcom.ac_if, "failed to enable AUI\n");
279 * XXX This was rewritten from Linux driver without any tests.
282 enable_bnc(struct cs_softc *sc)
284 control_dc_dc(sc, 1);
285 cs_writereg(sc->nic_addr, PP_LineCTL,
286 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
288 if (!send_test_pkt(sc)) {
289 if_printf(&sc->arpcom.ac_if, "failed to enable BNC\n");
296 cs_cs89x0_probe(device_t dev)
304 struct cs_softc *sc = device_get_softc(dev);
306 unsigned rev_type = 0;
308 int eeprom_buff[CHKSUM_LEN];
309 int chip_type, pp_isaint, pp_isadma;
311 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
315 iobase=rman_get_start(sc->port_res);
317 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG) {
318 /* Chip not detected. Let's try to reset it */
320 device_printf(dev, "trying to reset the chip.\n");
321 outw(iobase+ADD_PORT, PP_SelfCTL);
322 i = inw(iobase+DATA_PORT);
323 outw(iobase+ADD_PORT, PP_SelfCTL);
324 outw(iobase+DATA_PORT, i | POWER_ON_RESET);
325 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG)
329 outw(iobase+ADD_PORT, PP_ChipID);
330 if (inw(iobase+DATA_PORT) != CHIP_EISA_ID_SIG)
333 rev_type = cs_readreg(iobase, PRODUCT_ID_ADD);
334 chip_type = rev_type & ~REVISON_BITS;
335 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
337 sc->nic_addr = iobase;
338 sc->chip_type = chip_type;
340 if(chip_type==CS8900) {
341 pp_isaint = PP_CS8900_ISAINT;
342 pp_isadma = PP_CS8900_ISADMA;
343 sc->send_cmd = TX_CS8900_AFTER_ALL;
345 pp_isaint = PP_CS8920_ISAINT;
346 pp_isadma = PP_CS8920_ISADMA;
347 sc->send_cmd = TX_CS8920_AFTER_ALL;
351 * Clear some fields so that fail of EEPROM will left them clean
353 sc->auto_neg_cnf = 0;
358 * If no interrupt specified (or "?"), use what the board tells us.
360 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
363 * Get data from EEPROM
365 if((cs_readreg(iobase, PP_SelfST) & EEPROM_PRESENT) == 0) {
366 device_printf(dev, "No EEPROM, assuming defaults.\n");
368 if (get_eeprom_data(dev, START_EEPROM_DATA, CHKSUM_LEN,
370 device_printf(dev, "EEPROM read failed, "
371 "assuming defaults.\n");
373 if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
374 device_printf(dev, "EEPROM cheksum bad, "
375 "assuming defaults.\n");
378 eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
380 eeprom_buff[ADAPTER_CNF_OFFSET/2];
382 eeprom_buff[ISA_CNF_OFFSET/2];
384 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
385 sc->arpcom.ac_enaddr[i*2]=
387 sc->arpcom.ac_enaddr[i*2+1]=
392 * If no interrupt specified (or "?"),
393 * use what the board tells us.
396 irq = sc->isa_config & INT_NO_MASK;
397 if (chip_type==CS8900) {
416 device_printf(dev, "invalid irq in EEPROM.\n");
420 if (irq>CS8920_NO_INTS) {
421 device_printf(dev, "invalid irq in EEPROM.\n");
429 bus_set_resource(dev, SYS_RES_IRQ, 0,
430 irq, 1, machintr_legacy_intr_cpuid(irq));
438 if (chip_type == CS8900) {
456 if (irq > CS8920_NO_INTS) {
463 cs_writereg(iobase, pp_isaint, irq);
465 device_printf(dev, "Unknown or invalid irq\n");
473 cs_writereg(iobase, pp_isadma, drq);
475 device_printf(dev, "incorrect drq\n");
481 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
482 chip_type==CS8900 ? '0' : '2',
483 chip_type==CS8920M ? "M" : "",
485 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
486 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
487 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
489 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
490 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
491 sc->line_ctl = LOW_RX_SQUELCH;
500 * Allocate a port resource with the given resource id.
503 cs_alloc_port(device_t dev, int rid, int size)
505 struct cs_softc *sc = device_get_softc(dev);
506 struct resource *res;
508 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
509 0ul, ~0ul, size, RF_ACTIVE);
513 sc->port_used = size;
521 * Allocate a memory resource with the given resource id.
524 cs_alloc_memory(device_t dev, int rid, int size)
526 struct cs_softc *sc = device_get_softc(dev);
527 struct resource *res;
529 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
530 0ul, ~0ul, size, RF_ACTIVE);
542 * Allocate an irq resource with the given resource id.
545 cs_alloc_irq(device_t dev, int rid, int flags)
547 struct cs_softc *sc = device_get_softc(dev);
548 struct resource *res;
550 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
551 (RF_ACTIVE | flags));
562 * Release all resources
565 cs_release_resources(device_t dev)
567 struct cs_softc *sc = device_get_softc(dev);
570 bus_release_resource(dev, SYS_RES_IOPORT,
571 sc->port_rid, sc->port_res);
575 bus_release_resource(dev, SYS_RES_MEMORY,
576 sc->mem_rid, sc->mem_res);
580 bus_release_resource(dev, SYS_RES_IRQ,
581 sc->irq_rid, sc->irq_res);
587 * Install the interface into kernel networking data structures
590 cs_attach(device_t dev)
592 struct cs_softc *sc = device_get_softc(dev);
593 int media = 0, error;
594 struct ifnet *ifp = &sc->arpcom.ac_if;
597 * Initialize the media structures.
599 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
601 KASSERT(sc->port_used > 0 || sc->mem_used > 0,
602 ("%s: either I/O port or I/O memory should be used",
603 device_get_nameunit(dev)));
604 if (sc->port_used > 0)
605 cs_alloc_port(dev, sc->port_rid, sc->port_used);
606 if (sc->mem_used > 0)
607 cs_alloc_memory(dev, sc->mem_rid, sc->mem_used);
609 error = cs_alloc_irq(dev, sc->irq_rid, 0);
611 device_printf(dev, "Couldn't allocate irq");
618 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
619 ifp->if_start=cs_start;
620 ifp->if_ioctl=cs_ioctl;
621 ifp->if_watchdog=cs_watchdog;
622 ifp->if_init=cs_init;
623 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
624 ifq_set_ready(&ifp->if_snd);
630 ifp->if_linkmib=&sc->mibdata;
631 ifp->if_linkmiblen=sizeof sc->mibdata;
634 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
638 * this code still in progress (DMA support)
640 sc->recv_ring=kmalloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_WAITOK);
641 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
642 < (128*1024-CS_DMA_BUFFER_SIZE))
643 sc->recv_ring+=16*1024;
646 sc->buffer=kmalloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_WAITOK);
648 if (sc->adapter_cnf & A_CNF_10B_T) {
649 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
650 if (sc->chip_type != CS8900) {
651 ifmedia_add(&sc->media,
652 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
653 ifmedia_add(&sc->media,
654 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
658 if (sc->adapter_cnf & A_CNF_10B_2)
659 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
661 if (sc->adapter_cnf & A_CNF_AUI)
662 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
664 if (sc->adapter_cnf & A_CNF_MEDIA)
665 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
667 /* Set default media from EEPROM */
668 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
669 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
670 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
671 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
672 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
673 default: device_printf(dev, "adapter has no media\n");
675 ifmedia_set(&sc->media, media);
676 cs_mediaset(sc, media);
678 ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
680 error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
681 csintr, sc, &sc->irq_handle,
684 device_printf(dev, "Couldn't set up irq");
689 ifp->if_cpuid = rman_get_cpuid(sc->irq_res);
690 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
700 cs_detach(device_t dev)
702 struct cs_softc *sc = device_get_softc(dev);
703 struct ifnet *ifp = &sc->arpcom.ac_if;
705 if (device_is_attached(dev)) {
706 lwkt_serialize_enter(ifp->if_serializer);
708 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
709 lwkt_serialize_exit(ifp->if_serializer);
711 ether_ifdetach(&sc->arpcom.ac_if);
716 * this code still in progress (DMA support)
718 if (sc->recv_ring != NULL)
719 kfree(sc->recv_ring, M_DEVBUF);
722 if (sc->buffer != NULL)
723 kfree(sc->buffer, M_DEVBUF);
724 cs_release_resources(dev);
725 ifmedia_removeall(&sc->media);
731 * Initialize the board
736 struct cs_softc *sc=(struct cs_softc *)xsc;
737 struct ifnet *ifp = &sc->arpcom.ac_if;
741 * reset whatchdog timer
747 * Hardware initialization of cs
750 /* Enable receiver and transmitter */
751 cs_writereg(sc->nic_addr, PP_LineCTL,
752 cs_readreg( sc->nic_addr, PP_LineCTL ) |
753 SERIAL_RX_ON | SERIAL_TX_ON);
755 /* Configure the receiver mode */
759 * This defines what type of frames will cause interrupts
760 * Bad frames should generate interrupts so that the driver
761 * could track statistics of discarded packets
763 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
765 if (sc->isa_config & STREAM_TRANSFER)
766 rx_cfg |= RX_STREAM_ENBL;
767 cs_writereg(sc->nic_addr, PP_RxCFG, rx_cfg);
769 cs_writereg(sc->nic_addr, PP_TxCFG, TX_LOST_CRS_ENBL |
770 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
771 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
773 cs_writereg(sc->nic_addr, PP_BufCFG, READY_FOR_TX_ENBL |
774 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
775 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
777 /* Write MAC address into IA filter */
778 for (i=0; i<ETHER_ADDR_LEN/2; i++)
779 cs_writereg(sc->nic_addr, PP_IA+i*2,
780 sc->arpcom.ac_enaddr[i*2] |
781 (sc->arpcom.ac_enaddr[i*2+1] << 8) );
784 * Now enable everything
787 #ifdef CS_USE_64K_DMA
788 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
791 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
794 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
797 * Set running and clear output active flags
799 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
800 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
803 * Start sending process
805 if_devstart(&sc->arpcom.ac_if);
809 * Get the packet from the board and send it to the upper layer
813 cs_get_packet(struct cs_softc *sc)
815 struct ifnet *ifp = &(sc->arpcom.ac_if);
816 int iobase = sc->nic_addr, status, length;
823 status = inw(iobase + RX_FRAME_PORT);
824 length = inw(iobase + RX_FRAME_PORT);
827 if_printf(ifp, "rcvd: stat %x, len %d\n", status, length);
830 if (!(status & RX_OK)) {
832 if_printf(ifp, "bad pkt stat %x\n", status);
838 MGETHDR(m, MB_DONTWAIT, MT_DATA);
842 if (length > MHLEN) {
843 MCLGET(m, MB_DONTWAIT);
844 if (!(m->m_flags & M_EXT)) {
850 /* Initialize packet's header info */
851 m->m_pkthdr.rcvif = ifp;
852 m->m_pkthdr.len = length;
856 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
859 for (i=0;i<length;i++)
860 kprintf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
864 if (status & (RX_IA | RX_BROADCAST) ||
865 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
866 ifp->if_input(ifp, m);
870 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
871 DELAY( cs_recv_delay );
885 struct cs_softc *sc = (struct cs_softc*) arg;
886 struct ifnet *ifp = &(sc->arpcom.ac_if);
890 if_printf(ifp, "Interrupt.\n");
893 while ((status=cs_readword(sc->nic_addr, ISQ_PORT))) {
896 if_printf(ifp, "from ISQ: %04x\n", status);
899 switch (status & ISQ_EVENT_MASK) {
900 case ISQ_RECEIVER_EVENT:
904 case ISQ_TRANSMITTER_EVENT:
909 ifp->if_flags &= ~IFF_OACTIVE;
913 case ISQ_BUFFER_EVENT:
914 if (status & READY_FOR_TX) {
915 ifp->if_flags &= ~IFF_OACTIVE;
919 if (status & TX_UNDERRUN) {
920 ifp->if_flags &= ~IFF_OACTIVE;
926 case ISQ_RX_MISS_EVENT:
927 ifp->if_ierrors+=(status>>6);
930 case ISQ_TX_COL_EVENT:
931 ifp->if_collisions+=(status>>6);
936 if (!(ifp->if_flags & IFF_OACTIVE))
941 * Save the data in buffer
945 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
949 unsigned char *data, *buf;
951 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
961 * Find actual data address
963 data = mtod(mp, caddr_t);
965 bcopy((caddr_t) data, (caddr_t) buf, len);
973 cs_xmit_buf( struct cs_softc *sc )
975 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
980 cs_start(struct ifnet *ifp)
984 struct cs_softc *sc = ifp->if_softc;
988 length = sc->buf_len;
990 m = ifq_dequeue(&ifp->if_snd, NULL);
996 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
999 /* Skip zero-length packets */
1005 cs_write_mbufs(sc, m);
1013 * Issue a SEND command
1015 outw(sc->nic_addr+TX_CMD_PORT, sc->send_cmd);
1016 outw(sc->nic_addr+TX_LEN_PORT, length );
1019 * If there's no free space in the buffer then leave
1020 * this packet for the next time: indicate output active
1023 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
1024 ifp->if_timer = sc->buf_len;
1025 ifp->if_flags |= IFF_OACTIVE;
1032 * Set the watchdog timer in case we never hear
1033 * from board again. (I don't know about correct
1034 * value for this timeout)
1036 ifp->if_timer = length;
1038 ifp->if_flags |= IFF_OACTIVE;
1044 * Stop everything on the interface
1047 cs_stop(struct cs_softc *sc)
1049 cs_writereg(sc->nic_addr, PP_RxCFG, 0);
1050 cs_writereg(sc->nic_addr, PP_TxCFG, 0);
1051 cs_writereg(sc->nic_addr, PP_BufCFG, 0);
1052 cs_writereg(sc->nic_addr, PP_BusCTL, 0);
1054 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1055 sc->arpcom.ac_if.if_timer = 0;
1059 * Reset the interface
1062 cs_reset(struct cs_softc *sc)
1069 cs_setmode(struct cs_softc *sc)
1071 struct ifnet *ifp = &(sc->arpcom.ac_if);
1074 /* Stop the receiver while changing filters */
1075 cs_writereg(sc->nic_addr, PP_LineCTL,
1076 cs_readreg(sc->nic_addr, PP_LineCTL) & ~SERIAL_RX_ON);
1078 if (ifp->if_flags & IFF_PROMISC) {
1079 /* Turn on promiscuous mode. */
1080 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1082 if (ifp->if_flags & IFF_MULTICAST) {
1083 /* Allow receiving frames with multicast addresses */
1084 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1085 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1087 * Here the reconfiguration of chip's multicast
1088 * filters should be done but I've no idea about
1089 * hash transformation in this chip. If you can
1090 * add this code or describe me the transformation
1095 * Receive only good frames addressed for us and
1098 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1103 /* Set up the filter */
1104 cs_writereg(sc->nic_addr, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1106 /* Turn on receiver */
1107 cs_writereg(sc->nic_addr, PP_LineCTL,
1108 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_RX_ON);
1112 cs_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1114 struct cs_softc *sc=ifp->if_softc;
1115 struct ifreq *ifr = (struct ifreq *)data;
1119 if_printf(ifp, "ioctl(%lx)\n", command);
1125 * Switch interface state between "running" and
1126 * "stopped", reflecting the UP flag.
1128 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1129 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1133 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1138 * Promiscuous and/or multicast flags may have changed,
1139 * so reprogram the multicast filter and/or receive mode.
1141 * See note about multicasts in cs_setmode
1149 * Multicast list has changed; set the hardware filter
1152 * See note about multicasts in cs_setmode
1160 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1163 error = ether_ioctl(ifp, command, data);
1171 * Device timeout/watchdog routine. Entered if the device neglects to
1172 * generate an interrupt after a transmit has been started on it.
1175 cs_watchdog(struct ifnet *ifp)
1177 struct cs_softc *sc = ifp->if_softc;
1180 log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
1182 /* Reset the interface */
1183 if (ifp->if_flags & IFF_UP)
1190 cs_mediachange(struct ifnet *ifp)
1192 struct cs_softc *sc = ifp->if_softc;
1193 struct ifmedia *ifm = &sc->media;
1195 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1198 return cs_mediaset(sc, ifm->ifm_media);
1202 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1205 struct cs_softc *sc = ifp->if_softc;
1207 ifmr->ifm_active = IFM_ETHER;
1208 line_status = cs_readreg(sc->nic_addr, PP_LineST);
1209 if (line_status & TENBASET_ON) {
1210 ifmr->ifm_active |= IFM_10_T;
1211 if (sc->chip_type != CS8900) {
1212 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & FDX_ACTIVE)
1213 ifmr->ifm_active |= IFM_FDX;
1214 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & HDX_ACTIVE)
1215 ifmr->ifm_active |= IFM_HDX;
1217 ifmr->ifm_status = IFM_AVALID;
1218 if (line_status & LINK_OK)
1219 ifmr->ifm_status |= IFM_ACTIVE;
1221 if (line_status & AUI_ON) {
1222 cs_writereg(sc->nic_addr, PP_SelfCTL,
1223 cs_readreg(sc->nic_addr, PP_SelfCTL) |
1225 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1226 (cs_readreg(sc->nic_addr, PP_SelfCTL)&HCB1))
1227 ifmr->ifm_active |= IFM_10_2;
1229 ifmr->ifm_active |= IFM_10_5;
1235 cs_mediaset(struct cs_softc *sc, int media)
1239 /* Stop the receiver & transmitter */
1240 cs_writereg(sc->nic_addr, PP_LineCTL,
1241 cs_readreg(sc->nic_addr, PP_LineCTL) &
1242 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1245 if_printf(&sc->arpcom.ac_if, "cs_setmedia(%x)\n", media);
1248 switch (IFM_SUBTYPE(media)) {
1251 if ((error=enable_tp(sc))==0)
1252 error = cs_duplex_auto(sc);
1253 else if ((error=enable_bnc(sc)) != 0)
1254 error = enable_aui(sc);
1257 if ((error=enable_tp(sc)) != 0)
1259 if (media & IFM_FDX)
1261 else if (media & IFM_HDX)
1264 error = cs_duplex_auto(sc);
1267 error = enable_bnc(sc);
1270 error = enable_aui(sc);
1275 * Turn the transmitter & receiver back on
1277 cs_writereg(sc->nic_addr, PP_LineCTL,
1278 cs_readreg( sc->nic_addr, PP_LineCTL ) |
1279 SERIAL_RX_ON | SERIAL_TX_ON);