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