if_cnw.c revision 1.1.4.1 1 1.1.4.1 thorpej /* $NetBSD: if_cnw.c,v 1.1.4.1 1999/06/21 01:18:46 thorpej Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1998 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos /*
40 1.1 christos * This is a driver for the Xircom CreditCard Netwave (also known as
41 1.1 christos * the Netwave Airsurfer) wireless LAN PCMCIA adapter.
42 1.1 christos *
43 1.1 christos * When this driver was developed, the Linux Netwave driver was used
44 1.1 christos * as a hardware manual. That driver is Copyright (c) 1997 University
45 1.1 christos * of Troms, Norway. It is part of the Linix pcmcia-cs package that
46 1.1 christos * can be found at
47 1.1 christos * http://hyper.stanford.edu/HyperNews/get/pcmcia/home.html. The most
48 1.1 christos * recent version of the pcmcia-cs package when this driver was
49 1.1 christos * written was 3.0.6.
50 1.1 christos *
51 1.1 christos * Unfortunately, a lot of explicit numeric constants were used in the
52 1.1 christos * Linux driver. I have tried to use symbolic names whenever possible,
53 1.1 christos * but since I don't have any real hardware documentation, there's
54 1.1 christos * still one or two "magic numbers" :-(.
55 1.1 christos *
56 1.1 christos * Driver limitations: This driver doesn't do multicasting or receiver
57 1.1 christos * promiscuity, because of missing hardware documentation. I couldn't
58 1.1 christos * get receiver promiscuity to work, and I haven't even tried
59 1.1 christos * multicast. Volunteers are welcome, of course :-).
60 1.1 christos */
61 1.1 christos
62 1.1 christos #include "opt_inet.h"
63 1.1 christos #include "bpfilter.h"
64 1.1 christos
65 1.1 christos #include <sys/param.h>
66 1.1 christos #include <sys/systm.h>
67 1.1 christos #include <sys/device.h>
68 1.1 christos #include <sys/socket.h>
69 1.1 christos #include <sys/mbuf.h>
70 1.1 christos #include <sys/ioctl.h>
71 1.1 christos
72 1.1 christos #include <dev/pcmcia/if_cnwreg.h>
73 1.1 christos
74 1.1 christos #include <dev/pcmcia/pcmciareg.h>
75 1.1 christos #include <dev/pcmcia/pcmciavar.h>
76 1.1 christos #include <dev/pcmcia/pcmciadevs.h>
77 1.1 christos
78 1.1 christos #include <net/if.h>
79 1.1 christos #include <net/if_dl.h>
80 1.1 christos #include <net/if_ether.h>
81 1.1 christos
82 1.1 christos #ifdef INET
83 1.1 christos #include <netinet/in.h>
84 1.1 christos #include <netinet/in_systm.h>
85 1.1 christos #include <netinet/in_var.h>
86 1.1 christos #include <netinet/ip.h>
87 1.1 christos #include <netinet/if_inarp.h>
88 1.1 christos #endif
89 1.1 christos
90 1.1 christos #if NBPFILTER > 0
91 1.1 christos #include <net/bpf.h>
92 1.1 christos #include <net/bpfdesc.h>
93 1.1 christos #endif
94 1.1 christos
95 1.1 christos
96 1.1 christos /*
97 1.1 christos * Let these be patchable variables, initialized from macros that can
98 1.1 christos * be set in the kernel config file. Someone with lots of spare time
99 1.1 christos * could probably write a nice Netwave configuration program to do
100 1.1 christos * this a little bit more elegantly :-).
101 1.1 christos */
102 1.1 christos #ifndef CNW_DOMAIN
103 1.1 christos #define CNW_DOMAIN 0x100
104 1.1 christos #endif
105 1.1 christos int cnw_domain = CNW_DOMAIN; /* Domain */
106 1.1 christos #ifndef CNW_SCRAMBLEKEY
107 1.1 christos #define CNW_SCRAMBLEKEY 0
108 1.1 christos #endif
109 1.1 christos int cnw_skey = CNW_SCRAMBLEKEY; /* Scramble key */
110 1.1 christos
111 1.1 christos
112 1.1 christos int cnw_match __P((struct device *, struct cfdata *, void *));
113 1.1 christos void cnw_attach __P((struct device *, struct device *, void *));
114 1.1 christos
115 1.1 christos struct cnw_softc {
116 1.1 christos struct device sc_dev; /* Device glue (must be first) */
117 1.1 christos struct ethercom sc_ethercom; /* Ethernet common part */
118 1.1 christos int sc_domain; /* Netwave domain */
119 1.1 christos int sc_skey; /* Netwave scramble key */
120 1.1 christos
121 1.1 christos /* PCMCIA-specific stuff */
122 1.1 christos struct pcmcia_function *sc_pf; /* PCMCIA function */
123 1.1 christos struct pcmcia_io_handle sc_pcioh; /* PCMCIA I/O space handle */
124 1.1 christos int sc_iowin; /* ...window */
125 1.1 christos bus_space_tag_t sc_iot; /* ...bus_space tag */
126 1.1 christos bus_space_handle_t sc_ioh; /* ...bus_space handle */
127 1.1 christos struct pcmcia_mem_handle sc_pcmemh; /* PCMCIA memory handle */
128 1.1 christos bus_addr_t sc_memoff; /* ...offset */
129 1.1 christos int sc_memwin; /* ...window */
130 1.1 christos bus_space_tag_t sc_memt; /* ...bus_space tag */
131 1.1 christos bus_space_handle_t sc_memh; /* ...bus_space handle */
132 1.1 christos void *sc_ih; /* Interrupt cookie */
133 1.1 christos };
134 1.1 christos
135 1.1 christos struct cfattach cnw_ca = {
136 1.1 christos sizeof(struct cnw_softc), cnw_match, cnw_attach
137 1.1 christos };
138 1.1 christos
139 1.1 christos
140 1.1 christos void cnw_reset __P((struct cnw_softc *));
141 1.1 christos void cnw_init __P((struct cnw_softc *));
142 1.1 christos int cnw_enable __P((struct cnw_softc *sc));
143 1.1 christos void cnw_disable __P((struct cnw_softc *sc));
144 1.1 christos void cnw_config __P((struct cnw_softc *sc, u_int8_t *));
145 1.1 christos void cnw_start __P((struct ifnet *));
146 1.1 christos void cnw_transmit __P((struct cnw_softc *, struct mbuf *));
147 1.1 christos struct mbuf *cnw_read __P((struct cnw_softc *));
148 1.1 christos void cnw_recv __P((struct cnw_softc *));
149 1.1 christos int cnw_intr __P((void *arg));
150 1.1 christos int cnw_ioctl __P((struct ifnet *, u_long, caddr_t));
151 1.1 christos void cnw_watchdog __P((struct ifnet *));
152 1.1 christos
153 1.1 christos /* ---------------------------------------------------------------- */
154 1.1 christos
155 1.1 christos /* Help routines */
156 1.1 christos static int wait_WOC __P((struct cnw_softc *, int));
157 1.1 christos static int read16 __P((struct cnw_softc *, int));
158 1.1 christos static int cnw_cmd __P((struct cnw_softc *, int, int, int, int));
159 1.1 christos
160 1.1 christos /*
161 1.1 christos * Wait until the WOC (Write Operation Complete) bit in the
162 1.1 christos * ASR (Adapter Status Register) is asserted.
163 1.1 christos */
164 1.1 christos static int
165 1.1 christos wait_WOC(sc, line)
166 1.1 christos struct cnw_softc *sc;
167 1.1 christos int line;
168 1.1 christos {
169 1.1 christos int i, asr;
170 1.1 christos
171 1.1 christos for (i = 0; i < 5000; i++) {
172 1.1 christos asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
173 1.1 christos if (asr & CNW_ASR_WOC)
174 1.1 christos return (0);
175 1.1 christos DELAY(100);
176 1.1 christos }
177 1.1 christos if (line > 0)
178 1.1 christos printf("%s: wedged at line %d\n", sc->sc_dev.dv_xname, line);
179 1.1 christos return (1);
180 1.1 christos }
181 1.1 christos #define WAIT_WOC(sc) wait_WOC(sc, __LINE__)
182 1.1 christos
183 1.1 christos
184 1.1 christos /*
185 1.1 christos * Read a 16 bit value from the card.
186 1.1 christos */
187 1.1 christos static int
188 1.1 christos read16(sc, offset)
189 1.1 christos struct cnw_softc *sc;
190 1.1 christos int offset;
191 1.1 christos {
192 1.1 christos int hi, lo;
193 1.1 christos int offs = sc->sc_memoff + offset;
194 1.1 christos
195 1.1 christos /* This could presumably be done more efficient with
196 1.1 christos * bus_space_read_2(), but I don't know anything about the
197 1.1 christos * byte sex guarantees... Besides, this is pretty cheap as
198 1.1 christos * well :-)
199 1.1 christos */
200 1.1 christos lo = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs);
201 1.1 christos hi = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs + 1);
202 1.1 christos return ((hi << 8) | lo);
203 1.1 christos }
204 1.1 christos
205 1.1 christos
206 1.1 christos /*
207 1.1 christos * Send a command to the card by writing it to the command buffer.
208 1.1 christos */
209 1.1 christos int
210 1.1 christos cnw_cmd(sc, cmd, count, arg1, arg2)
211 1.1 christos struct cnw_softc *sc;
212 1.1 christos int cmd, count, arg1, arg2;
213 1.1 christos {
214 1.1 christos int ptr = sc->sc_memoff + CNW_EREG_CB;
215 1.1 christos
216 1.1 christos if (wait_WOC(sc, 0)) {
217 1.1 christos printf("%s: wedged when issuing cmd 0x%x\n",
218 1.1 christos sc->sc_dev.dv_xname, cmd);
219 1.1 christos /*
220 1.1 christos * We'll continue anyway, as that's probably the best
221 1.1 christos * thing we can do; at least the user knows there's a
222 1.1 christos * problem, and can reset the interface with ifconfig
223 1.1 christos * down/up.
224 1.1 christos */
225 1.1 christos }
226 1.1 christos
227 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd);
228 1.1 christos if (count > 0) {
229 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1);
230 1.1 christos if (count > 1)
231 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
232 1.1 christos ptr + 2, arg2);
233 1.1 christos }
234 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
235 1.1 christos ptr + count + 1, CNW_CMD_EOC);
236 1.1 christos return (0);
237 1.1 christos }
238 1.1 christos #define CNW_CMD0(sc, cmd) \
239 1.1 christos do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
240 1.1 christos #define CNW_CMD1(sc, cmd, arg1) \
241 1.1 christos do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
242 1.1 christos #define CNW_CMD2(sc, cmd, arg1, arg2) \
243 1.1 christos do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
244 1.1 christos
245 1.1 christos /* ---------------------------------------------------------------- */
246 1.1 christos
247 1.1 christos /*
248 1.1 christos * Reset the hardware.
249 1.1 christos */
250 1.1 christos void
251 1.1 christos cnw_reset(sc)
252 1.1 christos struct cnw_softc *sc;
253 1.1 christos {
254 1.1 christos #ifdef CNW_DEBUG
255 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
256 1.1 christos printf("%s: resetting\n", sc->sc_dev.dv_xname);
257 1.1 christos #endif
258 1.1 christos wait_WOC(sc, 0);
259 1.1 christos bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET);
260 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
261 1.1 christos sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC);
262 1.1 christos bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0);
263 1.1 christos }
264 1.1 christos
265 1.1 christos
266 1.1 christos /*
267 1.1 christos * Initialize the card.
268 1.1 christos */
269 1.1 christos void
270 1.1 christos cnw_init(sc)
271 1.1 christos struct cnw_softc *sc;
272 1.1 christos {
273 1.1 christos /* Reset the card */
274 1.1 christos cnw_reset(sc);
275 1.1 christos
276 1.1 christos /* Issue a NOP to check the card */
277 1.1 christos CNW_CMD0(sc, CNW_CMD_NOP);
278 1.1 christos
279 1.1 christos /* Set up receive configuration */
280 1.1 christos CNW_CMD1(sc, CNW_CMD_SRC, CNW_RXCONF_RXENA | CNW_RXCONF_BCAST);
281 1.1 christos
282 1.1 christos /* Set up transmit configuration */
283 1.1 christos CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA);
284 1.1 christos
285 1.1 christos /* Set domain */
286 1.1 christos CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8);
287 1.1 christos
288 1.1 christos /* Set scramble key */
289 1.1 christos CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8);
290 1.1 christos
291 1.1 christos /* Enable interrupts */
292 1.1 christos WAIT_WOC(sc);
293 1.1 christos bus_space_write_1(sc->sc_iot, sc->sc_ioh,
294 1.1 christos CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1);
295 1.1 christos
296 1.1 christos /* Enable receiver */
297 1.1 christos CNW_CMD0(sc, CNW_CMD_ER);
298 1.1 christos
299 1.1 christos /* "Set the IENA bit in COR" */
300 1.1 christos WAIT_WOC(sc);
301 1.1 christos bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR,
302 1.1 christos CNW_COR_IENA | CNW_COR_LVLREQ);
303 1.1 christos }
304 1.1 christos
305 1.1 christos
306 1.1 christos /*
307 1.1 christos * Enable and initialize the card.
308 1.1 christos */
309 1.1 christos int
310 1.1 christos cnw_enable(sc)
311 1.1 christos struct cnw_softc *sc;
312 1.1 christos {
313 1.1 christos struct ifnet *ifp = &sc->sc_ethercom.ec_if;
314 1.1 christos
315 1.1 christos sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc);
316 1.1 christos if (sc->sc_ih == NULL) {
317 1.1 christos printf("%s: couldn't establish interrupt handler\n",
318 1.1 christos sc->sc_dev.dv_xname);
319 1.1 christos return (EIO);
320 1.1 christos }
321 1.1 christos if (pcmcia_function_enable(sc->sc_pf) != 0) {
322 1.1 christos printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
323 1.1 christos return (EIO);
324 1.1 christos }
325 1.1 christos cnw_init(sc);
326 1.1 christos ifp->if_flags |= IFF_RUNNING;
327 1.1 christos return (0);
328 1.1 christos }
329 1.1 christos
330 1.1 christos
331 1.1 christos /*
332 1.1 christos * Stop and disable the card.
333 1.1 christos */
334 1.1 christos void
335 1.1 christos cnw_disable(sc)
336 1.1 christos struct cnw_softc *sc;
337 1.1 christos {
338 1.1 christos struct ifnet *ifp = &sc->sc_ethercom.ec_if;
339 1.1 christos
340 1.1 christos pcmcia_function_disable(sc->sc_pf);
341 1.1 christos pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
342 1.1 christos ifp->if_flags &= ~IFF_RUNNING;
343 1.1 christos ifp->if_timer = 0;
344 1.1 christos }
345 1.1 christos
346 1.1 christos
347 1.1 christos /*
348 1.1 christos * Match the hardware we handle.
349 1.1 christos */
350 1.1 christos int
351 1.1 christos cnw_match(parent, match, aux)
352 1.1 christos struct device *parent;
353 1.1 christos struct cfdata *match;
354 1.1 christos void *aux;
355 1.1 christos {
356 1.1 christos struct pcmcia_attach_args *pa = aux;
357 1.1 christos
358 1.1 christos return (pa->manufacturer == PCMCIA_VENDOR_TDK &&
359 1.1 christos pa->product == PCMCIA_PRODUCT_TDK_XIR_CNW);
360 1.1 christos }
361 1.1 christos
362 1.1 christos
363 1.1 christos /*
364 1.1 christos * Attach the card.
365 1.1 christos */
366 1.1 christos void
367 1.1 christos cnw_attach(parent, self, aux)
368 1.1 christos struct device *parent, *self;
369 1.1 christos void *aux;
370 1.1 christos {
371 1.1 christos struct cnw_softc *sc = (void *) self;
372 1.1 christos struct pcmcia_attach_args *pa = aux;
373 1.1 christos struct ifnet *ifp = &sc->sc_ethercom.ec_if;
374 1.1 christos u_int8_t macaddr[ETHER_ADDR_LEN];
375 1.1 christos int i;
376 1.1 christos
377 1.1 christos /* Enable the card */
378 1.1 christos sc->sc_pf = pa->pf;
379 1.1 christos pcmcia_function_init(sc->sc_pf, sc->sc_pf->cfe_head.sqh_first);
380 1.1 christos if (pcmcia_function_enable(sc->sc_pf)) {
381 1.1 christos printf(": function enable failed\n");
382 1.1 christos return;
383 1.1 christos }
384 1.1 christos
385 1.1 christos /* Map I/O register and "memory" */
386 1.1 christos if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
387 1.1 christos &sc->sc_pcioh) != 0) {
388 1.1 christos printf(": can't allocate i/o space\n");
389 1.1 christos return;
390 1.1 christos }
391 1.1 christos if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, 0,
392 1.1 christos CNW_IO_SIZE, &sc->sc_pcioh, &sc->sc_iowin) != 0) {
393 1.1 christos printf(": can't map i/o space\n");
394 1.1 christos return;
395 1.1 christos }
396 1.1 christos sc->sc_iot = sc->sc_pcioh.iot;
397 1.1 christos sc->sc_ioh = sc->sc_pcioh.ioh;
398 1.1 christos if (pcmcia_mem_alloc(sc->sc_pf, CNW_MEM_SIZE, &sc->sc_pcmemh) != 0) {
399 1.1 christos printf(": can't allocate memory\n");
400 1.1 christos return;
401 1.1 christos }
402 1.1 christos if (pcmcia_mem_map(sc->sc_pf, PCMCIA_MEM_COMMON, CNW_MEM_ADDR,
403 1.1 christos CNW_MEM_SIZE, &sc->sc_pcmemh, &sc->sc_memoff,
404 1.1 christos &sc->sc_memwin) != 0) {
405 1.1 christos printf(": can't map memory\n");
406 1.1 christos return;
407 1.1 christos }
408 1.1 christos sc->sc_memt = sc->sc_pcmemh.memt;
409 1.1 christos sc->sc_memh = sc->sc_pcmemh.memh;
410 1.1 christos printf(": %s\n", PCMCIA_STR_TDK_XIR_CNW);
411 1.1 christos
412 1.1 christos /* Finish setup of softc */
413 1.1 christos sc->sc_domain = cnw_domain;
414 1.1 christos sc->sc_skey = cnw_skey;
415 1.1 christos
416 1.1 christos /* Get MAC address */
417 1.1 christos cnw_reset(sc);
418 1.1 christos for (i = 0; i < ETHER_ADDR_LEN; i++)
419 1.1 christos macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
420 1.1 christos sc->sc_memoff + CNW_EREG_PA + i);
421 1.1 christos printf("%s: address %s\n", sc->sc_dev.dv_xname,
422 1.1 christos ether_sprintf(macaddr));
423 1.1 christos
424 1.1 christos /* Set up ifnet structure */
425 1.1 christos bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
426 1.1 christos ifp->if_softc = sc;
427 1.1 christos ifp->if_start = cnw_start;
428 1.1 christos ifp->if_ioctl = cnw_ioctl;
429 1.1 christos ifp->if_watchdog = cnw_watchdog;
430 1.1 christos ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
431 1.1 christos
432 1.1 christos /* Attach the interface */
433 1.1 christos if_attach(ifp);
434 1.1 christos ether_ifattach(ifp, macaddr);
435 1.1 christos #if NBPFILTER > 0
436 1.1 christos bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
437 1.1 christos sizeof(struct ether_header));
438 1.1 christos #endif
439 1.1 christos
440 1.1 christos /* Disable the card now, and turn it on when the interface goes up */
441 1.1 christos pcmcia_function_disable(sc->sc_pf);
442 1.1 christos }
443 1.1 christos
444 1.1 christos /*
445 1.1 christos * Start outputting on the interface.
446 1.1 christos */
447 1.1 christos void
448 1.1 christos cnw_start(ifp)
449 1.1 christos struct ifnet *ifp;
450 1.1 christos {
451 1.1 christos struct cnw_softc *sc = ifp->if_softc;
452 1.1 christos struct mbuf *m0;
453 1.1 christos int asr;
454 1.1 christos
455 1.1 christos #ifdef CNW_DEBUG
456 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
457 1.1 christos printf("%s: cnw_start\n", ifp->if_xname);
458 1.1 christos #endif
459 1.1 christos
460 1.1 christos for (;;) {
461 1.1 christos /* Is there any buffer space available on the card? */
462 1.1 christos WAIT_WOC(sc);
463 1.1 christos asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
464 1.1 christos if (!(asr & CNW_ASR_TXBA)) {
465 1.1 christos #ifdef CNW_DEBUG
466 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
467 1.1 christos printf("%s: no buffer space\n", ifp->if_xname);
468 1.1 christos #endif
469 1.1 christos return;
470 1.1 christos }
471 1.1 christos
472 1.1 christos IF_DEQUEUE(&ifp->if_snd, m0);
473 1.1 christos if (m0 == 0)
474 1.1 christos return;
475 1.1 christos
476 1.1 christos #if NBPFILTER > 0
477 1.1 christos if (ifp->if_bpf)
478 1.1 christos bpf_mtap(ifp->if_bpf, m0);
479 1.1 christos #endif
480 1.1 christos
481 1.1 christos cnw_transmit(sc, m0);
482 1.1 christos ++ifp->if_opackets;
483 1.1 christos ifp->if_timer = 3; /* start watchdog timer */
484 1.1 christos }
485 1.1 christos }
486 1.1 christos
487 1.1 christos
488 1.1 christos /*
489 1.1 christos * Transmit a packet.
490 1.1 christos */
491 1.1 christos void
492 1.1 christos cnw_transmit(sc, m0)
493 1.1 christos struct cnw_softc *sc;
494 1.1 christos struct mbuf *m0;
495 1.1 christos {
496 1.1 christos int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
497 1.1 christos struct mbuf *m;
498 1.1 christos u_int8_t *mptr;
499 1.1 christos
500 1.1 christos /* Get buffer info from card */
501 1.1 christos buffer = read16(sc, CNW_EREG_TDP);
502 1.1 christos bufsize = read16(sc, CNW_EREG_TDP + 2);
503 1.1 christos bufoffset = read16(sc, CNW_EREG_TDP + 4);
504 1.1 christos #ifdef CNW_DEBUG
505 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
506 1.1 christos printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
507 1.1 christos sc->sc_dev.dv_xname, buffer, bufsize, bufoffset);
508 1.1 christos #endif
509 1.1 christos
510 1.1 christos /* Copy data from mbuf chain to card buffers */
511 1.1 christos bufptr = sc->sc_memoff + buffer + bufoffset;
512 1.1 christos bufspace = bufsize;
513 1.1 christos len = 0;
514 1.1 christos for (m = m0; m; ) {
515 1.1 christos mptr = mtod(m, u_int8_t *);
516 1.1 christos mbytes = m->m_len;
517 1.1 christos len += mbytes;
518 1.1 christos while (mbytes > 0) {
519 1.1 christos if (bufspace == 0) {
520 1.1 christos buffer = read16(sc, buffer);
521 1.1 christos bufptr = sc->sc_memoff + buffer + bufoffset;
522 1.1 christos bufspace = bufsize;
523 1.1 christos #ifdef CNW_DEBUG
524 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
525 1.1 christos printf("%s: next buffer @0x%x\n",
526 1.1 christos sc->sc_dev.dv_xname, buffer);
527 1.1 christos #endif
528 1.1 christos }
529 1.1 christos n = mbytes <= bufspace ? mbytes : bufspace;
530 1.1 christos bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
531 1.1 christos bufptr, mptr, n);
532 1.1 christos bufptr += n;
533 1.1 christos bufspace -= n;
534 1.1 christos mptr += n;
535 1.1 christos mbytes -= n;
536 1.1 christos }
537 1.1 christos MFREE(m, m0);
538 1.1 christos m = m0;
539 1.1 christos }
540 1.1 christos
541 1.1 christos /* Issue transmit command */
542 1.1 christos CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
543 1.1 christos }
544 1.1 christos
545 1.1 christos
546 1.1 christos /*
547 1.1 christos * Pull a packet from the card into an mbuf chain.
548 1.1 christos */
549 1.1 christos struct mbuf *
550 1.1 christos cnw_read(sc)
551 1.1 christos struct cnw_softc *sc;
552 1.1 christos {
553 1.1 christos struct mbuf *m, *top, **mp;
554 1.1 christos int totbytes, buffer, bufbytes, bufptr, mbytes, n;
555 1.1 christos u_int8_t *mptr;
556 1.1 christos
557 1.1 christos WAIT_WOC(sc);
558 1.1 christos totbytes = read16(sc, CNW_EREG_RDP);
559 1.1 christos #ifdef CNW_DEBUG
560 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
561 1.1 christos printf("%s: recv %d bytes\n", sc->sc_dev.dv_xname, totbytes);
562 1.1 christos #endif
563 1.1 christos buffer = CNW_EREG_RDP + 2;
564 1.1 christos bufbytes = 0;
565 1.1 christos bufptr = 0; /* XXX make gcc happy */
566 1.1 christos
567 1.1 christos MGETHDR(m, M_DONTWAIT, MT_DATA);
568 1.1 christos if (m == 0)
569 1.1 christos return (0);
570 1.1 christos m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
571 1.1 christos m->m_pkthdr.len = totbytes;
572 1.1 christos mbytes = MHLEN;
573 1.1 christos top = 0;
574 1.1 christos mp = ⊤
575 1.1 christos
576 1.1 christos while (totbytes > 0) {
577 1.1 christos if (top) {
578 1.1 christos MGET(m, M_DONTWAIT, MT_DATA);
579 1.1 christos if (m == 0) {
580 1.1 christos m_freem(top);
581 1.1 christos return (0);
582 1.1 christos }
583 1.1 christos mbytes = MLEN;
584 1.1 christos }
585 1.1 christos if (totbytes >= MINCLSIZE) {
586 1.1 christos MCLGET(m, M_DONTWAIT);
587 1.1 christos if ((m->m_flags & M_EXT) == 0) {
588 1.1 christos m_free(m);
589 1.1 christos m_freem(top);
590 1.1 christos return (0);
591 1.1 christos }
592 1.1 christos mbytes = MCLBYTES;
593 1.1 christos }
594 1.1 christos if (!top) {
595 1.1 christos int pad = ALIGN(sizeof(struct ether_header)) -
596 1.1 christos sizeof(struct ether_header);
597 1.1 christos m->m_data += pad;
598 1.1 christos mbytes -= pad;
599 1.1 christos }
600 1.1 christos mptr = mtod(m, u_int8_t *);
601 1.1 christos mbytes = m->m_len = min(totbytes, mbytes);
602 1.1 christos totbytes -= mbytes;
603 1.1 christos while (mbytes > 0) {
604 1.1 christos if (bufbytes == 0) {
605 1.1 christos buffer = read16(sc, buffer);
606 1.1 christos bufbytes = read16(sc, buffer + 2);
607 1.1 christos bufptr = sc->sc_memoff + buffer +
608 1.1 christos read16(sc, buffer + 4);
609 1.1 christos #ifdef CNW_DEBUG
610 1.1 christos if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
611 1.1 christos printf("%s: %d bytes @0x%x+0x%x\n",
612 1.1 christos sc->sc_dev.dv_xname, bufbytes,
613 1.1 christos buffer, bufptr - buffer -
614 1.1 christos sc->sc_memoff);
615 1.1 christos #endif
616 1.1 christos }
617 1.1 christos n = mbytes <= bufbytes ? mbytes : bufbytes;
618 1.1 christos bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
619 1.1 christos bufptr, mptr, n);
620 1.1 christos bufbytes -= n;
621 1.1 christos bufptr += n;
622 1.1 christos mbytes -= n;
623 1.1 christos mptr += n;
624 1.1 christos }
625 1.1 christos *mp = m;
626 1.1 christos mp = &m->m_next;
627 1.1 christos }
628 1.1 christos
629 1.1 christos return (top);
630 1.1 christos }
631 1.1 christos
632 1.1 christos
633 1.1 christos /*
634 1.1 christos * Handle received packets.
635 1.1 christos */
636 1.1 christos void
637 1.1 christos cnw_recv(sc)
638 1.1 christos struct cnw_softc *sc;
639 1.1 christos {
640 1.1 christos int rser;
641 1.1 christos struct ifnet *ifp = &sc->sc_ethercom.ec_if;
642 1.1 christos struct mbuf *m;
643 1.1 christos struct ether_header *eh;
644 1.1 christos
645 1.1 christos for (;;) {
646 1.1 christos WAIT_WOC(sc);
647 1.1 christos rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
648 1.1 christos sc->sc_memoff + CNW_EREG_RSER);
649 1.1 christos if (!(rser & CNW_RSER_RXAVAIL))
650 1.1 christos return;
651 1.1 christos
652 1.1 christos /* Pull packet off card */
653 1.1 christos m = cnw_read(sc);
654 1.1 christos
655 1.1 christos /* Acknowledge packet */
656 1.1 christos CNW_CMD0(sc, CNW_CMD_SRP);
657 1.1 christos
658 1.1 christos /* Did we manage to get the packet from the interface? */
659 1.1 christos if (m == 0) {
660 1.1 christos ++ifp->if_ierrors;
661 1.1 christos return;
662 1.1 christos }
663 1.1 christos ++ifp->if_ipackets;
664 1.1 christos
665 1.1 christos #if NBPFILTER > 0
666 1.1 christos if (ifp->if_bpf)
667 1.1 christos bpf_mtap(ifp->if_bpf, m);
668 1.1 christos #endif
669 1.1 christos
670 1.1 christos /*
671 1.1 christos * Check that the packet is for us or {multi,broad}cast. Maybe
672 1.1 christos * there's a fool-poof hardware check for this, but I don't
673 1.1 christos * really know...
674 1.1 christos */
675 1.1 christos eh = mtod(m, struct ether_header *);
676 1.1 christos if ((eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
677 1.1 christos bcmp(LLADDR(sc->sc_ethercom.ec_if.if_sadl),
678 1.1 christos eh->ether_dhost, sizeof(eh->ether_dhost)) != 0) {
679 1.1 christos m_freem(m);
680 1.1 christos continue;
681 1.1 christos }
682 1.1 christos
683 1.1.4.1 thorpej /* Pass the packet up. */
684 1.1.4.1 thorpej (*ifp->if_input)(ifp, m);
685 1.1 christos }
686 1.1 christos }
687 1.1 christos
688 1.1 christos
689 1.1 christos /*
690 1.1 christos * Interrupt handler.
691 1.1 christos */
692 1.1 christos int
693 1.1 christos cnw_intr(arg)
694 1.1 christos void *arg;
695 1.1 christos {
696 1.1 christos struct cnw_softc *sc = arg;
697 1.1 christos struct ifnet *ifp = &sc->sc_ethercom.ec_if;
698 1.1 christos int ret, status, rser, tser;
699 1.1 christos
700 1.1 christos if (!(sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING))
701 1.1 christos return (0);
702 1.1 christos ifp->if_timer = 0; /* stop watchdog timer */
703 1.1 christos
704 1.1 christos ret = 0;
705 1.1 christos for (;;) {
706 1.1 christos WAIT_WOC(sc);
707 1.1 christos if (!(bus_space_read_1(sc->sc_iot, sc->sc_ioh,
708 1.1 christos CNW_REG_CCSR) & 0x02)) {
709 1.1 christos if (ret == 0)
710 1.1 christos printf("%s: spurious interrupt\n",
711 1.1 christos sc->sc_dev.dv_xname);
712 1.1 christos return (ret);
713 1.1 christos }
714 1.1 christos ret = 1;
715 1.1 christos status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
716 1.1 christos
717 1.1 christos /* Anything to receive? */
718 1.1 christos if (status & CNW_ASR_RXRDY)
719 1.1 christos cnw_recv(sc);
720 1.1 christos
721 1.1 christos /* Receive error */
722 1.1 christos if (status & CNW_ASR_RXERR) {
723 1.1 christos /*
724 1.1 christos * I get a *lot* of spurious receive errors
725 1.1 christos * (many per second), even when the interface
726 1.1 christos * is quiescent, so we don't increment
727 1.1 christos * if_ierrors here.
728 1.1 christos */
729 1.1 christos rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
730 1.1 christos sc->sc_memoff + CNW_EREG_RSER);
731 1.1 christos /* Clear error bits in RSER */
732 1.1 christos WAIT_WOC(sc);
733 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
734 1.1 christos sc->sc_memoff + CNW_EREG_RSERW,
735 1.1 christos CNW_RSER_RXERR |
736 1.1 christos (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
737 1.1 christos /* Clear RXERR in ASR */
738 1.1 christos WAIT_WOC(sc);
739 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
740 1.1 christos sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
741 1.1 christos }
742 1.1 christos
743 1.1 christos /* Transmit done */
744 1.1 christos if (status & CNW_ASR_TXDN) {
745 1.1 christos tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
746 1.1 christos CNW_EREG_TSER);
747 1.1 christos if (tser & CNW_TSER_TXOK) {
748 1.1 christos WAIT_WOC(sc);
749 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
750 1.1 christos sc->sc_memoff + CNW_EREG_TSERW,
751 1.1 christos CNW_TSER_TXOK | CNW_TSER_RTRY);
752 1.1 christos }
753 1.1 christos if (tser & CNW_TSER_ERROR) {
754 1.1 christos ++ifp->if_oerrors;
755 1.1 christos WAIT_WOC(sc);
756 1.1 christos bus_space_write_1(sc->sc_memt, sc->sc_memh,
757 1.1 christos sc->sc_memoff + CNW_EREG_TSERW,
758 1.1 christos (tser & CNW_TSER_ERROR) |
759 1.1 christos CNW_TSER_RTRY);
760 1.1 christos }
761 1.1 christos /* Continue to send packets from the queue */
762 1.1 christos cnw_start(&sc->sc_ethercom.ec_if);
763 1.1 christos }
764 1.1 christos
765 1.1 christos }
766 1.1 christos }
767 1.1 christos
768 1.1 christos
769 1.1 christos /*
770 1.1 christos * Handle device ioctls.
771 1.1 christos */
772 1.1 christos int
773 1.1 christos cnw_ioctl(ifp, cmd, data)
774 1.1 christos register struct ifnet *ifp;
775 1.1 christos u_long cmd;
776 1.1 christos caddr_t data;
777 1.1 christos {
778 1.1 christos struct cnw_softc *sc = ifp->if_softc;
779 1.1 christos struct ifaddr *ifa = (struct ifaddr *)data;
780 1.1 christos int s, error = 0;
781 1.1 christos
782 1.1 christos s = splnet();
783 1.1 christos
784 1.1 christos switch (cmd) {
785 1.1 christos
786 1.1 christos case SIOCSIFADDR:
787 1.1 christos if (!(ifp->if_flags & IFF_RUNNING) &&
788 1.1 christos (error = cnw_enable(sc)) != 0)
789 1.1 christos break;
790 1.1 christos ifp->if_flags |= IFF_UP;
791 1.1 christos switch (ifa->ifa_addr->sa_family) {
792 1.1 christos #ifdef INET
793 1.1 christos case AF_INET:
794 1.1 christos arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
795 1.1 christos break;
796 1.1 christos #endif
797 1.1 christos }
798 1.1 christos break;
799 1.1 christos
800 1.1 christos case SIOCSIFFLAGS:
801 1.1 christos if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
802 1.1 christos /*
803 1.1 christos * The interface is marked down and it is running, so
804 1.1 christos * stop it.
805 1.1 christos */
806 1.1 christos cnw_disable(sc);
807 1.1 christos } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP){
808 1.1 christos /*
809 1.1 christos * The interface is marked up and it is stopped, so
810 1.1 christos * start it.
811 1.1 christos */
812 1.1 christos error = cnw_enable(sc);
813 1.1 christos }
814 1.1 christos break;
815 1.1 christos
816 1.1 christos default:
817 1.1 christos error = EINVAL;
818 1.1 christos break;
819 1.1 christos }
820 1.1 christos
821 1.1 christos splx(s);
822 1.1 christos return (error);
823 1.1 christos }
824 1.1 christos
825 1.1 christos
826 1.1 christos /*
827 1.1 christos * Device timeout/watchdog routine. Entered if the device neglects to
828 1.1 christos * generate an interrupt after a transmit has been started on it.
829 1.1 christos */
830 1.1 christos void
831 1.1 christos cnw_watchdog(ifp)
832 1.1 christos struct ifnet *ifp;
833 1.1 christos {
834 1.1 christos struct cnw_softc *sc = ifp->if_softc;
835 1.1 christos
836 1.1 christos printf("%s: device timeout; card reset\n", sc->sc_dev.dv_xname);
837 1.1 christos ++ifp->if_oerrors;
838 1.1 christos cnw_init(sc);
839 1.1 christos }
840