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