if_cnw.c revision 1.41.10.1 1 /* $NetBSD: if_cnw.c,v 1.41.10.1 2007/09/03 10:21:55 skrll 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.41.10.1 2007/09/03 10:21:55 skrll 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", sc->sc_dev.dv_xname, 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 sc->sc_dev.dv_xname, 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", sc->sc_dev.dv_xname);
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 printf("%s: couldn't establish interrupt handler\n",
443 sc->sc_dev.dv_xname);
444 return (EIO);
445 }
446 if (pcmcia_function_enable(sc->sc_pf) != 0) {
447 printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
448 return (EIO);
449 }
450 sc->sc_resource |= CNW_RES_PCIC;
451 cnw_init(sc);
452 ifp->if_flags &= ~IFF_OACTIVE;
453 ifp->if_flags |= IFF_RUNNING;
454 return (0);
455 }
456
457
458 /*
459 * Stop and disable the card.
460 */
461 void
462 cnw_disable(sc)
463 struct cnw_softc *sc;
464 {
465 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
466
467 if ((ifp->if_flags & IFF_RUNNING) == 0)
468 return;
469
470 pcmcia_function_disable(sc->sc_pf);
471 sc->sc_resource &= ~CNW_RES_PCIC;
472 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
473 ifp->if_flags &= ~IFF_RUNNING;
474 ifp->if_timer = 0;
475 }
476
477
478 /*
479 * Match the hardware we handle.
480 */
481 int
482 cnw_match(struct device *parent, struct cfdata *match,
483 void *aux)
484 {
485 struct pcmcia_attach_args *pa = aux;
486
487 if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
488 pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801)
489 return 1;
490 if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
491 pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802)
492 return 1;
493 return 0;
494 }
495
496
497 /*
498 * Attach the card.
499 */
500 void
501 cnw_attach(struct device *parent, struct device *self, void *aux)
502 {
503 struct cnw_softc *sc = (void *) self;
504 struct pcmcia_attach_args *pa = aux;
505 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
506 u_int8_t macaddr[ETHER_ADDR_LEN];
507 int i;
508 bus_size_t memsize;
509
510 sc->sc_resource = 0;
511
512 /* Enable the card */
513 sc->sc_pf = pa->pf;
514 pcmcia_function_init(sc->sc_pf, SIMPLEQ_FIRST(&sc->sc_pf->cfe_head));
515 if (pcmcia_function_enable(sc->sc_pf)) {
516 printf("%s: function enable failed\n", self->dv_xname);
517 return;
518 }
519 sc->sc_resource |= CNW_RES_PCIC;
520
521 /* Map I/O register and "memory" */
522 #ifndef MEMORY_MAPPED
523 if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
524 &sc->sc_pcioh) != 0) {
525 printf("%s: can't allocate i/o space\n", self->dv_xname);
526 goto fail;
527 }
528 if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, &sc->sc_pcioh,
529 &sc->sc_iowin) != 0) {
530 printf("%s: can't map i/o space\n", self->dv_xname);
531 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
532 goto fail;
533 }
534 sc->sc_iot = sc->sc_pcioh.iot;
535 sc->sc_ioh = sc->sc_pcioh.ioh;
536 sc->sc_resource |= CNW_RES_IO;
537 #endif
538 #ifndef MEMORY_MAPPED
539 memsize = CNW_MEM_SIZE;
540 #else
541 memsize = CNW_MEM_SIZE + CNW_IOM_SIZE;
542 #endif
543 if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) {
544 printf("%s: can't allocate memory\n", self->dv_xname);
545 goto fail;
546 }
547 if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON,
548 CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff,
549 &sc->sc_memwin) != 0) {
550 printf("%s: can't map memory\n", self->dv_xname);
551 pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
552 goto fail;
553 }
554 sc->sc_memt = sc->sc_pcmemh.memt;
555 sc->sc_memh = sc->sc_pcmemh.memh;
556 sc->sc_resource |= CNW_RES_MEM;
557
558 /* Finish setup of softc */
559 sc->sc_domain = cnw_domain;
560 sc->sc_skey = cnw_skey;
561
562 /* Get MAC address */
563 cnw_reset(sc);
564 for (i = 0; i < ETHER_ADDR_LEN; i++)
565 macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
566 sc->sc_memoff + CNW_EREG_PA + i);
567 printf("%s: address %s\n", sc->sc_dev.dv_xname,
568 ether_sprintf(macaddr));
569
570 /* Set up ifnet structure */
571 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
572 ifp->if_softc = sc;
573 ifp->if_start = cnw_start;
574 ifp->if_ioctl = cnw_ioctl;
575 ifp->if_watchdog = cnw_watchdog;
576 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX |
577 IFF_NOTRAILERS;
578 IFQ_SET_READY(&ifp->if_snd);
579
580 /* Attach the interface */
581 if_attach(ifp);
582 ether_ifattach(ifp, macaddr);
583
584 sc->sc_resource |= CNW_RES_NET;
585
586 ifp->if_baudrate = IF_Mbps(1);
587
588 /* Disable the card now, and turn it on when the interface goes up */
589 pcmcia_function_disable(sc->sc_pf);
590 sc->sc_resource &= ~CNW_RES_PCIC;
591 return;
592
593 fail:
594 #ifndef MEMORY_MAPPED
595 if ((sc->sc_resource & CNW_RES_IO) != 0) {
596 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
597 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
598 sc->sc_resource &= ~CNW_RES_IO;
599 }
600 #endif
601 if ((sc->sc_resource & CNW_RES_PCIC) != 0) {
602 pcmcia_function_disable(sc->sc_pf);
603 sc->sc_resource &= ~CNW_RES_PCIC;
604 }
605 }
606
607 /*
608 * Start outputting on the interface.
609 */
610 void
611 cnw_start(ifp)
612 struct ifnet *ifp;
613 {
614 struct cnw_softc *sc = ifp->if_softc;
615 struct mbuf *m0;
616 int lif;
617 int asr;
618 #ifdef ONE_AT_A_TIME
619 struct timeval now;
620 #endif
621
622 #ifdef CNW_DEBUG
623 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
624 printf("%s: cnw_start\n", ifp->if_xname);
625 if (ifp->if_flags & IFF_OACTIVE)
626 printf("%s: cnw_start reentered\n", ifp->if_xname);
627 #endif
628
629 ifp->if_flags |= IFF_OACTIVE;
630
631 for (;;) {
632 #ifdef ONE_AT_A_TIME
633 microtime(&now);
634 now.tv_sec -= sc->sc_txlast.tv_sec;
635 now.tv_usec -= sc->sc_txlast.tv_usec;
636 if (now.tv_usec < 0) {
637 now.tv_usec += 1000000;
638 now.tv_sec--;
639 }
640
641 /*
642 * Don't ship this packet out until the last
643 * packet has left the building.
644 * If we have not tried to send a packet for 1/5
645 * a second then we assume we lost an interrupt,
646 * lets go on and send the next packet anyhow.
647 *
648 * I suppose we could check to see if it is okay
649 * to put additional packets on the card (beyond
650 * the one already waiting to be sent) but I don't
651 * think we would get any improvement in speed as
652 * we should have ample time to put the next packet
653 * on while this one is going out.
654 */
655 if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000)
656 break;
657 #endif
658
659 /* Make sure the link integrity field is on */
660 WAIT_WOC(sc);
661 lif = bus_space_read_1(sc->sc_memt, sc->sc_memh,
662 sc->sc_memoff + CNW_EREG_LIF);
663 if (lif == 0) {
664 #ifdef CNW_DEBUG
665 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
666 printf("%s: link integrity %d\n", ifp->if_xname, lif);
667 #endif
668 break;
669 }
670
671 /* Is there any buffer space available on the card? */
672 WAIT_WOC(sc);
673 #ifndef MEMORY_MAPPED
674 asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
675 #else
676 asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
677 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
678 #endif
679 if (!(asr & CNW_ASR_TXBA)) {
680 #ifdef CNW_DEBUG
681 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
682 printf("%s: no buffer space\n", ifp->if_xname);
683 #endif
684 break;
685 }
686
687 sc->sc_stats.nws_tx++;
688
689 IFQ_DEQUEUE(&ifp->if_snd, m0);
690 if (m0 == 0)
691 break;
692
693 #if NBPFILTER > 0
694 if (ifp->if_bpf)
695 bpf_mtap(ifp->if_bpf, m0);
696 #endif
697
698 cnw_transmit(sc, m0);
699 ++ifp->if_opackets;
700 ifp->if_timer = 3; /* start watchdog timer */
701
702 microtime(&sc->sc_txlast);
703 sc->sc_active = 1;
704 }
705
706 ifp->if_flags &= ~IFF_OACTIVE;
707 }
708
709 /*
710 * Transmit a packet.
711 */
712 void
713 cnw_transmit(sc, m0)
714 struct cnw_softc *sc;
715 struct mbuf *m0;
716 {
717 int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
718 struct mbuf *m;
719 u_int8_t *mptr;
720
721 /* Get buffer info from card */
722 buffer = read16(sc, CNW_EREG_TDP);
723 bufsize = read16(sc, CNW_EREG_TDP + 2);
724 bufoffset = read16(sc, CNW_EREG_TDP + 4);
725 #ifdef CNW_DEBUG
726 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
727 printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
728 sc->sc_dev.dv_xname, buffer, bufsize, bufoffset);
729 #endif
730
731 /* Copy data from mbuf chain to card buffers */
732 bufptr = sc->sc_memoff + buffer + bufoffset;
733 bufspace = bufsize;
734 len = 0;
735 for (m = m0; m; ) {
736 mptr = mtod(m, u_int8_t *);
737 mbytes = m->m_len;
738 len += mbytes;
739 while (mbytes > 0) {
740 if (bufspace == 0) {
741 buffer = read16(sc, buffer);
742 bufptr = sc->sc_memoff + buffer + bufoffset;
743 bufspace = bufsize;
744 #ifdef CNW_DEBUG
745 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
746 printf("%s: next buffer @0x%x\n",
747 sc->sc_dev.dv_xname, buffer);
748 #endif
749 }
750 n = mbytes <= bufspace ? mbytes : bufspace;
751 bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
752 bufptr, mptr, n);
753 bufptr += n;
754 bufspace -= n;
755 mptr += n;
756 mbytes -= n;
757 }
758 MFREE(m, m0);
759 m = m0;
760 }
761
762 /* Issue transmit command */
763 CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
764 }
765
766
767 /*
768 * Pull a packet from the card into an mbuf chain.
769 */
770 struct mbuf *
771 cnw_read(sc)
772 struct cnw_softc *sc;
773 {
774 struct mbuf *m, *top, **mp;
775 int totbytes, buffer, bufbytes, bufptr, mbytes, n;
776 u_int8_t *mptr;
777
778 WAIT_WOC(sc);
779 totbytes = read16(sc, CNW_EREG_RDP);
780 #ifdef CNW_DEBUG
781 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
782 printf("%s: recv %d bytes\n", sc->sc_dev.dv_xname, totbytes);
783 #endif
784 buffer = CNW_EREG_RDP + 2;
785 bufbytes = 0;
786 bufptr = 0; /* XXX make gcc happy */
787
788 MGETHDR(m, M_DONTWAIT, MT_DATA);
789 if (m == 0)
790 return (0);
791 m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
792 m->m_pkthdr.len = totbytes;
793 mbytes = MHLEN;
794 top = 0;
795 mp = ⊤
796
797 while (totbytes > 0) {
798 if (top) {
799 MGET(m, M_DONTWAIT, MT_DATA);
800 if (m == 0) {
801 m_freem(top);
802 return (0);
803 }
804 mbytes = MLEN;
805 }
806 if (totbytes >= MINCLSIZE) {
807 MCLGET(m, M_DONTWAIT);
808 if ((m->m_flags & M_EXT) == 0) {
809 m_free(m);
810 m_freem(top);
811 return (0);
812 }
813 mbytes = MCLBYTES;
814 }
815 if (!top) {
816 int pad = ALIGN(sizeof(struct ether_header)) -
817 sizeof(struct ether_header);
818 m->m_data += pad;
819 mbytes -= pad;
820 }
821 mptr = mtod(m, u_int8_t *);
822 mbytes = m->m_len = min(totbytes, mbytes);
823 totbytes -= mbytes;
824 while (mbytes > 0) {
825 if (bufbytes == 0) {
826 buffer = read16(sc, buffer);
827 bufbytes = read16(sc, buffer + 2);
828 bufptr = sc->sc_memoff + buffer +
829 read16(sc, buffer + 4);
830 #ifdef CNW_DEBUG
831 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
832 printf("%s: %d bytes @0x%x+0x%lx\n",
833 sc->sc_dev.dv_xname, bufbytes,
834 buffer, bufptr - buffer -
835 sc->sc_memoff);
836 #endif
837 }
838 n = mbytes <= bufbytes ? mbytes : bufbytes;
839 bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
840 bufptr, mptr, n);
841 bufbytes -= n;
842 bufptr += n;
843 mbytes -= n;
844 mptr += n;
845 }
846 *mp = m;
847 mp = &m->m_next;
848 }
849
850 return (top);
851 }
852
853
854 /*
855 * Handle received packets.
856 */
857 void
858 cnw_recv(sc)
859 struct cnw_softc *sc;
860 {
861 int rser;
862 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
863 struct mbuf *m;
864
865 for (;;) {
866 WAIT_WOC(sc);
867 rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
868 sc->sc_memoff + CNW_EREG_RSER);
869 if (!(rser & CNW_RSER_RXAVAIL))
870 return;
871
872 /* Pull packet off card */
873 m = cnw_read(sc);
874
875 /* Acknowledge packet */
876 CNW_CMD0(sc, CNW_CMD_SRP);
877
878 /* Did we manage to get the packet from the interface? */
879 if (m == 0) {
880 ++ifp->if_ierrors;
881 return;
882 }
883 ++ifp->if_ipackets;
884
885 #if NBPFILTER > 0
886 if (ifp->if_bpf)
887 bpf_mtap(ifp->if_bpf, m);
888 #endif
889
890 /* Pass the packet up. */
891 (*ifp->if_input)(ifp, m);
892 }
893 }
894
895
896 /*
897 * Interrupt handler.
898 */
899 int
900 cnw_intr(arg)
901 void *arg;
902 {
903 struct cnw_softc *sc = arg;
904 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
905 int ret, status, rser, tser;
906
907 if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 ||
908 !device_is_active(&sc->sc_dev))
909 return (0);
910 ifp->if_timer = 0; /* stop watchdog timer */
911
912 ret = 0;
913 for (;;) {
914 WAIT_WOC(sc);
915 #ifndef MEMORY_MAPPED
916 status = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
917 CNW_REG_CCSR);
918 #else
919 status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
920 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR);
921 #endif
922 if (!(status & 0x02)) {
923 if (ret == 0)
924 printf("%s: spurious interrupt\n",
925 sc->sc_dev.dv_xname);
926 return (ret);
927 }
928 ret = 1;
929 #ifndef MEMORY_MAPPED
930 status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
931 #else
932 status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
933 sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
934 #endif
935
936 /* Anything to receive? */
937 if (status & CNW_ASR_RXRDY) {
938 sc->sc_stats.nws_rx++;
939 cnw_recv(sc);
940 }
941
942 /* Receive error */
943 if (status & CNW_ASR_RXERR) {
944 /*
945 * I get a *lot* of spurious receive errors
946 * (many per second), even when the interface
947 * is quiescent, so we don't increment
948 * if_ierrors here.
949 */
950 rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
951 sc->sc_memoff + CNW_EREG_RSER);
952
953 /* RX statistics */
954 sc->sc_stats.nws_rxerr++;
955 if (rser & CNW_RSER_RXBIG)
956 sc->sc_stats.nws_rxframe++;
957 if (rser & CNW_RSER_RXCRC)
958 sc->sc_stats.nws_rxcrcerror++;
959 if (rser & CNW_RSER_RXOVERRUN)
960 sc->sc_stats.nws_rxoverrun++;
961 if (rser & CNW_RSER_RXOVERFLOW)
962 sc->sc_stats.nws_rxoverflow++;
963 if (rser & CNW_RSER_RXERR)
964 sc->sc_stats.nws_rxerrors++;
965 if (rser & CNW_RSER_RXAVAIL)
966 sc->sc_stats.nws_rxavail++;
967
968 /* Clear error bits in RSER */
969 WAIT_WOC(sc);
970 bus_space_write_1(sc->sc_memt, sc->sc_memh,
971 sc->sc_memoff + CNW_EREG_RSERW,
972 CNW_RSER_RXERR |
973 (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
974 /* Clear RXERR in ASR */
975 WAIT_WOC(sc);
976 bus_space_write_1(sc->sc_memt, sc->sc_memh,
977 sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
978 }
979
980 /* Transmit done */
981 if (status & CNW_ASR_TXDN) {
982 tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
983 CNW_EREG_TSER);
984
985 /* TX statistics */
986 if (tser & CNW_TSER_TXERR)
987 sc->sc_stats.nws_txerrors++;
988 if (tser & CNW_TSER_TXNOAP)
989 sc->sc_stats.nws_txlostcd++;
990 if (tser & CNW_TSER_TXGU)
991 sc->sc_stats.nws_txabort++;
992
993 if (tser & CNW_TSER_TXOK) {
994 sc->sc_stats.nws_txokay++;
995 sc->sc_stats.nws_txretries[status & 0xf]++;
996 WAIT_WOC(sc);
997 bus_space_write_1(sc->sc_memt, sc->sc_memh,
998 sc->sc_memoff + CNW_EREG_TSERW,
999 CNW_TSER_TXOK | CNW_TSER_RTRY);
1000 }
1001
1002 if (tser & CNW_TSER_ERROR) {
1003 ++ifp->if_oerrors;
1004 WAIT_WOC(sc);
1005 bus_space_write_1(sc->sc_memt, sc->sc_memh,
1006 sc->sc_memoff + CNW_EREG_TSERW,
1007 (tser & CNW_TSER_ERROR) |
1008 CNW_TSER_RTRY);
1009 }
1010
1011 sc->sc_active = 0;
1012 ifp->if_flags &= ~IFF_OACTIVE;
1013
1014 /* Continue to send packets from the queue */
1015 cnw_start(&sc->sc_ethercom.ec_if);
1016 }
1017
1018 }
1019 }
1020
1021
1022 /*
1023 * Handle device ioctls.
1024 */
1025 int
1026 cnw_ioctl(ifp, cmd, data)
1027 struct ifnet *ifp;
1028 u_long cmd;
1029 void *data;
1030 {
1031 struct cnw_softc *sc = ifp->if_softc;
1032 struct ifaddr *ifa = (struct ifaddr *)data;
1033 struct ifreq *ifr = (struct ifreq *)data;
1034 int s, error = 0;
1035 struct lwp *l = curlwp; /*XXX*/
1036
1037 switch (cmd) {
1038 case SIOCSIFADDR:
1039 case SIOCSIFFLAGS:
1040 case SIOCADDMULTI:
1041 case SIOCDELMULTI:
1042 case SIOCGCNWDOMAIN:
1043 case SIOCGCNWSTATS:
1044 break;
1045 case SIOCSCNWDOMAIN:
1046 case SIOCSCNWKEY:
1047 case SIOCGCNWSTATUS:
1048 error = kauth_authorize_generic(l->l_cred,
1049 KAUTH_GENERIC_ISSUSER, NULL);
1050 if (error)
1051 return (error);
1052 break;
1053 default:
1054 return (EINVAL);
1055 }
1056
1057 s = splnet();
1058
1059 switch (cmd) {
1060
1061 case SIOCSIFADDR:
1062 if (!(ifp->if_flags & IFF_RUNNING) &&
1063 (error = cnw_enable(sc)) != 0)
1064 break;
1065 ifp->if_flags |= IFF_UP;
1066 switch (ifa->ifa_addr->sa_family) {
1067 #ifdef INET
1068 case AF_INET:
1069 cnw_init(sc);
1070 arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
1071 break;
1072 #endif
1073 default:
1074 cnw_init(sc);
1075 break;
1076 }
1077 break;
1078
1079 case SIOCSIFFLAGS:
1080 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
1081 /*
1082 * The interface is marked down and it is running, so
1083 * stop it.
1084 */
1085 cnw_disable(sc);
1086 } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP){
1087 /*
1088 * The interface is marked up and it is stopped, so
1089 * start it.
1090 */
1091 error = cnw_enable(sc);
1092 } else {
1093 /* IFF_PROMISC may be changed */
1094 cnw_init(sc);
1095 }
1096 break;
1097
1098 case SIOCADDMULTI:
1099 case SIOCDELMULTI:
1100 /* Update our multicast list. */
1101 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1102 if (ifp->if_flags & IFF_RUNNING)
1103 cnw_init(sc);
1104 error = 0;
1105 }
1106 break;
1107
1108 case SIOCGCNWDOMAIN:
1109 ifr->ifr_domain = sc->sc_domain;
1110 break;
1111
1112 case SIOCSCNWDOMAIN:
1113 error = cnw_setdomain(sc, ifr->ifr_domain);
1114 break;
1115
1116 case SIOCSCNWKEY:
1117 error = cnw_setkey(sc, ifr->ifr_key);
1118 break;
1119
1120 case SIOCGCNWSTATUS:
1121 if ((ifp->if_flags & IFF_RUNNING) == 0)
1122 break;
1123 bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
1124 sc->sc_memoff + CNW_EREG_CB,
1125 ((struct cnwstatus *)data)->data,
1126 sizeof(((struct cnwstatus *)data)->data));
1127 break;
1128
1129 case SIOCGCNWSTATS:
1130 memcpy((void *)&(((struct cnwistats *)data)->stats),
1131 (void *)&sc->sc_stats, sizeof(struct cnwstats));
1132 break;
1133
1134 default:
1135 error = EINVAL;
1136 break;
1137 }
1138
1139 splx(s);
1140 return (error);
1141 }
1142
1143
1144 /*
1145 * Device timeout/watchdog routine. Entered if the device neglects to
1146 * generate an interrupt after a transmit has been started on it.
1147 */
1148 void
1149 cnw_watchdog(ifp)
1150 struct ifnet *ifp;
1151 {
1152 struct cnw_softc *sc = ifp->if_softc;
1153
1154 printf("%s: device timeout; card reset\n", sc->sc_dev.dv_xname);
1155 ++ifp->if_oerrors;
1156 cnw_init(sc);
1157 }
1158
1159 int
1160 cnw_setdomain(sc, domain)
1161 struct cnw_softc *sc;
1162 int domain;
1163 {
1164 int s;
1165
1166 if (domain & ~0x1ff)
1167 return EINVAL;
1168
1169 s = splnet();
1170 CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8);
1171 splx(s);
1172
1173 sc->sc_domain = domain;
1174 return 0;
1175 }
1176
1177 int
1178 cnw_setkey(sc, key)
1179 struct cnw_softc *sc;
1180 int key;
1181 {
1182 int s;
1183
1184 if (key & ~0xffff)
1185 return EINVAL;
1186
1187 s = splnet();
1188 CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8);
1189 splx(s);
1190
1191 sc->sc_skey = key;
1192 return 0;
1193 }
1194
1195 int
1196 cnw_activate(self, act)
1197 struct device *self;
1198 enum devact act;
1199 {
1200 struct cnw_softc *sc = (struct cnw_softc *)self;
1201 int rv = 0, s;
1202
1203 s = splnet();
1204 switch (act) {
1205 case DVACT_ACTIVATE:
1206 rv = EOPNOTSUPP;
1207 break;
1208
1209 case DVACT_DEACTIVATE:
1210 if_deactivate(&sc->sc_ethercom.ec_if);
1211 break;
1212 }
1213 splx(s);
1214 return (rv);
1215 }
1216
1217 int
1218 cnw_detach(struct device *self, int flags)
1219 {
1220 struct cnw_softc *sc = (struct cnw_softc *)self;
1221 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1222
1223 /* cnw_disable() checks IFF_RUNNING */
1224 cnw_disable(sc);
1225
1226 if ((sc->sc_resource & CNW_RES_NET) != 0) {
1227 ether_ifdetach(ifp);
1228 if_detach(ifp);
1229 }
1230
1231 #ifndef MEMORY_MAPPED
1232 /* unmap and free our i/o windows */
1233 if ((sc->sc_resource & CNW_RES_IO) != 0) {
1234 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
1235 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
1236 }
1237 #endif
1238
1239 /* unmap and free our memory windows */
1240 if ((sc->sc_resource & CNW_RES_MEM) != 0) {
1241 pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin);
1242 pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
1243 }
1244
1245 return (0);
1246 }
1247