2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997,1998 Maxim Bolotin and Oleg Sharoiko.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 * Device driver for Crystal Semiconductor CS8920 based ethernet
37 * adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
54 #include <sys/module.h>
56 #include <machine/bus.h>
58 #include <machine/resource.h>
61 #include <net/if_var.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
66 #include <net/ethernet.h>
69 #include <dev/cs/if_csvar.h>
70 #include <dev/cs/if_csreg.h>
73 #define CS_DMA_BUFFER_SIZE 65536
75 #define CS_DMA_BUFFER_SIZE 16384
78 static void cs_init(void *);
79 static void cs_init_locked(struct cs_softc *);
80 static int cs_ioctl(struct ifnet *, u_long, caddr_t);
81 static void cs_start(struct ifnet *);
82 static void cs_start_locked(struct ifnet *);
83 static void cs_stop(struct cs_softc *);
84 static void cs_reset(struct cs_softc *);
85 static void cs_watchdog(void *);
87 static int cs_mediachange(struct ifnet *);
88 static void cs_mediastatus(struct ifnet *, struct ifmediareq *);
89 static int cs_mediaset(struct cs_softc *, int);
91 static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
92 static void cs_xmit_buf(struct cs_softc*);
93 static int cs_get_packet(struct cs_softc*);
94 static void cs_setmode(struct cs_softc*);
96 static int get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *);
97 static int get_eeprom_cksum(int, int, uint16_t *);
98 static int wait_eeprom_ready( struct cs_softc *);
99 static void control_dc_dc( struct cs_softc *, int );
100 static int enable_tp(struct cs_softc *);
101 static int enable_aui(struct cs_softc *);
102 static int enable_bnc(struct cs_softc *);
103 static int cs_duplex_auto(struct cs_softc *);
105 devclass_t cs_devclass;
106 driver_intr_t csintr;
109 static SYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters");
111 int cs_ignore_cksum_failure = 0;
112 SYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RWTUN,
113 &cs_ignore_cksum_failure, 0,
114 "ignore checksum errors in cs card EEPROM");
116 static int cs_recv_delay = 570;
117 SYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RWTUN, &cs_recv_delay, 570, "");
119 static int cs8900_eeint2irq[16] = {
120 10, 11, 12, 5, 255, 255, 255, 255,
121 255, 255, 255, 255, 255, 255, 255, 255
124 static int cs8900_irq2eeint[16] = {
125 255, 255, 255, 255, 255, 3, 255, 255,
126 255, 0, 1, 2, 255, 255, 255, 255
130 get_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer)
135 device_printf(sc->dev, "EEPROM data from %x for %x:\n", off, len);
137 for (i=0; i < len; i++) {
138 if (wait_eeprom_ready(sc) < 0)
140 /* Send command to EEPROM to read */
141 cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD);
142 if (wait_eeprom_ready(sc) < 0)
144 buffer[i] = cs_readreg(sc, PP_EEData);
147 printf("%04x ",buffer[i]);
158 get_eeprom_cksum(int off, int len, uint16_t *buffer)
163 for (i = 0; i < len; i++)
166 if (cksum == 0 || cs_ignore_cksum_failure)
172 wait_eeprom_ready(struct cs_softc *sc)
177 * From the CS8900A datasheet, section 3.5.2:
178 * "Before issuing any command to the EEPROM, the host must wait
179 * for the SIBUSY bit (Register 16, SelfST, bit 8) to clear. After
180 * each command has been issued, the host must wait again for SIBUSY
183 * Before we issue the command, we should be !busy, so that will
184 * be fast. The datasheet suggests that clock out from the part
185 * per word will be on the order of 25us, which is consistent with
186 * the 1MHz serial clock and 16bits... We should never hit 100,
187 * let alone 15,000 here. The original code did an unconditional
188 * 30ms DELAY here. Bad Kharma. cs_readreg takes ~2us.
190 for (i = 0; i < 15000; i++) /* 30ms max */
191 if (!(cs_readreg(sc, PP_SelfST) & SI_BUSY))
197 control_dc_dc(struct cs_softc *sc, int on_not_off)
199 unsigned int self_control = HCB1_ENBL;
201 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
202 self_control |= HCB1;
204 self_control &= ~HCB1;
205 cs_writereg(sc, PP_SelfCTL, self_control);
206 DELAY(500000); /* Bad! */
211 cs_duplex_auto(struct cs_softc *sc)
215 cs_writereg(sc, PP_AutoNegCTL,
216 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE);
217 for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) {
219 device_printf(sc->dev,
220 "full/half duplex auto negotiation timeout\n");
230 enable_tp(struct cs_softc *sc)
233 cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
234 control_dc_dc(sc, 0);
239 enable_aui(struct cs_softc *sc)
242 cs_writereg(sc, PP_LineCTL,
243 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
244 control_dc_dc(sc, 0);
249 enable_bnc(struct cs_softc *sc)
252 cs_writereg(sc, PP_LineCTL,
253 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
254 control_dc_dc(sc, 1);
259 cs_cs89x0_probe(device_t dev)
263 rman_res_t irq, junk;
264 struct cs_softc *sc = device_get_softc(dev);
265 unsigned rev_type = 0;
268 uint16_t eeprom_buff[CHKSUM_LEN];
269 int chip_type, pp_isaint;
272 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
276 if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) {
277 /* Chip not detected. Let's try to reset it */
279 device_printf(dev, "trying to reset the chip.\n");
280 cs_outw(sc, ADD_PORT, PP_SelfCTL);
281 i = cs_inw(sc, DATA_PORT);
282 cs_outw(sc, ADD_PORT, PP_SelfCTL);
283 cs_outw(sc, DATA_PORT, i | POWER_ON_RESET);
284 if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG)
288 for (i = 0; i < 10000; i++) {
289 id = cs_readreg(sc, PP_ChipID);
290 if (id == CHIP_EISA_ID_SIG)
296 rev_type = cs_readreg(sc, PRODUCT_ID_ADD);
297 chip_type = rev_type & ~REVISON_BITS;
298 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
300 sc->chip_type = chip_type;
302 if (chip_type == CS8900) {
303 pp_isaint = PP_CS8900_ISAINT;
304 sc->send_cmd = TX_CS8900_AFTER_ALL;
306 pp_isaint = PP_CS8920_ISAINT;
307 sc->send_cmd = TX_CS8920_AFTER_ALL;
311 * Clear some fields so that fail of EEPROM will left them clean
313 sc->auto_neg_cnf = 0;
318 * If no interrupt specified, use what the board tells us.
320 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
323 * Get data from EEPROM
325 if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) {
326 device_printf(dev, "No EEPROM, assuming defaults.\n");
327 } else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
328 device_printf(dev, "EEPROM read failed, assuming defaults.\n");
329 } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
330 device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n");
332 sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET];
333 sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET];
334 sc->isa_config = eeprom_buff[ISA_CNF_OFFSET];
335 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
336 sc->enaddr[i*2] = eeprom_buff[i];
337 sc->enaddr[i*2+1] = eeprom_buff[i] >> 8;
340 * If no interrupt specified, use what the
344 irq = sc->isa_config & INT_NO_MASK;
346 if (chip_type == CS8900) {
347 irq = cs8900_eeint2irq[irq];
349 if (irq > CS8920_NO_INTS)
353 device_printf(dev, "invalid irq in EEPROM.\n");
357 bus_set_resource(dev, SYS_RES_IRQ, 0,
362 if (!error && !(sc->flags & CS_NO_IRQ)) {
363 if (chip_type == CS8900) {
365 irq = cs8900_irq2eeint[irq];
369 if (irq > CS8920_NO_INTS)
377 device_printf(dev, "Unknown or invalid irq\n");
381 if (!(sc->flags & CS_NO_IRQ))
382 cs_writereg(sc, pp_isaint, irq);
385 device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
386 chip_type == CS8900 ? '0' : '2',
387 chip_type == CS8920M ? "M" : "",
389 (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "",
390 (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "",
391 (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
393 if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
394 (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
395 sc->line_ctl = LOW_RX_SQUELCH;
403 * Allocate a port resource with the given resource id.
406 cs_alloc_port(device_t dev, int rid, int size)
408 struct cs_softc *sc = device_get_softc(dev);
409 struct resource *res;
411 res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
421 * Allocate an irq resource with the given resource id.
424 cs_alloc_irq(device_t dev, int rid)
426 struct cs_softc *sc = device_get_softc(dev);
427 struct resource *res;
429 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
438 * Release all resources
441 cs_release_resources(device_t dev)
443 struct cs_softc *sc = device_get_softc(dev);
446 bus_release_resource(dev, SYS_RES_IOPORT,
447 sc->port_rid, sc->port_res);
451 bus_release_resource(dev, SYS_RES_IRQ,
452 sc->irq_rid, sc->irq_res);
458 * Install the interface into kernel networking data structures
461 cs_attach(device_t dev)
464 struct cs_softc *sc = device_get_softc(dev);
469 ifp = sc->ifp = if_alloc(IFT_ETHER);
471 device_printf(dev, "can not if_alloc()\n");
472 cs_release_resources(dev);
476 mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
478 callout_init_mtx(&sc->timer, &sc->lock, 0);
485 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
486 ifp->if_start=cs_start;
487 ifp->if_ioctl=cs_ioctl;
488 ifp->if_init=cs_init;
489 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
491 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
494 * this code still in progress (DMA support)
497 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
498 if (sc->recv_ring == NULL) {
500 "%s: Couldn't allocate memory for NIC\n", ifp->if_xname);
503 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
504 < (128*1024-CS_DMA_BUFFER_SIZE))
505 sc->recv_ring+=16*1024;
509 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
510 if (sc->buffer == NULL) {
511 device_printf(sc->dev, "Couldn't allocate memory for NIC\n");
513 mtx_destroy(&sc->lock);
514 cs_release_resources(dev);
519 * Initialize the media structures.
521 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
523 if (sc->adapter_cnf & A_CNF_10B_T) {
524 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
525 if (sc->chip_type != CS8900) {
526 ifmedia_add(&sc->media,
527 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
528 ifmedia_add(&sc->media,
529 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
533 if (sc->adapter_cnf & A_CNF_10B_2)
534 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
536 if (sc->adapter_cnf & A_CNF_AUI)
537 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
539 if (sc->adapter_cnf & A_CNF_MEDIA)
540 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
542 /* Set default media from EEPROM */
543 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
544 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
545 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
546 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
547 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
549 device_printf(sc->dev, "no media, assuming 10baseT\n");
550 sc->adapter_cnf |= A_CNF_10B_T;
551 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
552 if (sc->chip_type != CS8900) {
553 ifmedia_add(&sc->media,
554 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
555 ifmedia_add(&sc->media,
556 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
558 media = IFM_ETHER | IFM_10_T;
561 ifmedia_set(&sc->media, media);
562 cs_mediaset(sc, media);
564 ether_ifattach(ifp, sc->enaddr);
566 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
567 NULL, csintr, sc, &sc->irq_handle);
570 free(sc->buffer, M_DEVBUF);
572 mtx_destroy(&sc->lock);
573 cs_release_resources(dev);
581 cs_detach(device_t dev)
586 sc = device_get_softc(dev);
592 callout_drain(&sc->timer);
594 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
595 cs_release_resources(dev);
596 free(sc->buffer, M_DEVBUF);
598 mtx_destroy(&sc->lock);
603 * Initialize the board
608 struct cs_softc *sc=(struct cs_softc *)xsc;
616 cs_init_locked(struct cs_softc *sc)
618 struct ifnet *ifp = sc->ifp;
622 * reset watchdog timer
628 * Hardware initialization of cs
631 /* Enable receiver and transmitter */
632 cs_writereg(sc, PP_LineCTL,
633 cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
635 /* Configure the receiver mode */
639 * This defines what type of frames will cause interrupts
640 * Bad frames should generate interrupts so that the driver
641 * could track statistics of discarded packets
643 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
645 if (sc->isa_config & STREAM_TRANSFER)
646 rx_cfg |= RX_STREAM_ENBL;
647 cs_writereg(sc, PP_RxCFG, rx_cfg);
648 cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL |
649 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
650 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
651 cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL |
652 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
653 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
655 /* Write MAC address into IA filter */
656 for (i=0; i<ETHER_ADDR_LEN/2; i++)
657 cs_writereg(sc, PP_IA + i * 2,
659 (sc->enaddr[i * 2 + 1] << 8) );
662 * Now enable everything
665 #ifdef CS_USE_64K_DMA
666 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
668 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
671 cs_writereg(sc, PP_BusCTL, ENABLE_IRQ);
674 * Set running and clear output active flags
676 sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
677 sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
678 callout_reset(&sc->timer, hz, cs_watchdog, sc);
681 * Start sending process
683 cs_start_locked(ifp);
687 * Get the packet from the board and send it to the upper layer.
690 cs_get_packet(struct cs_softc *sc)
692 struct ifnet *ifp = sc->ifp;
700 status = cs_inw(sc, RX_FRAME_PORT);
701 length = cs_inw(sc, RX_FRAME_PORT);
704 device_printf(sc->dev, "rcvd: stat %x, len %d\n",
708 if (!(status & RX_OK)) {
710 device_printf(sc->dev, "bad pkt stat %x\n", status);
712 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
716 MGETHDR(m, M_NOWAIT, MT_DATA);
720 if (length > MHLEN) {
721 if (!(MCLGET(m, M_NOWAIT))) {
727 /* Initialize packet's header info */
728 m->m_pkthdr.rcvif = ifp;
729 m->m_pkthdr.len = length;
733 bus_read_multi_2(sc->port_res, RX_FRAME_PORT, mtod(m, uint16_t *),
737 for (i=0;i<length;i++)
738 printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
742 if (status & (RX_IA | RX_BROADCAST) ||
743 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
744 /* Feed the packet to the upper layer */
745 (*ifp->if_input)(ifp, m);
746 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
747 if (length == ETHER_MAX_LEN-ETHER_CRC_LEN)
748 DELAY(cs_recv_delay);
762 struct cs_softc *sc = (struct cs_softc*) arg;
763 struct ifnet *ifp = sc->ifp;
767 device_printf(sc->dev, "Interrupt.\n");
771 while ((status=cs_inw(sc, ISQ_PORT))) {
774 device_printf(sc->dev, "from ISQ: %04x\n", status);
777 switch (status & ISQ_EVENT_MASK) {
778 case ISQ_RECEIVER_EVENT:
782 case ISQ_TRANSMITTER_EVENT:
784 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
786 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
787 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
791 case ISQ_BUFFER_EVENT:
792 if (status & READY_FOR_TX) {
793 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
797 if (status & TX_UNDERRUN) {
798 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
800 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
804 case ISQ_RX_MISS_EVENT:
805 if_inc_counter(ifp, IFCOUNTER_IERRORS, status >> 6);
808 case ISQ_TX_COL_EVENT:
809 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, status >> 6);
814 if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
815 cs_start_locked(ifp);
821 * Save the data in buffer
825 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
829 unsigned char *data, *buf;
831 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
841 * Find actual data address
843 data = mtod(mp, caddr_t);
845 bcopy((caddr_t) data, (caddr_t) buf, len);
853 cs_xmit_buf( struct cs_softc *sc )
855 bus_write_multi_2(sc->port_res, TX_FRAME_PORT, (uint16_t *)sc->buffer,
856 (sc->buf_len + 1) >> 1);
861 cs_start(struct ifnet *ifp)
863 struct cs_softc *sc = ifp->if_softc;
866 cs_start_locked(ifp);
871 cs_start_locked(struct ifnet *ifp)
875 struct cs_softc *sc = ifp->if_softc;
879 length = sc->buf_len;
881 IF_DEQUEUE( &ifp->if_snd, m );
887 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
890 /* Skip zero-length packets */
896 cs_write_mbufs(sc, m);
904 * Issue a SEND command
906 cs_outw(sc, TX_CMD_PORT, sc->send_cmd);
907 cs_outw(sc, TX_LEN_PORT, length );
910 * If there's no free space in the buffer then leave
911 * this packet for the next time: indicate output active
914 if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) {
915 sc->tx_timeout = sc->buf_len;
916 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
923 * Set the watchdog timer in case we never hear
924 * from board again. (I don't know about correct
925 * value for this timeout)
927 sc->tx_timeout = length;
929 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
935 * Stop everything on the interface
938 cs_stop(struct cs_softc *sc)
941 CS_ASSERT_LOCKED(sc);
942 cs_writereg(sc, PP_RxCFG, 0);
943 cs_writereg(sc, PP_TxCFG, 0);
944 cs_writereg(sc, PP_BufCFG, 0);
945 cs_writereg(sc, PP_BusCTL, 0);
947 sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
949 callout_stop(&sc->timer);
953 * Reset the interface
956 cs_reset(struct cs_softc *sc)
959 CS_ASSERT_LOCKED(sc);
965 cs_hash_index(struct sockaddr_dl *addr)
972 crc = ether_crc32_le(lla, ETHER_ADDR_LEN);
979 cs_setmode(struct cs_softc *sc)
983 uint16_t port, mask, index;
984 struct ifnet *ifp = sc->ifp;
985 struct ifmultiaddr *ifma;
987 /* Stop the receiver while changing filters */
988 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON);
990 if (ifp->if_flags & IFF_PROMISC) {
991 /* Turn on promiscuous mode. */
992 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
993 } else if (ifp->if_flags & IFF_MULTICAST) {
994 /* Allow receiving frames with multicast addresses */
995 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
996 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
998 /* Start with an empty filter */
999 af[0] = af[1] = af[2] = af[3] = 0x0000;
1001 if (ifp->if_flags & IFF_ALLMULTI) {
1002 /* Accept all multicast frames */
1003 af[0] = af[1] = af[2] = af[3] = 0xffff;
1006 * Set up the filter to only accept multicast
1007 * frames we're interested in.
1009 if_maddr_rlock(ifp);
1010 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1011 struct sockaddr_dl *dl =
1012 (struct sockaddr_dl *)ifma->ifma_addr;
1014 index = cs_hash_index(dl);
1015 port = (u_int16_t) (index >> 4);
1016 mask = (u_int16_t) (1 << (index & 0xf));
1019 if_maddr_runlock(ifp);
1022 cs_writereg(sc, PP_LAF + 0, af[0]);
1023 cs_writereg(sc, PP_LAF + 2, af[1]);
1024 cs_writereg(sc, PP_LAF + 4, af[2]);
1025 cs_writereg(sc, PP_LAF + 6, af[3]);
1028 * Receive only good frames addressed for us and
1031 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1035 /* Set up the filter */
1036 cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1038 /* Turn on receiver */
1039 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON);
1043 cs_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1045 struct cs_softc *sc=ifp->if_softc;
1046 struct ifreq *ifr = (struct ifreq *)data;
1050 if_printf(ifp, "%s command=%lx\n", __func__, command);
1056 * Switch interface state between "running" and
1057 * "stopped", reflecting the UP flag.
1060 if (sc->ifp->if_flags & IFF_UP) {
1061 if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) {
1065 if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) {
1070 * Promiscuous and/or multicast flags may have changed,
1071 * so reprogram the multicast filter and/or receive mode.
1073 * See note about multicasts in cs_setmode
1082 * Multicast list has changed; set the hardware filter
1085 * See note about multicasts in cs_setmode
1095 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1099 error = ether_ioctl(ifp, command, data);
1107 * Device timeout/watchdog routine. Entered if the device neglects to
1108 * generate an interrupt after a transmit has been started on it.
1111 cs_watchdog(void *arg)
1113 struct cs_softc *sc = arg;
1114 struct ifnet *ifp = sc->ifp;
1116 CS_ASSERT_LOCKED(sc);
1117 if (sc->tx_timeout && --sc->tx_timeout == 0) {
1118 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1119 log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
1121 /* Reset the interface */
1122 if (ifp->if_flags & IFF_UP)
1127 callout_reset(&sc->timer, hz, cs_watchdog, sc);
1131 cs_mediachange(struct ifnet *ifp)
1133 struct cs_softc *sc = ifp->if_softc;
1134 struct ifmedia *ifm = &sc->media;
1137 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1141 error = cs_mediaset(sc, ifm->ifm_media);
1147 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1150 struct cs_softc *sc = ifp->if_softc;
1153 ifmr->ifm_active = IFM_ETHER;
1154 line_status = cs_readreg(sc, PP_LineST);
1155 if (line_status & TENBASET_ON) {
1156 ifmr->ifm_active |= IFM_10_T;
1157 if (sc->chip_type != CS8900) {
1158 if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE)
1159 ifmr->ifm_active |= IFM_FDX;
1160 if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE)
1161 ifmr->ifm_active |= IFM_HDX;
1163 ifmr->ifm_status = IFM_AVALID;
1164 if (line_status & LINK_OK)
1165 ifmr->ifm_status |= IFM_ACTIVE;
1167 if (line_status & AUI_ON) {
1168 cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) |
1170 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1171 (cs_readreg(sc, PP_SelfCTL) & HCB1))
1172 ifmr->ifm_active |= IFM_10_2;
1174 ifmr->ifm_active |= IFM_10_5;
1181 cs_mediaset(struct cs_softc *sc, int media)
1185 /* Stop the receiver & transmitter */
1186 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) &
1187 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1190 device_printf(sc->dev, "%s media=%x\n", __func__, media);
1193 switch (IFM_SUBTYPE(media)) {
1197 * This chip makes it a little hard to support this, so treat
1198 * it as IFM_10_T, auto duplex.
1205 if (media & IFM_FDX)
1207 else if (media & IFM_HDX)
1210 error = cs_duplex_auto(sc);
1221 * Turn the transmitter & receiver back on
1223 cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) |
1224 SERIAL_RX_ON | SERIAL_TX_ON);