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