if_cs.c revision 179532
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 179532 2008-06-04 06:07:13Z 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 *); 76122024Simpstatic int cs_ioctl(struct ifnet *, u_long, caddr_t); 77122024Simpstatic void cs_start(struct ifnet *); 78122024Simpstatic void cs_stop(struct cs_softc *); 79122024Simpstatic void cs_reset(struct cs_softc *); 80122024Simpstatic void cs_watchdog(struct ifnet *); 8138592Smsmith 82122024Simpstatic int cs_mediachange(struct ifnet *); 83122024Simpstatic void cs_mediastatus(struct ifnet *, struct ifmediareq *); 84122024Simpstatic int cs_mediaset(struct cs_softc *, int); 8558816Simp 8637785Smsmithstatic void cs_write_mbufs(struct cs_softc*, struct mbuf*); 8737785Smsmithstatic void cs_xmit_buf(struct cs_softc*); 8837785Smsmithstatic int cs_get_packet(struct cs_softc*); 8937785Smsmithstatic void cs_setmode(struct cs_softc*); 9037785Smsmith 91122024Simpstatic int get_eeprom_data(struct cs_softc *sc, int, int, uint16_t *); 92122024Simpstatic int get_eeprom_cksum(int, int, uint16_t *); 9337785Smsmithstatic int wait_eeprom_ready( struct cs_softc *); 9437785Smsmithstatic void control_dc_dc( struct cs_softc *, int ); 9537785Smsmithstatic int send_test_pkt( struct cs_softc * ); 9637785Smsmithstatic int enable_tp(struct cs_softc *); 9737785Smsmithstatic int enable_aui(struct cs_softc *); 9837785Smsmithstatic int enable_bnc(struct cs_softc *); 9937785Smsmithstatic int cs_duplex_auto(struct cs_softc *); 10037785Smsmith 10171316Simpdevclass_t cs_devclass; 10237785Smsmith 103122024Simp/* sysctl vars */ 104122024SimpSYSCTL_NODE(_hw, OID_AUTO, cs, CTLFLAG_RD, 0, "cs device parameters"); 105122024Simp 106122024Simpint cs_debug = 0; 107122024SimpTUNABLE_INT("hw.cs.debug", &cs_debug); 108122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, debug, CTLFLAG_RW, 109122024Simp &cs_debug, 0, 110122024Simp "cs debug"); 111122024Simp 112122024Simpint cs_ignore_cksum_failure = 0; 113122024SimpTUNABLE_INT("hw.cs.ignore_checksum_failure", &cs_ignore_cksum_failure); 114122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, ignore_checksum_failure, CTLFLAG_RW, 115122024Simp &cs_ignore_cksum_failure, 0, 116122024Simp "ignore checksum errors in cs card EEPROM"); 117122024Simp 118122024Simpstatic int cs_recv_delay = 570; 119140925SimpTUNABLE_INT("hw.cs.recv_delay", &cs_recv_delay); 120122024SimpSYSCTL_INT(_hw_cs, OID_AUTO, recv_delay, CTLFLAG_RW, &cs_recv_delay, 570, ""); 121122024Simp 122179532Simpstatic int cs8900_eeint2irq[16] = { 123179532Simp 10, 11, 12, 5, 255, 255, 255, 255, 124179532Simp 255, 255, 255, 255, 255, 255, 255, 255 125179532Simp}; 126179532Simp 127179532Simpstatic int cs8900_irq2eeint[16] = { 128179532Simp 255, 255, 255, 255, 255, 3, 255, 255, 129179532Simp 255, 0, 1, 2, 255, 255, 255, 255 130179532Simp}; 131179532Simp 13237785Smsmithstatic int 133122024Simpget_eeprom_data(struct cs_softc *sc, int off, int len, uint16_t *buffer) 13437785Smsmith{ 13537785Smsmith int i; 13637785Smsmith 13737785Smsmith#ifdef CS_DEBUG 138122024Simp printf(CS_NAME":EEPROM data from %x for %x:\n", off, len); 13937785Smsmith#endif 14037785Smsmith 141122024Simp for (i=0; i < len; i++) { 142122024Simp if (wait_eeprom_ready(sc) < 0) 143122024Simp return (-1); 14437785Smsmith /* Send command to EEPROM to read */ 14572940Simp cs_writereg(sc, PP_EECMD, (off + i) | EEPROM_READ_CMD); 146122024Simp if (wait_eeprom_ready(sc) < 0) 14772940Simp return (-1); 14872940Simp buffer[i] = cs_readreg(sc, PP_EEData); 14937785Smsmith 15037785Smsmith#ifdef CS_DEBUG 15137785Smsmith printf("%02x %02x ",(unsigned char)buffer[i], 152122024Simp (unsigned char)buffer[i] >> 8); 15337785Smsmith#endif 15437785Smsmith } 15537785Smsmith 15637785Smsmith#ifdef CS_DEBUG 15737785Smsmith printf("\n"); 15837785Smsmith#endif 15972940Simp return (0); 16037785Smsmith} 16137785Smsmith 16237785Smsmithstatic int 163122024Simpget_eeprom_cksum(int off, int len, uint16_t *buffer) 16437785Smsmith{ 165122024Simp int i; 166122024Simp uint16_t cksum=0; 16737785Smsmith 168122024Simp for (i = 0; i < len; i++) 169122024Simp cksum += buffer[i]; 17037785Smsmith cksum &= 0xffff; 17137785Smsmith if (cksum==0) 172122024Simp return (0); 173122024Simp if (cs_ignore_cksum_failure) { 174122024Simp printf(CS_NAME": checksum mismatch, ignoring\n"); 175122024Simp return (0); 176122024Simp } 177122024Simp return (-1); 17837785Smsmith} 17937785Smsmith 18037785Smsmithstatic int 18137785Smsmithwait_eeprom_ready(struct cs_softc *sc) 18237785Smsmith{ 183122024Simp DELAY(30000); /* XXX should we do some checks here ? */ 184140928Simp return (0); 18537785Smsmith} 18637785Smsmith 18737785Smsmithstatic void 18837785Smsmithcontrol_dc_dc(struct cs_softc *sc, int on_not_off) 18937785Smsmith{ 19037785Smsmith unsigned int self_control = HCB1_ENBL; 19137785Smsmith 19237785Smsmith if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off) 19337785Smsmith self_control |= HCB1; 19437785Smsmith else 19537785Smsmith self_control &= ~HCB1; 19672940Simp cs_writereg(sc, PP_SelfCTL, self_control); 19737785Smsmith 198122024Simp DELAY(500000); 19937785Smsmith} 20037785Smsmith 20137785Smsmith 20237785Smsmithstatic int 20337785Smsmithcs_duplex_auto(struct cs_softc *sc) 20437785Smsmith{ 205140927Simp int i, error=0; 206140927Simp 20772940Simp cs_writereg(sc, PP_AutoNegCTL, 20872940Simp RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE); 209140927Simp for (i=0; cs_readreg(sc, PP_AutoNegST) & AUTO_NEG_BUSY; i++) { 210140927Simp if (i > 40000) { 211162321Sglebius device_printf(sc->dev, 212140927Simp "full/half duplex auto negotiation timeout\n"); 21337785Smsmith error = ETIMEDOUT; 214140927Simp break; 215140927Simp } 216140927Simp DELAY(1000); 217140927Simp } 218140927Simp DELAY(1000000); 219140928Simp return (error); 22037785Smsmith} 22137785Smsmith 22237785Smsmithstatic int 22337785Smsmithenable_tp(struct cs_softc *sc) 22437785Smsmith{ 22537785Smsmith 22672940Simp cs_writereg(sc, PP_LineCTL, sc->line_ctl & ~AUI_ONLY); 22737785Smsmith control_dc_dc(sc, 0); 22837785Smsmith DELAY( 150000 ); 22937785Smsmith 23072940Simp if ((cs_readreg(sc, PP_LineST) & LINK_OK)==0) { 231162321Sglebius device_printf(sc->dev, "failed to enable TP\n"); 232140928Simp return (EINVAL); 23337785Smsmith } 23437785Smsmith 235140928Simp return (0); 23637785Smsmith} 23737785Smsmith 23837785Smsmith/* 23937785Smsmith * XXX This was rewritten from Linux driver without any tests. 240140927Simp */ 24137785Smsmithstatic int 24237785Smsmithsend_test_pkt(struct cs_softc *sc) 24337785Smsmith{ 24437785Smsmith char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0, 24537785Smsmith 0, 46, /* A 46 in network order */ 24637785Smsmith 0, 0, /* DSAP=0 & SSAP=0 fields */ 24737785Smsmith 0xf3, 0 /* Control (Test Req + P bit set) */ }; 24838305Smsmith int i; 24938305Smsmith u_char ether_address_backup[ETHER_ADDR_LEN]; 25037785Smsmith 251122024Simp for (i = 0; i < ETHER_ADDR_LEN; i++) 252147256Sbrooks ether_address_backup[i] = sc->enaddr[i]; 25338305Smsmith 25472940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_TX_ON); 255147256Sbrooks bcopy(test_packet, sc->enaddr, ETHER_ADDR_LEN); 25672940Simp bcopy(test_packet+ETHER_ADDR_LEN, 257147256Sbrooks sc->enaddr, ETHER_ADDR_LEN); 25872940Simp cs_outw(sc, TX_CMD_PORT, sc->send_cmd); 25972940Simp cs_outw(sc, TX_LEN_PORT, sizeof(test_packet)); 26037785Smsmith 26137785Smsmith /* Wait for chip to allocate memory */ 26237785Smsmith DELAY(50000); 26372940Simp if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) { 264122024Simp for (i = 0; i < ETHER_ADDR_LEN; i++) 265147256Sbrooks sc->enaddr[i] = ether_address_backup[i]; 266140928Simp return (0); 26738305Smsmith } 26837785Smsmith 26937785Smsmith outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet)); 27037785Smsmith 27137785Smsmith DELAY(30000); 27237785Smsmith 273122024Simp for (i = 0; i < ETHER_ADDR_LEN; i++) 274147256Sbrooks sc->enaddr[i] = ether_address_backup[i]; 275122024Simp if ((cs_readreg(sc, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) 276140928Simp return (1); 277140928Simp return (0); 27837785Smsmith} 27937785Smsmith 28037785Smsmith/* 28137785Smsmith * XXX This was rewritten from Linux driver without any tests. 28237785Smsmith */ 28337785Smsmithstatic int 28437785Smsmithenable_aui(struct cs_softc *sc) 28537785Smsmith{ 28637785Smsmith 28737785Smsmith control_dc_dc(sc, 0); 28872940Simp cs_writereg(sc, PP_LineCTL, 28972940Simp (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY); 29037785Smsmith 29137785Smsmith if (!send_test_pkt(sc)) { 292162321Sglebius device_printf(sc->dev, "failed to enable AUI\n"); 293140928Simp return (EINVAL); 294140927Simp } 295140928Simp return (0); 29637785Smsmith} 29737785Smsmith 29837785Smsmith/* 29937785Smsmith * XXX This was rewritten from Linux driver without any tests. 300140927Simp */ 30137785Smsmithstatic int 30237785Smsmithenable_bnc(struct cs_softc *sc) 30337785Smsmith{ 30437785Smsmith 30537785Smsmith control_dc_dc(sc, 1); 30672940Simp cs_writereg(sc, PP_LineCTL, 30772940Simp (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY); 30837785Smsmith 30937785Smsmith if (!send_test_pkt(sc)) { 310162321Sglebius device_printf(sc->dev, "failed to enable BNC\n"); 311140928Simp return (EINVAL); 312140927Simp } 313140928Simp return (0); 31437785Smsmith} 31537785Smsmith 31671316Simpint 31758816Simpcs_cs89x0_probe(device_t dev) 31837785Smsmith{ 31958816Simp int i; 32058816Simp int error; 32158816Simp u_long irq, junk; 32258816Simp struct cs_softc *sc = device_get_softc(dev); 32337785Smsmith unsigned rev_type = 0; 324122024Simp uint16_t id; 32558816Simp char chip_revision; 326122024Simp uint16_t eeprom_buff[CHKSUM_LEN]; 32737785Smsmith int chip_type, pp_isaint, pp_isadma; 32837785Smsmith 32958816Simp error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS); 33058816Simp if (error) 33158816Simp return (error); 33258816Simp 33372940Simp sc->nic_addr = rman_get_start(sc->port_res); 33458816Simp 33572940Simp if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) { 33637785Smsmith /* Chip not detected. Let's try to reset it */ 33737785Smsmith if (bootverbose) 33858816Simp device_printf(dev, "trying to reset the chip.\n"); 33972940Simp cs_outw(sc, ADD_PORT, PP_SelfCTL); 34072940Simp i = cs_inw(sc, DATA_PORT); 34172940Simp cs_outw(sc, ADD_PORT, PP_SelfCTL); 34272940Simp cs_outw(sc, DATA_PORT, i | POWER_ON_RESET); 34372940Simp if ((cs_inw(sc, ADD_PORT) & ADD_MASK) != ADD_SIG) 34458816Simp return (ENXIO); 34537785Smsmith } 34637785Smsmith 34772940Simp for (i = 0; i < 10000; i++) { 34872940Simp id = cs_readreg(sc, PP_ChipID); 34972940Simp if (id == CHIP_EISA_ID_SIG) 35072940Simp break; 35172940Simp } 35272940Simp if (i == 10000) 35358816Simp return (ENXIO); 35437785Smsmith 35572940Simp rev_type = cs_readreg(sc, PRODUCT_ID_ADD); 35637785Smsmith chip_type = rev_type & ~REVISON_BITS; 35737785Smsmith chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A'; 35837785Smsmith 35937785Smsmith sc->chip_type = chip_type; 36058816Simp 361179532Simp if (chip_type == CS8900) { 36237785Smsmith pp_isaint = PP_CS8900_ISAINT; 36337785Smsmith pp_isadma = PP_CS8900_ISADMA; 36437785Smsmith sc->send_cmd = TX_CS8900_AFTER_ALL; 36537785Smsmith } else { 36637785Smsmith pp_isaint = PP_CS8920_ISAINT; 36737785Smsmith pp_isadma = PP_CS8920_ISADMA; 36837785Smsmith sc->send_cmd = TX_CS8920_AFTER_ALL; 36937785Smsmith } 37037785Smsmith 37137785Smsmith /* 372140927Simp * Clear some fields so that fail of EEPROM will left them clean 373140927Simp */ 374140927Simp sc->auto_neg_cnf = 0; 375140927Simp sc->adapter_cnf = 0; 376140927Simp sc->isa_config = 0; 377140927Simp 378140927Simp /* 379179532Simp * If no interrupt specified, use what the board tells us. 38037785Smsmith */ 38158816Simp error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk); 38258816Simp 38358816Simp /* 38458816Simp * Get data from EEPROM 38558816Simp */ 38672940Simp if((cs_readreg(sc, PP_SelfST) & EEPROM_PRESENT) == 0) { 38758816Simp device_printf(dev, "No EEPROM, assuming defaults.\n"); 388179532Simp } else if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) { 389179532Simp device_printf(dev, "EEPROM read failed, assuming defaults.\n"); 390179532Simp } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) { 391179532Simp device_printf(dev, "EEPROM cheksum bad, assuming defaults.\n"); 39237785Smsmith } else { 393179532Simp sc->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2]; 394179532Simp sc->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2]; 395179532Simp sc->isa_config = eeprom_buff[ISA_CNF_OFFSET/2]; 396179532Simp for (i=0; i<ETHER_ADDR_LEN/2; i++) { 397179532Simp sc->enaddr[i*2] = eeprom_buff[i]; 398179532Simp sc->enaddr[i*2+1] = eeprom_buff[i] >> 8; 399179532Simp } 400179532Simp /* 401179532Simp * If no interrupt specified, use what the 402179532Simp * board tells us. 403179532Simp */ 404179532Simp if (error) { 405179532Simp irq = sc->isa_config & INT_NO_MASK; 406179532Simp error = 0; 407179532Simp if (chip_type == CS8900) { 408179532Simp irq = cs8900_eeint2irq[irq]; 40937785Smsmith } else { 410179532Simp if (irq > CS8920_NO_INTS) 411179532Simp irq = 255; 41237785Smsmith } 413179532Simp if (irq == 255) { 414179532Simp device_printf(dev, "invalid irq in EEPROM.\n"); 415179532Simp error = EINVAL; 416179532Simp } 417179532Simp if (!error) 418179532Simp bus_set_resource(dev, SYS_RES_IRQ, 0, 419179532Simp irq, 1); 420140927Simp } 421140927Simp } 42237785Smsmith 423179532Simp if (!error && !(sc->flags & CS_NO_IRQ)) { 424140927Simp if (chip_type == CS8900) { 425179532Simp if (irq >= 0 || irq < 16) 426179532Simp irq = cs8900_irq2eeint[irq]; 427179532Simp else 428179532Simp irq = 255; 429140927Simp } else { 430179532Simp if (irq > CS8920_NO_INTS) 431179532Simp irq = 255; 432140927Simp } 433179532Simp if (irq == 255) 434179532Simp error = EINVAL; 43558816Simp } 43658816Simp 437179507Simp if (error) { 43858816Simp device_printf(dev, "Unknown or invalid irq\n"); 439179507Simp return (error); 440140927Simp } 441140927Simp 442179507Simp if (!(sc->flags & CS_NO_IRQ)) 443179507Simp cs_writereg(sc, pp_isaint, irq); 444179507Simp 445140927Simp /* 446140927Simp * Temporary disabled 447140927Simp * 448140927Simp if (drq>0) 44972940Simp cs_writereg(sc, pp_isadma, drq); 45037785Smsmith else { 451104252Sbrooks device_printf(dev, "incorrect drq\n",); 452140928Simp return (0); 45337785Smsmith } 454140927Simp */ 45537785Smsmith 45637785Smsmith if (bootverbose) 45758816Simp device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n", 458179532Simp chip_type == CS8900 ? '0' : '2', 459179532Simp chip_type == CS8920M ? "M" : "", 46037785Smsmith chip_revision, 46137785Smsmith (sc->adapter_cnf & A_CNF_10B_T) ? " TP" : "", 46237785Smsmith (sc->adapter_cnf & A_CNF_AUI) ? " AUI" : "", 46358816Simp (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : ""); 46437785Smsmith 465140927Simp if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) && 466140927Simp (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH)) 467140927Simp sc->line_ctl = LOW_RX_SQUELCH; 468140927Simp else 469140927Simp sc->line_ctl = 0; 47037785Smsmith 471140928Simp return (0); 47237785Smsmith} 47337785Smsmith 47437785Smsmith/* 47558816Simp * Allocate a port resource with the given resource id. 47658816Simp */ 47758816Simpint cs_alloc_port(device_t dev, int rid, int size) 47858816Simp{ 479140927Simp struct cs_softc *sc = device_get_softc(dev); 480140927Simp struct resource *res; 48158816Simp 482140927Simp res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 483140927Simp 0ul, ~0ul, size, RF_ACTIVE); 484140928Simp if (res == NULL) 485140927Simp return (ENOENT); 486140928Simp sc->port_rid = rid; 487140928Simp sc->port_res = res; 488140928Simp sc->port_used = size; 489140928Simp return (0); 49058816Simp} 49158816Simp 49258816Simp/* 49358816Simp * Allocate a memory resource with the given resource id. 49458816Simp */ 49558816Simpint cs_alloc_memory(device_t dev, int rid, int size) 49658816Simp{ 497140927Simp struct cs_softc *sc = device_get_softc(dev); 498140927Simp struct resource *res; 49958816Simp 500140927Simp res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 501140927Simp 0ul, ~0ul, size, RF_ACTIVE); 502140928Simp if (res == NULL) 503140927Simp return (ENOENT); 504140928Simp sc->mem_rid = rid; 505140928Simp sc->mem_res = res; 506140928Simp sc->mem_used = size; 507140928Simp return (0); 50858816Simp} 50958816Simp 51058816Simp/* 51158816Simp * Allocate an irq resource with the given resource id. 51258816Simp */ 51358816Simpint cs_alloc_irq(device_t dev, int rid, int flags) 51458816Simp{ 515140927Simp struct cs_softc *sc = device_get_softc(dev); 516140927Simp struct resource *res; 51758816Simp 518140927Simp res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 519140927Simp (RF_ACTIVE | flags)); 520140928Simp if (res == NULL) 521140927Simp return (ENOENT); 522140928Simp sc->irq_rid = rid; 523140928Simp sc->irq_res = res; 524140928Simp return (0); 52558816Simp} 52658816Simp 52758816Simp/* 52858816Simp * Release all resources 52958816Simp */ 53058816Simpvoid cs_release_resources(device_t dev) 53158816Simp{ 532140927Simp struct cs_softc *sc = device_get_softc(dev); 53358816Simp 534140927Simp if (sc->port_res) { 535140927Simp bus_release_resource(dev, SYS_RES_IOPORT, 536140927Simp sc->port_rid, sc->port_res); 537140927Simp sc->port_res = 0; 538140927Simp } 539140927Simp if (sc->mem_res) { 540140927Simp bus_release_resource(dev, SYS_RES_MEMORY, 541140927Simp sc->mem_rid, sc->mem_res); 542140927Simp sc->mem_res = 0; 543140927Simp } 544140927Simp if (sc->irq_res) { 545140927Simp bus_release_resource(dev, SYS_RES_IRQ, 546140927Simp sc->irq_rid, sc->irq_res); 547140927Simp sc->irq_res = 0; 548140927Simp } 54958816Simp} 55058816Simp 55158816Simp/* 55237785Smsmith * Install the interface into kernel networking data structures 55337785Smsmith */ 55471316Simpint 555121816Sbrookscs_attach(device_t dev) 55637785Smsmith{ 557140927Simp int media=0; 558121816Sbrooks struct cs_softc *sc = device_get_softc(dev);; 559147256Sbrooks struct ifnet *ifp; 56037785Smsmith 561162321Sglebius sc->dev = dev; 562162321Sglebius 563147256Sbrooks ifp = sc->ifp = if_alloc(IFT_ETHER); 564147256Sbrooks if (ifp == NULL) { 565147256Sbrooks device_printf(dev, "can not if_alloc()\n"); 566147256Sbrooks return (0); 567147256Sbrooks } 568147256Sbrooks 56958816Simp cs_stop( sc ); 57058816Simp 571121752Sbrooks ifp->if_softc=sc; 572121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 573121752Sbrooks ifp->if_start=cs_start; 574121752Sbrooks ifp->if_ioctl=cs_ioctl; 575121752Sbrooks ifp->if_watchdog=cs_watchdog; 576121752Sbrooks ifp->if_init=cs_init; 577121752Sbrooks ifp->if_snd.ifq_maxlen= IFQ_MAXLEN; 578121752Sbrooks /* 579121752Sbrooks * MIB DATA 580121752Sbrooks */ 581121752Sbrooks /* 582121752Sbrooks ifp->if_linkmib=&sc->mibdata; 583121752Sbrooks ifp->if_linkmiblen=sizeof sc->mibdata; 584121752Sbrooks */ 58537785Smsmith 586133681Srwatson ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | 587133681Srwatson IFF_NEEDSGIANT); 58837785Smsmith 589121752Sbrooks /* 590121752Sbrooks * this code still in progress (DMA support) 591121752Sbrooks * 59237785Smsmith 593121752Sbrooks sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT); 594121752Sbrooks if (sc->recv_ring == NULL) { 595121816Sbrooks log(LOG_ERR, 596121816Sbrooks "%s: Couldn't allocate memory for NIC\n", ifp->if_xname); 597121752Sbrooks return(0); 598121752Sbrooks } 599121752Sbrooks if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF)) 600121752Sbrooks < (128*1024-CS_DMA_BUFFER_SIZE)) 601121752Sbrooks sc->recv_ring+=16*1024; 60237785Smsmith 603121752Sbrooks */ 60437785Smsmith 605121752Sbrooks sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT); 606121752Sbrooks if (sc->buffer == NULL) { 607162321Sglebius device_printf(sc->dev, "Couldn't allocate memory for NIC\n"); 608121752Sbrooks return(0); 609121752Sbrooks } 61037785Smsmith 611121752Sbrooks /* 612121752Sbrooks * Initialize the media structures. 613121752Sbrooks */ 614121752Sbrooks ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus); 61537785Smsmith 616121752Sbrooks if (sc->adapter_cnf & A_CNF_10B_T) { 617121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL); 618121752Sbrooks if (sc->chip_type != CS8900) { 619121752Sbrooks ifmedia_add(&sc->media, 620121752Sbrooks IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL); 621121752Sbrooks ifmedia_add(&sc->media, 622121752Sbrooks IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL); 623121752Sbrooks } 624121752Sbrooks } 62537785Smsmith 626121752Sbrooks if (sc->adapter_cnf & A_CNF_10B_2) 627121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL); 62837785Smsmith 629121752Sbrooks if (sc->adapter_cnf & A_CNF_AUI) 630121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL); 63137785Smsmith 632121752Sbrooks if (sc->adapter_cnf & A_CNF_MEDIA) 633121752Sbrooks ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL); 63437785Smsmith 635121752Sbrooks /* Set default media from EEPROM */ 636121752Sbrooks switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) { 637121752Sbrooks case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break; 638121752Sbrooks case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break; 639121752Sbrooks case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break; 640121752Sbrooks case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break; 641122024Simp default: 642162321Sglebius device_printf(sc->dev, "no media, assuming 10baseT\n"); 643122024Simp sc->adapter_cnf |= A_CNF_10B_T; 644122024Simp ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL); 645122024Simp if (sc->chip_type != CS8900) { 646122024Simp ifmedia_add(&sc->media, 647122024Simp IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL); 648122024Simp ifmedia_add(&sc->media, 649122024Simp IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL); 650122024Simp } 651122024Simp media = IFM_ETHER | IFM_10_T; 652122024Simp break; 65337785Smsmith } 654121752Sbrooks ifmedia_set(&sc->media, media); 655121752Sbrooks cs_mediaset(sc, media); 65637785Smsmith 657147256Sbrooks ether_ifattach(ifp, sc->enaddr); 658121752Sbrooks 65958816Simp return (0); 66037785Smsmith} 66137785Smsmith 662140888Simpint 663140888Simpcs_detach(device_t dev) 664140888Simp{ 665140888Simp struct cs_softc *sc; 666140888Simp struct ifnet *ifp; 667140888Simp 668140888Simp sc = device_get_softc(dev); 669147256Sbrooks ifp = sc->ifp; 670140888Simp 671140888Simp cs_stop(sc); 672148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 673140888Simp ether_ifdetach(ifp); 674150306Simp cs_release_resources(dev); 675147256Sbrooks if_free(ifp); 676140888Simp return (0); 677140888Simp} 678140888Simp 67937785Smsmith/* 68037785Smsmith * Initialize the board 68137785Smsmith */ 68237785Smsmithstatic void 68337785Smsmithcs_init(void *xsc) 68437785Smsmith{ 68537785Smsmith struct cs_softc *sc=(struct cs_softc *)xsc; 686147256Sbrooks struct ifnet *ifp = sc->ifp; 68741591Sarchie int i, s, rx_cfg; 68837785Smsmith 68937785Smsmith /* 69037785Smsmith * reset whatchdog timer 69137785Smsmith */ 69237785Smsmith ifp->if_timer=0; 69337785Smsmith sc->buf_len = 0; 69437785Smsmith 69537785Smsmith s=splimp(); 69637785Smsmith 69737785Smsmith /* 69837785Smsmith * Hardware initialization of cs 69937785Smsmith */ 70037785Smsmith 70137785Smsmith /* Enable receiver and transmitter */ 70272940Simp cs_writereg(sc, PP_LineCTL, 70372940Simp cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON); 70437785Smsmith 70537785Smsmith /* Configure the receiver mode */ 70637785Smsmith cs_setmode(sc); 70737785Smsmith 70837785Smsmith /* 70937785Smsmith * This defines what type of frames will cause interrupts 71037785Smsmith * Bad frames should generate interrupts so that the driver 71137785Smsmith * could track statistics of discarded packets 71237785Smsmith */ 713140927Simp rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL | 71437785Smsmith RX_EXTRA_DATA_ENBL; 71537785Smsmith if (sc->isa_config & STREAM_TRANSFER) 71637785Smsmith rx_cfg |= RX_STREAM_ENBL; 71772940Simp cs_writereg(sc, PP_RxCFG, rx_cfg); 71872940Simp cs_writereg(sc, PP_TxCFG, TX_LOST_CRS_ENBL | 71937785Smsmith TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL | 72037785Smsmith TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL); 72172940Simp cs_writereg(sc, PP_BufCFG, READY_FOR_TX_ENBL | 72237785Smsmith RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL | 72337785Smsmith TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/); 72437785Smsmith 725140927Simp /* Write MAC address into IA filter */ 726140927Simp for (i=0; i<ETHER_ADDR_LEN/2; i++) 727140927Simp cs_writereg(sc, PP_IA + i * 2, 728147256Sbrooks sc->enaddr[i * 2] | 729147256Sbrooks (sc->enaddr[i * 2 + 1] << 8) ); 73037785Smsmith 73137785Smsmith /* 73237785Smsmith * Now enable everything 73337785Smsmith */ 73437785Smsmith/* 73537785Smsmith#ifdef CS_USE_64K_DMA 73672940Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K); 73772940Simp#else 738140927Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ); 73937785Smsmith#endif 74037785Smsmith*/ 74172940Simp cs_writereg(sc, PP_BusCTL, ENABLE_IRQ); 74237785Smsmith 74337785Smsmith /* 74437785Smsmith * Set running and clear output active flags 74537785Smsmith */ 746148887Srwatson sc->ifp->if_drv_flags |= IFF_DRV_RUNNING; 747148887Srwatson sc->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 74837785Smsmith 74937785Smsmith /* 75037785Smsmith * Start sending process 75137785Smsmith */ 75237785Smsmith cs_start(ifp); 75337785Smsmith 75437785Smsmith (void) splx(s); 75537785Smsmith} 75637785Smsmith 75737785Smsmith/* 758106937Ssam * Get the packet from the board and send it to the upper layer. 75937785Smsmith */ 76037785Smsmithstatic int 76137785Smsmithcs_get_packet(struct cs_softc *sc) 76237785Smsmith{ 763147256Sbrooks struct ifnet *ifp = sc->ifp; 76437785Smsmith int iobase = sc->nic_addr, status, length; 76537785Smsmith struct ether_header *eh; 76637785Smsmith struct mbuf *m; 76737785Smsmith 76837785Smsmith#ifdef CS_DEBUG 76937785Smsmith int i; 77037785Smsmith#endif 77137785Smsmith 77272940Simp status = cs_inw(sc, RX_FRAME_PORT); 77372940Simp length = cs_inw(sc, RX_FRAME_PORT); 77437785Smsmith 77537785Smsmith#ifdef CS_DEBUG 776162321Sglebius device_printf(sc->dev, "rcvd: stat %x, len %d\n", 777104252Sbrooks status, length); 77837785Smsmith#endif 77937785Smsmith 78037785Smsmith if (!(status & RX_OK)) { 78137785Smsmith#ifdef CS_DEBUG 782162321Sglebius device_printf(sc->dev, "bad pkt stat %x\n", status); 78337785Smsmith#endif 78437785Smsmith ifp->if_ierrors++; 785140928Simp return (-1); 78637785Smsmith } 78737785Smsmith 788111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 78937785Smsmith if (m==NULL) 790140928Simp return (-1); 79137785Smsmith 79237785Smsmith if (length > MHLEN) { 793111119Simp MCLGET(m, M_DONTWAIT); 79437785Smsmith if (!(m->m_flags & M_EXT)) { 79537785Smsmith m_freem(m); 796140928Simp return (-1); 79737785Smsmith } 79837785Smsmith } 79937785Smsmith 80037785Smsmith /* Initialize packet's header info */ 80137785Smsmith m->m_pkthdr.rcvif = ifp; 80237785Smsmith m->m_pkthdr.len = length; 80337785Smsmith m->m_len = length; 80437785Smsmith 80537785Smsmith /* Get the data */ 80637785Smsmith insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1); 80737785Smsmith 80837785Smsmith eh = mtod(m, struct ether_header *); 80937785Smsmith 81037785Smsmith#ifdef CS_DEBUG 81137785Smsmith for (i=0;i<length;i++) 81237785Smsmith printf(" %02x",(unsigned char)*((char *)(m->m_data+i))); 81337785Smsmith printf( "\n" ); 81437785Smsmith#endif 81537785Smsmith 81637785Smsmith if (status & (RX_IA | RX_BROADCAST) || 81737785Smsmith (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) { 81837785Smsmith /* Feed the packet to the upper layer */ 819106937Ssam (*ifp->if_input)(ifp, m); 82037785Smsmith ifp->if_ipackets++; 821122024Simp if (length == ETHER_MAX_LEN-ETHER_CRC_LEN) 822140927Simp DELAY(cs_recv_delay); 82337785Smsmith } else { 82437785Smsmith m_freem(m); 82537785Smsmith } 82637785Smsmith 827140928Simp return (0); 82837785Smsmith} 82937785Smsmith 83037785Smsmith/* 83158816Simp * Handle interrupts 83237785Smsmith */ 83358816Simpvoid 83458816Simpcsintr(void *arg) 83537785Smsmith{ 83658816Simp struct cs_softc *sc = (struct cs_softc*) arg; 837147256Sbrooks struct ifnet *ifp = sc->ifp; 83841591Sarchie int status; 83937785Smsmith 84037785Smsmith#ifdef CS_DEBUG 841162321Sglebius device_printf(sc->dev, "Interrupt.\n"); 84237785Smsmith#endif 84337785Smsmith 84472940Simp while ((status=cs_inw(sc, ISQ_PORT))) { 84537785Smsmith 84637785Smsmith#ifdef CS_DEBUG 847162321Sglebius device_printf(sc->dev, "from ISQ: %04x\n", status); 84837785Smsmith#endif 84937785Smsmith 85037785Smsmith switch (status & ISQ_EVENT_MASK) { 851140927Simp case ISQ_RECEIVER_EVENT: 852140927Simp cs_get_packet(sc); 853140927Simp break; 85437785Smsmith 855140927Simp case ISQ_TRANSMITTER_EVENT: 856140927Simp if (status & TX_OK) 857140927Simp ifp->if_opackets++; 858140927Simp else 859140927Simp ifp->if_oerrors++; 860148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 861140927Simp ifp->if_timer = 0; 862140927Simp break; 86337785Smsmith 864140927Simp case ISQ_BUFFER_EVENT: 865140927Simp if (status & READY_FOR_TX) { 866148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 867140927Simp ifp->if_timer = 0; 868140927Simp } 86937785Smsmith 870140927Simp if (status & TX_UNDERRUN) { 871148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 872140927Simp ifp->if_timer = 0; 873140927Simp ifp->if_oerrors++; 874140927Simp } 875140927Simp break; 87637785Smsmith 877140927Simp case ISQ_RX_MISS_EVENT: 878140927Simp ifp->if_ierrors+=(status>>6); 879140927Simp break; 88037785Smsmith 881140927Simp case ISQ_TX_COL_EVENT: 882140927Simp ifp->if_collisions+=(status>>6); 883140927Simp break; 884140927Simp } 885140927Simp } 88637785Smsmith 887148887Srwatson if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE)) { 888140927Simp cs_start(ifp); 889140927Simp } 89037785Smsmith} 89137785Smsmith 89237785Smsmith/* 89337785Smsmith * Save the data in buffer 89437785Smsmith */ 89537785Smsmith 89637785Smsmithstatic void 89737785Smsmithcs_write_mbufs( struct cs_softc *sc, struct mbuf *m ) 89837785Smsmith{ 89937785Smsmith int len; 90037785Smsmith struct mbuf *mp; 90137785Smsmith unsigned char *data, *buf; 90237785Smsmith 90337785Smsmith for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) { 90437785Smsmith len = mp->m_len; 90537785Smsmith 90637785Smsmith /* 90737785Smsmith * Ignore empty parts 90837785Smsmith */ 90937785Smsmith if (!len) 91037785Smsmith continue; 91137785Smsmith 91237785Smsmith /* 91337785Smsmith * Find actual data address 91437785Smsmith */ 91537785Smsmith data = mtod(mp, caddr_t); 91637785Smsmith 91737785Smsmith bcopy((caddr_t) data, (caddr_t) buf, len); 91837785Smsmith buf += len; 91937785Smsmith sc->buf_len += len; 92037785Smsmith } 92137785Smsmith} 92237785Smsmith 92337785Smsmith 92437785Smsmithstatic void 92537785Smsmithcs_xmit_buf( struct cs_softc *sc ) 92637785Smsmith{ 92737785Smsmith outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1); 92837785Smsmith sc->buf_len = 0; 92937785Smsmith} 93037785Smsmith 93137785Smsmithstatic void 93237785Smsmithcs_start(struct ifnet *ifp) 93337785Smsmith{ 93437785Smsmith int s, length; 93537785Smsmith struct mbuf *m, *mp; 93637785Smsmith struct cs_softc *sc = ifp->if_softc; 93737785Smsmith 93837785Smsmith s = splimp(); 93937785Smsmith 94037785Smsmith for (;;) { 94137785Smsmith if (sc->buf_len) 94237785Smsmith length = sc->buf_len; 94337785Smsmith else { 94437785Smsmith IF_DEQUEUE( &ifp->if_snd, m ); 94537785Smsmith 94637785Smsmith if (m==NULL) { 94737785Smsmith (void) splx(s); 94837785Smsmith return; 94937785Smsmith } 95037785Smsmith 95137785Smsmith for (length=0, mp=m; mp != NULL; mp=mp->m_next) 95237785Smsmith length += mp->m_len; 95337785Smsmith 95437785Smsmith /* Skip zero-length packets */ 95537785Smsmith if (length == 0) { 95637785Smsmith m_freem(m); 95737785Smsmith continue; 95837785Smsmith } 95937785Smsmith 96037785Smsmith cs_write_mbufs(sc, m); 96137785Smsmith 962106937Ssam BPF_MTAP(ifp, m); 96337785Smsmith 96437785Smsmith m_freem(m); 96537785Smsmith } 96637785Smsmith 96737785Smsmith /* 96837785Smsmith * Issue a SEND command 96937785Smsmith */ 97072940Simp cs_outw(sc, TX_CMD_PORT, sc->send_cmd); 97172940Simp cs_outw(sc, TX_LEN_PORT, length ); 97237785Smsmith 97337785Smsmith /* 97437785Smsmith * If there's no free space in the buffer then leave 97537785Smsmith * this packet for the next time: indicate output active 97637785Smsmith * and return. 97737785Smsmith */ 97872940Simp if (!(cs_readreg(sc, PP_BusST) & READY_FOR_TX_NOW)) { 97937785Smsmith ifp->if_timer = sc->buf_len; 98037785Smsmith (void) splx(s); 981148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 98237785Smsmith return; 98337785Smsmith } 98437785Smsmith 985140927Simp cs_xmit_buf(sc); 98637785Smsmith 98737785Smsmith /* 98837785Smsmith * Set the watchdog timer in case we never hear 98937785Smsmith * from board again. (I don't know about correct 99037785Smsmith * value for this timeout) 99137785Smsmith */ 99237785Smsmith ifp->if_timer = length; 99337785Smsmith 99437785Smsmith (void) splx(s); 995148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 99637785Smsmith return; 99737785Smsmith } 99837785Smsmith} 99937785Smsmith 100037785Smsmith/* 100137785Smsmith * Stop everything on the interface 100237785Smsmith */ 100337785Smsmithstatic void 100437785Smsmithcs_stop(struct cs_softc *sc) 100537785Smsmith{ 100637785Smsmith int s = splimp(); 100737785Smsmith 100872940Simp cs_writereg(sc, PP_RxCFG, 0); 100972940Simp cs_writereg(sc, PP_TxCFG, 0); 101072940Simp cs_writereg(sc, PP_BufCFG, 0); 101172940Simp cs_writereg(sc, PP_BusCTL, 0); 101237785Smsmith 1013148887Srwatson sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1014147256Sbrooks sc->ifp->if_timer = 0; 101537785Smsmith 101637785Smsmith (void) splx(s); 101737785Smsmith} 101837785Smsmith 101937785Smsmith/* 102037785Smsmith * Reset the interface 102137785Smsmith */ 102237785Smsmithstatic void 102337785Smsmithcs_reset(struct cs_softc *sc) 102437785Smsmith{ 102537785Smsmith cs_stop(sc); 102637785Smsmith cs_init(sc); 102737785Smsmith} 102837785Smsmith 1029156559Sphilipstatic uint16_t 1030156559Sphilipcs_hash_index(struct sockaddr_dl *addr) 1031156559Sphilip{ 1032156559Sphilip uint32_t crc; 1033156559Sphilip uint16_t idx; 1034156559Sphilip caddr_t lla; 1035156559Sphilip 1036156559Sphilip lla = LLADDR(addr); 1037156559Sphilip crc = ether_crc32_le(lla, ETHER_ADDR_LEN); 1038156559Sphilip idx = crc >> 26; 1039156559Sphilip 1040156559Sphilip return (idx); 1041156559Sphilip} 1042156559Sphilip 104337785Smsmithstatic void 104437785Smsmithcs_setmode(struct cs_softc *sc) 104537785Smsmith{ 1046156559Sphilip int rx_ctl; 1047156559Sphilip uint16_t af[4]; 1048156559Sphilip uint16_t port, mask, index; 1049147256Sbrooks struct ifnet *ifp = sc->ifp; 1050156559Sphilip struct ifmultiaddr *ifma; 105137785Smsmith 105237785Smsmith /* Stop the receiver while changing filters */ 105372940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & ~SERIAL_RX_ON); 105437785Smsmith 105537785Smsmith if (ifp->if_flags & IFF_PROMISC) { 105637785Smsmith /* Turn on promiscuous mode. */ 105737785Smsmith rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT; 1058156559Sphilip } else if (ifp->if_flags & IFF_MULTICAST) { 1059156559Sphilip /* Allow receiving frames with multicast addresses */ 1060156559Sphilip rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT | 1061156559Sphilip RX_OK_ACCEPT | RX_MULTCAST_ACCEPT; 1062156559Sphilip 1063156559Sphilip /* Start with an empty filter */ 1064156559Sphilip af[0] = af[1] = af[2] = af[3] = 0x0000; 1065156559Sphilip 1066156559Sphilip if (ifp->if_flags & IFF_ALLMULTI) { 1067156559Sphilip /* Accept all multicast frames */ 1068156559Sphilip af[0] = af[1] = af[2] = af[3] = 0xffff; 106937785Smsmith } else { 1070156559Sphilip /* 1071156559Sphilip * Set up the filter to only accept multicast 1072156559Sphilip * frames we're interested in. 107337785Smsmith */ 1074156559Sphilip IF_ADDR_LOCK(ifp); 1075156559Sphilip TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1076156559Sphilip struct sockaddr_dl *dl = 1077156559Sphilip (struct sockaddr_dl *)ifma->ifma_addr; 1078156559Sphilip 1079156559Sphilip index = cs_hash_index(dl); 1080156559Sphilip port = (u_int16_t) (index >> 4); 1081156559Sphilip mask = (u_int16_t) (1 << (index & 0xf)); 1082156559Sphilip af[port] |= mask; 1083156559Sphilip } 1084156559Sphilip IF_ADDR_UNLOCK(ifp); 108537785Smsmith } 1086156559Sphilip 1087156559Sphilip cs_writereg(sc, PP_LAF + 0, af[0]); 1088156559Sphilip cs_writereg(sc, PP_LAF + 2, af[1]); 1089156559Sphilip cs_writereg(sc, PP_LAF + 4, af[2]); 1090156559Sphilip cs_writereg(sc, PP_LAF + 6, af[3]); 1091156559Sphilip } else { 1092156559Sphilip /* 1093156559Sphilip * Receive only good frames addressed for us and 1094156559Sphilip * good broadcasts. 1095156559Sphilip */ 1096156559Sphilip rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT | 1097156559Sphilip RX_OK_ACCEPT; 109837785Smsmith } 109937785Smsmith 110037785Smsmith /* Set up the filter */ 110172940Simp cs_writereg(sc, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl); 110237785Smsmith 110337785Smsmith /* Turn on receiver */ 110472940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | SERIAL_RX_ON); 110537785Smsmith} 110637785Smsmith 110737785Smsmithstatic int 110837785Smsmithcs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data) 110937785Smsmith{ 111037785Smsmith struct cs_softc *sc=ifp->if_softc; 111137785Smsmith struct ifreq *ifr = (struct ifreq *)data; 111237785Smsmith int s,error=0; 111337785Smsmith 111437785Smsmith#ifdef CS_DEBUG 1115162321Sglebius if_printf(ifp, "%s command=%lx\n", __func__, command); 111637785Smsmith#endif 111737785Smsmith 111837785Smsmith s=splimp(); 111937785Smsmith 112037785Smsmith switch (command) { 112137785Smsmith case SIOCSIFFLAGS: 112237785Smsmith /* 112337785Smsmith * Switch interface state between "running" and 112437785Smsmith * "stopped", reflecting the UP flag. 1125140927Simp */ 1126147256Sbrooks if (sc->ifp->if_flags & IFF_UP) { 1127148887Srwatson if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)==0) { 1128140927Simp cs_init(sc); 1129140927Simp } 1130140927Simp } else { 1131148887Srwatson if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)!=0) { 1132140927Simp cs_stop(sc); 1133140927Simp } 113437785Smsmith } 113537785Smsmith /* 113637785Smsmith * Promiscuous and/or multicast flags may have changed, 113737785Smsmith * so reprogram the multicast filter and/or receive mode. 113837785Smsmith * 113937785Smsmith * See note about multicasts in cs_setmode 114037785Smsmith */ 114137785Smsmith cs_setmode(sc); 114237785Smsmith break; 114337785Smsmith 114437785Smsmith case SIOCADDMULTI: 114537785Smsmith case SIOCDELMULTI: 114637785Smsmith /* 114737785Smsmith * Multicast list has changed; set the hardware filter 114837785Smsmith * accordingly. 114937785Smsmith * 115037785Smsmith * See note about multicasts in cs_setmode 115137785Smsmith */ 115237785Smsmith cs_setmode(sc); 115337785Smsmith error = 0; 115437785Smsmith break; 115537785Smsmith 1156140927Simp case SIOCSIFMEDIA: 1157140927Simp case SIOCGIFMEDIA: 1158140927Simp error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 1159140927Simp break; 116037785Smsmith 1161140927Simp default: 1162140921Simp error = ether_ioctl(ifp, command, data); 1163106937Ssam break; 1164140927Simp } 116537785Smsmith 116637785Smsmith (void) splx(s); 1167140928Simp return (error); 116837785Smsmith} 116937785Smsmith 117037785Smsmith/* 117137785Smsmith * Device timeout/watchdog routine. Entered if the device neglects to 117237785Smsmith * generate an interrupt after a transmit has been started on it. 117337785Smsmith */ 117437785Smsmithstatic void 117537785Smsmithcs_watchdog(struct ifnet *ifp) 117637785Smsmith{ 117758816Simp struct cs_softc *sc = ifp->if_softc; 117837785Smsmith 117937785Smsmith ifp->if_oerrors++; 1180121816Sbrooks log(LOG_ERR, "%s: device timeout\n", ifp->if_xname); 118137785Smsmith 118237785Smsmith /* Reset the interface */ 118337785Smsmith if (ifp->if_flags & IFF_UP) 118437785Smsmith cs_reset(sc); 118537785Smsmith else 118637785Smsmith cs_stop(sc); 118737785Smsmith} 118837785Smsmith 118937785Smsmithstatic int 119037785Smsmithcs_mediachange(struct ifnet *ifp) 119137785Smsmith{ 119237785Smsmith struct cs_softc *sc = ifp->if_softc; 119337785Smsmith struct ifmedia *ifm = &sc->media; 119437785Smsmith 119537785Smsmith if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 1196140928Simp return (EINVAL); 119737785Smsmith 1198140928Simp return (cs_mediaset(sc, ifm->ifm_media)); 119937785Smsmith} 120037785Smsmith 120137785Smsmithstatic void 120237785Smsmithcs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 120337785Smsmith{ 120437785Smsmith int line_status; 120537785Smsmith struct cs_softc *sc = ifp->if_softc; 120637785Smsmith 120737785Smsmith ifmr->ifm_active = IFM_ETHER; 120872940Simp line_status = cs_readreg(sc, PP_LineST); 120937785Smsmith if (line_status & TENBASET_ON) { 121037785Smsmith ifmr->ifm_active |= IFM_10_T; 121137785Smsmith if (sc->chip_type != CS8900) { 121272940Simp if (cs_readreg(sc, PP_AutoNegST) & FDX_ACTIVE) 121337785Smsmith ifmr->ifm_active |= IFM_FDX; 121472940Simp if (cs_readreg(sc, PP_AutoNegST) & HDX_ACTIVE) 121537785Smsmith ifmr->ifm_active |= IFM_HDX; 121637785Smsmith } 121737785Smsmith ifmr->ifm_status = IFM_AVALID; 121837785Smsmith if (line_status & LINK_OK) 121937785Smsmith ifmr->ifm_status |= IFM_ACTIVE; 122037785Smsmith } else { 122138305Smsmith if (line_status & AUI_ON) { 122272940Simp cs_writereg(sc, PP_SelfCTL, cs_readreg(sc, PP_SelfCTL) | 122372940Simp HCB1_ENBL); 122438305Smsmith if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^ 122572940Simp (cs_readreg(sc, PP_SelfCTL) & HCB1)) 122638305Smsmith ifmr->ifm_active |= IFM_10_2; 122738305Smsmith else 122838305Smsmith ifmr->ifm_active |= IFM_10_5; 122938305Smsmith } 123037785Smsmith } 123137785Smsmith} 123237785Smsmith 123337785Smsmithstatic int 123437785Smsmithcs_mediaset(struct cs_softc *sc, int media) 123537785Smsmith{ 1236179532Simp int error = 0; 123737785Smsmith 123837785Smsmith /* Stop the receiver & transmitter */ 123972940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) & 124072940Simp ~(SERIAL_RX_ON | SERIAL_TX_ON)); 124137785Smsmith 124237785Smsmith#ifdef CS_DEBUG 1243162321Sglebius device_printf(sc->dev, "%s media=%x\n", __func__, media); 124437785Smsmith#endif 124537785Smsmith 124637785Smsmith switch (IFM_SUBTYPE(media)) { 124737785Smsmith default: 124837785Smsmith case IFM_AUTO: 124938592Smsmith if ((error=enable_tp(sc))==0) 125038592Smsmith error = cs_duplex_auto(sc); 125143314Sdillon else if ((error=enable_bnc(sc)) != 0) 125238592Smsmith error = enable_aui(sc); 125337785Smsmith break; 125437785Smsmith case IFM_10_T: 1255179532Simp enable_tp(sc); 125637785Smsmith if (media & IFM_FDX) 125737785Smsmith cs_duplex_full(sc); 125837785Smsmith else if (media & IFM_HDX) 125937785Smsmith cs_duplex_half(sc); 126037785Smsmith else 126137785Smsmith error = cs_duplex_auto(sc); 126237785Smsmith break; 126337785Smsmith case IFM_10_2: 126437785Smsmith error = enable_bnc(sc); 126537785Smsmith break; 126637785Smsmith case IFM_10_5: 126737785Smsmith error = enable_aui(sc); 126837785Smsmith break; 126937785Smsmith } 127037785Smsmith 127137785Smsmith /* 127237785Smsmith * Turn the transmitter & receiver back on 127337785Smsmith */ 127472940Simp cs_writereg(sc, PP_LineCTL, cs_readreg(sc, PP_LineCTL) | 127572940Simp SERIAL_RX_ON | SERIAL_TX_ON); 127637785Smsmith 1277140928Simp return (error); 127837785Smsmith} 1279