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