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