Home | History | Annotate | Line # | Download | only in pcmcia
if_cnw.c revision 1.2.2.3
      1  1.2.2.2    bouyer /*	$NetBSD: if_cnw.c,v 1.2.2.3 2001/01/05 17:36:22 bouyer Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5      1.1  christos  * All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  christos  * by Michael Eriksson.
      9      1.1  christos  *
     10      1.1  christos  * Redistribution and use in source and binary forms, with or without
     11      1.1  christos  * modification, are permitted provided that the following conditions
     12      1.1  christos  * are met:
     13      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  christos  *    documentation and/or other materials provided with the distribution.
     18      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19      1.1  christos  *    must display the following acknowledgement:
     20      1.1  christos  *	This product includes software developed by the NetBSD
     21      1.1  christos  *	Foundation, Inc. and its contributors.
     22      1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1  christos  *    contributors may be used to endorse or promote products derived
     24      1.1  christos  *    from this software without specific prior written permission.
     25      1.1  christos  *
     26      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1  christos  */
     38      1.1  christos 
     39      1.1  christos /*
     40  1.2.2.1    bouyer  * Copyright (c) 1996, 1997 Berkeley Software Design, Inc.
     41  1.2.2.1    bouyer  * All rights reserved.
     42  1.2.2.1    bouyer  *
     43  1.2.2.1    bouyer  * Redistribution and use in source and binary forms, with or without
     44  1.2.2.1    bouyer  * modification, are permitted provided that this notice is retained,
     45  1.2.2.1    bouyer  * the conditions in the following notices are met, and terms applying
     46  1.2.2.1    bouyer  * to contributors in the following notices also apply to Berkeley
     47  1.2.2.1    bouyer  * Software Design, Inc.
     48  1.2.2.1    bouyer  *
     49  1.2.2.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     50  1.2.2.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     51  1.2.2.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.2.2.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     53  1.2.2.1    bouyer  *    documentation and/or other materials provided with the distribution.
     54  1.2.2.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     55  1.2.2.1    bouyer  *    must display the following acknowledgement:
     56  1.2.2.1    bouyer  *      This product includes software developed by
     57  1.2.2.1    bouyer  *	Berkeley Software Design, Inc.
     58  1.2.2.1    bouyer  * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
     59  1.2.2.1    bouyer  *    of its contributors may be used to endorse or promote products derived
     60  1.2.2.1    bouyer  *    from this software without specific prior written permission.
     61  1.2.2.1    bouyer  *
     62  1.2.2.1    bouyer  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
     63  1.2.2.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  1.2.2.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  1.2.2.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
     66  1.2.2.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  1.2.2.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  1.2.2.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  1.2.2.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  1.2.2.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  1.2.2.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  1.2.2.1    bouyer  * SUCH DAMAGE.
     73  1.2.2.1    bouyer  *
     74  1.2.2.1    bouyer  * Paul Borman, December 1996
     75  1.2.2.1    bouyer  *
     76  1.2.2.1    bouyer  * This driver is derived from a generic frame work which is
     77  1.2.2.1    bouyer  * Copyright(c) 1994,1995,1996
     78  1.2.2.1    bouyer  * Yoichi Shinoda, Yoshitaka Tokugawa, WIDE Project, Wildboar Project
     79  1.2.2.1    bouyer  * and Foretune.  All rights reserved.
     80  1.2.2.1    bouyer  *
     81  1.2.2.1    bouyer  * A linux driver was used as the "hardware reference manual" (i.e.,
     82  1.2.2.1    bouyer  * to determine registers and a general outline of how the card works)
     83  1.2.2.1    bouyer  * That driver is publically available and copyright
     84  1.2.2.1    bouyer  *
     85  1.2.2.1    bouyer  * John Markus Bjrndalen
     86  1.2.2.1    bouyer  * Department of Computer Science
     87  1.2.2.1    bouyer  * University of Troms
     88  1.2.2.1    bouyer  * Norway
     89  1.2.2.1    bouyer  * johnm (at) staff.cs.uit.no, http://www.cs.uit.no/~johnm/
     90  1.2.2.1    bouyer  */
     91  1.2.2.1    bouyer 
     92  1.2.2.1    bouyer /*
     93      1.1  christos  * This is a driver for the Xircom CreditCard Netwave (also known as
     94      1.1  christos  * the Netwave Airsurfer) wireless LAN PCMCIA adapter.
     95      1.1  christos  *
     96      1.1  christos  * When this driver was developed, the Linux Netwave driver was used
     97      1.1  christos  * as a hardware manual. That driver is Copyright (c) 1997 University
     98      1.1  christos  * of Troms, Norway. It is part of the Linix pcmcia-cs package that
     99      1.1  christos  * can be found at
    100      1.1  christos  * http://hyper.stanford.edu/HyperNews/get/pcmcia/home.html. The most
    101      1.1  christos  * recent version of the pcmcia-cs package when this driver was
    102      1.1  christos  * written was 3.0.6.
    103      1.1  christos  *
    104      1.1  christos  * Unfortunately, a lot of explicit numeric constants were used in the
    105      1.1  christos  * Linux driver. I have tried to use symbolic names whenever possible,
    106      1.1  christos  * but since I don't have any real hardware documentation, there's
    107      1.1  christos  * still one or two "magic numbers" :-(.
    108      1.1  christos  *
    109      1.1  christos  * Driver limitations: This driver doesn't do multicasting or receiver
    110      1.1  christos  * promiscuity, because of missing hardware documentation. I couldn't
    111      1.1  christos  * get receiver promiscuity to work, and I haven't even tried
    112      1.1  christos  * multicast. Volunteers are welcome, of course :-).
    113      1.1  christos  */
    114      1.1  christos 
    115      1.1  christos #include "opt_inet.h"
    116      1.1  christos #include "bpfilter.h"
    117      1.1  christos 
    118      1.1  christos #include <sys/param.h>
    119      1.1  christos #include <sys/systm.h>
    120      1.1  christos #include <sys/device.h>
    121      1.1  christos #include <sys/socket.h>
    122      1.1  christos #include <sys/mbuf.h>
    123      1.1  christos #include <sys/ioctl.h>
    124  1.2.2.1    bouyer #include <sys/proc.h>
    125  1.2.2.1    bouyer 
    126  1.2.2.1    bouyer #include <net/if.h>
    127      1.1  christos 
    128      1.1  christos #include <dev/pcmcia/if_cnwreg.h>
    129  1.2.2.1    bouyer #include <dev/pcmcia/if_cnwioctl.h>
    130      1.1  christos 
    131      1.1  christos #include <dev/pcmcia/pcmciareg.h>
    132      1.1  christos #include <dev/pcmcia/pcmciavar.h>
    133      1.1  christos #include <dev/pcmcia/pcmciadevs.h>
    134      1.1  christos 
    135      1.1  christos #include <net/if_dl.h>
    136      1.1  christos #include <net/if_ether.h>
    137      1.1  christos 
    138      1.1  christos #ifdef INET
    139      1.1  christos #include <netinet/in.h>
    140      1.1  christos #include <netinet/in_systm.h>
    141      1.1  christos #include <netinet/in_var.h>
    142      1.1  christos #include <netinet/ip.h>
    143      1.1  christos #include <netinet/if_inarp.h>
    144      1.1  christos #endif
    145      1.1  christos 
    146      1.1  christos #if NBPFILTER > 0
    147      1.1  christos #include <net/bpf.h>
    148      1.1  christos #include <net/bpfdesc.h>
    149      1.1  christos #endif
    150      1.1  christos 
    151      1.1  christos /*
    152      1.1  christos  * Let these be patchable variables, initialized from macros that can
    153      1.1  christos  * be set in the kernel config file. Someone with lots of spare time
    154      1.1  christos  * could probably write a nice Netwave configuration program to do
    155      1.1  christos  * this a little bit more elegantly :-).
    156      1.1  christos  */
    157      1.1  christos #ifndef CNW_DOMAIN
    158      1.1  christos #define CNW_DOMAIN	0x100
    159      1.1  christos #endif
    160      1.1  christos int cnw_domain = CNW_DOMAIN;		/* Domain */
    161      1.1  christos #ifndef CNW_SCRAMBLEKEY
    162      1.1  christos #define CNW_SCRAMBLEKEY 0
    163      1.1  christos #endif
    164      1.1  christos int cnw_skey = CNW_SCRAMBLEKEY;		/* Scramble key */
    165      1.1  christos 
    166  1.2.2.1    bouyer /*
    167  1.2.2.1    bouyer  * The card appears to work much better when we only allow one packet
    168  1.2.2.1    bouyer  * "in the air" at a time.  This is done by not allowing another packet
    169  1.2.2.1    bouyer  * on the card, even if there is room.  Turning this off will allow the
    170  1.2.2.1    bouyer  * driver to stuff packets on the card as soon as a transmit buffer is
    171  1.2.2.1    bouyer  * available.  This does increase the number of collisions, though.
    172  1.2.2.1    bouyer  * We can que a second packet if there are transmit buffers available,
    173  1.2.2.1    bouyer  * but we do not actually send the packet until the last packet has
    174  1.2.2.1    bouyer  * been written.
    175  1.2.2.1    bouyer  */
    176  1.2.2.1    bouyer #define	ONE_AT_A_TIME
    177  1.2.2.1    bouyer 
    178  1.2.2.1    bouyer /*
    179  1.2.2.1    bouyer  * Netwave cards choke if we try to use io memory address >= 0x400.
    180  1.2.2.1    bouyer  * Even though, CIS tuple does not talk about this.
    181  1.2.2.1    bouyer  * Use memory mapped access.
    182  1.2.2.1    bouyer  */
    183  1.2.2.1    bouyer #define MEMORY_MAPPED
    184      1.1  christos 
    185      1.1  christos int	cnw_match __P((struct device *, struct cfdata *, void *));
    186      1.1  christos void	cnw_attach __P((struct device *, struct device *, void *));
    187  1.2.2.1    bouyer int	cnw_detach __P((struct device *, int));
    188  1.2.2.1    bouyer 
    189  1.2.2.1    bouyer int	cnw_activate __P((struct device *, enum devact));
    190      1.1  christos 
    191      1.1  christos struct cnw_softc {
    192      1.1  christos 	struct device sc_dev;		    /* Device glue (must be first) */
    193      1.1  christos 	struct ethercom sc_ethercom;	    /* Ethernet common part */
    194      1.1  christos 	int sc_domain;			    /* Netwave domain */
    195      1.1  christos 	int sc_skey;			    /* Netwave scramble key */
    196  1.2.2.1    bouyer 	struct cnwstats sc_stats;
    197      1.1  christos 
    198      1.1  christos 	/* PCMCIA-specific stuff */
    199      1.1  christos 	struct pcmcia_function *sc_pf;	    /* PCMCIA function */
    200  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    201      1.1  christos 	struct pcmcia_io_handle sc_pcioh;   /* PCMCIA I/O space handle */
    202      1.1  christos 	int sc_iowin;			    /*   ...window */
    203      1.1  christos 	bus_space_tag_t sc_iot;		    /*   ...bus_space tag */
    204      1.1  christos 	bus_space_handle_t sc_ioh;	    /*   ...bus_space handle */
    205  1.2.2.1    bouyer #endif
    206      1.1  christos 	struct pcmcia_mem_handle sc_pcmemh; /* PCMCIA memory handle */
    207      1.1  christos 	bus_addr_t sc_memoff;		    /*   ...offset */
    208      1.1  christos 	int sc_memwin;			    /*   ...window */
    209      1.1  christos 	bus_space_tag_t sc_memt;	    /*   ...bus_space tag */
    210      1.1  christos 	bus_space_handle_t sc_memh;	    /*   ...bus_space handle */
    211      1.1  christos 	void *sc_ih;			    /* Interrupt cookie */
    212  1.2.2.1    bouyer 	struct timeval sc_txlast;	    /* When the last xmit was made */
    213  1.2.2.1    bouyer 	int sc_active;			    /* Currently xmitting a packet */
    214  1.2.2.1    bouyer 
    215  1.2.2.1    bouyer 	int sc_resource;		    /* Resources alloc'ed on attach */
    216  1.2.2.1    bouyer #define CNW_RES_PCIC	1
    217  1.2.2.1    bouyer #define CNW_RES_IO	2
    218  1.2.2.1    bouyer #define CNW_RES_MEM	4
    219  1.2.2.1    bouyer #define CNW_RES_NET	8
    220      1.1  christos };
    221      1.1  christos 
    222      1.1  christos struct cfattach cnw_ca = {
    223  1.2.2.1    bouyer 	sizeof(struct cnw_softc), cnw_match, cnw_attach, cnw_detach,
    224  1.2.2.1    bouyer 		cnw_activate
    225      1.1  christos };
    226      1.1  christos 
    227      1.1  christos 
    228      1.1  christos void cnw_reset __P((struct cnw_softc *));
    229      1.1  christos void cnw_init __P((struct cnw_softc *));
    230      1.1  christos int cnw_enable __P((struct cnw_softc *sc));
    231      1.1  christos void cnw_disable __P((struct cnw_softc *sc));
    232      1.1  christos void cnw_config __P((struct cnw_softc *sc, u_int8_t *));
    233      1.1  christos void cnw_start __P((struct ifnet *));
    234      1.1  christos void cnw_transmit __P((struct cnw_softc *, struct mbuf *));
    235      1.1  christos struct mbuf *cnw_read __P((struct cnw_softc *));
    236      1.1  christos void cnw_recv __P((struct cnw_softc *));
    237      1.1  christos int cnw_intr __P((void *arg));
    238      1.1  christos int cnw_ioctl __P((struct ifnet *, u_long, caddr_t));
    239      1.1  christos void cnw_watchdog __P((struct ifnet *));
    240  1.2.2.1    bouyer static int cnw_setdomain __P((struct cnw_softc *, int));
    241  1.2.2.1    bouyer static int cnw_setkey __P((struct cnw_softc *, int));
    242      1.1  christos 
    243      1.1  christos /* ---------------------------------------------------------------- */
    244      1.1  christos 
    245      1.1  christos /* Help routines */
    246      1.1  christos static int wait_WOC __P((struct cnw_softc *, int));
    247      1.1  christos static int read16 __P((struct cnw_softc *, int));
    248      1.1  christos static int cnw_cmd __P((struct cnw_softc *, int, int, int, int));
    249      1.1  christos 
    250      1.1  christos /*
    251      1.1  christos  * Wait until the WOC (Write Operation Complete) bit in the
    252      1.1  christos  * ASR (Adapter Status Register) is asserted.
    253      1.1  christos  */
    254      1.1  christos static int
    255      1.1  christos wait_WOC(sc, line)
    256      1.1  christos 	struct cnw_softc *sc;
    257      1.1  christos 	int line;
    258      1.1  christos {
    259      1.1  christos 	int i, asr;
    260      1.1  christos 
    261      1.1  christos 	for (i = 0; i < 5000; i++) {
    262  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    263      1.1  christos 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    264  1.2.2.1    bouyer #else
    265  1.2.2.1    bouyer 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    266  1.2.2.1    bouyer 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    267  1.2.2.1    bouyer #endif
    268      1.1  christos 		if (asr & CNW_ASR_WOC)
    269      1.1  christos 			return (0);
    270      1.1  christos 		DELAY(100);
    271      1.1  christos 	}
    272      1.1  christos 	if (line > 0)
    273      1.1  christos 		printf("%s: wedged at line %d\n", sc->sc_dev.dv_xname, line);
    274      1.1  christos 	return (1);
    275      1.1  christos }
    276      1.1  christos #define WAIT_WOC(sc) wait_WOC(sc, __LINE__)
    277      1.1  christos 
    278      1.1  christos 
    279      1.1  christos /*
    280      1.1  christos  * Read a 16 bit value from the card.
    281      1.1  christos  */
    282      1.1  christos static int
    283      1.1  christos read16(sc, offset)
    284      1.1  christos 	struct cnw_softc *sc;
    285      1.1  christos 	int offset;
    286      1.1  christos {
    287      1.1  christos 	int hi, lo;
    288      1.1  christos 	int offs = sc->sc_memoff + offset;
    289      1.1  christos 
    290      1.1  christos 	/* This could presumably be done more efficient with
    291      1.1  christos 	 * bus_space_read_2(), but I don't know anything about the
    292      1.1  christos 	 * byte sex guarantees... Besides, this is pretty cheap as
    293      1.1  christos 	 * well :-)
    294      1.1  christos 	 */
    295      1.1  christos 	lo = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs);
    296      1.1  christos 	hi = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs + 1);
    297      1.1  christos 	return ((hi << 8) | lo);
    298      1.1  christos }
    299      1.1  christos 
    300      1.1  christos 
    301      1.1  christos /*
    302      1.1  christos  * Send a command to the card by writing it to the command buffer.
    303      1.1  christos  */
    304      1.1  christos int
    305      1.1  christos cnw_cmd(sc, cmd, count, arg1, arg2)
    306      1.1  christos 	struct cnw_softc *sc;
    307      1.1  christos 	int cmd, count, arg1, arg2;
    308      1.1  christos {
    309      1.1  christos 	int ptr = sc->sc_memoff + CNW_EREG_CB;
    310      1.1  christos 
    311      1.1  christos 	if (wait_WOC(sc, 0)) {
    312      1.1  christos 		printf("%s: wedged when issuing cmd 0x%x\n",
    313      1.1  christos 		    sc->sc_dev.dv_xname, cmd);
    314      1.1  christos 		/*
    315      1.1  christos 		 * We'll continue anyway, as that's probably the best
    316      1.1  christos 		 * thing we can do; at least the user knows there's a
    317      1.1  christos 		 * problem, and can reset the interface with ifconfig
    318      1.1  christos 		 * down/up.
    319      1.1  christos 		 */
    320      1.1  christos 	}
    321      1.1  christos 
    322      1.1  christos 	bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd);
    323      1.1  christos 	if (count > 0) {
    324      1.1  christos 		bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1);
    325      1.1  christos 		if (count > 1)
    326      1.1  christos 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    327      1.1  christos 			    ptr + 2, arg2);
    328      1.1  christos 	}
    329      1.1  christos 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    330      1.1  christos 	    ptr + count + 1, CNW_CMD_EOC);
    331      1.1  christos 	return (0);
    332      1.1  christos }
    333      1.1  christos #define CNW_CMD0(sc, cmd) \
    334      1.1  christos     do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
    335      1.1  christos #define CNW_CMD1(sc, cmd, arg1)	\
    336      1.1  christos     do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
    337      1.1  christos #define CNW_CMD2(sc, cmd, arg1, arg2) \
    338      1.1  christos     do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
    339      1.1  christos 
    340      1.1  christos /* ---------------------------------------------------------------- */
    341      1.1  christos 
    342      1.1  christos /*
    343      1.1  christos  * Reset the hardware.
    344      1.1  christos  */
    345      1.1  christos void
    346      1.1  christos cnw_reset(sc)
    347      1.1  christos 	struct cnw_softc *sc;
    348      1.1  christos {
    349      1.1  christos #ifdef CNW_DEBUG
    350      1.1  christos 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    351      1.1  christos 		printf("%s: resetting\n", sc->sc_dev.dv_xname);
    352      1.1  christos #endif
    353      1.1  christos 	wait_WOC(sc, 0);
    354  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    355      1.1  christos 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET);
    356  1.2.2.1    bouyer #else
    357  1.2.2.1    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    358  1.2.2.1    bouyer 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, CNW_PMR_RESET);
    359  1.2.2.1    bouyer #endif
    360      1.1  christos 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    361      1.1  christos 	    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC);
    362  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    363      1.1  christos 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0);
    364  1.2.2.1    bouyer #else
    365  1.2.2.1    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    366  1.2.2.1    bouyer 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, 0);
    367  1.2.2.1    bouyer #endif
    368      1.1  christos }
    369      1.1  christos 
    370      1.1  christos 
    371      1.1  christos /*
    372      1.1  christos  * Initialize the card.
    373      1.1  christos  */
    374      1.1  christos void
    375      1.1  christos cnw_init(sc)
    376      1.1  christos 	struct cnw_softc *sc;
    377      1.1  christos {
    378  1.2.2.1    bouyer 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    379  1.2.2.1    bouyer 	const u_int8_t rxmode =
    380  1.2.2.1    bouyer 	    CNW_RXCONF_RXENA | CNW_RXCONF_BCAST | CNW_RXCONF_AMP;
    381  1.2.2.1    bouyer 
    382      1.1  christos 	/* Reset the card */
    383      1.1  christos 	cnw_reset(sc);
    384      1.1  christos 
    385      1.1  christos 	/* Issue a NOP to check the card */
    386      1.1  christos 	CNW_CMD0(sc, CNW_CMD_NOP);
    387      1.1  christos 
    388      1.1  christos 	/* Set up receive configuration */
    389  1.2.2.1    bouyer 	CNW_CMD1(sc, CNW_CMD_SRC,
    390  1.2.2.1    bouyer 	    rxmode | ((ifp->if_flags & IFF_PROMISC) ? CNW_RXCONF_PRO : 0));
    391      1.1  christos 
    392      1.1  christos 	/* Set up transmit configuration */
    393      1.1  christos 	CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA);
    394      1.1  christos 
    395      1.1  christos 	/* Set domain */
    396      1.1  christos 	CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8);
    397      1.1  christos 
    398      1.1  christos 	/* Set scramble key */
    399      1.1  christos 	CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8);
    400      1.1  christos 
    401      1.1  christos 	/* Enable interrupts */
    402      1.1  christos 	WAIT_WOC(sc);
    403  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    404      1.1  christos 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    405      1.1  christos 	    CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1);
    406  1.2.2.1    bouyer #else
    407  1.2.2.1    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    408  1.2.2.1    bouyer 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_IMR,
    409  1.2.2.1    bouyer 	    CNW_IMR_IENA | CNW_IMR_RFU1);
    410  1.2.2.1    bouyer #endif
    411      1.1  christos 
    412      1.1  christos 	/* Enable receiver */
    413      1.1  christos 	CNW_CMD0(sc, CNW_CMD_ER);
    414      1.1  christos 
    415      1.1  christos 	/* "Set the IENA bit in COR" */
    416      1.1  christos 	WAIT_WOC(sc);
    417  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    418      1.1  christos 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR,
    419      1.1  christos 	    CNW_COR_IENA | CNW_COR_LVLREQ);
    420  1.2.2.1    bouyer #else
    421  1.2.2.1    bouyer 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    422  1.2.2.1    bouyer 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_COR,
    423  1.2.2.1    bouyer 	    CNW_COR_IENA | CNW_COR_LVLREQ);
    424  1.2.2.1    bouyer #endif
    425      1.1  christos }
    426      1.1  christos 
    427      1.1  christos 
    428      1.1  christos /*
    429      1.1  christos  * Enable and initialize the card.
    430      1.1  christos  */
    431      1.1  christos int
    432      1.1  christos cnw_enable(sc)
    433      1.1  christos 	struct cnw_softc *sc;
    434      1.1  christos {
    435      1.1  christos 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    436      1.1  christos 
    437  1.2.2.1    bouyer 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    438  1.2.2.1    bouyer 		return (0);
    439  1.2.2.1    bouyer 
    440      1.1  christos 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc);
    441      1.1  christos 	if (sc->sc_ih == NULL) {
    442      1.1  christos 		printf("%s: couldn't establish interrupt handler\n",
    443      1.1  christos 		    sc->sc_dev.dv_xname);
    444      1.1  christos 		return (EIO);
    445      1.1  christos 	}
    446      1.1  christos 	if (pcmcia_function_enable(sc->sc_pf) != 0) {
    447      1.1  christos 		printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
    448      1.1  christos 		return (EIO);
    449      1.1  christos 	}
    450  1.2.2.1    bouyer 	sc->sc_resource |= CNW_RES_PCIC;
    451      1.1  christos 	cnw_init(sc);
    452  1.2.2.1    bouyer 	ifp->if_flags &= ~IFF_OACTIVE;
    453      1.1  christos 	ifp->if_flags |= IFF_RUNNING;
    454      1.1  christos 	return (0);
    455      1.1  christos }
    456      1.1  christos 
    457      1.1  christos 
    458      1.1  christos /*
    459      1.1  christos  * Stop and disable the card.
    460      1.1  christos  */
    461      1.1  christos void
    462      1.1  christos cnw_disable(sc)
    463      1.1  christos 	struct cnw_softc *sc;
    464      1.1  christos {
    465      1.1  christos 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    466      1.1  christos 
    467  1.2.2.1    bouyer 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    468  1.2.2.1    bouyer 		return;
    469  1.2.2.1    bouyer 
    470      1.1  christos 	pcmcia_function_disable(sc->sc_pf);
    471  1.2.2.1    bouyer 	sc->sc_resource &= ~CNW_RES_PCIC;
    472      1.1  christos 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    473      1.1  christos 	ifp->if_flags &= ~IFF_RUNNING;
    474      1.1  christos 	ifp->if_timer = 0;
    475      1.1  christos }
    476      1.1  christos 
    477      1.1  christos 
    478      1.1  christos /*
    479      1.1  christos  * Match the hardware we handle.
    480      1.1  christos  */
    481      1.1  christos int
    482      1.1  christos cnw_match(parent, match, aux)
    483      1.1  christos 	struct device *parent;
    484      1.1  christos 	struct cfdata *match;
    485      1.1  christos 	void *aux;
    486      1.1  christos {
    487      1.1  christos 	struct pcmcia_attach_args *pa = aux;
    488      1.1  christos 
    489  1.2.2.1    bouyer 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    490  1.2.2.1    bouyer 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801)
    491  1.2.2.1    bouyer 		return 1;
    492  1.2.2.1    bouyer 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    493  1.2.2.1    bouyer 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802)
    494  1.2.2.1    bouyer 		return 1;
    495  1.2.2.1    bouyer 	return 0;
    496      1.1  christos }
    497      1.1  christos 
    498      1.1  christos 
    499      1.1  christos /*
    500      1.1  christos  * Attach the card.
    501      1.1  christos  */
    502      1.1  christos void
    503      1.1  christos cnw_attach(parent, self, aux)
    504      1.1  christos 	struct device  *parent, *self;
    505      1.1  christos 	void           *aux;
    506      1.1  christos {
    507      1.1  christos 	struct cnw_softc *sc = (void *) self;
    508      1.1  christos 	struct pcmcia_attach_args *pa = aux;
    509      1.1  christos 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    510      1.1  christos 	u_int8_t macaddr[ETHER_ADDR_LEN];
    511      1.1  christos 	int i;
    512  1.2.2.1    bouyer 	bus_size_t memsize;
    513  1.2.2.1    bouyer 
    514  1.2.2.1    bouyer 	sc->sc_resource = 0;
    515      1.1  christos 
    516      1.1  christos 	/* Enable the card */
    517      1.1  christos 	sc->sc_pf = pa->pf;
    518      1.1  christos 	pcmcia_function_init(sc->sc_pf, sc->sc_pf->cfe_head.sqh_first);
    519      1.1  christos 	if (pcmcia_function_enable(sc->sc_pf)) {
    520      1.1  christos 		printf(": function enable failed\n");
    521      1.1  christos 		return;
    522      1.1  christos 	}
    523  1.2.2.1    bouyer 	sc->sc_resource |= CNW_RES_PCIC;
    524      1.1  christos 
    525      1.1  christos 	/* Map I/O register and "memory" */
    526  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    527      1.1  christos 	if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
    528      1.1  christos 	    &sc->sc_pcioh) != 0) {
    529      1.1  christos 		printf(": can't allocate i/o space\n");
    530  1.2.2.1    bouyer 		goto fail;
    531      1.1  christos 	}
    532      1.1  christos 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, 0,
    533      1.1  christos 	    CNW_IO_SIZE, &sc->sc_pcioh, &sc->sc_iowin) != 0) {
    534      1.1  christos 		printf(": can't map i/o space\n");
    535  1.2.2.1    bouyer 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    536  1.2.2.1    bouyer 		goto fail;
    537      1.1  christos 	}
    538      1.1  christos 	sc->sc_iot = sc->sc_pcioh.iot;
    539      1.1  christos 	sc->sc_ioh = sc->sc_pcioh.ioh;
    540  1.2.2.1    bouyer 	sc->sc_resource |= CNW_RES_IO;
    541  1.2.2.1    bouyer #endif
    542  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    543  1.2.2.1    bouyer 	memsize = CNW_MEM_SIZE;
    544  1.2.2.1    bouyer #else
    545  1.2.2.1    bouyer 	memsize = CNW_MEM_SIZE + CNW_IOM_SIZE;
    546  1.2.2.1    bouyer #endif
    547  1.2.2.1    bouyer 	if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) {
    548      1.1  christos 		printf(": can't allocate memory\n");
    549  1.2.2.1    bouyer 		goto fail;
    550      1.1  christos 	}
    551  1.2.2.1    bouyer 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON,
    552  1.2.2.1    bouyer 	    CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff,
    553      1.1  christos 	    &sc->sc_memwin) != 0) {
    554      1.1  christos 		printf(": can't map memory\n");
    555  1.2.2.1    bouyer 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
    556  1.2.2.1    bouyer 		goto fail;
    557      1.1  christos 	}
    558      1.1  christos 	sc->sc_memt = sc->sc_pcmemh.memt;
    559      1.1  christos 	sc->sc_memh = sc->sc_pcmemh.memh;
    560  1.2.2.1    bouyer 	sc->sc_resource |= CNW_RES_MEM;
    561  1.2.2.1    bouyer 	switch (pa->product) {
    562  1.2.2.1    bouyer 	case PCMCIA_PRODUCT_XIRCOM_CNW_801:
    563  1.2.2.1    bouyer 		printf(": %s\n", PCMCIA_STR_XIRCOM_CNW_801);
    564  1.2.2.1    bouyer 		break;
    565  1.2.2.1    bouyer 	case PCMCIA_PRODUCT_XIRCOM_CNW_802:
    566  1.2.2.1    bouyer 		printf(": %s\n", PCMCIA_STR_XIRCOM_CNW_802);
    567  1.2.2.1    bouyer 		break;
    568  1.2.2.1    bouyer 	}
    569      1.1  christos 
    570      1.1  christos 	/* Finish setup of softc */
    571      1.1  christos 	sc->sc_domain = cnw_domain;
    572      1.1  christos 	sc->sc_skey = cnw_skey;
    573      1.1  christos 
    574      1.1  christos 	/* Get MAC address */
    575      1.1  christos 	cnw_reset(sc);
    576      1.1  christos 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    577      1.1  christos 		macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    578      1.1  christos 		    sc->sc_memoff + CNW_EREG_PA + i);
    579      1.1  christos 	printf("%s: address %s\n", sc->sc_dev.dv_xname,
    580      1.1  christos 	    ether_sprintf(macaddr));
    581      1.1  christos 
    582      1.1  christos 	/* Set up ifnet structure */
    583      1.1  christos 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    584      1.1  christos 	ifp->if_softc = sc;
    585      1.1  christos 	ifp->if_start = cnw_start;
    586      1.1  christos 	ifp->if_ioctl = cnw_ioctl;
    587      1.1  christos 	ifp->if_watchdog = cnw_watchdog;
    588  1.2.2.1    bouyer 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX |
    589  1.2.2.1    bouyer 	    IFF_NOTRAILERS;
    590  1.2.2.3    bouyer 	IFQ_SET_READY(&ifp->if_snd);
    591      1.1  christos 
    592      1.1  christos 	/* Attach the interface */
    593      1.1  christos 	if_attach(ifp);
    594      1.1  christos 	ether_ifattach(ifp, macaddr);
    595  1.2.2.2    bouyer 
    596  1.2.2.1    bouyer 	sc->sc_resource |= CNW_RES_NET;
    597  1.2.2.1    bouyer 
    598  1.2.2.1    bouyer 	ifp->if_baudrate = IF_Mbps(1);
    599      1.1  christos 
    600      1.1  christos 	/* Disable the card now, and turn it on when the interface goes up */
    601      1.1  christos 	pcmcia_function_disable(sc->sc_pf);
    602  1.2.2.1    bouyer 	sc->sc_resource &= ~CNW_RES_PCIC;
    603  1.2.2.1    bouyer 	return;
    604  1.2.2.1    bouyer 
    605  1.2.2.1    bouyer fail:
    606  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    607  1.2.2.1    bouyer 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
    608  1.2.2.1    bouyer 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
    609  1.2.2.1    bouyer 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    610  1.2.2.1    bouyer 		sc->sc_resource &= ~CNW_RES_IO;
    611  1.2.2.1    bouyer 	}
    612  1.2.2.1    bouyer #endif
    613  1.2.2.1    bouyer 	if ((sc->sc_resource & CNW_RES_PCIC) != 0) {
    614  1.2.2.1    bouyer 		pcmcia_function_disable(sc->sc_pf);
    615  1.2.2.1    bouyer 		sc->sc_resource &= ~CNW_RES_PCIC;
    616  1.2.2.1    bouyer 	}
    617      1.1  christos }
    618      1.1  christos 
    619      1.1  christos /*
    620      1.1  christos  * Start outputting on the interface.
    621      1.1  christos  */
    622      1.1  christos void
    623      1.1  christos cnw_start(ifp)
    624      1.1  christos 	struct ifnet *ifp;
    625      1.1  christos {
    626      1.1  christos 	struct cnw_softc *sc = ifp->if_softc;
    627      1.1  christos 	struct mbuf *m0;
    628  1.2.2.1    bouyer 	int lif;
    629      1.1  christos 	int asr;
    630  1.2.2.1    bouyer #ifdef ONE_AT_A_TIME
    631  1.2.2.1    bouyer 	struct timeval now;
    632  1.2.2.1    bouyer #endif
    633      1.1  christos 
    634      1.1  christos #ifdef CNW_DEBUG
    635      1.1  christos 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    636      1.1  christos 		printf("%s: cnw_start\n", ifp->if_xname);
    637  1.2.2.1    bouyer 	if (ifp->if_flags & IFF_OACTIVE)
    638  1.2.2.1    bouyer 		printf("%s: cnw_start reentered\n", ifp->if_xname);
    639      1.1  christos #endif
    640      1.1  christos 
    641  1.2.2.1    bouyer 	ifp->if_flags |= IFF_OACTIVE;
    642  1.2.2.1    bouyer 
    643      1.1  christos 	for (;;) {
    644  1.2.2.1    bouyer #ifdef ONE_AT_A_TIME
    645  1.2.2.1    bouyer 		microtime(&now);
    646  1.2.2.1    bouyer 		now.tv_sec -= sc->sc_txlast.tv_sec;
    647  1.2.2.1    bouyer 		now.tv_usec -= sc->sc_txlast.tv_usec;
    648  1.2.2.1    bouyer 		if (now.tv_usec < 0) {
    649  1.2.2.1    bouyer 			now.tv_usec += 1000000;
    650  1.2.2.1    bouyer 			now.tv_sec--;
    651  1.2.2.1    bouyer 		}
    652  1.2.2.1    bouyer 
    653  1.2.2.1    bouyer 		/*
    654  1.2.2.1    bouyer 		 * Don't ship this packet out until the last
    655  1.2.2.1    bouyer 		 * packet has left the building.
    656  1.2.2.1    bouyer 		 * If we have not tried to send a packet for 1/5
    657  1.2.2.1    bouyer 		 * a second then we assume we lost an interrupt,
    658  1.2.2.1    bouyer 		 * lets go on and send the next packet anyhow.
    659  1.2.2.1    bouyer 		 *
    660  1.2.2.1    bouyer 		 * I suppose we could check to see if it is okay
    661  1.2.2.1    bouyer 		 * to put additional packets on the card (beyond
    662  1.2.2.1    bouyer 		 * the one already waiting to be sent) but I don't
    663  1.2.2.1    bouyer 		 * think we would get any improvement in speed as
    664  1.2.2.1    bouyer 		 * we should have ample time to put the next packet
    665  1.2.2.1    bouyer 		 * on while this one is going out.
    666  1.2.2.1    bouyer 		 */
    667  1.2.2.1    bouyer 		if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000)
    668  1.2.2.1    bouyer 			break;
    669  1.2.2.1    bouyer #endif
    670  1.2.2.1    bouyer 
    671  1.2.2.1    bouyer 		/* Make sure the link integrity field is on */
    672  1.2.2.1    bouyer 		WAIT_WOC(sc);
    673  1.2.2.1    bouyer 		lif = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    674  1.2.2.1    bouyer 		    sc->sc_memoff + CNW_EREG_LIF);
    675  1.2.2.1    bouyer 		if (lif == 0) {
    676  1.2.2.1    bouyer #ifdef CNW_DEBUG
    677  1.2.2.1    bouyer 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    678  1.2.2.1    bouyer 				printf("%s: link integrity %d\n", lif);
    679  1.2.2.1    bouyer #endif
    680  1.2.2.1    bouyer 			break;
    681  1.2.2.1    bouyer 		}
    682  1.2.2.1    bouyer 
    683      1.1  christos 		/* Is there any buffer space available on the card? */
    684      1.1  christos 		WAIT_WOC(sc);
    685  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    686      1.1  christos 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    687  1.2.2.1    bouyer #else
    688  1.2.2.1    bouyer 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    689  1.2.2.1    bouyer 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    690  1.2.2.1    bouyer #endif
    691      1.1  christos 		if (!(asr & CNW_ASR_TXBA)) {
    692      1.1  christos #ifdef CNW_DEBUG
    693      1.1  christos 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    694      1.1  christos 				printf("%s: no buffer space\n", ifp->if_xname);
    695      1.1  christos #endif
    696  1.2.2.1    bouyer 			break;
    697      1.1  christos 		}
    698      1.1  christos 
    699  1.2.2.1    bouyer 		sc->sc_stats.nws_tx++;
    700  1.2.2.1    bouyer 
    701  1.2.2.3    bouyer 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    702      1.1  christos 		if (m0 == 0)
    703  1.2.2.1    bouyer 			break;
    704      1.1  christos 
    705      1.1  christos #if NBPFILTER > 0
    706      1.1  christos 		if (ifp->if_bpf)
    707      1.1  christos 			bpf_mtap(ifp->if_bpf, m0);
    708      1.1  christos #endif
    709      1.1  christos 
    710      1.1  christos 		cnw_transmit(sc, m0);
    711      1.1  christos 		++ifp->if_opackets;
    712      1.1  christos 		ifp->if_timer = 3; /* start watchdog timer */
    713  1.2.2.1    bouyer 
    714  1.2.2.1    bouyer 		microtime(&sc->sc_txlast);
    715  1.2.2.1    bouyer 		sc->sc_active = 1;
    716      1.1  christos 	}
    717      1.1  christos 
    718  1.2.2.1    bouyer 	ifp->if_flags &= ~IFF_OACTIVE;
    719  1.2.2.1    bouyer }
    720      1.1  christos 
    721      1.1  christos /*
    722      1.1  christos  * Transmit a packet.
    723      1.1  christos  */
    724      1.1  christos void
    725      1.1  christos cnw_transmit(sc, m0)
    726      1.1  christos 	struct cnw_softc *sc;
    727      1.1  christos 	struct mbuf *m0;
    728      1.1  christos {
    729      1.1  christos 	int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
    730      1.1  christos 	struct mbuf *m;
    731      1.1  christos 	u_int8_t *mptr;
    732      1.1  christos 
    733      1.1  christos 	/* Get buffer info from card */
    734      1.1  christos 	buffer = read16(sc, CNW_EREG_TDP);
    735      1.1  christos 	bufsize = read16(sc, CNW_EREG_TDP + 2);
    736      1.1  christos 	bufoffset = read16(sc, CNW_EREG_TDP + 4);
    737      1.1  christos #ifdef CNW_DEBUG
    738      1.1  christos 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    739      1.1  christos 		printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
    740      1.1  christos 		    sc->sc_dev.dv_xname, buffer, bufsize, bufoffset);
    741      1.1  christos #endif
    742      1.1  christos 
    743      1.1  christos 	/* Copy data from mbuf chain to card buffers */
    744      1.1  christos 	bufptr = sc->sc_memoff + buffer + bufoffset;
    745      1.1  christos 	bufspace = bufsize;
    746      1.1  christos 	len = 0;
    747      1.1  christos 	for (m = m0; m; ) {
    748      1.1  christos 		mptr = mtod(m, u_int8_t *);
    749      1.1  christos 		mbytes = m->m_len;
    750      1.1  christos 		len += mbytes;
    751      1.1  christos 		while (mbytes > 0) {
    752      1.1  christos 			if (bufspace == 0) {
    753      1.1  christos 				buffer = read16(sc, buffer);
    754      1.1  christos 				bufptr = sc->sc_memoff + buffer + bufoffset;
    755      1.1  christos 				bufspace = bufsize;
    756      1.1  christos #ifdef CNW_DEBUG
    757      1.1  christos 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    758      1.1  christos 					printf("%s:   next buffer @0x%x\n",
    759      1.1  christos 					    sc->sc_dev.dv_xname, buffer);
    760      1.1  christos #endif
    761      1.1  christos 			}
    762      1.1  christos 			n = mbytes <= bufspace ? mbytes : bufspace;
    763      1.1  christos 			bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
    764      1.1  christos 			    bufptr, mptr, n);
    765      1.1  christos 			bufptr += n;
    766      1.1  christos 			bufspace -= n;
    767      1.1  christos 			mptr += n;
    768      1.1  christos 			mbytes -= n;
    769      1.1  christos 		}
    770      1.1  christos 		MFREE(m, m0);
    771      1.1  christos 		m = m0;
    772      1.1  christos 	}
    773      1.1  christos 
    774      1.1  christos 	/* Issue transmit command */
    775      1.1  christos 	CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
    776      1.1  christos }
    777      1.1  christos 
    778      1.1  christos 
    779      1.1  christos /*
    780      1.1  christos  * Pull a packet from the card into an mbuf chain.
    781      1.1  christos  */
    782      1.1  christos struct mbuf *
    783      1.1  christos cnw_read(sc)
    784      1.1  christos 	struct cnw_softc *sc;
    785      1.1  christos {
    786      1.1  christos 	struct mbuf *m, *top, **mp;
    787      1.1  christos 	int totbytes, buffer, bufbytes, bufptr, mbytes, n;
    788      1.1  christos 	u_int8_t *mptr;
    789      1.1  christos 
    790      1.1  christos 	WAIT_WOC(sc);
    791      1.1  christos 	totbytes = read16(sc, CNW_EREG_RDP);
    792      1.1  christos #ifdef CNW_DEBUG
    793      1.1  christos 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    794      1.1  christos 		printf("%s: recv %d bytes\n", sc->sc_dev.dv_xname, totbytes);
    795      1.1  christos #endif
    796      1.1  christos 	buffer = CNW_EREG_RDP + 2;
    797      1.1  christos 	bufbytes = 0;
    798      1.1  christos 	bufptr = 0; /* XXX make gcc happy */
    799      1.1  christos 
    800      1.1  christos 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    801      1.1  christos 	if (m == 0)
    802      1.1  christos 		return (0);
    803      1.1  christos 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    804      1.1  christos 	m->m_pkthdr.len = totbytes;
    805      1.1  christos 	mbytes = MHLEN;
    806      1.1  christos 	top = 0;
    807      1.1  christos 	mp = &top;
    808      1.1  christos 
    809      1.1  christos 	while (totbytes > 0) {
    810      1.1  christos 		if (top) {
    811      1.1  christos 			MGET(m, M_DONTWAIT, MT_DATA);
    812      1.1  christos 			if (m == 0) {
    813      1.1  christos 				m_freem(top);
    814      1.1  christos 				return (0);
    815      1.1  christos 			}
    816      1.1  christos 			mbytes = MLEN;
    817      1.1  christos 		}
    818      1.1  christos 		if (totbytes >= MINCLSIZE) {
    819      1.1  christos 			MCLGET(m, M_DONTWAIT);
    820      1.1  christos 			if ((m->m_flags & M_EXT) == 0) {
    821      1.1  christos 				m_free(m);
    822      1.1  christos 				m_freem(top);
    823      1.1  christos 				return (0);
    824      1.1  christos 			}
    825      1.1  christos 			mbytes = MCLBYTES;
    826      1.1  christos 		}
    827      1.1  christos 		if (!top) {
    828      1.1  christos 			int pad = ALIGN(sizeof(struct ether_header)) -
    829      1.1  christos 			    sizeof(struct ether_header);
    830      1.1  christos 			m->m_data += pad;
    831      1.1  christos 			mbytes -= pad;
    832      1.1  christos 		}
    833      1.1  christos 		mptr = mtod(m, u_int8_t *);
    834      1.1  christos 		mbytes = m->m_len = min(totbytes, mbytes);
    835      1.1  christos 		totbytes -= mbytes;
    836      1.1  christos 		while (mbytes > 0) {
    837      1.1  christos 			if (bufbytes == 0) {
    838      1.1  christos 				buffer = read16(sc, buffer);
    839      1.1  christos 				bufbytes = read16(sc, buffer + 2);
    840      1.1  christos 				bufptr = sc->sc_memoff + buffer +
    841      1.1  christos 				    read16(sc, buffer + 4);
    842      1.1  christos #ifdef CNW_DEBUG
    843      1.1  christos 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    844      1.1  christos 					printf("%s:   %d bytes @0x%x+0x%x\n",
    845      1.1  christos 					    sc->sc_dev.dv_xname, bufbytes,
    846      1.1  christos 					    buffer, bufptr - buffer -
    847      1.1  christos 					    sc->sc_memoff);
    848      1.1  christos #endif
    849      1.1  christos 			}
    850      1.1  christos 			n = mbytes <= bufbytes ? mbytes : bufbytes;
    851      1.1  christos 			bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
    852      1.1  christos 			    bufptr, mptr, n);
    853      1.1  christos 			bufbytes -= n;
    854      1.1  christos 			bufptr += n;
    855      1.1  christos 			mbytes -= n;
    856      1.1  christos 			mptr += n;
    857      1.1  christos 		}
    858      1.1  christos 		*mp = m;
    859      1.1  christos 		mp = &m->m_next;
    860      1.1  christos 	}
    861      1.1  christos 
    862      1.1  christos 	return (top);
    863      1.1  christos }
    864      1.1  christos 
    865      1.1  christos 
    866      1.1  christos /*
    867      1.1  christos  * Handle received packets.
    868      1.1  christos  */
    869      1.1  christos void
    870      1.1  christos cnw_recv(sc)
    871      1.1  christos 	struct cnw_softc *sc;
    872      1.1  christos {
    873      1.1  christos 	int rser;
    874      1.1  christos 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    875      1.1  christos 	struct mbuf *m;
    876      1.1  christos 
    877      1.1  christos 	for (;;) {
    878      1.1  christos 		WAIT_WOC(sc);
    879      1.1  christos 		rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    880      1.1  christos 		    sc->sc_memoff + CNW_EREG_RSER);
    881      1.1  christos 		if (!(rser & CNW_RSER_RXAVAIL))
    882      1.1  christos 			return;
    883      1.1  christos 
    884      1.1  christos 		/* Pull packet off card */
    885      1.1  christos 		m = cnw_read(sc);
    886      1.1  christos 
    887      1.1  christos 		/* Acknowledge packet */
    888      1.1  christos 		CNW_CMD0(sc, CNW_CMD_SRP);
    889      1.1  christos 
    890      1.1  christos 		/* Did we manage to get the packet from the interface? */
    891      1.1  christos 		if (m == 0) {
    892      1.1  christos 			++ifp->if_ierrors;
    893      1.1  christos 			return;
    894      1.1  christos 		}
    895      1.1  christos 		++ifp->if_ipackets;
    896      1.1  christos 
    897      1.1  christos #if NBPFILTER > 0
    898      1.1  christos 		if (ifp->if_bpf)
    899      1.1  christos 			bpf_mtap(ifp->if_bpf, m);
    900      1.1  christos #endif
    901      1.1  christos 
    902      1.2   thorpej 		/* Pass the packet up. */
    903      1.2   thorpej 		(*ifp->if_input)(ifp, m);
    904      1.1  christos 	}
    905      1.1  christos }
    906      1.1  christos 
    907      1.1  christos 
    908      1.1  christos /*
    909      1.1  christos  * Interrupt handler.
    910      1.1  christos  */
    911      1.1  christos int
    912      1.1  christos cnw_intr(arg)
    913      1.1  christos 	void *arg;
    914      1.1  christos {
    915      1.1  christos 	struct cnw_softc *sc = arg;
    916      1.1  christos 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    917      1.1  christos 	int ret, status, rser, tser;
    918      1.1  christos 
    919  1.2.2.1    bouyer 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 ||
    920  1.2.2.1    bouyer 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    921      1.1  christos 		return (0);
    922      1.1  christos 	ifp->if_timer = 0;	/* stop watchdog timer */
    923      1.1  christos 
    924      1.1  christos 	ret = 0;
    925      1.1  christos 	for (;;) {
    926      1.1  christos 		WAIT_WOC(sc);
    927  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    928  1.2.2.1    bouyer 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    929  1.2.2.1    bouyer 		    CNW_REG_CCSR);
    930  1.2.2.1    bouyer #else
    931  1.2.2.1    bouyer 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    932  1.2.2.1    bouyer 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR);
    933  1.2.2.1    bouyer #endif
    934  1.2.2.1    bouyer 		if (!(status & 0x02)) {
    935      1.1  christos 			if (ret == 0)
    936      1.1  christos 				printf("%s: spurious interrupt\n",
    937      1.1  christos 				    sc->sc_dev.dv_xname);
    938      1.1  christos 			return (ret);
    939      1.1  christos 		}
    940      1.1  christos 		ret = 1;
    941  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
    942      1.1  christos 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    943  1.2.2.1    bouyer #else
    944  1.2.2.1    bouyer 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    945  1.2.2.1    bouyer 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    946  1.2.2.1    bouyer #endif
    947      1.1  christos 
    948      1.1  christos 		/* Anything to receive? */
    949  1.2.2.1    bouyer 		if (status & CNW_ASR_RXRDY) {
    950  1.2.2.1    bouyer 			sc->sc_stats.nws_rx++;
    951      1.1  christos 			cnw_recv(sc);
    952  1.2.2.1    bouyer 		}
    953      1.1  christos 
    954      1.1  christos 		/* Receive error */
    955      1.1  christos 		if (status & CNW_ASR_RXERR) {
    956      1.1  christos 			/*
    957      1.1  christos 			 * I get a *lot* of spurious receive errors
    958      1.1  christos 			 * (many per second), even when the interface
    959      1.1  christos 			 * is quiescent, so we don't increment
    960      1.1  christos 			 * if_ierrors here.
    961      1.1  christos 			 */
    962      1.1  christos 			rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    963      1.1  christos 			    sc->sc_memoff + CNW_EREG_RSER);
    964  1.2.2.1    bouyer 
    965  1.2.2.1    bouyer 			/* RX statistics */
    966  1.2.2.1    bouyer 			sc->sc_stats.nws_rxerr++;
    967  1.2.2.1    bouyer 			if (rser & CNW_RSER_RXBIG)
    968  1.2.2.1    bouyer 				sc->sc_stats.nws_rxframe++;
    969  1.2.2.1    bouyer 			if (rser & CNW_RSER_RXCRC)
    970  1.2.2.1    bouyer 				sc->sc_stats.nws_rxcrcerror++;
    971  1.2.2.1    bouyer 			if (rser & CNW_RSER_RXOVERRUN)
    972  1.2.2.1    bouyer 				sc->sc_stats.nws_rxoverrun++;
    973  1.2.2.1    bouyer 			if (rser & CNW_RSER_RXOVERFLOW)
    974  1.2.2.1    bouyer 				sc->sc_stats.nws_rxoverflow++;
    975  1.2.2.1    bouyer 			if (rser & CNW_RSER_RXERR)
    976  1.2.2.1    bouyer 				sc->sc_stats.nws_rxerrors++;
    977  1.2.2.1    bouyer 			if (rser & CNW_RSER_RXAVAIL)
    978  1.2.2.1    bouyer 				sc->sc_stats.nws_rxavail++;
    979  1.2.2.1    bouyer 
    980      1.1  christos 			/* Clear error bits in RSER */
    981      1.1  christos 			WAIT_WOC(sc);
    982      1.1  christos 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    983      1.1  christos 			    sc->sc_memoff + CNW_EREG_RSERW,
    984      1.1  christos 			    CNW_RSER_RXERR |
    985      1.1  christos 			    (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
    986      1.1  christos 			/* Clear RXERR in ASR */
    987      1.1  christos 			WAIT_WOC(sc);
    988      1.1  christos 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    989      1.1  christos 			    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
    990      1.1  christos 		}
    991      1.1  christos 
    992      1.1  christos 		/* Transmit done */
    993      1.1  christos 		if (status & CNW_ASR_TXDN) {
    994      1.1  christos 			tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    995      1.1  christos 						CNW_EREG_TSER);
    996  1.2.2.1    bouyer 
    997  1.2.2.1    bouyer 			/* TX statistics */
    998  1.2.2.1    bouyer 			if (tser & CNW_TSER_TXERR)
    999  1.2.2.1    bouyer 				sc->sc_stats.nws_txerrors++;
   1000  1.2.2.1    bouyer 			if (tser & CNW_TSER_TXNOAP)
   1001  1.2.2.1    bouyer 				sc->sc_stats.nws_txlostcd++;
   1002  1.2.2.1    bouyer 			if (tser & CNW_TSER_TXGU)
   1003  1.2.2.1    bouyer 				sc->sc_stats.nws_txabort++;
   1004  1.2.2.1    bouyer 
   1005      1.1  christos 			if (tser & CNW_TSER_TXOK) {
   1006  1.2.2.1    bouyer 				sc->sc_stats.nws_txokay++;
   1007  1.2.2.1    bouyer 				sc->sc_stats.nws_txretries[status & 0xf]++;
   1008      1.1  christos 				WAIT_WOC(sc);
   1009      1.1  christos 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
   1010      1.1  christos 				    sc->sc_memoff + CNW_EREG_TSERW,
   1011      1.1  christos 				    CNW_TSER_TXOK | CNW_TSER_RTRY);
   1012      1.1  christos 			}
   1013  1.2.2.1    bouyer 
   1014      1.1  christos 			if (tser & CNW_TSER_ERROR) {
   1015      1.1  christos 				++ifp->if_oerrors;
   1016      1.1  christos 				WAIT_WOC(sc);
   1017      1.1  christos 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
   1018      1.1  christos 				    sc->sc_memoff + CNW_EREG_TSERW,
   1019      1.1  christos 				    (tser & CNW_TSER_ERROR) |
   1020      1.1  christos 				    CNW_TSER_RTRY);
   1021      1.1  christos 			}
   1022  1.2.2.1    bouyer 
   1023  1.2.2.1    bouyer 			sc->sc_active = 0;
   1024  1.2.2.1    bouyer 			ifp->if_flags &= ~IFF_OACTIVE;
   1025  1.2.2.1    bouyer 
   1026      1.1  christos 			/* Continue to send packets from the queue */
   1027      1.1  christos 			cnw_start(&sc->sc_ethercom.ec_if);
   1028      1.1  christos 		}
   1029      1.1  christos 
   1030      1.1  christos 	}
   1031      1.1  christos }
   1032      1.1  christos 
   1033      1.1  christos 
   1034      1.1  christos /*
   1035      1.1  christos  * Handle device ioctls.
   1036      1.1  christos  */
   1037      1.1  christos int
   1038      1.1  christos cnw_ioctl(ifp, cmd, data)
   1039  1.2.2.1    bouyer 	struct ifnet *ifp;
   1040      1.1  christos 	u_long cmd;
   1041      1.1  christos 	caddr_t data;
   1042      1.1  christos {
   1043      1.1  christos 	struct cnw_softc *sc = ifp->if_softc;
   1044      1.1  christos 	struct ifaddr *ifa = (struct ifaddr *)data;
   1045  1.2.2.1    bouyer 	struct ifreq *ifr = (struct ifreq *)data;
   1046      1.1  christos 	int s, error = 0;
   1047  1.2.2.1    bouyer 	struct proc *p = curproc;	/*XXX*/
   1048      1.1  christos 
   1049      1.1  christos 	s = splnet();
   1050      1.1  christos 
   1051      1.1  christos 	switch (cmd) {
   1052      1.1  christos 
   1053      1.1  christos 	case SIOCSIFADDR:
   1054      1.1  christos 		if (!(ifp->if_flags & IFF_RUNNING) &&
   1055      1.1  christos 		    (error = cnw_enable(sc)) != 0)
   1056      1.1  christos 			break;
   1057      1.1  christos 		ifp->if_flags |= IFF_UP;
   1058      1.1  christos 		switch (ifa->ifa_addr->sa_family) {
   1059      1.1  christos #ifdef INET
   1060      1.1  christos 		case AF_INET:
   1061  1.2.2.1    bouyer 			cnw_init(sc);
   1062      1.1  christos 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
   1063      1.1  christos 			break;
   1064      1.1  christos #endif
   1065  1.2.2.1    bouyer 		default:
   1066  1.2.2.1    bouyer 			cnw_init(sc);
   1067  1.2.2.1    bouyer 			break;
   1068      1.1  christos 		}
   1069      1.1  christos 		break;
   1070      1.1  christos 
   1071      1.1  christos 	case SIOCSIFFLAGS:
   1072      1.1  christos 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
   1073      1.1  christos 			/*
   1074      1.1  christos 			 * The interface is marked down and it is running, so
   1075      1.1  christos 			 * stop it.
   1076      1.1  christos 			 */
   1077      1.1  christos 			cnw_disable(sc);
   1078      1.1  christos 		} else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP){
   1079      1.1  christos 			/*
   1080      1.1  christos 			 * The interface is marked up and it is stopped, so
   1081      1.1  christos 			 * start it.
   1082      1.1  christos 			 */
   1083      1.1  christos 			error = cnw_enable(sc);
   1084  1.2.2.1    bouyer 		} else {
   1085  1.2.2.1    bouyer 			/* IFF_PROMISC may be changed */
   1086  1.2.2.1    bouyer 			cnw_init(sc);
   1087  1.2.2.1    bouyer 		}
   1088  1.2.2.1    bouyer 		break;
   1089  1.2.2.1    bouyer 
   1090  1.2.2.1    bouyer 	case SIOCADDMULTI:
   1091  1.2.2.1    bouyer 	case SIOCDELMULTI:
   1092  1.2.2.1    bouyer 		/* Update our multicast list. */
   1093  1.2.2.1    bouyer 		error = (cmd == SIOCADDMULTI) ?
   1094  1.2.2.1    bouyer 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1095  1.2.2.1    bouyer 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1096  1.2.2.1    bouyer 		if (error == ENETRESET || error == 0) {
   1097  1.2.2.1    bouyer 			cnw_init(sc);
   1098  1.2.2.1    bouyer 			error = 0;
   1099      1.1  christos 		}
   1100      1.1  christos 		break;
   1101      1.1  christos 
   1102  1.2.2.1    bouyer 	case SIOCGCNWDOMAIN:
   1103  1.2.2.1    bouyer 		((struct ifreq *)data)->ifr_domain = sc->sc_domain;
   1104  1.2.2.1    bouyer 		break;
   1105  1.2.2.1    bouyer 
   1106  1.2.2.1    bouyer 	case SIOCSCNWDOMAIN:
   1107  1.2.2.1    bouyer 		error = suser(p->p_ucred, &p->p_acflag);
   1108  1.2.2.1    bouyer 		if (error)
   1109  1.2.2.1    bouyer 			break;
   1110  1.2.2.1    bouyer 		error = cnw_setdomain(sc, ifr->ifr_domain);
   1111  1.2.2.1    bouyer 		break;
   1112  1.2.2.1    bouyer 
   1113  1.2.2.1    bouyer 	case SIOCSCNWKEY:
   1114  1.2.2.1    bouyer 		error = suser(p->p_ucred, &p->p_acflag);
   1115  1.2.2.1    bouyer 		if (error)
   1116  1.2.2.1    bouyer 			break;
   1117  1.2.2.1    bouyer 		error = cnw_setkey(sc, ifr->ifr_key);
   1118  1.2.2.1    bouyer 		break;
   1119  1.2.2.1    bouyer 
   1120  1.2.2.1    bouyer 	case SIOCGCNWSTATUS:
   1121  1.2.2.1    bouyer 		error = suser(p->p_ucred, &p->p_acflag);
   1122  1.2.2.1    bouyer 		if (error)
   1123  1.2.2.1    bouyer 			break;
   1124  1.2.2.1    bouyer 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1125  1.2.2.1    bouyer 			break;
   1126  1.2.2.1    bouyer 		bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
   1127  1.2.2.1    bouyer 		    sc->sc_memoff + CNW_EREG_CB,
   1128  1.2.2.1    bouyer 		    ((struct cnwstatus *)data)->data,
   1129  1.2.2.1    bouyer 		    sizeof(((struct cnwstatus *)data)->data));
   1130  1.2.2.1    bouyer 		break;
   1131  1.2.2.1    bouyer 
   1132  1.2.2.1    bouyer 	case SIOCGCNWSTATS:
   1133  1.2.2.1    bouyer 		bcopy((void *)&sc->sc_stats,
   1134  1.2.2.1    bouyer 		    (void *)&(((struct cnwistats *)data)->stats),
   1135  1.2.2.1    bouyer 		    sizeof(struct cnwstats));
   1136  1.2.2.1    bouyer 			break;
   1137  1.2.2.1    bouyer 
   1138      1.1  christos 	default:
   1139      1.1  christos 		error = EINVAL;
   1140      1.1  christos 		break;
   1141      1.1  christos 	}
   1142      1.1  christos 
   1143      1.1  christos 	splx(s);
   1144      1.1  christos 	return (error);
   1145      1.1  christos }
   1146      1.1  christos 
   1147      1.1  christos 
   1148      1.1  christos /*
   1149      1.1  christos  * Device timeout/watchdog routine. Entered if the device neglects to
   1150      1.1  christos  * generate an interrupt after a transmit has been started on it.
   1151      1.1  christos  */
   1152      1.1  christos void
   1153      1.1  christos cnw_watchdog(ifp)
   1154      1.1  christos 	struct ifnet *ifp;
   1155      1.1  christos {
   1156      1.1  christos 	struct cnw_softc *sc = ifp->if_softc;
   1157      1.1  christos 
   1158      1.1  christos 	printf("%s: device timeout; card reset\n", sc->sc_dev.dv_xname);
   1159      1.1  christos 	++ifp->if_oerrors;
   1160      1.1  christos 	cnw_init(sc);
   1161  1.2.2.1    bouyer }
   1162  1.2.2.1    bouyer 
   1163  1.2.2.1    bouyer int
   1164  1.2.2.1    bouyer cnw_setdomain(sc, domain)
   1165  1.2.2.1    bouyer 	struct cnw_softc *sc;
   1166  1.2.2.1    bouyer 	int domain;
   1167  1.2.2.1    bouyer {
   1168  1.2.2.1    bouyer 	int s;
   1169  1.2.2.1    bouyer 
   1170  1.2.2.1    bouyer 	if (domain & ~0x1ff)
   1171  1.2.2.1    bouyer 		return EINVAL;
   1172  1.2.2.1    bouyer 
   1173  1.2.2.1    bouyer 	s = splnet();
   1174  1.2.2.1    bouyer 	CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8);
   1175  1.2.2.1    bouyer 	splx(s);
   1176  1.2.2.1    bouyer 
   1177  1.2.2.1    bouyer 	sc->sc_domain = domain;
   1178  1.2.2.1    bouyer 	return 0;
   1179  1.2.2.1    bouyer }
   1180  1.2.2.1    bouyer 
   1181  1.2.2.1    bouyer int
   1182  1.2.2.1    bouyer cnw_setkey(sc, key)
   1183  1.2.2.1    bouyer 	struct cnw_softc *sc;
   1184  1.2.2.1    bouyer 	int key;
   1185  1.2.2.1    bouyer {
   1186  1.2.2.1    bouyer 	int s;
   1187  1.2.2.1    bouyer 
   1188  1.2.2.1    bouyer 	if (key & ~0xffff)
   1189  1.2.2.1    bouyer 		return EINVAL;
   1190  1.2.2.1    bouyer 
   1191  1.2.2.1    bouyer 	s = splnet();
   1192  1.2.2.1    bouyer 	CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8);
   1193  1.2.2.1    bouyer 	splx(s);
   1194  1.2.2.1    bouyer 
   1195  1.2.2.1    bouyer 	sc->sc_skey = key;
   1196  1.2.2.1    bouyer 	return 0;
   1197  1.2.2.1    bouyer }
   1198  1.2.2.1    bouyer 
   1199  1.2.2.1    bouyer int
   1200  1.2.2.1    bouyer cnw_activate(self, act)
   1201  1.2.2.1    bouyer 	struct device *self;
   1202  1.2.2.1    bouyer 	enum devact act;
   1203  1.2.2.1    bouyer {
   1204  1.2.2.1    bouyer 	struct cnw_softc *sc = (struct cnw_softc *)self;
   1205  1.2.2.1    bouyer 	int rv = 0, s;
   1206  1.2.2.1    bouyer 
   1207  1.2.2.1    bouyer 	s = splnet();
   1208  1.2.2.1    bouyer 	switch (act) {
   1209  1.2.2.1    bouyer 	case DVACT_ACTIVATE:
   1210  1.2.2.1    bouyer 		rv = EOPNOTSUPP;
   1211  1.2.2.1    bouyer 		break;
   1212  1.2.2.1    bouyer 
   1213  1.2.2.1    bouyer 	case DVACT_DEACTIVATE:
   1214  1.2.2.1    bouyer 		if_deactivate(&sc->sc_ethercom.ec_if);
   1215  1.2.2.1    bouyer 		break;
   1216  1.2.2.1    bouyer 	}
   1217  1.2.2.1    bouyer 	splx(s);
   1218  1.2.2.1    bouyer 	return (rv);
   1219  1.2.2.1    bouyer }
   1220  1.2.2.1    bouyer 
   1221  1.2.2.1    bouyer int
   1222  1.2.2.1    bouyer cnw_detach(self, flags)
   1223  1.2.2.1    bouyer 	struct device *self;
   1224  1.2.2.1    bouyer 	int flags;
   1225  1.2.2.1    bouyer {
   1226  1.2.2.1    bouyer 	struct cnw_softc *sc = (struct cnw_softc *)self;
   1227  1.2.2.1    bouyer 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1228  1.2.2.1    bouyer 
   1229  1.2.2.1    bouyer 	/* cnw_disable() checks IFF_RUNNING */
   1230  1.2.2.1    bouyer 	cnw_disable(sc);
   1231  1.2.2.1    bouyer 
   1232  1.2.2.1    bouyer 	if ((sc->sc_resource & CNW_RES_NET) != 0) {
   1233  1.2.2.1    bouyer 		ether_ifdetach(ifp);
   1234  1.2.2.1    bouyer 		if_detach(ifp);
   1235  1.2.2.1    bouyer 	}
   1236  1.2.2.1    bouyer 
   1237  1.2.2.1    bouyer #ifndef MEMORY_MAPPED
   1238  1.2.2.1    bouyer 	/* unmap and free our i/o windows */
   1239  1.2.2.1    bouyer 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
   1240  1.2.2.1    bouyer 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
   1241  1.2.2.1    bouyer 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
   1242  1.2.2.1    bouyer 	}
   1243  1.2.2.1    bouyer #endif
   1244  1.2.2.1    bouyer 
   1245  1.2.2.1    bouyer 	/* unmap and free our memory windows */
   1246  1.2.2.1    bouyer 	if ((sc->sc_resource & CNW_RES_MEM) != 0) {
   1247  1.2.2.1    bouyer 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin);
   1248  1.2.2.1    bouyer 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
   1249  1.2.2.1    bouyer 	}
   1250  1.2.2.1    bouyer 
   1251  1.2.2.1    bouyer 	return (0);
   1252      1.1  christos }
   1253