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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 * Device driver for Crystal Semiconductor CS8920 based ethernet
35 * adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.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>
52 #include <sys/module.h>
54 #include <machine/bus.h>
56 #include <machine/resource.h>
59 #include <net/if_arp.h>
60 #include <net/if_media.h>
61 #include <net/ethernet.h>
64 #include <dev/cs/if_csvar.h>
65 #include <dev/cs/if_csreg.h>
68 #define CS_DMA_BUFFER_SIZE 65536
70 #define CS_DMA_BUFFER_SIZE 16384
73 static void cs_init(void *);
74 static int cs_ioctl(struct ifnet *, u_long, caddr_t);
75 static void cs_start(struct ifnet *);
76 static void cs_stop(struct cs_softc *);
77 static void cs_reset(struct cs_softc *);
78 static void cs_watchdog(struct ifnet *);
80 static int cs_mediachange(struct ifnet *);
81 static void cs_mediastatus(struct ifnet *, struct ifmediareq *);
82 static int cs_mediaset(struct cs_softc *, int);
84 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
85 static void cs_xmit_buf(struct cs_softc*);
86 static int cs_get_packet(struct cs_softc*);
87 static void cs_setmode(struct cs_softc*);
89 static int get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *);
90 static int get_eeprom_cksum(int, int, uint16_t *);
91 static int wait_eeprom_ready( struct cs_softc *);
92 static void control_dc_dc( struct cs_softc *, int );
93 static int send_test_pkt( struct cs_softc * );
94 static int enable_tp(struct cs_softc *);
95 static int enable_aui(struct cs_softc *);
96 static int enable_bnc(struct cs_softc *);
97 static int cs_duplex_auto(struct cs_softc *);
99 devclass_t cs_devclass;
102 SYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
105 TUNABLE_INT("hw.cs.debug", &cs_debug);
106 SYSCTL_INT(_hw_cs, OID_AUTO, debug, CTLFLAG_RW,
110 int cs_ignore_cksum_failure = 0;
111 TUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure);
112 SYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW,
113 &cs_ignore_cksum_failure, 0,
114 "ignore checksum errors in cs card EEPROM");
116 static int cs_recv_delay = 570;
117 TUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay);
118 SYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, "");
121 get_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
126 printf(CS_NAME":EEPROM data from %x for %x:\n", off, len);
129 for (i=0; i < len; i++) {
130 if (wait_eeprom_ready(sc) < 0)
132 /* Send command to EEPROM to read */
133 cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
134 if (wait_eeprom_ready(sc) < 0)
136 buffer[i] = cs_readreg(sc, PP_EEData);
139 printf("%02x %02x ",(unsigned char)buffer[i],
140 (unsigned char)buffer[i] >> 8);
151 get_eeprom_cksum(int off, int len, uint16_t *buffer)
156 for (i = 0; i < len; i++)
161 if (cs_ignore_cksum_failure) {
162 printf(CS_NAME": checksum mismatch, ignoring\n");
169 wait_eeprom_ready(struct cs_softc *sc)
171 DELAY(30000); /* XXX should we do some checks here ? */
176 control_dc_dc(struct cs_softc *sc, int on_not_off)
178 unsigned int self_control = HCB1_ENBL;
180 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
181 self_control |= HCB1;
183 self_control &= ~HCB1;
184 cs_writereg(sc, PP_SelfCTL, self_control);
191 cs_duplex_auto(struct cs_softc *sc)
195 cs_writereg(sc, PP_AutoNegCTL,
196 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
197 for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
199 if_printf(&sc->arpcom.ac_if,
200 "full/half duplex auto negotiation timeout\n");
211 enable_tp(struct cs_softc *sc)
214 cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
215 control_dc_dc(sc, 0);
218 if ((cs_readreg(sc, PP_LineST) & LINK_OK)==0) {
219 if_printf(&sc->arpcom.ac_if, "failed to enable TP\n");
227 * XXX This was rewritten from Linux driver without any tests.
230 send_test_pkt(struct cs_softc *sc)
232 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
233 0, 46, /* A 46 in network order */
234 0, 0, /* DSAP=0 & SSAP=0 fields */
235 0xf3, 0 /* Control (Test Req + P bit set) */ };
237 u_char ether_address_backup[ETHER_ADDR_LEN];
239 for (i = 0; i < ETHER_ADDR_LEN; i++)
240 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
242 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_TX_ON);
243 bcopy(test_packet, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
244 bcopy(test_packet+ETHER_ADDR_LEN,
245 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
246 cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
247 cs_outw(sc, TX_LEN_PORT, sizeof(test_packet));
249 /* Wait for chip to allocate memory */
251 if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
252 for (i = 0; i < ETHER_ADDR_LEN; i++)
253 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
257 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
261 for (i = 0; i < ETHER_ADDR_LEN; i++)
262 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
263 if ((cs_readreg(sc, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK)
269 * XXX This was rewritten from Linux driver without any tests.
272 enable_aui(struct cs_softc *sc)
275 control_dc_dc(sc, 0);
276 cs_writereg(sc, PP_LineCTL,
277 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
279 if (!send_test_pkt(sc)) {
280 if_printf(&sc->arpcom.ac_if, "failed to enable AUI\n");
287 * XXX This was rewritten from Linux driver without any tests.
290 enable_bnc(struct cs_softc *sc)
293 control_dc_dc(sc, 1);
294 cs_writereg(sc, PP_LineCTL,
295 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
297 if (!send_test_pkt(sc)) {
298 if_printf(&sc->arpcom.ac_if, "failed to enable BNC\n");
305 cs_cs89x0_probe(device_t dev)
310 struct cs_softc *sc = device_get_softc(dev);
311 unsigned rev_type = 0;
314 uint16_t eeprom_buff[CHKSUM_LEN];
315 int chip_type, pp_isaint, pp_isadma;
317 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
321 sc->nic_addr = rman_get_start(sc->port_res);
323 if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
324 /* Chip not detected. Let's try to reset it */
326 device_printf(dev, "trying to reset the chip.\n");
327 cs_outw(sc, ADD_PORT, PP_SelfCTL);
328 i = cs_inw(sc, DATA_PORT);
329 cs_outw(sc, ADD_PORT, PP_SelfCTL);
330 cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
331 if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
335 for (i = 0; i < 10000; i++) {
336 id = cs_readreg(sc, PP_ChipID);
337 if (id == CHIP_EISA_ID_SIG)
343 rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
344 chip_type = rev_type & ~REVISON_BITS;
345 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
347 sc->chip_type = chip_type;
349 if(chip_type==CS8900) {
350 pp_isaint = PP_CS8900_ISAINT;
351 pp_isadma = PP_CS8900_ISADMA;
352 sc->send_cmd = TX_CS8900_AFTER_ALL;
354 pp_isaint = PP_CS8920_ISAINT;
355 pp_isadma = PP_CS8920_ISADMA;
356 sc->send_cmd = TX_CS8920_AFTER_ALL;
360 * Clear some fields so that fail of EEPROM will left them clean
362 sc->auto_neg_cnf = 0;
367 * If no interrupt specified (or "?"), use what the board tells us.
369 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
372 * Get data from EEPROM
374 if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
375 device_printf(dev, "No EEPROM, assuming defaults.\n");
377 if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
378 device_printf(dev, "EEPROM read failed, "
379 "assuming defaults.\n");
381 if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
382 device_printf(dev, "EEPROM cheksum bad, "
383 "assuming defaults.\n");
386 eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
388 eeprom_buff[ADAPTER_CNF_OFFSET/2];
390 eeprom_buff[ISA_CNF_OFFSET/2];
392 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
393 sc->arpcom.ac_enaddr[i*2]=
395 sc->arpcom.ac_enaddr[i*2+1]=
400 * If no interrupt specified (or "?"),
401 * use what the board tells us.
404 irq = sc->isa_config & INT_NO_MASK;
405 if (chip_type==CS8900) {
424 device_printf(dev, "invalid irq in EEPROM.\n");
428 if (irq>CS8920_NO_INTS) {
429 device_printf(dev, "invalid irq in EEPROM.\n");
437 bus_set_resource(dev, SYS_RES_IRQ, 0,
445 if (chip_type == CS8900) {
463 if (irq > CS8920_NO_INTS) {
470 if (!(sc->flags & CS_NO_IRQ))
471 cs_writereg(sc, pp_isaint, irq);
473 device_printf(dev, "Unknown or invalid irq\n");
481 cs_writereg(sc, pp_isadma, drq);
483 device_printf(dev, "incorrect drq\n",);
489 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
490 chip_type==CS8900 ? '0' : '2',
491 chip_type==CS8920M ? "M" : "",
493 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
494 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
495 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
497 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
498 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
499 sc->line_ctl = LOW_RX_SQUELCH;
508 * Allocate a port resource with the given resource id.
510 int cs_alloc_port(device_t dev, int rid, int size)
512 struct cs_softc *sc = device_get_softc(dev);
513 struct resource *res;
515 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
516 0ul, ~0ul, size, RF_ACTIVE);
520 sc->port_used = size;
528 * Allocate a memory resource with the given resource id.
530 int cs_alloc_memory(device_t dev, int rid, int size)
532 struct cs_softc *sc = device_get_softc(dev);
533 struct resource *res;
535 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
536 0ul, ~0ul, size, RF_ACTIVE);
548 * Allocate an irq resource with the given resource id.
550 int cs_alloc_irq(device_t dev, int rid, int flags)
552 struct cs_softc *sc = device_get_softc(dev);
553 struct resource *res;
555 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
556 (RF_ACTIVE | flags));
567 * Release all resources
569 void cs_release_resources(device_t dev)
571 struct cs_softc *sc = device_get_softc(dev);
574 bus_release_resource(dev, SYS_RES_IOPORT,
575 sc->port_rid, sc->port_res);
579 bus_release_resource(dev, SYS_RES_MEMORY,
580 sc->mem_rid, sc->mem_res);
584 bus_release_resource(dev, SYS_RES_IRQ,
585 sc->irq_rid, sc->irq_res);
591 * Install the interface into kernel networking data structures
594 cs_attach(device_t dev)
597 struct cs_softc *sc = device_get_softc(dev);;
598 struct ifnet *ifp = &(sc->arpcom.ac_if);
603 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
604 ifp->if_start=cs_start;
605 ifp->if_ioctl=cs_ioctl;
606 ifp->if_watchdog=cs_watchdog;
607 ifp->if_init=cs_init;
608 ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
613 ifp->if_linkmib=&sc->mibdata;
614 ifp->if_linkmiblen=sizeof sc->mibdata;
617 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
621 * this code still in progress (DMA support)
624 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
625 if (sc->recv_ring == NULL) {
627 "%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
630 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
631 < (128*1024-CS_DMA_BUFFER_SIZE))
632 sc->recv_ring+=16*1024;
636 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
637 if (sc->buffer == NULL) {
638 if_printf(ifp, "Couldn't allocate memory for NIC\n");
643 * Initialize the media structures.
645 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
647 if (sc->adapter_cnf & A_CNF_10B_T) {
648 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
649 if (sc->chip_type != CS8900) {
650 ifmedia_add(&sc->media,
651 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
652 ifmedia_add(&sc->media,
653 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
657 if (sc->adapter_cnf & A_CNF_10B_2)
658 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
660 if (sc->adapter_cnf & A_CNF_AUI)
661 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
663 if (sc->adapter_cnf & A_CNF_MEDIA)
664 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
666 /* Set default media from EEPROM */
667 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
668 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
669 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
670 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
671 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
673 if_printf(ifp, "no media, assuming 10baseT\n");
674 sc->adapter_cnf |= A_CNF_10B_T;
675 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
676 if (sc->chip_type != CS8900) {
677 ifmedia_add(&sc->media,
678 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
679 ifmedia_add(&sc->media,
680 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
682 media = IFM_ETHER | IFM_10_T;
685 ifmedia_set(&sc->media, media);
686 cs_mediaset(sc, media);
688 ether_ifattach(ifp, sc->arpcom.ac_enaddr);
694 cs_detach(device_t dev)
699 sc = device_get_softc(dev);
700 ifp = &sc->arpcom.ac_if;
703 ifp->if_flags &= ~IFF_RUNNING;
705 cs_release_resources(dev);
710 * Initialize the board
715 struct cs_softc *sc=(struct cs_softc *)xsc;
716 struct ifnet *ifp = &sc->arpcom.ac_if;
720 * reset whatchdog timer
728 * Hardware initialization of cs
731 /* Enable receiver and transmitter */
732 cs_writereg(sc, PP_LineCTL,
733 cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
735 /* Configure the receiver mode */
739 * This defines what type of frames will cause interrupts
740 * Bad frames should generate interrupts so that the driver
741 * could track statistics of discarded packets
743 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
745 if (sc->isa_config & STREAM_TRANSFER)
746 rx_cfg |= RX_STREAM_ENBL;
747 cs_writereg(sc, PP_RxCFG, rx_cfg);
748 cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
749 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
750 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
751 cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
752 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
753 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
755 /* Write MAC address into IA filter */
756 for (i=0; i<ETHER_ADDR_LEN/2; i++)
757 cs_writereg(sc, PP_IA + i * 2,
758 sc->arpcom.ac_enaddr[i * 2] |
759 (sc->arpcom.ac_enaddr[i * 2 + 1] << 8) );
762 * Now enable everything
765 #ifdef CS_USE_64K_DMA
766 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
768 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
771 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
774 * Set running and clear output active flags
776 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
777 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
780 * Start sending process
788 * Get the packet from the board and send it to the upper layer.
791 cs_get_packet(struct cs_softc *sc)
793 struct ifnet *ifp = &(sc->arpcom.ac_if);
794 int iobase = sc->nic_addr, status, length;
795 struct ether_header *eh;
802 status = cs_inw(sc, RX_FRAME_PORT);
803 length = cs_inw(sc, RX_FRAME_PORT);
806 if_printf(ifp, "rcvd: stat %x, len %d\n",
810 if (!(status & RX_OK)) {
812 if_printf(ifp, "bad pkt stat %x\n", status);
818 MGETHDR(m, M_DONTWAIT, MT_DATA);
822 if (length > MHLEN) {
823 MCLGET(m, M_DONTWAIT);
824 if (!(m->m_flags & M_EXT)) {
830 /* Initialize packet's header info */
831 m->m_pkthdr.rcvif = ifp;
832 m->m_pkthdr.len = length;
836 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
838 eh = mtod(m, struct ether_header *);
841 for (i=0;i<length;i++)
842 printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
846 if (status & (RX_IA | RX_BROADCAST) ||
847 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
848 /* Feed the packet to the upper layer */
849 (*ifp->if_input)(ifp, m);
851 if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
852 DELAY(cs_recv_delay);
866 struct cs_softc *sc = (struct cs_softc*) arg;
867 struct ifnet *ifp = &(sc->arpcom.ac_if);
871 if_printf(ifp, "Interrupt.\n");
874 while ((status=cs_inw(sc, ISQ_PORT))) {
877 if_printf(ifp, "from ISQ: %04x\n", status);
880 switch (status & ISQ_EVENT_MASK) {
881 case ISQ_RECEIVER_EVENT:
885 case ISQ_TRANSMITTER_EVENT:
890 ifp->if_flags &= ~IFF_OACTIVE;
894 case ISQ_BUFFER_EVENT:
895 if (status & READY_FOR_TX) {
896 ifp->if_flags &= ~IFF_OACTIVE;
900 if (status & TX_UNDERRUN) {
901 ifp->if_flags &= ~IFF_OACTIVE;
907 case ISQ_RX_MISS_EVENT:
908 ifp->if_ierrors+=(status>>6);
911 case ISQ_TX_COL_EVENT:
912 ifp->if_collisions+=(status>>6);
917 if (!(ifp->if_flags & IFF_OACTIVE)) {
923 * Save the data in buffer
927 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
931 unsigned char *data, *buf;
933 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
943 * Find actual data address
945 data = mtod(mp, caddr_t);
947 bcopy((caddr_t) data, (caddr_t) buf, len);
955 cs_xmit_buf( struct cs_softc *sc )
957 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
962 cs_start(struct ifnet *ifp)
966 struct cs_softc *sc = ifp->if_softc;
972 length = sc->buf_len;
974 IF_DEQUEUE( &ifp->if_snd, m );
981 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
984 /* Skip zero-length packets */
990 cs_write_mbufs(sc, m);
998 * Issue a SEND command
1000 cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
1001 cs_outw(sc, TX_LEN_PORT, length );
1004 * If there's no free space in the buffer then leave
1005 * this packet for the next time: indicate output active
1008 if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
1009 ifp->if_timer = sc->buf_len;
1011 ifp->if_flags |= IFF_OACTIVE;
1018 * Set the watchdog timer in case we never hear
1019 * from board again. (I don't know about correct
1020 * value for this timeout)
1022 ifp->if_timer = length;
1025 ifp->if_flags |= IFF_OACTIVE;
1031 * Stop everything on the interface
1034 cs_stop(struct cs_softc *sc)
1038 cs_writereg(sc, PP_RxCFG, 0);
1039 cs_writereg(sc, PP_TxCFG, 0);
1040 cs_writereg(sc, PP_BufCFG, 0);
1041 cs_writereg(sc, PP_BusCTL, 0);
1043 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1044 sc->arpcom.ac_if.if_timer = 0;
1050 * Reset the interface
1053 cs_reset(struct cs_softc *sc)
1060 cs_setmode(struct cs_softc *sc)
1062 struct ifnet *ifp = &(sc->arpcom.ac_if);
1065 /* Stop the receiver while changing filters */
1066 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
1068 if (ifp->if_flags & IFF_PROMISC) {
1069 /* Turn on promiscuous mode. */
1070 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1072 if (ifp->if_flags & IFF_MULTICAST) {
1073 /* Allow receiving frames with multicast addresses */
1074 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1075 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1077 * Here the reconfiguration of chip's multicast
1078 * filters should be done but I've no idea about
1079 * hash transformation in this chip. If you can
1080 * add this code or describe me the transformation
1085 * Receive only good frames addressed for us and
1088 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1093 /* Set up the filter */
1094 cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1096 /* Turn on receiver */
1097 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
1101 cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
1103 struct cs_softc *sc=ifp->if_softc;
1104 struct ifreq *ifr = (struct ifreq *)data;
1108 if_printf(ifp, "ioctl(%lx)\n", command);
1116 * Switch interface state between "running" and
1117 * "stopped", reflecting the UP flag.
1119 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1120 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1124 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1129 * Promiscuous and/or multicast flags may have changed,
1130 * so reprogram the multicast filter and/or receive mode.
1132 * See note about multicasts in cs_setmode
1140 * Multicast list has changed; set the hardware filter
1143 * See note about multicasts in cs_setmode
1151 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1155 error = ether_ioctl(ifp, command, data);
1164 * Device timeout/watchdog routine. Entered if the device neglects to
1165 * generate an interrupt after a transmit has been started on it.
1168 cs_watchdog(struct ifnet *ifp)
1170 struct cs_softc *sc = ifp->if_softc;
1173 log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
1175 /* Reset the interface */
1176 if (ifp->if_flags & IFF_UP)
1183 cs_mediachange(struct ifnet *ifp)
1185 struct cs_softc *sc = ifp->if_softc;
1186 struct ifmedia *ifm = &sc->media;
1188 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1191 return cs_mediaset(sc, ifm->ifm_media);
1195 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1198 struct cs_softc *sc = ifp->if_softc;
1200 ifmr->ifm_active = IFM_ETHER;
1201 line_status = cs_readreg(sc, PP_LineST);
1202 if (line_status & TENBASET_ON) {
1203 ifmr->ifm_active |= IFM_10_T;
1204 if (sc->chip_type != CS8900) {
1205 if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
1206 ifmr->ifm_active |= IFM_FDX;
1207 if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
1208 ifmr->ifm_active |= IFM_HDX;
1210 ifmr->ifm_status = IFM_AVALID;
1211 if (line_status & LINK_OK)
1212 ifmr->ifm_status |= IFM_ACTIVE;
1214 if (line_status & AUI_ON) {
1215 cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
1217 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1218 (cs_readreg(sc, PP_SelfCTL) & HCB1))
1219 ifmr->ifm_active |= IFM_10_2;
1221 ifmr->ifm_active |= IFM_10_5;
1227 cs_mediaset(struct cs_softc *sc, int media)
1231 /* Stop the receiver & transmitter */
1232 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
1233 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1236 if_printf(&sc->arpcom.ac_if, "cs_setmedia(%x)\n", media);
1239 switch (IFM_SUBTYPE(media)) {
1242 if ((error=enable_tp(sc))==0)
1243 error = cs_duplex_auto(sc);
1244 else if ((error=enable_bnc(sc)) != 0)
1245 error = enable_aui(sc);
1248 if ((error=enable_tp(sc)) != 0)
1250 if (media & IFM_FDX)
1252 else if (media & IFM_HDX)
1255 error = cs_duplex_auto(sc);
1258 error = enable_bnc(sc);
1261 error = enable_aui(sc);
1266 * Turn the transmitter & receiver back on
1268 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
1269 SERIAL_RX_ON | SERIAL_TX_ON);