Home | History | Annotate | Line # | Download | only in ic
cs89x0.c revision 1.42
      1 /*	$NetBSD: cs89x0.c,v 1.42 2019/02/05 06:17:02 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 u_int8_t * to u_int16_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.42 2019/02/05 06:17:02 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 
    229 #include <sys/rndsource.h>
    230 
    231 #include <net/if.h>
    232 #include <net/if_ether.h>
    233 #include <net/if_media.h>
    234 #include <net/bpf.h>
    235 
    236 #ifdef INET
    237 #include <netinet/in.h>
    238 #include <netinet/if_inarp.h>
    239 #endif
    240 
    241 #include <sys/bus.h>
    242 #include <sys/intr.h>
    243 
    244 #include <dev/ic/cs89x0reg.h>
    245 #include <dev/ic/cs89x0var.h>
    246 
    247 #ifdef SHARK
    248 #include <shark/shark/sequoia.h>
    249 #endif
    250 
    251 /*
    252  * MACRO DEFINITIONS
    253  */
    254 #define CS_OUTPUT_LOOP_MAX 100	/* max times round notorious tx loop */
    255 
    256 /*
    257  * FUNCTION PROTOTYPES
    258  */
    259 static void	cs_get_default_media(struct cs_softc *);
    260 static int	cs_get_params(struct cs_softc *);
    261 static int	cs_get_enaddr(struct cs_softc *);
    262 static int	cs_reset_chip(struct cs_softc *);
    263 static void	cs_reset(struct cs_softc *);
    264 static int	cs_ioctl(struct ifnet *, u_long, void *);
    265 static void	cs_initChip(struct cs_softc *);
    266 static void	cs_buffer_event(struct cs_softc *, u_int16_t);
    267 static void	cs_transmit_event(struct cs_softc *, u_int16_t);
    268 static void	cs_receive_event(struct cs_softc *, u_int16_t);
    269 static void	cs_process_receive(struct cs_softc *);
    270 static void	cs_process_rx_early(struct cs_softc *);
    271 static void	cs_start_output(struct ifnet *);
    272 static void	cs_copy_tx_frame(struct cs_softc *, struct mbuf *);
    273 static void	cs_set_ladr_filt(struct cs_softc *, struct ethercom *);
    274 static u_int16_t cs_hash_index(char *);
    275 static void	cs_counter_event(struct cs_softc *, u_int16_t);
    276 
    277 static int	cs_mediachange(struct ifnet *);
    278 static void	cs_mediastatus(struct ifnet *, struct ifmediareq *);
    279 
    280 static bool cs_shutdown(device_t, int);
    281 static int cs_enable(struct cs_softc *);
    282 static void cs_disable(struct cs_softc *);
    283 static void cs_stop(struct ifnet *, int);
    284 static int cs_scan_eeprom(struct cs_softc *);
    285 static int cs_read_pktpg_from_eeprom(struct cs_softc *, int, u_int16_t *);
    286 
    287 
    288 /*
    289  * GLOBAL DECLARATIONS
    290  */
    291 
    292 /*
    293  * Xmit-early table.
    294  *
    295  * To get better performance, we tell the chip to start packet
    296  * transmission before the whole packet is copied to the chip.
    297  * However, this can fail under load.  When it fails, we back off
    298  * to a safer setting for a little while.
    299  *
    300  * txcmd is the value of txcmd used to indicate when to start transmission.
    301  * better is the next 'better' state in the table.
    302  * better_count is the number of output packets before transition to the
    303  *   better state.
    304  * worse is the next 'worse' state in the table.
    305  *
    306  * Transition to the next worse state happens automatically when a
    307  * transmittion underrun occurs.
    308  */
    309 struct cs_xmit_early {
    310 	u_int16_t       txcmd;
    311 	int             better;
    312 	int             better_count;
    313 	int             worse;
    314 } cs_xmit_early_table[3] = {
    315 	{ TX_CMD_START_381,	0,	INT_MAX,	1, },
    316 	{ TX_CMD_START_1021,	0,	50000,		2, },
    317 	{ TX_CMD_START_ALL,	1,	5000,		2, },
    318 };
    319 
    320 int cs_default_media[] = {
    321 	IFM_ETHER|IFM_10_2,
    322 	IFM_ETHER|IFM_10_5,
    323 	IFM_ETHER|IFM_10_T,
    324 	IFM_ETHER|IFM_10_T|IFM_FDX,
    325 };
    326 int cs_default_nmedia = sizeof(cs_default_media) / sizeof(cs_default_media[0]);
    327 
    328 int
    329 cs_attach(struct cs_softc *sc, u_int8_t *enaddr, int *media,
    330 	  int nmedia, int defmedia)
    331 {
    332 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    333 	const char *chipname, *medname;
    334 	u_int16_t reg;
    335 	int i;
    336 
    337 	/* Start out in IO mode */
    338 	sc->sc_memorymode = FALSE;
    339 
    340 	/* make sure we're right */
    341 	for (i = 0; i < 10000; i++) {
    342 		reg = CS_READ_PACKET_PAGE(sc, PKTPG_EISA_NUM);
    343 		if (reg == EISA_NUM_CRYSTAL) {
    344 			break;
    345 		}
    346 	}
    347 	if (i == 10000) {
    348 		aprint_error_dev(sc->sc_dev, "wrong id(0x%x)\n", reg);
    349 		return 1; /* XXX should panic? */
    350 	}
    351 
    352 	reg = CS_READ_PACKET_PAGE(sc, PKTPG_PRODUCT_ID);
    353 	sc->sc_prodid = reg & PROD_ID_MASK;
    354 	sc->sc_prodrev = (reg & PROD_REV_MASK) >> 8;
    355 
    356 	switch (sc->sc_prodid) {
    357 	case PROD_ID_CS8900:
    358 		chipname = "CS8900";
    359 		break;
    360 	case PROD_ID_CS8920:
    361 		chipname = "CS8920";
    362 		break;
    363 	case PROD_ID_CS8920M:
    364 		chipname = "CS8920M";
    365 		break;
    366 	default:
    367 		panic("cs_attach: impossible");
    368 	}
    369 
    370 	/*
    371 	 * the first thing to do is check that the mbuf cluster size is
    372 	 * greater than the MTU for an ethernet frame. The code depends on
    373 	 * this and to port this to a OS where this was not the case would
    374 	 * not be straightforward.
    375 	 *
    376 	 * we need 1 byte spare because our
    377 	 * packet read loop can overrun.
    378 	 * and we may need pad bytes to align ip header.
    379 	 */
    380 	if (MCLBYTES < ETHER_MAX_LEN + 1 +
    381 		ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header)) {
    382 		printf("%s: MCLBYTES too small for Ethernet frame\n",
    383 		    device_xname(sc->sc_dev));
    384 		return 1;
    385 	}
    386 
    387 	/* Start out not transmitting */
    388 	sc->sc_txbusy = FALSE;
    389 
    390 	/* Set up early transmit threshhold */
    391 	sc->sc_xe_ent = 0;
    392 	sc->sc_xe_togo = cs_xmit_early_table[sc->sc_xe_ent].better_count;
    393 
    394 	/* Initialize ifnet structure. */
    395 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    396 	ifp->if_softc = sc;
    397 	ifp->if_start = cs_start_output;
    398 	ifp->if_init = cs_init;
    399 	ifp->if_ioctl = cs_ioctl;
    400 	ifp->if_stop = cs_stop;
    401 	ifp->if_watchdog = NULL;	/* no watchdog at this stage */
    402 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    403 	IFQ_SET_READY(&ifp->if_snd);
    404 
    405 	/* Initialize ifmedia structures. */
    406 	ifmedia_init(&sc->sc_media, 0, cs_mediachange, cs_mediastatus);
    407 
    408 	if (media != NULL) {
    409 		for (i = 0; i < nmedia; i++)
    410 			ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    411 		ifmedia_set(&sc->sc_media, defmedia);
    412 	} else {
    413 		for (i = 0; i < cs_default_nmedia; i++)
    414 			ifmedia_add(&sc->sc_media, cs_default_media[i],
    415 			    0, NULL);
    416 		cs_get_default_media(sc);
    417 	}
    418 
    419 	if (sc->sc_cfgflags & CFGFLG_PARSE_EEPROM) {
    420 		if (cs_scan_eeprom(sc) == CS_ERROR) {
    421 			/* failed to scan the eeprom, pretend there isn't an eeprom */
    422 			aprint_error_dev(sc->sc_dev, "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 	/*
    524 	 * XXX not necessary
    525 	 */
    526 	if (sc->sc_cfgflags & CFGFLG_DMA_MODE) {
    527 		isa_dmamem_unmap(sc->sc_ic, sc->sc_drq, sc->sc_dmabase, sc->sc_dmasize);
    528 		isa_dmamem_free(sc->sc_ic, sc->sc_drq, sc->sc_dmaaddr, 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 	u_int16_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|IFM_FDX);
    584 		else
    585 			ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T);
    586 		break;
    587 	}
    588 	return;
    589 
    590  fakeit:
    591 	aprint_error_dev(sc->sc_dev,
    592 	    "WARNING: default media setting may be inaccurate\n");
    593 	/* XXX Arbitrary... */
    594 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T);
    595 }
    596 
    597 /*
    598  * cs_scan_eeprom
    599  *
    600  * Attempt to take a complete copy of the eeprom into main memory.
    601  * this will allow faster parsing of the eeprom data.
    602  *
    603  * Only tested against a 8920M's eeprom, but the data sheet for the
    604  * 8920A indicates that is uses the same layout.
    605  */
    606 int
    607 cs_scan_eeprom(struct cs_softc *sc)
    608 {
    609 	u_int16_t result;
    610 	int	i;
    611 	int	eeprom_size;
    612 	u_int8_t checksum = 0;
    613 
    614 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    615 		aprint_error_dev(sc->sc_dev,
    616 		    "cs_scan_params: EEPROM missing or bad\n");
    617 		return (CS_ERROR);
    618 	}
    619 
    620 	/*
    621 	 * read the 0th word from the eeprom, it will tell us the length
    622 	 * and if the eeprom is valid
    623 	 */
    624 	cs_read_eeprom(sc, 0, &result);
    625 
    626 	/* check the eeprom signature */
    627 	if ((result & 0xE000) != 0xA000) {
    628 		/* empty eeprom */
    629 		return (CS_ERROR);
    630 	}
    631 
    632 	/*
    633 	 * take the eeprom size (note the read value doesn't include the header
    634 	 * word)
    635 	 */
    636 	eeprom_size = (result & 0xff) + 2;
    637 
    638 	sc->eeprom_data = malloc(eeprom_size, M_DEVBUF, M_WAITOK);
    639 	if (sc->eeprom_data == NULL) {
    640 		/* no memory, treat this as if there's no eeprom */
    641 		return (CS_ERROR);
    642 	}
    643 
    644 	sc->eeprom_size = eeprom_size;
    645 
    646 	/* read the eeprom into the buffer, also calculate the checksum  */
    647 	for (i = 0; i < (eeprom_size >> 1); i++) {
    648 		cs_read_eeprom(sc, i, &(sc->eeprom_data[i]));
    649 		checksum += (sc->eeprom_data[i] & 0xff00) >> 8;
    650 		checksum += (sc->eeprom_data[i] & 0x00ff);
    651 	}
    652 
    653 	/*
    654 	 * validate checksum calculation, the sum of all the bytes should be 0,
    655 	 * as the high byte of the last word is the 2's complement of the
    656 	 * sum to that point.
    657 	 */
    658 	if (checksum != 0) {
    659 		aprint_error_dev(sc->sc_dev, "eeprom checksum failure\n");
    660 		return (CS_ERROR);
    661 	}
    662 
    663 	return (CS_OK);
    664 }
    665 
    666 static int
    667 cs_read_pktpg_from_eeprom(struct cs_softc *sc, int pktpg, u_int16_t *pValue)
    668 {
    669 	int x, maxword;
    670 
    671 	/* Check that we have eeprom data */
    672 	if ((sc->eeprom_data == NULL) || (sc->eeprom_size < 2))
    673 		return (CS_ERROR);
    674 
    675 	/*
    676 	 * We only want to read the data words, the last word contains the
    677 	 * checksum
    678 	 */
    679 	maxword = (sc->eeprom_size - 2) >> 1;
    680 
    681 	/* start 1 word in, as the first word is the length and signature */
    682 	x = 1;
    683 
    684 	while ( x < (maxword)) {
    685 		u_int16_t header;
    686 		int group_size;
    687 		int offset;
    688 		int offset_max;
    689 
    690 		/* read in the group header word */
    691 		header = sc->eeprom_data[x];
    692 		x++;	/* skip group header */
    693 
    694 		/*
    695 		 * size of group in words is in the top 4 bits, note that it
    696 		 * is one less than the number of words
    697 		 */
    698 		group_size = header & 0xF000;
    699 
    700 		/*
    701 		 * CS8900 Data sheet says this should be 0x01ff,
    702 		 * but my cs8920 eeprom has higher offsets,
    703 		 * perhaps the 8920 allows higher offsets, otherwise
    704 		 * it's writing to places that it shouldn't
    705 		 */
    706 		/* work out the offsets this group covers */
    707 		offset = header & 0x0FFF;
    708 		offset_max = offset + (group_size << 1);
    709 
    710 		/* check if the pkgpg we're after is in this group */
    711 		if ((offset <= pktpg) && (pktpg <= offset_max)) {
    712 			/* the pkgpg value we want is in here */
    713 			int eeprom_location;
    714 
    715 			eeprom_location = ((pktpg - offset) >> 1) ;
    716 
    717 			*pValue = sc->eeprom_data[x + eeprom_location];
    718 			return (CS_OK);
    719 		} else {
    720 			/* skip this group (+ 1 for first entry) */
    721 			x += group_size + 1;
    722 		}
    723 	}
    724 
    725 	/*
    726 	 * if we've fallen out here then we don't have a value in the EEPROM
    727 	 * for this pktpg so return an error
    728 	 */
    729 	return (CS_ERROR);
    730 }
    731 
    732 int
    733 cs_get_params(struct cs_softc *sc)
    734 {
    735 	u_int16_t isaConfig;
    736 	u_int16_t adapterConfig;
    737 
    738 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    739 		aprint_error_dev(sc->sc_dev,
    740 		    "cs_get_params: EEPROM missing or bad\n");
    741 		return (CS_ERROR);
    742 	}
    743 
    744 	if (sc->sc_cfgflags & CFGFLG_PARSE_EEPROM) {
    745 		/* Get ISA configuration from the EEPROM */
    746 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_BUS_CTL, &isaConfig)
    747 			       	== CS_ERROR) {
    748 			/* eeprom doesn't have this value, use data sheet default */
    749 			isaConfig = 0x0017;
    750 		}
    751 
    752 		/* Get adapter configuration from the EEPROM */
    753 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_SELF_CTL, &adapterConfig)
    754 				== CS_ERROR) {
    755 			/* eeprom doesn't have this value, use data sheet default */
    756 			adapterConfig = 0x0015;
    757 		}
    758 
    759 		/* Copy the USE_SA flag */
    760 		if (isaConfig & BUS_CTL_USE_SA)
    761 			sc->sc_cfgflags |= CFGFLG_USE_SA;
    762 
    763 		/* Copy the IO Channel Ready flag */
    764 		if (isaConfig & BUS_CTL_IOCHRDY)
    765 			sc->sc_cfgflags |= CFGFLG_IOCHRDY;
    766 
    767 		/* Copy the DC/DC Polarity flag */
    768 		if (adapterConfig & SELF_CTL_HCB1)
    769 			sc->sc_cfgflags |= CFGFLG_DCDC_POL;
    770 	} else {
    771 		/* Get ISA configuration from the EEPROM */
    772 		if (cs_read_eeprom(sc, EEPROM_ISA_CFG, &isaConfig) == CS_ERROR)
    773 			goto eeprom_bad;
    774 
    775 		/* Get adapter configuration from the EEPROM */
    776 		if (cs_read_eeprom(sc, EEPROM_ADPTR_CFG, &adapterConfig) == CS_ERROR)
    777 			goto eeprom_bad;
    778 
    779 		/* Copy the USE_SA flag */
    780 		if (isaConfig & ISA_CFG_USE_SA)
    781 			sc->sc_cfgflags |= CFGFLG_USE_SA;
    782 
    783 		/* Copy the IO Channel Ready flag */
    784 		if (isaConfig & ISA_CFG_IOCHRDY)
    785 			sc->sc_cfgflags |= CFGFLG_IOCHRDY;
    786 
    787 		/* Copy the DC/DC Polarity flag */
    788 		if (adapterConfig & ADPTR_CFG_DCDC_POL)
    789 			sc->sc_cfgflags |= CFGFLG_DCDC_POL;
    790 	}
    791 
    792 	return (CS_OK);
    793 eeprom_bad:
    794 	aprint_error_dev(sc->sc_dev,
    795 	    "cs_get_params: unable to read from EEPROM\n");
    796 	return (CS_ERROR);
    797 }
    798 
    799 int
    800 cs_get_enaddr(struct cs_softc *sc)
    801 {
    802 	uint16_t myea[ETHER_ADDR_LEN / sizeof(uint16_t)];
    803 	int i;
    804 
    805 	if (cs_verify_eeprom(sc) == CS_ERROR) {
    806 		aprint_error_dev(sc->sc_dev,
    807 		    "cs_get_enaddr: EEPROM missing or bad\n");
    808 		return (CS_ERROR);
    809 	}
    810 
    811 	/* Get Ethernet address from the EEPROM */
    812 	if (sc->sc_cfgflags & CFGFLG_PARSE_EEPROM) {
    813 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_IND_ADDR, &myea[0])
    814 				== CS_ERROR)
    815 			goto eeprom_bad;
    816 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_IND_ADDR + 2, &myea[1])
    817 				== CS_ERROR)
    818 			goto eeprom_bad;
    819 		if (cs_read_pktpg_from_eeprom(sc, PKTPG_IND_ADDR + 4, &myea[2])
    820 				== CS_ERROR)
    821 			goto eeprom_bad;
    822 	} else {
    823 		if (cs_read_eeprom(sc, EEPROM_IND_ADDR_H, &myea[0]) == CS_ERROR)
    824 			goto eeprom_bad;
    825 		if (cs_read_eeprom(sc, EEPROM_IND_ADDR_M, &myea[1]) == CS_ERROR)
    826 			goto eeprom_bad;
    827 		if (cs_read_eeprom(sc, EEPROM_IND_ADDR_L, &myea[2]) == CS_ERROR)
    828 			goto eeprom_bad;
    829 	}
    830 
    831 	for (i = 0; i < __arraycount(myea); i++) {
    832 		sc->sc_enaddr[i * 2 + 0] = myea[i];
    833 		sc->sc_enaddr[i * 2 + 1] = myea[i] >> 8;
    834 	}
    835 
    836 	return (CS_OK);
    837 
    838  eeprom_bad:
    839 	aprint_error_dev(sc->sc_dev,
    840 	    "cs_get_enaddr: unable to read from EEPROM\n");
    841 	return (CS_ERROR);
    842 }
    843 
    844 int
    845 cs_reset_chip(struct cs_softc *sc)
    846 {
    847 	int intState;
    848 	int x;
    849 
    850 	/* Disable interrupts at the CPU so reset command is atomic */
    851 	intState = splnet();
    852 
    853 	/*
    854 	 * We are now resetting the chip
    855 	 *
    856 	 * A spurious interrupt is generated by the chip when it is reset. This
    857 	 * variable informs the interrupt handler to ignore this interrupt.
    858 	 */
    859 	sc->sc_resetting = TRUE;
    860 
    861 	/* Issue a reset command to the chip */
    862 	CS_WRITE_PACKET_PAGE(sc, PKTPG_SELF_CTL, SELF_CTL_RESET);
    863 
    864 	/* Re-enable interrupts at the CPU */
    865 	splx(intState);
    866 
    867 	/* The chip is always in IO mode after a reset */
    868 	sc->sc_memorymode = FALSE;
    869 
    870 	/* If transmission was in progress, it is not now */
    871 	sc->sc_txbusy = FALSE;
    872 
    873 	/*
    874 	 * there was a delay(125); here, but it seems uneccesary 125 usec is
    875 	 * 1/8000 of a second, not 1/8 of a second. the data sheet advises
    876 	 * 1/10 of a second here, but the SI_BUSY and INIT_DONE loops below
    877 	 * should be sufficient.
    878 	 */
    879 
    880 	/* Transition SBHE to switch chip from 8-bit to 16-bit */
    881 	IO_READ_1(sc, PORT_PKTPG_PTR + 0);
    882 	IO_READ_1(sc, PORT_PKTPG_PTR + 1);
    883 	IO_READ_1(sc, PORT_PKTPG_PTR + 0);
    884 	IO_READ_1(sc, PORT_PKTPG_PTR + 1);
    885 
    886 	/* Wait until the EEPROM is not busy */
    887 	for (x = 0; x < MAXLOOP; x++) {
    888 		if (!(CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_SI_BUSY))
    889 			break;
    890 	}
    891 
    892 	if (x == MAXLOOP)
    893 		return CS_ERROR;
    894 
    895 	/* Wait until initialization is done */
    896 	for (x = 0; x < MAXLOOP; x++) {
    897 		if (CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_INIT_DONE)
    898 			break;
    899 	}
    900 
    901 	if (x == MAXLOOP)
    902 		return CS_ERROR;
    903 
    904 	/* Reset is no longer in progress */
    905 	sc->sc_resetting = FALSE;
    906 
    907 	return CS_OK;
    908 }
    909 
    910 int
    911 cs_verify_eeprom(struct cs_softc *sc)
    912 {
    913 	u_int16_t self_status;
    914 
    915 	/* Verify that the EEPROM is present and OK */
    916 	self_status = CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST);
    917 	if (((self_status & SELF_ST_EEP_PRES) &&
    918 	     (self_status & SELF_ST_EEP_OK)) == 0)
    919 		return (CS_ERROR);
    920 
    921 	return (CS_OK);
    922 }
    923 
    924 int
    925 cs_read_eeprom(struct cs_softc *sc, int offset, u_int16_t *pValue)
    926 {
    927 	int x;
    928 
    929 	/* Ensure that the EEPROM is not busy */
    930 	for (x = 0; x < MAXLOOP; x++) {
    931 		if (!(CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST) &
    932 		      SELF_ST_SI_BUSY))
    933 			break;
    934 	}
    935 
    936 	if (x == MAXLOOP)
    937 		return (CS_ERROR);
    938 
    939 	/* Issue the command to read the offset within the EEPROM */
    940 	CS_WRITE_PACKET_PAGE_IO(sc, PKTPG_EEPROM_CMD,
    941 	    offset | EEPROM_CMD_READ);
    942 
    943 	/* Wait until the command is completed */
    944 	for (x = 0; x < MAXLOOP; x++) {
    945 		if (!(CS_READ_PACKET_PAGE_IO(sc, PKTPG_SELF_ST) &
    946 		      SELF_ST_SI_BUSY))
    947 			break;
    948 	}
    949 
    950 	if (x == MAXLOOP)
    951 		return (CS_ERROR);
    952 
    953 	/* Get the EEPROM data from the EEPROM Data register */
    954 	*pValue = CS_READ_PACKET_PAGE_IO(sc, PKTPG_EEPROM_DATA);
    955 
    956 	return (CS_OK);
    957 }
    958 
    959 void
    960 cs_initChip(struct cs_softc *sc)
    961 {
    962 	u_int16_t busCtl;
    963 	u_int16_t selfCtl;
    964 	u_int16_t v;
    965 	u_int16_t isaId;
    966 	int i;
    967 	int media = IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media);
    968 
    969 	/* Disable reception and transmission of frames */
    970 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL,
    971 	    CS_READ_PACKET_PAGE(sc, PKTPG_LINE_CTL) &
    972 	    ~LINE_CTL_RX_ON & ~LINE_CTL_TX_ON);
    973 
    974 	/* Disable interrupt at the chip */
    975 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    976 	    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) & ~BUS_CTL_INT_ENBL);
    977 
    978 	/* If IOCHRDY is enabled then clear the bit in the busCtl register */
    979 	busCtl = CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL);
    980 	if (sc->sc_cfgflags & CFGFLG_IOCHRDY) {
    981 		CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    982 		    busCtl & ~BUS_CTL_IOCHRDY);
    983 	} else {
    984 		CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
    985 		    busCtl | BUS_CTL_IOCHRDY);
    986 	}
    987 
    988 	/* Set the Line Control register to match the media type */
    989 	if (media == IFM_10_T)
    990 		CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL, LINE_CTL_10BASET);
    991 	else
    992 		CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL, LINE_CTL_AUI_ONLY);
    993 
    994 	/*
    995 	 * Set the BSTATUS/HC1 pin to be used as HC1.  HC1 is used to
    996 	 * enable the DC/DC converter
    997 	 */
    998 	selfCtl = SELF_CTL_HC1E;
    999 
   1000 	/* If the media type is 10Base2 */
   1001 	if (media == IFM_10_2) {
   1002 		/*
   1003 		 * Enable the DC/DC converter if it has a low enable.
   1004 		 */
   1005 		if ((sc->sc_cfgflags & CFGFLG_DCDC_POL) == 0)
   1006 			/*
   1007 			 * Set the HCB1 bit, which causes the HC1 pin to go
   1008 			 * low.
   1009 			 */
   1010 			selfCtl |= SELF_CTL_HCB1;
   1011 	} else { /* Media type is 10BaseT or AUI */
   1012 		/*
   1013 		 * Disable the DC/DC converter if it has a high enable.
   1014 		 */
   1015 		if ((sc->sc_cfgflags & CFGFLG_DCDC_POL) != 0) {
   1016 			/*
   1017 			 * Set the HCB1 bit, which causes the HC1 pin to go
   1018 			 * low.
   1019 			 */
   1020 			selfCtl |= SELF_CTL_HCB1;
   1021 		}
   1022 	}
   1023 	CS_WRITE_PACKET_PAGE(sc, PKTPG_SELF_CTL, selfCtl);
   1024 
   1025 	/* enable normal link pulse */
   1026 	if (sc->sc_prodid == PROD_ID_CS8920 || sc->sc_prodid == PROD_ID_CS8920M)
   1027 		CS_WRITE_PACKET_PAGE(sc, PKTPG_AUTONEG_CTL, AUTOCTL_NLP_ENABLE);
   1028 
   1029 	/* Enable full-duplex, if appropriate */
   1030 	if (sc->sc_media.ifm_cur->ifm_media & IFM_FDX)
   1031 		CS_WRITE_PACKET_PAGE(sc, PKTPG_TEST_CTL, TEST_CTL_FDX);
   1032 
   1033 	/* RX_CTL set in cs_set_ladr_filt(), below */
   1034 
   1035 	/* enable all transmission interrupts */
   1036 	CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CFG, TX_CFG_ALL_IE);
   1037 
   1038 	/* Accept all receive interrupts */
   1039 	CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG, RX_CFG_ALL_IE);
   1040 
   1041 	/*
   1042 	 * Configure Operational Modes
   1043 	 *
   1044 	 * I have turned off the BUF_CFG_RX_MISS_IE, to speed things up, this is
   1045 	 * a better way to do it because the card has a counter which can be
   1046 	 * read to update the RX_MISS counter. This saves many interrupts.
   1047 	 *
   1048 	 * I have turned on the tx and rx overflow interrupts to counter using
   1049 	 * the receive miss interrupt. This is a better estimate of errors
   1050 	 * and requires lower system overhead.
   1051 	 */
   1052 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG, BUF_CFG_TX_UNDR_IE |
   1053 			  BUF_CFG_RX_DMA_IE);
   1054 
   1055 	if (sc->sc_dma_chipinit)
   1056 		(*sc->sc_dma_chipinit)(sc);
   1057 
   1058 	/* If memory mode is enabled */
   1059 	if (sc->sc_cfgflags & CFGFLG_MEM_MODE) {
   1060 		/* If external logic is present for address decoding */
   1061 		if (CS_READ_PACKET_PAGE(sc, PKTPG_SELF_ST) & SELF_ST_EL_PRES) {
   1062 			/*
   1063 			 * Program the external logic to decode address bits
   1064 			 * SA20-SA23
   1065 			 */
   1066 			CS_WRITE_PACKET_PAGE(sc, PKTPG_EEPROM_CMD,
   1067 			    ((sc->sc_pktpgaddr & 0xffffff) >> 20) |
   1068 			    EEPROM_CMD_ELSEL);
   1069 		}
   1070 
   1071 		/*
   1072 		 * Write the packet page base physical address to the memory
   1073 		 * base register.
   1074 		 */
   1075 		CS_WRITE_PACKET_PAGE(sc, PKTPG_MEM_BASE + 0,
   1076 		    sc->sc_pktpgaddr & 0xFFFF);
   1077 		CS_WRITE_PACKET_PAGE(sc, PKTPG_MEM_BASE + 2,
   1078 		    sc->sc_pktpgaddr >> 16);
   1079 		busCtl = BUS_CTL_MEM_MODE;
   1080 
   1081 		/* tell the chip to read the addresses off the SA pins */
   1082 		if (sc->sc_cfgflags & CFGFLG_USE_SA) {
   1083 			busCtl |= BUS_CTL_USE_SA;
   1084 		}
   1085 		CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
   1086 		    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) | busCtl);
   1087 
   1088 		/* We are in memory mode now! */
   1089 		sc->sc_memorymode = TRUE;
   1090 
   1091 		/*
   1092 		 * wait here (10ms) for the chip to swap over. this is the
   1093 		 * maximum time that this could take.
   1094 		 */
   1095 		delay(10000);
   1096 
   1097 		/* Verify that we can read from the chip */
   1098 		isaId = CS_READ_PACKET_PAGE(sc, PKTPG_EISA_NUM);
   1099 
   1100 		/*
   1101 		 * As a last minute sanity check before actually using mapped
   1102 		 * memory we verify that we can read the isa number from the
   1103 		 * chip in memory mode.
   1104 		 */
   1105 		if (isaId != EISA_NUM_CRYSTAL) {
   1106 			aprint_error_dev(sc->sc_dev,
   1107 			    "failed to enable memory mode\n");
   1108 			sc->sc_memorymode = FALSE;
   1109 		} else {
   1110 			/*
   1111 			 * we are in memory mode so if we aren't using DMA,
   1112 			 * then program the chip to interrupt early.
   1113 			 */
   1114 			if ((sc->sc_cfgflags & CFGFLG_DMA_MODE) == 0) {
   1115 				CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG,
   1116 				    BUF_CFG_RX_DEST_IE |
   1117 				    BUF_CFG_RX_MISS_OVER_IE |
   1118 				    BUF_CFG_TX_COL_OVER_IE);
   1119 			}
   1120 		}
   1121 
   1122 	}
   1123 
   1124 	/* Put Ethernet address into the Individual Address register */
   1125 	for (i = 0; i < 6; i += 2) {
   1126 		v = sc->sc_enaddr[i + 0] | (sc->sc_enaddr[i + 1]) << 8;
   1127 		CS_WRITE_PACKET_PAGE(sc, PKTPG_IND_ADDR + i, v);
   1128 	}
   1129 
   1130 	if (sc->sc_irq != -1) {
   1131 		/* Set the interrupt level in the chip */
   1132 		if (sc->sc_prodid == PROD_ID_CS8900) {
   1133 			if (sc->sc_irq == 5) {
   1134 				CS_WRITE_PACKET_PAGE(sc, PKTPG_INT_NUM, 3);
   1135 			} else {
   1136 				CS_WRITE_PACKET_PAGE(sc, PKTPG_INT_NUM, (sc->sc_irq) - 10);
   1137 			}
   1138 		}
   1139 		else { /* CS8920 */
   1140 			CS_WRITE_PACKET_PAGE(sc, PKTPG_8920_INT_NUM, sc->sc_irq);
   1141 		}
   1142 	}
   1143 
   1144 	/* write the multicast mask to the address filter register */
   1145 	cs_set_ladr_filt(sc, &sc->sc_ethercom);
   1146 
   1147 	/* Enable reception and transmission of frames */
   1148 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LINE_CTL,
   1149 	    CS_READ_PACKET_PAGE(sc, PKTPG_LINE_CTL) |
   1150 	    LINE_CTL_RX_ON | LINE_CTL_TX_ON);
   1151 
   1152 	/* Enable interrupt at the chip */
   1153 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL,
   1154 	    CS_READ_PACKET_PAGE(sc, PKTPG_BUS_CTL) | BUS_CTL_INT_ENBL);
   1155 }
   1156 
   1157 int
   1158 cs_init(struct ifnet *ifp)
   1159 {
   1160 	int intState;
   1161 	int error = CS_OK;
   1162 	struct cs_softc *sc = ifp->if_softc;
   1163 
   1164 	if (cs_enable(sc))
   1165 		goto out;
   1166 
   1167 	cs_stop(ifp, 0);
   1168 
   1169 	intState = splnet();
   1170 
   1171 #if 0
   1172 	/* Mark the interface as down */
   1173 	sc->sc_ethercom.ec_if.if_flags &= ~(IFF_UP | IFF_RUNNING);
   1174 #endif
   1175 
   1176 #ifdef CS_DEBUG
   1177 	/* Enable debugging */
   1178 	sc->sc_ethercom.ec_if.if_flags |= IFF_DEBUG;
   1179 #endif
   1180 
   1181 	/* Reset the chip */
   1182 	if ((error = cs_reset_chip(sc)) == CS_OK) {
   1183 		/* Initialize the chip */
   1184 		cs_initChip(sc);
   1185 
   1186 		/* Mark the interface as running */
   1187 		sc->sc_ethercom.ec_if.if_flags |= IFF_RUNNING;
   1188 		sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1189 		sc->sc_ethercom.ec_if.if_timer = 0;
   1190 
   1191 		/* Assume we have carrier until we are told otherwise. */
   1192 		sc->sc_carrier = 1;
   1193 	} else {
   1194 		aprint_error_dev(sc->sc_dev, "unable to reset chip\n");
   1195 	}
   1196 
   1197 	splx(intState);
   1198 out:
   1199 	if (error == CS_OK)
   1200 		return 0;
   1201 	return EIO;
   1202 }
   1203 
   1204 void
   1205 cs_set_ladr_filt(struct cs_softc *sc, struct ethercom *ec)
   1206 {
   1207 	struct ifnet *ifp = &ec->ec_if;
   1208 	struct ether_multi *enm;
   1209 	struct ether_multistep step;
   1210 	u_int16_t af[4];
   1211 	u_int16_t port, mask, index;
   1212 
   1213 	/*
   1214          * Set up multicast address filter by passing all multicast addresses
   1215          * through a crc generator, and then using the high order 6 bits as an
   1216          * index into the 64 bit logical address filter.  The high order bit
   1217          * selects the word, while the rest of the bits select the bit within
   1218          * the word.
   1219          */
   1220 	if (ifp->if_flags & IFF_PROMISC) {
   1221 		/* accept all valid frames. */
   1222 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CTL,
   1223 		    RX_CTL_PROMISC_A | RX_CTL_RX_OK_A |
   1224 		    RX_CTL_IND_A | RX_CTL_BCAST_A | RX_CTL_MCAST_A);
   1225 		ifp->if_flags |= IFF_ALLMULTI;
   1226 		return;
   1227 	}
   1228 
   1229 	/*
   1230 	 * accept frames if a. crc valid, b. individual address match c.
   1231 	 * broadcast address,and d. multicast addresses matched in the hash
   1232 	 * filter
   1233 	 */
   1234 	CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CTL,
   1235 	    RX_CTL_RX_OK_A | RX_CTL_IND_A | RX_CTL_BCAST_A | RX_CTL_MCAST_A);
   1236 
   1237 
   1238 	/*
   1239 	 * start off with all multicast flag clear, set it if we need to
   1240 	 * later, otherwise we will leave it.
   1241 	 */
   1242 	ifp->if_flags &= ~IFF_ALLMULTI;
   1243 	af[0] = af[1] = af[2] = af[3] = 0x0000;
   1244 
   1245 	/*
   1246 	 * Loop through all the multicast addresses unless we get a range of
   1247 	 * addresses, in which case we will just accept all packets.
   1248 	 * Justification for this is given in the next comment.
   1249 	 */
   1250 	ETHER_FIRST_MULTI(step, ec, enm);
   1251 	while (enm != NULL) {
   1252 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1253 		    sizeof enm->enm_addrlo)) {
   1254 			/*
   1255 	                 * We must listen to a range of multicast addresses.
   1256 	                 * For now, just accept all multicasts, rather than
   1257 	                 * trying to set only those filter bits needed to match
   1258 	                 * the range.  (At this time, the only use of address
   1259 	                 * ranges is for IP multicast routing, for which the
   1260 	                 * range is big enough to require all bits set.)
   1261 	                 */
   1262 			ifp->if_flags |= IFF_ALLMULTI;
   1263 			af[0] = af[1] = af[2] = af[3] = 0xffff;
   1264 			break;
   1265 		} else {
   1266 			/*
   1267 	                 * we have got an individual address so just set that
   1268 	                 * bit.
   1269 	                 */
   1270 			index = cs_hash_index(enm->enm_addrlo);
   1271 
   1272 			/* Set the bit the Logical address filter. */
   1273 			port = (u_int16_t) (index >> 4);
   1274 			mask = (u_int16_t) (1 << (index & 0xf));
   1275 			af[port] |= mask;
   1276 
   1277 			ETHER_NEXT_MULTI(step, enm);
   1278 		}
   1279 	}
   1280 
   1281 	/* now program the chip with the addresses */
   1282 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 0, af[0]);
   1283 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 2, af[1]);
   1284 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 4, af[2]);
   1285 	CS_WRITE_PACKET_PAGE(sc, PKTPG_LOG_ADDR + 6, af[3]);
   1286 	return;
   1287 }
   1288 
   1289 u_int16_t
   1290 cs_hash_index(char *addr)
   1291 {
   1292 	uint32_t crc;
   1293 	uint16_t hash_code;
   1294 
   1295 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
   1296 
   1297 	hash_code = crc >> 26;
   1298 	return (hash_code);
   1299 }
   1300 
   1301 void
   1302 cs_reset(struct cs_softc *sc)
   1303 {
   1304 
   1305 	/* Mark the interface as down */
   1306 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
   1307 
   1308 	/* Reset the chip */
   1309 	cs_reset_chip(sc);
   1310 }
   1311 
   1312 int
   1313 cs_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1314 {
   1315 	struct cs_softc *sc = ifp->if_softc;
   1316 	struct ifreq *ifr = data;
   1317 	int state;
   1318 	int result;
   1319 
   1320 	state = splnet();
   1321 
   1322 	result = 0;		/* only set if something goes wrong */
   1323 
   1324 	switch (cmd) {
   1325 	case SIOCGIFMEDIA:
   1326 	case SIOCSIFMEDIA:
   1327 		result = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1328 		break;
   1329 
   1330 	default:
   1331 		result = ether_ioctl(ifp, cmd, data);
   1332 		if (result == ENETRESET) {
   1333 			if (ifp->if_flags & IFF_RUNNING) {
   1334 				/*
   1335 				 * Multicast list has changed.  Set the
   1336 				 * hardware filter accordingly.
   1337 				 */
   1338 				cs_set_ladr_filt(sc, &sc->sc_ethercom);
   1339 			}
   1340 			result = 0;
   1341 		}
   1342 		break;
   1343 	}
   1344 
   1345 	splx(state);
   1346 
   1347 	return result;
   1348 }
   1349 
   1350 int
   1351 cs_mediachange(struct ifnet *ifp)
   1352 {
   1353 
   1354 	/*
   1355 	 * Current media is already set up.  Just reset the interface
   1356 	 * to let the new value take hold.
   1357 	 */
   1358 	cs_init(ifp);
   1359 	return (0);
   1360 }
   1361 
   1362 void
   1363 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1364 {
   1365 	struct cs_softc *sc = ifp->if_softc;
   1366 
   1367 	/*
   1368 	 * The currently selected media is always the active media.
   1369 	 */
   1370 	ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
   1371 
   1372 	if (ifp->if_flags & IFF_UP) {
   1373 		/* Interface up, status is valid. */
   1374 		ifmr->ifm_status = IFM_AVALID |
   1375 		    (sc->sc_carrier ? IFM_ACTIVE : 0);
   1376 	}
   1377 		else ifmr->ifm_status = 0;
   1378 }
   1379 
   1380 int
   1381 cs_intr(void *arg)
   1382 {
   1383 	struct cs_softc *sc = arg;
   1384 	u_int16_t Event;
   1385 	u_int16_t rndEvent;
   1386 
   1387 /*printf("cs_intr %p\n", sc);*/
   1388 	/* Ignore any interrupts that happen while the chip is being reset */
   1389 	if (sc->sc_resetting) {
   1390 		printf("%s: cs_intr: reset in progress\n",
   1391 		    device_xname(sc->sc_dev));
   1392 		return 1;
   1393 	}
   1394 
   1395 	/* Read an event from the Interrupt Status Queue */
   1396 	if (sc->sc_memorymode)
   1397 		Event = CS_READ_PACKET_PAGE(sc, PKTPG_ISQ);
   1398 	else
   1399 		Event = CS_READ_PORT(sc, PORT_ISQ);
   1400 
   1401 	if ((Event & REG_NUM_MASK) == 0 || Event == 0xffff)
   1402 		return 0;	/* not ours */
   1403 
   1404 	rndEvent = Event;
   1405 
   1406 	/* Process all the events in the Interrupt Status Queue */
   1407 	while ((Event & REG_NUM_MASK) != 0 && Event != 0xffff) {
   1408 		/* Dispatch to an event handler based on the register number */
   1409 		switch (Event & REG_NUM_MASK) {
   1410 		case REG_NUM_RX_EVENT:
   1411 			cs_receive_event(sc, Event);
   1412 			break;
   1413 		case REG_NUM_TX_EVENT:
   1414 			cs_transmit_event(sc, Event);
   1415 			break;
   1416 		case REG_NUM_BUF_EVENT:
   1417 			cs_buffer_event(sc, Event);
   1418 			break;
   1419 		case REG_NUM_TX_COL:
   1420 		case REG_NUM_RX_MISS:
   1421 			cs_counter_event(sc, Event);
   1422 			break;
   1423 		default:
   1424 			printf("%s: unknown interrupt event 0x%x\n",
   1425 			    device_xname(sc->sc_dev), Event);
   1426 			break;
   1427 		}
   1428 
   1429 		/* Read another event from the Interrupt Status Queue */
   1430 		if (sc->sc_memorymode)
   1431 			Event = CS_READ_PACKET_PAGE(sc, PKTPG_ISQ);
   1432 		else
   1433 			Event = CS_READ_PORT(sc, PORT_ISQ);
   1434 	}
   1435 
   1436 	/* have handled the interrupt */
   1437 	rnd_add_uint32(&sc->rnd_source, rndEvent);
   1438 	return 1;
   1439 }
   1440 
   1441 void
   1442 cs_counter_event(struct cs_softc *sc, u_int16_t cntEvent)
   1443 {
   1444 	struct ifnet *ifp;
   1445 	u_int16_t errorCount;
   1446 
   1447 	ifp = &sc->sc_ethercom.ec_if;
   1448 
   1449 	switch (cntEvent & REG_NUM_MASK) {
   1450 	case REG_NUM_TX_COL:
   1451 		/*
   1452 		 * the count should be read before an overflow occurs.
   1453 		 */
   1454 		errorCount = CS_READ_PACKET_PAGE(sc, PKTPG_TX_COL);
   1455 		/*
   1456 		 * the tramsit event routine always checks the number of
   1457 		 * collisions for any packet so we don't increment any
   1458 		 * counters here, as they should already have been
   1459 		 * considered.
   1460 		 */
   1461 		break;
   1462 	case REG_NUM_RX_MISS:
   1463 		/*
   1464 		 * the count should be read before an overflow occurs.
   1465 		 */
   1466 		errorCount = CS_READ_PACKET_PAGE(sc, PKTPG_RX_MISS);
   1467 		/*
   1468 		 * Increment the input error count, the first 6bits are the
   1469 		 * register id.
   1470 		 */
   1471 		ifp->if_ierrors += ((errorCount & 0xffC0) >> 6);
   1472 		break;
   1473 	default:
   1474 		/* do nothing */
   1475 		break;
   1476 	}
   1477 }
   1478 
   1479 void
   1480 cs_buffer_event(struct cs_softc *sc, u_int16_t bufEvent)
   1481 {
   1482 
   1483 	/*
   1484 	 * multiple events can be in the buffer event register at one time so
   1485 	 * a standard switch statement will not suffice, here every event
   1486 	 * must be checked.
   1487 	 */
   1488 
   1489 	/*
   1490 	 * if 128 bits have been rxed by the time we get here, the dest event
   1491 	 * will be cleared and 128 event will be set.
   1492 	 */
   1493 	if ((bufEvent & (BUF_EVENT_RX_DEST | BUF_EVENT_RX_128)) != 0) {
   1494 		cs_process_rx_early(sc);
   1495 	}
   1496 
   1497 	if (bufEvent & BUF_EVENT_RX_DMA) {
   1498 		/* process the receive data */
   1499 		if (sc->sc_dma_process_rx)
   1500 			(*sc->sc_dma_process_rx)(sc);
   1501 		else
   1502 			/* should panic? */
   1503 			aprint_error_dev(sc->sc_dev, "unexpected DMA event\n");
   1504 	}
   1505 
   1506 	if (bufEvent & BUF_EVENT_TX_UNDR) {
   1507 #if 0
   1508 		/*
   1509 		 * This can happen occasionally, and it's not worth worrying
   1510 		 * about.
   1511 		 */
   1512 		printf("%s: transmit underrun (%d -> %d)\n",
   1513 		    device_xname(sc->sc_dev), sc->sc_xe_ent,
   1514 		    cs_xmit_early_table[sc->sc_xe_ent].worse);
   1515 #endif
   1516 		sc->sc_xe_ent = cs_xmit_early_table[sc->sc_xe_ent].worse;
   1517 		sc->sc_xe_togo =
   1518 		    cs_xmit_early_table[sc->sc_xe_ent].better_count;
   1519 
   1520 		/* had an underrun, transmit is finished */
   1521 		sc->sc_txbusy = FALSE;
   1522 	}
   1523 
   1524 	if (bufEvent & BUF_EVENT_SW_INT) {
   1525 		printf("%s: software initiated interrupt\n",
   1526 		    device_xname(sc->sc_dev));
   1527 	}
   1528 }
   1529 
   1530 void
   1531 cs_transmit_event(struct cs_softc *sc, u_int16_t txEvent)
   1532 {
   1533 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1534 
   1535 	/* If there were any errors transmitting this frame */
   1536 	if (txEvent & (TX_EVENT_LOSS_CRS | TX_EVENT_SQE_ERR | TX_EVENT_OUT_WIN |
   1537 		       TX_EVENT_JABBER | TX_EVENT_16_COLL)) {
   1538 		/* Increment the output error count */
   1539 		ifp->if_oerrors++;
   1540 
   1541 		/* Note carrier loss. */
   1542 		if (txEvent & TX_EVENT_LOSS_CRS)
   1543 			sc->sc_carrier = 0;
   1544 
   1545 		/* If debugging is enabled then log error messages */
   1546 		if (ifp->if_flags & IFF_DEBUG) {
   1547 			if (txEvent & TX_EVENT_LOSS_CRS) {
   1548 				aprint_error_dev(sc->sc_dev, "lost carrier\n");
   1549 			}
   1550 			if (txEvent & TX_EVENT_SQE_ERR) {
   1551 				aprint_error_dev(sc->sc_dev, "SQE error\n");
   1552 			}
   1553 			if (txEvent & TX_EVENT_OUT_WIN) {
   1554 				aprint_error_dev(sc->sc_dev,
   1555 				    "out-of-window collision\n");
   1556 			}
   1557 			if (txEvent & TX_EVENT_JABBER) {
   1558 				aprint_error_dev(sc->sc_dev, "jabber\n");
   1559 			}
   1560 			if (txEvent & TX_EVENT_16_COLL) {
   1561 				aprint_error_dev(sc->sc_dev, "16 collisions\n");
   1562 			}
   1563 		}
   1564 	}
   1565 	else {
   1566 		/* Transmission successful, carrier is up. */
   1567 		sc->sc_carrier = 1;
   1568 #ifdef SHARK
   1569 		ledNetActive();
   1570 #endif
   1571 	}
   1572 
   1573 	/* Add the number of collisions for this frame */
   1574 	if (txEvent & TX_EVENT_16_COLL) {
   1575 		ifp->if_collisions += 16;
   1576 	} else {
   1577 		ifp->if_collisions += ((txEvent & TX_EVENT_COLL_MASK) >> 11);
   1578 	}
   1579 
   1580 	ifp->if_opackets++;
   1581 
   1582 	/* Transmission is no longer in progress */
   1583 	sc->sc_txbusy = FALSE;
   1584 
   1585 	/* If there is more to transmit, start the next transmission */
   1586 	if_schedule_deferred_start(ifp);
   1587 }
   1588 
   1589 void
   1590 cs_print_rx_errors(struct cs_softc *sc, u_int16_t rxEvent)
   1591 {
   1592 
   1593 	if (rxEvent & RX_EVENT_RUNT)
   1594 		aprint_error_dev(sc->sc_dev, "runt\n");
   1595 
   1596 	if (rxEvent & RX_EVENT_X_DATA)
   1597 		aprint_error_dev(sc->sc_dev, "extra data\n");
   1598 
   1599 	if (rxEvent & RX_EVENT_CRC_ERR) {
   1600 		if (rxEvent & RX_EVENT_DRIBBLE)
   1601 			aprint_error_dev(sc->sc_dev, "alignment error\n");
   1602 		else
   1603 			aprint_error_dev(sc->sc_dev, "CRC error\n");
   1604 	} else {
   1605 		if (rxEvent & RX_EVENT_DRIBBLE)
   1606 			aprint_error_dev(sc->sc_dev, "dribble bits\n");
   1607 	}
   1608 }
   1609 
   1610 void
   1611 cs_receive_event(struct cs_softc *sc, u_int16_t rxEvent)
   1612 {
   1613 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1614 
   1615 	/* If the frame was not received OK */
   1616 	if (!(rxEvent & RX_EVENT_RX_OK)) {
   1617 		/* Increment the input error count */
   1618 		ifp->if_ierrors++;
   1619 
   1620 		/*
   1621 		 * If debugging is enabled then log error messages.
   1622 		 */
   1623 		if (ifp->if_flags & IFF_DEBUG) {
   1624 			if (rxEvent != REG_NUM_RX_EVENT) {
   1625 				cs_print_rx_errors(sc, rxEvent);
   1626 
   1627 				/*
   1628 				 * Must read the length of all received
   1629 				 * frames
   1630 				 */
   1631 				CS_READ_PACKET_PAGE(sc, PKTPG_RX_LENGTH);
   1632 
   1633 				/* Skip the received frame */
   1634 				CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1635 					CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) |
   1636 						  RX_CFG_SKIP);
   1637 			} else {
   1638 				aprint_error_dev(sc->sc_dev, "implied skip\n");
   1639 			}
   1640 		}
   1641 	} else {
   1642 		/*
   1643 		 * process the received frame and pass it up to the upper
   1644 		 * layers.
   1645 		 */
   1646 		cs_process_receive(sc);
   1647 	}
   1648 }
   1649 
   1650 void
   1651 cs_ether_input(struct cs_softc *sc, struct mbuf *m)
   1652 {
   1653 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1654 
   1655 	/* Pass the packet up. */
   1656 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1657 }
   1658 
   1659 void
   1660 cs_process_receive(struct cs_softc *sc)
   1661 {
   1662 	struct ifnet *ifp;
   1663 	struct mbuf *m;
   1664 	int totlen;
   1665 	u_int16_t *pBuff, *pBuffLimit;
   1666 	int pad;
   1667 	unsigned int frameOffset = 0;	/* XXX: gcc */
   1668 
   1669 #ifdef SHARK
   1670 	ledNetActive();
   1671 #endif
   1672 
   1673 	ifp = &sc->sc_ethercom.ec_if;
   1674 
   1675 	/* Received a packet; carrier is up. */
   1676 	sc->sc_carrier = 1;
   1677 
   1678 	if (sc->sc_memorymode) {
   1679 		/* Initialize the frame offset */
   1680 		frameOffset = PKTPG_RX_LENGTH;
   1681 
   1682 		/* Get the length of the received frame */
   1683 		totlen = CS_READ_PACKET_PAGE(sc, frameOffset);
   1684 		frameOffset += 2;
   1685 	}
   1686 	else {
   1687 		/* drop status */
   1688 		CS_READ_PORT(sc, PORT_RXTX_DATA);
   1689 
   1690 		/* Get the length of the received frame */
   1691 		totlen = CS_READ_PORT(sc, PORT_RXTX_DATA);
   1692 	}
   1693 
   1694 	if (totlen > ETHER_MAX_LEN) {
   1695 		aprint_error_dev(sc->sc_dev, "invalid packet length %d\n",
   1696 		    totlen);
   1697 
   1698 		/* skip the received frame */
   1699 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1700 			CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1701 		return;
   1702 	}
   1703 
   1704 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1705 	if (m == 0) {
   1706 		aprint_error_dev(sc->sc_dev,
   1707 		    "cs_process_receive: unable to allocate mbuf\n");
   1708 		ifp->if_ierrors++;
   1709 		/*
   1710 		 * couldn't allocate an mbuf so things are not good, may as
   1711 		 * well drop the packet I think.
   1712 		 *
   1713 		 * have already read the length so we should be right to skip
   1714 		 * the packet.
   1715 		 */
   1716 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1717 		    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1718 		return;
   1719 	}
   1720 	m_set_rcvif(m, ifp);
   1721 	m->m_pkthdr.len = totlen;
   1722 
   1723 	/* number of bytes to align ip header on word boundary for ipintr */
   1724 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
   1725 
   1726 	/*
   1727 	 * alloc mbuf cluster if we need.
   1728 	 * we need 1 byte spare because following
   1729 	 * packet read loop can overrun.
   1730 	 */
   1731 	if (totlen + pad + 1 > MHLEN) {
   1732 		MCLGET(m, M_DONTWAIT);
   1733 		if ((m->m_flags & M_EXT) == 0) {
   1734 			/* couldn't allocate an mbuf cluster */
   1735 			aprint_error_dev(sc->sc_dev,
   1736 			    "cs_process_receive: "
   1737 			    "unable to allocate a cluster\n");
   1738 			m_freem(m);
   1739 
   1740 			/* skip the received frame */
   1741 			CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1742 				CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1743 			return;
   1744 		}
   1745 	}
   1746 
   1747 	/* align ip header on word boundary for ipintr */
   1748 	m->m_data += pad;
   1749 
   1750 	m->m_len = totlen;
   1751 	pBuff = mtod(m, u_int16_t *);
   1752 
   1753 	/* now read the data from the chip */
   1754 	if (sc->sc_memorymode) {
   1755 		pBuffLimit = pBuff + (totlen + 1) / 2;	/* don't want to go over */
   1756 		while (pBuff < pBuffLimit) {
   1757 			*pBuff++ = CS_READ_PACKET_PAGE(sc, frameOffset);
   1758 			frameOffset += 2;
   1759 		}
   1760 	}
   1761 	else {
   1762 		IO_READ_MULTI_2(sc, PORT_RXTX_DATA, pBuff, (totlen + 1)>>1);
   1763 	}
   1764 
   1765 	cs_ether_input(sc, m);
   1766 }
   1767 
   1768 void
   1769 cs_process_rx_early(struct cs_softc *sc)
   1770 {
   1771 	struct ifnet *ifp;
   1772 	struct mbuf *m;
   1773 	u_int16_t frameCount, oldFrameCount;
   1774 	u_int16_t rxEvent;
   1775 	u_int16_t *pBuff;
   1776 	int pad;
   1777 	unsigned int frameOffset;
   1778 
   1779 
   1780 	ifp = &sc->sc_ethercom.ec_if;
   1781 
   1782 	/* Initialize the frame offset */
   1783 	frameOffset = PKTPG_RX_FRAME;
   1784 	frameCount = 0;
   1785 
   1786 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1787 	if (m == 0) {
   1788 		aprint_error_dev(sc->sc_dev,
   1789 		    "cs_process_rx_early: unable to allocate mbuf\n");
   1790 		ifp->if_ierrors++;
   1791 		/*
   1792 		 * couldn't allocate an mbuf so things are not good, may as
   1793 		 * well drop the packet I think.
   1794 		 *
   1795 		 * have already read the length so we should be right to skip
   1796 		 * the packet.
   1797 		 */
   1798 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1799 		    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1800 		return;
   1801 	}
   1802 	m_set_rcvif(m, ifp);
   1803 	/*
   1804 	 * save processing by always using a mbuf cluster, guaranteed to fit
   1805 	 * packet
   1806 	 */
   1807 	MCLGET(m, M_DONTWAIT);
   1808 	if ((m->m_flags & M_EXT) == 0) {
   1809 		/* couldn't allocate an mbuf cluster */
   1810 		aprint_error_dev(sc->sc_dev,
   1811 		    "cs_process_rx_early: unable to allocate a cluster\n");
   1812 		m_freem(m);
   1813 		/* skip the frame */
   1814 		CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG,
   1815 		    CS_READ_PACKET_PAGE(sc, PKTPG_RX_CFG) | RX_CFG_SKIP);
   1816 		return;
   1817 	}
   1818 
   1819 	/* align ip header on word boundary for ipintr */
   1820 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
   1821 	m->m_data += pad;
   1822 
   1823 	/* set up the buffer pointer to point to the data area */
   1824 	pBuff = mtod(m, u_int16_t *);
   1825 
   1826 	/*
   1827 	 * now read the frame byte counter until we have finished reading the
   1828 	 * frame
   1829 	 */
   1830 	oldFrameCount = 0;
   1831 	frameCount = CS_READ_PACKET_PAGE(sc, PKTPG_FRAME_BYTE_COUNT);
   1832 	while ((frameCount != 0) && (frameCount < MCLBYTES)) {
   1833 		for (; oldFrameCount < frameCount; oldFrameCount += 2) {
   1834 			*pBuff++ = CS_READ_PACKET_PAGE(sc, frameOffset);
   1835 			frameOffset += 2;
   1836 		}
   1837 
   1838 		/* read the new count from the chip */
   1839 		frameCount = CS_READ_PACKET_PAGE(sc, PKTPG_FRAME_BYTE_COUNT);
   1840 	}
   1841 
   1842 	/* update the mbuf counts */
   1843 	m->m_len = oldFrameCount;
   1844 	m->m_pkthdr.len = oldFrameCount;
   1845 
   1846 	/* now check the Rx Event register */
   1847 	rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_EVENT);
   1848 
   1849 	if ((rxEvent & RX_EVENT_RX_OK) != 0) {
   1850 		/*
   1851 		 * do an implied skip, it seems to be more reliable than a
   1852 		 * forced skip.
   1853 		 */
   1854 		rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_STATUS);
   1855 		rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_LENGTH);
   1856 
   1857 		/*
   1858 		 * now read the RX_EVENT register to perform an implied skip.
   1859 		 */
   1860 		rxEvent = CS_READ_PACKET_PAGE(sc, PKTPG_RX_EVENT);
   1861 
   1862 		cs_ether_input(sc, m);
   1863 	} else {
   1864 		m_freem(m);
   1865 		ifp->if_ierrors++;
   1866 	}
   1867 }
   1868 
   1869 void
   1870 cs_start_output(struct ifnet *ifp)
   1871 {
   1872 	struct cs_softc *sc;
   1873 	struct mbuf *pMbuf;
   1874 	struct mbuf *pMbufChain;
   1875 	u_int16_t BusStatus;
   1876 	u_int16_t Length;
   1877 	int txLoop = 0;
   1878 	int dropout = 0;
   1879 
   1880 	sc = ifp->if_softc;
   1881 
   1882 	/* check that the interface is up and running */
   1883 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
   1884 		return;
   1885 	}
   1886 
   1887 	/* Don't interrupt a transmission in progress */
   1888 	if (sc->sc_txbusy) {
   1889 		return;
   1890 	}
   1891 
   1892 	/* this loop will only run through once if transmission is successful */
   1893 	/*
   1894 	 * While there are packets to transmit and a transmit is not in
   1895 	 * progress
   1896 	 */
   1897 	while (sc->sc_txbusy == 0 && dropout == 0) {
   1898 		IFQ_DEQUEUE(&ifp->if_snd, pMbufChain);
   1899 		if (pMbufChain == NULL)
   1900 			break;
   1901 
   1902 		/*
   1903 	         * If BPF is listening on this interface, let it see the packet
   1904 	         * before we commit it to the wire.
   1905 	         */
   1906 		bpf_mtap(ifp, pMbufChain, BPF_D_OUT);
   1907 
   1908 		/* Find the total length of the data to transmit */
   1909 		Length = 0;
   1910 		for (pMbuf = pMbufChain; pMbuf != NULL; pMbuf = pMbuf->m_next)
   1911 			Length += pMbuf->m_len;
   1912 
   1913 		do {
   1914 			/*
   1915 			 * Request that the transmit be started after all
   1916 			 * data has been copied
   1917 			 *
   1918 			 * In IO mode must write to the IO port not the packet
   1919 			 * page address
   1920 			 *
   1921 			 * If this is changed to start transmission after a
   1922 			 * small amount of data has been copied you tend to
   1923 			 * get packet missed errors i think because the ISA
   1924 			 * bus is too slow. Or possibly the copy routine is
   1925 			 * not streamlined enough.
   1926 			 */
   1927 			if (sc->sc_memorymode) {
   1928 				CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CMD,
   1929 					cs_xmit_early_table[sc->sc_xe_ent].txcmd);
   1930 				CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_LENGTH, Length);
   1931 			}
   1932 			else {
   1933 				CS_WRITE_PORT(sc, PORT_TX_CMD,
   1934 					cs_xmit_early_table[sc->sc_xe_ent].txcmd);
   1935 				CS_WRITE_PORT(sc, PORT_TX_LENGTH, Length);
   1936 			}
   1937 
   1938 			/*
   1939 			 * Adjust early-transmit machinery.
   1940 			 */
   1941 			if (--sc->sc_xe_togo == 0) {
   1942 				sc->sc_xe_ent =
   1943 				    cs_xmit_early_table[sc->sc_xe_ent].better;
   1944 				sc->sc_xe_togo =
   1945 			    cs_xmit_early_table[sc->sc_xe_ent].better_count;
   1946 			}
   1947 			/*
   1948 			 * Read the BusStatus register which indicates
   1949 			 * success of the request
   1950 			 */
   1951 			BusStatus = CS_READ_PACKET_PAGE(sc, PKTPG_BUS_ST);
   1952 
   1953 			/*
   1954 			 * If there was an error in the transmit bid free the
   1955 			 * mbuf and go on. This is presuming that mbuf is
   1956 			 * corrupt.
   1957 			 */
   1958 			if (BusStatus & BUS_ST_TX_BID_ERR) {
   1959 				aprint_error_dev(sc->sc_dev,
   1960 				    "transmit bid error (too big)");
   1961 
   1962 				/* Discard the bad mbuf chain */
   1963 				m_freem(pMbufChain);
   1964 				sc->sc_ethercom.ec_if.if_oerrors++;
   1965 
   1966 				/* Loop up to transmit the next chain */
   1967 				txLoop = 0;
   1968 			} else {
   1969 				if (BusStatus & BUS_ST_RDY4TXNOW) {
   1970 					/*
   1971 					 * The chip is ready for transmission
   1972 					 * now
   1973 					 */
   1974 					/*
   1975 					 * Copy the frame to the chip to
   1976 					 * start transmission
   1977 					 */
   1978 					cs_copy_tx_frame(sc, pMbufChain);
   1979 
   1980 					/* Free the mbuf chain */
   1981 					m_freem(pMbufChain);
   1982 
   1983 					/* Transmission is now in progress */
   1984 					sc->sc_txbusy = TRUE;
   1985 					txLoop = 0;
   1986 				} else {
   1987 					/*
   1988 					 * if we get here we want to try
   1989 					 * again with the same mbuf, until
   1990 					 * the chip lets us transmit.
   1991 					 */
   1992 					txLoop++;
   1993 					if (txLoop > CS_OUTPUT_LOOP_MAX) {
   1994 						/* Free the mbuf chain */
   1995 						m_freem(pMbufChain);
   1996 						/*
   1997 						 * Transmission is not in
   1998 						 * progress
   1999 						 */
   2000 						sc->sc_txbusy = FALSE;
   2001 						/*
   2002 						 * Increment the output error
   2003 						 * count
   2004 						 */
   2005 						ifp->if_oerrors++;
   2006 						/*
   2007 						 * exit the routine and drop
   2008 						 * the packet.
   2009 						 */
   2010 						txLoop = 0;
   2011 						dropout = 1;
   2012 					}
   2013 				}
   2014 			}
   2015 		} while (txLoop);
   2016 	}
   2017 }
   2018 
   2019 void
   2020 cs_copy_tx_frame(struct cs_softc *sc, struct mbuf *m0)
   2021 {
   2022 	struct mbuf *m;
   2023 	int len, leftover, frameoff;
   2024 	u_int16_t dbuf;
   2025 	u_int8_t *p;
   2026 #ifdef DIAGNOSTIC
   2027 	u_int8_t *lim;
   2028 #endif
   2029 
   2030 	/* Initialize frame pointer and data port address */
   2031 	frameoff = PKTPG_TX_FRAME;
   2032 
   2033 	/* start out with no leftover data */
   2034 	leftover = 0;
   2035 	dbuf = 0;
   2036 
   2037 	/* Process the chain of mbufs */
   2038 	for (m = m0; m != NULL; m = m->m_next) {
   2039 		/*
   2040 		 * Process all of the data in a single mbuf.
   2041 		 */
   2042 		p = mtod(m, u_int8_t *);
   2043 		len = m->m_len;
   2044 #ifdef DIAGNOSTIC
   2045 		lim = p + len;
   2046 #endif
   2047 
   2048 		while (len > 0) {
   2049 			if (leftover) {
   2050 				/*
   2051 				 * Data left over (from mbuf or realignment).
   2052 				 * Buffer the next byte, and write it and
   2053 				 * the leftover data out.
   2054 				 */
   2055 				dbuf |= *p++ << 8;
   2056 				len--;
   2057 				if (sc->sc_memorymode) {
   2058 					CS_WRITE_PACKET_PAGE(sc, frameoff, dbuf);
   2059 					frameoff += 2;
   2060 				}
   2061 				else {
   2062 					CS_WRITE_PORT(sc, PORT_RXTX_DATA, dbuf);
   2063 				}
   2064 				leftover = 0;
   2065 			} else if ((long) p & 1) {
   2066 				/*
   2067 				 * Misaligned data.  Buffer the next byte.
   2068 				 */
   2069 				dbuf = *p++;
   2070 				len--;
   2071 				leftover = 1;
   2072 			} else {
   2073 				/*
   2074 				 * Aligned data.  This is the case we like.
   2075 				 *
   2076 				 * Write-region out as much as we can, then
   2077 				 * buffer the remaining byte (if any).
   2078 				 */
   2079 				leftover = len & 1;
   2080 				len &= ~1;
   2081 				if (sc->sc_memorymode) {
   2082 					MEM_WRITE_REGION_2(sc, frameoff,
   2083 						(u_int16_t *) p, len >> 1);
   2084 					frameoff += len;
   2085 				}
   2086 				else {
   2087 					IO_WRITE_MULTI_2(sc,
   2088 						PORT_RXTX_DATA, (u_int16_t *)p, len >> 1);
   2089 				}
   2090 				p += len;
   2091 
   2092 				if (leftover)
   2093 					dbuf = *p++;
   2094 				len = 0;
   2095 			}
   2096 		}
   2097 		if (len < 0)
   2098 			panic("cs_copy_tx_frame: negative len");
   2099 #ifdef DIAGNOSTIC
   2100 		if (p != lim)
   2101 			panic("cs_copy_tx_frame: p != lim");
   2102 #endif
   2103 	}
   2104 	if (leftover) {
   2105 		if (sc->sc_memorymode) {
   2106 			CS_WRITE_PACKET_PAGE(sc, frameoff, dbuf);
   2107 		}
   2108 		else {
   2109 			CS_WRITE_PORT(sc, PORT_RXTX_DATA, dbuf);
   2110 		}
   2111 	}
   2112 }
   2113 
   2114 static int
   2115 cs_enable(struct cs_softc *sc)
   2116 {
   2117 
   2118 	if (CS_IS_ENABLED(sc) == 0) {
   2119 		if (sc->sc_enable != NULL) {
   2120 			int error;
   2121 
   2122 			error = (*sc->sc_enable)(sc);
   2123 			if (error)
   2124 				return (error);
   2125 		}
   2126 		sc->sc_cfgflags |= CFGFLG_ENABLED;
   2127 	}
   2128 
   2129 	return (0);
   2130 }
   2131 
   2132 static void
   2133 cs_disable(struct cs_softc *sc)
   2134 {
   2135 
   2136 	if (CS_IS_ENABLED(sc)) {
   2137 		if (sc->sc_disable != NULL)
   2138 			(*sc->sc_disable)(sc);
   2139 
   2140 		sc->sc_cfgflags &= ~CFGFLG_ENABLED;
   2141 	}
   2142 }
   2143 
   2144 static void
   2145 cs_stop(struct ifnet *ifp, int disable)
   2146 {
   2147 	struct cs_softc *sc = ifp->if_softc;
   2148 
   2149 	CS_WRITE_PACKET_PAGE(sc, PKTPG_RX_CFG, 0);
   2150 	CS_WRITE_PACKET_PAGE(sc, PKTPG_TX_CFG, 0);
   2151 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUF_CFG, 0);
   2152 	CS_WRITE_PACKET_PAGE(sc, PKTPG_BUS_CTL, 0);
   2153 
   2154 	if (disable) {
   2155 		cs_disable(sc);
   2156 	}
   2157 
   2158 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2159 }
   2160 
   2161 int
   2162 cs_activate(device_t self, enum devact act)
   2163 {
   2164 	struct cs_softc *sc = device_private(self);
   2165 
   2166 	switch (act) {
   2167 	case DVACT_DEACTIVATE:
   2168 		if_deactivate(&sc->sc_ethercom.ec_if);
   2169 		return 0;
   2170 	default:
   2171 		return EOPNOTSUPP;
   2172 	}
   2173 }
   2174