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 $
28 * $DragonFly: src/sys/dev/netif/cs/if_cs.c,v 1.30 2008/08/17 04:32:33 sephe Exp $
32 * Device driver for Crystal Semiconductor CS8920 based ethernet
33 * adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/interrupt.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/module.h>
53 #include <sys/thread2.h>
54 #include <sys/machintr.h>
56 #include <machine/clock.h>
59 #include <net/ifq_var.h>
60 #include <net/if_arp.h>
61 #include <net/if_media.h>
62 #include <net/ethernet.h>
69 #define CS_DMA_BUFFER_SIZE 65536
71 #define CS_DMA_BUFFER_SIZE 16384
74 static int cs_recv_delay = 570;
75 SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
77 static void cs_init(void *);
78 static int cs_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
79 static void cs_start(struct ifnet *);
80 static void cs_stop(struct cs_softc *);
81 static void cs_reset(struct cs_softc *);
82 static void cs_watchdog(struct ifnet *);
83 static void csintr(void *);
85 static int cs_mediachange(struct ifnet *);
86 static void cs_mediastatus(struct ifnet *, struct ifmediareq *);
87 static int cs_mediaset(struct cs_softc *, int);
89 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
90 static void cs_xmit_buf(struct cs_softc*);
91 static int cs_get_packet(struct cs_softc*);
92 static void cs_setmode(struct cs_softc*);
94 static int get_eeprom_data(device_t, int, int, int *);
95 static int get_eeprom_cksum(int, int, int *);
96 static int wait_eeprom_ready( struct cs_softc *);
97 static void control_dc_dc(struct cs_softc *, int );
98 static int send_test_pkt(struct cs_softc * );
99 static int enable_tp(struct cs_softc *);
100 static int enable_aui(struct cs_softc *);
101 static int enable_bnc(struct cs_softc *);
102 static int cs_duplex_auto(struct cs_softc *);
104 devclass_t cs_devclass;
106 DECLARE_DUMMY_MODULE(if_cs);
109 get_eeprom_data(device_t dev, int off, int len, int *buffer)
114 sc = device_get_softc(dev);
117 device_printf(dev, "EEPROM data from %x for %x:\n", off, len);
120 for (i=0;i<len;i++) {
121 if (wait_eeprom_ready(sc) < 0) return -1;
122 /* Send command to EEPROM to read */
123 cs_writereg(sc->nic_addr, PP_EECMD, (off+i)|EEPROM_READ_CMD );
124 if (wait_eeprom_ready(sc)<0)
126 buffer[i] = cs_readreg (sc->nic_addr, PP_EEData);
129 kprintf("%02x %02x ",(unsigned char)buffer[i],
130 (unsigned char)buffer[i+1]);
142 get_eeprom_cksum(int off, int len, int *buffer)
155 wait_eeprom_ready(struct cs_softc *sc)
157 DELAY ( 30000 ); /* XXX should we do some checks here ? */
162 control_dc_dc(struct cs_softc *sc, int on_not_off)
164 unsigned int self_control = HCB1_ENBL;
166 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
167 self_control |= HCB1;
169 self_control &= ~HCB1;
170 cs_writereg( sc->nic_addr, PP_SelfCTL, self_control );
177 cs_duplex_auto(struct cs_softc *sc)
181 cs_writereg(sc->nic_addr, PP_AutoNegCTL,
182 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE );
183 for (i=0; cs_readreg(sc->nic_addr,PP_AutoNegST)&AUTO_NEG_BUSY; i++) {
185 if_printf(&sc->arpcom.ac_if, "full/half duplex "
186 "auto negotiation timeout\n");
197 enable_tp(struct cs_softc *sc)
199 cs_writereg(sc->nic_addr, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
200 control_dc_dc(sc, 0);
203 if ((cs_readreg(sc->nic_addr, PP_LineST) & LINK_OK)==0) {
204 if_printf(&sc->arpcom.ac_if, "failed to enable TP\n");
212 * XXX This was rewritten from Linux driver without any tests.
215 send_test_pkt(struct cs_softc *sc)
217 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
218 0, 46, /* A 46 in network order */
219 0, 0, /* DSAP=0 & SSAP=0 fields */
220 0xf3, 0 /* Control (Test Req + P bit set) */ };
222 u_char ether_address_backup[ETHER_ADDR_LEN];
224 for (i = 0; i < ETHER_ADDR_LEN; i++) {
225 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
228 cs_writereg(sc->nic_addr, PP_LineCTL,
229 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_TX_ON );
231 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
232 bcopy(test_packet+ETHER_ADDR_LEN,
233 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
234 outw(sc->nic_addr + TX_CMD_PORT, sc->send_cmd);
235 outw(sc->nic_addr + TX_LEN_PORT, sizeof(test_packet));
237 /* Wait for chip to allocate memory */
239 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
240 for (i = 0; i < ETHER_ADDR_LEN; i++) {
241 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
246 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
250 if ((cs_readreg(sc->nic_addr,PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
251 for (i = 0; i < ETHER_ADDR_LEN; i++) {
252 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
256 for (i = 0; i < ETHER_ADDR_LEN; i++) {
257 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
263 * XXX This was rewritten from Linux driver without any tests.
266 enable_aui(struct cs_softc *sc)
268 control_dc_dc(sc, 0);
269 cs_writereg(sc->nic_addr, PP_LineCTL,
270 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
272 if (!send_test_pkt(sc)) {
273 if_printf(&sc->arpcom.ac_if, "failed to enable AUI\n");
280 * XXX This was rewritten from Linux driver without any tests.
283 enable_bnc(struct cs_softc *sc)
285 control_dc_dc(sc, 1);
286 cs_writereg(sc->nic_addr, PP_LineCTL,
287 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
289 if (!send_test_pkt(sc)) {
290 if_printf(&sc->arpcom.ac_if, "failed to enable BNC\n");
297 cs_cs89x0_probe(device_t dev)
305 struct cs_softc *sc = device_get_softc(dev);
307 unsigned rev_type = 0;
309 int eeprom_buff[CHKSUM_LEN];
310 int chip_type, pp_isaint, pp_isadma;
312 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
316 iobase=rman_get_start(sc->port_res);
318 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG) {
319 /* Chip not detected. Let's try to reset it */
321 device_printf(dev, "trying to reset the chip.\n");
322 outw(iobase+ADD_PORT, PP_SelfCTL);
323 i = inw(iobase+DATA_PORT);
324 outw(iobase+ADD_PORT, PP_SelfCTL);
325 outw(iobase+DATA_PORT, i | POWER_ON_RESET);
326 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG)
330 outw(iobase+ADD_PORT, PP_ChipID);
331 if (inw(iobase+DATA_PORT) != CHIP_EISA_ID_SIG)
334 rev_type = cs_readreg(iobase, PRODUCT_ID_ADD);
335 chip_type = rev_type & ~REVISON_BITS;
336 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
338 sc->nic_addr = iobase;
339 sc->chip_type = chip_type;
341 if(chip_type==CS8900) {
342 pp_isaint = PP_CS8900_ISAINT;
343 pp_isadma = PP_CS8900_ISADMA;
344 sc->send_cmd = TX_CS8900_AFTER_ALL;
346 pp_isaint = PP_CS8920_ISAINT;
347 pp_isadma = PP_CS8920_ISADMA;
348 sc->send_cmd = TX_CS8920_AFTER_ALL;
352 * Clear some fields so that fail of EEPROM will left them clean
354 sc->auto_neg_cnf = 0;
359 * If no interrupt specified (or "?"), use what the board tells us.
361 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
364 * Get data from EEPROM
366 if((cs_readreg(iobase, PP_SelfST) & EEPROM_PRESENT) == 0) {
367 device_printf(dev, "No EEPROM, assuming defaults.\n");
369 if (get_eeprom_data(dev, START_EEPROM_DATA, CHKSUM_LEN,
371 device_printf(dev, "EEPROM read failed, "
372 "assuming defaults.\n");
374 if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
375 device_printf(dev, "EEPROM cheksum bad, "
376 "assuming defaults.\n");
379 eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
381 eeprom_buff[ADAPTER_CNF_OFFSET/2];
383 eeprom_buff[ISA_CNF_OFFSET/2];
385 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
386 sc->arpcom.ac_enaddr[i*2]=
388 sc->arpcom.ac_enaddr[i*2+1]=
393 * If no interrupt specified (or "?"),
394 * use what the board tells us.
397 irq = sc->isa_config & INT_NO_MASK;
398 if (chip_type==CS8900) {
417 device_printf(dev, "invalid irq in EEPROM.\n");
421 if (irq>CS8920_NO_INTS) {
422 device_printf(dev, "invalid irq in EEPROM.\n");
430 bus_set_resource(dev, SYS_RES_IRQ, 0,
431 irq, 1, machintr_intr_cpuid(irq));
439 if (chip_type == CS8900) {
457 if (irq > CS8920_NO_INTS) {
464 cs_writereg(iobase, pp_isaint, irq);
466 device_printf(dev, "Unknown or invalid irq\n");
474 cs_writereg(iobase, pp_isadma, drq);
476 device_printf(dev, "incorrect drq\n");
482 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
483 chip_type==CS8900 ? '0' : '2',
484 chip_type==CS8920M ? "M" : "",
486 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
487 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
488 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
490 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
491 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
492 sc->line_ctl = LOW_RX_SQUELCH;
501 * Allocate a port resource with the given resource id.
504 cs_alloc_port(device_t dev, int rid, int size)
506 struct cs_softc *sc = device_get_softc(dev);
507 struct resource *res;
509 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
510 0ul, ~0ul, size, RF_ACTIVE);
514 sc->port_used = size;
522 * Allocate a memory resource with the given resource id.
525 cs_alloc_memory(device_t dev, int rid, int size)
527 struct cs_softc *sc = device_get_softc(dev);
528 struct resource *res;
530 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
531 0ul, ~0ul, size, RF_ACTIVE);
543 * Allocate an irq resource with the given resource id.
546 cs_alloc_irq(device_t dev, int rid, int flags)
548 struct cs_softc *sc = device_get_softc(dev);
549 struct resource *res;
551 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
552 (RF_ACTIVE | flags));
563 * Release all resources
566 cs_release_resources(device_t dev)
568 struct cs_softc *sc = device_get_softc(dev);
571 bus_release_resource(dev, SYS_RES_IOPORT,
572 sc->port_rid, sc->port_res);
576 bus_release_resource(dev, SYS_RES_MEMORY,
577 sc->mem_rid, sc->mem_res);
581 bus_release_resource(dev, SYS_RES_IRQ,
582 sc->irq_rid, sc->irq_res);
588 * Install the interface into kernel networking data structures
591 cs_attach(device_t dev)
593 struct cs_softc *sc = device_get_softc(dev);
594 int media = 0, error;
595 struct ifnet *ifp = &sc->arpcom.ac_if;
598 * Initialize the media structures.
600 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
602 KASSERT(sc->port_used > 0 || sc->mem_used > 0,
603 ("%s: either I/O port or I/O memory should be used",
604 device_get_nameunit(dev)));
605 if (sc->port_used > 0)
606 cs_alloc_port(dev, sc->port_rid, sc->port_used);
607 if (sc->mem_used > 0)
608 cs_alloc_memory(dev, sc->mem_rid, sc->mem_used);
610 error = cs_alloc_irq(dev, sc->irq_rid, 0);
612 device_printf(dev, "Couldn't allocate irq");
619 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
620 ifp->if_start=cs_start;
621 ifp->if_ioctl=cs_ioctl;
622 ifp->if_watchdog=cs_watchdog;
623 ifp->if_init=cs_init;
624 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
625 ifq_set_ready(&ifp->if_snd);
631 ifp->if_linkmib=&sc->mibdata;
632 ifp->if_linkmiblen=sizeof sc->mibdata;
635 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
639 * this code still in progress (DMA support)
641 sc->recv_ring=kmalloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_WAITOK);
642 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
643 < (128*1024-CS_DMA_BUFFER_SIZE))
644 sc->recv_ring+=16*1024;
647 sc->buffer=kmalloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_WAITOK);
649 if (sc->adapter_cnf & A_CNF_10B_T) {
650 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
651 if (sc->chip_type != CS8900) {
652 ifmedia_add(&sc->media,
653 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
654 ifmedia_add(&sc->media,
655 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
659 if (sc->adapter_cnf & A_CNF_10B_2)
660 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
662 if (sc->adapter_cnf & A_CNF_AUI)
663 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
665 if (sc->adapter_cnf & A_CNF_MEDIA)
666 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
668 /* Set default media from EEPROM */
669 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
670 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
671 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
672 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
673 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
674 default: device_printf(dev, "adapter has no media\n");
676 ifmedia_set(&sc->media, media);
677 cs_mediaset(sc, media);
679 ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
681 error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
682 csintr, sc, &sc->irq_handle,
685 device_printf(dev, "Couldn't set up irq");
690 ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->irq_res));
691 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
701 cs_detach(device_t dev)
703 struct cs_softc *sc = device_get_softc(dev);
704 struct ifnet *ifp = &sc->arpcom.ac_if;
706 if (device_is_attached(dev)) {
707 lwkt_serialize_enter(ifp->if_serializer);
709 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
710 lwkt_serialize_exit(ifp->if_serializer);
712 ether_ifdetach(&sc->arpcom.ac_if);
717 * this code still in progress (DMA support)
719 if (sc->recv_ring != NULL)
720 kfree(sc->recv_ring, M_DEVBUF);
723 if (sc->buffer != NULL)
724 kfree(sc->buffer, M_DEVBUF);
725 cs_release_resources(dev);
726 ifmedia_removeall(&sc->media);
732 * Initialize the board
737 struct cs_softc *sc=(struct cs_softc *)xsc;
738 struct ifnet *ifp = &sc->arpcom.ac_if;
742 * reset whatchdog timer
748 * Hardware initialization of cs
751 /* Enable receiver and transmitter */
752 cs_writereg(sc->nic_addr, PP_LineCTL,
753 cs_readreg( sc->nic_addr, PP_LineCTL ) |
754 SERIAL_RX_ON | SERIAL_TX_ON);
756 /* Configure the receiver mode */
760 * This defines what type of frames will cause interrupts
761 * Bad frames should generate interrupts so that the driver
762 * could track statistics of discarded packets
764 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
766 if (sc->isa_config & STREAM_TRANSFER)
767 rx_cfg |= RX_STREAM_ENBL;
768 cs_writereg(sc->nic_addr, PP_RxCFG, rx_cfg);
770 cs_writereg(sc->nic_addr, PP_TxCFG, TX_LOST_CRS_ENBL |
771 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
772 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
774 cs_writereg(sc->nic_addr, PP_BufCFG, READY_FOR_TX_ENBL |
775 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
776 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
778 /* Write MAC address into IA filter */
779 for (i=0; i<ETHER_ADDR_LEN/2; i++)
780 cs_writereg(sc->nic_addr, PP_IA+i*2,
781 sc->arpcom.ac_enaddr[i*2] |
782 (sc->arpcom.ac_enaddr[i*2+1] << 8) );
785 * Now enable everything
788 #ifdef CS_USE_64K_DMA
789 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
792 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
795 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
798 * Set running and clear output active flags
800 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
801 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
804 * Start sending process
806 if_devstart(&sc->arpcom.ac_if);
810 * Get the packet from the board and send it to the upper layer
814 cs_get_packet(struct cs_softc *sc)
816 struct ifnet *ifp = &(sc->arpcom.ac_if);
817 int iobase = sc->nic_addr, status, length;
824 status = inw(iobase + RX_FRAME_PORT);
825 length = inw(iobase + RX_FRAME_PORT);
828 if_printf(ifp, "rcvd: stat %x, len %d\n", status, length);
831 if (!(status & RX_OK)) {
833 if_printf(ifp, "bad pkt stat %x\n", status);
839 MGETHDR(m, MB_DONTWAIT, MT_DATA);
843 if (length > MHLEN) {
844 MCLGET(m, MB_DONTWAIT);
845 if (!(m->m_flags & M_EXT)) {
851 /* Initialize packet's header info */
852 m->m_pkthdr.rcvif = ifp;
853 m->m_pkthdr.len = length;
857 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
860 for (i=0;i<length;i++)
861 kprintf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
865 if (status & (RX_IA | RX_BROADCAST) ||
866 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
867 ifp->if_input(ifp, m);
871 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
872 DELAY( cs_recv_delay );
886 struct cs_softc *sc = (struct cs_softc*) arg;
887 struct ifnet *ifp = &(sc->arpcom.ac_if);
891 if_printf(ifp, "Interrupt.\n");
894 while ((status=cs_readword(sc->nic_addr, ISQ_PORT))) {
897 if_printf(ifp, "from ISQ: %04x\n", status);
900 switch (status & ISQ_EVENT_MASK) {
901 case ISQ_RECEIVER_EVENT:
905 case ISQ_TRANSMITTER_EVENT:
910 ifp->if_flags &= ~IFF_OACTIVE;
914 case ISQ_BUFFER_EVENT:
915 if (status & READY_FOR_TX) {
916 ifp->if_flags &= ~IFF_OACTIVE;
920 if (status & TX_UNDERRUN) {
921 ifp->if_flags &= ~IFF_OACTIVE;
927 case ISQ_RX_MISS_EVENT:
928 ifp->if_ierrors+=(status>>6);
931 case ISQ_TX_COL_EVENT:
932 ifp->if_collisions+=(status>>6);
937 if (!(ifp->if_flags & IFF_OACTIVE))
942 * Save the data in buffer
946 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
950 unsigned char *data, *buf;
952 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
962 * Find actual data address
964 data = mtod(mp, caddr_t);
966 bcopy((caddr_t) data, (caddr_t) buf, len);
974 cs_xmit_buf( struct cs_softc *sc )
976 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
981 cs_start(struct ifnet *ifp)
985 struct cs_softc *sc = ifp->if_softc;
989 length = sc->buf_len;
991 m = ifq_dequeue(&ifp->if_snd, NULL);
997 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
1000 /* Skip zero-length packets */
1006 cs_write_mbufs(sc, m);
1014 * Issue a SEND command
1016 outw(sc->nic_addr+TX_CMD_PORT, sc->send_cmd);
1017 outw(sc->nic_addr+TX_LEN_PORT, length );
1020 * If there's no free space in the buffer then leave
1021 * this packet for the next time: indicate output active
1024 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
1025 ifp->if_timer = sc->buf_len;
1026 ifp->if_flags |= IFF_OACTIVE;
1033 * Set the watchdog timer in case we never hear
1034 * from board again. (I don't know about correct
1035 * value for this timeout)
1037 ifp->if_timer = length;
1039 ifp->if_flags |= IFF_OACTIVE;
1045 * Stop everything on the interface
1048 cs_stop(struct cs_softc *sc)
1050 cs_writereg(sc->nic_addr, PP_RxCFG, 0);
1051 cs_writereg(sc->nic_addr, PP_TxCFG, 0);
1052 cs_writereg(sc->nic_addr, PP_BufCFG, 0);
1053 cs_writereg(sc->nic_addr, PP_BusCTL, 0);
1055 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1056 sc->arpcom.ac_if.if_timer = 0;
1060 * Reset the interface
1063 cs_reset(struct cs_softc *sc)
1070 cs_setmode(struct cs_softc *sc)
1072 struct ifnet *ifp = &(sc->arpcom.ac_if);
1075 /* Stop the receiver while changing filters */
1076 cs_writereg(sc->nic_addr, PP_LineCTL,
1077 cs_readreg(sc->nic_addr, PP_LineCTL) & ~SERIAL_RX_ON);
1079 if (ifp->if_flags & IFF_PROMISC) {
1080 /* Turn on promiscuous mode. */
1081 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1083 if (ifp->if_flags & IFF_MULTICAST) {
1084 /* Allow receiving frames with multicast addresses */
1085 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1086 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1088 * Here the reconfiguration of chip's multicast
1089 * filters should be done but I've no idea about
1090 * hash transformation in this chip. If you can
1091 * add this code or describe me the transformation
1096 * Receive only good frames addressed for us and
1099 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1104 /* Set up the filter */
1105 cs_writereg(sc->nic_addr, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1107 /* Turn on receiver */
1108 cs_writereg(sc->nic_addr, PP_LineCTL,
1109 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_RX_ON);
1113 cs_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1115 struct cs_softc *sc=ifp->if_softc;
1116 struct ifreq *ifr = (struct ifreq *)data;
1120 if_printf(ifp, "ioctl(%lx)\n", command);
1126 * Switch interface state between "running" and
1127 * "stopped", reflecting the UP flag.
1129 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1130 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1134 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1139 * Promiscuous and/or multicast flags may have changed,
1140 * so reprogram the multicast filter and/or receive mode.
1142 * See note about multicasts in cs_setmode
1150 * Multicast list has changed; set the hardware filter
1153 * See note about multicasts in cs_setmode
1161 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1164 error = ether_ioctl(ifp, command, data);
1172 * Device timeout/watchdog routine. Entered if the device neglects to
1173 * generate an interrupt after a transmit has been started on it.
1176 cs_watchdog(struct ifnet *ifp)
1178 struct cs_softc *sc = ifp->if_softc;
1181 log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
1183 /* Reset the interface */
1184 if (ifp->if_flags & IFF_UP)
1191 cs_mediachange(struct ifnet *ifp)
1193 struct cs_softc *sc = ifp->if_softc;
1194 struct ifmedia *ifm = &sc->media;
1196 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1199 return cs_mediaset(sc, ifm->ifm_media);
1203 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1206 struct cs_softc *sc = ifp->if_softc;
1208 ifmr->ifm_active = IFM_ETHER;
1209 line_status = cs_readreg(sc->nic_addr, PP_LineST);
1210 if (line_status & TENBASET_ON) {
1211 ifmr->ifm_active |= IFM_10_T;
1212 if (sc->chip_type != CS8900) {
1213 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & FDX_ACTIVE)
1214 ifmr->ifm_active |= IFM_FDX;
1215 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & HDX_ACTIVE)
1216 ifmr->ifm_active |= IFM_HDX;
1218 ifmr->ifm_status = IFM_AVALID;
1219 if (line_status & LINK_OK)
1220 ifmr->ifm_status |= IFM_ACTIVE;
1222 if (line_status & AUI_ON) {
1223 cs_writereg(sc->nic_addr, PP_SelfCTL,
1224 cs_readreg(sc->nic_addr, PP_SelfCTL) |
1226 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1227 (cs_readreg(sc->nic_addr, PP_SelfCTL)&HCB1))
1228 ifmr->ifm_active |= IFM_10_2;
1230 ifmr->ifm_active |= IFM_10_5;
1236 cs_mediaset(struct cs_softc *sc, int media)
1240 /* Stop the receiver & transmitter */
1241 cs_writereg(sc->nic_addr, PP_LineCTL,
1242 cs_readreg(sc->nic_addr, PP_LineCTL) &
1243 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1246 if_printf(&sc->arpcom.ac_if, "cs_setmedia(%x)\n", media);
1249 switch (IFM_SUBTYPE(media)) {
1252 if ((error=enable_tp(sc))==0)
1253 error = cs_duplex_auto(sc);
1254 else if ((error=enable_bnc(sc)) != 0)
1255 error = enable_aui(sc);
1258 if ((error=enable_tp(sc)) != 0)
1260 if (media & IFM_FDX)
1262 else if (media & IFM_HDX)
1265 error = cs_duplex_auto(sc);
1268 error = enable_bnc(sc);
1271 error = enable_aui(sc);
1276 * Turn the transmitter & receiver back on
1278 cs_writereg(sc->nic_addr, PP_LineCTL,
1279 cs_readreg( sc->nic_addr, PP_LineCTL ) |
1280 SERIAL_RX_ON | SERIAL_TX_ON);