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