Home | History | Annotate | Line # | Download | only in ic
cs89x0.c revision 1.46
      1 /*	$NetBSD: cs89x0.c,v 1.46 2019/05/28 07:41:48 msaitoh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Christopher Gilbert
      5  * All rights reserved.
      6  *
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  * 3. The name of the company nor the name of the author may be used to
     13  *    endorse or promote products derived from this software without specific
     14  *    prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright 1997
     31  * Digital Equipment Corporation. All rights reserved.
     32  *
     33  * This software is furnished under license and may be used and
     34  * copied only in accordance with the following terms and conditions.
     35  * Subject to these conditions, you may download, copy, install,
     36  * use, modify and distribute this software in source and/or binary
     37  * form. No title or ownership is transferred hereby.
     38  *
     39  * 1) Any source code used, modified or distributed must reproduce
     40  *    and retain this copyright notice and list of conditions as
     41  *    they appear in the source file.
     42  *
     43  * 2) No right is granted to use any trade name, trademark, or logo of
     44  *    Digital Equipment Corporation. Neither the "Digital Equipment
     45  *    Corporation" name nor any trademark or logo of Digital Equipment
     46  *    Corporation may be used to endorse or promote products derived
     47  *    from this software without the prior written permission of
     48  *    Digital Equipment Corporation.
     49  *
     50  * 3) This software is provided "AS-IS" and any express or implied
     51  *    warranties, including but not limited to, any implied warranties
     52  *    of merchantability, fitness for a particular purpose, or
     53  *    non-infringement are disclaimed. In no event shall DIGITAL be
     54  *    liable for any damages whatsoever, and in particular, DIGITAL
     55  *    shall not be liable for special, indirect, consequential, or
     56  *    incidental damages or damages for lost profits, loss of
     57  *    revenue or loss of use, whether such damages arise in contract,
     58  *    negligence, tort, under statute, in equity, at law or otherwise,
     59  *    even if advised of the possibility of such damage.
     60  */
     61 
     62 /*
     63 **++
     64 **  FACILITY
     65 **
     66 **     Device Driver for the Crystal CS8900 ISA Ethernet Controller.
     67 **
     68 **  ABSTRACT
     69 **
     70 **     This module provides standard ethernet access for INET protocols
     71 **     only.
     72 **
     73 **  AUTHORS
     74 **
     75 **     Peter Dettori     SEA - Software Engineering.
     76 **
     77 **  CREATION DATE:
     78 **
     79 **     13-Feb-1997.
     80 **
     81 **  MODIFICATION HISTORY (Digital):
     82 **
     83 **     Revision 1.27  1998/01/20  17:59:40  cgd
     84 **     update for moved headers
     85 **
     86 **     Revision 1.26  1998/01/12  19:29:36  cgd
     87 **     use arm32/isa versions of isadma code.
     88 **
     89 **     Revision 1.25  1997/12/12  01:35:27  cgd
     90 **     convert to use new arp code (from Brini)
     91 **
     92 **     Revision 1.24  1997/12/10  22:31:56  cgd
     93 **     trim some fat (get rid of ability to explicitly supply enet addr, since
     94 **     it was never used and added a bunch of code which really doesn't belong in
     95 **     an enet driver), and clean up slightly.
     96 **
     97 **     Revision 1.23  1997/10/06  16:42:12  cgd
     98 **     copyright notices
     99 **
    100 **     Revision 1.22  1997/06/20  19:38:01  chaiken
    101 **     fixes some smartcard problems
    102 **
    103 **     Revision 1.21  1997/06/10 02:56:20  grohn
    104 **     Added call to ledNetActive
    105 **
    106 **     Revision 1.20  1997/06/05 00:47:06  dettori
    107 **     Changed cs_process_rx_dma to reset and re-initialise the
    108 **     ethernet chip when DMA gets out of sync, or mbufs
    109 **     can't be allocated.
    110 **
    111 **     Revision 1.19  1997/06/03 03:09:58  dettori
    112 **     Turn off sc_txbusy flag when a transmit underrun
    113 **     occurs.
    114 **
    115 **     Revision 1.18  1997/06/02 00:04:35  dettori
    116 **     redefined the transmit table to get around the nfs_timer bug while we are
    117 **     looking into it further.
    118 **
    119 **     Also changed interrupts from EDGE to LEVEL.
    120 **
    121 **     Revision 1.17  1997/05/27 23:31:01  dettori
    122 **     Pulled out changes to DMAMODE defines.
    123 **
    124 **     Revision 1.16  1997/05/23 04:25:16  cgd
    125 **     reformat log so it fits in 80cols
    126 **
    127 **     Revision 1.15  1997/05/23  04:22:18  cgd
    128 **     remove the existing copyright notice (which Peter Dettori indicated
    129 **     was incorrect, copied from an existing NetBSD file only so that the
    130 **     file would have a copyright notice on it, and which he'd intended to
    131 **     replace).  Replace it with a Digital copyright notice, cloned from
    132 **     ess.c.  It's not really correct either (it indicates that the source
    133 **     is Digital confidential!), but is better than nothing and more
    134 **     correct than what was there before.
    135 **
    136 **     Revision 1.14  1997/05/23  04:12:50  cgd
    137 **     use an adaptive transmit start algorithm: start by telling the chip
    138 **     to start transmitting after 381 bytes have been fed to it.  if that
    139 **     gets transmit underruns, ramp down to 1021 bytes then "whole
    140 **     packet."  If successful at a given level for a while, try the next
    141 **     more agressive level.  This code doesn't ever try to start
    142 **     transmitting after 5 bytes have been sent to the NIC, because
    143 **     that underruns rather regularly.  The back-off and ramp-up mechanism
    144 **     could probably be tuned a little bit, but this works well enough to
    145 **     support > 1MB/s transmit rates on a clear ethernet (which is about
    146 **     20-25% better than the driver had previously been getting).
    147 **
    148 **     Revision 1.13  1997/05/22  21:06:54  cgd
    149 **     redo cs_copy_tx_frame() from scratch.  It had a fatal flaw: it was blindly
    150 **     casting from uint8_t * to uint16_t * without worrying about alignment
    151 **     issues.  This would cause bogus data to be spit out for mbufs with
    152 **     misaligned data.  For instance, it caused the following bits to appear
    153 **     on the wire:
    154 **     	... etBND 1S2C .SHA(K) R ...
    155 **     	    11112222333344445555
    156 **     which should have appeared as:
    157 **     	... NetBSD 1.2C (SHARK) ...
    158 **     	    11112222333344445555
    159 **     Note the apparent 'rotate' of the bytes in the word, which was due to
    160 **     incorrect unaligned accesses.  This data corruption was the cause of
    161 **     incoming telnet/rlogin hangs.
    162 **
    163 **     Revision 1.12  1997/05/22  01:55:32  cgd
    164 **     reformat log so it fits in 80cols
    165 **
    166 **     Revision 1.11  1997/05/22  01:50:27  cgd
    167 **     * enable input packet address checking in the BPF+IFF_PROMISCUOUS case,
    168 **       so packets aimed at other hosts don't get sent to ether_input().
    169 **     * Add a static const char *rcsid initialized with an RCS Id tag, so that
    170 **       you can easily tell (`strings`) what version of the driver is in your
    171 **       kernel binary.
    172 **     * get rid of ether_cmp().  It was inconsistently used, not necessarily
    173 **       safe, and not really a performance win anyway.  (It was only used when
    174 **       setting up the multicast logical address filter, which is an
    175 **       infrequent event.  It could have been used in the IFF_PROMISCUOUS
    176 **       address check above, but the benefit of it vs. memcmp would be
    177 **       inconsequential, there.)  Use memcmp() instead.
    178 **     * restructure csStartOuput to avoid the following bugs in the case where
    179 **       txWait was being set:
    180 **         * it would accidentally drop the outgoing packet if told to wait
    181 **           but the outgoing packet queue was empty.
    182 **         * it would bpf_mtap() the outgoing packet multiple times (once for
    183 **           each time it was told to wait), and would also recalculate
    184 **           the length of the outgoing packet each time it was told to
    185 **           wait.
    186 **       While there, rename txWait to txLoop, since with the new structure of
    187 **       the code, the latter name makes more sense.
    188 **
    189 **     Revision 1.10  1997/05/19  02:03:20  cgd
    190 **     Set RX_CTL in cs_set_ladr_filt(), rather than cs_initChip().  cs_initChip()
    191 **     is the only caller of cs_set_ladr_filt(), and always calls it, so this
    192 **     ends up being logically the same.  In cs_set_ladr_filt(), if IFF_PROMISC
    193 **     is set, enable promiscuous mode (and set IFF_ALLMULTI), otherwise behave
    194 **     as before.
    195 **
    196 **     Revision 1.9  1997/05/19  01:45:37  cgd
    197 **     create a new function, cs_ether_input(), which does received-packet
    198 **     BPF and ether_input processing.  This code used to be in three places,
    199 **     and centralizing it will make adding IFF_PROMISC support much easier.
    200 **     Also, in cs_copy_tx_frame(), put it some (currently disabled) code to
    201 **     do copies with bus_space_write_region_2().  It's more correct, and
    202 **     potentially more efficient.  That function needs to be gutted (to
    203 **     deal properly with alignment issues, which it currently does wrong),
    204 **     however, and the change doesn't gain much, so there's no point in
    205 **     enabling it now.
    206 **
    207 **     Revision 1.8  1997/05/19  01:17:10  cgd
    208 **     fix a comment re: the setting of the TxConfig register.  Clean up
    209 **     interface counter maintenance (make it use standard idiom).
    210 **
    211 **--
    212 */
    213 
    214 #include <sys/cdefs.h>
    215 __KERNEL_RCSID(0, "$NetBSD: cs89x0.c,v 1.46 2019/05/28 07:41:48 msaitoh Exp $");
    216 
    217 #include "opt_inet.h"
    218 
    219 #include <sys/param.h>
    220 #include <sys/systm.h>
    221 #include <sys/mbuf.h>
    222 #include <sys/syslog.h>
    223 #include <sys/socket.h>
    224 #include <sys/device.h>
    225 #include <sys/malloc.h>
    226 #include <sys/ioctl.h>
    227 #include <sys/errno.h>
    228 #include <sys/bus.h>
    229 #include <sys/intr.h>
    230 #include <sys/rndsource.h>
    231 
    232 #include <net/if.h>
    233 #include <net/if_ether.h>
    234 #include <net/if_media.h>
    235 #include <net/bpf.h>
    236 
    237 #ifdef INET
    238 #include <netinet/in.h>
    239 #include <netinet/if_inarp.h>
    240 #endif
    241 
    242 #include <dev/ic/cs89x0reg.h>
    243 #include <dev/ic/cs89x0var.h>
    244 
    245 #ifdef SHARK
    246 #include <shark/shark/sequoia.h>
    247 #endif
    248 
    249 /*
    250  * MACRO DEFINITIONS
    251  */
    252 #define CS_OUTPUT_LOOP_MAX 100	/* max times round notorious tx loop */
    253 
    254 /*
    255  * FUNCTION PROTOTYPES
    256  */
    257 static void	cs_get_default_media(struct cs_softc *);
    258 static int	cs_get_params(struct cs_softc *);
    259 static int	cs_get_enaddr(struct cs_softc *);
    260 static int	cs_reset_chip(struct cs_softc *);
    261 static void	cs_reset(struct cs_softc *);
    262 static int	cs_ioctl(struct ifnet *, u_long, void *);
    263 static void	cs_initChip(struct cs_softc *);
    264 static void	cs_buffer_event(struct cs_softc *, uint16_t);
    265 static void	cs_transmit_event(struct cs_softc *, uint16_t);
    266 static void	cs_receive_event(struct cs_softc *, uint16_t);
    267 static void	cs_process_receive(struct cs_softc *);
    268 static void	cs_process_rx_early(struct cs_softc *);
    269 static void	cs_start_output(struct ifnet *);
    270 static void	cs_copy_tx_frame(struct cs_softc *, struct mbuf *);
    271 static void	cs_set_ladr_filt(struct cs_softc *, struct ethercom *);
    272 static uint16_t cs_hash_index(char *);
    273 static void	cs_counter_event(struct cs_softc *, uint16_t);
    274 
    275 static int	cs_mediachange(struct ifnet *);
    276 static void	cs_mediastatus(struct ifnet *, struct ifmediareq *);
    277 
    278 static bool cs_shutdown(device_t, int);
    279 static int cs_enable(struct cs_softc *);
    280 static void cs_disable(struct cs_softc *);
    281 static void cs_stop(struct ifnet *, int);
    282 static int cs_scan_eeprom(struct cs_softc *);
    283 static int cs_read_pktpg_from_eeprom(struct cs_softc *, int, uint16_t *);
    284 
    285 
    286 /*
    287  * GLOBAL DECLARATIONS
    288  */
    289 
    290 /*
    291  * Xmit-early table.
    292  *
    293  * To get better performance, we tell the chip to start packet
    294  * transmission before the whole packet is copied to the chip.
    295  * However, this can fail under load.  When it fails, we back off
    296  * to a safer setting for a little while.
    297  *
    298  * txcmd is the value of txcmd used to indicate when to start transmission.
    299  * better is the next 'better' state in the table.
    300  * better_count is the number of output packets before transition to the
    301  *   better state.
    302  * worse is the next 'worse' state in the table.
    303  *
    304  * Transition to the next worse state happens automatically when a
    305  * transmittion underrun occurs.
    306  */
    307 struct cs_xmit_early {
    308 	uint16_t	txcmd;
    309 	int		better;
    310 	int		better_count;
    311 	int		worse;
    312 } cs_xmit_early_table[3] = {
    313 	{ TX_CMD_START_381,	0,	INT_MAX,	1, },
    314 	{ TX_CMD_START_1021,	0,	50000,		2, },
    315 	{ TX_CMD_START_ALL,	1,	5000,		2, },
    316 };
    317 
    318 int cs_default_media[] = {
    319 	IFM_ETHER | IFM_10_2,
    320 	IFM_ETHER | IFM_10_5,
    321 	IFM_ETHER | IFM_10_T,
    322 	IFM_ETHER | IFM_10_T | IFM_FDX,
    323 };
    324 int cs_default_nmedia = __arraycount(cs_default_media);
    325 
    326 int
    327 cs_attach(struct cs_softc *sc, uint8_t *enaddr, int *media,
    328 	  int nmedia, int defmedia)
    329 {
    330 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    331 	const char *chipname, *medname;
    332 	uint16_t reg;
    333 	int i;
    334 
    335 	/* Start out in IO mode */
    336 	sc->sc_memorymode = FALSE;
    337 
    338 	/* Make sure we're right */
    339 	for (i = 0; i < 10000; i++) {
    340 		reg = CS_READ_PACKET_PAGE(sc, PKTPG_EISA_NUM);
    341 		if (reg == EISA_NUM_CRYSTAL)
    342 			break;
    343 	}
    344 	if (i == 10000) {
    345 		aprint_error_dev(sc->sc_dev, "wrong id(0x%x)\n", reg);
    346 		return 1; /* XXX should panic? */
    347 	}
    348 
    349 	reg = CS_READ_PACKET_PAGE(sc, PKTPG_PRODUCT_ID);
    350 	sc->sc_prodid = reg & PROD_ID_MASK;
    351 	sc->sc_prodrev = (reg & PROD_REV_MASK) >> 8;
    352 
    353 	switch (sc->sc_prodid) {
    354 	case PROD_ID_CS8900:
    355 		chipname = "CS8900";
    356 		break;
    357 	case PROD_ID_CS8920:
    358 		chipname = "CS8920";
    359 		break;
    360 	case PROD_ID_CS8920M:
    361 		chipname = "CS8920M";
    362 		break;
    363 	default:
    364 		panic("cs_attach: impossible");
    365 	}
    366 
    367 	/*
    368 	 * The first thing to do is check that the mbuf cluster size is
    369 	 * greater than the MTU for an ethernet frame. The code depends on
    370 	 * this and to port this to a OS where this was not the case would
    371 	 * not be straightforward.
    372 	 *
    373 	 * We need 1 byte spare because our packet read loop can overrun.
    374 	 * and we may need pad bytes to align ip header.
    375 	 */
    376 	if (MCLBYTES < ETHER_MAX_LEN + 1 + ALIGN(sizeof(struct ether_header))
    377 	    - sizeof(struct ether_header)) {
    378 		printf("%s: MCLBYTES too small for Ethernet frame\n",
    379 		    device_xname(sc->sc_dev));
    380 		return 1;
    381 	}
    382 
    383 	/* Start out not transmitting */
    384 	sc->sc_txbusy = FALSE;
    385 
    386 	/* Set up early transmit threshhold */
    387 	sc->sc_xe_ent = 0;
    388 	sc->sc_xe_togo = cs_xmit_early_table[sc->sc_xe_ent].better_count;
    389 
    390 	/* Initialize ifnet structure. */
    391 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    392 	ifp->if_softc = sc;
    393 	ifp->if_start = cs_start_output;
    394 	ifp->if_init = cs_init;
    395 	ifp->if_ioctl = cs_ioctl;
    396 	ifp->if_stop = cs_stop;
    397 	ifp->if_watchdog = NULL;	/* No watchdog at this stage */
    398 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    399 	IFQ_SET_READY(&ifp->if_snd);
    400 
    401 	/* Initialize ifmedia structures. */
    402 	ifmedia_init(&sc->sc_media, 0, cs_mediachange, cs_mediastatus);
    403 
    404 	if (media != NULL) {
    405 		for (i = 0; i < nmedia; i++)
    406 			ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    407 		ifmedia_set(&sc->sc_media, defmedia);
    408 	} else {
    409 		for (i = 0; i < cs_default_nmedia; i++)
    410 			ifmedia_add(&sc->sc_media, cs_default_media[i],
    411 			    0, NULL);
    412 		cs_get_default_media(sc);
    413 	}
    414 
    415 	if (sc->sc_cfgflags & CFGFLG_PARSE_EEPROM) {
    416 		if (cs_scan_eeprom(sc) == CS_ERROR) {
    417 			/*
    418 			 * Failed to scan the eeprom, pretend there isn't an
    419 			 * eeprom
    420 			 */
    421 			aprint_error_dev(sc->sc_dev,
    422 			    "unable to scan EEPROM\n");
    423 			sc->sc_cfgflags |= CFGFLG_NOT_EEPROM;
    424 		}
    425 	}
    426 
    427 	if ((sc->sc_cfgflags & CFGFLG_NOT_EEPROM) == 0) {
    428 		/* Get parameters from the EEPROM */
    429 		if (cs_get_params(sc) == CS_ERROR) {
    430 			aprint_error_dev(sc->sc_dev,
    431 			    "unable to get settings from EEPROM\n");
    432 			return 1;
    433 		}
    434 	}
    435 
    436 	if (enaddr != NULL)
    437 		memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
    438 	else if ((sc->sc_cfgflags & CFGFLG_NOT_EEPROM) == 0) {
    439 		/* Get and store the Ethernet address */
    440 		if (cs_get_enaddr(sc) == CS_ERROR) {
    441 			aprint_error_dev(sc->sc_dev,
    442 			    "unable to read Ethernet address\n");
    443 			return 1;
    444 		}
    445 	} else {
    446 #if 1
    447 		int j;
    448 		uint v;
    449 
    450 		for (j = 0; j < 6; j += 2) {
    451 			v = CS_READ_PACKET_PAGE(sc, PKTPG_IND_ADDR + j);
    452 			sc->sc_enaddr[j + 0] = v;
    453 			sc->sc_enaddr[j + 1] = v >> 8;
    454 		}
    455 #else
    456 		printf("%s: no Ethernet address!\n", device_xname(sc->sc_dev));
    457 		return 1;
    458 #endif
    459 	}
    460 
    461 	switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
    462 	case IFM_10_2:
    463 		medname = "BNC";
    464 		break;
    465 	case IFM_10_5:
    466 		medname = "AUI";
    467 		break;
    468 	case IFM_10_T:
    469 		if (sc->sc_media.ifm_cur->ifm_media & IFM_FDX)
    470 			medname = "UTP <full-duplex>";
    471 		else
    472 			medname = "UTP";
    473 		break;
    474 	default:
    475 		panic("cs_attach: impossible");
    476 	}
    477 	printf("%s: %s rev. %c, address %s, media %s\n",
    478 	    device_xname(sc->sc_dev),
    479 	    chipname, sc->sc_prodrev + 'A', ether_sprintf(sc->sc_enaddr),
    480 	    medname);
    481 
    482 	if (sc->sc_dma_attach)
    483 		(*sc->sc_dma_attach)(sc);
    484 
    485 	/* Attach the interface. */
    486 	if_attach(ifp);
    487 	if_deferred_start_init(ifp, NULL);
    488 	ether_ifattach(ifp, sc->sc_enaddr);
    489 
    490 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    491 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
    492 	sc->sc_cfgflags |= CFGFLG_ATTACHED;
    493 
    494 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, cs_shutdown))
    495 		pmf_class_network_register(sc->sc_dev, ifp);
    496 	else
    497 		aprint_error_dev(sc->sc_dev,
    498 		    "couldn't establish power handler\n");
    499 
    500 	/* Reset the chip */
    501 	if (cs_reset_chip(sc) == CS_ERROR) {
    502 		aprint_error_dev(sc->sc_dev, "reset failed\n");
    503 		cs_detach(sc);
    504 		return 1;
    505 	}
    506 
    507 	return 0;
    508 }
    509 
    510 int
    511 cs_detach(struct cs_softc *sc)
    512 {
    513 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    514 
    515 	if (sc->sc_cfgflags & CFGFLG_ATTACHED) {
    516 		rnd_detach_source(&sc->rnd_source);
    517 		ether_ifdetach(ifp);
    518 		if_detach(ifp);
    519 		sc->sc_cfgflags &= ~CFGFLG_ATTACHED;
    520 	}
    521 
    522 #if 0
    523 	/* XXX not necessary */
    524 	if (sc->sc_cfgflags & CFGFLG_DMA_MODE) {
    525 		isa_dmamem_unmap(sc->sc_ic, sc->sc_drq, sc->sc_dmabase,
    526 		    sc->sc_dmasize);
    527 		isa_dmamem_free(sc->sc_ic, sc->sc_drq, sc->sc_dmaaddr,
    528 		    sc->sc_dmasize);
    529 		isa_dmamap_destroy(sc->sc_ic, sc->sc_drq);
    530 		sc->sc_cfgflags &= ~CFGFLG_DMA_MODE;
    531 	}
    532 #endif
    533 
    534 	pmf_device_deregister(sc->sc_dev);
    535 
    536 	return 0;
    537 }
    538 
    539 bool
    540 cs_shutdown(device_t self, int howto)
    541 {
    542 	struct cs_softc *sc;
    543 
    544 	sc = device_private(self);
    545 	cs_reset(sc);
    546 
    547 	return true;
    548 }
    549 
    550 void
    551 cs_get_default_media(struct cs_softc *sc)
    552 {
    553 	uint16_t adp_cfg, xmit_ctl;
    554 
    555 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    556 		aprint_error_dev(sc->sc_dev,
    557 		    "cs_get_default_media: EEPROM missing or bad\n");
    558 		goto fakeit;
    559 	}
    560 
    561 	if (cs_read_eeprom(sc, EEPROM_ADPTR_CFG, &adp_cfg) == CS_ERROR) {
    562 		aprint_error_dev(sc->sc_dev,
    563 		    "unable to read adapter config from EEPROM\n");
    564 		goto fakeit;
    565 	}
    566 
    567 	if (cs_read_eeprom(sc, EEPROM_XMIT_CTL, &xmit_ctl) == CS_ERROR) {
    568 		aprint_error_dev(sc->sc_dev,
    569 		    "unable to read transmit control from EEPROM\n");
    570 		goto fakeit;
    571 	}
    572 
    573 	switch (adp_cfg & ADPTR_CFG_MEDIA) {
    574 	case ADPTR_CFG_AUI:
    575 		ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10_5);
    576 		break;
    577 	case ADPTR_CFG_10BASE2:
    578 		ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10_2);
    579 		break;
    580 	case ADPTR_CFG_10BASET:
    581 	default:
    582 		if (xmit_ctl & XMIT_CTL_FDX)
    583 			ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10_T
    584 			    | IFM_FDX);
    585 		else
    586 			ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10_T);
    587 		break;
    588 	}
    589 	return;
    590 
    591  fakeit:
    592 	aprint_error_dev(sc->sc_dev,
    593 	    "WARNING: default media setting may be inaccurate\n");
    594 	/* XXX Arbitrary... */
    595 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10_T);
    596 }
    597 
    598 /*
    599  * cs_scan_eeprom
    600  *
    601  * Attempt to take a complete copy of the eeprom into main memory.
    602  * this will allow faster parsing of the eeprom data.
    603  *
    604  * Only tested against a 8920M's eeprom, but the data sheet for the
    605  * 8920A indicates that is uses the same layout.
    606  */
    607 int
    608 cs_scan_eeprom(struct cs_softc *sc)
    609 {
    610 	uint16_t result;
    611 	int	i;
    612 	int	eeprom_size;
    613 	uint8_t checksum = 0;
    614 
    615 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    616 		aprint_error_dev(sc->sc_dev,
    617 		    "cs_scan_params: EEPROM missing or bad\n");
    618 		return CS_ERROR;
    619 	}
    620 
    621 	/*
    622 	 * Read the 0th word from the eeprom, it will tell us the length
    623 	 * and if the eeprom is valid
    624 	 */
    625 	cs_read_eeprom(sc, 0, &result);
    626 
    627 	/* Check the eeprom signature */
    628 	if ((result & 0xE000) != 0xA000) {
    629 		/* Empty eeprom */
    630 		return CS_ERROR;
    631 	}
    632 
    633 	/*
    634 	 * Take the eeprom size (note the read value doesn't include the header
    635 	 * word)
    636 	 */
    637 	eeprom_size = (result & 0xff) + 2;
    638 
    639 	sc->eeprom_data = malloc(eeprom_size, M_DEVBUF, M_WAITOK);
    640 	if (sc->eeprom_data == NULL) {
    641 		/* No memory, treat this as if there's no eeprom */
    642 		return CS_ERROR;
    643 	}
    644 
    645 	sc->eeprom_size = eeprom_size;
    646 
    647 	/* Read the eeprom into the buffer, also calculate the checksum	 */
    648 	for (i = 0; i < (eeprom_size >> 1); i++) {
    649 		cs_read_eeprom(sc, i, &(sc->eeprom_data[i]));
    650 		checksum += (sc->eeprom_data[i] & 0xff00) >> 8;
    651 		checksum += (sc->eeprom_data[i] & 0x00ff);
    652 	}
    653 
    654 	/*
    655 	 * Validate checksum calculation, the sum of all the bytes should be 0,
    656 	 * as the high byte of the last word is the 2's complement of the
    657 	 * sum to that point.
    658 	 */
    659 	if (checksum != 0) {
    660 		aprint_error_dev(sc->sc_dev, "eeprom checksum failure\n");
    661 		return CS_ERROR;
    662 	}
    663 
    664 	return CS_OK;
    665 }
    666 
    667 static int
    668 cs_read_pktpg_from_eeprom(struct cs_softc *sc, int pktpg, uint16_t *pValue)
    669 {
    670 	int x, maxword;
    671 
    672 	/* Check that we have eeprom data */
    673 	if ((sc->eeprom_data == NULL) || (sc->eeprom_size < 2))
    674 		return CS_ERROR;
    675 
    676 	/*
    677 	 * We only want to read the data words, the last word contains the
    678 	 * checksum
    679 	 */
    680 	maxword = (sc->eeprom_size - 2) >> 1;
    681 
    682 	/* Start 1 word in, as the first word is the length and signature */
    683 	x = 1;
    684 
    685 	while ( x < (maxword)) {
    686 		uint16_t header;
    687 		int group_size;
    688 		int offset;
    689 		int offset_max;
    690 
    691 		/* Read in the group header word */
    692 		header = sc->eeprom_data[x];
    693 		x++;	/* Skip group header */
    694 
    695 		/*
    696 		 * Size of group in words is in the top 4 bits, note that it
    697 		 * is one less than the number of words
    698 		 */
    699 		group_size = header & 0xF000;
    700 
    701 		/*
    702 		 * CS8900 Data sheet says this should be 0x01ff,
    703 		 * but my cs8920 eeprom has higher offsets,
    704 		 * perhaps the 8920 allows higher offsets, otherwise
    705 		 * it's writing to places that it shouldn't
    706 		 */
    707 		/* Work out the offsets this group covers */
    708 		offset = header & 0x0FFF;
    709 		offset_max = offset + (group_size << 1);
    710 
    711 		/* Check if the pkgpg we're after is in this group */
    712 		if ((offset <= pktpg) && (pktpg <= offset_max)) {
    713 			/* The pkgpg value we want is in here */
    714 			int eeprom_location;
    715 
    716 			eeprom_location = ((pktpg - offset) >> 1) ;
    717 
    718 			*pValue = sc->eeprom_data[x + eeprom_location];
    719 			return CS_OK;
    720 		} else {
    721 			/* Skip this group (+ 1 for first entry) */
    722 			x += group_size + 1;
    723 		}
    724 	}
    725 
    726 	/*
    727 	 * If we've fallen out here then we don't have a value in the EEPROM
    728 	 * for this pktpg so return an error
    729 	 */
    730 	return CS_ERROR;
    731 }
    732 
    733 int
    734 cs_get_params(struct cs_softc *sc)
    735 {
    736 	uint16_t isaConfig;
    737 	uint16_t adapterConfig;
    738 
    739 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    740 		aprint_error_dev(sc->sc_dev,
    741 		    "cs_get_params: EEPROM missing or bad\n");
    742 		return CS_ERROR;
    743 	}
    744 
    745 	if (sc->sc_cfgflags & CFGFLG_PARSE_EEPROM) {
    746 		/* Get ISA configuration from the EEPROM */
    747 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_BUS_CTL, &isaConfig)
    748 		    == CS_ERROR) {
    749 			/*
    750 			 * Eeprom doesn't have this value, use data sheet
    751 			 * default
    752 			 */
    753 			isaConfig = 0x0017;
    754 		}
    755 
    756 		/* Get adapter configuration from the EEPROM */
    757 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_SELF_CTL,
    758 		    &adapterConfig) == CS_ERROR) {
    759 			/*
    760 			 * Eeprom doesn't have this value, use data sheet
    761 			 * default
    762 			 */
    763 			adapterConfig = 0x0015;
    764 		}
    765 
    766 		/* Copy the USE_SA flag */
    767 		if (isaConfig & BUS_CTL_USE_SA)
    768 			sc->sc_cfgflags |= CFGFLG_USE_SA;
    769 
    770 		/* Copy the IO Channel Ready flag */
    771 		if (isaConfig & BUS_CTL_IOCHRDY)
    772 			sc->sc_cfgflags |= CFGFLG_IOCHRDY;
    773 
    774 		/* Copy the DC/DC Polarity flag */
    775 		if (adapterConfig & SELF_CTL_HCB1)
    776 			sc->sc_cfgflags |= CFGFLG_DCDC_POL;
    777 	} else {
    778 		/* Get ISA configuration from the EEPROM */
    779 		if (cs_read_eeprom(sc, EEPROM_ISA_CFG, &isaConfig) == CS_ERROR)
    780 			goto eeprom_bad;
    781 
    782 		/* Get adapter configuration from the EEPROM */
    783 		if (cs_read_eeprom(sc, EEPROM_ADPTR_CFG, &adapterConfig)
    784 		    == CS_ERROR)
    785 			goto eeprom_bad;
    786 
    787 		/* Copy the USE_SA flag */
    788 		if (isaConfig & ISA_CFG_USE_SA)
    789 			sc->sc_cfgflags |= CFGFLG_USE_SA;
    790 
    791 		/* Copy the IO Channel Ready flag */
    792 		if (isaConfig & ISA_CFG_IOCHRDY)
    793 			sc->sc_cfgflags |= CFGFLG_IOCHRDY;
    794 
    795 		/* Copy the DC/DC Polarity flag */
    796 		if (adapterConfig & ADPTR_CFG_DCDC_POL)
    797 			sc->sc_cfgflags |= CFGFLG_DCDC_POL;
    798 	}
    799 
    800 	return CS_OK;
    801 eeprom_bad:
    802 	aprint_error_dev(sc->sc_dev,
    803 	    "cs_get_params: unable to read from EEPROM\n");
    804 	return CS_ERROR;
    805 }
    806 
    807 int
    808 cs_get_enaddr(struct cs_softc *sc)
    809 {
    810 	uint16_t myea[ETHER_ADDR_LEN / sizeof(uint16_t)];
    811 	int i;
    812 
    813 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    814 		aprint_error_dev(sc->sc_dev,
    815 		    "cs_get_enaddr: EEPROM missing or bad\n");
    816 		return CS_ERROR;
    817 	}
    818 
    819 	/* Get Ethernet address from the EEPROM */
    820 	if (sc->sc_cfgflags & CFGFLG_PARSE_EEPROM) {
    821 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_IND_ADDR, &myea[0])
    822 				== CS_ERROR)
    823 			goto eeprom_bad;
    824 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_IND_ADDR + 2, &myea[1])
    825 				== CS_ERROR)
    826 			goto eeprom_bad;
    827 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_IND_ADDR + 4, &myea[2])
    828 				== CS_ERROR)
    829 			goto eeprom_bad;
    830 	} else {
    831 		if (cs_read_eeprom(sc, EEPROM_IND_ADDR_H, &myea[0]) == CS_ERROR)
    832 			goto eeprom_bad;
    833 		if (cs_read_eeprom(sc, EEPROM_IND_ADDR_M, &myea[1]) == CS_ERROR)
    834 			goto eeprom_bad;
    835 		if (cs_read_eeprom(sc, EEPROM_IND_ADDR_L, &myea[2]) == CS_ERROR)
    836 			goto eeprom_bad;
    837 	}
    838 
    839 	for (i = 0; i < __arraycount(myea); i++) {
    840 		sc->sc_enaddr[i * 2 + 0] = myea[i];
    841 		sc->sc_enaddr[i * 2 + 1] = myea[i] >> 8;
    842 	}
    843 
    844 	return CS_OK;
    845 
    846  eeprom_bad:
    847 	aprint_error_dev(sc->sc_dev,
    848 	    "cs_get_enaddr: unable to read from EEPROM\n");
    849 	return CS_ERROR;
    850 }
    851 
    852 int
    853 cs_reset_chip(struct cs_softc *sc)
    854 {
    855 	int intState;
    856 	int x;
    857 
    858 	/* Disable interrupts at the CPU so reset command is atomic */
    859 	intState = splnet();
    860 
    861 	/*
    862 	 * We are now resetting the chip
    863 	 *
    864 	 * A spurious interrupt is generated by the chip when it is reset. This
    865 	 * variable informs the interrupt handler to ignore this interrupt.
    866 	 */
    867 	sc->sc_resetting = TRUE;
    868 
    869 	/* Issue a reset command to the chip */
    870 	CS_WRITE_PACKET_PAGE(sc, PKTPG_SELF_CTL, SELF_CTL_RESET);
    871 
    872 	/* Re-enable interrupts at the CPU */
    873 	splx(intState);
    874 
    875 	/* The chip is always in IO mode after a reset */
    876 	sc->sc_memorymode = FALSE;
    877 
    878 	/* If transmission was in progress, it is not now */
    879 	sc->sc_txbusy = FALSE;
    880 
    881 	/*
    882 	 * There was a delay(125); here, but it seems uneccesary 125 usec is
    883 	 * 1/8000 of a second, not 1/8 of a second. the data sheet advises
    884 	 * 1/10 of a second here, but the SI_BUSY and INIT_DONE loops below
    885 	 * should be sufficient.
    886 	 */
    887 
    888 	/* Transition SBHE to switch chip from 8-bit to 16-bit */
    889 	IO_READ_1(sc, PORT_PKTPG_PTR + 0);
    890 	IO_READ_1(sc, PORT_PKTPG_PTR + 1);
    891 	IO_READ_1(sc, PORT_PKTPG_PTR + 0);
    892 	IO_READ_1(sc, PORT_PKTPG_PTR + 1);
    893 
    894 	/* Wait until the EEPROM is not busy */
    895 	for (x = 0; x < MAXLOOP; x++) {
    896 		if (!(CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_SI_BUSY))
    897 			break;
    898 	}
    899 
    900 	if (x == MAXLOOP)
    901 		return CS_ERROR;
    902 
    903 	/* Wait until initialization is done */
    904 	for (x = 0; x < MAXLOOP; x++) {
    905 		if (CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_INIT_DONE)
    906 			break;
    907 	}
    908 
    909 	if (x == MAXLOOP)
    910 		return CS_ERROR;
    911 
    912 	/* Reset is no longer in progress */
    913 	sc->sc_resetting = FALSE;
    914 
    915 	return CS_OK;
    916 }
    917 
    918 int
    919 cs_verify_eeprom(struct cs_softc *sc)
    920 {
    921 	uint16_t self_status;
    922 
    923 	/* Verify that the EEPROM is present and OK */
    924 	self_status = CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST);
    925 	if (((self_status & SELF_ST_EEP_PRES) &&
    926 	     (self_status & SELF_ST_EEP_OK)) == 0)
    927 		return CS_ERROR;
    928 
    929 	return CS_OK;
    930 }
    931 
    932 int
    933 cs_read_eeprom(struct cs_softc *sc, int offset, uint16_t *pValue)
    934 {
    935 	int x;
    936 
    937 	/* Ensure that the EEPROM is not busy */
    938 	for (x = 0; x < MAXLOOP; x++) {
    939 		if (!(CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST) &
    940 		      SELF_ST_SI_BUSY))
    941 			break;
    942 	}
    943 
    944 	if (x == MAXLOOP)
    945 		return CS_ERROR;
    946 
    947 	/* Issue the command to read the offset within the EEPROM */
    948 	CS_WRITE_PACKET_PAGE_IO(sc, PKTPG_EEPROM_CMD,
    949 	    offset | EEPROM_CMD_READ);
    950 
    951 	/* Wait until the command is completed */
    952 	for (x = 0; x < MAXLOOP; x++) {
    953 		if (!(CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST) &
    954 		      SELF_ST_SI_BUSY))
    955 			break;
    956 	}
    957 
    958 	if (x == MAXLOOP)
    959 		return CS_ERROR;
    960 
    961 	/* Get the EEPROM data from the EEPROM Data register */
    962 	*pValue = CS_READ_PACKET_PAGE_IO(sc, PKTPG_EEPROM_DATA);
    963 
    964 	return CS_OK;
    965 }
    966 
    967 void
    968 cs_initChip(struct cs_softc *sc)
    969 {
    970 	uint16_t busCtl;
    971 	uint16_t selfCtl;
    972 	uint16_t v;
    973 	uint16_t isaId;
    974 	int i;
    975 	int media = IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media);
    976 
    977 	/* Disable reception and transmission of frames */
    978 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL,
    979 	    CS_READ_PACKET_PAGE(sc, PKTPG_LINE_CTL) &
    980 	    ~LINE_CTL_RX_ON & ~LINE_CTL_TX_ON);
    981 
    982 	/* Disable interrupt at the chip */
    983 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    984 	    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) & ~BUS_CTL_INT_ENBL);
    985 
    986 	/* If IOCHRDY is enabled then clear the bit in the busCtl register */
    987 	busCtl = CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL);
    988 	if (sc->sc_cfgflags & CFGFLG_IOCHRDY) {
    989 		CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    990 		    busCtl & ~BUS_CTL_IOCHRDY);
    991 	} else {
    992 		CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    993 		    busCtl | BUS_CTL_IOCHRDY);
    994 	}
    995 
    996 	/* Set the Line Control register to match the media type */
    997 	if (media == IFM_10_T)
    998 		CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL, LINE_CTL_10BASET);
    999 	else
   1000 		CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL, LINE_CTL_AUI_ONLY);
   1001 
   1002 	/*
   1003 	 * Set the BSTATUS/HC1 pin to be used as HC1.  HC1 is used to
   1004 	 * enable the DC/DC converter
   1005 	 */
   1006 	selfCtl = SELF_CTL_HC1E;
   1007 
   1008 	/* If the media type is 10Base2 */
   1009 	if (media == IFM_10_2) {
   1010 		/* Enable the DC/DC converter if it has a low enable. */
   1011 		if ((sc->sc_cfgflags & CFGFLG_DCDC_POL) == 0)
   1012 			/*
   1013 			 * Set the HCB1 bit, which causes the HC1 pin to go
   1014 			 * low.
   1015 			 */
   1016 			selfCtl |= SELF_CTL_HCB1;
   1017 	} else { /* Media type is 10BaseT or AUI */
   1018 		/* Disable the DC/DC converter if it has a high enable. */
   1019 		if ((sc->sc_cfgflags & CFGFLG_DCDC_POL) != 0) {
   1020 			/*
   1021 			 * Set the HCB1 bit, which causes the HC1 pin to go
   1022 			 * low.
   1023 			 */
   1024 			selfCtl |= SELF_CTL_HCB1;
   1025 		}
   1026 	}
   1027 	CS_WRITE_PACKET_PAGE(sc, PKTPG_SELF_CTL, selfCtl);
   1028 
   1029 	/* Enable normal link pulse */
   1030 	if (sc->sc_prodid == PROD_ID_CS8920 || sc->sc_prodid == PROD_ID_CS8920M)
   1031 		CS_WRITE_PACKET_PAGE(sc, PKTPG_AUTONEG_CTL, AUTOCTL_NLP_ENABLE);
   1032 
   1033 	/* Enable full-duplex, if appropriate */
   1034 	if (sc->sc_media.ifm_cur->ifm_media & IFM_FDX)
   1035 		CS_WRITE_PACKET_PAGE(sc, PKTPG_TEST_CTL, TEST_CTL_FDX);
   1036 
   1037 	/* RX_CTL set in cs_set_ladr_filt(), below */
   1038 
   1039 	/* Enable all transmission interrupts */
   1040 	CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CFG, TX_CFG_ALL_IE);
   1041 
   1042 	/* Accept all receive interrupts */
   1043 	CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG, RX_CFG_ALL_IE);
   1044 
   1045 	/*
   1046 	 * Configure Operational Modes
   1047 	 *
   1048 	 * I have turned off the BUF_CFG_RX_MISS_IE, to speed things up, this
   1049 	 * is a better way to do it because the card has a counter which can be
   1050 	 * read to update the RX_MISS counter. This saves many interrupts.
   1051 	 *
   1052 	 * I have turned on the tx and rx overflow interrupts to counter using
   1053 	 * the receive miss interrupt. This is a better estimate of errors
   1054 	 * and requires lower system overhead.
   1055 	 */
   1056 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG, BUF_CFG_TX_UNDR_IE |
   1057 			  BUF_CFG_RX_DMA_IE);
   1058 
   1059 	if (sc->sc_dma_chipinit)
   1060 		(*sc->sc_dma_chipinit)(sc);
   1061 
   1062 	/* If memory mode is enabled */
   1063 	if (sc->sc_cfgflags & CFGFLG_MEM_MODE) {
   1064 		/* If external logic is present for address decoding */
   1065 		if (CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_EL_PRES) {
   1066 			/*
   1067 			 * Program the external logic to decode address bits
   1068 			 * SA20-SA23
   1069 			 */
   1070 			CS_WRITE_PACKET_PAGE(sc, PKTPG_EEPROM_CMD,
   1071 			    ((sc->sc_pktpgaddr & 0xffffff) >> 20) |
   1072 			    EEPROM_CMD_ELSEL);
   1073 		}
   1074 
   1075 		/*
   1076 		 * Write the packet page base physical address to the memory
   1077 		 * base register.
   1078 		 */
   1079 		CS_WRITE_PACKET_PAGE(sc, PKTPG_MEM_BASE + 0,
   1080 		    sc->sc_pktpgaddr & 0xFFFF);
   1081 		CS_WRITE_PACKET_PAGE(sc, PKTPG_MEM_BASE + 2,
   1082 		    sc->sc_pktpgaddr >> 16);
   1083 		busCtl = BUS_CTL_MEM_MODE;
   1084 
   1085 		/* Tell the chip to read the addresses off the SA pins */
   1086 		if (sc->sc_cfgflags & CFGFLG_USE_SA) {
   1087 			busCtl |= BUS_CTL_USE_SA;
   1088 		}
   1089 		CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
   1090 		    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) | busCtl);
   1091 
   1092 		/* We are in memory mode now! */
   1093 		sc->sc_memorymode = TRUE;
   1094 
   1095 		/*
   1096 		 * Wait here (10ms) for the chip to swap over. this is the
   1097 		 * maximum time that this could take.
   1098 		 */
   1099 		delay(10000);
   1100 
   1101 		/* Verify that we can read from the chip */
   1102 		isaId = CS_READ_PACKET_PAGE(sc, PKTPG_EISA_NUM);
   1103 
   1104 		/*
   1105 		 * As a last minute sanity check before actually using mapped
   1106 		 * memory we verify that we can read the isa number from the
   1107 		 * chip in memory mode.
   1108 		 */
   1109 		if (isaId != EISA_NUM_CRYSTAL) {
   1110 			aprint_error_dev(sc->sc_dev,
   1111 			    "failed to enable memory mode\n");
   1112 			sc->sc_memorymode = FALSE;
   1113 		} else {
   1114 			/*
   1115 			 * We are in memory mode so if we aren't using DMA,
   1116 			 * then program the chip to interrupt early.
   1117 			 */
   1118 			if ((sc->sc_cfgflags & CFGFLG_DMA_MODE) == 0) {
   1119 				CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG,
   1120 				    BUF_CFG_RX_DEST_IE |
   1121 				    BUF_CFG_RX_MISS_OVER_IE |
   1122 				    BUF_CFG_TX_COL_OVER_IE);
   1123 			}
   1124 		}
   1125 
   1126 	}
   1127 
   1128 	/* Put Ethernet address into the Individual Address register */
   1129 	for (i = 0; i < 6; i += 2) {
   1130 		v = sc->sc_enaddr[i + 0] | (sc->sc_enaddr[i + 1]) << 8;
   1131 		CS_WRITE_PACKET_PAGE(sc, PKTPG_IND_ADDR + i, v);
   1132 	}
   1133 
   1134 	if (sc->sc_irq != -1) {
   1135 		/* Set the interrupt level in the chip */
   1136 		if (sc->sc_prodid == PROD_ID_CS8900) {
   1137 			if (sc->sc_irq == 5)
   1138 				CS_WRITE_PACKET_PAGE(sc, PKTPG_INT_NUM, 3);
   1139 			else
   1140 				CS_WRITE_PACKET_PAGE(sc, PKTPG_INT_NUM,
   1141 				    (sc->sc_irq) - 10);
   1142 		} else { /* CS8920 */
   1143 			CS_WRITE_PACKET_PAGE(sc, PKTPG_8920_INT_NUM,
   1144 			    sc->sc_irq);
   1145 		}
   1146 	}
   1147 
   1148 	/* Write the multicast mask to the address filter register */
   1149 	cs_set_ladr_filt(sc, &sc->sc_ethercom);
   1150 
   1151 	/* Enable reception and transmission of frames */
   1152 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL,
   1153 	    CS_READ_PACKET_PAGE(sc, PKTPG_LINE_CTL) |
   1154 	    LINE_CTL_RX_ON | LINE_CTL_TX_ON);
   1155 
   1156 	/* Enable interrupt at the chip */
   1157 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
   1158 	    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) | BUS_CTL_INT_ENBL);
   1159 }
   1160 
   1161 int
   1162 cs_init(struct ifnet *ifp)
   1163 {
   1164 	int intState;
   1165 	int error = CS_OK;
   1166 	struct cs_softc *sc = ifp->if_softc;
   1167 
   1168 	if (cs_enable(sc))
   1169 		goto out;
   1170 
   1171 	cs_stop(ifp, 0);
   1172 
   1173 	intState = splnet();
   1174 
   1175 #if 0
   1176 	/* Mark the interface as down */
   1177 	sc->sc_ethercom.ec_if.if_flags &= ~(IFF_UP | IFF_RUNNING);
   1178 #endif
   1179 
   1180 #ifdef CS_DEBUG
   1181 	/* Enable debugging */
   1182 	sc->sc_ethercom.ec_if.if_flags |= IFF_DEBUG;
   1183 #endif
   1184 
   1185 	/* Reset the chip */
   1186 	if ((error = cs_reset_chip(sc)) == CS_OK) {
   1187 		/* Initialize the chip */
   1188 		cs_initChip(sc);
   1189 
   1190 		/* Mark the interface as running */
   1191 		sc->sc_ethercom.ec_if.if_flags |= IFF_RUNNING;
   1192 		sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1193 		sc->sc_ethercom.ec_if.if_timer = 0;
   1194 
   1195 		/* Assume we have carrier until we are told otherwise. */
   1196 		sc->sc_carrier = 1;
   1197 	} else
   1198 		aprint_error_dev(sc->sc_dev, "unable to reset chip\n");
   1199 
   1200 	splx(intState);
   1201 out:
   1202 	if (error == CS_OK)
   1203 		return 0;
   1204 	return EIO;
   1205 }
   1206 
   1207 void
   1208 cs_set_ladr_filt(struct cs_softc *sc, struct ethercom *ec)
   1209 {
   1210 	struct ifnet *ifp = &ec->ec_if;
   1211 	struct ether_multi *enm;
   1212 	struct ether_multistep step;
   1213 	uint16_t af[4];
   1214 	uint16_t port, mask, index;
   1215 
   1216 	/*
   1217 	 * Set up multicast address filter by passing all multicast addresses
   1218 	 * through a crc generator, and then using the high order 6 bits as an
   1219 	 * index into the 64 bit logical address filter.  The high order bit
   1220 	 * selects the word, while the rest of the bits select the bit within
   1221 	 * the word.
   1222 	 */
   1223 	if (ifp->if_flags & IFF_PROMISC) {
   1224 		/* Accept all valid frames. */
   1225 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CTL,
   1226 		    RX_CTL_PROMISC_A | RX_CTL_RX_OK_A |
   1227 		    RX_CTL_IND_A | RX_CTL_BCAST_A | RX_CTL_MCAST_A);
   1228 		ifp->if_flags |= IFF_ALLMULTI;
   1229 		return;
   1230 	}
   1231 
   1232 	/*
   1233 	 * Accept frames if a. crc valid, b. individual address match c.
   1234 	 * broadcast address,and d. multicast addresses matched in the hash
   1235 	 * filter
   1236 	 */
   1237 	CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CTL,
   1238 	    RX_CTL_RX_OK_A | RX_CTL_IND_A | RX_CTL_BCAST_A | RX_CTL_MCAST_A);
   1239 
   1240 
   1241 	/*
   1242 	 * Start off with all multicast flag clear, set it if we need to
   1243 	 * later, otherwise we will leave it.
   1244 	 */
   1245 	ifp->if_flags &= ~IFF_ALLMULTI;
   1246 	af[0] = af[1] = af[2] = af[3] = 0x0000;
   1247 
   1248 	/*
   1249 	 * Loop through all the multicast addresses unless we get a range of
   1250 	 * addresses, in which case we will just accept all packets.
   1251 	 * Justification for this is given in the next comment.
   1252 	 */
   1253 	ETHER_LOCK(ec);
   1254 	ETHER_FIRST_MULTI(step, ec, enm);
   1255 	while (enm != NULL) {
   1256 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1257 		    sizeof enm->enm_addrlo)) {
   1258 			/*
   1259 			 * We must listen to a range of multicast addresses.
   1260 			 * For now, just accept all multicasts, rather than
   1261 			 * trying to set only those filter bits needed to match
   1262 			 * the range.  (At this time, the only use of address
   1263 			 * ranges is for IP multicast routing, for which the
   1264 			 * range is big enough to require all bits set.)
   1265 			 */
   1266 			ifp->if_flags |= IFF_ALLMULTI;
   1267 			af[0] = af[1] = af[2] = af[3] = 0xffff;
   1268 			break;
   1269 		} else {
   1270 			/*
   1271 			 * We have got an individual address so just set that
   1272 			 * bit.
   1273 			 */
   1274 			index = cs_hash_index(enm->enm_addrlo);
   1275 
   1276 			/* Set the bit the Logical address filter. */
   1277 			port = (uint16_t) (index >> 4);
   1278 			mask = (uint16_t) (1 << (index & 0xf));
   1279 			af[port] |= mask;
   1280 
   1281 			ETHER_NEXT_MULTI(step, enm);
   1282 		}
   1283 	}
   1284 	ETHER_UNLOCK(ec);
   1285 
   1286 	/* Now program the chip with the addresses */
   1287 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 0, af[0]);
   1288 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 2, af[1]);
   1289 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 4, af[2]);
   1290 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 6, af[3]);
   1291 	return;
   1292 }
   1293 
   1294 uint16_t
   1295 cs_hash_index(char *addr)
   1296 {
   1297 	uint32_t crc;
   1298 	uint16_t hash_code;
   1299 
   1300 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
   1301 
   1302 	hash_code = crc >> 26;
   1303 	return hash_code;
   1304 }
   1305 
   1306 void
   1307 cs_reset(struct cs_softc *sc)
   1308 {
   1309 
   1310 	/* Mark the interface as down */
   1311 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
   1312 
   1313 	/* Reset the chip */
   1314 	cs_reset_chip(sc);
   1315 }
   1316 
   1317 int
   1318 cs_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1319 {
   1320 	struct cs_softc *sc = ifp->if_softc;
   1321 	struct ifreq *ifr = data;
   1322 	int state;
   1323 	int result;
   1324 
   1325 	state = splnet();
   1326 
   1327 	result = 0;		/* Only set if something goes wrong */
   1328 
   1329 	switch (cmd) {
   1330 	case SIOCGIFMEDIA:
   1331 	case SIOCSIFMEDIA:
   1332 		result = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1333 		break;
   1334 
   1335 	default:
   1336 		result = ether_ioctl(ifp, cmd, data);
   1337 		if (result == ENETRESET) {
   1338 			if (ifp->if_flags & IFF_RUNNING) {
   1339 				/*
   1340 				 * Multicast list has changed.  Set the
   1341 				 * hardware filter accordingly.
   1342 				 */
   1343 				cs_set_ladr_filt(sc, &sc->sc_ethercom);
   1344 			}
   1345 			result = 0;
   1346 		}
   1347 		break;
   1348 	}
   1349 
   1350 	splx(state);
   1351 
   1352 	return result;
   1353 }
   1354 
   1355 int
   1356 cs_mediachange(struct ifnet *ifp)
   1357 {
   1358 
   1359 	/*
   1360 	 * Current media is already set up.  Just reset the interface
   1361 	 * to let the new value take hold.
   1362 	 */
   1363 	cs_init(ifp);
   1364 	return 0;
   1365 }
   1366 
   1367 void
   1368 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1369 {
   1370 	struct cs_softc *sc = ifp->if_softc;
   1371 
   1372 	/* The currently selected media is always the active media. */
   1373 	ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
   1374 
   1375 	if (ifp->if_flags & IFF_UP) {
   1376 		/* Interface up, status is valid. */
   1377 		ifmr->ifm_status = IFM_AVALID |
   1378 		    (sc->sc_carrier ? IFM_ACTIVE : 0);
   1379 	}
   1380 		else ifmr->ifm_status = 0;
   1381 }
   1382 
   1383 int
   1384 cs_intr(void *arg)
   1385 {
   1386 	struct cs_softc *sc = arg;
   1387 	uint16_t Event;
   1388 	uint16_t rndEvent;
   1389 
   1390 /*printf("cs_intr %p\n", sc);*/
   1391 	/* Ignore any interrupts that happen while the chip is being reset */
   1392 	if (sc->sc_resetting) {
   1393 		printf("%s: cs_intr: reset in progress\n",
   1394 		    device_xname(sc->sc_dev));
   1395 		return 1;
   1396 	}
   1397 
   1398 	/* Read an event from the Interrupt Status Queue */
   1399 	if (sc->sc_memorymode)
   1400 		Event = CS_READ_PACKET_PAGE(sc, PKTPG_ISQ);
   1401 	else
   1402 		Event = CS_READ_PORT(sc, PORT_ISQ);
   1403 
   1404 	if ((Event & REG_NUM_MASK) == 0 || Event == 0xffff)
   1405 		return 0;	/* Not ours */
   1406 
   1407 	rndEvent = Event;
   1408 
   1409 	/* Process all the events in the Interrupt Status Queue */
   1410 	while ((Event & REG_NUM_MASK) != 0 && Event != 0xffff) {
   1411 		/* Dispatch to an event handler based on the register number */
   1412 		switch (Event & REG_NUM_MASK) {
   1413 		case REG_NUM_RX_EVENT:
   1414 			cs_receive_event(sc, Event);
   1415 			break;
   1416 		case REG_NUM_TX_EVENT:
   1417 			cs_transmit_event(sc, Event);
   1418 			break;
   1419 		case REG_NUM_BUF_EVENT:
   1420 			cs_buffer_event(sc, Event);
   1421 			break;
   1422 		case REG_NUM_TX_COL:
   1423 		case REG_NUM_RX_MISS:
   1424 			cs_counter_event(sc, Event);
   1425 			break;
   1426 		default:
   1427 			printf("%s: unknown interrupt event 0x%x\n",
   1428 			    device_xname(sc->sc_dev), Event);
   1429 			break;
   1430 		}
   1431 
   1432 		/* Read another event from the Interrupt Status Queue */
   1433 		if (sc->sc_memorymode)
   1434 			Event = CS_READ_PACKET_PAGE(sc, PKTPG_ISQ);
   1435 		else
   1436 			Event = CS_READ_PORT(sc, PORT_ISQ);
   1437 	}
   1438 
   1439 	/* have handled the interrupt */
   1440 	rnd_add_uint32(&sc->rnd_source, rndEvent);
   1441 	return 1;
   1442 }
   1443 
   1444 void
   1445 cs_counter_event(struct cs_softc *sc, uint16_t cntEvent)
   1446 {
   1447 	struct ifnet *ifp;
   1448 	uint16_t errorCount;
   1449 
   1450 	ifp = &sc->sc_ethercom.ec_if;
   1451 
   1452 	switch (cntEvent & REG_NUM_MASK) {
   1453 	case REG_NUM_TX_COL:
   1454 		/* The count should be read before an overflow occurs. */
   1455 		errorCount = CS_READ_PACKET_PAGE(sc, PKTPG_TX_COL);
   1456 		/*
   1457 		 * The tramsit event routine always checks the number of
   1458 		 * collisions for any packet so we don't increment any
   1459 		 * counters here, as they should already have been
   1460 		 * considered.
   1461 		 */
   1462 		break;
   1463 	case REG_NUM_RX_MISS:
   1464 		/* The count should be read before an overflow occurs. */
   1465 		errorCount = CS_READ_PACKET_PAGE(sc, PKTPG_RX_MISS);
   1466 		/*
   1467 		 * Increment the input error count, the first 6bits are the
   1468 		 * register id.
   1469 		 */
   1470 		ifp->if_ierrors += ((errorCount & 0xffC0) >> 6);
   1471 		break;
   1472 	default:
   1473 		/* Do nothing */
   1474 		break;
   1475 	}
   1476 }
   1477 
   1478 void
   1479 cs_buffer_event(struct cs_softc *sc, uint16_t bufEvent)
   1480 {
   1481 
   1482 	/*
   1483 	 * Multiple events can be in the buffer event register at one time so
   1484 	 * a standard switch statement will not suffice, here every event
   1485 	 * must be checked.
   1486 	 */
   1487 
   1488 	/*
   1489 	 * If 128 bits have been rxed by the time we get here, the dest event
   1490 	 * will be cleared and 128 event will be set.
   1491 	 */
   1492 	if ((bufEvent & (BUF_EVENT_RX_DEST | BUF_EVENT_RX_128)) != 0)
   1493 		cs_process_rx_early(sc);
   1494 
   1495 	if (bufEvent & BUF_EVENT_RX_DMA) {
   1496 		/* Process the receive data */
   1497 		if (sc->sc_dma_process_rx)
   1498 			(*sc->sc_dma_process_rx)(sc);
   1499 		else
   1500 			/* Should panic? */
   1501 			aprint_error_dev(sc->sc_dev, "unexpected DMA event\n");
   1502 	}
   1503 
   1504 	if (bufEvent & BUF_EVENT_TX_UNDR) {
   1505 #if 0
   1506 		/*
   1507 		 * This can happen occasionally, and it's not worth worrying
   1508 		 * about.
   1509 		 */
   1510 		printf("%s: transmit underrun (%d -> %d)\n",
   1511 		    device_xname(sc->sc_dev), sc->sc_xe_ent,
   1512 		    cs_xmit_early_table[sc->sc_xe_ent].worse);
   1513 #endif
   1514 		sc->sc_xe_ent = cs_xmit_early_table[sc->sc_xe_ent].worse;
   1515 		sc->sc_xe_togo =
   1516 		    cs_xmit_early_table[sc->sc_xe_ent].better_count;
   1517 
   1518 		/* had an underrun, transmit is finished */
   1519 		sc->sc_txbusy = FALSE;
   1520 	}
   1521 
   1522 	if (bufEvent & BUF_EVENT_SW_INT)
   1523 		printf("%s: software initiated interrupt\n",
   1524 		    device_xname(sc->sc_dev));
   1525 }
   1526 
   1527 void
   1528 cs_transmit_event(struct cs_softc *sc, uint16_t txEvent)
   1529 {
   1530 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1531 
   1532 	/* If there were any errors transmitting this frame */
   1533 	if (txEvent & (TX_EVENT_LOSS_CRS | TX_EVENT_SQE_ERR |
   1534 	    TX_EVENT_OUT_WIN | TX_EVENT_JABBER | TX_EVENT_16_COLL)) {
   1535 		/* Increment the output error count */
   1536 		ifp->if_oerrors++;
   1537 
   1538 		/* Note carrier loss. */
   1539 		if (txEvent & TX_EVENT_LOSS_CRS)
   1540 			sc->sc_carrier = 0;
   1541 
   1542 		/* If debugging is enabled then log error messages */
   1543 		if (ifp->if_flags & IFF_DEBUG) {
   1544 			if (txEvent & TX_EVENT_LOSS_CRS)
   1545 				aprint_error_dev(sc->sc_dev, "lost carrier\n");
   1546 
   1547 			if (txEvent & TX_EVENT_SQE_ERR)
   1548 				aprint_error_dev(sc->sc_dev, "SQE error\n");
   1549 
   1550 			if (txEvent & TX_EVENT_OUT_WIN)
   1551 				aprint_error_dev(sc->sc_dev,
   1552 				    "out-of-window collision\n");
   1553 
   1554 			if (txEvent & TX_EVENT_JABBER)
   1555 				aprint_error_dev(sc->sc_dev, "jabber\n");
   1556 
   1557 			if (txEvent & TX_EVENT_16_COLL)
   1558 				aprint_error_dev(sc->sc_dev,
   1559 				    "16 collisions\n");
   1560 		}
   1561 	} else {
   1562 		/* Transmission successful, carrier is up. */
   1563 		sc->sc_carrier = 1;
   1564 #ifdef SHARK
   1565 		ledNetActive();
   1566 #endif
   1567 	}
   1568 
   1569 	/* Add the number of collisions for this frame */
   1570 	if (txEvent & TX_EVENT_16_COLL)
   1571 		ifp->if_collisions += 16;
   1572 	else
   1573 		ifp->if_collisions += ((txEvent & TX_EVENT_COLL_MASK) >> 11);
   1574 
   1575 	ifp->if_opackets++;
   1576 
   1577 	/* Transmission is no longer in progress */
   1578 	sc->sc_txbusy = FALSE;
   1579 
   1580 	/* If there is more to transmit, start the next transmission */
   1581 	if_schedule_deferred_start(ifp);
   1582 }
   1583 
   1584 void
   1585 cs_print_rx_errors(struct cs_softc *sc, uint16_t rxEvent)
   1586 {
   1587 
   1588 	if (rxEvent & RX_EVENT_RUNT)
   1589 		aprint_error_dev(sc->sc_dev, "runt\n");
   1590 
   1591 	if (rxEvent & RX_EVENT_X_DATA)
   1592 		aprint_error_dev(sc->sc_dev, "extra data\n");
   1593 
   1594 	if (rxEvent & RX_EVENT_CRC_ERR) {
   1595 		if (rxEvent & RX_EVENT_DRIBBLE)
   1596 			aprint_error_dev(sc->sc_dev, "alignment error\n");
   1597 		else
   1598 			aprint_error_dev(sc->sc_dev, "CRC error\n");
   1599 	} else {
   1600 		if (rxEvent & RX_EVENT_DRIBBLE)
   1601 			aprint_error_dev(sc->sc_dev, "dribble bits\n");
   1602 	}
   1603 }
   1604 
   1605 void
   1606 cs_receive_event(struct cs_softc *sc, uint16_t rxEvent)
   1607 {
   1608 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1609 
   1610 	/* If the frame was not received OK */
   1611 	if (!(rxEvent & RX_EVENT_RX_OK)) {
   1612 		/* Increment the input error count */
   1613 		ifp->if_ierrors++;
   1614 
   1615 		/* If debugging is enabled then log error messages. */
   1616 		if (ifp->if_flags & IFF_DEBUG) {
   1617 			if (rxEvent != REG_NUM_RX_EVENT) {
   1618 				cs_print_rx_errors(sc, rxEvent);
   1619 
   1620 				/*
   1621 				 * Must read the length of all received
   1622 				 * frames
   1623 				 */
   1624 				CS_READ_PACKET_PAGE(sc, PKTPG_RX_LENGTH);
   1625 
   1626 				/* Skip the received frame */
   1627 				CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1628 					CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) |
   1629 						  RX_CFG_SKIP);
   1630 			} else
   1631 				aprint_error_dev(sc->sc_dev, "implied skip\n");
   1632 		}
   1633 	} else {
   1634 		/*
   1635 		 * Process the received frame and pass it up to the upper
   1636 		 * layers.
   1637 		 */
   1638 		cs_process_receive(sc);
   1639 	}
   1640 }
   1641 
   1642 void
   1643 cs_ether_input(struct cs_softc *sc, struct mbuf *m)
   1644 {
   1645 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1646 
   1647 	/* Pass the packet up. */
   1648 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1649 }
   1650 
   1651 void
   1652 cs_process_receive(struct cs_softc *sc)
   1653 {
   1654 	struct ifnet *ifp;
   1655 	struct mbuf *m;
   1656 	int totlen;
   1657 	uint16_t *pBuff, *pBuffLimit;
   1658 	int pad;
   1659 	unsigned int frameOffset = 0;	/* XXX: gcc */
   1660 
   1661 #ifdef SHARK
   1662 	ledNetActive();
   1663 #endif
   1664 
   1665 	ifp = &sc->sc_ethercom.ec_if;
   1666 
   1667 	/* Received a packet; carrier is up. */
   1668 	sc->sc_carrier = 1;
   1669 
   1670 	if (sc->sc_memorymode) {
   1671 		/* Initialize the frame offset */
   1672 		frameOffset = PKTPG_RX_LENGTH;
   1673 
   1674 		/* Get the length of the received frame */
   1675 		totlen = CS_READ_PACKET_PAGE(sc, frameOffset);
   1676 		frameOffset += 2;
   1677 	} else {
   1678 		/* Drop status */
   1679 		CS_READ_PORT(sc, PORT_RXTX_DATA);
   1680 
   1681 		/* Get the length of the received frame */
   1682 		totlen = CS_READ_PORT(sc, PORT_RXTX_DATA);
   1683 	}
   1684 
   1685 	if (totlen > ETHER_MAX_LEN) {
   1686 		aprint_error_dev(sc->sc_dev, "invalid packet length %d\n",
   1687 		    totlen);
   1688 
   1689 		/* Skip the received frame */
   1690 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1691 			CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1692 		return;
   1693 	}
   1694 
   1695 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1696 	if (m == 0) {
   1697 		aprint_error_dev(sc->sc_dev,
   1698 		    "cs_process_receive: unable to allocate mbuf\n");
   1699 		ifp->if_ierrors++;
   1700 		/*
   1701 		 * Couldn't allocate an mbuf so things are not good, may as
   1702 		 * well drop the packet I think.
   1703 		 *
   1704 		 * have already read the length so we should be right to skip
   1705 		 * the packet.
   1706 		 */
   1707 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1708 		    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1709 		return;
   1710 	}
   1711 	m_set_rcvif(m, ifp);
   1712 	m->m_pkthdr.len = totlen;
   1713 
   1714 	/* Number of bytes to align ip header on word boundary for ipintr */
   1715 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
   1716 
   1717 	/*
   1718 	 * Alloc mbuf cluster if we need.
   1719 	 * We need 1 byte spare because following packet read loop can overrun.
   1720 	 */
   1721 	if (totlen + pad + 1 > MHLEN) {
   1722 		MCLGET(m, M_DONTWAIT);
   1723 		if ((m->m_flags & M_EXT) == 0) {
   1724 			/* Couldn't allocate an mbuf cluster */
   1725 			aprint_error_dev(sc->sc_dev,
   1726 			    "cs_process_receive: "
   1727 			    "unable to allocate a cluster\n");
   1728 			m_freem(m);
   1729 
   1730 			/* Skip the received frame */
   1731 			CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1732 			    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG)
   1733 			    | RX_CFG_SKIP);
   1734 			return;
   1735 		}
   1736 	}
   1737 
   1738 	/* Align ip header on word boundary for ipintr */
   1739 	m->m_data += pad;
   1740 
   1741 	m->m_len = totlen;
   1742 	pBuff = mtod(m, uint16_t *);
   1743 
   1744 	/* Now read the data from the chip */
   1745 	if (sc->sc_memorymode) {
   1746 		/* Don't want to go over */
   1747 		pBuffLimit = pBuff + (totlen + 1) / 2;
   1748 
   1749 		while (pBuff < pBuffLimit) {
   1750 			*pBuff++ = CS_READ_PACKET_PAGE(sc, frameOffset);
   1751 			frameOffset += 2;
   1752 		}
   1753 	} else
   1754 		IO_READ_MULTI_2(sc, PORT_RXTX_DATA, pBuff, (totlen + 1)>>1);
   1755 
   1756 	cs_ether_input(sc, m);
   1757 }
   1758 
   1759 void
   1760 cs_process_rx_early(struct cs_softc *sc)
   1761 {
   1762 	struct ifnet *ifp;
   1763 	struct mbuf *m;
   1764 	uint16_t frameCount, oldFrameCount;
   1765 	uint16_t rxEvent;
   1766 	uint16_t *pBuff;
   1767 	int pad;
   1768 	unsigned int frameOffset;
   1769 
   1770 
   1771 	ifp = &sc->sc_ethercom.ec_if;
   1772 
   1773 	/* Initialize the frame offset */
   1774 	frameOffset = PKTPG_RX_FRAME;
   1775 	frameCount = 0;
   1776 
   1777 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1778 	if (m == 0) {
   1779 		aprint_error_dev(sc->sc_dev,
   1780 		    "cs_process_rx_early: unable to allocate mbuf\n");
   1781 		ifp->if_ierrors++;
   1782 		/*
   1783 		 * Couldn't allocate an mbuf so things are not good, may as
   1784 		 * well drop the packet I think.
   1785 		 *
   1786 		 * have already read the length so we should be right to skip
   1787 		 * the packet.
   1788 		 */
   1789 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1790 		    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1791 		return;
   1792 	}
   1793 	m_set_rcvif(m, ifp);
   1794 	/*
   1795 	 * Save processing by always using a mbuf cluster, guaranteed to fit
   1796 	 * packet
   1797 	 */
   1798 	MCLGET(m, M_DONTWAIT);
   1799 	if ((m->m_flags & M_EXT) == 0) {
   1800 		/* Couldn't allocate an mbuf cluster */
   1801 		aprint_error_dev(sc->sc_dev,
   1802 		    "cs_process_rx_early: unable to allocate a cluster\n");
   1803 		m_freem(m);
   1804 		/* Skip the frame */
   1805 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1806 		    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1807 		return;
   1808 	}
   1809 
   1810 	/* Align ip header on word boundary for ipintr */
   1811 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
   1812 	m->m_data += pad;
   1813 
   1814 	/* Set up the buffer pointer to point to the data area */
   1815 	pBuff = mtod(m, uint16_t *);
   1816 
   1817 	/*
   1818 	 * Now read the frame byte counter until we have finished reading the
   1819 	 * frame
   1820 	 */
   1821 	oldFrameCount = 0;
   1822 	frameCount = CS_READ_PACKET_PAGE(sc, PKTPG_FRAME_BYTE_COUNT);
   1823 	while ((frameCount != 0) && (frameCount < MCLBYTES)) {
   1824 		for (; oldFrameCount < frameCount; oldFrameCount += 2) {
   1825 			*pBuff++ = CS_READ_PACKET_PAGE(sc, frameOffset);
   1826 			frameOffset += 2;
   1827 		}
   1828 
   1829 		/* Read the new count from the chip */
   1830 		frameCount = CS_READ_PACKET_PAGE(sc, PKTPG_FRAME_BYTE_COUNT);
   1831 	}
   1832 
   1833 	/* Update the mbuf counts */
   1834 	m->m_len = oldFrameCount;
   1835 	m->m_pkthdr.len = oldFrameCount;
   1836 
   1837 	/* Now check the Rx Event register */
   1838 	rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_EVENT);
   1839 
   1840 	if ((rxEvent & RX_EVENT_RX_OK) != 0) {
   1841 		/*
   1842 		 * Do an implied skip, it seems to be more reliable than a
   1843 		 * forced skip.
   1844 		 */
   1845 		rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_STATUS);
   1846 		rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_LENGTH);
   1847 
   1848 		/*
   1849 		 * Now read the RX_EVENT register to perform an implied skip.
   1850 		 */
   1851 		rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_EVENT);
   1852 
   1853 		cs_ether_input(sc, m);
   1854 	} else {
   1855 		m_freem(m);
   1856 		ifp->if_ierrors++;
   1857 	}
   1858 }
   1859 
   1860 void
   1861 cs_start_output(struct ifnet *ifp)
   1862 {
   1863 	struct cs_softc *sc;
   1864 	struct mbuf *pMbuf;
   1865 	struct mbuf *pMbufChain;
   1866 	uint16_t BusStatus;
   1867 	uint16_t Length;
   1868 	int txLoop = 0;
   1869 	int dropout = 0;
   1870 
   1871 	sc = ifp->if_softc;
   1872 
   1873 	/* Check that the interface is up and running */
   1874 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1875 		return;
   1876 
   1877 	/* Don't interrupt a transmission in progress */
   1878 	if (sc->sc_txbusy)
   1879 		return;
   1880 
   1881 	/* This loop will only run through once if transmission is successful */
   1882 	/*
   1883 	 * While there are packets to transmit and a transmit is not in
   1884 	 * progress
   1885 	 */
   1886 	while (sc->sc_txbusy == 0 && dropout == 0) {
   1887 		IFQ_DEQUEUE(&ifp->if_snd, pMbufChain);
   1888 		if (pMbufChain == NULL)
   1889 			break;
   1890 
   1891 		/*
   1892 		 * If BPF is listening on this interface, let it see the packet
   1893 		 * before we commit it to the wire.
   1894 		 */
   1895 		bpf_mtap(ifp, pMbufChain, BPF_D_OUT);
   1896 
   1897 		/* Find the total length of the data to transmit */
   1898 		Length = 0;
   1899 		for (pMbuf = pMbufChain; pMbuf != NULL; pMbuf = pMbuf->m_next)
   1900 			Length += pMbuf->m_len;
   1901 
   1902 		do {
   1903 			/*
   1904 			 * Request that the transmit be started after all
   1905 			 * data has been copied
   1906 			 *
   1907 			 * In IO mode must write to the IO port not the packet
   1908 			 * page address
   1909 			 *
   1910 			 * If this is changed to start transmission after a
   1911 			 * small amount of data has been copied you tend to
   1912 			 * get packet missed errors i think because the ISA
   1913 			 * bus is too slow. Or possibly the copy routine is
   1914 			 * not streamlined enough.
   1915 			 */
   1916 			if (sc->sc_memorymode) {
   1917 				CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CMD,
   1918 				    cs_xmit_early_table[sc->sc_xe_ent].txcmd);
   1919 				CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_LENGTH, Length);
   1920 			} else {
   1921 				CS_WRITE_PORT(sc, PORT_TX_CMD,
   1922 				    cs_xmit_early_table[sc->sc_xe_ent].txcmd);
   1923 				CS_WRITE_PORT(sc, PORT_TX_LENGTH, Length);
   1924 			}
   1925 
   1926 			/* Adjust early-transmit machinery. */
   1927 			if (--sc->sc_xe_togo == 0) {
   1928 				sc->sc_xe_ent =
   1929 				    cs_xmit_early_table[sc->sc_xe_ent].better;
   1930 				sc->sc_xe_togo =
   1931 			    cs_xmit_early_table[sc->sc_xe_ent].better_count;
   1932 			}
   1933 			/*
   1934 			 * Read the BusStatus register which indicates
   1935 			 * success of the request
   1936 			 */
   1937 			BusStatus = CS_READ_PACKET_PAGE(sc, PKTPG_BUS_ST);
   1938 
   1939 			/*
   1940 			 * If there was an error in the transmit bid free the
   1941 			 * mbuf and go on. This is presuming that mbuf is
   1942 			 * corrupt.
   1943 			 */
   1944 			if (BusStatus & BUS_ST_TX_BID_ERR) {
   1945 				aprint_error_dev(sc->sc_dev,
   1946 				    "transmit bid error (too big)");
   1947 
   1948 				/* Discard the bad mbuf chain */
   1949 				m_freem(pMbufChain);
   1950 				sc->sc_ethercom.ec_if.if_oerrors++;
   1951 
   1952 				/* Loop up to transmit the next chain */
   1953 				txLoop = 0;
   1954 			} else {
   1955 				if (BusStatus & BUS_ST_RDY4TXNOW) {
   1956 					/*
   1957 					 * The chip is ready for transmission
   1958 					 * now
   1959 					 */
   1960 					/*
   1961 					 * Copy the frame to the chip to
   1962 					 * start transmission
   1963 					 */
   1964 					cs_copy_tx_frame(sc, pMbufChain);
   1965 
   1966 					/* Free the mbuf chain */
   1967 					m_freem(pMbufChain);
   1968 
   1969 					/* Transmission is now in progress */
   1970 					sc->sc_txbusy = TRUE;
   1971 					txLoop = 0;
   1972 				} else {
   1973 					/*
   1974 					 * If we get here we want to try
   1975 					 * again with the same mbuf, until
   1976 					 * the chip lets us transmit.
   1977 					 */
   1978 					txLoop++;
   1979 					if (txLoop > CS_OUTPUT_LOOP_MAX) {
   1980 						/* Free the mbuf chain */
   1981 						m_freem(pMbufChain);
   1982 						/*
   1983 						 * Transmission is not in
   1984 						 * progress
   1985 						 */
   1986 						sc->sc_txbusy = FALSE;
   1987 						/*
   1988 						 * Increment the output error
   1989 						 * count
   1990 						 */
   1991 						ifp->if_oerrors++;
   1992 						/*
   1993 						 * exit the routine and drop
   1994 						 * the packet.
   1995 						 */
   1996 						txLoop = 0;
   1997 						dropout = 1;
   1998 					}
   1999 				}
   2000 			}
   2001 		} while (txLoop);
   2002 	}
   2003 }
   2004 
   2005 void
   2006 cs_copy_tx_frame(struct cs_softc *sc, struct mbuf *m0)
   2007 {
   2008 	struct mbuf *m;
   2009 	int len, leftover, frameoff;
   2010 	uint16_t dbuf;
   2011 	uint8_t *p;
   2012 #ifdef DIAGNOSTIC
   2013 	uint8_t *lim;
   2014 #endif
   2015 
   2016 	/* Initialize frame pointer and data port address */
   2017 	frameoff = PKTPG_TX_FRAME;
   2018 
   2019 	/* Start out with no leftover data */
   2020 	leftover = 0;
   2021 	dbuf = 0;
   2022 
   2023 	/* Process the chain of mbufs */
   2024 	for (m = m0; m != NULL; m = m->m_next) {
   2025 		/* Process all of the data in a single mbuf. */
   2026 		p = mtod(m, uint8_t *);
   2027 		len = m->m_len;
   2028 #ifdef DIAGNOSTIC
   2029 		lim = p + len;
   2030 #endif
   2031 
   2032 		while (len > 0) {
   2033 			if (leftover) {
   2034 				/*
   2035 				 * Data left over (from mbuf or realignment).
   2036 				 * Buffer the next byte, and write it and
   2037 				 * the leftover data out.
   2038 				 */
   2039 				dbuf |= *p++ << 8;
   2040 				len--;
   2041 				if (sc->sc_memorymode) {
   2042 					CS_WRITE_PACKET_PAGE(sc, frameoff, dbuf);
   2043 					frameoff += 2;
   2044 				}
   2045 				else {
   2046 					CS_WRITE_PORT(sc, PORT_RXTX_DATA, dbuf);
   2047 				}
   2048 				leftover = 0;
   2049 			} else if ((long) p & 1) {
   2050 				/* Misaligned data.  Buffer the next byte. */
   2051 				dbuf = *p++;
   2052 				len--;
   2053 				leftover = 1;
   2054 			} else {
   2055 				/*
   2056 				 * Aligned data.  This is the case we like.
   2057 				 *
   2058 				 * Write-region out as much as we can, then
   2059 				 * buffer the remaining byte (if any).
   2060 				 */
   2061 				leftover = len & 1;
   2062 				len &= ~1;
   2063 				if (sc->sc_memorymode) {
   2064 					MEM_WRITE_REGION_2(sc, frameoff,
   2065 					    (uint16_t *) p, len >> 1);
   2066 					frameoff += len;
   2067 				} else
   2068 					IO_WRITE_MULTI_2(sc, PORT_RXTX_DATA,
   2069 					    (uint16_t *)p, len >> 1);
   2070 				p += len;
   2071 
   2072 				if (leftover)
   2073 					dbuf = *p++;
   2074 				len = 0;
   2075 			}
   2076 		}
   2077 		if (len < 0)
   2078 			panic("cs_copy_tx_frame: negative len");
   2079 #ifdef DIAGNOSTIC
   2080 		if (p != lim)
   2081 			panic("cs_copy_tx_frame: p != lim");
   2082 #endif
   2083 	}
   2084 	if (leftover) {
   2085 		if (sc->sc_memorymode)
   2086 			CS_WRITE_PACKET_PAGE(sc, frameoff, dbuf);
   2087 		else
   2088 			CS_WRITE_PORT(sc, PORT_RXTX_DATA, dbuf);
   2089 	}
   2090 }
   2091 
   2092 static int
   2093 cs_enable(struct cs_softc *sc)
   2094 {
   2095 
   2096 	if (CS_IS_ENABLED(sc) == 0) {
   2097 		if (sc->sc_enable != NULL) {
   2098 			int error;
   2099 
   2100 			error = (*sc->sc_enable)(sc);
   2101 			if (error)
   2102 				return error;
   2103 		}
   2104 		sc->sc_cfgflags |= CFGFLG_ENABLED;
   2105 	}
   2106 
   2107 	return 0;
   2108 }
   2109 
   2110 static void
   2111 cs_disable(struct cs_softc *sc)
   2112 {
   2113 
   2114 	if (CS_IS_ENABLED(sc)) {
   2115 		if (sc->sc_disable != NULL)
   2116 			(*sc->sc_disable)(sc);
   2117 
   2118 		sc->sc_cfgflags &= ~CFGFLG_ENABLED;
   2119 	}
   2120 }
   2121 
   2122 static void
   2123 cs_stop(struct ifnet *ifp, int disable)
   2124 {
   2125 	struct cs_softc *sc = ifp->if_softc;
   2126 
   2127 	CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG, 0);
   2128 	CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CFG, 0);
   2129 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG, 0);
   2130 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL, 0);
   2131 
   2132 	if (disable)
   2133 		cs_disable(sc);
   2134 
   2135 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2136 }
   2137 
   2138 int
   2139 cs_activate(device_t self, enum devact act)
   2140 {
   2141 	struct cs_softc *sc = device_private(self);
   2142 
   2143 	switch (act) {
   2144 	case DVACT_DEACTIVATE:
   2145 		if_deactivate(&sc->sc_ethercom.ec_if);
   2146 		return 0;
   2147 	default:
   2148 		return EOPNOTSUPP;
   2149 	}
   2150 }
   2151