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
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/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>
50 #include <sys/module.h>
52 #include <machine/bus.h>
54 #include <machine/resource.h>
57 #include <net/if_arp.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
63 #include <isa/isavar.h>
66 #include <net/bridge.h>
69 #include <machine/clock.h>
71 #include <dev/cs/if_csreg.h>
74 #define CS_DMA_BUFFER_SIZE 65536
76 #define CS_DMA_BUFFER_SIZE 16384
80 * cs_softc: per line info and status
84 /* Ethernet common code */
87 /* Configuration words from EEPROM */
88 int auto_neg_cnf; /* AutoNegotitation configuration */
89 int adapter_cnf; /* Adapter configuration */
90 int isa_config; /* ISA configuration */
91 int chip_type; /* Type of chip */
93 struct ifmedia media; /* Media information */
95 int port_rid; /* resource id for port range */
96 int port_used; /* nonzero if ports used */
97 struct resource* port_res; /* resource for port range */
98 int mem_rid; /* resource id for memory range */
99 int mem_used; /* nonzero if memory used */
100 struct resource* mem_res; /* resource for memory range */
101 int irq_rid; /* resource id for irq */
102 struct resource* irq_res; /* resource for irq */
103 void* irq_handle; /* handle for irq handler */
105 int nic_addr; /* Base IO address of card */
111 unsigned char *buffer;
115 static int cs_recv_delay = 570;
116 SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
118 static int cs_isa_probe __P((device_t dev));
119 static int cs_isa_attach __P((device_t dev));
121 static int cs_cs89x0_probe __P((device_t dev));
123 driver_intr_t csintr;
125 static int cs_attach __P((struct cs_softc *, int, int));
127 static void cs_init __P((void *));
128 static int cs_ioctl __P((struct ifnet *, u_long, caddr_t));
129 static void cs_start __P((struct ifnet *));
130 static void cs_stop __P((struct cs_softc *));
131 static void cs_reset __P((struct cs_softc *));
132 static void cs_watchdog __P((struct ifnet *));
134 static int cs_alloc_port(device_t dev, int rid, int size);
135 static int cs_alloc_memory(device_t dev, int rid, int size);
136 static int cs_alloc_irq(device_t dev, int rid, int flags);
137 static void cs_release_resources(device_t dev);
139 static int cs_mediachange __P((struct ifnet *));
140 static void cs_mediastatus __P((struct ifnet *, struct ifmediareq *));
141 static int cs_mediaset __P((struct cs_softc *, int));
143 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
144 static void cs_xmit_buf(struct cs_softc*);
145 static int cs_get_packet(struct cs_softc*);
146 static void cs_setmode(struct cs_softc*);
148 static int get_eeprom_data(struct cs_softc *sc, int, int, int *);
149 static int get_eeprom_cksum(int, int, int *);
150 static int wait_eeprom_ready( struct cs_softc *);
151 static void control_dc_dc( struct cs_softc *, int );
152 static int send_test_pkt( struct cs_softc * );
153 static int enable_tp(struct cs_softc *);
154 static int enable_aui(struct cs_softc *);
155 static int enable_bnc(struct cs_softc *);
156 static int cs_duplex_auto(struct cs_softc *);
158 static device_method_t cs_methods[] = {
159 /* Device interface */
160 DEVMETHOD(device_probe, cs_isa_probe),
161 DEVMETHOD(device_attach, cs_isa_attach),
165 static driver_t cs_driver = {
168 sizeof(struct cs_softc)
171 static devclass_t cs_devclass;
173 DRIVER_MODULE(cs, isa, cs_driver, cs_devclass, 0, 0);
176 get_eeprom_data( struct cs_softc *sc, int off, int len, int *buffer)
181 printf(CS_NAME":EEPROM data from %x for %x:\n", off,len);
184 for (i=0;i<len;i++) {
185 if (wait_eeprom_ready(sc) < 0) return -1;
186 /* Send command to EEPROM to read */
187 cs_writereg(sc->nic_addr, PP_EECMD, (off+i)|EEPROM_READ_CMD );
188 if (wait_eeprom_ready(sc)<0)
190 buffer[i] = cs_readreg (sc->nic_addr, PP_EEData);
193 printf("%02x %02x ",(unsigned char)buffer[i],
194 (unsigned char)buffer[i+1]);
206 get_eeprom_cksum(int off, int len, int *buffer)
219 wait_eeprom_ready(struct cs_softc *sc)
221 DELAY ( 30000 ); /* XXX should we do some checks here ? */
226 control_dc_dc(struct cs_softc *sc, int on_not_off)
228 unsigned int self_control = HCB1_ENBL;
230 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
231 self_control |= HCB1;
233 self_control &= ~HCB1;
234 cs_writereg( sc->nic_addr, PP_SelfCTL, self_control );
241 cs_duplex_auto(struct cs_softc *sc)
243 int i, error=0, unit=sc->arpcom.ac_if.if_unit;
245 cs_writereg(sc->nic_addr, PP_AutoNegCTL,
246 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE );
247 for (i=0; cs_readreg(sc->nic_addr,PP_AutoNegST)&AUTO_NEG_BUSY; i++) {
249 printf(CS_NAME"%1d: full/half duplex "
250 "auto negotiation timeout\n", unit);
261 enable_tp(struct cs_softc *sc)
263 int unit = sc->arpcom.ac_if.if_unit;
265 cs_writereg(sc->nic_addr, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
266 control_dc_dc(sc, 0);
269 if ((cs_readreg(sc->nic_addr, PP_LineST) & LINK_OK)==0) {
270 printf(CS_NAME"%1d: failed to enable TP\n", unit);
278 * XXX This was rewritten from Linux driver without any tests.
281 send_test_pkt(struct cs_softc *sc)
283 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
284 0, 46, /* A 46 in network order */
285 0, 0, /* DSAP=0 & SSAP=0 fields */
286 0xf3, 0 /* Control (Test Req + P bit set) */ };
288 u_char ether_address_backup[ETHER_ADDR_LEN];
290 for (i = 0; i < ETHER_ADDR_LEN; i++) {
291 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
294 cs_writereg(sc->nic_addr, PP_LineCTL,
295 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_TX_ON );
297 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
298 bcopy(test_packet+ETHER_ADDR_LEN,
299 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
300 outw(sc->nic_addr + TX_CMD_PORT, sc->send_cmd);
301 outw(sc->nic_addr + TX_LEN_PORT, sizeof(test_packet));
303 /* Wait for chip to allocate memory */
305 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
306 for (i = 0; i < ETHER_ADDR_LEN; i++) {
307 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
312 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
316 if ((cs_readreg(sc->nic_addr,PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
317 for (i = 0; i < ETHER_ADDR_LEN; i++) {
318 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
322 for (i = 0; i < ETHER_ADDR_LEN; i++) {
323 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
329 * XXX This was rewritten from Linux driver without any tests.
332 enable_aui(struct cs_softc *sc)
334 int unit = sc->arpcom.ac_if.if_unit;
336 control_dc_dc(sc, 0);
337 cs_writereg(sc->nic_addr, PP_LineCTL,
338 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
340 if (!send_test_pkt(sc)) {
341 printf(CS_NAME"%1d failed to enable AUI\n", unit);
348 * XXX This was rewritten from Linux driver without any tests.
351 enable_bnc(struct cs_softc *sc)
353 int unit = sc->arpcom.ac_if.if_unit;
355 control_dc_dc(sc, 1);
356 cs_writereg(sc->nic_addr, PP_LineCTL,
357 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
359 if (!send_test_pkt(sc)) {
360 printf(CS_NAME"%1d failed to enable BNC\n", unit);
367 cs_cs89x0_probe(device_t dev)
375 struct cs_softc *sc = device_get_softc(dev);
377 unsigned rev_type = 0;
379 int eeprom_buff[CHKSUM_LEN];
380 int chip_type, pp_isaint, pp_isadma;
382 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
386 iobase=rman_get_start(sc->port_res);
388 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG) {
389 /* Chip not detected. Let's try to reset it */
391 device_printf(dev, "trying to reset the chip.\n");
392 outw(iobase+ADD_PORT, PP_SelfCTL);
393 i = inw(iobase+DATA_PORT);
394 outw(iobase+ADD_PORT, PP_SelfCTL);
395 outw(iobase+DATA_PORT, i | POWER_ON_RESET);
396 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG)
400 outw(iobase+ADD_PORT, PP_ChipID);
401 if (inw(iobase+DATA_PORT) != CHIP_EISA_ID_SIG)
404 rev_type = cs_readreg(iobase, PRODUCT_ID_ADD);
405 chip_type = rev_type & ~REVISON_BITS;
406 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
408 sc->nic_addr = iobase;
409 sc->chip_type = chip_type;
411 if(chip_type==CS8900) {
412 pp_isaint = PP_CS8900_ISAINT;
413 pp_isadma = PP_CS8900_ISADMA;
414 sc->send_cmd = TX_CS8900_AFTER_ALL;
416 pp_isaint = PP_CS8920_ISAINT;
417 pp_isadma = PP_CS8920_ISADMA;
418 sc->send_cmd = TX_CS8920_AFTER_ALL;
422 * Clear some fields so that fail of EEPROM will left them clean
424 sc->auto_neg_cnf = 0;
429 * If no interrupt specified (or "?"), use what the board tells us.
431 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
434 * Get data from EEPROM
436 if((cs_readreg(iobase, PP_SelfST) & EEPROM_PRESENT) == 0) {
437 device_printf(dev, "No EEPROM, assuming defaults.\n");
439 if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
440 device_printf(dev, "EEPROM read failed, "
441 "assuming defaults.\n");
443 if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
444 device_printf(dev, "EEPROM cheksum bad, "
445 "assuming defaults.\n");
448 eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
450 eeprom_buff[ADAPTER_CNF_OFFSET/2];
452 eeprom_buff[ISA_CNF_OFFSET/2];
454 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
455 sc->arpcom.ac_enaddr[i*2]=
457 sc->arpcom.ac_enaddr[i*2+1]=
462 * If no interrupt specified (or "?"),
463 * use what the board tells us.
466 irq = sc->isa_config & INT_NO_MASK;
467 if (chip_type==CS8900) {
486 device_printf(dev, "invalid irq in EEPROM.\n");
490 if (irq>CS8920_NO_INTS) {
491 device_printf(dev, "invalid irq in EEPROM.\n");
499 bus_set_resource(dev, SYS_RES_IRQ, 0,
507 if (chip_type == CS8900) {
525 if (irq > CS8920_NO_INTS) {
532 cs_writereg(iobase, pp_isaint, irq);
534 device_printf(dev, "Unknown or invalid irq\n");
542 cs_writereg(iobase, pp_isadma, drq);
544 printf( CS_NAME"%1d: incorrect drq\n", unit );
550 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
551 chip_type==CS8900 ? '0' : '2',
552 chip_type==CS8920M ? "M" : "",
554 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
555 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
556 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
558 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
559 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
560 sc->line_ctl = LOW_RX_SQUELCH;
569 * Allocate a port resource with the given resource id.
571 int cs_alloc_port(device_t dev, int rid, int size)
573 struct cs_softc *sc = device_get_softc(dev);
574 struct resource *res;
576 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
577 0ul, ~0ul, size, RF_ACTIVE);
581 sc->port_used = size;
589 * Allocate a memory resource with the given resource id.
591 int cs_alloc_memory(device_t dev, int rid, int size)
593 struct cs_softc *sc = device_get_softc(dev);
594 struct resource *res;
596 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
597 0ul, ~0ul, size, RF_ACTIVE);
609 * Allocate an irq resource with the given resource id.
611 int cs_alloc_irq(device_t dev, int rid, int flags)
613 struct cs_softc *sc = device_get_softc(dev);
614 struct resource *res;
616 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
617 0ul, ~0ul, 1, (RF_ACTIVE | flags));
628 * Release all resources
630 void cs_release_resources(device_t dev)
632 struct cs_softc *sc = device_get_softc(dev);
635 bus_release_resource(dev, SYS_RES_IOPORT,
636 sc->port_rid, sc->port_res);
640 bus_release_resource(dev, SYS_RES_MEMORY,
641 sc->mem_rid, sc->mem_res);
645 bus_release_resource(dev, SYS_RES_IRQ,
646 sc->irq_rid, sc->irq_res);
651 static struct isa_pnp_id cs_ids[] = {
652 { 0x4060630e, NULL }, /* CSC6040 */
653 { 0x10104d24, NULL }, /* IBM EtherJet */
658 * Determine if the device is present
661 cs_isa_probe(device_t dev)
665 struct cs_softc *sc = device_get_softc(dev);
667 bzero(sc, sizeof(struct cs_softc));
669 /* Check isapnp ids */
670 error = ISA_PNP_PROBE(device_get_parent(dev), dev, cs_ids);
672 /* If the card had a PnP ID that didn't match any we know about */
673 if (error == ENXIO) {
677 /* If we had some other problem. */
678 if (!(error == 0 || error == ENOENT)) {
682 error=cs_cs89x0_probe(dev);
686 error = cs_alloc_irq(dev, 0, 0);
688 cs_release_resources(dev);
692 static int cs_isa_attach(device_t dev)
694 struct cs_softc *sc = device_get_softc(dev);
695 int flags = device_get_flags(dev);
698 if (sc->port_used > 0)
699 cs_alloc_port(dev, sc->port_rid, sc->port_used);
701 cs_alloc_memory(dev, sc->mem_rid, sc->mem_used);
702 cs_alloc_irq(dev, sc->irq_rid, 0);
704 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
705 csintr, sc, &sc->irq_handle);
707 cs_release_resources(dev);
711 return cs_attach(sc, device_get_unit(dev), flags);
715 * Install the interface into kernel networking data structures
718 cs_attach(struct cs_softc *sc, int unit, int flags)
721 struct ifnet *ifp = &(sc->arpcom.ac_if);
729 ifp->if_output=ether_output;
730 ifp->if_start=cs_start;
731 ifp->if_ioctl=cs_ioctl;
732 ifp->if_watchdog=cs_watchdog;
733 ifp->if_init=cs_init;
734 ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
739 ifp->if_linkmib=&sc->mibdata;
740 ifp->if_linkmiblen=sizeof sc->mibdata;
743 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
746 * this code still in progress (DMA support)
749 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
750 if (sc->recv_ring == NULL) {
752 "%d: Couldn't allocate memory for NIC\n", unit);
755 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
756 < (128*1024-CS_DMA_BUFFER_SIZE))
757 sc->recv_ring+=16*1024;
761 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
762 if (sc->buffer == NULL) {
763 printf(CS_NAME"%d: Couldn't allocate memory for NIC\n",
769 * Initialize the media structures.
771 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
773 if (sc->adapter_cnf & A_CNF_10B_T) {
774 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
775 if (sc->chip_type != CS8900) {
776 ifmedia_add(&sc->media,
777 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
778 ifmedia_add(&sc->media,
779 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
783 if (sc->adapter_cnf & A_CNF_10B_2)
784 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
786 if (sc->adapter_cnf & A_CNF_AUI)
787 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
789 if (sc->adapter_cnf & A_CNF_MEDIA)
790 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
792 /* Set default media from EEPROM */
793 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
794 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
795 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
796 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
797 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
798 default: printf(CS_NAME"%d: adapter has no media\n", unit);
800 ifmedia_set(&sc->media, media);
801 cs_mediaset(sc, media);
808 printf(CS_NAME"%d: ethernet address %6D\n",
809 ifp->if_unit, sc->arpcom.ac_enaddr, ":");
811 bpfattach(ifp, DLT_EN10MB, sizeof (struct ether_header));
816 * Initialize the board
821 struct cs_softc *sc=(struct cs_softc *)xsc;
822 struct ifnet *ifp = &sc->arpcom.ac_if;
825 /* address not known */
826 if (TAILQ_EMPTY(&ifp->if_addrhead)) /* unlikely? XXX */
830 * reset whatchdog timer
838 * Hardware initialization of cs
841 /* Enable receiver and transmitter */
842 cs_writereg(sc->nic_addr, PP_LineCTL,
843 cs_readreg( sc->nic_addr, PP_LineCTL ) |
844 SERIAL_RX_ON | SERIAL_TX_ON);
846 /* Configure the receiver mode */
850 * This defines what type of frames will cause interrupts
851 * Bad frames should generate interrupts so that the driver
852 * could track statistics of discarded packets
854 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
856 if (sc->isa_config & STREAM_TRANSFER)
857 rx_cfg |= RX_STREAM_ENBL;
858 cs_writereg(sc->nic_addr, PP_RxCFG, rx_cfg);
860 cs_writereg(sc->nic_addr, PP_TxCFG, TX_LOST_CRS_ENBL |
861 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
862 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
864 cs_writereg(sc->nic_addr, PP_BufCFG, READY_FOR_TX_ENBL |
865 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
866 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
868 /* Write MAC address into IA filter */
869 for (i=0; i<ETHER_ADDR_LEN/2; i++)
870 cs_writereg(sc->nic_addr, PP_IA+i*2,
871 sc->arpcom.ac_enaddr[i*2] |
872 (sc->arpcom.ac_enaddr[i*2+1] << 8) );
875 * Now enable everything
878 #ifdef CS_USE_64K_DMA
879 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
882 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
885 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
888 * Set running and clear output active flags
890 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
891 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
894 * Start sending process
902 * Get the packet from the board and send it to the upper layer
906 cs_get_packet(struct cs_softc *sc)
908 struct ifnet *ifp = &(sc->arpcom.ac_if);
909 int iobase = sc->nic_addr, status, length;
910 struct ether_header *eh;
917 status = inw(iobase + RX_FRAME_PORT);
918 length = inw(iobase + RX_FRAME_PORT);
921 printf(CS_NAME"%1d: rcvd: stat %x, len %d\n",
922 ifp->if_unit, status, length);
925 if (!(status & RX_OK)) {
927 printf(CS_NAME"%1d: bad pkt stat %x\n", ifp->if_unit, status);
933 MGETHDR(m, M_DONTWAIT, MT_DATA);
937 if (length > MHLEN) {
938 MCLGET(m, M_DONTWAIT);
939 if (!(m->m_flags & M_EXT)) {
945 /* Initialize packet's header info */
946 m->m_pkthdr.rcvif = ifp;
947 m->m_pkthdr.len = length;
951 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
953 eh = mtod(m, struct ether_header *);
959 for (i=0;i<length;i++)
960 printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
964 if (status & (RX_IA | RX_BROADCAST) ||
965 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
966 m->m_pkthdr.len -= sizeof(struct ether_header);
967 m->m_len -= sizeof(struct ether_header);
968 m->m_data += sizeof(struct ether_header);
970 /* Feed the packet to the upper layer */
971 ether_input(ifp, eh, m);
975 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
976 DELAY( cs_recv_delay );
990 struct cs_softc *sc = (struct cs_softc*) arg;
991 struct ifnet *ifp = &(sc->arpcom.ac_if);
995 int unit = ifp->if_unit;
996 printf(CS_NAME"%1d: Interrupt.\n", unit);
999 while ((status=cs_readword(sc->nic_addr, ISQ_PORT))) {
1002 printf( CS_NAME"%1d:from ISQ: %04x\n", unit, status );
1005 switch (status & ISQ_EVENT_MASK) {
1006 case ISQ_RECEIVER_EVENT:
1010 case ISQ_TRANSMITTER_EVENT:
1015 ifp->if_flags &= ~IFF_OACTIVE;
1019 case ISQ_BUFFER_EVENT:
1020 if (status & READY_FOR_TX) {
1021 ifp->if_flags &= ~IFF_OACTIVE;
1025 if (status & TX_UNDERRUN) {
1026 ifp->if_flags &= ~IFF_OACTIVE;
1032 case ISQ_RX_MISS_EVENT:
1033 ifp->if_ierrors+=(status>>6);
1036 case ISQ_TX_COL_EVENT:
1037 ifp->if_collisions+=(status>>6);
1042 if (!(ifp->if_flags & IFF_OACTIVE)) {
1048 * Save the data in buffer
1052 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
1056 unsigned char *data, *buf;
1058 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
1062 * Ignore empty parts
1068 * Find actual data address
1070 data = mtod(mp, caddr_t);
1072 bcopy((caddr_t) data, (caddr_t) buf, len);
1080 cs_xmit_buf( struct cs_softc *sc )
1082 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
1087 cs_start(struct ifnet *ifp)
1090 struct mbuf *m, *mp;
1091 struct cs_softc *sc = ifp->if_softc;
1097 length = sc->buf_len;
1099 IF_DEQUEUE( &ifp->if_snd, m );
1106 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
1107 length += mp->m_len;
1109 /* Skip zero-length packets */
1115 cs_write_mbufs(sc, m);
1125 * Issue a SEND command
1127 outw(sc->nic_addr+TX_CMD_PORT, sc->send_cmd);
1128 outw(sc->nic_addr+TX_LEN_PORT, length );
1131 * If there's no free space in the buffer then leave
1132 * this packet for the next time: indicate output active
1135 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
1136 ifp->if_timer = sc->buf_len;
1138 ifp->if_flags |= IFF_OACTIVE;
1145 * Set the watchdog timer in case we never hear
1146 * from board again. (I don't know about correct
1147 * value for this timeout)
1149 ifp->if_timer = length;
1152 ifp->if_flags |= IFF_OACTIVE;
1158 * Stop everything on the interface
1161 cs_stop(struct cs_softc *sc)
1165 cs_writereg(sc->nic_addr, PP_RxCFG, 0);
1166 cs_writereg(sc->nic_addr, PP_TxCFG, 0);
1167 cs_writereg(sc->nic_addr, PP_BufCFG, 0);
1168 cs_writereg(sc->nic_addr, PP_BusCTL, 0);
1170 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1171 sc->arpcom.ac_if.if_timer = 0;
1177 * Reset the interface
1180 cs_reset(struct cs_softc *sc)
1187 cs_setmode(struct cs_softc *sc)
1189 struct ifnet *ifp = &(sc->arpcom.ac_if);
1192 /* Stop the receiver while changing filters */
1193 cs_writereg(sc->nic_addr, PP_LineCTL,
1194 cs_readreg(sc->nic_addr, PP_LineCTL) & ~SERIAL_RX_ON);
1196 if (ifp->if_flags & IFF_PROMISC) {
1197 /* Turn on promiscuous mode. */
1198 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1200 if (ifp->if_flags & IFF_MULTICAST) {
1201 /* Allow receiving frames with multicast addresses */
1202 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1203 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1205 * Here the reconfiguration of chip's multicast
1206 * filters should be done but I've no idea about
1207 * hash transformation in this chip. If you can
1208 * add this code or describe me the transformation
1213 * Receive only good frames addressed for us and
1216 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1221 /* Set up the filter */
1222 cs_writereg(sc->nic_addr, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1224 /* Turn on receiver */
1225 cs_writereg(sc->nic_addr, PP_LineCTL,
1226 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_RX_ON);
1230 cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
1232 struct cs_softc *sc=ifp->if_softc;
1233 struct ifreq *ifr = (struct ifreq *)data;
1237 printf(CS_NAME"%d: ioctl(%lx)\n",sc->arpcom.ac_if.if_unit,command);
1246 ether_ioctl(ifp, command, data);
1251 * Switch interface state between "running" and
1252 * "stopped", reflecting the UP flag.
1254 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1255 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1259 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1264 * Promiscuous and/or multicast flags may have changed,
1265 * so reprogram the multicast filter and/or receive mode.
1267 * See note about multicasts in cs_setmode
1275 * Multicast list has changed; set the hardware filter
1278 * See note about multicasts in cs_setmode
1286 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1298 * Device timeout/watchdog routine. Entered if the device neglects to
1299 * generate an interrupt after a transmit has been started on it.
1302 cs_watchdog(struct ifnet *ifp)
1304 struct cs_softc *sc = ifp->if_softc;
1307 log(LOG_ERR, CS_NAME"%d: device timeout\n", ifp->if_unit);
1309 /* Reset the interface */
1310 if (ifp->if_flags & IFF_UP)
1317 cs_mediachange(struct ifnet *ifp)
1319 struct cs_softc *sc = ifp->if_softc;
1320 struct ifmedia *ifm = &sc->media;
1322 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1325 return cs_mediaset(sc, ifm->ifm_media);
1329 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1332 struct cs_softc *sc = ifp->if_softc;
1334 ifmr->ifm_active = IFM_ETHER;
1335 line_status = cs_readreg(sc->nic_addr, PP_LineST);
1336 if (line_status & TENBASET_ON) {
1337 ifmr->ifm_active |= IFM_10_T;
1338 if (sc->chip_type != CS8900) {
1339 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & FDX_ACTIVE)
1340 ifmr->ifm_active |= IFM_FDX;
1341 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & HDX_ACTIVE)
1342 ifmr->ifm_active |= IFM_HDX;
1344 ifmr->ifm_status = IFM_AVALID;
1345 if (line_status & LINK_OK)
1346 ifmr->ifm_status |= IFM_ACTIVE;
1348 if (line_status & AUI_ON) {
1349 cs_writereg(sc->nic_addr, PP_SelfCTL,
1350 cs_readreg(sc->nic_addr, PP_SelfCTL) |
1352 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1353 (cs_readreg(sc->nic_addr, PP_SelfCTL)&HCB1))
1354 ifmr->ifm_active |= IFM_10_2;
1356 ifmr->ifm_active |= IFM_10_5;
1362 cs_mediaset(struct cs_softc *sc, int media)
1366 /* Stop the receiver & transmitter */
1367 cs_writereg(sc->nic_addr, PP_LineCTL,
1368 cs_readreg(sc->nic_addr, PP_LineCTL) &
1369 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1372 printf(CS_NAME"%d: cs_setmedia(%x)\n",sc->arpcom.ac_if.if_unit,media);
1375 switch (IFM_SUBTYPE(media)) {
1378 if ((error=enable_tp(sc))==0)
1379 error = cs_duplex_auto(sc);
1380 else if ((error=enable_bnc(sc)) != 0)
1381 error = enable_aui(sc);
1384 if ((error=enable_tp(sc)) != 0)
1386 if (media & IFM_FDX)
1388 else if (media & IFM_HDX)
1391 error = cs_duplex_auto(sc);
1394 error = enable_bnc(sc);
1397 error = enable_aui(sc);
1402 * Turn the transmitter & receiver back on
1404 cs_writereg(sc->nic_addr, PP_LineCTL,
1405 cs_readreg( sc->nic_addr, PP_LineCTL ) |
1406 SERIAL_RX_ON | SERIAL_TX_ON);