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>
62 #include <dev/cs/if_csvar.h>
63 #include <dev/cs/if_csreg.h>
66 #define CS_DMA_BUFFER_SIZE 65536
68 #define CS_DMA_BUFFER_SIZE 16384
71 static int cs_recv_delay = 570;
72 SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
74 static void cs_init (void *);
75 static int cs_ioctl (struct ifnet *, u_long, caddr_t);
76 static void cs_start (struct ifnet *);
77 static void cs_stop (struct cs_softc *);
78 static void cs_reset (struct cs_softc *);
79 static void cs_watchdog (struct ifnet *);
81 static int cs_mediachange (struct ifnet *);
82 static void cs_mediastatus (struct ifnet *, struct ifmediareq *);
83 static int cs_mediaset (struct cs_softc *, int);
85 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
86 static void cs_xmit_buf(struct cs_softc*);
87 static int cs_get_packet(struct cs_softc*);
88 static void cs_setmode(struct cs_softc*);
90 static int get_eeprom_data(struct cs_softc *sc, int, int, int *);
91 static int get_eeprom_cksum(int, int, int *);
92 static int wait_eeprom_ready( struct cs_softc *);
93 static void control_dc_dc( struct cs_softc *, int );
94 static int send_test_pkt( struct cs_softc * );
95 static int enable_tp(struct cs_softc *);
96 static int enable_aui(struct cs_softc *);
97 static int enable_bnc(struct cs_softc *);
98 static int cs_duplex_auto(struct cs_softc *);
100 devclass_t cs_devclass;
103 get_eeprom_data( struct cs_softc *sc, int off, int len, int *buffer)
108 printf(CS_NAME":EEPROM data from %x for %x:\n", off,len);
111 for (i=0;i<len;i++) {
112 if (wait_eeprom_ready(sc) < 0) return -1;
113 /* Send command to EEPROM to read */
114 cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
115 if (wait_eeprom_ready(sc)<0)
117 buffer[i] = cs_readreg(sc, PP_EEData);
120 printf("%02x %02x ",(unsigned char)buffer[i],
121 (unsigned char)buffer[i+1]);
132 get_eeprom_cksum(int off, int len, int *buffer)
145 wait_eeprom_ready(struct cs_softc *sc)
147 DELAY ( 30000 ); /* XXX should we do some checks here ? */
152 control_dc_dc(struct cs_softc *sc, int on_not_off)
154 unsigned int self_control = HCB1_ENBL;
156 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
157 self_control |= HCB1;
159 self_control &= ~HCB1;
160 cs_writereg(sc, PP_SelfCTL, self_control);
167 cs_duplex_auto(struct cs_softc *sc)
169 int i, error=0, unit=sc->arpcom.ac_if.if_unit;
171 cs_writereg(sc, PP_AutoNegCTL,
172 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
173 for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
175 printf(CS_NAME"%1d: full/half duplex "
176 "auto negotiation timeout\n", unit);
187 enable_tp(struct cs_softc *sc)
189 int unit = sc->arpcom.ac_if.if_unit;
191 cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
192 control_dc_dc(sc, 0);
195 if ((cs_readreg(sc, PP_LineST) & LINK_OK)==0) {
196 printf(CS_NAME"%1d: failed to enable TP\n", unit);
204 * XXX This was rewritten from Linux driver without any tests.
207 send_test_pkt(struct cs_softc *sc)
209 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
210 0, 46, /* A 46 in network order */
211 0, 0, /* DSAP=0 & SSAP=0 fields */
212 0xf3, 0 /* Control (Test Req + P bit set) */ };
214 u_char ether_address_backup[ETHER_ADDR_LEN];
216 for (i = 0; i < ETHER_ADDR_LEN; i++) {
217 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
220 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_TX_ON);
221 bcopy(test_packet, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
222 bcopy(test_packet+ETHER_ADDR_LEN,
223 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
224 cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
225 cs_outw(sc, TX_LEN_PORT, sizeof(test_packet));
227 /* Wait for chip to allocate memory */
229 if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
230 for (i = 0; i < ETHER_ADDR_LEN; i++) {
231 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
236 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
240 if ((cs_readreg(sc, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
241 for (i = 0; i < ETHER_ADDR_LEN; i++) {
242 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
246 for (i = 0; i < ETHER_ADDR_LEN; i++) {
247 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
253 * XXX This was rewritten from Linux driver without any tests.
256 enable_aui(struct cs_softc *sc)
258 int unit = sc->arpcom.ac_if.if_unit;
260 control_dc_dc(sc, 0);
261 cs_writereg(sc, PP_LineCTL,
262 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
264 if (!send_test_pkt(sc)) {
265 printf(CS_NAME"%1d failed to enable AUI\n", unit);
272 * XXX This was rewritten from Linux driver without any tests.
275 enable_bnc(struct cs_softc *sc)
277 int unit = sc->arpcom.ac_if.if_unit;
279 control_dc_dc(sc, 1);
280 cs_writereg(sc, PP_LineCTL,
281 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
283 if (!send_test_pkt(sc)) {
284 printf(CS_NAME"%1d failed to enable BNC\n", unit);
291 cs_cs89x0_probe(device_t dev)
296 struct cs_softc *sc = device_get_softc(dev);
297 unsigned rev_type = 0;
300 int eeprom_buff[CHKSUM_LEN];
301 int chip_type, pp_isaint, pp_isadma;
303 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
307 sc->nic_addr = rman_get_start(sc->port_res);
309 if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
310 /* Chip not detected. Let's try to reset it */
312 device_printf(dev, "trying to reset the chip.\n");
313 cs_outw(sc, ADD_PORT, PP_SelfCTL);
314 i = cs_inw(sc, DATA_PORT);
315 cs_outw(sc, ADD_PORT, PP_SelfCTL);
316 cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
317 if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
321 for (i = 0; i < 10000; i++) {
322 id = cs_readreg(sc, PP_ChipID);
323 if (id == CHIP_EISA_ID_SIG)
329 rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
330 chip_type = rev_type & ~REVISON_BITS;
331 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
333 sc->chip_type = chip_type;
335 if(chip_type==CS8900) {
336 pp_isaint = PP_CS8900_ISAINT;
337 pp_isadma = PP_CS8900_ISADMA;
338 sc->send_cmd = TX_CS8900_AFTER_ALL;
340 pp_isaint = PP_CS8920_ISAINT;
341 pp_isadma = PP_CS8920_ISADMA;
342 sc->send_cmd = TX_CS8920_AFTER_ALL;
346 * Clear some fields so that fail of EEPROM will left them clean
348 sc->auto_neg_cnf = 0;
353 * If no interrupt specified (or "?"), use what the board tells us.
355 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
358 * Get data from EEPROM
360 if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
361 device_printf(dev, "No EEPROM, assuming defaults.\n");
363 if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
364 device_printf(dev, "EEPROM read failed, "
365 "assuming defaults.\n");
367 if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
368 device_printf(dev, "EEPROM cheksum bad, "
369 "assuming defaults.\n");
372 eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
374 eeprom_buff[ADAPTER_CNF_OFFSET/2];
376 eeprom_buff[ISA_CNF_OFFSET/2];
378 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
379 sc->arpcom.ac_enaddr[i*2]=
381 sc->arpcom.ac_enaddr[i*2+1]=
386 * If no interrupt specified (or "?"),
387 * use what the board tells us.
390 irq = sc->isa_config & INT_NO_MASK;
391 if (chip_type==CS8900) {
410 device_printf(dev, "invalid irq in EEPROM.\n");
414 if (irq>CS8920_NO_INTS) {
415 device_printf(dev, "invalid irq in EEPROM.\n");
423 bus_set_resource(dev, SYS_RES_IRQ, 0,
431 if (chip_type == CS8900) {
449 if (irq > CS8920_NO_INTS) {
456 cs_writereg(sc, pp_isaint, irq);
458 device_printf(dev, "Unknown or invalid irq\n");
466 cs_writereg(sc, pp_isadma, drq);
468 printf( CS_NAME"%1d: incorrect drq\n", unit );
474 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
475 chip_type==CS8900 ? '0' : '2',
476 chip_type==CS8920M ? "M" : "",
478 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
479 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
480 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
482 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
483 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
484 sc->line_ctl = LOW_RX_SQUELCH;
493 * Allocate a port resource with the given resource id.
495 int cs_alloc_port(device_t dev, int rid, int size)
497 struct cs_softc *sc = device_get_softc(dev);
498 struct resource *res;
500 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
501 0ul, ~0ul, size, RF_ACTIVE);
505 sc->port_used = size;
513 * Allocate a memory resource with the given resource id.
515 int cs_alloc_memory(device_t dev, int rid, int size)
517 struct cs_softc *sc = device_get_softc(dev);
518 struct resource *res;
520 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
521 0ul, ~0ul, size, RF_ACTIVE);
533 * Allocate an irq resource with the given resource id.
535 int cs_alloc_irq(device_t dev, int rid, int flags)
537 struct cs_softc *sc = device_get_softc(dev);
538 struct resource *res;
540 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
541 0ul, ~0ul, 1, (RF_ACTIVE | flags));
552 * Release all resources
554 void cs_release_resources(device_t dev)
556 struct cs_softc *sc = device_get_softc(dev);
559 bus_release_resource(dev, SYS_RES_IOPORT,
560 sc->port_rid, sc->port_res);
564 bus_release_resource(dev, SYS_RES_MEMORY,
565 sc->mem_rid, sc->mem_res);
569 bus_release_resource(dev, SYS_RES_IRQ,
570 sc->irq_rid, sc->irq_res);
576 * Install the interface into kernel networking data structures
579 cs_attach(struct cs_softc *sc, int unit, int flags)
582 struct ifnet *ifp = &(sc->arpcom.ac_if);
590 ifp->if_output=ether_output;
591 ifp->if_start=cs_start;
592 ifp->if_ioctl=cs_ioctl;
593 ifp->if_watchdog=cs_watchdog;
594 ifp->if_init=cs_init;
595 ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
600 ifp->if_linkmib=&sc->mibdata;
601 ifp->if_linkmiblen=sizeof sc->mibdata;
604 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
607 * this code still in progress (DMA support)
610 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
611 if (sc->recv_ring == NULL) {
613 "%d: Couldn't allocate memory for NIC\n", unit);
616 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
617 < (128*1024-CS_DMA_BUFFER_SIZE))
618 sc->recv_ring+=16*1024;
622 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
623 if (sc->buffer == NULL) {
624 printf(CS_NAME"%d: Couldn't allocate memory for NIC\n",
630 * Initialize the media structures.
632 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
634 if (sc->adapter_cnf & A_CNF_10B_T) {
635 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
636 if (sc->chip_type != CS8900) {
637 ifmedia_add(&sc->media,
638 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
639 ifmedia_add(&sc->media,
640 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
644 if (sc->adapter_cnf & A_CNF_10B_2)
645 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
647 if (sc->adapter_cnf & A_CNF_AUI)
648 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
650 if (sc->adapter_cnf & A_CNF_MEDIA)
651 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
653 /* Set default media from EEPROM */
654 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
655 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
656 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
657 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
658 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
659 default: printf(CS_NAME"%d: adapter has no media\n", unit);
661 ifmedia_set(&sc->media, media);
662 cs_mediaset(sc, media);
664 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
668 printf(CS_NAME"%d: ethernet address %6D\n",
669 ifp->if_unit, sc->arpcom.ac_enaddr, ":");
675 * Initialize the board
680 struct cs_softc *sc=(struct cs_softc *)xsc;
681 struct ifnet *ifp = &sc->arpcom.ac_if;
684 /* address not known */
685 if (TAILQ_EMPTY(&ifp->if_addrhead)) /* unlikely? XXX */
689 * reset whatchdog timer
697 * Hardware initialization of cs
700 /* Enable receiver and transmitter */
701 cs_writereg(sc, PP_LineCTL,
702 cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
704 /* Configure the receiver mode */
708 * This defines what type of frames will cause interrupts
709 * Bad frames should generate interrupts so that the driver
710 * could track statistics of discarded packets
712 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
714 if (sc->isa_config & STREAM_TRANSFER)
715 rx_cfg |= RX_STREAM_ENBL;
716 cs_writereg(sc, PP_RxCFG, rx_cfg);
717 cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
718 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
719 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
720 cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
721 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
722 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
724 /* Write MAC address into IA filter */
725 for (i=0; i<ETHER_ADDR_LEN/2; i++)
726 cs_writereg(sc, PP_IA + i * 2,
727 sc->arpcom.ac_enaddr[i * 2] |
728 (sc->arpcom.ac_enaddr[i * 2 + 1] << 8) );
731 * Now enable everything
734 #ifdef CS_USE_64K_DMA
735 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
737 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
740 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
743 * Set running and clear output active flags
745 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
746 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
749 * Start sending process
757 * Get the packet from the board and send it to the upper layer
761 cs_get_packet(struct cs_softc *sc)
763 struct ifnet *ifp = &(sc->arpcom.ac_if);
764 int iobase = sc->nic_addr, status, length;
765 struct ether_header *eh;
772 status = cs_inw(sc, RX_FRAME_PORT);
773 length = cs_inw(sc, RX_FRAME_PORT);
776 printf(CS_NAME"%1d: rcvd: stat %x, len %d\n",
777 ifp->if_unit, status, length);
780 if (!(status & RX_OK)) {
782 printf(CS_NAME"%1d: bad pkt stat %x\n", ifp->if_unit, status);
788 MGETHDR(m, M_DONTWAIT, MT_DATA);
792 if (length > MHLEN) {
793 MCLGET(m, M_DONTWAIT);
794 if (!(m->m_flags & M_EXT)) {
800 /* Initialize packet's header info */
801 m->m_pkthdr.rcvif = ifp;
802 m->m_pkthdr.len = length;
806 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
808 eh = mtod(m, struct ether_header *);
811 for (i=0;i<length;i++)
812 printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
816 if (status & (RX_IA | RX_BROADCAST) ||
817 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
818 m->m_pkthdr.len -= sizeof(struct ether_header);
819 m->m_len -= sizeof(struct ether_header);
820 m->m_data += sizeof(struct ether_header);
822 /* Feed the packet to the upper layer */
823 ether_input(ifp, eh, m);
827 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
828 DELAY( cs_recv_delay );
842 struct cs_softc *sc = (struct cs_softc*) arg;
843 struct ifnet *ifp = &(sc->arpcom.ac_if);
847 int unit = ifp->if_unit;
848 printf(CS_NAME"%1d: Interrupt.\n", unit);
851 while ((status=cs_inw(sc, ISQ_PORT))) {
854 printf( CS_NAME"%1d:from ISQ: %04x\n", unit, status );
857 switch (status & ISQ_EVENT_MASK) {
858 case ISQ_RECEIVER_EVENT:
862 case ISQ_TRANSMITTER_EVENT:
867 ifp->if_flags &= ~IFF_OACTIVE;
871 case ISQ_BUFFER_EVENT:
872 if (status & READY_FOR_TX) {
873 ifp->if_flags &= ~IFF_OACTIVE;
877 if (status & TX_UNDERRUN) {
878 ifp->if_flags &= ~IFF_OACTIVE;
884 case ISQ_RX_MISS_EVENT:
885 ifp->if_ierrors+=(status>>6);
888 case ISQ_TX_COL_EVENT:
889 ifp->if_collisions+=(status>>6);
894 if (!(ifp->if_flags & IFF_OACTIVE)) {
900 * Save the data in buffer
904 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
908 unsigned char *data, *buf;
910 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
920 * Find actual data address
922 data = mtod(mp, caddr_t);
924 bcopy((caddr_t) data, (caddr_t) buf, len);
932 cs_xmit_buf( struct cs_softc *sc )
934 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
939 cs_start(struct ifnet *ifp)
943 struct cs_softc *sc = ifp->if_softc;
949 length = sc->buf_len;
951 IF_DEQUEUE( &ifp->if_snd, m );
958 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
961 /* Skip zero-length packets */
967 cs_write_mbufs(sc, m);
977 * Issue a SEND command
979 cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
980 cs_outw(sc, TX_LEN_PORT, length );
983 * If there's no free space in the buffer then leave
984 * this packet for the next time: indicate output active
987 if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
988 ifp->if_timer = sc->buf_len;
990 ifp->if_flags |= IFF_OACTIVE;
997 * Set the watchdog timer in case we never hear
998 * from board again. (I don't know about correct
999 * value for this timeout)
1001 ifp->if_timer = length;
1004 ifp->if_flags |= IFF_OACTIVE;
1010 * Stop everything on the interface
1013 cs_stop(struct cs_softc *sc)
1017 cs_writereg(sc, PP_RxCFG, 0);
1018 cs_writereg(sc, PP_TxCFG, 0);
1019 cs_writereg(sc, PP_BufCFG, 0);
1020 cs_writereg(sc, PP_BusCTL, 0);
1022 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1023 sc->arpcom.ac_if.if_timer = 0;
1029 * Reset the interface
1032 cs_reset(struct cs_softc *sc)
1039 cs_setmode(struct cs_softc *sc)
1041 struct ifnet *ifp = &(sc->arpcom.ac_if);
1044 /* Stop the receiver while changing filters */
1045 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
1047 if (ifp->if_flags & IFF_PROMISC) {
1048 /* Turn on promiscuous mode. */
1049 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1051 if (ifp->if_flags & IFF_MULTICAST) {
1052 /* Allow receiving frames with multicast addresses */
1053 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1054 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1056 * Here the reconfiguration of chip's multicast
1057 * filters should be done but I've no idea about
1058 * hash transformation in this chip. If you can
1059 * add this code or describe me the transformation
1064 * Receive only good frames addressed for us and
1067 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1072 /* Set up the filter */
1073 cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1075 /* Turn on receiver */
1076 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
1080 cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
1082 struct cs_softc *sc=ifp->if_softc;
1083 struct ifreq *ifr = (struct ifreq *)data;
1087 printf(CS_NAME"%d: ioctl(%lx)\n",sc->arpcom.ac_if.if_unit,command);
1096 ether_ioctl(ifp, command, data);
1101 * Switch interface state between "running" and
1102 * "stopped", reflecting the UP flag.
1104 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1105 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1109 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1114 * Promiscuous and/or multicast flags may have changed,
1115 * so reprogram the multicast filter and/or receive mode.
1117 * See note about multicasts in cs_setmode
1125 * Multicast list has changed; set the hardware filter
1128 * See note about multicasts in cs_setmode
1136 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1148 * Device timeout/watchdog routine. Entered if the device neglects to
1149 * generate an interrupt after a transmit has been started on it.
1152 cs_watchdog(struct ifnet *ifp)
1154 struct cs_softc *sc = ifp->if_softc;
1157 log(LOG_ERR, CS_NAME"%d: device timeout\n", ifp->if_unit);
1159 /* Reset the interface */
1160 if (ifp->if_flags & IFF_UP)
1167 cs_mediachange(struct ifnet *ifp)
1169 struct cs_softc *sc = ifp->if_softc;
1170 struct ifmedia *ifm = &sc->media;
1172 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1175 return cs_mediaset(sc, ifm->ifm_media);
1179 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1182 struct cs_softc *sc = ifp->if_softc;
1184 ifmr->ifm_active = IFM_ETHER;
1185 line_status = cs_readreg(sc, PP_LineST);
1186 if (line_status & TENBASET_ON) {
1187 ifmr->ifm_active |= IFM_10_T;
1188 if (sc->chip_type != CS8900) {
1189 if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
1190 ifmr->ifm_active |= IFM_FDX;
1191 if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
1192 ifmr->ifm_active |= IFM_HDX;
1194 ifmr->ifm_status = IFM_AVALID;
1195 if (line_status & LINK_OK)
1196 ifmr->ifm_status |= IFM_ACTIVE;
1198 if (line_status & AUI_ON) {
1199 cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
1201 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1202 (cs_readreg(sc, PP_SelfCTL) & HCB1))
1203 ifmr->ifm_active |= IFM_10_2;
1205 ifmr->ifm_active |= IFM_10_5;
1211 cs_mediaset(struct cs_softc *sc, int media)
1215 /* Stop the receiver & transmitter */
1216 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
1217 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1220 printf(CS_NAME"%d: cs_setmedia(%x)\n",sc->arpcom.ac_if.if_unit,media);
1223 switch (IFM_SUBTYPE(media)) {
1226 if ((error=enable_tp(sc))==0)
1227 error = cs_duplex_auto(sc);
1228 else if ((error=enable_bnc(sc)) != 0)
1229 error = enable_aui(sc);
1232 if ((error=enable_tp(sc)) != 0)
1234 if (media & IFM_FDX)
1236 else if (media & IFM_HDX)
1239 error = cs_duplex_auto(sc);
1242 error = enable_bnc(sc);
1245 error = enable_aui(sc);
1250 * Turn the transmitter & receiver back on
1252 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
1253 SERIAL_RX_ON | SERIAL_TX_ON);