if_cs.c revision 179620
1139749Simp/*- 237785Smsmith * Copyright (c) 1997,1998 Maxim Bolotin and Oleg Sharoiko. 337785Smsmith * All rights reserved. 437785Smsmith * 537785Smsmith * Redistribution and use in source and binary forms, with or without 637785Smsmith * modification, are permitted provided that the following conditions 737785Smsmith * are met: 837785Smsmith * 1. Redistributions of source code must retain the above copyright 937785Smsmith * notice unmodified, this list of conditions, and the following 1037785Smsmith * disclaimer. 1137785Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1237785Smsmith * notice, this list of conditions and the following disclaimer in the 1337785Smsmith * documentation and/or other materials provided with the distribution. 1437785Smsmith * 1537785Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1637785Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1737785Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1837785Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1937785Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2037785Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2137785Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2237785Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2337785Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2437785Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2537785Smsmith * SUCH DAMAGE. 2637785Smsmith * 2737785Smsmith */ 2837785Smsmith 29119418Sobrien#include <sys/cdefs.h> 30119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/cs/if_cs.c 179620 2008-06-06 17:22:07Z imp $"); 31119418Sobrien 3237785Smsmith/* 3337785Smsmith * 3437785Smsmith * Device driver for Crystal Semiconductor CS8920 based ethernet 3537785Smsmith * adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997 3637785Smsmith */ 3737785Smsmith 3858816Simp/* 3958816Simp#define CS_DEBUG 4058816Simp */ 4137785Smsmith 4237785Smsmith#include <sys/param.h> 4337785Smsmith#include <sys/systm.h> 4437785Smsmith#include <sys/malloc.h> 4558816Simp#include <sys/mbuf.h> 4658816Simp#include <sys/socket.h> 4737785Smsmith#include <sys/sockio.h> 4837785Smsmith#include <sys/kernel.h> 4938592Smsmith#include <sys/sysctl.h> 5037785Smsmith#include <sys/syslog.h> 5137785Smsmith 5258816Simp#include <sys/module.h> 5358816Simp#include <sys/bus.h> 5458816Simp#include <machine/bus.h> 5558816Simp#include <sys/rman.h> 5658816Simp#include <machine/resource.h> 5758816Simp 5837785Smsmith#include <net/if.h> 5937785Smsmith#include <net/if_arp.h> 60156559Sphilip#include <net/if_dl.h> 6137785Smsmith#include <net/if_media.h> 62147256Sbrooks#include <net/if_types.h> 6337785Smsmith#include <net/ethernet.h> 6437785Smsmith#include <net/bpf.h> 6537785Smsmith 6672940Simp#include <dev/cs/if_csvar.h> 6758816Simp#include <dev/cs/if_csreg.h> 6837785Smsmith 6937785Smsmith#ifdef CS_USE_64K_DMA 7037785Smsmith#define CS_DMA_BUFFER_SIZE 65536 7137785Smsmith#else 7237785Smsmith#define CS_DMA_BUFFER_SIZE 16384 7337785Smsmith#endif 7437785Smsmith 75122024Simpstatic void cs_init(void *); 76179560Sjhbstatic void cs_init_locked(struct cs_softc *); 77122024Simpstatic int cs_ioctl(struct ifnet *, u_long, caddr_t); 78122024Simpstatic void cs_start(struct ifnet *); 79179560Sjhbstatic void cs_start_locked(struct ifnet *); 80122024Simpstatic void cs_stop(struct cs_softc *); 81122024Simpstatic void cs_reset(struct cs_softc *); 82179560Sjhbstatic void cs_watchdog(void *); 8338592Smsmith 84122024Simpstatic int cs_mediachange(struct ifnet *); 85122024Simpstatic void cs_mediastatus(struct ifnet *, struct ifmediareq *); 86122024Simpstatic int cs_mediaset(struct cs_softc *, int); 8758816Simp 8837785Smsmithstatic void cs_write_mbufs(struct cs_softc*, struct mbuf*); 8937785Smsmithstatic void cs_xmit_buf(struct cs_softc*); 9037785Smsmithstatic int cs_get_packet(struct cs_softc*); 9137785Smsmithstatic void cs_setmode(struct cs_softc*); 9237785Smsmith 93122024Simpstatic int get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *); 94122024Simpstatic int get_eeprom_cksum(int, int, uint16_t *); 9537785Smsmithstatic int wait_eeprom_ready( struct cs_softc *); 9637785Smsmithstatic void control_dc_dc( struct cs_softc *, int ); 9737785Smsmithstatic int enable_tp(struct cs_softc *); 9837785Smsmithstatic int enable_aui(struct cs_softc *); 9937785Smsmithstatic int enable_bnc(struct cs_softc *); 10037785Smsmithstatic int cs_duplex_auto(struct cs_softc *); 10137785Smsmith 10271316Simpdevclass_t cs_devclass; 103179597Simpdriver_intr_t csintr; 10437785Smsmith 105122024Simp/* sysctl vars */ 106122024SimpSYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters"); 107122024Simp 108122024Simpint cs_ignore_cksum_failure = 0; 109122024SimpTUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure); 110122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW, 111122024Simp &cs_ignore_cksum_failure, 0, 112122024Simp "ignore checksum errors in cs card EEPROM"); 113122024Simp 114122024Simpstatic int cs_recv_delay = 570; 115140925SimpTUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay); 116122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, ""); 117122024Simp 118179532Simpstatic int cs8900_eeint2irq[16] = { 119179532Simp 10, 11, 12, 5, 255, 255, 255, 255, 120179532Simp 255, 255, 255, 255, 255, 255, 255, 255 121179532Simp}; 122179532Simp 123179532Simpstatic int cs8900_irq2eeint[16] = { 124179532Simp 255, 255, 255, 255, 255, 3, 255, 255, 125179532Simp 255, 0, 1, 2, 255, 255, 255, 255 126179532Simp}; 127179532Simp 12837785Smsmithstatic int 129122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer) 13037785Smsmith{ 13137785Smsmith int i; 13237785Smsmith 13337785Smsmith#ifdef CS_DEBUG 134179620Simp device_printf(sc->dev, "EEPROM data from %x for %x:\n", off, len); 13537785Smsmith#endif 136122024Simp for (i=0; i < len; i++) { 137122024Simp if (wait_eeprom_ready(sc) < 0) 138122024Simp return (-1); 13937785Smsmith /* Send command to EEPROM to read */ 14072940Simp cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD); 141122024Simp if (wait_eeprom_ready(sc) < 0) 14272940Simp return (-1); 14372940Simp buffer[i] = cs_readreg(sc, PP_EEData); 14437785Smsmith 14537785Smsmith#ifdef CS_DEBUG 146179591Simp printf("%04x ",buffer[i]); 14737785Smsmith#endif 14837785Smsmith } 14937785Smsmith 15037785Smsmith#ifdef CS_DEBUG 15137785Smsmith printf("\n"); 15237785Smsmith#endif 15372940Simp return (0); 15437785Smsmith} 15537785Smsmith 15637785Smsmithstatic int 157122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer) 15837785Smsmith{ 159122024Simp int i; 160122024Simp uint16_t cksum=0; 16137785Smsmith 162122024Simp for (i = 0; i < len; i++) 163122024Simp cksum += buffer[i]; 16437785Smsmith cksum &= 0xffff; 165179591Simp if (cksum == 0 || cs_ignore_cksum_failure) 166122024Simp return (0); 167122024Simp return (-1); 16837785Smsmith} 16937785Smsmith 17037785Smsmithstatic int 17137785Smsmithwait_eeprom_ready(struct cs_softc *sc) 17237785Smsmith{ 173179620Simp int i; 174179620Simp 175179620Simp /* 176179620Simp * From the CS8900A datasheet, section 3.5.2: 177179620Simp * "Before issuing any command to the EEPROM, the host must wait 178179620Simp * for the SIBUSY bit (Register 16, SelfST, bit 8) to clear. After 179179620Simp * each command has been issued, the host must wait again for SIBUSY 180179620Simp * to clear." 181179620Simp * 182179620Simp * Before we issue the command, we should be !busy, so that will 183179620Simp * be fast. The datasheet suggests that clock out from the part 184179620Simp * per word will be on the order of 25us, which is consistant with 185179620Simp * the 1MHz serial clock and 16bits... We should never hit 100, 186179620Simp * let alone 30,000 here. The original code did an unconditional 187179620Simp * 30ms DELAY here. Bad Kharma. 188179620Simp */ 189179620Simp for (i = 0; i < 30000; i++) /* 30ms max */ 190179620Simp if (!(cs_readreg(sc, PP_SelfST) & SI_BUSY)) 191179620Simp return (0); 192179620Simp return (1); 19337785Smsmith} 19437785Smsmith 19537785Smsmithstatic void 19637785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off) 19737785Smsmith{ 19837785Smsmith unsigned int self_control = HCB1_ENBL; 19937785Smsmith 20037785Smsmith if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off) 20137785Smsmith self_control |= HCB1; 20237785Smsmith else 20337785Smsmith self_control &= ~HCB1; 20472940Simp cs_writereg(sc, PP_SelfCTL, self_control); 205122024Simp DELAY(500000); 20637785Smsmith} 20737785Smsmith 20837785Smsmith 20937785Smsmithstatic int 21037785Smsmithcs_duplex_auto(struct cs_softc *sc) 21137785Smsmith{ 212140927Simp int i, error=0; 213140927Simp 21472940Simp cs_writereg(sc, PP_AutoNegCTL, 21572940Simp RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE); 216140927Simp for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) { 217179597Simp if (i > 4000) { 218162321Sglebius device_printf(sc->dev, 219140927Simp "full/half duplex auto negotiation timeout\n"); 22037785Smsmith error = ETIMEDOUT; 221140927Simp break; 222140927Simp } 223140927Simp DELAY(1000); 224140927Simp } 225140928Simp return (error); 22637785Smsmith} 22737785Smsmith 22837785Smsmithstatic int 22937785Smsmithenable_tp(struct cs_softc *sc) 23037785Smsmith{ 23137785Smsmith 23272940Simp cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY); 23337785Smsmith control_dc_dc(sc, 0); 234140928Simp return (0); 23537785Smsmith} 23637785Smsmith 23737785Smsmithstatic int 23837785Smsmithenable_aui(struct cs_softc *sc) 23937785Smsmith{ 24037785Smsmith 24172940Simp cs_writereg(sc, PP_LineCTL, 24272940Simp (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY); 243179597Simp control_dc_dc(sc, 0); 244140928Simp return (0); 24537785Smsmith} 24637785Smsmith 24737785Smsmithstatic int 24837785Smsmithenable_bnc(struct cs_softc *sc) 24937785Smsmith{ 25037785Smsmith 25172940Simp cs_writereg(sc, PP_LineCTL, 25272940Simp (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY); 253179597Simp control_dc_dc(sc, 1); 254140928Simp return (0); 25537785Smsmith} 25637785Smsmith 25771316Simpint 25858816Simpcs_cs89x0_probe(device_t dev) 25937785Smsmith{ 26058816Simp int i; 26158816Simp int error; 26258816Simp u_long irq, junk; 26358816Simp struct cs_softc *sc = device_get_softc(dev); 26437785Smsmith unsigned rev_type = 0; 265122024Simp uint16_t id; 26658816Simp char chip_revision; 267122024Simp uint16_t eeprom_buff[CHKSUM_LEN]; 26837785Smsmith int chip_type, pp_isaint, pp_isadma; 26937785Smsmith 270179620Simp sc->dev = dev; 27158816Simp error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS); 27258816Simp if (error) 27358816Simp return (error); 27458816Simp 27572940Simp sc->nic_addr = rman_get_start(sc->port_res); 27658816Simp 27772940Simp if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) { 27837785Smsmith /* Chip not detected. Let's try to reset it */ 27937785Smsmith if (bootverbose) 28058816Simp device_printf(dev, "trying to reset the chip.\n"); 28172940Simp cs_outw(sc, ADD_PORT, PP_SelfCTL); 28272940Simp i = cs_inw(sc, DATA_PORT); 28372940Simp cs_outw(sc, ADD_PORT, PP_SelfCTL); 28472940Simp cs_outw(sc, DATA_PORT, i | POWER_ON_RESET); 28572940Simp if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) 28658816Simp return (ENXIO); 28737785Smsmith } 28837785Smsmith 28972940Simp for (i = 0; i < 10000; i++) { 29072940Simp id = cs_readreg(sc, PP_ChipID); 29172940Simp if (id == CHIP_EISA_ID_SIG) 29272940Simp break; 29372940Simp } 29472940Simp if (i == 10000) 29558816Simp return (ENXIO); 29637785Smsmith 29772940Simp rev_type = cs_readreg(sc, PRODUCT_ID_ADD); 29837785Smsmith chip_type = rev_type & ~REVISON_BITS; 29937785Smsmith chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A'; 30037785Smsmith 30137785Smsmith sc->chip_type = chip_type; 30258816Simp 303179532Simp if (chip_type == CS8900) { 30437785Smsmith pp_isaint = PP_CS8900_ISAINT; 30537785Smsmith pp_isadma = PP_CS8900_ISADMA; 30637785Smsmith sc->send_cmd = TX_CS8900_AFTER_ALL; 30737785Smsmith } else { 30837785Smsmith pp_isaint = PP_CS8920_ISAINT; 30937785Smsmith pp_isadma = PP_CS8920_ISADMA; 31037785Smsmith sc->send_cmd = TX_CS8920_AFTER_ALL; 31137785Smsmith } 31237785Smsmith 31337785Smsmith /* 314140927Simp * Clear some fields so that fail of EEPROM will left them clean 315140927Simp */ 316140927Simp sc->auto_neg_cnf = 0; 317140927Simp sc->adapter_cnf = 0; 318140927Simp sc->isa_config = 0; 319140927Simp 320140927Simp /* 321179532Simp * If no interrupt specified, use what the board tells us. 32237785Smsmith */ 32358816Simp error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); 32458816Simp 32558816Simp /* 32658816Simp * Get data from EEPROM 32758816Simp */ 32872940Simp if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) { 32958816Simp device_printf(dev, "No EEPROM, assuming defaults.\n"); 330179532Simp } else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) { 331179532Simp device_printf(dev, "EEPROM read failed, assuming defaults.\n"); 332179532Simp } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) { 333179532Simp device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n"); 33437785Smsmith } else { 335179532Simp sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2]; 336179532Simp sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2]; 337179532Simp sc->isa_config = eeprom_buff[ISA_CNF_OFFSET/2]; 338179532Simp for (i=0; i<ETHER_ADDR_LEN/2; i++) { 339179532Simp sc->enaddr[i*2] = eeprom_buff[i]; 340179532Simp sc->enaddr[i*2+1] = eeprom_buff[i] >> 8; 341179532Simp } 342179532Simp /* 343179532Simp * If no interrupt specified, use what the 344179532Simp * board tells us. 345179532Simp */ 346179532Simp if (error) { 347179532Simp irq = sc->isa_config & INT_NO_MASK; 348179532Simp error = 0; 349179532Simp if (chip_type == CS8900) { 350179532Simp irq = cs8900_eeint2irq[irq]; 35137785Smsmith } else { 352179532Simp if (irq > CS8920_NO_INTS) 353179532Simp irq = 255; 35437785Smsmith } 355179532Simp if (irq == 255) { 356179532Simp device_printf(dev, "invalid irq in EEPROM.\n"); 357179532Simp error = EINVAL; 358179532Simp } 359179532Simp if (!error) 360179532Simp bus_set_resource(dev, SYS_RES_IRQ, 0, 361179532Simp irq, 1); 362140927Simp } 363140927Simp } 36437785Smsmith 365179532Simp if (!error && !(sc->flags & CS_NO_IRQ)) { 366140927Simp if (chip_type == CS8900) { 367179532Simp if (irq >= 0 || irq < 16) 368179532Simp irq = cs8900_irq2eeint[irq]; 369179532Simp else 370179532Simp irq = 255; 371140927Simp } else { 372179532Simp if (irq > CS8920_NO_INTS) 373179532Simp irq = 255; 374140927Simp } 375179532Simp if (irq == 255) 376179532Simp error = EINVAL; 37758816Simp } 37858816Simp 379179507Simp if (error) { 38058816Simp device_printf(dev, "Unknown or invalid irq\n"); 381179507Simp return (error); 382140927Simp } 383140927Simp 384179507Simp if (!(sc->flags & CS_NO_IRQ)) 385179507Simp cs_writereg(sc, pp_isaint, irq); 386179507Simp 387140927Simp /* 388140927Simp * Temporary disabled 389140927Simp * 390140927Simp if (drq>0) 39172940Simp cs_writereg(sc, pp_isadma, drq); 39237785Smsmith else { 393104252Sbrooks device_printf(dev, "incorrect drq\n",); 394140928Simp return (0); 39537785Smsmith } 396140927Simp */ 39737785Smsmith 39837785Smsmith if (bootverbose) 39958816Simp device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n", 400179532Simp chip_type == CS8900 ? '0' : '2', 401179532Simp chip_type == CS8920M ? "M" : "", 40237785Smsmith chip_revision, 40337785Smsmith (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "", 40437785Smsmith (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "", 40558816Simp (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : ""); 40637785Smsmith 407140927Simp if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) && 408140927Simp (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH)) 409140927Simp sc->line_ctl = LOW_RX_SQUELCH; 410140927Simp else 411140927Simp sc->line_ctl = 0; 41237785Smsmith 413140928Simp return (0); 41437785Smsmith} 41537785Smsmith 41637785Smsmith/* 41758816Simp * Allocate a port resource with the given resource id. 41858816Simp */ 419179560Sjhbint 420179560Sjhbcs_alloc_port(device_t dev, int rid, int size) 42158816Simp{ 422140927Simp struct cs_softc *sc = device_get_softc(dev); 423140927Simp struct resource *res; 42458816Simp 425140927Simp res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 426140927Simp 0ul, ~0ul, size, RF_ACTIVE); 427140928Simp if (res == NULL) 428140927Simp return (ENOENT); 429140928Simp sc->port_rid = rid; 430140928Simp sc->port_res = res; 431140928Simp return (0); 43258816Simp} 43358816Simp 43458816Simp/* 43558816Simp * Allocate an irq resource with the given resource id. 43658816Simp */ 437179560Sjhbint 438179597Simpcs_alloc_irq(device_t dev, int rid) 43958816Simp{ 440140927Simp struct cs_softc *sc = device_get_softc(dev); 441140927Simp struct resource *res; 44258816Simp 443179597Simp res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 444140928Simp if (res == NULL) 445140927Simp return (ENOENT); 446140928Simp sc->irq_rid = rid; 447140928Simp sc->irq_res = res; 448140928Simp return (0); 44958816Simp} 45058816Simp 45158816Simp/* 45258816Simp * Release all resources 45358816Simp */ 454179560Sjhbvoid 455179560Sjhbcs_release_resources(device_t dev) 45658816Simp{ 457140927Simp struct cs_softc *sc = device_get_softc(dev); 45858816Simp 459140927Simp if (sc->port_res) { 460140927Simp bus_release_resource(dev, SYS_RES_IOPORT, 461140927Simp sc->port_rid, sc->port_res); 462140927Simp sc->port_res = 0; 463140927Simp } 464140927Simp if (sc->irq_res) { 465140927Simp bus_release_resource(dev, SYS_RES_IRQ, 466140927Simp sc->irq_rid, sc->irq_res); 467140927Simp sc->irq_res = 0; 468140927Simp } 46958816Simp} 47058816Simp 47158816Simp/* 47237785Smsmith * Install the interface into kernel networking data structures 47337785Smsmith */ 47471316Simpint 475121816Sbrookscs_attach(device_t dev) 47637785Smsmith{ 477179560Sjhb int error, media=0; 478121816Sbrooks struct cs_softc *sc = device_get_softc(dev);; 479147256Sbrooks struct ifnet *ifp; 48037785Smsmith 481162321Sglebius sc->dev = dev; 482162321Sglebius 483147256Sbrooks ifp = sc->ifp = if_alloc(IFT_ETHER); 484147256Sbrooks if (ifp == NULL) { 485147256Sbrooks device_printf(dev, "can not if_alloc()\n"); 486179560Sjhb cs_release_resources(dev); 487179560Sjhb return (ENOMEM); 488147256Sbrooks } 489147256Sbrooks 490179560Sjhb mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK, 491179560Sjhb MTX_DEF); 492179560Sjhb callout_init_mtx(&sc->timer, &sc->lock, 0); 49358816Simp 494179560Sjhb CS_LOCK(sc); 495179560Sjhb cs_stop(sc); 496179560Sjhb CS_UNLOCK(sc); 497179560Sjhb 498121752Sbrooks ifp->if_softc=sc; 499121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 500121752Sbrooks ifp->if_start=cs_start; 501121752Sbrooks ifp->if_ioctl=cs_ioctl; 502121752Sbrooks ifp->if_init=cs_init; 503179560Sjhb IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 50437785Smsmith 505179560Sjhb ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 50637785Smsmith 507121752Sbrooks /* 508121752Sbrooks * this code still in progress (DMA support) 509121752Sbrooks * 51037785Smsmith 511121752Sbrooks sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT); 512121752Sbrooks if (sc->recv_ring == NULL) { 513121816Sbrooks log(LOG_ERR, 514121816Sbrooks "%s: Couldn't allocate memory for NIC\n", ifp->if_xname); 515121752Sbrooks return(0); 516121752Sbrooks } 517121752Sbrooks if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF)) 518121752Sbrooks < (128*1024-CS_DMA_BUFFER_SIZE)) 519121752Sbrooks sc->recv_ring+=16*1024; 52037785Smsmith 521121752Sbrooks */ 52237785Smsmith 523121752Sbrooks sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT); 524121752Sbrooks if (sc->buffer == NULL) { 525162321Sglebius device_printf(sc->dev, "Couldn't allocate memory for NIC\n"); 526179560Sjhb if_free(ifp); 527179560Sjhb mtx_destroy(&sc->lock); 528179560Sjhb cs_release_resources(dev); 529179560Sjhb return(ENOMEM); 530121752Sbrooks } 53137785Smsmith 532121752Sbrooks /* 533121752Sbrooks * Initialize the media structures. 534121752Sbrooks */ 535121752Sbrooks ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus); 53637785Smsmith 537121752Sbrooks if (sc->adapter_cnf & A_CNF_10B_T) { 538121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL); 539121752Sbrooks if (sc->chip_type != CS8900) { 540121752Sbrooks ifmedia_add(&sc->media, 541121752Sbrooks IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL); 542121752Sbrooks ifmedia_add(&sc->media, 543121752Sbrooks IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL); 544121752Sbrooks } 545121752Sbrooks } 54637785Smsmith 547121752Sbrooks if (sc->adapter_cnf & A_CNF_10B_2) 548121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL); 54937785Smsmith 550121752Sbrooks if (sc->adapter_cnf & A_CNF_AUI) 551121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL); 55237785Smsmith 553121752Sbrooks if (sc->adapter_cnf & A_CNF_MEDIA) 554121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL); 55537785Smsmith 556121752Sbrooks /* Set default media from EEPROM */ 557121752Sbrooks switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) { 558121752Sbrooks case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break; 559121752Sbrooks case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break; 560121752Sbrooks case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break; 561121752Sbrooks case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break; 562122024Simp default: 563162321Sglebius device_printf(sc->dev, "no media, assuming 10baseT\n"); 564122024Simp sc->adapter_cnf |= A_CNF_10B_T; 565122024Simp ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL); 566122024Simp if (sc->chip_type != CS8900) { 567122024Simp ifmedia_add(&sc->media, 568122024Simp IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL); 569122024Simp ifmedia_add(&sc->media, 570122024Simp IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL); 571122024Simp } 572122024Simp media = IFM_ETHER | IFM_10_T; 573122024Simp break; 57437785Smsmith } 575121752Sbrooks ifmedia_set(&sc->media, media); 576121752Sbrooks cs_mediaset(sc, media); 57737785Smsmith 578147256Sbrooks ether_ifattach(ifp, sc->enaddr); 579121752Sbrooks 580179560Sjhb error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE, 581179560Sjhb NULL, csintr, sc, &sc->irq_handle); 582179560Sjhb if (error) { 583179560Sjhb ether_ifdetach(ifp); 584179560Sjhb free(sc->buffer, M_DEVBUF); 585179560Sjhb if_free(ifp); 586179560Sjhb mtx_destroy(&sc->lock); 587179560Sjhb cs_release_resources(dev); 588179560Sjhb return (error); 589179560Sjhb } 590179560Sjhb 59158816Simp return (0); 59237785Smsmith} 59337785Smsmith 594140888Simpint 595140888Simpcs_detach(device_t dev) 596140888Simp{ 597140888Simp struct cs_softc *sc; 598140888Simp struct ifnet *ifp; 599140888Simp 600140888Simp sc = device_get_softc(dev); 601147256Sbrooks ifp = sc->ifp; 602140888Simp 603179560Sjhb CS_LOCK(sc); 604140888Simp cs_stop(sc); 605179560Sjhb CS_UNLOCK(sc); 606179560Sjhb callout_drain(&sc->timer); 607140888Simp ether_ifdetach(ifp); 608179560Sjhb bus_teardown_intr(dev, sc->irq_res, sc->irq_handle); 609150306Simp cs_release_resources(dev); 610179560Sjhb free(sc->buffer, M_DEVBUF); 611147256Sbrooks if_free(ifp); 612179560Sjhb mtx_destroy(&sc->lock); 613140888Simp return (0); 614140888Simp} 615140888Simp 61637785Smsmith/* 61737785Smsmith * Initialize the board 61837785Smsmith */ 61937785Smsmithstatic void 62037785Smsmithcs_init(void *xsc) 62137785Smsmith{ 62237785Smsmith struct cs_softc *sc=(struct cs_softc *)xsc; 623179560Sjhb 624179560Sjhb CS_LOCK(sc); 625179560Sjhb cs_init_locked(sc); 626179560Sjhb CS_UNLOCK(sc); 627179560Sjhb} 628179560Sjhb 629179560Sjhbstatic void 630179560Sjhbcs_init_locked(struct cs_softc *sc) 631179560Sjhb{ 632147256Sbrooks struct ifnet *ifp = sc->ifp; 633179560Sjhb int i, rx_cfg; 63437785Smsmith 63537785Smsmith /* 636179560Sjhb * reset watchdog timer 63737785Smsmith */ 638179560Sjhb sc->tx_timeout = 0; 63937785Smsmith sc->buf_len = 0; 64037785Smsmith 64137785Smsmith /* 64237785Smsmith * Hardware initialization of cs 64337785Smsmith */ 64437785Smsmith 64537785Smsmith /* Enable receiver and transmitter */ 64672940Simp cs_writereg(sc, PP_LineCTL, 64772940Simp cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON); 64837785Smsmith 64937785Smsmith /* Configure the receiver mode */ 65037785Smsmith cs_setmode(sc); 65137785Smsmith 65237785Smsmith /* 65337785Smsmith * This defines what type of frames will cause interrupts 65437785Smsmith * Bad frames should generate interrupts so that the driver 65537785Smsmith * could track statistics of discarded packets 65637785Smsmith */ 657140927Simp rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL | 65837785Smsmith RX_EXTRA_DATA_ENBL; 65937785Smsmith if (sc->isa_config & STREAM_TRANSFER) 66037785Smsmith rx_cfg |= RX_STREAM_ENBL; 66172940Simp cs_writereg(sc, PP_RxCFG, rx_cfg); 66272940Simp cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL | 66337785Smsmith TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL | 66437785Smsmith TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL); 66572940Simp cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL | 66637785Smsmith RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL | 66737785Smsmith TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/); 66837785Smsmith 669140927Simp /* Write MAC address into IA filter */ 670140927Simp for (i=0; i<ETHER_ADDR_LEN/2; i++) 671140927Simp cs_writereg(sc, PP_IA + i * 2, 672147256Sbrooks sc->enaddr[i * 2] | 673147256Sbrooks (sc->enaddr[i * 2 + 1] << 8) ); 67437785Smsmith 67537785Smsmith /* 67637785Smsmith * Now enable everything 67737785Smsmith */ 67837785Smsmith/* 67937785Smsmith#ifdef CS_USE_64K_DMA 68072940Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K); 68172940Simp#else 682140927Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ); 68337785Smsmith#endif 68437785Smsmith*/ 68572940Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ); 68637785Smsmith 68737785Smsmith /* 68837785Smsmith * Set running and clear output active flags 68937785Smsmith */ 690148887Srwatson sc->ifp->if_drv_flags |= IFF_DRV_RUNNING; 691148887Srwatson sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 692179560Sjhb callout_reset(&sc->timer, hz, cs_watchdog, sc); 69337785Smsmith 69437785Smsmith /* 69537785Smsmith * Start sending process 69637785Smsmith */ 697179560Sjhb cs_start_locked(ifp); 69837785Smsmith} 69937785Smsmith 70037785Smsmith/* 701106937Ssam * Get the packet from the board and send it to the upper layer. 70237785Smsmith */ 70337785Smsmithstatic int 70437785Smsmithcs_get_packet(struct cs_softc *sc) 70537785Smsmith{ 706147256Sbrooks struct ifnet *ifp = sc->ifp; 70737785Smsmith int iobase = sc->nic_addr, status, length; 70837785Smsmith struct ether_header *eh; 70937785Smsmith struct mbuf *m; 71037785Smsmith 71137785Smsmith#ifdef CS_DEBUG 71237785Smsmith int i; 71337785Smsmith#endif 71437785Smsmith 71572940Simp status = cs_inw(sc, RX_FRAME_PORT); 71672940Simp length = cs_inw(sc, RX_FRAME_PORT); 71737785Smsmith 71837785Smsmith#ifdef CS_DEBUG 719162321Sglebius device_printf(sc->dev, "rcvd: stat %x, len %d\n", 720104252Sbrooks status, length); 72137785Smsmith#endif 72237785Smsmith 72337785Smsmith if (!(status & RX_OK)) { 72437785Smsmith#ifdef CS_DEBUG 725162321Sglebius device_printf(sc->dev, "bad pkt stat %x\n", status); 72637785Smsmith#endif 72737785Smsmith ifp->if_ierrors++; 728140928Simp return (-1); 72937785Smsmith } 73037785Smsmith 731111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 73237785Smsmith if (m==NULL) 733140928Simp return (-1); 73437785Smsmith 73537785Smsmith if (length > MHLEN) { 736111119Simp MCLGET(m, M_DONTWAIT); 73737785Smsmith if (!(m->m_flags & M_EXT)) { 73837785Smsmith m_freem(m); 739140928Simp return (-1); 74037785Smsmith } 74137785Smsmith } 74237785Smsmith 74337785Smsmith /* Initialize packet's header info */ 74437785Smsmith m->m_pkthdr.rcvif = ifp; 74537785Smsmith m->m_pkthdr.len = length; 74637785Smsmith m->m_len = length; 74737785Smsmith 74837785Smsmith /* Get the data */ 74937785Smsmith insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1); 75037785Smsmith 75137785Smsmith eh = mtod(m, struct ether_header *); 75237785Smsmith 75337785Smsmith#ifdef CS_DEBUG 75437785Smsmith for (i=0;i<length;i++) 75537785Smsmith printf(" %02x",(unsigned char)*((char *)(m->m_data+i))); 75637785Smsmith printf( "\n" ); 75737785Smsmith#endif 75837785Smsmith 75937785Smsmith if (status & (RX_IA | RX_BROADCAST) || 76037785Smsmith (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) { 76137785Smsmith /* Feed the packet to the upper layer */ 762106937Ssam (*ifp->if_input)(ifp, m); 76337785Smsmith ifp->if_ipackets++; 764122024Simp if (length == ETHER_MAX_LEN-ETHER_CRC_LEN) 765140927Simp DELAY(cs_recv_delay); 76637785Smsmith } else { 76737785Smsmith m_freem(m); 76837785Smsmith } 76937785Smsmith 770140928Simp return (0); 77137785Smsmith} 77237785Smsmith 77337785Smsmith/* 77458816Simp * Handle interrupts 77537785Smsmith */ 77658816Simpvoid 77758816Simpcsintr(void *arg) 77837785Smsmith{ 77958816Simp struct cs_softc *sc = (struct cs_softc*) arg; 780147256Sbrooks struct ifnet *ifp = sc->ifp; 78141591Sarchie int status; 78237785Smsmith 78337785Smsmith#ifdef CS_DEBUG 784162321Sglebius device_printf(sc->dev, "Interrupt.\n"); 78537785Smsmith#endif 78637785Smsmith 787179560Sjhb CS_LOCK(sc); 78872940Simp while ((status=cs_inw(sc, ISQ_PORT))) { 78937785Smsmith 79037785Smsmith#ifdef CS_DEBUG 791162321Sglebius device_printf(sc->dev, "from ISQ: %04x\n", status); 79237785Smsmith#endif 79337785Smsmith 79437785Smsmith switch (status & ISQ_EVENT_MASK) { 795140927Simp case ISQ_RECEIVER_EVENT: 796140927Simp cs_get_packet(sc); 797140927Simp break; 79837785Smsmith 799140927Simp case ISQ_TRANSMITTER_EVENT: 800140927Simp if (status & TX_OK) 801140927Simp ifp->if_opackets++; 802140927Simp else 803140927Simp ifp->if_oerrors++; 804148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 805179560Sjhb sc->tx_timeout = 0; 806140927Simp break; 80737785Smsmith 808140927Simp case ISQ_BUFFER_EVENT: 809140927Simp if (status & READY_FOR_TX) { 810148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 811179560Sjhb sc->tx_timeout = 0; 812140927Simp } 81337785Smsmith 814140927Simp if (status & TX_UNDERRUN) { 815148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 816179560Sjhb sc->tx_timeout = 0; 817140927Simp ifp->if_oerrors++; 818140927Simp } 819140927Simp break; 82037785Smsmith 821140927Simp case ISQ_RX_MISS_EVENT: 822140927Simp ifp->if_ierrors+=(status>>6); 823140927Simp break; 82437785Smsmith 825140927Simp case ISQ_TX_COL_EVENT: 826140927Simp ifp->if_collisions+=(status>>6); 827140927Simp break; 828140927Simp } 829140927Simp } 83037785Smsmith 831148887Srwatson if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) { 832179560Sjhb cs_start_locked(ifp); 833140927Simp } 834179560Sjhb CS_UNLOCK(sc); 83537785Smsmith} 83637785Smsmith 83737785Smsmith/* 83837785Smsmith * Save the data in buffer 83937785Smsmith */ 84037785Smsmith 84137785Smsmithstatic void 84237785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m ) 84337785Smsmith{ 84437785Smsmith int len; 84537785Smsmith struct mbuf *mp; 84637785Smsmith unsigned char *data, *buf; 84737785Smsmith 84837785Smsmith for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) { 84937785Smsmith len = mp->m_len; 85037785Smsmith 85137785Smsmith /* 85237785Smsmith * Ignore empty parts 85337785Smsmith */ 85437785Smsmith if (!len) 85537785Smsmith continue; 85637785Smsmith 85737785Smsmith /* 85837785Smsmith * Find actual data address 85937785Smsmith */ 86037785Smsmith data = mtod(mp, caddr_t); 86137785Smsmith 86237785Smsmith bcopy((caddr_t) data, (caddr_t) buf, len); 86337785Smsmith buf += len; 86437785Smsmith sc->buf_len += len; 86537785Smsmith } 86637785Smsmith} 86737785Smsmith 86837785Smsmith 86937785Smsmithstatic void 87037785Smsmithcs_xmit_buf( struct cs_softc *sc ) 87137785Smsmith{ 87237785Smsmith outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1); 87337785Smsmith sc->buf_len = 0; 87437785Smsmith} 87537785Smsmith 87637785Smsmithstatic void 87737785Smsmithcs_start(struct ifnet *ifp) 87837785Smsmith{ 879179560Sjhb struct cs_softc *sc = ifp->if_softc; 880179560Sjhb 881179560Sjhb CS_LOCK(sc); 882179560Sjhb cs_start_locked(ifp); 883179560Sjhb CS_UNLOCK(sc); 884179560Sjhb} 885179560Sjhb 886179560Sjhbstatic void 887179560Sjhbcs_start_locked(struct ifnet *ifp) 888179560Sjhb{ 889179560Sjhb int length; 89037785Smsmith struct mbuf *m, *mp; 89137785Smsmith struct cs_softc *sc = ifp->if_softc; 89237785Smsmith 89337785Smsmith for (;;) { 89437785Smsmith if (sc->buf_len) 89537785Smsmith length = sc->buf_len; 89637785Smsmith else { 89737785Smsmith IF_DEQUEUE( &ifp->if_snd, m ); 89837785Smsmith 89937785Smsmith if (m==NULL) { 90037785Smsmith return; 90137785Smsmith } 90237785Smsmith 90337785Smsmith for (length=0, mp=m; mp != NULL; mp=mp->m_next) 90437785Smsmith length += mp->m_len; 90537785Smsmith 90637785Smsmith /* Skip zero-length packets */ 90737785Smsmith if (length == 0) { 90837785Smsmith m_freem(m); 90937785Smsmith continue; 91037785Smsmith } 91137785Smsmith 91237785Smsmith cs_write_mbufs(sc, m); 91337785Smsmith 914106937Ssam BPF_MTAP(ifp, m); 91537785Smsmith 91637785Smsmith m_freem(m); 91737785Smsmith } 91837785Smsmith 91937785Smsmith /* 92037785Smsmith * Issue a SEND command 92137785Smsmith */ 92272940Simp cs_outw(sc, TX_CMD_PORT, sc->send_cmd); 92372940Simp cs_outw(sc, TX_LEN_PORT, length ); 92437785Smsmith 92537785Smsmith /* 92637785Smsmith * If there's no free space in the buffer then leave 92737785Smsmith * this packet for the next time: indicate output active 92837785Smsmith * and return. 92937785Smsmith */ 93072940Simp if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) { 931179560Sjhb sc->tx_timeout = sc->buf_len; 932148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 93337785Smsmith return; 93437785Smsmith } 93537785Smsmith 936140927Simp cs_xmit_buf(sc); 93737785Smsmith 93837785Smsmith /* 93937785Smsmith * Set the watchdog timer in case we never hear 94037785Smsmith * from board again. (I don't know about correct 94137785Smsmith * value for this timeout) 94237785Smsmith */ 943179560Sjhb sc->tx_timeout = length; 94437785Smsmith 945148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 94637785Smsmith return; 94737785Smsmith } 94837785Smsmith} 94937785Smsmith 95037785Smsmith/* 95137785Smsmith * Stop everything on the interface 95237785Smsmith */ 95337785Smsmithstatic void 95437785Smsmithcs_stop(struct cs_softc *sc) 95537785Smsmith{ 95637785Smsmith 957179560Sjhb CS_ASSERT_LOCKED(sc); 95872940Simp cs_writereg(sc, PP_RxCFG, 0); 95972940Simp cs_writereg(sc, PP_TxCFG, 0); 96072940Simp cs_writereg(sc, PP_BufCFG, 0); 96172940Simp cs_writereg(sc, PP_BusCTL, 0); 96237785Smsmith 963148887Srwatson sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 964179560Sjhb sc->tx_timeout = 0; 965179560Sjhb callout_stop(&sc->timer); 96637785Smsmith} 96737785Smsmith 96837785Smsmith/* 96937785Smsmith * Reset the interface 97037785Smsmith */ 97137785Smsmithstatic void 97237785Smsmithcs_reset(struct cs_softc *sc) 97337785Smsmith{ 974179560Sjhb 975179560Sjhb CS_ASSERT_LOCKED(sc); 97637785Smsmith cs_stop(sc); 977179560Sjhb cs_init_locked(sc); 97837785Smsmith} 97937785Smsmith 980156559Sphilipstatic uint16_t 981156559Sphilipcs_hash_index(struct sockaddr_dl *addr) 982156559Sphilip{ 983156559Sphilip uint32_t crc; 984156559Sphilip uint16_t idx; 985156559Sphilip caddr_t lla; 986156559Sphilip 987156559Sphilip lla = LLADDR(addr); 988156559Sphilip crc = ether_crc32_le(lla, ETHER_ADDR_LEN); 989156559Sphilip idx = crc >> 26; 990156559Sphilip 991156559Sphilip return (idx); 992156559Sphilip} 993156559Sphilip 99437785Smsmithstatic void 99537785Smsmithcs_setmode(struct cs_softc *sc) 99637785Smsmith{ 997156559Sphilip int rx_ctl; 998156559Sphilip uint16_t af[4]; 999156559Sphilip uint16_t port, mask, index; 1000147256Sbrooks struct ifnet *ifp = sc->ifp; 1001156559Sphilip struct ifmultiaddr *ifma; 100237785Smsmith 100337785Smsmith /* Stop the receiver while changing filters */ 100472940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON); 100537785Smsmith 100637785Smsmith if (ifp->if_flags & IFF_PROMISC) { 100737785Smsmith /* Turn on promiscuous mode. */ 100837785Smsmith rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT; 1009156559Sphilip } else if (ifp->if_flags & IFF_MULTICAST) { 1010156559Sphilip /* Allow receiving frames with multicast addresses */ 1011156559Sphilip rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT | 1012156559Sphilip RX_OK_ACCEPT | RX_MULTCAST_ACCEPT; 1013156559Sphilip 1014156559Sphilip /* Start with an empty filter */ 1015156559Sphilip af[0] = af[1] = af[2] = af[3] = 0x0000; 1016156559Sphilip 1017156559Sphilip if (ifp->if_flags & IFF_ALLMULTI) { 1018156559Sphilip /* Accept all multicast frames */ 1019156559Sphilip af[0] = af[1] = af[2] = af[3] = 0xffff; 102037785Smsmith } else { 1021156559Sphilip /* 1022156559Sphilip * Set up the filter to only accept multicast 1023156559Sphilip * frames we're interested in. 102437785Smsmith */ 1025156559Sphilip IF_ADDR_LOCK(ifp); 1026156559Sphilip TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1027156559Sphilip struct sockaddr_dl *dl = 1028156559Sphilip (struct sockaddr_dl *)ifma->ifma_addr; 1029156559Sphilip 1030156559Sphilip index = cs_hash_index(dl); 1031156559Sphilip port = (u_int16_t) (index >> 4); 1032156559Sphilip mask = (u_int16_t) (1 << (index & 0xf)); 1033156559Sphilip af[port] |= mask; 1034156559Sphilip } 1035156559Sphilip IF_ADDR_UNLOCK(ifp); 103637785Smsmith } 1037156559Sphilip 1038156559Sphilip cs_writereg(sc, PP_LAF + 0, af[0]); 1039156559Sphilip cs_writereg(sc, PP_LAF + 2, af[1]); 1040156559Sphilip cs_writereg(sc, PP_LAF + 4, af[2]); 1041156559Sphilip cs_writereg(sc, PP_LAF + 6, af[3]); 1042156559Sphilip } else { 1043156559Sphilip /* 1044156559Sphilip * Receive only good frames addressed for us and 1045156559Sphilip * good broadcasts. 1046156559Sphilip */ 1047156559Sphilip rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT | 1048156559Sphilip RX_OK_ACCEPT; 104937785Smsmith } 105037785Smsmith 105137785Smsmith /* Set up the filter */ 105272940Simp cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl); 105337785Smsmith 105437785Smsmith /* Turn on receiver */ 105572940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON); 105637785Smsmith} 105737785Smsmith 105837785Smsmithstatic int 105937785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data) 106037785Smsmith{ 106137785Smsmith struct cs_softc *sc=ifp->if_softc; 106237785Smsmith struct ifreq *ifr = (struct ifreq *)data; 1063179560Sjhb int error=0; 106437785Smsmith 106537785Smsmith#ifdef CS_DEBUG 1066162321Sglebius if_printf(ifp, "%s command=%lx\n", __func__, command); 106737785Smsmith#endif 106837785Smsmith 106937785Smsmith switch (command) { 107037785Smsmith case SIOCSIFFLAGS: 107137785Smsmith /* 107237785Smsmith * Switch interface state between "running" and 107337785Smsmith * "stopped", reflecting the UP flag. 1074140927Simp */ 1075179560Sjhb CS_LOCK(sc); 1076147256Sbrooks if (sc->ifp->if_flags & IFF_UP) { 1077148887Srwatson if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) { 1078179560Sjhb cs_init_locked(sc); 1079140927Simp } 1080140927Simp } else { 1081148887Srwatson if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) { 1082140927Simp cs_stop(sc); 1083140927Simp } 1084179560Sjhb } 108537785Smsmith /* 108637785Smsmith * Promiscuous and/or multicast flags may have changed, 108737785Smsmith * so reprogram the multicast filter and/or receive mode. 108837785Smsmith * 108937785Smsmith * See note about multicasts in cs_setmode 109037785Smsmith */ 109137785Smsmith cs_setmode(sc); 1092179560Sjhb CS_UNLOCK(sc); 109337785Smsmith break; 109437785Smsmith 109537785Smsmith case SIOCADDMULTI: 109637785Smsmith case SIOCDELMULTI: 109737785Smsmith /* 109837785Smsmith * Multicast list has changed; set the hardware filter 109937785Smsmith * accordingly. 110037785Smsmith * 110137785Smsmith * See note about multicasts in cs_setmode 110237785Smsmith */ 1103179560Sjhb CS_LOCK(sc); 110437785Smsmith cs_setmode(sc); 1105179560Sjhb CS_UNLOCK(sc); 110637785Smsmith error = 0; 110737785Smsmith break; 110837785Smsmith 1109140927Simp case SIOCSIFMEDIA: 1110140927Simp case SIOCGIFMEDIA: 1111140927Simp error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 1112140927Simp break; 111337785Smsmith 1114140927Simp default: 1115140921Simp error = ether_ioctl(ifp, command, data); 1116106937Ssam break; 1117140927Simp } 111837785Smsmith 1119140928Simp return (error); 112037785Smsmith} 112137785Smsmith 112237785Smsmith/* 112337785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to 112437785Smsmith * generate an interrupt after a transmit has been started on it. 112537785Smsmith */ 112637785Smsmithstatic void 1127179560Sjhbcs_watchdog(void *arg) 112837785Smsmith{ 1129179560Sjhb struct cs_softc *sc = arg; 1130179560Sjhb struct ifnet *ifp = sc->ifp; 113137785Smsmith 1132179560Sjhb CS_ASSERT_LOCKED(sc); 1133179560Sjhb if (sc->tx_timeout && --sc->tx_timeout == 0) { 1134179560Sjhb ifp->if_oerrors++; 1135179560Sjhb log(LOG_ERR, "%s: device timeout\n", ifp->if_xname); 113637785Smsmith 1137179560Sjhb /* Reset the interface */ 1138179560Sjhb if (ifp->if_flags & IFF_UP) 1139179560Sjhb cs_reset(sc); 1140179560Sjhb else 1141179560Sjhb cs_stop(sc); 1142179560Sjhb } 1143179560Sjhb callout_reset(&sc->timer, hz, cs_watchdog, sc); 114437785Smsmith} 114537785Smsmith 114637785Smsmithstatic int 114737785Smsmithcs_mediachange(struct ifnet *ifp) 114837785Smsmith{ 114937785Smsmith struct cs_softc *sc = ifp->if_softc; 115037785Smsmith struct ifmedia *ifm = &sc->media; 1151179560Sjhb int error; 115237785Smsmith 115337785Smsmith if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 1154140928Simp return (EINVAL); 115537785Smsmith 1156179560Sjhb CS_LOCK(sc); 1157179560Sjhb error = cs_mediaset(sc, ifm->ifm_media); 1158179560Sjhb CS_UNLOCK(sc); 1159179560Sjhb return (error); 116037785Smsmith} 116137785Smsmith 116237785Smsmithstatic void 116337785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 116437785Smsmith{ 116537785Smsmith int line_status; 116637785Smsmith struct cs_softc *sc = ifp->if_softc; 116737785Smsmith 1168179560Sjhb CS_LOCK(sc); 116937785Smsmith ifmr->ifm_active = IFM_ETHER; 117072940Simp line_status = cs_readreg(sc, PP_LineST); 117137785Smsmith if (line_status & TENBASET_ON) { 117237785Smsmith ifmr->ifm_active |= IFM_10_T; 117337785Smsmith if (sc->chip_type != CS8900) { 117472940Simp if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE) 117537785Smsmith ifmr->ifm_active |= IFM_FDX; 117672940Simp if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE) 117737785Smsmith ifmr->ifm_active |= IFM_HDX; 117837785Smsmith } 117937785Smsmith ifmr->ifm_status = IFM_AVALID; 118037785Smsmith if (line_status & LINK_OK) 118137785Smsmith ifmr->ifm_status |= IFM_ACTIVE; 118237785Smsmith } else { 118338305Smsmith if (line_status & AUI_ON) { 118472940Simp cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) | 118572940Simp HCB1_ENBL); 118638305Smsmith if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^ 118772940Simp (cs_readreg(sc, PP_SelfCTL) & HCB1)) 118838305Smsmith ifmr->ifm_active |= IFM_10_2; 118938305Smsmith else 119038305Smsmith ifmr->ifm_active |= IFM_10_5; 119138305Smsmith } 119237785Smsmith } 1193179560Sjhb CS_UNLOCK(sc); 119437785Smsmith} 119537785Smsmith 119637785Smsmithstatic int 119737785Smsmithcs_mediaset(struct cs_softc *sc, int media) 119837785Smsmith{ 1199179532Simp int error = 0; 120037785Smsmith 120137785Smsmith /* Stop the receiver & transmitter */ 120272940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & 120372940Simp ~(SERIAL_RX_ON | SERIAL_TX_ON)); 120437785Smsmith 120537785Smsmith#ifdef CS_DEBUG 1206162321Sglebius device_printf(sc->dev, "%s media=%x\n", __func__, media); 120737785Smsmith#endif 120837785Smsmith 120937785Smsmith switch (IFM_SUBTYPE(media)) { 121037785Smsmith default: 121137785Smsmith case IFM_AUTO: 1212179553Simp /* 1213179553Simp * This chip makes it a little hard to support this, so treat 1214179553Simp * it as IFM_10_T, auto duplex. 1215179553Simp */ 1216179553Simp enable_tp(sc); 1217179553Simp cs_duplex_auto(sc); 121837785Smsmith break; 121937785Smsmith case IFM_10_T: 1220179532Simp enable_tp(sc); 122137785Smsmith if (media & IFM_FDX) 122237785Smsmith cs_duplex_full(sc); 122337785Smsmith else if (media & IFM_HDX) 122437785Smsmith cs_duplex_half(sc); 122537785Smsmith else 122637785Smsmith error = cs_duplex_auto(sc); 122737785Smsmith break; 122837785Smsmith case IFM_10_2: 122937785Smsmith error = enable_bnc(sc); 123037785Smsmith break; 123137785Smsmith case IFM_10_5: 123237785Smsmith error = enable_aui(sc); 123337785Smsmith break; 123437785Smsmith } 123537785Smsmith 123637785Smsmith /* 123737785Smsmith * Turn the transmitter & receiver back on 123837785Smsmith */ 123972940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | 124072940Simp SERIAL_RX_ON | SERIAL_TX_ON); 124137785Smsmith 1242140928Simp return (error); 124337785Smsmith} 1244