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