if_awi_pcmcia.c revision 1.12 1 /* $NetBSD: if_awi_pcmcia.c,v 1.12 2000/02/17 15:58:32 sommerfeld Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
41 * based on the AMD 79c930 802.11 controller chip.
42 *
43 * This attachment can probably be trivally adapted for other FH and
44 * DS cards based on the same chipset.
45 */
46
47 #include "opt_inet.h"
48 #include "opt_ns.h"
49 #include "bpfilter.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/syslog.h>
58 #include <sys/select.h>
59 #include <sys/device.h>
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_ether.h>
64 #include <net/if_media.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #include <netinet/if_inarp.h>
72 #endif
73
74 #ifdef NS
75 #include <netns/ns.h>
76 #include <netns/ns_if.h>
77 #endif
78
79 #if NBPFILTER > 0
80 #include <net/bpf.h>
81 #include <net/bpfdesc.h>
82 #endif
83
84 #include <machine/cpu.h>
85 #include <machine/bus.h>
86 #include <machine/intr.h>
87
88 #include <dev/ic/am79c930reg.h>
89 #include <dev/ic/am79c930var.h>
90 #include <dev/ic/awireg.h>
91 #include <dev/ic/awivar.h>
92
93 #include <dev/pcmcia/pcmciareg.h>
94 #include <dev/pcmcia/pcmciavar.h>
95 #include <dev/pcmcia/pcmciadevs.h>
96
97 int awi_pcmcia_match __P((struct device *, struct cfdata *, void *));
98 void awi_pcmcia_attach __P((struct device *, struct device *, void *));
99 int awi_pcmcia_detach __P((struct device *, int));
100
101 int awi_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
102 int awi_pcmcia_enable __P((struct awi_softc *));
103 void awi_pcmcia_disable __P((struct awi_softc *));
104
105 struct awi_pcmcia_softc {
106 struct awi_softc sc_awi; /* real "awi" softc */
107
108 /* PCMCIA-specific goo */
109 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
110 struct pcmcia_mem_handle sc_memh; /* PCMCIA mem space info */
111 int sc_io_window; /* our i/o window */
112 int sc_mem_window; /* our memory window */
113 struct pcmcia_function *sc_pf; /* our PCMCIA function */
114 };
115
116 int awi_pcmcia_find __P((struct awi_pcmcia_softc *,
117 struct pcmcia_attach_args *, struct pcmcia_config_entry *));
118
119 struct cfattach awi_pcmcia_ca = {
120 sizeof(struct awi_pcmcia_softc),
121 awi_pcmcia_match, awi_pcmcia_attach,
122 awi_pcmcia_detach, awi_activate
123 };
124
125 #if __NetBSD_Version__ <= 104120000
126 #define PCMCIA_VENDOR_BAY 0x01eb /* Bay Networks */
127 #define PCMCIA_PRODUCT_BAY_STACK_650 0x804
128 #endif
129
130 /*
131 * XXX following is common to most PCMCIA NIC's and belongs
132 * in common code
133 */
134
135 struct awi_pcmcia_get_enaddr_args {
136 u_int8_t enaddr[ETHER_ADDR_LEN];
137 };
138 int awi_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
139
140 struct awi_pcmcia_product {
141 u_int32_t app_vendor; /* vendor ID */
142 u_int32_t app_product; /* product ID */
143 const char *app_cisinfo[4]; /* CIS information */
144 const char *app_name; /* product name */
145 } awi_pcmcia_products[] = {
146 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_650,
147 PCMCIA_CIS_BAY_STACK_650, PCMCIA_STR_BAY_STACK_650 },
148
149 #ifdef notyet
150 { PCMCIA_VENDOR_MELCO, PCMCIA_PRODUCT_MELCO_WLI_PCM,
151 PCMCIA_CIS_MELCO_WLI_PCM, PCMCIA_STR_MELCO_WLI_PCM },
152
153 { PCMCIA_VENDOR_ICOM, PCMCIA_PRODUCT_ICOM_SL200,
154 PCMCIA_CIS_ICOM_SL200, PCMCIA_STR_ICOM_SL200 },
155 #endif
156
157 { 0, 0,
158 { NULL, NULL, NULL, NULL }, NULL },
159 };
160
161 struct awi_pcmcia_product *
162 awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
163
164 struct awi_pcmcia_product *
165 awi_pcmcia_lookup(pa)
166 struct pcmcia_attach_args *pa;
167 {
168 struct awi_pcmcia_product *app;
169
170 for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
171 /* match by vendor/product id */
172 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
173 pa->manufacturer == app->app_vendor &&
174 pa->product != PCMCIA_PRODUCT_INVALID &&
175 pa->product == app->app_product)
176 return (app);
177
178 /* match by CIS information */
179 if (pa->card->cis1_info[0] != NULL &&
180 app->app_cisinfo[0] != NULL &&
181 strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
182 pa->card->cis1_info[1] != NULL &&
183 app->app_cisinfo[1] != NULL &&
184 strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
185 return (app);
186 }
187
188 return (NULL);
189 }
190
191 int
192 awi_pcmcia_enable(sc)
193 struct awi_softc *sc;
194 {
195 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
196 struct pcmcia_function *pf = psc->sc_pf;
197
198 /* establish the interrupt. */
199 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
200 if (sc->sc_ih == NULL) {
201 printf("%s: couldn't establish interrupt\n",
202 sc->sc_dev.dv_xname);
203 return (1);
204 }
205 return (pcmcia_function_enable(pf));
206 }
207
208 void
209 awi_pcmcia_disable(sc)
210 struct awi_softc *sc;
211 {
212 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
213 struct pcmcia_function *pf = psc->sc_pf;
214
215 pcmcia_function_disable(pf);
216 pcmcia_intr_disestablish(pf, sc->sc_ih);
217 }
218
219 int
220 awi_pcmcia_match(parent, match, aux)
221 struct device *parent;
222 struct cfdata *match;
223 void *aux;
224 {
225 struct pcmcia_attach_args *pa = aux;
226
227 if (awi_pcmcia_lookup(pa) != NULL)
228 return (1);
229
230 return (0);
231 }
232
233 int
234 awi_pcmcia_find(psc, pa, cfe)
235 struct awi_pcmcia_softc *psc;
236 struct pcmcia_attach_args *pa;
237 struct pcmcia_config_entry *cfe;
238 {
239 struct awi_softc *sc = &psc->sc_awi;
240 int fail = 0;
241 u_int8_t version[AWI_BANNER_LEN];
242
243 /*
244 * see if we can read the firmware version sanely
245 * through the i/o ports.
246 * if not, try a different CIS string..
247 */
248 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
249 cfe->iospace[0].length, cfe->iospace[0].length,
250 &psc->sc_pcioh) != 0)
251 goto fail;
252
253 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
254 &psc->sc_pcioh, &psc->sc_io_window))
255 goto fail_io_free;
256
257 /* Enable the card. */
258 pcmcia_function_init(psc->sc_pf, cfe);
259 if (pcmcia_function_enable(psc->sc_pf))
260 goto fail_io_unmap;
261
262 sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
263 sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
264 am79c930_chip_init(&sc->sc_chip, 0);
265
266 DELAY(100);
267
268 awi_read_bytes(sc, AWI_BANNER, version, AWI_BANNER_LEN);
269
270 if (memcmp(version, "PCnetMobile:", 12) == 0)
271 return (0);
272
273 fail++;
274 pcmcia_function_disable(psc->sc_pf);
275
276 fail_io_unmap:
277 fail++;
278 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
279 psc->sc_io_window = -1;
280
281 fail_io_free:
282 fail++;
283 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
284 fail:
285 fail++;
286 return (fail);
287 }
288
289
290
291 void
292 awi_pcmcia_attach(parent, self, aux)
293 struct device *parent, *self;
294 void *aux;
295 {
296 struct awi_pcmcia_softc *psc = (void *)self;
297 struct awi_softc *sc = &psc->sc_awi;
298 struct awi_pcmcia_product *app;
299 struct pcmcia_attach_args *pa = aux;
300 struct pcmcia_config_entry *cfe;
301 struct awi_pcmcia_get_enaddr_args pgea;
302 bus_addr_t memoff;
303 int rv;
304
305 psc->sc_mem_window = -1;
306 psc->sc_io_window = -1;
307 #if 0
308 int i, j;
309
310 for (cfe = pa->pf->cfe_head.sqh_first, i=0;
311 cfe != NULL;
312 cfe = cfe->cfe_list.sqe_next, i++) {
313 printf("%d: %d memspaces, %d iospaces\n",
314 i, cfe->num_memspace, cfe->num_iospace);
315 printf("%d: number %d flags %x iftype %d iomask %lx "
316 "irqmask %x maxtwins %x\n",
317 i, cfe->number, cfe->flags, cfe->iftype, cfe->iomask,
318 cfe->irqmask, cfe->maxtwins);
319 for (j = 0; j < cfe->num_memspace; j++) {
320 printf("%d: mem %d: len %lx card %lx host %lx\n",
321 i, j,
322 cfe->memspace[j].length,
323 cfe->memspace[j].cardaddr,
324 cfe->memspace[j].hostaddr);
325 }
326 for (j = 0; j < cfe->num_iospace; j++) {
327 printf("%d: io %d: len %lx start %lx\n",
328 i, j,
329 cfe->iospace[j].length,
330 cfe->iospace[j].start);
331 }
332 }
333 #endif
334
335 app = awi_pcmcia_lookup(pa);
336 if (app == NULL)
337 panic("awi_pcmcia_attach: impossible");
338
339 psc->sc_pf = pa->pf;
340
341 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
342 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
343 if (cfe->iftype != PCMCIA_IFTYPE_IO)
344 continue;
345 if (cfe->num_iospace < 1)
346 continue;
347 if (cfe->iospace[0].length < AM79C930_IO_SIZE)
348 continue;
349
350 if (awi_pcmcia_find(psc, pa, cfe) == 0)
351 break;
352 }
353 if (cfe == NULL) {
354 printf(": no suitable CIS info found\n");
355 return;
356 }
357
358 sc->sc_enabled = 1;
359 sc->sc_state = AWI_ST_SELFTEST;
360 printf(": %s\n", app->app_name);
361
362 if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE, &psc->sc_memh) != 0) {
363 printf("%s: unable to allocate memory space; using i/o only\n",
364 sc->sc_dev.dv_xname);
365 } else if (pcmcia_mem_map(psc->sc_pf,
366 PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
367 AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
368 printf("%s: unable to map memory space; using i/o only\n",
369 sc->sc_dev.dv_xname);
370 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
371 } else {
372 sc->sc_chip.sc_memt = psc->sc_memh.memt;
373 sc->sc_chip.sc_memh = psc->sc_memh.memh;
374 am79c930_chip_init(&sc->sc_chip, 1);
375 }
376
377 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
378
379 sc->sc_enable = awi_pcmcia_enable;
380 sc->sc_disable = awi_pcmcia_disable;
381
382 /* Read station address. */
383 rv = pcmcia_scan_cis(parent, awi_pcmcia_get_enaddr, &pgea);
384 if (rv == -1) {
385 printf("%s: Couldn't read CIS to get ethernet address\n",
386 sc->sc_dev.dv_xname);
387 return;
388 } else if (rv == 0) {
389 printf("%s: Couldn't get ethernet address from CIS\n",
390 sc->sc_dev.dv_xname);
391 return;
392 }
393
394 #ifdef DIAGNOSTIC
395 if (rv != 1) {
396 printf("%s: pcmcia_scan_cis returns %d\n", sc->sc_dev.dv_xname,
397 rv);
398 panic("awi_pcmcia_attach");
399 }
400 printf("%s: Ethernet address from CIS: %s\n",
401 sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
402 #endif
403
404 awi_attach(sc, pgea.enaddr);
405
406 sc->sc_state = AWI_ST_OFF;
407
408 sc->sc_enabled = 0;
409 pcmcia_function_disable(psc->sc_pf);
410 }
411
412
413 int
414 awi_pcmcia_detach(self, flags)
415 struct device *self;
416 int flags;
417 {
418 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
419
420 /* Unmap our i/o window. */
421 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
422
423 /* Free our i/o space. */
424 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
425
426 if (psc->sc_mem_window != -1) {
427 pcmcia_mem_unmap (psc->sc_pf, psc->sc_mem_window);
428 pcmcia_mem_free (psc->sc_pf, &psc->sc_memh);
429 }
430
431 return awi_detach(&psc->sc_awi);
432 }
433
434 /*
435 * XXX copied verbatim from if_mbe_pcmcia.c.
436 * this function should be in common pcmcia code..
437 */
438
439 int
440 awi_pcmcia_get_enaddr(tuple, arg)
441 struct pcmcia_tuple *tuple;
442 void *arg;
443 {
444 struct awi_pcmcia_get_enaddr_args *p = arg;
445 int i;
446
447 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
448 if (tuple->length < 2) /* sub code and ether addr length */
449 return (0);
450
451 if ((pcmcia_tuple_read_1(tuple, 0) !=
452 PCMCIA_TPLFE_TYPE_LAN_NID) ||
453 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
454 return (0);
455
456 for (i = 0; i < ETHER_ADDR_LEN; i++)
457 p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
458 return (1);
459 }
460 return (0);
461 }
462