1 /* $NetBSD: if_cnw.c,v 1.52 2009/11/22 21:18:42 dsl Exp $ */
4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c) 1996, 1997 Berkeley Software Design, Inc.
34 * All rights reserved.
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.
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.
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
67 * Paul Borman, December 1996
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.
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
78 * John Markus Bjørndalen
79 * Department of Computer Science
80 * University of Tromsø
82 * johnm@staff.cs.uit.no, http://www.cs.uit.no/~johnm/
86 * This is a driver for the Xircom CreditCard Netwave (also known as
87 * the Netwave Airsurfer) wireless LAN PCMCIA adapter.
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
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" :-(.
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 :-).
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: if_cnw.c,v 1.52 2009/11/22 21:18:42 dsl Exp $");
110 #include "opt_inet.h"
111 #include "bpfilter.h"
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/device.h>
116 #include <sys/socket.h>
117 #include <sys/mbuf.h>
118 #include <sys/ioctl.h>
119 #include <sys/proc.h>
120 #include <sys/kauth.h>
124 #include <dev/pcmcia/if_cnwreg.h>
125 #include <dev/pcmcia/if_cnwioctl.h>
127 #include <dev/pcmcia/pcmciareg.h>
128 #include <dev/pcmcia/pcmciavar.h>
129 #include <dev/pcmcia/pcmciadevs.h>
131 #include <net/if_dl.h>
132 #include <net/if_ether.h>
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>
144 #include <net/bpfdesc.h>
148 * Let these be patchable variables, initialized from macros that can
149 * be set in the kernel config file. Someone with lots of spare time
150 * could probably write a nice Netwave configuration program to do
151 * this a little bit more elegantly :-).
154 #define CNW_DOMAIN 0x100
156 int cnw_domain
= CNW_DOMAIN
; /* Domain */
157 #ifndef CNW_SCRAMBLEKEY
158 #define CNW_SCRAMBLEKEY 0
160 int cnw_skey
= CNW_SCRAMBLEKEY
; /* Scramble key */
163 * The card appears to work much better when we only allow one packet
164 * "in the air" at a time. This is done by not allowing another packet
165 * on the card, even if there is room. Turning this off will allow the
166 * driver to stuff packets on the card as soon as a transmit buffer is
167 * available. This does increase the number of collisions, though.
168 * We can que a second packet if there are transmit buffers available,
169 * but we do not actually send the packet until the last packet has
172 #define ONE_AT_A_TIME
175 * Netwave cards choke if we try to use io memory address >= 0x400.
176 * Even though, CIS tuple does not talk about this.
177 * Use memory mapped access.
179 #define MEMORY_MAPPED
181 int cnw_match(device_t
, cfdata_t
, void *);
182 void cnw_attach(device_t
, device_t
, void *);
183 int cnw_detach(device_t
, int);
185 int cnw_activate(device_t
, enum devact
);
188 struct device sc_dev
; /* Device glue (must be first) */
189 struct ethercom sc_ethercom
; /* Ethernet common part */
190 int sc_domain
; /* Netwave domain */
191 int sc_skey
; /* Netwave scramble key */
192 struct cnwstats sc_stats
;
194 /* PCMCIA-specific stuff */
195 struct pcmcia_function
*sc_pf
; /* PCMCIA function */
196 #ifndef MEMORY_MAPPED
197 struct pcmcia_io_handle sc_pcioh
; /* PCMCIA I/O space handle */
198 int sc_iowin
; /* ...window */
199 bus_space_tag_t sc_iot
; /* ...bus_space tag */
200 bus_space_handle_t sc_ioh
; /* ...bus_space handle */
202 struct pcmcia_mem_handle sc_pcmemh
; /* PCMCIA memory handle */
203 bus_size_t sc_memoff
; /* ...offset */
204 int sc_memwin
; /* ...window */
205 bus_space_tag_t sc_memt
; /* ...bus_space tag */
206 bus_space_handle_t sc_memh
; /* ...bus_space handle */
207 void *sc_ih
; /* Interrupt cookie */
208 struct timeval sc_txlast
; /* When the last xmit was made */
209 int sc_active
; /* Currently xmitting a packet */
211 int sc_resource
; /* Resources alloc'ed on attach */
212 #define CNW_RES_PCIC 1
214 #define CNW_RES_MEM 4
215 #define CNW_RES_NET 8
218 CFATTACH_DECL(cnw
, sizeof(struct cnw_softc
),
219 cnw_match
, cnw_attach
, cnw_detach
, cnw_activate
);
221 void cnw_reset(struct cnw_softc
*);
222 void cnw_init(struct cnw_softc
*);
223 int cnw_enable(struct cnw_softc
*sc
);
224 void cnw_disable(struct cnw_softc
*sc
);
225 void cnw_config(struct cnw_softc
*sc
, u_int8_t
*);
226 void cnw_start(struct ifnet
*);
227 void cnw_transmit(struct cnw_softc
*, struct mbuf
*);
228 struct mbuf
*cnw_read(struct cnw_softc
*);
229 void cnw_recv(struct cnw_softc
*);
230 int cnw_intr(void *arg
);
231 int cnw_ioctl(struct ifnet
*, u_long
, void *);
232 void cnw_watchdog(struct ifnet
*);
233 static int cnw_setdomain(struct cnw_softc
*, int);
234 static int cnw_setkey(struct cnw_softc
*, int);
236 /* ---------------------------------------------------------------- */
239 static int wait_WOC(struct cnw_softc
*, int);
240 static int read16(struct cnw_softc
*, int);
241 static int cnw_cmd(struct cnw_softc
*, int, int, int, int);
244 * Wait until the WOC (Write Operation Complete) bit in the
245 * ASR (Adapter Status Register) is asserted.
248 wait_WOC(struct cnw_softc
*sc
, int line
)
252 for (i
= 0; i
< 5000; i
++) {
253 #ifndef MEMORY_MAPPED
254 asr
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, CNW_REG_ASR
);
256 asr
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
257 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_ASR
);
259 if (asr
& CNW_ASR_WOC
)
264 printf("%s: wedged at line %d\n", device_xname(&sc
->sc_dev
), line
);
267 #define WAIT_WOC(sc) wait_WOC(sc, __LINE__)
271 * Read a 16 bit value from the card.
274 read16(struct cnw_softc
*sc
, int offset
)
277 int offs
= sc
->sc_memoff
+ offset
;
279 /* This could presumably be done more efficient with
280 * bus_space_read_2(), but I don't know anything about the
281 * byte sex guarantees... Besides, this is pretty cheap as
284 lo
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
, offs
);
285 hi
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
, offs
+ 1);
286 return ((hi
<< 8) | lo
);
291 * Send a command to the card by writing it to the command buffer.
294 cnw_cmd(struct cnw_softc
*sc
, int cmd
, int count
, int arg1
, int arg2
)
296 int ptr
= sc
->sc_memoff
+ CNW_EREG_CB
;
298 if (wait_WOC(sc
, 0)) {
299 printf("%s: wedged when issuing cmd 0x%x\n",
300 device_xname(&sc
->sc_dev
), cmd
);
302 * We'll continue anyway, as that's probably the best
303 * thing we can do; at least the user knows there's a
304 * problem, and can reset the interface with ifconfig
309 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
, ptr
, cmd
);
311 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
, ptr
+ 1, arg1
);
313 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
316 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
317 ptr
+ count
+ 1, CNW_CMD_EOC
);
320 #define CNW_CMD0(sc, cmd) \
321 do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
322 #define CNW_CMD1(sc, cmd, arg1) \
323 do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
324 #define CNW_CMD2(sc, cmd, arg1, arg2) \
325 do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
327 /* ---------------------------------------------------------------- */
330 * Reset the hardware.
333 cnw_reset(struct cnw_softc
*sc
)
336 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
337 printf("%s: resetting\n", device_xname(&sc
->sc_dev
));
340 #ifndef MEMORY_MAPPED
341 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, CNW_REG_PMR
, CNW_PMR_RESET
);
343 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
344 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_PMR
, CNW_PMR_RESET
);
346 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
347 sc
->sc_memoff
+ CNW_EREG_ASCC
, CNW_ASR_WOC
);
348 #ifndef MEMORY_MAPPED
349 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, CNW_REG_PMR
, 0);
351 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
352 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_PMR
, 0);
358 * Initialize the card.
361 cnw_init(struct cnw_softc
*sc
)
363 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
364 const u_int8_t rxmode
=
365 CNW_RXCONF_RXENA
| CNW_RXCONF_BCAST
| CNW_RXCONF_AMP
;
370 /* Issue a NOP to check the card */
371 CNW_CMD0(sc
, CNW_CMD_NOP
);
373 /* Set up receive configuration */
374 CNW_CMD1(sc
, CNW_CMD_SRC
,
375 rxmode
| ((ifp
->if_flags
& IFF_PROMISC
) ? CNW_RXCONF_PRO
: 0));
377 /* Set up transmit configuration */
378 CNW_CMD1(sc
, CNW_CMD_STC
, CNW_TXCONF_TXENA
);
381 CNW_CMD2(sc
, CNW_CMD_SMD
, sc
->sc_domain
, sc
->sc_domain
>> 8);
383 /* Set scramble key */
384 CNW_CMD2(sc
, CNW_CMD_SSK
, sc
->sc_skey
, sc
->sc_skey
>> 8);
386 /* Enable interrupts */
388 #ifndef MEMORY_MAPPED
389 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
,
390 CNW_REG_IMR
, CNW_IMR_IENA
| CNW_IMR_RFU1
);
392 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
393 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_IMR
,
394 CNW_IMR_IENA
| CNW_IMR_RFU1
);
397 /* Enable receiver */
398 CNW_CMD0(sc
, CNW_CMD_ER
);
400 /* "Set the IENA bit in COR" */
402 #ifndef MEMORY_MAPPED
403 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, CNW_REG_COR
,
404 CNW_COR_IENA
| CNW_COR_LVLREQ
);
406 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
407 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_COR
,
408 CNW_COR_IENA
| CNW_COR_LVLREQ
);
414 * Enable and initialize the card.
417 cnw_enable(struct cnw_softc
*sc
)
419 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
421 if ((ifp
->if_flags
& IFF_RUNNING
) != 0)
424 sc
->sc_ih
= pcmcia_intr_establish(sc
->sc_pf
, IPL_NET
, cnw_intr
, sc
);
425 if (sc
->sc_ih
== NULL
) {
426 aprint_error_dev(&sc
->sc_dev
, "couldn't establish interrupt handler\n");
429 if (pcmcia_function_enable(sc
->sc_pf
) != 0) {
430 aprint_error_dev(&sc
->sc_dev
, "couldn't enable card\n");
433 sc
->sc_resource
|= CNW_RES_PCIC
;
435 ifp
->if_flags
&= ~IFF_OACTIVE
;
436 ifp
->if_flags
|= IFF_RUNNING
;
442 * Stop and disable the card.
445 cnw_disable(struct cnw_softc
*sc
)
447 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
449 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
452 pcmcia_function_disable(sc
->sc_pf
);
453 sc
->sc_resource
&= ~CNW_RES_PCIC
;
454 pcmcia_intr_disestablish(sc
->sc_pf
, sc
->sc_ih
);
455 ifp
->if_flags
&= ~IFF_RUNNING
;
461 * Match the hardware we handle.
464 cnw_match(device_t parent
, cfdata_t match
,
467 struct pcmcia_attach_args
*pa
= aux
;
469 if (pa
->manufacturer
== PCMCIA_VENDOR_XIRCOM
&&
470 pa
->product
== PCMCIA_PRODUCT_XIRCOM_CNW_801
)
472 if (pa
->manufacturer
== PCMCIA_VENDOR_XIRCOM
&&
473 pa
->product
== PCMCIA_PRODUCT_XIRCOM_CNW_802
)
483 cnw_attach(struct device
*parent
, device_t self
, void *aux
)
485 struct cnw_softc
*sc
= (void *) self
;
486 struct pcmcia_attach_args
*pa
= aux
;
487 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
488 u_int8_t macaddr
[ETHER_ADDR_LEN
];
494 /* Enable the card */
496 pcmcia_function_init(sc
->sc_pf
, SIMPLEQ_FIRST(&sc
->sc_pf
->cfe_head
));
497 if (pcmcia_function_enable(sc
->sc_pf
)) {
498 aprint_error_dev(self
, "function enable failed\n");
501 sc
->sc_resource
|= CNW_RES_PCIC
;
503 /* Map I/O register and "memory" */
504 #ifndef MEMORY_MAPPED
505 if (pcmcia_io_alloc(sc
->sc_pf
, 0, CNW_IO_SIZE
, CNW_IO_SIZE
,
506 &sc
->sc_pcioh
) != 0) {
507 aprint_error_dev(self
, "can't allocate i/o space\n");
510 if (pcmcia_io_map(sc
->sc_pf
, PCMCIA_WIDTH_IO16
, &sc
->sc_pcioh
,
511 &sc
->sc_iowin
) != 0) {
512 aprint_error_dev(self
, "can't map i/o space\n");
513 pcmcia_io_free(sc
->sc_pf
, &sc
->sc_pcioh
);
516 sc
->sc_iot
= sc
->sc_pcioh
.iot
;
517 sc
->sc_ioh
= sc
->sc_pcioh
.ioh
;
518 sc
->sc_resource
|= CNW_RES_IO
;
520 #ifndef MEMORY_MAPPED
521 memsize
= CNW_MEM_SIZE
;
523 memsize
= CNW_MEM_SIZE
+ CNW_IOM_SIZE
;
525 if (pcmcia_mem_alloc(sc
->sc_pf
, memsize
, &sc
->sc_pcmemh
) != 0) {
526 aprint_error_dev(self
, "can't allocate memory\n");
529 if (pcmcia_mem_map(sc
->sc_pf
, PCMCIA_WIDTH_MEM8
|PCMCIA_MEM_COMMON
,
530 CNW_MEM_ADDR
, memsize
, &sc
->sc_pcmemh
, &sc
->sc_memoff
,
531 &sc
->sc_memwin
) != 0) {
532 aprint_error_dev(self
, "can't map memory\n");
533 pcmcia_mem_free(sc
->sc_pf
, &sc
->sc_pcmemh
);
536 sc
->sc_memt
= sc
->sc_pcmemh
.memt
;
537 sc
->sc_memh
= sc
->sc_pcmemh
.memh
;
538 sc
->sc_resource
|= CNW_RES_MEM
;
540 /* Finish setup of softc */
541 sc
->sc_domain
= cnw_domain
;
542 sc
->sc_skey
= cnw_skey
;
544 /* Get MAC address */
546 for (i
= 0; i
< ETHER_ADDR_LEN
; i
++)
547 macaddr
[i
] = bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
548 sc
->sc_memoff
+ CNW_EREG_PA
+ i
);
549 printf("%s: address %s\n", device_xname(&sc
->sc_dev
),
550 ether_sprintf(macaddr
));
552 /* Set up ifnet structure */
553 strlcpy(ifp
->if_xname
, device_xname(&sc
->sc_dev
), IFNAMSIZ
);
555 ifp
->if_start
= cnw_start
;
556 ifp
->if_ioctl
= cnw_ioctl
;
557 ifp
->if_watchdog
= cnw_watchdog
;
558 ifp
->if_flags
= IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
|
560 IFQ_SET_READY(&ifp
->if_snd
);
562 /* Attach the interface */
564 ether_ifattach(ifp
, macaddr
);
566 sc
->sc_resource
|= CNW_RES_NET
;
568 ifp
->if_baudrate
= IF_Mbps(1);
570 /* Disable the card now, and turn it on when the interface goes up */
571 pcmcia_function_disable(sc
->sc_pf
);
572 sc
->sc_resource
&= ~CNW_RES_PCIC
;
576 #ifndef MEMORY_MAPPED
577 if ((sc
->sc_resource
& CNW_RES_IO
) != 0) {
578 pcmcia_io_unmap(sc
->sc_pf
, sc
->sc_iowin
);
579 pcmcia_io_free(sc
->sc_pf
, &sc
->sc_pcioh
);
580 sc
->sc_resource
&= ~CNW_RES_IO
;
583 if ((sc
->sc_resource
& CNW_RES_PCIC
) != 0) {
584 pcmcia_function_disable(sc
->sc_pf
);
585 sc
->sc_resource
&= ~CNW_RES_PCIC
;
590 * Start outputting on the interface.
593 cnw_start(struct ifnet
*ifp
)
595 struct cnw_softc
*sc
= ifp
->if_softc
;
604 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
605 printf("%s: cnw_start\n", ifp
->if_xname
);
606 if (ifp
->if_flags
& IFF_OACTIVE
)
607 printf("%s: cnw_start reentered\n", ifp
->if_xname
);
610 ifp
->if_flags
|= IFF_OACTIVE
;
615 now
.tv_sec
-= sc
->sc_txlast
.tv_sec
;
616 now
.tv_usec
-= sc
->sc_txlast
.tv_usec
;
617 if (now
.tv_usec
< 0) {
618 now
.tv_usec
+= 1000000;
623 * Don't ship this packet out until the last
624 * packet has left the building.
625 * If we have not tried to send a packet for 1/5
626 * a second then we assume we lost an interrupt,
627 * lets go on and send the next packet anyhow.
629 * I suppose we could check to see if it is okay
630 * to put additional packets on the card (beyond
631 * the one already waiting to be sent) but I don't
632 * think we would get any improvement in speed as
633 * we should have ample time to put the next packet
634 * on while this one is going out.
636 if (sc
->sc_active
&& now
.tv_sec
== 0 && now
.tv_usec
< 200000)
640 /* Make sure the link integrity field is on */
642 lif
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
643 sc
->sc_memoff
+ CNW_EREG_LIF
);
646 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
647 printf("%s: link integrity %d\n", ifp
->if_xname
, lif
);
652 /* Is there any buffer space available on the card? */
654 #ifndef MEMORY_MAPPED
655 asr
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, CNW_REG_ASR
);
657 asr
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
658 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_ASR
);
660 if (!(asr
& CNW_ASR_TXBA
)) {
662 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
663 printf("%s: no buffer space\n", ifp
->if_xname
);
668 sc
->sc_stats
.nws_tx
++;
670 IFQ_DEQUEUE(&ifp
->if_snd
, m0
);
676 bpf_mtap(ifp
->if_bpf
, m0
);
679 cnw_transmit(sc
, m0
);
681 ifp
->if_timer
= 3; /* start watchdog timer */
683 microtime(&sc
->sc_txlast
);
687 ifp
->if_flags
&= ~IFF_OACTIVE
;
694 cnw_transmit(struct cnw_softc
*sc
, struct mbuf
*m0
)
696 int buffer
, bufsize
, bufoffset
, bufptr
, bufspace
, len
, mbytes
, n
;
700 /* Get buffer info from card */
701 buffer
= read16(sc
, CNW_EREG_TDP
);
702 bufsize
= read16(sc
, CNW_EREG_TDP
+ 2);
703 bufoffset
= read16(sc
, CNW_EREG_TDP
+ 4);
705 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
706 printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
707 device_xname(&sc
->sc_dev
), buffer
, bufsize
, bufoffset
);
710 /* Copy data from mbuf chain to card buffers */
711 bufptr
= sc
->sc_memoff
+ buffer
+ bufoffset
;
715 mptr
= mtod(m
, u_int8_t
*);
720 buffer
= read16(sc
, buffer
);
721 bufptr
= sc
->sc_memoff
+ buffer
+ bufoffset
;
724 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
725 printf("%s: next buffer @0x%x\n",
726 device_xname(&sc
->sc_dev
), buffer
);
729 n
= mbytes
<= bufspace
? mbytes
: bufspace
;
730 bus_space_write_region_1(sc
->sc_memt
, sc
->sc_memh
,
741 /* Issue transmit command */
742 CNW_CMD2(sc
, CNW_CMD_TL
, len
, len
>> 8);
747 * Pull a packet from the card into an mbuf chain.
750 cnw_read(struct cnw_softc
*sc
)
752 struct mbuf
*m
, *top
, **mp
;
753 int totbytes
, buffer
, bufbytes
, bufptr
, mbytes
, n
;
757 totbytes
= read16(sc
, CNW_EREG_RDP
);
759 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
760 printf("%s: recv %d bytes\n", device_xname(&sc
->sc_dev
), totbytes
);
762 buffer
= CNW_EREG_RDP
+ 2;
764 bufptr
= 0; /* XXX make gcc happy */
766 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
769 m
->m_pkthdr
.rcvif
= &sc
->sc_ethercom
.ec_if
;
770 m
->m_pkthdr
.len
= totbytes
;
775 while (totbytes
> 0) {
777 MGET(m
, M_DONTWAIT
, MT_DATA
);
784 if (totbytes
>= MINCLSIZE
) {
785 MCLGET(m
, M_DONTWAIT
);
786 if ((m
->m_flags
& M_EXT
) == 0) {
794 int pad
= ALIGN(sizeof(struct ether_header
)) -
795 sizeof(struct ether_header
);
799 mptr
= mtod(m
, u_int8_t
*);
800 mbytes
= m
->m_len
= min(totbytes
, mbytes
);
804 buffer
= read16(sc
, buffer
);
805 bufbytes
= read16(sc
, buffer
+ 2);
806 bufptr
= sc
->sc_memoff
+ buffer
+
807 read16(sc
, buffer
+ 4);
809 if (sc
->sc_ethercom
.ec_if
.if_flags
& IFF_DEBUG
)
810 printf("%s: %d bytes @0x%x+0x%lx\n",
811 device_xname(&sc
->sc_dev
), bufbytes
,
812 buffer
, (u_long
)(bufptr
- buffer
-
816 n
= mbytes
<= bufbytes
? mbytes
: bufbytes
;
817 bus_space_read_region_1(sc
->sc_memt
, sc
->sc_memh
,
833 * Handle received packets.
836 cnw_recv(struct cnw_softc
*sc
)
839 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
844 rser
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
845 sc
->sc_memoff
+ CNW_EREG_RSER
);
846 if (!(rser
& CNW_RSER_RXAVAIL
))
849 /* Pull packet off card */
852 /* Acknowledge packet */
853 CNW_CMD0(sc
, CNW_CMD_SRP
);
855 /* Did we manage to get the packet from the interface? */
864 bpf_mtap(ifp
->if_bpf
, m
);
867 /* Pass the packet up. */
868 (*ifp
->if_input
)(ifp
, m
);
879 struct cnw_softc
*sc
= arg
;
880 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
881 int ret
, status
, rser
, tser
;
883 if ((sc
->sc_ethercom
.ec_if
.if_flags
& IFF_RUNNING
) == 0 ||
884 !device_is_active(&sc
->sc_dev
))
886 ifp
->if_timer
= 0; /* stop watchdog timer */
891 #ifndef MEMORY_MAPPED
892 status
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
,
895 status
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
896 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_CCSR
);
898 if (!(status
& 0x02))
899 /* No more commands, or shared IRQ */
902 #ifndef MEMORY_MAPPED
903 status
= bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, CNW_REG_ASR
);
905 status
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
906 sc
->sc_memoff
+ CNW_IOM_OFF
+ CNW_REG_ASR
);
909 /* Anything to receive? */
910 if (status
& CNW_ASR_RXRDY
) {
911 sc
->sc_stats
.nws_rx
++;
916 if (status
& CNW_ASR_RXERR
) {
918 * I get a *lot* of spurious receive errors
919 * (many per second), even when the interface
920 * is quiescent, so we don't increment
923 rser
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
924 sc
->sc_memoff
+ CNW_EREG_RSER
);
927 sc
->sc_stats
.nws_rxerr
++;
928 if (rser
& CNW_RSER_RXBIG
)
929 sc
->sc_stats
.nws_rxframe
++;
930 if (rser
& CNW_RSER_RXCRC
)
931 sc
->sc_stats
.nws_rxcrcerror
++;
932 if (rser
& CNW_RSER_RXOVERRUN
)
933 sc
->sc_stats
.nws_rxoverrun
++;
934 if (rser
& CNW_RSER_RXOVERFLOW
)
935 sc
->sc_stats
.nws_rxoverflow
++;
936 if (rser
& CNW_RSER_RXERR
)
937 sc
->sc_stats
.nws_rxerrors
++;
938 if (rser
& CNW_RSER_RXAVAIL
)
939 sc
->sc_stats
.nws_rxavail
++;
941 /* Clear error bits in RSER */
943 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
944 sc
->sc_memoff
+ CNW_EREG_RSERW
,
946 (rser
& (CNW_RSER_RXCRC
| CNW_RSER_RXBIG
)));
947 /* Clear RXERR in ASR */
949 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
950 sc
->sc_memoff
+ CNW_EREG_ASCC
, CNW_ASR_RXERR
);
954 if (status
& CNW_ASR_TXDN
) {
955 tser
= bus_space_read_1(sc
->sc_memt
, sc
->sc_memh
,
959 if (tser
& CNW_TSER_TXERR
)
960 sc
->sc_stats
.nws_txerrors
++;
961 if (tser
& CNW_TSER_TXNOAP
)
962 sc
->sc_stats
.nws_txlostcd
++;
963 if (tser
& CNW_TSER_TXGU
)
964 sc
->sc_stats
.nws_txabort
++;
966 if (tser
& CNW_TSER_TXOK
) {
967 sc
->sc_stats
.nws_txokay
++;
968 sc
->sc_stats
.nws_txretries
[status
& 0xf]++;
970 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
971 sc
->sc_memoff
+ CNW_EREG_TSERW
,
972 CNW_TSER_TXOK
| CNW_TSER_RTRY
);
975 if (tser
& CNW_TSER_ERROR
) {
978 bus_space_write_1(sc
->sc_memt
, sc
->sc_memh
,
979 sc
->sc_memoff
+ CNW_EREG_TSERW
,
980 (tser
& CNW_TSER_ERROR
) |
985 ifp
->if_flags
&= ~IFF_OACTIVE
;
987 /* Continue to send packets from the queue */
988 cnw_start(&sc
->sc_ethercom
.ec_if
);
996 * Handle device ioctls.
999 cnw_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1001 struct cnw_softc
*sc
= ifp
->if_softc
;
1002 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
1003 struct ifreq
*ifr
= (struct ifreq
*)data
;
1005 struct lwp
*l
= curlwp
; /*XXX*/
1008 case SIOCINITIFADDR
:
1012 case SIOCGCNWDOMAIN
:
1015 case SIOCSCNWDOMAIN
:
1017 error
= kauth_authorize_network(l
->l_cred
,
1018 KAUTH_NETWORK_INTERFACE
,
1019 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
, ifp
, KAUTH_ARG(cmd
),
1024 case SIOCGCNWSTATUS
:
1025 error
= kauth_authorize_network(l
->l_cred
,
1026 KAUTH_NETWORK_INTERFACE
,
1027 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV
, ifp
, KAUTH_ARG(cmd
),
1040 case SIOCINITIFADDR
:
1041 if (!(ifp
->if_flags
& IFF_RUNNING
) &&
1042 (error
= cnw_enable(sc
)) != 0)
1044 ifp
->if_flags
|= IFF_UP
;
1046 switch (ifa
->ifa_addr
->sa_family
) {
1049 arp_ifinit(&sc
->sc_ethercom
.ec_if
, ifa
);
1058 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
1060 /* XXX re-use ether_ioctl() */
1061 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
1064 * The interface is marked down and it is running, so
1071 * The interface is marked up and it is stopped, so
1074 error
= cnw_enable(sc
);
1077 /* IFF_PROMISC may be changed */
1085 /* Update our multicast list. */
1086 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
1087 if (ifp
->if_flags
& IFF_RUNNING
)
1093 case SIOCGCNWDOMAIN
:
1094 ifr
->ifr_domain
= sc
->sc_domain
;
1097 case SIOCSCNWDOMAIN
:
1098 error
= cnw_setdomain(sc
, ifr
->ifr_domain
);
1102 error
= cnw_setkey(sc
, ifr
->ifr_key
);
1105 case SIOCGCNWSTATUS
:
1106 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
1108 bus_space_read_region_1(sc
->sc_memt
, sc
->sc_memh
,
1109 sc
->sc_memoff
+ CNW_EREG_CB
,
1110 ((struct cnwstatus
*)data
)->data
,
1111 sizeof(((struct cnwstatus
*)data
)->data
));
1115 memcpy((void *)&(((struct cnwistats
*)data
)->stats
),
1116 (void *)&sc
->sc_stats
, sizeof(struct cnwstats
));
1120 error
= ether_ioctl(ifp
, cmd
, data
);
1130 * Device timeout/watchdog routine. Entered if the device neglects to
1131 * generate an interrupt after a transmit has been started on it.
1134 cnw_watchdog(struct ifnet
*ifp
)
1136 struct cnw_softc
*sc
= ifp
->if_softc
;
1138 printf("%s: device timeout; card reset\n", device_xname(&sc
->sc_dev
));
1144 cnw_setdomain(struct cnw_softc
*sc
, int domain
)
1148 if (domain
& ~0x1ff)
1152 CNW_CMD2(sc
, CNW_CMD_SMD
, domain
, domain
>> 8);
1155 sc
->sc_domain
= domain
;
1160 cnw_setkey(struct cnw_softc
*sc
, int key
)
1168 CNW_CMD2(sc
, CNW_CMD_SSK
, key
, key
>> 8);
1176 cnw_activate(device_t self
, enum devact act
)
1178 struct cnw_softc
*sc
= (struct cnw_softc
*)self
;
1181 case DVACT_DEACTIVATE
:
1182 if_deactivate(&sc
->sc_ethercom
.ec_if
);
1190 cnw_detach(device_t self
, int flags
)
1192 struct cnw_softc
*sc
= (struct cnw_softc
*)self
;
1193 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
1195 /* cnw_disable() checks IFF_RUNNING */
1198 if ((sc
->sc_resource
& CNW_RES_NET
) != 0) {
1199 ether_ifdetach(ifp
);
1203 #ifndef MEMORY_MAPPED
1204 /* unmap and free our i/o windows */
1205 if ((sc
->sc_resource
& CNW_RES_IO
) != 0) {
1206 pcmcia_io_unmap(sc
->sc_pf
, sc
->sc_iowin
);
1207 pcmcia_io_free(sc
->sc_pf
, &sc
->sc_pcioh
);
1211 /* unmap and free our memory windows */
1212 if ((sc
->sc_resource
& CNW_RES_MEM
) != 0) {
1213 pcmcia_mem_unmap(sc
->sc_pf
, sc
->sc_memwin
);
1214 pcmcia_mem_free(sc
->sc_pf
, &sc
->sc_pcmemh
);