if_cs.c revision 271836
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 271836 2014-09-18 21:14:46Z glebius $"); 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> 59257176Sglebius#include <net/if_var.h> 6037785Smsmith#include <net/if_arp.h> 61156559Sphilip#include <net/if_dl.h> 6237785Smsmith#include <net/if_media.h> 63147256Sbrooks#include <net/if_types.h> 6437785Smsmith#include <net/ethernet.h> 6537785Smsmith#include <net/bpf.h> 6637785Smsmith 6772940Simp#include <dev/cs/if_csvar.h> 6858816Simp#include <dev/cs/if_csreg.h> 6937785Smsmith 7037785Smsmith#ifdef CS_USE_64K_DMA 7137785Smsmith#define CS_DMA_BUFFER_SIZE 65536 7237785Smsmith#else 7337785Smsmith#define CS_DMA_BUFFER_SIZE 16384 7437785Smsmith#endif 7537785Smsmith 76122024Simpstatic void cs_init(void *); 77179560Sjhbstatic void cs_init_locked(struct cs_softc *); 78122024Simpstatic int cs_ioctl(struct ifnet *, u_long, caddr_t); 79122024Simpstatic void cs_start(struct ifnet *); 80179560Sjhbstatic void cs_start_locked(struct ifnet *); 81122024Simpstatic void cs_stop(struct cs_softc *); 82122024Simpstatic void cs_reset(struct cs_softc *); 83179560Sjhbstatic void cs_watchdog(void *); 8438592Smsmith 85122024Simpstatic int cs_mediachange(struct ifnet *); 86122024Simpstatic void cs_mediastatus(struct ifnet *, struct ifmediareq *); 87122024Simpstatic int cs_mediaset(struct cs_softc *, int); 8858816Simp 8937785Smsmithstatic void cs_write_mbufs(struct cs_softc*, struct mbuf*); 9037785Smsmithstatic void cs_xmit_buf(struct cs_softc*); 9137785Smsmithstatic int cs_get_packet(struct cs_softc*); 9237785Smsmithstatic void cs_setmode(struct cs_softc*); 9337785Smsmith 94122024Simpstatic int get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *); 95122024Simpstatic int get_eeprom_cksum(int, int, uint16_t *); 9637785Smsmithstatic int wait_eeprom_ready( struct cs_softc *); 9737785Smsmithstatic void control_dc_dc( struct cs_softc *, int ); 9837785Smsmithstatic int enable_tp(struct cs_softc *); 9937785Smsmithstatic int enable_aui(struct cs_softc *); 10037785Smsmithstatic int enable_bnc(struct cs_softc *); 10137785Smsmithstatic int cs_duplex_auto(struct cs_softc *); 10237785Smsmith 10371316Simpdevclass_t cs_devclass; 104179597Simpdriver_intr_t csintr; 10537785Smsmith 106122024Simp/* sysctl vars */ 107227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters"); 108122024Simp 109122024Simpint cs_ignore_cksum_failure = 0; 110267992ShselaskySYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RWTUN, 111122024Simp &cs_ignore_cksum_failure, 0, 112122024Simp "ignore checksum errors in cs card EEPROM"); 113122024Simp 114122024Simpstatic int cs_recv_delay = 570; 115267992ShselaskySYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RWTUN, &cs_recv_delay, 570, ""); 116122024Simp 117179532Simpstatic int cs8900_eeint2irq[16] = { 118179532Simp 10, 11, 12, 5, 255, 255, 255, 255, 119179532Simp 255, 255, 255, 255, 255, 255, 255, 255 120179532Simp}; 121179532Simp 122179532Simpstatic int cs8900_irq2eeint[16] = { 123179532Simp 255, 255, 255, 255, 255, 3, 255, 255, 124179532Simp 255, 0, 1, 2, 255, 255, 255, 255 125179532Simp}; 126179532Simp 12737785Smsmithstatic int 128122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer) 12937785Smsmith{ 13037785Smsmith int i; 13137785Smsmith 13237785Smsmith#ifdef CS_DEBUG 133179620Simp device_printf(sc->dev, "EEPROM data from %x for %x:\n", off, len); 13437785Smsmith#endif 135122024Simp for (i=0; i < len; i++) { 136122024Simp if (wait_eeprom_ready(sc) < 0) 137122024Simp return (-1); 13837785Smsmith /* Send command to EEPROM to read */ 13972940Simp cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD); 140122024Simp if (wait_eeprom_ready(sc) < 0) 14172940Simp return (-1); 14272940Simp buffer[i] = cs_readreg(sc, PP_EEData); 14337785Smsmith 14437785Smsmith#ifdef CS_DEBUG 145179591Simp printf("%04x ",buffer[i]); 14637785Smsmith#endif 14737785Smsmith } 14837785Smsmith 14937785Smsmith#ifdef CS_DEBUG 15037785Smsmith printf("\n"); 15137785Smsmith#endif 15272940Simp return (0); 15337785Smsmith} 15437785Smsmith 15537785Smsmithstatic int 156122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer) 15737785Smsmith{ 158122024Simp int i; 159122024Simp uint16_t cksum=0; 16037785Smsmith 161122024Simp for (i = 0; i < len; i++) 162122024Simp cksum += buffer[i]; 16337785Smsmith cksum &= 0xffff; 164179591Simp if (cksum == 0 || cs_ignore_cksum_failure) 165122024Simp return (0); 166122024Simp return (-1); 16737785Smsmith} 16837785Smsmith 16937785Smsmithstatic int 17037785Smsmithwait_eeprom_ready(struct cs_softc *sc) 17137785Smsmith{ 172179620Simp int i; 173179620Simp 174179620Simp /* 175179620Simp * From the CS8900A datasheet, section 3.5.2: 176179620Simp * "Before issuing any command to the EEPROM, the host must wait 177179620Simp * for the SIBUSY bit (Register 16, SelfST, bit 8) to clear. After 178179620Simp * each command has been issued, the host must wait again for SIBUSY 179179620Simp * to clear." 180179620Simp * 181179620Simp * Before we issue the command, we should be !busy, so that will 182179620Simp * be fast. The datasheet suggests that clock out from the part 183179620Simp * per word will be on the order of 25us, which is consistant with 184179620Simp * the 1MHz serial clock and 16bits... We should never hit 100, 185179621Simp * let alone 15,000 here. The original code did an unconditional 186179621Simp * 30ms DELAY here. Bad Kharma. cs_readreg takes ~2us. 187179620Simp */ 188179621Simp for (i = 0; i < 15000; i++) /* 30ms max */ 189179620Simp if (!(cs_readreg(sc, PP_SelfST) & SI_BUSY)) 190179620Simp return (0); 191179620Simp return (1); 19237785Smsmith} 19337785Smsmith 19437785Smsmithstatic void 19537785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off) 19637785Smsmith{ 19737785Smsmith unsigned int self_control = HCB1_ENBL; 19837785Smsmith 19937785Smsmith if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off) 20037785Smsmith self_control |= HCB1; 20137785Smsmith else 20237785Smsmith self_control &= ~HCB1; 20372940Simp cs_writereg(sc, PP_SelfCTL, self_control); 204180388Simp DELAY(500000); /* Bad! */ 20537785Smsmith} 20637785Smsmith 20737785Smsmith 20837785Smsmithstatic int 20937785Smsmithcs_duplex_auto(struct cs_softc *sc) 21037785Smsmith{ 211140927Simp int i, error=0; 212140927Simp 21372940Simp cs_writereg(sc, PP_AutoNegCTL, 21472940Simp RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE); 215140927Simp for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) { 216179597Simp if (i > 4000) { 217162321Sglebius device_printf(sc->dev, 218140927Simp "full/half duplex auto negotiation timeout\n"); 21937785Smsmith error = ETIMEDOUT; 220140927Simp break; 221140927Simp } 222140927Simp DELAY(1000); 223140927Simp } 224140928Simp return (error); 22537785Smsmith} 22637785Smsmith 22737785Smsmithstatic int 22837785Smsmithenable_tp(struct cs_softc *sc) 22937785Smsmith{ 23037785Smsmith 23172940Simp cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY); 23237785Smsmith control_dc_dc(sc, 0); 233140928Simp return (0); 23437785Smsmith} 23537785Smsmith 23637785Smsmithstatic int 23737785Smsmithenable_aui(struct cs_softc *sc) 23837785Smsmith{ 23937785Smsmith 24072940Simp cs_writereg(sc, PP_LineCTL, 24172940Simp (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY); 242179597Simp control_dc_dc(sc, 0); 243140928Simp return (0); 24437785Smsmith} 24537785Smsmith 24637785Smsmithstatic int 24737785Smsmithenable_bnc(struct cs_softc *sc) 24837785Smsmith{ 24937785Smsmith 25072940Simp cs_writereg(sc, PP_LineCTL, 25172940Simp (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY); 252179597Simp control_dc_dc(sc, 1); 253140928Simp return (0); 25437785Smsmith} 25537785Smsmith 25671316Simpint 25758816Simpcs_cs89x0_probe(device_t dev) 25837785Smsmith{ 25958816Simp int i; 26058816Simp int error; 26158816Simp u_long irq, junk; 26258816Simp struct cs_softc *sc = device_get_softc(dev); 26337785Smsmith unsigned rev_type = 0; 264122024Simp uint16_t id; 26558816Simp char chip_revision; 266122024Simp uint16_t eeprom_buff[CHKSUM_LEN]; 267269980Simp int chip_type, pp_isaint; 26837785Smsmith 269179620Simp sc->dev = dev; 27058816Simp error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS); 27158816Simp if (error) 27258816Simp return (error); 27358816Simp 27472940Simp if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) { 27537785Smsmith /* Chip not detected. Let's try to reset it */ 27637785Smsmith if (bootverbose) 27758816Simp device_printf(dev, "trying to reset the chip.\n"); 27872940Simp cs_outw(sc, ADD_PORT, PP_SelfCTL); 27972940Simp i = cs_inw(sc, DATA_PORT); 28072940Simp cs_outw(sc, ADD_PORT, PP_SelfCTL); 28172940Simp cs_outw(sc, DATA_PORT, i | POWER_ON_RESET); 28272940Simp if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) 28358816Simp return (ENXIO); 28437785Smsmith } 28537785Smsmith 28672940Simp for (i = 0; i < 10000; i++) { 28772940Simp id = cs_readreg(sc, PP_ChipID); 28872940Simp if (id == CHIP_EISA_ID_SIG) 28972940Simp break; 29072940Simp } 29172940Simp if (i == 10000) 29258816Simp return (ENXIO); 29337785Smsmith 29472940Simp rev_type = cs_readreg(sc, PRODUCT_ID_ADD); 29537785Smsmith chip_type = rev_type & ~REVISON_BITS; 29637785Smsmith chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A'; 29737785Smsmith 29837785Smsmith sc->chip_type = chip_type; 29958816Simp 300179532Simp if (chip_type == CS8900) { 30137785Smsmith pp_isaint = PP_CS8900_ISAINT; 30237785Smsmith sc->send_cmd = TX_CS8900_AFTER_ALL; 30337785Smsmith } else { 30437785Smsmith pp_isaint = PP_CS8920_ISAINT; 30537785Smsmith sc->send_cmd = TX_CS8920_AFTER_ALL; 30637785Smsmith } 30737785Smsmith 30837785Smsmith /* 309140927Simp * Clear some fields so that fail of EEPROM will left them clean 310140927Simp */ 311140927Simp sc->auto_neg_cnf = 0; 312140927Simp sc->adapter_cnf = 0; 313140927Simp sc->isa_config = 0; 314140927Simp 315140927Simp /* 316179532Simp * If no interrupt specified, use what the board tells us. 31737785Smsmith */ 31858816Simp error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); 31958816Simp 32058816Simp /* 32158816Simp * Get data from EEPROM 32258816Simp */ 32372940Simp if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) { 32458816Simp device_printf(dev, "No EEPROM, assuming defaults.\n"); 325179532Simp } else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) { 326179532Simp device_printf(dev, "EEPROM read failed, assuming defaults.\n"); 327179532Simp } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) { 328179532Simp device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n"); 32937785Smsmith } else { 330180388Simp sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET]; 331180388Simp sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET]; 332180388Simp sc->isa_config = eeprom_buff[ISA_CNF_OFFSET]; 333179532Simp for (i=0; i<ETHER_ADDR_LEN/2; i++) { 334179532Simp sc->enaddr[i*2] = eeprom_buff[i]; 335179532Simp sc->enaddr[i*2+1] = eeprom_buff[i] >> 8; 336179532Simp } 337179532Simp /* 338179532Simp * If no interrupt specified, use what the 339179532Simp * board tells us. 340179532Simp */ 341179532Simp if (error) { 342179532Simp irq = sc->isa_config & INT_NO_MASK; 343179532Simp error = 0; 344179532Simp if (chip_type == CS8900) { 345179532Simp irq = cs8900_eeint2irq[irq]; 34637785Smsmith } else { 347179532Simp if (irq > CS8920_NO_INTS) 348179532Simp irq = 255; 34937785Smsmith } 350179532Simp if (irq == 255) { 351179532Simp device_printf(dev, "invalid irq in EEPROM.\n"); 352179532Simp error = EINVAL; 353179532Simp } 354179532Simp if (!error) 355179532Simp bus_set_resource(dev, SYS_RES_IRQ, 0, 356179532Simp irq, 1); 357140927Simp } 358140927Simp } 35937785Smsmith 360179532Simp if (!error && !(sc->flags & CS_NO_IRQ)) { 361140927Simp if (chip_type == CS8900) { 362217538Sjhb if (irq < 16) 363179532Simp irq = cs8900_irq2eeint[irq]; 364179532Simp else 365179532Simp irq = 255; 366140927Simp } else { 367179532Simp if (irq > CS8920_NO_INTS) 368179532Simp irq = 255; 369140927Simp } 370179532Simp if (irq == 255) 371179532Simp error = EINVAL; 37258816Simp } 37358816Simp 374179507Simp if (error) { 37558816Simp device_printf(dev, "Unknown or invalid irq\n"); 376179507Simp return (error); 377140927Simp } 378140927Simp 379179507Simp if (!(sc->flags & CS_NO_IRQ)) 380179507Simp cs_writereg(sc, pp_isaint, irq); 381179507Simp 38237785Smsmith if (bootverbose) 38358816Simp device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n", 384179532Simp chip_type == CS8900 ? '0' : '2', 385179532Simp chip_type == CS8920M ? "M" : "", 38637785Smsmith chip_revision, 38737785Smsmith (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "", 38837785Smsmith (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "", 38958816Simp (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : ""); 39037785Smsmith 391140927Simp if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) && 392140927Simp (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH)) 393140927Simp sc->line_ctl = LOW_RX_SQUELCH; 394140927Simp else 395140927Simp sc->line_ctl = 0; 39637785Smsmith 397140928Simp return (0); 39837785Smsmith} 39937785Smsmith 40037785Smsmith/* 40158816Simp * Allocate a port resource with the given resource id. 40258816Simp */ 403179560Sjhbint 404179560Sjhbcs_alloc_port(device_t dev, int rid, int size) 40558816Simp{ 406140927Simp struct cs_softc *sc = device_get_softc(dev); 407140927Simp struct resource *res; 40858816Simp 409140927Simp res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 410140927Simp 0ul, ~0ul, size, RF_ACTIVE); 411140928Simp if (res == NULL) 412140927Simp return (ENOENT); 413140928Simp sc->port_rid = rid; 414140928Simp sc->port_res = res; 415140928Simp return (0); 41658816Simp} 41758816Simp 41858816Simp/* 41958816Simp * Allocate an irq resource with the given resource id. 42058816Simp */ 421179560Sjhbint 422179597Simpcs_alloc_irq(device_t dev, int rid) 42358816Simp{ 424140927Simp struct cs_softc *sc = device_get_softc(dev); 425140927Simp struct resource *res; 42658816Simp 427179597Simp res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 428140928Simp if (res == NULL) 429140927Simp return (ENOENT); 430140928Simp sc->irq_rid = rid; 431140928Simp sc->irq_res = res; 432140928Simp return (0); 43358816Simp} 43458816Simp 43558816Simp/* 43658816Simp * Release all resources 43758816Simp */ 438179560Sjhbvoid 439179560Sjhbcs_release_resources(device_t dev) 44058816Simp{ 441140927Simp struct cs_softc *sc = device_get_softc(dev); 44258816Simp 443140927Simp if (sc->port_res) { 444140927Simp bus_release_resource(dev, SYS_RES_IOPORT, 445140927Simp sc->port_rid, sc->port_res); 446140927Simp sc->port_res = 0; 447140927Simp } 448140927Simp if (sc->irq_res) { 449140927Simp bus_release_resource(dev, SYS_RES_IRQ, 450140927Simp sc->irq_rid, sc->irq_res); 451140927Simp sc->irq_res = 0; 452140927Simp } 45358816Simp} 45458816Simp 45558816Simp/* 45637785Smsmith * Install the interface into kernel networking data structures 45737785Smsmith */ 45871316Simpint 459121816Sbrookscs_attach(device_t dev) 46037785Smsmith{ 461179560Sjhb int error, media=0; 462201758Smbr struct cs_softc *sc = device_get_softc(dev); 463147256Sbrooks struct ifnet *ifp; 46437785Smsmith 465162321Sglebius sc->dev = dev; 466162321Sglebius 467147256Sbrooks ifp = sc->ifp = if_alloc(IFT_ETHER); 468147256Sbrooks if (ifp == NULL) { 469147256Sbrooks device_printf(dev, "can not if_alloc()\n"); 470179560Sjhb cs_release_resources(dev); 471179560Sjhb return (ENOMEM); 472147256Sbrooks } 473147256Sbrooks 474179560Sjhb mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK, 475179560Sjhb MTX_DEF); 476179560Sjhb callout_init_mtx(&sc->timer, &sc->lock, 0); 47758816Simp 478179560Sjhb CS_LOCK(sc); 479179560Sjhb cs_stop(sc); 480179560Sjhb CS_UNLOCK(sc); 481179560Sjhb 482121752Sbrooks ifp->if_softc=sc; 483121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 484121752Sbrooks ifp->if_start=cs_start; 485121752Sbrooks ifp->if_ioctl=cs_ioctl; 486121752Sbrooks ifp->if_init=cs_init; 487207554Ssobomax IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 48837785Smsmith 489179560Sjhb ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 49037785Smsmith 491121752Sbrooks /* 492121752Sbrooks * this code still in progress (DMA support) 493121752Sbrooks * 49437785Smsmith 495121752Sbrooks sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT); 496121752Sbrooks if (sc->recv_ring == NULL) { 497121816Sbrooks log(LOG_ERR, 498121816Sbrooks "%s: Couldn't allocate memory for NIC\n", ifp->if_xname); 499121752Sbrooks return(0); 500121752Sbrooks } 501121752Sbrooks if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF)) 502121752Sbrooks < (128*1024-CS_DMA_BUFFER_SIZE)) 503121752Sbrooks sc->recv_ring+=16*1024; 50437785Smsmith 505121752Sbrooks */ 50637785Smsmith 507121752Sbrooks sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT); 508121752Sbrooks if (sc->buffer == NULL) { 509162321Sglebius device_printf(sc->dev, "Couldn't allocate memory for NIC\n"); 510179560Sjhb if_free(ifp); 511179560Sjhb mtx_destroy(&sc->lock); 512179560Sjhb cs_release_resources(dev); 513179560Sjhb return(ENOMEM); 514121752Sbrooks } 51537785Smsmith 516121752Sbrooks /* 517121752Sbrooks * Initialize the media structures. 518121752Sbrooks */ 519121752Sbrooks ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus); 52037785Smsmith 521121752Sbrooks if (sc->adapter_cnf & A_CNF_10B_T) { 522121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL); 523121752Sbrooks if (sc->chip_type != CS8900) { 524121752Sbrooks ifmedia_add(&sc->media, 525121752Sbrooks IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL); 526121752Sbrooks ifmedia_add(&sc->media, 527121752Sbrooks IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL); 528121752Sbrooks } 529121752Sbrooks } 53037785Smsmith 531121752Sbrooks if (sc->adapter_cnf & A_CNF_10B_2) 532121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL); 53337785Smsmith 534121752Sbrooks if (sc->adapter_cnf & A_CNF_AUI) 535121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL); 53637785Smsmith 537121752Sbrooks if (sc->adapter_cnf & A_CNF_MEDIA) 538121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL); 53937785Smsmith 540121752Sbrooks /* Set default media from EEPROM */ 541121752Sbrooks switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) { 542121752Sbrooks case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break; 543121752Sbrooks case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break; 544121752Sbrooks case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break; 545121752Sbrooks case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break; 546122024Simp default: 547162321Sglebius device_printf(sc->dev, "no media, assuming 10baseT\n"); 548122024Simp sc->adapter_cnf |= A_CNF_10B_T; 549122024Simp ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL); 550122024Simp if (sc->chip_type != CS8900) { 551122024Simp ifmedia_add(&sc->media, 552122024Simp IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL); 553122024Simp ifmedia_add(&sc->media, 554122024Simp IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL); 555122024Simp } 556122024Simp media = IFM_ETHER | IFM_10_T; 557122024Simp break; 55837785Smsmith } 559121752Sbrooks ifmedia_set(&sc->media, media); 560121752Sbrooks cs_mediaset(sc, media); 56137785Smsmith 562147256Sbrooks ether_ifattach(ifp, sc->enaddr); 563121752Sbrooks 564179560Sjhb error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE, 565179560Sjhb NULL, csintr, sc, &sc->irq_handle); 566179560Sjhb if (error) { 567179560Sjhb ether_ifdetach(ifp); 568179560Sjhb free(sc->buffer, M_DEVBUF); 569179560Sjhb if_free(ifp); 570179560Sjhb mtx_destroy(&sc->lock); 571179560Sjhb cs_release_resources(dev); 572179560Sjhb return (error); 573179560Sjhb } 574179560Sjhb 57558816Simp return (0); 57637785Smsmith} 57737785Smsmith 578140888Simpint 579140888Simpcs_detach(device_t dev) 580140888Simp{ 581140888Simp struct cs_softc *sc; 582140888Simp struct ifnet *ifp; 583140888Simp 584140888Simp sc = device_get_softc(dev); 585147256Sbrooks ifp = sc->ifp; 586140888Simp 587179560Sjhb CS_LOCK(sc); 588140888Simp cs_stop(sc); 589179560Sjhb CS_UNLOCK(sc); 590179560Sjhb callout_drain(&sc->timer); 591140888Simp ether_ifdetach(ifp); 592179560Sjhb bus_teardown_intr(dev, sc->irq_res, sc->irq_handle); 593150306Simp cs_release_resources(dev); 594179560Sjhb free(sc->buffer, M_DEVBUF); 595147256Sbrooks if_free(ifp); 596179560Sjhb mtx_destroy(&sc->lock); 597140888Simp return (0); 598140888Simp} 599140888Simp 60037785Smsmith/* 60137785Smsmith * Initialize the board 60237785Smsmith */ 60337785Smsmithstatic void 60437785Smsmithcs_init(void *xsc) 60537785Smsmith{ 60637785Smsmith struct cs_softc *sc=(struct cs_softc *)xsc; 607179560Sjhb 608179560Sjhb CS_LOCK(sc); 609179560Sjhb cs_init_locked(sc); 610179560Sjhb CS_UNLOCK(sc); 611179560Sjhb} 612179560Sjhb 613179560Sjhbstatic void 614179560Sjhbcs_init_locked(struct cs_softc *sc) 615179560Sjhb{ 616147256Sbrooks struct ifnet *ifp = sc->ifp; 617179560Sjhb int i, rx_cfg; 61837785Smsmith 61937785Smsmith /* 620179560Sjhb * reset watchdog timer 62137785Smsmith */ 622179560Sjhb sc->tx_timeout = 0; 62337785Smsmith sc->buf_len = 0; 62437785Smsmith 62537785Smsmith /* 62637785Smsmith * Hardware initialization of cs 62737785Smsmith */ 62837785Smsmith 62937785Smsmith /* Enable receiver and transmitter */ 63072940Simp cs_writereg(sc, PP_LineCTL, 63172940Simp cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON); 63237785Smsmith 63337785Smsmith /* Configure the receiver mode */ 63437785Smsmith cs_setmode(sc); 63537785Smsmith 63637785Smsmith /* 63737785Smsmith * This defines what type of frames will cause interrupts 63837785Smsmith * Bad frames should generate interrupts so that the driver 63937785Smsmith * could track statistics of discarded packets 64037785Smsmith */ 641140927Simp rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL | 64237785Smsmith RX_EXTRA_DATA_ENBL; 64337785Smsmith if (sc->isa_config & STREAM_TRANSFER) 64437785Smsmith rx_cfg |= RX_STREAM_ENBL; 64572940Simp cs_writereg(sc, PP_RxCFG, rx_cfg); 64672940Simp cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL | 64737785Smsmith TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL | 64837785Smsmith TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL); 64972940Simp cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL | 65037785Smsmith RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL | 65137785Smsmith TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/); 65237785Smsmith 653140927Simp /* Write MAC address into IA filter */ 654140927Simp for (i=0; i<ETHER_ADDR_LEN/2; i++) 655140927Simp cs_writereg(sc, PP_IA + i * 2, 656147256Sbrooks sc->enaddr[i * 2] | 657147256Sbrooks (sc->enaddr[i * 2 + 1] << 8) ); 65837785Smsmith 65937785Smsmith /* 66037785Smsmith * Now enable everything 66137785Smsmith */ 66237785Smsmith/* 66337785Smsmith#ifdef CS_USE_64K_DMA 66472940Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K); 66572940Simp#else 666140927Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ); 66737785Smsmith#endif 66837785Smsmith*/ 66972940Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ); 67037785Smsmith 67137785Smsmith /* 67237785Smsmith * Set running and clear output active flags 67337785Smsmith */ 674148887Srwatson sc->ifp->if_drv_flags |= IFF_DRV_RUNNING; 675148887Srwatson sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 676179560Sjhb callout_reset(&sc->timer, hz, cs_watchdog, sc); 67737785Smsmith 67837785Smsmith /* 67937785Smsmith * Start sending process 68037785Smsmith */ 681179560Sjhb cs_start_locked(ifp); 68237785Smsmith} 68337785Smsmith 68437785Smsmith/* 685106937Ssam * Get the packet from the board and send it to the upper layer. 68637785Smsmith */ 68737785Smsmithstatic int 68837785Smsmithcs_get_packet(struct cs_softc *sc) 68937785Smsmith{ 690147256Sbrooks struct ifnet *ifp = sc->ifp; 691218055Sandrew int status, length; 69237785Smsmith struct mbuf *m; 69337785Smsmith 69437785Smsmith#ifdef CS_DEBUG 69537785Smsmith int i; 69637785Smsmith#endif 69737785Smsmith 69872940Simp status = cs_inw(sc, RX_FRAME_PORT); 69972940Simp length = cs_inw(sc, RX_FRAME_PORT); 70037785Smsmith 70137785Smsmith#ifdef CS_DEBUG 702162321Sglebius device_printf(sc->dev, "rcvd: stat %x, len %d\n", 703104252Sbrooks status, length); 70437785Smsmith#endif 70537785Smsmith 70637785Smsmith if (!(status & RX_OK)) { 70737785Smsmith#ifdef CS_DEBUG 708162321Sglebius device_printf(sc->dev, "bad pkt stat %x\n", status); 70937785Smsmith#endif 710271836Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 711140928Simp return (-1); 71237785Smsmith } 71337785Smsmith 714243857Sglebius MGETHDR(m, M_NOWAIT, MT_DATA); 71537785Smsmith if (m==NULL) 716140928Simp return (-1); 71737785Smsmith 71837785Smsmith if (length > MHLEN) { 719243857Sglebius MCLGET(m, M_NOWAIT); 72037785Smsmith if (!(m->m_flags & M_EXT)) { 72137785Smsmith m_freem(m); 722140928Simp return (-1); 72337785Smsmith } 72437785Smsmith } 72537785Smsmith 72637785Smsmith /* Initialize packet's header info */ 72737785Smsmith m->m_pkthdr.rcvif = ifp; 72837785Smsmith m->m_pkthdr.len = length; 72937785Smsmith m->m_len = length; 73037785Smsmith 73137785Smsmith /* Get the data */ 732218055Sandrew bus_read_multi_2(sc->port_res, RX_FRAME_PORT, mtod(m, uint16_t *), 733218055Sandrew (length + 1) >> 1); 73437785Smsmith 73537785Smsmith#ifdef CS_DEBUG 73637785Smsmith for (i=0;i<length;i++) 73737785Smsmith printf(" %02x",(unsigned char)*((char *)(m->m_data+i))); 73837785Smsmith printf( "\n" ); 73937785Smsmith#endif 74037785Smsmith 74137785Smsmith if (status & (RX_IA | RX_BROADCAST) || 74237785Smsmith (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) { 74337785Smsmith /* Feed the packet to the upper layer */ 744106937Ssam (*ifp->if_input)(ifp, m); 745271836Sglebius if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 746122024Simp if (length == ETHER_MAX_LEN-ETHER_CRC_LEN) 747140927Simp DELAY(cs_recv_delay); 74837785Smsmith } else { 74937785Smsmith m_freem(m); 75037785Smsmith } 75137785Smsmith 752140928Simp return (0); 75337785Smsmith} 75437785Smsmith 75537785Smsmith/* 75658816Simp * Handle interrupts 75737785Smsmith */ 75858816Simpvoid 75958816Simpcsintr(void *arg) 76037785Smsmith{ 76158816Simp struct cs_softc *sc = (struct cs_softc*) arg; 762147256Sbrooks struct ifnet *ifp = sc->ifp; 76341591Sarchie int status; 76437785Smsmith 76537785Smsmith#ifdef CS_DEBUG 766162321Sglebius device_printf(sc->dev, "Interrupt.\n"); 76737785Smsmith#endif 76837785Smsmith 769179560Sjhb CS_LOCK(sc); 77072940Simp while ((status=cs_inw(sc, ISQ_PORT))) { 77137785Smsmith 77237785Smsmith#ifdef CS_DEBUG 773162321Sglebius device_printf(sc->dev, "from ISQ: %04x\n", status); 77437785Smsmith#endif 77537785Smsmith 77637785Smsmith switch (status & ISQ_EVENT_MASK) { 777140927Simp case ISQ_RECEIVER_EVENT: 778140927Simp cs_get_packet(sc); 779140927Simp break; 78037785Smsmith 781140927Simp case ISQ_TRANSMITTER_EVENT: 782140927Simp if (status & TX_OK) 783271836Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 784140927Simp else 785271836Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 786148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 787179560Sjhb sc->tx_timeout = 0; 788140927Simp break; 78937785Smsmith 790140927Simp case ISQ_BUFFER_EVENT: 791140927Simp if (status & READY_FOR_TX) { 792148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 793179560Sjhb sc->tx_timeout = 0; 794140927Simp } 79537785Smsmith 796140927Simp if (status & TX_UNDERRUN) { 797148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 798179560Sjhb sc->tx_timeout = 0; 799271836Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 800140927Simp } 801140927Simp break; 80237785Smsmith 803140927Simp case ISQ_RX_MISS_EVENT: 804271836Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, status >> 6); 805140927Simp break; 80637785Smsmith 807140927Simp case ISQ_TX_COL_EVENT: 808271836Sglebius if_inc_counter(ifp, IFCOUNTER_COLLISIONS, status >> 6); 809140927Simp break; 810140927Simp } 811140927Simp } 81237785Smsmith 813148887Srwatson if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) { 814179560Sjhb cs_start_locked(ifp); 815140927Simp } 816179560Sjhb CS_UNLOCK(sc); 81737785Smsmith} 81837785Smsmith 81937785Smsmith/* 82037785Smsmith * Save the data in buffer 82137785Smsmith */ 82237785Smsmith 82337785Smsmithstatic void 82437785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m ) 82537785Smsmith{ 82637785Smsmith int len; 82737785Smsmith struct mbuf *mp; 82837785Smsmith unsigned char *data, *buf; 82937785Smsmith 83037785Smsmith for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) { 83137785Smsmith len = mp->m_len; 83237785Smsmith 83337785Smsmith /* 83437785Smsmith * Ignore empty parts 83537785Smsmith */ 83637785Smsmith if (!len) 837202580Simp continue; 83837785Smsmith 83937785Smsmith /* 84037785Smsmith * Find actual data address 84137785Smsmith */ 84237785Smsmith data = mtod(mp, caddr_t); 84337785Smsmith 84437785Smsmith bcopy((caddr_t) data, (caddr_t) buf, len); 84537785Smsmith buf += len; 84637785Smsmith sc->buf_len += len; 84737785Smsmith } 84837785Smsmith} 84937785Smsmith 85037785Smsmith 85137785Smsmithstatic void 85237785Smsmithcs_xmit_buf( struct cs_softc *sc ) 85337785Smsmith{ 854218055Sandrew bus_write_multi_2(sc->port_res, TX_FRAME_PORT, (uint16_t *)sc->buffer, 855218055Sandrew (sc->buf_len + 1) >> 1); 85637785Smsmith sc->buf_len = 0; 85737785Smsmith} 85837785Smsmith 85937785Smsmithstatic void 86037785Smsmithcs_start(struct ifnet *ifp) 86137785Smsmith{ 862179560Sjhb struct cs_softc *sc = ifp->if_softc; 863179560Sjhb 864179560Sjhb CS_LOCK(sc); 865179560Sjhb cs_start_locked(ifp); 866179560Sjhb CS_UNLOCK(sc); 867179560Sjhb} 868179560Sjhb 869179560Sjhbstatic void 870179560Sjhbcs_start_locked(struct ifnet *ifp) 871179560Sjhb{ 872179560Sjhb int length; 87337785Smsmith struct mbuf *m, *mp; 87437785Smsmith struct cs_softc *sc = ifp->if_softc; 87537785Smsmith 87637785Smsmith for (;;) { 87737785Smsmith if (sc->buf_len) 87837785Smsmith length = sc->buf_len; 87937785Smsmith else { 88037785Smsmith IF_DEQUEUE( &ifp->if_snd, m ); 88137785Smsmith 88237785Smsmith if (m==NULL) { 88337785Smsmith return; 88437785Smsmith } 88537785Smsmith 88637785Smsmith for (length=0, mp=m; mp != NULL; mp=mp->m_next) 88737785Smsmith length += mp->m_len; 88837785Smsmith 88937785Smsmith /* Skip zero-length packets */ 89037785Smsmith if (length == 0) { 89137785Smsmith m_freem(m); 89237785Smsmith continue; 89337785Smsmith } 89437785Smsmith 89537785Smsmith cs_write_mbufs(sc, m); 89637785Smsmith 897106937Ssam BPF_MTAP(ifp, m); 89837785Smsmith 89937785Smsmith m_freem(m); 90037785Smsmith } 90137785Smsmith 90237785Smsmith /* 90337785Smsmith * Issue a SEND command 90437785Smsmith */ 90572940Simp cs_outw(sc, TX_CMD_PORT, sc->send_cmd); 90672940Simp cs_outw(sc, TX_LEN_PORT, length ); 90737785Smsmith 90837785Smsmith /* 90937785Smsmith * If there's no free space in the buffer then leave 91037785Smsmith * this packet for the next time: indicate output active 91137785Smsmith * and return. 91237785Smsmith */ 91372940Simp if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) { 914179560Sjhb sc->tx_timeout = sc->buf_len; 915148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 91637785Smsmith return; 91737785Smsmith } 91837785Smsmith 919140927Simp cs_xmit_buf(sc); 92037785Smsmith 92137785Smsmith /* 92237785Smsmith * Set the watchdog timer in case we never hear 92337785Smsmith * from board again. (I don't know about correct 92437785Smsmith * value for this timeout) 92537785Smsmith */ 926179560Sjhb sc->tx_timeout = length; 92737785Smsmith 928148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 92937785Smsmith return; 93037785Smsmith } 93137785Smsmith} 93237785Smsmith 93337785Smsmith/* 93437785Smsmith * Stop everything on the interface 93537785Smsmith */ 93637785Smsmithstatic void 93737785Smsmithcs_stop(struct cs_softc *sc) 93837785Smsmith{ 93937785Smsmith 940179560Sjhb CS_ASSERT_LOCKED(sc); 94172940Simp cs_writereg(sc, PP_RxCFG, 0); 94272940Simp cs_writereg(sc, PP_TxCFG, 0); 94372940Simp cs_writereg(sc, PP_BufCFG, 0); 94472940Simp cs_writereg(sc, PP_BusCTL, 0); 94537785Smsmith 946148887Srwatson sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 947179560Sjhb sc->tx_timeout = 0; 948179560Sjhb callout_stop(&sc->timer); 94937785Smsmith} 95037785Smsmith 95137785Smsmith/* 95237785Smsmith * Reset the interface 95337785Smsmith */ 95437785Smsmithstatic void 95537785Smsmithcs_reset(struct cs_softc *sc) 95637785Smsmith{ 957179560Sjhb 958179560Sjhb CS_ASSERT_LOCKED(sc); 95937785Smsmith cs_stop(sc); 960179560Sjhb cs_init_locked(sc); 96137785Smsmith} 96237785Smsmith 963156559Sphilipstatic uint16_t 964156559Sphilipcs_hash_index(struct sockaddr_dl *addr) 965156559Sphilip{ 966156559Sphilip uint32_t crc; 967156559Sphilip uint16_t idx; 968156559Sphilip caddr_t lla; 969156559Sphilip 970156559Sphilip lla = LLADDR(addr); 971156559Sphilip crc = ether_crc32_le(lla, ETHER_ADDR_LEN); 972156559Sphilip idx = crc >> 26; 973156559Sphilip 974156559Sphilip return (idx); 975156559Sphilip} 976156559Sphilip 97737785Smsmithstatic void 97837785Smsmithcs_setmode(struct cs_softc *sc) 97937785Smsmith{ 980156559Sphilip int rx_ctl; 981156559Sphilip uint16_t af[4]; 982156559Sphilip uint16_t port, mask, index; 983147256Sbrooks struct ifnet *ifp = sc->ifp; 984156559Sphilip struct ifmultiaddr *ifma; 98537785Smsmith 98637785Smsmith /* Stop the receiver while changing filters */ 98772940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON); 98837785Smsmith 98937785Smsmith if (ifp->if_flags & IFF_PROMISC) { 99037785Smsmith /* Turn on promiscuous mode. */ 99137785Smsmith rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT; 992156559Sphilip } else if (ifp->if_flags & IFF_MULTICAST) { 993156559Sphilip /* Allow receiving frames with multicast addresses */ 994156559Sphilip rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT | 995156559Sphilip RX_OK_ACCEPT | RX_MULTCAST_ACCEPT; 996156559Sphilip 997156559Sphilip /* Start with an empty filter */ 998156559Sphilip af[0] = af[1] = af[2] = af[3] = 0x0000; 999156559Sphilip 1000156559Sphilip if (ifp->if_flags & IFF_ALLMULTI) { 1001156559Sphilip /* Accept all multicast frames */ 1002156559Sphilip af[0] = af[1] = af[2] = af[3] = 0xffff; 100337785Smsmith } else { 1004156559Sphilip /* 1005156559Sphilip * Set up the filter to only accept multicast 1006156559Sphilip * frames we're interested in. 100737785Smsmith */ 1008195049Srwatson if_maddr_rlock(ifp); 1009156559Sphilip TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1010156559Sphilip struct sockaddr_dl *dl = 1011156559Sphilip (struct sockaddr_dl *)ifma->ifma_addr; 1012156559Sphilip 1013156559Sphilip index = cs_hash_index(dl); 1014156559Sphilip port = (u_int16_t) (index >> 4); 1015156559Sphilip mask = (u_int16_t) (1 << (index & 0xf)); 1016156559Sphilip af[port] |= mask; 1017156559Sphilip } 1018195049Srwatson if_maddr_runlock(ifp); 101937785Smsmith } 1020156559Sphilip 1021156559Sphilip cs_writereg(sc, PP_LAF + 0, af[0]); 1022156559Sphilip cs_writereg(sc, PP_LAF + 2, af[1]); 1023156559Sphilip cs_writereg(sc, PP_LAF + 4, af[2]); 1024156559Sphilip cs_writereg(sc, PP_LAF + 6, af[3]); 1025156559Sphilip } else { 1026156559Sphilip /* 1027156559Sphilip * Receive only good frames addressed for us and 1028156559Sphilip * good broadcasts. 1029156559Sphilip */ 1030156559Sphilip rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT | 1031156559Sphilip RX_OK_ACCEPT; 103237785Smsmith } 103337785Smsmith 103437785Smsmith /* Set up the filter */ 103572940Simp cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl); 103637785Smsmith 103737785Smsmith /* Turn on receiver */ 103872940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON); 103937785Smsmith} 104037785Smsmith 104137785Smsmithstatic int 104237785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data) 104337785Smsmith{ 104437785Smsmith struct cs_softc *sc=ifp->if_softc; 104537785Smsmith struct ifreq *ifr = (struct ifreq *)data; 1046179560Sjhb int error=0; 104737785Smsmith 104837785Smsmith#ifdef CS_DEBUG 1049162321Sglebius if_printf(ifp, "%s command=%lx\n", __func__, command); 105037785Smsmith#endif 105137785Smsmith 105237785Smsmith switch (command) { 105337785Smsmith case SIOCSIFFLAGS: 105437785Smsmith /* 105537785Smsmith * Switch interface state between "running" and 105637785Smsmith * "stopped", reflecting the UP flag. 1057140927Simp */ 1058179560Sjhb CS_LOCK(sc); 1059147256Sbrooks if (sc->ifp->if_flags & IFF_UP) { 1060148887Srwatson if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) { 1061179560Sjhb cs_init_locked(sc); 1062140927Simp } 1063140927Simp } else { 1064148887Srwatson if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) { 1065140927Simp cs_stop(sc); 1066140927Simp } 1067179560Sjhb } 106837785Smsmith /* 106937785Smsmith * Promiscuous and/or multicast flags may have changed, 107037785Smsmith * so reprogram the multicast filter and/or receive mode. 107137785Smsmith * 107237785Smsmith * See note about multicasts in cs_setmode 107337785Smsmith */ 107437785Smsmith cs_setmode(sc); 1075179560Sjhb CS_UNLOCK(sc); 107637785Smsmith break; 107737785Smsmith 107837785Smsmith case SIOCADDMULTI: 107937785Smsmith case SIOCDELMULTI: 108037785Smsmith /* 108137785Smsmith * Multicast list has changed; set the hardware filter 108237785Smsmith * accordingly. 108337785Smsmith * 108437785Smsmith * See note about multicasts in cs_setmode 108537785Smsmith */ 1086179560Sjhb CS_LOCK(sc); 108737785Smsmith cs_setmode(sc); 1088179560Sjhb CS_UNLOCK(sc); 108937785Smsmith error = 0; 109037785Smsmith break; 109137785Smsmith 1092140927Simp case SIOCSIFMEDIA: 1093140927Simp case SIOCGIFMEDIA: 1094140927Simp error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 1095140927Simp break; 109637785Smsmith 1097140927Simp default: 1098140921Simp error = ether_ioctl(ifp, command, data); 1099106937Ssam break; 1100140927Simp } 110137785Smsmith 1102140928Simp return (error); 110337785Smsmith} 110437785Smsmith 110537785Smsmith/* 110637785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to 110737785Smsmith * generate an interrupt after a transmit has been started on it. 110837785Smsmith */ 110937785Smsmithstatic void 1110179560Sjhbcs_watchdog(void *arg) 111137785Smsmith{ 1112179560Sjhb struct cs_softc *sc = arg; 1113179560Sjhb struct ifnet *ifp = sc->ifp; 111437785Smsmith 1115179560Sjhb CS_ASSERT_LOCKED(sc); 1116179560Sjhb if (sc->tx_timeout && --sc->tx_timeout == 0) { 1117271836Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1118179560Sjhb log(LOG_ERR, "%s: device timeout\n", ifp->if_xname); 111937785Smsmith 1120179560Sjhb /* Reset the interface */ 1121179560Sjhb if (ifp->if_flags & IFF_UP) 1122179560Sjhb cs_reset(sc); 1123179560Sjhb else 1124179560Sjhb cs_stop(sc); 1125179560Sjhb } 1126179560Sjhb callout_reset(&sc->timer, hz, cs_watchdog, sc); 112737785Smsmith} 112837785Smsmith 112937785Smsmithstatic int 113037785Smsmithcs_mediachange(struct ifnet *ifp) 113137785Smsmith{ 113237785Smsmith struct cs_softc *sc = ifp->if_softc; 113337785Smsmith struct ifmedia *ifm = &sc->media; 1134179560Sjhb int error; 113537785Smsmith 113637785Smsmith if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 1137140928Simp return (EINVAL); 113837785Smsmith 1139179560Sjhb CS_LOCK(sc); 1140179560Sjhb error = cs_mediaset(sc, ifm->ifm_media); 1141179560Sjhb CS_UNLOCK(sc); 1142179560Sjhb return (error); 114337785Smsmith} 114437785Smsmith 114537785Smsmithstatic void 114637785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 114737785Smsmith{ 114837785Smsmith int line_status; 114937785Smsmith struct cs_softc *sc = ifp->if_softc; 115037785Smsmith 1151179560Sjhb CS_LOCK(sc); 115237785Smsmith ifmr->ifm_active = IFM_ETHER; 115372940Simp line_status = cs_readreg(sc, PP_LineST); 115437785Smsmith if (line_status & TENBASET_ON) { 115537785Smsmith ifmr->ifm_active |= IFM_10_T; 115637785Smsmith if (sc->chip_type != CS8900) { 115772940Simp if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE) 115837785Smsmith ifmr->ifm_active |= IFM_FDX; 115972940Simp if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE) 116037785Smsmith ifmr->ifm_active |= IFM_HDX; 116137785Smsmith } 116237785Smsmith ifmr->ifm_status = IFM_AVALID; 116337785Smsmith if (line_status & LINK_OK) 116437785Smsmith ifmr->ifm_status |= IFM_ACTIVE; 116537785Smsmith } else { 116638305Smsmith if (line_status & AUI_ON) { 116772940Simp cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) | 116872940Simp HCB1_ENBL); 116938305Smsmith if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^ 117072940Simp (cs_readreg(sc, PP_SelfCTL) & HCB1)) 117138305Smsmith ifmr->ifm_active |= IFM_10_2; 117238305Smsmith else 117338305Smsmith ifmr->ifm_active |= IFM_10_5; 117438305Smsmith } 117537785Smsmith } 1176179560Sjhb CS_UNLOCK(sc); 117737785Smsmith} 117837785Smsmith 117937785Smsmithstatic int 118037785Smsmithcs_mediaset(struct cs_softc *sc, int media) 118137785Smsmith{ 1182179532Simp int error = 0; 118337785Smsmith 118437785Smsmith /* Stop the receiver & transmitter */ 118572940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & 118672940Simp ~(SERIAL_RX_ON | SERIAL_TX_ON)); 118737785Smsmith 118837785Smsmith#ifdef CS_DEBUG 1189162321Sglebius device_printf(sc->dev, "%s media=%x\n", __func__, media); 119037785Smsmith#endif 119137785Smsmith 119237785Smsmith switch (IFM_SUBTYPE(media)) { 119337785Smsmith default: 119437785Smsmith case IFM_AUTO: 1195179553Simp /* 1196179553Simp * This chip makes it a little hard to support this, so treat 1197179553Simp * it as IFM_10_T, auto duplex. 1198179553Simp */ 1199179553Simp enable_tp(sc); 1200179553Simp cs_duplex_auto(sc); 120137785Smsmith break; 120237785Smsmith case IFM_10_T: 1203179532Simp enable_tp(sc); 120437785Smsmith if (media & IFM_FDX) 120537785Smsmith cs_duplex_full(sc); 120637785Smsmith else if (media & IFM_HDX) 120737785Smsmith cs_duplex_half(sc); 120837785Smsmith else 120937785Smsmith error = cs_duplex_auto(sc); 121037785Smsmith break; 121137785Smsmith case IFM_10_2: 1212180388Simp enable_bnc(sc); 121337785Smsmith break; 121437785Smsmith case IFM_10_5: 1215180388Simp enable_aui(sc); 121637785Smsmith break; 121737785Smsmith } 121837785Smsmith 121937785Smsmith /* 122037785Smsmith * Turn the transmitter & receiver back on 122137785Smsmith */ 122272940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | 122372940Simp SERIAL_RX_ON | SERIAL_TX_ON); 122437785Smsmith 1225140928Simp return (error); 122637785Smsmith} 1227