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