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