if_ep_pcmcia.c revision 1.31 1 /* $NetBSD: if_ep_pcmcia.c,v 1.31 2000/03/13 20:32:28 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Marc Horowitz.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include "opt_inet.h"
70 #include "opt_ns.h"
71 #include "bpfilter.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/ioctl.h>
78 #include <sys/errno.h>
79 #include <sys/syslog.h>
80 #include <sys/select.h>
81 #include <sys/device.h>
82
83 #include <net/if.h>
84 #include <net/if_dl.h>
85 #include <net/if_ether.h>
86 #include <net/if_media.h>
87
88 #ifdef INET
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/if_inarp.h>
94 #endif
95
96 #ifdef NS
97 #include <netns/ns.h>
98 #include <netns/ns_if.h>
99 #endif
100
101 #if NBPFILTER > 0
102 #include <net/bpf.h>
103 #include <net/bpfdesc.h>
104 #endif
105
106 #include <machine/cpu.h>
107 #include <machine/bus.h>
108 #include <machine/intr.h>
109
110 #include <dev/mii/miivar.h>
111
112 #include <dev/ic/elink3var.h>
113 #include <dev/ic/elink3reg.h>
114
115 #include <dev/pcmcia/pcmciareg.h>
116 #include <dev/pcmcia/pcmciavar.h>
117 #include <dev/pcmcia/pcmciadevs.h>
118
119 int ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
120 void ep_pcmcia_attach __P((struct device *, struct device *, void *));
121 int ep_pcmcia_detach __P((struct device *, int));
122
123 int ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
124 int ep_pcmcia_enable __P((struct ep_softc *));
125 void ep_pcmcia_disable __P((struct ep_softc *));
126
127 int ep_pcmcia_enable1 __P((struct ep_softc *));
128 void ep_pcmcia_disable1 __P((struct ep_softc *));
129
130 struct ep_pcmcia_softc {
131 struct ep_softc sc_ep; /* real "ep" softc */
132
133 /* PCMCIA-specific goo */
134 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
135 int sc_io_window; /* our i/o window */
136 struct pcmcia_function *sc_pf; /* our PCMCIA function */
137 };
138
139 struct cfattach ep_pcmcia_ca = {
140 sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach,
141 ep_pcmcia_detach, ep_activate
142 };
143
144 const struct ep_pcmcia_product {
145 struct pcmcia_product epp_product;
146 u_short epp_chipset; /* 3Com chipset used */
147 int epp_flags; /* initial softc flags */
148 } ep_pcmcia_products[] = {
149 { { PCMCIA_STR_3COM_3C562, PCMCIA_VENDOR_3COM,
150 PCMCIA_PRODUCT_3COM_3C562, 0 },
151 ELINK_CHIPSET_3C509, 0 },
152
153 { { PCMCIA_STR_3COM_3C589, PCMCIA_VENDOR_3COM,
154 PCMCIA_PRODUCT_3COM_3C589, 0 },
155 ELINK_CHIPSET_3C509, 0 },
156
157 { { PCMCIA_STR_3COM_3CXEM556, PCMCIA_VENDOR_3COM,
158 PCMCIA_PRODUCT_3COM_3CXEM556, 0 },
159 ELINK_CHIPSET_3C509, 0 },
160
161 { { PCMCIA_STR_3COM_3CXEM556INT, PCMCIA_VENDOR_3COM,
162 PCMCIA_PRODUCT_3COM_3CXEM556INT, 0 },
163 ELINK_CHIPSET_3C509, 0 },
164
165 { { PCMCIA_STR_3COM_3C574, PCMCIA_VENDOR_3COM,
166 PCMCIA_PRODUCT_3COM_3C574, 0 },
167 ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
168
169 { { PCMCIA_STR_3COM_3CCFEM556BI, PCMCIA_VENDOR_3COM,
170 PCMCIA_PRODUCT_3COM_3CCFEM556BI, 0 },
171 ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
172
173 { { PCMCIA_STR_3COM_3C1, PCMCIA_VENDOR_3COM,
174 PCMCIA_PRODUCT_3COM_3C1, 0 },
175 ELINK_CHIPSET_3C509, 0 },
176
177 { { NULL } }
178 };
179
180 int
181 ep_pcmcia_match(parent, match, aux)
182 struct device *parent;
183 struct cfdata *match;
184 void *aux;
185 {
186 struct pcmcia_attach_args *pa = aux;
187
188 if (pcmcia_product_lookup(pa,
189 (const struct pcmcia_product *)ep_pcmcia_products,
190 sizeof ep_pcmcia_products[0], NULL) != NULL)
191 return (1);
192
193 return (0);
194 }
195
196 int
197 ep_pcmcia_enable(sc)
198 struct ep_softc *sc;
199 {
200 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
201 struct pcmcia_function *pf = psc->sc_pf;
202 int error;
203
204 /* establish the interrupt. */
205 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
206 if (sc->sc_ih == NULL) {
207 printf("%s: couldn't establish interrupt\n",
208 sc->sc_dev.dv_xname);
209 return (1);
210 }
211
212 error = ep_pcmcia_enable1(sc);
213 if (error != 0)
214 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
215 return (error);
216 }
217
218 int
219 ep_pcmcia_enable1(sc)
220 struct ep_softc *sc;
221 {
222 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
223 struct pcmcia_function *pf = psc->sc_pf;
224 int ret;
225
226 if ((ret = pcmcia_function_enable(pf)))
227 return (ret);
228
229 if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
230 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
231 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
232 int reg;
233
234 /* turn off the serial-disable bit */
235
236 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
237 if (reg & 0x08) {
238 reg &= ~0x08;
239 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
240 }
241
242 }
243
244 return (ret);
245 }
246
247 void
248 ep_pcmcia_disable(sc)
249 struct ep_softc *sc;
250 {
251 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
252
253 ep_pcmcia_disable1(sc);
254 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
255 }
256
257 void
258 ep_pcmcia_disable1(sc)
259 struct ep_softc *sc;
260 {
261 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
262
263 pcmcia_function_disable(psc->sc_pf);
264 }
265
266 void
267 ep_pcmcia_attach(parent, self, aux)
268 struct device *parent, *self;
269 void *aux;
270 {
271 struct ep_pcmcia_softc *psc = (void *) self;
272 struct ep_softc *sc = &psc->sc_ep;
273 struct pcmcia_attach_args *pa = aux;
274 struct pcmcia_config_entry *cfe;
275 const struct ep_pcmcia_product *epp;
276 u_int8_t myla[ETHER_ADDR_LEN];
277 u_int8_t *enaddr = NULL;
278 int i;
279
280 psc->sc_pf = pa->pf;
281 cfe = pa->pf->cfe_head.sqh_first;
282
283 /* Enable the card. */
284 pcmcia_function_init(pa->pf, cfe);
285 if (ep_pcmcia_enable1(sc)) {
286 printf(": function enable failed\n");
287 goto enable_failed;
288 }
289 sc->enabled = 1;
290
291 if (cfe->num_memspace != 0) {
292 printf(": unexpected number of memory spaces %d should be 0\n",
293 cfe->num_memspace);
294 goto ioalloc_failed;
295 }
296
297 if (cfe->num_iospace != 1) {
298 printf(": unexpected number of I/O spaces %d should be 1\n",
299 cfe->num_iospace);
300 goto ioalloc_failed;
301 }
302
303 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
304 bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
305
306 for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
307 /*
308 * the 3c562 can only use 0x??00-0x??7f
309 * according to the Linux driver
310 */
311 if (i & 0x80)
312 continue;
313 if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
314 0, &psc->sc_pcioh) == 0)
315 break;
316 }
317 if (i >= maxaddr) {
318 printf(": can't allocate i/o space\n");
319 goto ioalloc_failed;
320 }
321 } else {
322 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
323 cfe->iospace[0].length, &psc->sc_pcioh)) {
324 printf(": can't allocate i/o space\n");
325 goto ioalloc_failed;
326 }
327 }
328
329 sc->sc_iot = psc->sc_pcioh.iot;
330 sc->sc_ioh = psc->sc_pcioh.ioh;
331
332 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
333 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
334 &psc->sc_pcioh, &psc->sc_io_window)) {
335 printf(": can't map i/o space\n");
336 goto iomap_failed;
337 }
338
339 switch (pa->product) {
340 case PCMCIA_PRODUCT_3COM_3C562:
341 /*
342 * 3c562a-c use this; 3c562d does it in the regular way.
343 * we might want to check the revision and produce a warning
344 * in the future.
345 */
346 /* FALLTHROUGH */
347 case PCMCIA_PRODUCT_3COM_3C574:
348 case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
349 /*
350 * Apparently, some 3c574s do it this way, as well.
351 */
352 if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
353 enaddr = myla;
354 break;
355 }
356
357 epp = (const struct ep_pcmcia_product *)pcmcia_product_lookup(pa,
358 (const struct pcmcia_product *)ep_pcmcia_products,
359 sizeof ep_pcmcia_products[0], NULL);
360 if (epp == NULL)
361 panic("ep_pcmcia_attach: impossible");
362
363 printf(": %s\n", epp->epp_product.pp_name);
364
365 sc->bustype = ELINK_BUS_PCMCIA;
366 sc->ep_flags = epp->epp_flags;
367
368 sc->enable = ep_pcmcia_enable;
369 sc->disable = ep_pcmcia_disable;
370
371 if (epconfig(sc, epp->epp_chipset, enaddr)) {
372 printf("%s: couldn't configure controller\n",
373 sc->sc_dev.dv_xname);
374 goto config_failed;
375 }
376
377 sc->enabled = 0;
378 ep_pcmcia_disable1(sc);
379 return;
380
381 config_failed:
382 /* Unmap our i/o window. */
383 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
384
385 iomap_failed:
386 /* Free our i/o space. */
387 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
388
389 ioalloc_failed:
390 sc->enabled = 0;
391 ep_pcmcia_disable1(sc);
392
393 enable_failed:
394 psc->sc_io_window = -1;
395 }
396
397 int
398 ep_pcmcia_detach(self, flags)
399 struct device *self;
400 int flags;
401 {
402 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
403 int rv;
404
405 if (psc->sc_io_window == -1)
406 /* Nothing to detach. */
407 return (0);
408
409 rv = ep_detach(self, flags);
410 if (rv != 0)
411 return (rv);
412
413 /* Unmap our i/o window. */
414 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
415
416 /* Free our i/o space. */
417 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
418
419 return (0);
420 }
421
422 int
423 ep_pcmcia_get_enaddr(tuple, arg)
424 struct pcmcia_tuple *tuple;
425 void *arg;
426 {
427 u_int8_t *myla = arg;
428 int i;
429
430 /* this is 3c562a-c magic */
431 if (tuple->code == 0x88) {
432 if (tuple->length < ETHER_ADDR_LEN)
433 return (0);
434
435 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
436 myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
437 myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
438 }
439
440 return (1);
441 }
442 return (0);
443 }
444