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
30 * $FreeBSD: src/sys/dev/cs/if_cs.c,v 1.19.2.1 2001/01/25 20:13:48 imp Exp $
31 * $DragonFly: src/sys/dev/netif/cs/if_cs.c,v 1.4 2003/11/20 22:07:26 dillon Exp $
33 * Device driver for Crystal Semiconductor CS8920 based ethernet
34 * adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
41 #include <sys/param.h>
42 #include <sys/systm.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>
51 #include <sys/module.h>
53 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <machine/clock.h>
59 #include <net/if_arp.h>
60 #include <net/if_media.h>
61 #include <net/ethernet.h>
68 #define CS_DMA_BUFFER_SIZE 65536
70 #define CS_DMA_BUFFER_SIZE 16384
73 static int cs_recv_delay = 570;
74 SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
76 static void cs_init (void *);
77 static int cs_ioctl (struct ifnet *, u_long, caddr_t);
78 static void cs_start (struct ifnet *);
79 static void cs_stop (struct cs_softc *);
80 static void cs_reset (struct cs_softc *);
81 static void cs_watchdog (struct ifnet *);
83 static int cs_mediachange (struct ifnet *);
84 static void cs_mediastatus (struct ifnet *, struct ifmediareq *);
85 static int cs_mediaset (struct cs_softc *, int);
87 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
88 static void cs_xmit_buf(struct cs_softc*);
89 static int cs_get_packet(struct cs_softc*);
90 static void cs_setmode(struct cs_softc*);
92 static int get_eeprom_data(struct cs_softc *sc, int, int, int *);
93 static int get_eeprom_cksum(int, int, int *);
94 static int wait_eeprom_ready( struct cs_softc *);
95 static void control_dc_dc( struct cs_softc *, int );
96 static int send_test_pkt( struct cs_softc * );
97 static int enable_tp(struct cs_softc *);
98 static int enable_aui(struct cs_softc *);
99 static int enable_bnc(struct cs_softc *);
100 static int cs_duplex_auto(struct cs_softc *);
102 devclass_t cs_devclass;
104 DECLARE_DUMMY_MODULE(if_cs);
107 get_eeprom_data( struct cs_softc *sc, int off, int len, int *buffer)
112 printf(CS_NAME":EEPROM data from %x for %x:\n", off,len);
115 for (i=0;i<len;i++) {
116 if (wait_eeprom_ready(sc) < 0) return -1;
117 /* Send command to EEPROM to read */
118 cs_writereg(sc->nic_addr, PP_EECMD, (off+i)|EEPROM_READ_CMD );
119 if (wait_eeprom_ready(sc)<0)
121 buffer[i] = cs_readreg (sc->nic_addr, PP_EEData);
124 printf("%02x %02x ",(unsigned char)buffer[i],
125 (unsigned char)buffer[i+1]);
137 get_eeprom_cksum(int off, int len, int *buffer)
150 wait_eeprom_ready(struct cs_softc *sc)
152 DELAY ( 30000 ); /* XXX should we do some checks here ? */
157 control_dc_dc(struct cs_softc *sc, int on_not_off)
159 unsigned int self_control = HCB1_ENBL;
161 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
162 self_control |= HCB1;
164 self_control &= ~HCB1;
165 cs_writereg( sc->nic_addr, PP_SelfCTL, self_control );
172 cs_duplex_auto(struct cs_softc *sc)
174 int i, error=0, unit=sc->arpcom.ac_if.if_unit;
176 cs_writereg(sc->nic_addr, PP_AutoNegCTL,
177 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE );
178 for (i=0; cs_readreg(sc->nic_addr,PP_AutoNegST)&AUTO_NEG_BUSY; i++) {
180 printf(CS_NAME"%1d: full/half duplex "
181 "auto negotiation timeout\n", unit);
192 enable_tp(struct cs_softc *sc)
194 int unit = sc->arpcom.ac_if.if_unit;
196 cs_writereg(sc->nic_addr, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
197 control_dc_dc(sc, 0);
200 if ((cs_readreg(sc->nic_addr, PP_LineST) & LINK_OK)==0) {
201 printf(CS_NAME"%1d: failed to enable TP\n", unit);
209 * XXX This was rewritten from Linux driver without any tests.
212 send_test_pkt(struct cs_softc *sc)
214 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
215 0, 46, /* A 46 in network order */
216 0, 0, /* DSAP=0 & SSAP=0 fields */
217 0xf3, 0 /* Control (Test Req + P bit set) */ };
219 u_char ether_address_backup[ETHER_ADDR_LEN];
221 for (i = 0; i < ETHER_ADDR_LEN; i++) {
222 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
225 cs_writereg(sc->nic_addr, PP_LineCTL,
226 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_TX_ON );
228 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
229 bcopy(test_packet+ETHER_ADDR_LEN,
230 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
231 outw(sc->nic_addr + TX_CMD_PORT, sc->send_cmd);
232 outw(sc->nic_addr + TX_LEN_PORT, sizeof(test_packet));
234 /* Wait for chip to allocate memory */
236 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
237 for (i = 0; i < ETHER_ADDR_LEN; i++) {
238 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
243 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
247 if ((cs_readreg(sc->nic_addr,PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
248 for (i = 0; i < ETHER_ADDR_LEN; i++) {
249 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
253 for (i = 0; i < ETHER_ADDR_LEN; i++) {
254 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
260 * XXX This was rewritten from Linux driver without any tests.
263 enable_aui(struct cs_softc *sc)
265 int unit = sc->arpcom.ac_if.if_unit;
267 control_dc_dc(sc, 0);
268 cs_writereg(sc->nic_addr, PP_LineCTL,
269 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
271 if (!send_test_pkt(sc)) {
272 printf(CS_NAME"%1d failed to enable AUI\n", unit);
279 * XXX This was rewritten from Linux driver without any tests.
282 enable_bnc(struct cs_softc *sc)
284 int unit = sc->arpcom.ac_if.if_unit;
286 control_dc_dc(sc, 1);
287 cs_writereg(sc->nic_addr, PP_LineCTL,
288 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
290 if (!send_test_pkt(sc)) {
291 printf(CS_NAME"%1d failed to enable BNC\n", unit);
298 cs_cs89x0_probe(device_t dev)
306 struct cs_softc *sc = device_get_softc(dev);
308 unsigned rev_type = 0;
310 int eeprom_buff[CHKSUM_LEN];
311 int chip_type, pp_isaint, pp_isadma;
313 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
317 iobase=rman_get_start(sc->port_res);
319 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG) {
320 /* Chip not detected. Let's try to reset it */
322 device_printf(dev, "trying to reset the chip.\n");
323 outw(iobase+ADD_PORT, PP_SelfCTL);
324 i = inw(iobase+DATA_PORT);
325 outw(iobase+ADD_PORT, PP_SelfCTL);
326 outw(iobase+DATA_PORT, i | POWER_ON_RESET);
327 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG)
331 outw(iobase+ADD_PORT, PP_ChipID);
332 if (inw(iobase+DATA_PORT) != CHIP_EISA_ID_SIG)
335 rev_type = cs_readreg(iobase, PRODUCT_ID_ADD);
336 chip_type = rev_type & ~REVISON_BITS;
337 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
339 sc->nic_addr = iobase;
340 sc->chip_type = chip_type;
342 if(chip_type==CS8900) {
343 pp_isaint = PP_CS8900_ISAINT;
344 pp_isadma = PP_CS8900_ISADMA;
345 sc->send_cmd = TX_CS8900_AFTER_ALL;
347 pp_isaint = PP_CS8920_ISAINT;
348 pp_isadma = PP_CS8920_ISADMA;
349 sc->send_cmd = TX_CS8920_AFTER_ALL;
353 * Clear some fields so that fail of EEPROM will left them clean
355 sc->auto_neg_cnf = 0;
360 * If no interrupt specified (or "?"), use what the board tells us.
362 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
365 * Get data from EEPROM
367 if((cs_readreg(iobase, PP_SelfST) & EEPROM_PRESENT) == 0) {
368 device_printf(dev, "No EEPROM, assuming defaults.\n");
370 if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
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,
438 if (chip_type == CS8900) {
456 if (irq > CS8920_NO_INTS) {
463 cs_writereg(iobase, pp_isaint, irq);
465 device_printf(dev, "Unknown or invalid irq\n");
473 cs_writereg(iobase, pp_isadma, drq);
475 printf( CS_NAME"%1d: incorrect drq\n", unit );
481 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
482 chip_type==CS8900 ? '0' : '2',
483 chip_type==CS8920M ? "M" : "",
485 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
486 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
487 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
489 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
490 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
491 sc->line_ctl = LOW_RX_SQUELCH;
500 * Allocate a port resource with the given resource id.
502 int cs_alloc_port(device_t dev, int rid, int size)
504 struct cs_softc *sc = device_get_softc(dev);
505 struct resource *res;
507 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
508 0ul, ~0ul, size, RF_ACTIVE);
512 sc->port_used = size;
520 * Allocate a memory resource with the given resource id.
522 int cs_alloc_memory(device_t dev, int rid, int size)
524 struct cs_softc *sc = device_get_softc(dev);
525 struct resource *res;
527 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
528 0ul, ~0ul, size, RF_ACTIVE);
540 * Allocate an irq resource with the given resource id.
542 int cs_alloc_irq(device_t dev, int rid, int flags)
544 struct cs_softc *sc = device_get_softc(dev);
545 struct resource *res;
547 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
548 0ul, ~0ul, 1, (RF_ACTIVE | flags));
559 * Release all resources
561 void cs_release_resources(device_t dev)
563 struct cs_softc *sc = device_get_softc(dev);
566 bus_release_resource(dev, SYS_RES_IOPORT,
567 sc->port_rid, sc->port_res);
571 bus_release_resource(dev, SYS_RES_MEMORY,
572 sc->mem_rid, sc->mem_res);
576 bus_release_resource(dev, SYS_RES_IRQ,
577 sc->irq_rid, sc->irq_res);
583 * Install the interface into kernel networking data structures
586 cs_attach(struct cs_softc *sc, int unit, int flags)
589 struct ifnet *ifp = &(sc->arpcom.ac_if);
597 ifp->if_output=ether_output;
598 ifp->if_start=cs_start;
599 ifp->if_ioctl=cs_ioctl;
600 ifp->if_watchdog=cs_watchdog;
601 ifp->if_init=cs_init;
602 ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
607 ifp->if_linkmib=&sc->mibdata;
608 ifp->if_linkmiblen=sizeof sc->mibdata;
611 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
614 * this code still in progress (DMA support)
617 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
618 if (sc->recv_ring == NULL) {
620 "%d: Couldn't allocate memory for NIC\n", unit);
623 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
624 < (128*1024-CS_DMA_BUFFER_SIZE))
625 sc->recv_ring+=16*1024;
629 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
630 if (sc->buffer == NULL) {
631 printf(CS_NAME"%d: Couldn't allocate memory for NIC\n",
637 * Initialize the media structures.
639 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
641 if (sc->adapter_cnf & A_CNF_10B_T) {
642 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
643 if (sc->chip_type != CS8900) {
644 ifmedia_add(&sc->media,
645 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
646 ifmedia_add(&sc->media,
647 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
651 if (sc->adapter_cnf & A_CNF_10B_2)
652 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
654 if (sc->adapter_cnf & A_CNF_AUI)
655 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
657 if (sc->adapter_cnf & A_CNF_MEDIA)
658 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
660 /* Set default media from EEPROM */
661 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
662 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
663 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
664 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
665 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
666 default: printf(CS_NAME"%d: adapter has no media\n", unit);
668 ifmedia_set(&sc->media, media);
669 cs_mediaset(sc, media);
671 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
675 printf(CS_NAME"%d: ethernet address %6D\n",
676 ifp->if_unit, sc->arpcom.ac_enaddr, ":");
682 * Initialize the board
687 struct cs_softc *sc=(struct cs_softc *)xsc;
688 struct ifnet *ifp = &sc->arpcom.ac_if;
691 /* address not known */
692 if (TAILQ_EMPTY(&ifp->if_addrhead)) /* unlikely? XXX */
696 * reset whatchdog timer
704 * Hardware initialization of cs
707 /* Enable receiver and transmitter */
708 cs_writereg(sc->nic_addr, PP_LineCTL,
709 cs_readreg( sc->nic_addr, PP_LineCTL ) |
710 SERIAL_RX_ON | SERIAL_TX_ON);
712 /* Configure the receiver mode */
716 * This defines what type of frames will cause interrupts
717 * Bad frames should generate interrupts so that the driver
718 * could track statistics of discarded packets
720 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
722 if (sc->isa_config & STREAM_TRANSFER)
723 rx_cfg |= RX_STREAM_ENBL;
724 cs_writereg(sc->nic_addr, PP_RxCFG, rx_cfg);
726 cs_writereg(sc->nic_addr, PP_TxCFG, TX_LOST_CRS_ENBL |
727 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
728 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
730 cs_writereg(sc->nic_addr, PP_BufCFG, READY_FOR_TX_ENBL |
731 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
732 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
734 /* Write MAC address into IA filter */
735 for (i=0; i<ETHER_ADDR_LEN/2; i++)
736 cs_writereg(sc->nic_addr, PP_IA+i*2,
737 sc->arpcom.ac_enaddr[i*2] |
738 (sc->arpcom.ac_enaddr[i*2+1] << 8) );
741 * Now enable everything
744 #ifdef CS_USE_64K_DMA
745 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
748 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
751 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
754 * Set running and clear output active flags
756 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
757 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
760 * Start sending process
768 * Get the packet from the board and send it to the upper layer
772 cs_get_packet(struct cs_softc *sc)
774 struct ifnet *ifp = &(sc->arpcom.ac_if);
775 int iobase = sc->nic_addr, status, length;
776 struct ether_header *eh;
783 status = inw(iobase + RX_FRAME_PORT);
784 length = inw(iobase + RX_FRAME_PORT);
787 printf(CS_NAME"%1d: rcvd: stat %x, len %d\n",
788 ifp->if_unit, status, length);
791 if (!(status & RX_OK)) {
793 printf(CS_NAME"%1d: bad pkt stat %x\n", ifp->if_unit, status);
799 MGETHDR(m, M_DONTWAIT, MT_DATA);
803 if (length > MHLEN) {
804 MCLGET(m, M_DONTWAIT);
805 if (!(m->m_flags & M_EXT)) {
811 /* Initialize packet's header info */
812 m->m_pkthdr.rcvif = ifp;
813 m->m_pkthdr.len = length;
817 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
819 eh = mtod(m, struct ether_header *);
822 for (i=0;i<length;i++)
823 printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
827 if (status & (RX_IA | RX_BROADCAST) ||
828 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
829 m->m_pkthdr.len -= sizeof(struct ether_header);
830 m->m_len -= sizeof(struct ether_header);
831 m->m_data += sizeof(struct ether_header);
833 /* Feed the packet to the upper layer */
834 ether_input(ifp, eh, m);
838 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
839 DELAY( cs_recv_delay );
853 struct cs_softc *sc = (struct cs_softc*) arg;
854 struct ifnet *ifp = &(sc->arpcom.ac_if);
858 int unit = ifp->if_unit;
859 printf(CS_NAME"%1d: Interrupt.\n", unit);
862 while ((status=cs_readword(sc->nic_addr, ISQ_PORT))) {
865 printf( CS_NAME"%1d:from ISQ: %04x\n", unit, status );
868 switch (status & ISQ_EVENT_MASK) {
869 case ISQ_RECEIVER_EVENT:
873 case ISQ_TRANSMITTER_EVENT:
878 ifp->if_flags &= ~IFF_OACTIVE;
882 case ISQ_BUFFER_EVENT:
883 if (status & READY_FOR_TX) {
884 ifp->if_flags &= ~IFF_OACTIVE;
888 if (status & TX_UNDERRUN) {
889 ifp->if_flags &= ~IFF_OACTIVE;
895 case ISQ_RX_MISS_EVENT:
896 ifp->if_ierrors+=(status>>6);
899 case ISQ_TX_COL_EVENT:
900 ifp->if_collisions+=(status>>6);
905 if (!(ifp->if_flags & IFF_OACTIVE)) {
911 * Save the data in buffer
915 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
919 unsigned char *data, *buf;
921 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
931 * Find actual data address
933 data = mtod(mp, caddr_t);
935 bcopy((caddr_t) data, (caddr_t) buf, len);
943 cs_xmit_buf( struct cs_softc *sc )
945 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
950 cs_start(struct ifnet *ifp)
954 struct cs_softc *sc = ifp->if_softc;
960 length = sc->buf_len;
962 IF_DEQUEUE( &ifp->if_snd, m );
969 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
972 /* Skip zero-length packets */
978 cs_write_mbufs(sc, m);
988 * Issue a SEND command
990 outw(sc->nic_addr+TX_CMD_PORT, sc->send_cmd);
991 outw(sc->nic_addr+TX_LEN_PORT, length );
994 * If there's no free space in the buffer then leave
995 * this packet for the next time: indicate output active
998 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
999 ifp->if_timer = sc->buf_len;
1001 ifp->if_flags |= IFF_OACTIVE;
1008 * Set the watchdog timer in case we never hear
1009 * from board again. (I don't know about correct
1010 * value for this timeout)
1012 ifp->if_timer = length;
1015 ifp->if_flags |= IFF_OACTIVE;
1021 * Stop everything on the interface
1024 cs_stop(struct cs_softc *sc)
1028 cs_writereg(sc->nic_addr, PP_RxCFG, 0);
1029 cs_writereg(sc->nic_addr, PP_TxCFG, 0);
1030 cs_writereg(sc->nic_addr, PP_BufCFG, 0);
1031 cs_writereg(sc->nic_addr, PP_BusCTL, 0);
1033 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1034 sc->arpcom.ac_if.if_timer = 0;
1040 * Reset the interface
1043 cs_reset(struct cs_softc *sc)
1050 cs_setmode(struct cs_softc *sc)
1052 struct ifnet *ifp = &(sc->arpcom.ac_if);
1055 /* Stop the receiver while changing filters */
1056 cs_writereg(sc->nic_addr, PP_LineCTL,
1057 cs_readreg(sc->nic_addr, PP_LineCTL) & ~SERIAL_RX_ON);
1059 if (ifp->if_flags & IFF_PROMISC) {
1060 /* Turn on promiscuous mode. */
1061 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1063 if (ifp->if_flags & IFF_MULTICAST) {
1064 /* Allow receiving frames with multicast addresses */
1065 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1066 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1068 * Here the reconfiguration of chip's multicast
1069 * filters should be done but I've no idea about
1070 * hash transformation in this chip. If you can
1071 * add this code or describe me the transformation
1076 * Receive only good frames addressed for us and
1079 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1084 /* Set up the filter */
1085 cs_writereg(sc->nic_addr, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1087 /* Turn on receiver */
1088 cs_writereg(sc->nic_addr, PP_LineCTL,
1089 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_RX_ON);
1093 cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
1095 struct cs_softc *sc=ifp->if_softc;
1096 struct ifreq *ifr = (struct ifreq *)data;
1100 printf(CS_NAME"%d: ioctl(%lx)\n",sc->arpcom.ac_if.if_unit,command);
1109 ether_ioctl(ifp, command, data);
1114 * Switch interface state between "running" and
1115 * "stopped", reflecting the UP flag.
1117 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1118 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1122 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1127 * Promiscuous and/or multicast flags may have changed,
1128 * so reprogram the multicast filter and/or receive mode.
1130 * See note about multicasts in cs_setmode
1138 * Multicast list has changed; set the hardware filter
1141 * See note about multicasts in cs_setmode
1149 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1161 * Device timeout/watchdog routine. Entered if the device neglects to
1162 * generate an interrupt after a transmit has been started on it.
1165 cs_watchdog(struct ifnet *ifp)
1167 struct cs_softc *sc = ifp->if_softc;
1170 log(LOG_ERR, CS_NAME"%d: device timeout\n", ifp->if_unit);
1172 /* Reset the interface */
1173 if (ifp->if_flags & IFF_UP)
1180 cs_mediachange(struct ifnet *ifp)
1182 struct cs_softc *sc = ifp->if_softc;
1183 struct ifmedia *ifm = &sc->media;
1185 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1188 return cs_mediaset(sc, ifm->ifm_media);
1192 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1195 struct cs_softc *sc = ifp->if_softc;
1197 ifmr->ifm_active = IFM_ETHER;
1198 line_status = cs_readreg(sc->nic_addr, PP_LineST);
1199 if (line_status & TENBASET_ON) {
1200 ifmr->ifm_active |= IFM_10_T;
1201 if (sc->chip_type != CS8900) {
1202 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & FDX_ACTIVE)
1203 ifmr->ifm_active |= IFM_FDX;
1204 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & HDX_ACTIVE)
1205 ifmr->ifm_active |= IFM_HDX;
1207 ifmr->ifm_status = IFM_AVALID;
1208 if (line_status & LINK_OK)
1209 ifmr->ifm_status |= IFM_ACTIVE;
1211 if (line_status & AUI_ON) {
1212 cs_writereg(sc->nic_addr, PP_SelfCTL,
1213 cs_readreg(sc->nic_addr, PP_SelfCTL) |
1215 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1216 (cs_readreg(sc->nic_addr, PP_SelfCTL)&HCB1))
1217 ifmr->ifm_active |= IFM_10_2;
1219 ifmr->ifm_active |= IFM_10_5;
1225 cs_mediaset(struct cs_softc *sc, int media)
1229 /* Stop the receiver & transmitter */
1230 cs_writereg(sc->nic_addr, PP_LineCTL,
1231 cs_readreg(sc->nic_addr, PP_LineCTL) &
1232 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1235 printf(CS_NAME"%d: cs_setmedia(%x)\n",sc->arpcom.ac_if.if_unit,media);
1238 switch (IFM_SUBTYPE(media)) {
1241 if ((error=enable_tp(sc))==0)
1242 error = cs_duplex_auto(sc);
1243 else if ((error=enable_bnc(sc)) != 0)
1244 error = enable_aui(sc);
1247 if ((error=enable_tp(sc)) != 0)
1249 if (media & IFM_FDX)
1251 else if (media & IFM_HDX)
1254 error = cs_duplex_auto(sc);
1257 error = enable_bnc(sc);
1260 error = enable_aui(sc);
1265 * Turn the transmitter & receiver back on
1267 cs_writereg(sc->nic_addr, PP_LineCTL,
1268 cs_readreg( sc->nic_addr, PP_LineCTL ) |
1269 SERIAL_RX_ON | SERIAL_TX_ON);