if_xi.c revision 1.23 1 /* $NetBSD: if_xi.c,v 1.23 2002/09/22 10:13:32 martin Exp $ */
2 /* OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp */
3
4 /*
5 * XXX THIS DRIVER IS BROKEN WRT. MULTICAST LISTS AND PROMISC/ALLMULTI
6 * XXX FLAGS!
7 */
8
9 /*
10 * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Niklas Hallqvist,
24 * Brandon Creighton and Job de Haas.
25 * 4. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
42 */
43
44 /*
45 * Known Bugs:
46 *
47 * 1) Promiscuous mode doesn't work on at least the CE2.
48 * 2) Slow. ~450KB/s. Memory access would be better.
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: if_xi.c,v 1.23 2002/09/22 10:13:32 martin Exp $");
53
54 #include "opt_inet.h"
55 #include "bpfilter.h"
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #include <sys/mbuf.h>
62 #include <sys/malloc.h>
63 #include <sys/socket.h>
64
65 #include "rnd.h"
66 #if NRND > 0
67 #include <sys/rnd.h>
68 #endif
69
70 #include <net/if.h>
71 #include <net/if_dl.h>
72 #include <net/if_media.h>
73 #include <net/if_types.h>
74 #include <net/if_ether.h>
75
76 #ifdef INET
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/if_inarp.h>
82 #endif
83
84 #ifdef IPX
85 #include <netipx/ipx.h>
86 #include <netipx/ipx_if.h>
87 #endif
88
89 #ifdef NS
90 #include <netns/ns.h>
91 #include <netns/ns_if.h>
92 #endif
93
94 #if NBPFILTER > 0
95 #include <net/bpf.h>
96 #include <net/bpfdesc.h>
97 #endif
98
99 #define ETHER_MIN_LEN 64
100 #define ETHER_CRC_LEN 4
101
102 /*
103 * Maximum number of bytes to read per interrupt. Linux recommends
104 * somewhere between 2000-22000.
105 * XXX This is currently a hard maximum.
106 */
107 #define MAX_BYTES_INTR 12000
108
109 #include <dev/mii/mii.h>
110 #include <dev/mii/miivar.h>
111
112 #include <dev/pcmcia/pcmciareg.h>
113 #include <dev/pcmcia/pcmciavar.h>
114 #include <dev/pcmcia/pcmciadevs.h>
115
116 #include <dev/pcmcia/if_xireg.h>
117
118 #ifdef __GNUC__
119 #define INLINE __inline
120 #else
121 #define INLINE
122 #endif /* __GNUC__ */
123
124 #ifdef XIDEBUG
125 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
126
127 #define XID_CONFIG 0x1
128 #define XID_MII 0x2
129 #define XID_INTR 0x4
130 #define XID_FIFO 0x8
131
132 #ifdef XIDEBUG_VALUE
133 int xidebug = XIDEBUG_VALUE;
134 #else
135 int xidebug = 0;
136 #endif
137 #else
138 #define DPRINTF(cat, x) (void)0
139 #endif
140
141 int xi_pcmcia_match __P((struct device *, struct cfdata *, void *));
142 void xi_pcmcia_attach __P((struct device *, struct device *, void *));
143 int xi_pcmcia_detach __P((struct device *, int));
144 int xi_pcmcia_activate __P((struct device *, enum devact));
145
146 /*
147 * In case this chipset ever turns up out of pcmcia attachments (very
148 * unlikely) do the driver splitup.
149 */
150 struct xi_softc {
151 struct device sc_dev; /* Generic device info */
152 struct ethercom sc_ethercom; /* Ethernet common part */
153
154 struct mii_data sc_mii; /* MII media information */
155
156 bus_space_tag_t sc_bst; /* Bus cookie */
157 bus_space_handle_t sc_bsh; /* Bus I/O handle */
158 bus_size_t sc_offset; /* Offset of registers */
159
160 u_int8_t sc_rev; /* Chip revision */
161 u_int32_t sc_flags; /* Misc. flags */
162 int sc_all_mcasts; /* Receive all multicasts */
163 u_int8_t sc_enaddr[ETHER_ADDR_LEN];
164 #if NRND > 0
165 rndsource_element_t sc_rnd_source;
166 #endif
167 };
168
169 struct xi_pcmcia_softc {
170 struct xi_softc sc_xi; /* Generic device info */
171
172 /* PCMCIA-specific goo */
173 struct pcmcia_function *sc_pf; /* PCMCIA function */
174 struct pcmcia_io_handle sc_pcioh; /* iospace info */
175 int sc_io_window; /* io window info */
176 void *sc_ih; /* Interrupt handler */
177 void *sc_powerhook; /* power hook descriptor */
178 int sc_resource; /* resource allocated */
179 #define XI_RES_PCIC 1
180 #define XI_RES_IO_ALLOC 2
181 #define XI_RES_IO_MAP 4
182 #define XI_RES_MI 8
183 };
184
185 struct cfattach xi_pcmcia_ca = {
186 sizeof(struct xi_pcmcia_softc),
187 xi_pcmcia_match,
188 xi_pcmcia_attach,
189 xi_pcmcia_detach,
190 xi_pcmcia_activate
191 };
192
193 static int xi_pcmcia_cis_quirks __P((struct pcmcia_function *));
194 static void xi_cycle_power __P((struct xi_softc *));
195 static int xi_ether_ioctl __P((struct ifnet *, u_long cmd, caddr_t));
196 static void xi_full_reset __P((struct xi_softc *));
197 static void xi_init __P((struct xi_softc *));
198 static int xi_intr __P((void *));
199 static int xi_ioctl __P((struct ifnet *, u_long, caddr_t));
200 static int xi_mdi_read __P((struct device *, int, int));
201 static void xi_mdi_write __P((struct device *, int, int, int));
202 static int xi_mediachange __P((struct ifnet *));
203 static void xi_mediastatus __P((struct ifnet *, struct ifmediareq *));
204 static int xi_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
205 static int xi_pcmcia_lan_nid_ciscallback __P((struct pcmcia_tuple *, void *));
206 static int xi_pcmcia_manfid_ciscallback __P((struct pcmcia_tuple *, void *));
207 static u_int16_t xi_get __P((struct xi_softc *));
208 static void xi_reset __P((struct xi_softc *));
209 static void xi_set_address __P((struct xi_softc *));
210 static void xi_start __P((struct ifnet *));
211 static void xi_statchg __P((struct device *));
212 static void xi_stop __P((struct xi_softc *));
213 static void xi_watchdog __P((struct ifnet *));
214 const struct xi_pcmcia_product *xi_pcmcia_identify __P((struct device *,
215 struct pcmcia_attach_args *));
216 static int xi_pcmcia_enable __P((struct xi_pcmcia_softc *));
217 static void xi_pcmcia_disable __P((struct xi_pcmcia_softc *));
218 static void xi_pcmcia_power __P((int, void *));
219
220 /* flags */
221 #define XIFLAGS_MOHAWK 0x001 /* 100Mb capabilities (has phy) */
222 #define XIFLAGS_DINGO 0x002 /* realport cards ??? */
223 #define XIFLAGS_MODEM 0x004 /* modem also present */
224
225 const struct xi_pcmcia_product {
226 u_int32_t xpp_vendor; /* vendor ID */
227 u_int32_t xpp_product; /* product ID */
228 int xpp_expfunc; /* expected function number */
229 int xpp_flags; /* initial softc flags */
230 const char *xpp_name; /* device name */
231 } xi_pcmcia_products[] = {
232 #ifdef NOT_SUPPORTED
233 { PCMCIA_VENDOR_XIRCOM, 0x0141,
234 0, 0,
235 PCMCIA_STR_XIRCOM_CE },
236 #endif
237 { PCMCIA_VENDOR_XIRCOM, 0x0141,
238 0, 0,
239 PCMCIA_STR_XIRCOM_CE2 },
240 { PCMCIA_VENDOR_XIRCOM, 0x0142,
241 0, 0,
242 PCMCIA_STR_XIRCOM_CE2 },
243 { PCMCIA_VENDOR_XIRCOM, 0x0143,
244 0, XIFLAGS_MOHAWK,
245 PCMCIA_STR_XIRCOM_CE3 },
246 { PCMCIA_VENDOR_COMPAQ2, 0x0143,
247 0, XIFLAGS_MOHAWK,
248 PCMCIA_STR_COMPAQ2_CPQ_10_100 },
249 { PCMCIA_VENDOR_INTEL, 0x0143,
250 0, XIFLAGS_MOHAWK | XIFLAGS_MODEM,
251 PCMCIA_STR_INTEL_EEPRO100 },
252 { PCMCIA_VENDOR_XIRCOM, PCMCIA_PRODUCT_XIRCOM_XE2000,
253 0, XIFLAGS_MOHAWK,
254 PCMCIA_STR_XIRCOM_XE2000 },
255 { PCMCIA_VENDOR_XIRCOM, PCMCIA_PRODUCT_XIRCOM_REM56,
256 0, XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
257 PCMCIA_STR_XIRCOM_REM56 },
258 #ifdef NOT_SUPPORTED
259 { PCMCIA_VENDOR_XIRCOM, 0x1141,
260 0, XIFLAGS_MODEM,
261 PCMCIA_STR_XIRCOM_CEM },
262 #endif
263 { PCMCIA_VENDOR_XIRCOM, 0x1142,
264 0, XIFLAGS_MODEM,
265 PCMCIA_STR_XIRCOM_CEM },
266 { PCMCIA_VENDOR_XIRCOM, 0x1143,
267 0, XIFLAGS_MODEM,
268 PCMCIA_STR_XIRCOM_CEM },
269 { PCMCIA_VENDOR_XIRCOM, 0x1144,
270 0, XIFLAGS_MODEM,
271 PCMCIA_STR_XIRCOM_CEM33 },
272 { PCMCIA_VENDOR_XIRCOM, 0x1145,
273 0, XIFLAGS_MOHAWK | XIFLAGS_MODEM,
274 PCMCIA_STR_XIRCOM_CEM56 },
275 { PCMCIA_VENDOR_XIRCOM, 0x1146,
276 0, XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
277 PCMCIA_STR_XIRCOM_REM56 },
278 { PCMCIA_VENDOR_XIRCOM, 0x1147,
279 0, XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
280 PCMCIA_STR_XIRCOM_REM56 },
281 { 0, 0,
282 0, 0,
283 NULL },
284 };
285
286
287 const struct xi_pcmcia_product *
288 xi_pcmcia_identify(dev, pa)
289 struct device *dev;
290 struct pcmcia_attach_args *pa;
291 {
292 const struct xi_pcmcia_product *xpp;
293 u_int8_t id;
294 u_int32_t prod;
295
296 /*
297 * The Xircom ethernet cards swap the revision and product fields
298 * inside the CIS, which makes identification just a little
299 * bit different.
300 */
301
302 pcmcia_scan_cis(dev, xi_pcmcia_manfid_ciscallback, &id);
303
304 prod = (pa->product & ~0xff) | id;
305
306 DPRINTF(XID_CONFIG, ("product=0x%x\n", prod));
307
308 for (xpp = xi_pcmcia_products; xpp->xpp_name != NULL; xpp++)
309 if (pa->manufacturer == xpp->xpp_vendor &&
310 prod == xpp->xpp_product &&
311 pa->pf->number == xpp->xpp_expfunc)
312 return (xpp);
313 return (NULL);
314 }
315
316 /*
317 * The quirks are done here instead of the traditional framework because
318 * of the difficulty in identifying the devices.
319 */
320 static int
321 xi_pcmcia_cis_quirks(pf)
322 struct pcmcia_function *pf;
323 {
324 struct pcmcia_config_entry *cfe;
325
326 /* Tell the pcmcia framework where the CCR is. */
327 pf->ccr_base = 0x800;
328 pf->ccr_mask = 0x67;
329
330 /* Fake a cfe. */
331 SIMPLEQ_FIRST(&pf->cfe_head) = cfe = (struct pcmcia_config_entry *)
332 malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT|M_ZERO);
333
334 if (cfe == NULL)
335 return -1;
336
337 /*
338 * XXX Use preprocessor symbols instead.
339 * Enable ethernet & its interrupts, wiring them to -INT
340 * No I/O base.
341 */
342 cfe->number = 0x5;
343 cfe->flags = 0; /* XXX Check! */
344 cfe->iftype = PCMCIA_IFTYPE_IO;
345 cfe->num_iospace = 0;
346 cfe->num_memspace = 0;
347 cfe->irqmask = 0x8eb0;
348
349 return 0;
350 }
351
352 int
353 xi_pcmcia_match(parent, match, aux)
354 struct device *parent;
355 struct cfdata *match;
356 void *aux;
357 {
358 struct pcmcia_attach_args *pa = aux;
359
360 if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
361 pa->product == 0x110a)
362 return (2); /* prevent attach to com_pcmcia */
363 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
364 return (0);
365
366 if (pa->manufacturer == PCMCIA_VENDOR_COMPAQ2 &&
367 pa->product == PCMCIA_PRODUCT_COMPAQ2_CPQ_10_100)
368 return (1);
369
370 if (pa->manufacturer == PCMCIA_VENDOR_INTEL &&
371 pa->product == PCMCIA_PRODUCT_INTEL_EEPRO100)
372 return (1);
373
374 if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
375 ((pa->product >> 8) == XIMEDIA_ETHER ||
376 (pa->product >> 8) == (XIMEDIA_ETHER | XIMEDIA_MODEM)))
377 return (1);
378
379 return (0);
380 }
381
382 void
383 xi_pcmcia_attach(parent, self, aux)
384 struct device *parent, *self;
385 void *aux;
386 {
387 struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
388 struct xi_softc *sc = &psc->sc_xi;
389 struct pcmcia_attach_args *pa = aux;
390 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
391 const struct xi_pcmcia_product *xpp;
392
393 if (xi_pcmcia_cis_quirks(pa->pf) < 0) {
394 printf(": function enable failed\n");
395 return;
396 }
397
398 /* Enable the card */
399 psc->sc_pf = pa->pf;
400 pcmcia_function_init(psc->sc_pf, SIMPLEQ_FIRST(&psc->sc_pf->cfe_head));
401 if (pcmcia_function_enable(psc->sc_pf)) {
402 printf(": function enable failed\n");
403 goto fail;
404 }
405 psc->sc_resource |= XI_RES_PCIC;
406
407 /* allocate/map ISA I/O space */
408 if (pcmcia_io_alloc(psc->sc_pf, 0, XI_IOSIZE, XI_IOSIZE,
409 &psc->sc_pcioh) != 0) {
410 printf(": I/O allocation failed\n");
411 goto fail;
412 }
413 psc->sc_resource |= XI_RES_IO_ALLOC;
414
415 sc->sc_bst = psc->sc_pcioh.iot;
416 sc->sc_bsh = psc->sc_pcioh.ioh;
417 sc->sc_offset = 0;
418
419 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, XI_IOSIZE,
420 &psc->sc_pcioh, &psc->sc_io_window)) {
421 printf(": can't map I/O space\n");
422 goto fail;
423 }
424 psc->sc_resource |= XI_RES_IO_MAP;
425
426 xpp = xi_pcmcia_identify(parent,pa);
427 if (xpp == NULL) {
428 printf(": unrecognised model\n");
429 return;
430 }
431 sc->sc_flags = xpp->xpp_flags;
432
433 printf(": %s\n", xpp->xpp_name);
434
435 /*
436 * Configuration as advised by DINGO documentation.
437 * Dingo has some extra configuration registers in the CCR space.
438 */
439 if (sc->sc_flags & XIFLAGS_DINGO) {
440 struct pcmcia_mem_handle pcmh;
441 int ccr_window;
442 bus_addr_t ccr_offset;
443
444 /* get access to the DINGO CCR space */
445 if (pcmcia_mem_alloc(psc->sc_pf, PCMCIA_CCR_SIZE_DINGO,
446 &pcmh)) {
447 DPRINTF(XID_CONFIG, ("xi: bad mem alloc\n"));
448 goto fail;
449 }
450 if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR,
451 psc->sc_pf->ccr_base, PCMCIA_CCR_SIZE_DINGO,
452 &pcmh, &ccr_offset, &ccr_window)) {
453 DPRINTF(XID_CONFIG, ("xi: bad mem map\n"));
454 pcmcia_mem_free(psc->sc_pf, &pcmh);
455 goto fail;
456 }
457
458 /* enable the second function - usually modem */
459 bus_space_write_1(pcmh.memt, pcmh.memh,
460 ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
461 bus_space_write_1(pcmh.memt, pcmh.memh,
462 ccr_offset + PCMCIA_CCR_DCOR1,
463 PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
464 bus_space_write_1(pcmh.memt, pcmh.memh,
465 ccr_offset + PCMCIA_CCR_DCOR2, 0);
466 bus_space_write_1(pcmh.memt, pcmh.memh,
467 ccr_offset + PCMCIA_CCR_DCOR3, 0);
468 bus_space_write_1(pcmh.memt, pcmh.memh,
469 ccr_offset + PCMCIA_CCR_DCOR4, 0);
470
471 /* We don't need them anymore and can free them (I think). */
472 pcmcia_mem_unmap(psc->sc_pf, ccr_window);
473 pcmcia_mem_free(psc->sc_pf, &pcmh);
474 }
475
476 /*
477 * Get the ethernet address from FUNCE/LAN_NID tuple.
478 */
479 xi_pcmcia_funce_enaddr(parent, sc->sc_enaddr);
480 if (!sc->sc_enaddr) {
481 printf("%s: unable to get ethernet address\n",
482 sc->sc_dev.dv_xname);
483 goto fail;
484 }
485
486 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
487 ether_sprintf(sc->sc_enaddr));
488
489 ifp = &sc->sc_ethercom.ec_if;
490 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
491 ifp->if_softc = sc;
492 ifp->if_start = xi_start;
493 ifp->if_ioctl = xi_ioctl;
494 ifp->if_watchdog = xi_watchdog;
495 ifp->if_flags =
496 IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | IFF_MULTICAST;
497 IFQ_SET_READY(&ifp->if_snd);
498
499 /* Reset and initialize the card. */
500 xi_full_reset(sc);
501
502 /*
503 * Initialize our media structures and probe the MII.
504 */
505 sc->sc_mii.mii_ifp = ifp;
506 sc->sc_mii.mii_readreg = xi_mdi_read;
507 sc->sc_mii.mii_writereg = xi_mdi_write;
508 sc->sc_mii.mii_statchg = xi_statchg;
509 ifmedia_init(&sc->sc_mii.mii_media, 0, xi_mediachange,
510 xi_mediastatus);
511 DPRINTF(XID_MII | XID_CONFIG,
512 ("xi: bmsr %x\n", xi_mdi_read(&sc->sc_dev, 0, 1)));
513 mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
514 MII_OFFSET_ANY, 0);
515 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL)
516 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0,
517 NULL);
518 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
519
520 /* 802.1q capability */
521 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
522 /* Attach the interface. */
523 if_attach(ifp);
524 ether_ifattach(ifp, sc->sc_enaddr);
525 psc->sc_resource |= XI_RES_MI;
526
527 #if NRND > 0
528 rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
529 RND_TYPE_NET, 0);
530 #endif
531
532 /*
533 * Reset and initialize the card again for DINGO (as found in Linux
534 * driver). Without this Dingo will get a watchdog timeout the first
535 * time. The ugly media tickling seems to be necessary for getting
536 * autonegotiation to work too.
537 */
538 if (sc->sc_flags & XIFLAGS_DINGO) {
539 xi_full_reset(sc);
540 xi_init(sc);
541 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
542 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_NONE);
543 xi_stop(sc);
544 }
545
546 psc->sc_powerhook = powerhook_establish(xi_pcmcia_power, sc);
547
548 pcmcia_function_disable(psc->sc_pf);
549 psc->sc_resource &= ~XI_RES_PCIC;
550
551 return;
552
553 fail:
554 if ((psc->sc_resource & XI_RES_IO_MAP) != 0) {
555 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
556 psc->sc_resource &= ~XI_RES_IO_MAP;
557 }
558 if ((psc->sc_resource & XI_RES_IO_ALLOC) != 0) {
559 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
560 psc->sc_resource &= ~XI_RES_IO_ALLOC;
561 }
562 if (psc->sc_resource & XI_RES_PCIC) {
563 pcmcia_function_disable(pa->pf);
564 psc->sc_resource &= ~XI_RES_PCIC;
565 }
566 free(SIMPLEQ_FIRST(&psc->sc_pf->cfe_head), M_DEVBUF);
567 }
568
569 int
570 xi_pcmcia_detach(self, flags)
571 struct device *self;
572 int flags;
573 {
574 struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
575 struct xi_softc *sc = &psc->sc_xi;
576 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
577
578 DPRINTF(XID_CONFIG, ("xi_pcmcia_detach()\n"));
579
580 if (psc->sc_powerhook != NULL)
581 powerhook_disestablish(psc->sc_powerhook);
582
583 #if NRND > 0
584 rnd_detach_source(&sc->sc_rnd_source);
585 #endif
586
587 if ((psc->sc_resource & XI_RES_MI) != 0) {
588 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
589 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
590 ether_ifdetach(ifp);
591 if_detach(ifp);
592 psc->sc_resource &= ~XI_RES_MI;
593 }
594 if (psc->sc_resource & XI_RES_IO_MAP) {
595 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
596 psc->sc_resource &= ~XI_RES_IO_MAP;
597 }
598 if ((psc->sc_resource & XI_RES_IO_ALLOC) != 0) {
599 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
600 psc->sc_resource &= ~XI_RES_IO_ALLOC;
601 }
602
603 xi_pcmcia_disable(psc);
604
605 free(SIMPLEQ_FIRST(&psc->sc_pf->cfe_head), M_DEVBUF);
606
607 return 0;
608 }
609
610 int
611 xi_pcmcia_activate(self, act)
612 struct device *self;
613 enum devact act;
614 {
615 struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
616 struct xi_softc *sc = &psc->sc_xi;
617 int s, rv=0;
618
619 DPRINTF(XID_CONFIG, ("xi_pcmcia_activate()\n"));
620
621 s = splnet();
622 switch (act) {
623 case DVACT_ACTIVATE:
624 rv = EOPNOTSUPP;
625 break;
626
627 case DVACT_DEACTIVATE:
628 if_deactivate(&sc->sc_ethercom.ec_if);
629 break;
630 }
631 splx(s);
632 return (rv);
633 }
634
635 static int
636 xi_pcmcia_enable(psc)
637 struct xi_pcmcia_softc *psc;
638 {
639 struct xi_softc *sc = &psc->sc_xi;
640
641 DPRINTF(XID_CONFIG,("xi_pcmcia_enable()\n"));
642
643 if (pcmcia_function_enable(psc->sc_pf))
644 return (1);
645 psc->sc_resource |= XI_RES_PCIC;
646
647 /* establish the interrupt. */
648 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, xi_intr, sc);
649 if (psc->sc_ih == NULL) {
650 printf("%s: couldn't establish interrupt\n",
651 sc->sc_dev.dv_xname);
652 pcmcia_function_disable(psc->sc_pf);
653 psc->sc_resource &= ~XI_RES_PCIC;
654 return (1);
655 }
656
657 xi_full_reset(sc);
658
659 return (0);
660 }
661
662
663 static void
664 xi_pcmcia_disable(psc)
665 struct xi_pcmcia_softc *psc;
666 {
667 DPRINTF(XID_CONFIG,("xi_pcmcia_disable()\n"));
668
669 if (psc->sc_resource & XI_RES_PCIC) {
670 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
671 pcmcia_function_disable(psc->sc_pf);
672 psc->sc_resource &= ~XI_RES_PCIC;
673 }
674 }
675
676
677 static void
678 xi_pcmcia_power(why, arg)
679 int why;
680 void *arg;
681 {
682 struct xi_pcmcia_softc *psc = arg;
683 struct xi_softc *sc = &psc->sc_xi;
684 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
685 int s;
686
687 DPRINTF(XID_CONFIG,("xi_pcmcia_power()\n"));
688
689 s = splnet();
690
691 switch (why) {
692 case PWR_SUSPEND:
693 case PWR_STANDBY:
694 if (ifp->if_flags & IFF_RUNNING) {
695 xi_stop(sc);
696 }
697 ifp->if_flags &= ~IFF_RUNNING;
698 ifp->if_timer = 0;
699 break;
700 case PWR_RESUME:
701 if ((ifp->if_flags & IFF_RUNNING) == 0) {
702 xi_init(sc);
703 }
704 ifp->if_flags |= IFF_RUNNING;
705 break;
706 case PWR_SOFTSUSPEND:
707 case PWR_SOFTSTANDBY:
708 case PWR_SOFTRESUME:
709 break;
710 }
711 splx(s);
712 }
713
714 /*
715 * XXX These two functions might be OK to factor out into pcmcia.c since
716 * if_sm_pcmcia.c uses similar ones.
717 */
718 static int
719 xi_pcmcia_funce_enaddr(parent, myla)
720 struct device *parent;
721 u_int8_t *myla;
722 {
723 /* XXX The Linux driver has more ways to do this in case of failure. */
724 return (pcmcia_scan_cis(parent, xi_pcmcia_lan_nid_ciscallback, myla));
725 }
726
727 static int
728 xi_pcmcia_lan_nid_ciscallback(tuple, arg)
729 struct pcmcia_tuple *tuple;
730 void *arg;
731 {
732 u_int8_t *myla = arg;
733 int i;
734
735 DPRINTF(XID_CONFIG, ("xi_pcmcia_lan_nid_ciscallback()\n"));
736
737 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
738 if (tuple->length < 2)
739 return (0);
740
741 switch (pcmcia_tuple_read_1(tuple, 0)) {
742 case PCMCIA_TPLFE_TYPE_LAN_NID:
743 if (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)
744 return (0);
745 break;
746
747 case 0x02:
748 /*
749 * Not sure about this, I don't have a CE2
750 * that puts the ethernet addr here.
751 */
752 if (pcmcia_tuple_read_1(tuple, 1) != 13)
753 return (0);
754 break;
755
756 default:
757 return (0);
758 }
759
760 for (i = 0; i < ETHER_ADDR_LEN; i++)
761 myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
762 return (1);
763 }
764
765 /* Yet another spot where this might be. */
766 if (tuple->code == 0x89) {
767 pcmcia_tuple_read_1(tuple, 1);
768 for (i = 0; i < ETHER_ADDR_LEN; i++)
769 myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
770 return (1);
771 }
772 return (0);
773 }
774
775 int
776 xi_pcmcia_manfid_ciscallback(tuple, arg)
777 struct pcmcia_tuple *tuple;
778 void *arg;
779 {
780 u_int8_t *id = arg;
781
782 DPRINTF(XID_CONFIG, ("xi_pcmcia_manfid_callback()\n"));
783
784 if (tuple->code != PCMCIA_CISTPL_MANFID)
785 return (0);
786
787 if (tuple->length < 2)
788 return (0);
789
790 *id = pcmcia_tuple_read_1(tuple, 4);
791 return (1);
792 }
793
794 static int
795 xi_intr(arg)
796 void *arg;
797 {
798 struct xi_softc *sc = arg;
799 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
800 u_int8_t esr, rsr, isr, rx_status, savedpage;
801 u_int16_t tx_status, recvcount = 0, tempint;
802
803 DPRINTF(XID_CONFIG, ("xi_intr()\n"));
804
805 #if 0
806 if (!(ifp->if_flags & IFF_RUNNING))
807 return (0);
808 #endif
809
810 ifp->if_timer = 0; /* turn watchdog timer off */
811
812 if (sc->sc_flags & XIFLAGS_MOHAWK) {
813 /* Disable interrupt (Linux does it). */
814 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
815 0);
816 }
817
818 savedpage =
819 bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + PR);
820
821 PAGE(sc, 0);
822 esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ESR);
823 isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ISR0);
824 rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
825
826 /* Check to see if card has been ejected. */
827 if (isr == 0xff) {
828 #ifdef DIAGNOSTIC
829 printf("%s: interrupt for dead card\n", sc->sc_dev.dv_xname);
830 #endif
831 goto end;
832 }
833
834 PAGE(sc, 40);
835 rx_status =
836 bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0);
837 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0,
838 ~rx_status & 0xff);
839 tx_status =
840 bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
841 tx_status |=
842 bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1) << 8;
843 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0,0);
844 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1,0);
845
846 PAGE(sc, 0);
847 while (esr & FULL_PKT_RCV) {
848 if (!(rsr & RSR_RX_OK))
849 break;
850
851 /* Compare bytes read this interrupt to hard maximum. */
852 if (recvcount > MAX_BYTES_INTR) {
853 DPRINTF(XID_INTR,
854 ("xi: too many bytes this interrupt\n"));
855 ifp->if_iqdrops++;
856 /* Drop packet. */
857 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
858 sc->sc_offset + DO0, DO_SKIP_RX_PKT);
859 }
860 tempint = xi_get(sc); /* XXX doesn't check the error! */
861 recvcount += tempint;
862 ifp->if_ibytes += tempint;
863 esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
864 sc->sc_offset + ESR);
865 rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
866 sc->sc_offset + RSR);
867 }
868
869 /* Packet too long? */
870 if (rsr & RSR_TOO_LONG) {
871 ifp->if_ierrors++;
872 DPRINTF(XID_INTR, ("xi: packet too long\n"));
873 }
874
875 /* CRC error? */
876 if (rsr & RSR_CRCERR) {
877 ifp->if_ierrors++;
878 DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
879 }
880
881 /* Alignment error? */
882 if (rsr & RSR_ALIGNERR) {
883 ifp->if_ierrors++;
884 DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
885 }
886
887 /* Check for rx overrun. */
888 if (rx_status & RX_OVERRUN) {
889 ifp->if_ierrors++;
890 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
891 CLR_RX_OVERRUN);
892 DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
893 }
894
895 /* Try to start more packets transmitting. */
896 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
897 xi_start(ifp);
898
899 /* Detected excessive collisions? */
900 if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
901 DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
902 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
903 RESTART_TX);
904 ifp->if_oerrors++;
905 }
906
907 if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
908 ifp->if_oerrors++;
909
910 end:
911 /* Reenable interrupts. */
912 PAGE(sc, savedpage);
913 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
914 ENABLE_INT);
915
916 /* have handled the interrupt */
917 #if NRND > 0
918 rnd_add_uint32(&sc->sc_rnd_source, tx_status);
919 #endif
920
921 return (1);
922 }
923
924 /*
925 * Pull a packet from the card into an mbuf chain.
926 */
927 static u_int16_t
928 xi_get(sc)
929 struct xi_softc *sc;
930 {
931 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
932 struct mbuf *top, **mp, *m;
933 u_int16_t pktlen, len, recvcount = 0;
934 u_int8_t *data;
935 u_int8_t rsr;
936
937 DPRINTF(XID_CONFIG, ("xi_get()\n"));
938
939 PAGE(sc, 0);
940 rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
941
942 pktlen =
943 bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
944 RBC_COUNT_MASK;
945
946 DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
947
948 if (pktlen == 0) {
949 /*
950 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
951 * when MPE is set. It is not known why.
952 */
953 return (0);
954 }
955
956 /* XXX should this be incremented now ? */
957 recvcount += pktlen;
958
959 MGETHDR(m, M_DONTWAIT, MT_DATA);
960 if (m == 0)
961 return (recvcount);
962 m->m_pkthdr.rcvif = ifp;
963 m->m_pkthdr.len = pktlen;
964 m->m_flags |= M_HASFCS;
965 len = MHLEN;
966 top = 0;
967 mp = ⊤
968
969 while (pktlen > 0) {
970 if (top) {
971 MGET(m, M_DONTWAIT, MT_DATA);
972 if (m == 0) {
973 m_freem(top);
974 return (recvcount);
975 }
976 len = MLEN;
977 }
978 if (pktlen >= MINCLSIZE) {
979 MCLGET(m, M_DONTWAIT);
980 if (!(m->m_flags & M_EXT)) {
981 m_freem(m);
982 m_freem(top);
983 return (recvcount);
984 }
985 len = MCLBYTES;
986 }
987 if (!top) {
988 caddr_t newdata = (caddr_t)ALIGN(m->m_data +
989 sizeof(struct ether_header)) -
990 sizeof(struct ether_header);
991 len -= newdata - m->m_data;
992 m->m_data = newdata;
993 }
994 len = min(pktlen, len);
995 data = mtod(m, u_int8_t *);
996 if (len > 1) {
997 len &= ~1;
998 bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh,
999 sc->sc_offset + EDP, (u_int16_t *)data, len>>1);
1000 } else
1001 *data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
1002 sc->sc_offset + EDP);
1003 m->m_len = len;
1004 pktlen -= len;
1005 *mp = m;
1006 mp = &m->m_next;
1007 }
1008
1009 /* Skip Rx packet. */
1010 bus_space_write_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + DO0,
1011 DO_SKIP_RX_PKT);
1012
1013 ifp->if_ipackets++;
1014
1015 #if NBPFILTER > 0
1016 if (ifp->if_bpf)
1017 bpf_mtap(ifp->if_bpf, top);
1018 #endif
1019
1020 (*ifp->if_input)(ifp, top);
1021 return (recvcount);
1022 }
1023
1024 /*
1025 * Serial management for the MII.
1026 * The DELAY's below stem from the fact that the maximum frequency
1027 * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
1028 * go much faster than that.
1029 */
1030
1031 /* Let the MII serial management be idle for one period. */
1032 static INLINE void xi_mdi_idle __P((struct xi_softc *));
1033 static INLINE void
1034 xi_mdi_idle(sc)
1035 struct xi_softc *sc;
1036 {
1037 bus_space_tag_t bst = sc->sc_bst;
1038 bus_space_handle_t bsh = sc->sc_bsh;
1039 bus_addr_t offset = sc->sc_offset;
1040
1041 /* Drive MDC low... */
1042 bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
1043 DELAY(1);
1044
1045 /* and high again. */
1046 bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
1047 DELAY(1);
1048 }
1049
1050 /* Pulse out one bit of data. */
1051 static INLINE void xi_mdi_pulse __P((struct xi_softc *, int));
1052 static INLINE void
1053 xi_mdi_pulse(sc, data)
1054 struct xi_softc *sc;
1055 int data;
1056 {
1057 bus_space_tag_t bst = sc->sc_bst;
1058 bus_space_handle_t bsh = sc->sc_bsh;
1059 bus_addr_t offset = sc->sc_offset;
1060 u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
1061
1062 /* First latch the data bit MDIO with clock bit MDC low...*/
1063 bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_LOW);
1064 DELAY(1);
1065
1066 /* then raise the clock again, preserving the data bit. */
1067 bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_HIGH);
1068 DELAY(1);
1069 }
1070
1071 /* Probe one bit of data. */
1072 static INLINE int xi_mdi_probe __P((struct xi_softc *sc));
1073 static INLINE int
1074 xi_mdi_probe(sc)
1075 struct xi_softc *sc;
1076 {
1077 bus_space_tag_t bst = sc->sc_bst;
1078 bus_space_handle_t bsh = sc->sc_bsh;
1079 bus_size_t offset = sc->sc_offset;
1080 u_int8_t x;
1081
1082 /* Pull clock bit MDCK low... */
1083 bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
1084 DELAY(1);
1085
1086 /* Read data and drive clock high again. */
1087 x = bus_space_read_1(bst, bsh, offset + GP2) & MDIO;
1088 bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
1089 DELAY(1);
1090
1091 return (x);
1092 }
1093
1094 /* Pulse out a sequence of data bits. */
1095 static INLINE void xi_mdi_pulse_bits __P((struct xi_softc *, u_int32_t, int));
1096 static INLINE void
1097 xi_mdi_pulse_bits(sc, data, len)
1098 struct xi_softc *sc;
1099 u_int32_t data;
1100 int len;
1101 {
1102 u_int32_t mask;
1103
1104 for (mask = 1 << (len - 1); mask; mask >>= 1)
1105 xi_mdi_pulse(sc, data & mask);
1106 }
1107
1108 /* Read a PHY register. */
1109 static int
1110 xi_mdi_read(self, phy, reg)
1111 struct device *self;
1112 int phy;
1113 int reg;
1114 {
1115 struct xi_softc *sc = (struct xi_softc *)self;
1116 int i;
1117 u_int32_t mask;
1118 u_int32_t data = 0;
1119
1120 PAGE(sc, 2);
1121 for (i = 0; i < 32; i++) /* Synchronize. */
1122 xi_mdi_pulse(sc, 1);
1123 xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
1124 xi_mdi_pulse_bits(sc, phy, 5); /* PHY address */
1125 xi_mdi_pulse_bits(sc, reg, 5); /* PHY register */
1126 xi_mdi_idle(sc); /* Turn around. */
1127 xi_mdi_probe(sc); /* Drop initial zero bit. */
1128
1129 for (mask = 1 << 15; mask; mask >>= 1) {
1130 if (xi_mdi_probe(sc))
1131 data |= mask;
1132 }
1133 xi_mdi_idle(sc);
1134
1135 DPRINTF(XID_MII,
1136 ("xi_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
1137
1138 return (data);
1139 }
1140
1141 /* Write a PHY register. */
1142 static void
1143 xi_mdi_write(self, phy, reg, value)
1144 struct device *self;
1145 int phy;
1146 int reg;
1147 int value;
1148 {
1149 struct xi_softc *sc = (struct xi_softc *)self;
1150 int i;
1151
1152 PAGE(sc, 2);
1153 for (i = 0; i < 32; i++) /* Synchronize. */
1154 xi_mdi_pulse(sc, 1);
1155 xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
1156 xi_mdi_pulse_bits(sc, phy, 5); /* PHY address */
1157 xi_mdi_pulse_bits(sc, reg, 5); /* PHY register */
1158 xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
1159 xi_mdi_pulse_bits(sc, value, 16); /* Write the data */
1160 xi_mdi_idle(sc); /* Idle away. */
1161
1162 DPRINTF(XID_MII,
1163 ("xi_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
1164 }
1165
1166 static void
1167 xi_statchg(self)
1168 struct device *self;
1169 {
1170 /* XXX Update ifp->if_baudrate */
1171 }
1172
1173 /*
1174 * Change media according to request.
1175 */
1176 static int
1177 xi_mediachange(ifp)
1178 struct ifnet *ifp;
1179 {
1180 DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
1181
1182 if (ifp->if_flags & IFF_UP)
1183 xi_init(ifp->if_softc);
1184 return (0);
1185 }
1186
1187 /*
1188 * Notify the world which media we're using.
1189 */
1190 static void
1191 xi_mediastatus(ifp, ifmr)
1192 struct ifnet *ifp;
1193 struct ifmediareq *ifmr;
1194 {
1195 struct xi_softc *sc = ifp->if_softc;
1196
1197 DPRINTF(XID_CONFIG, ("xi_mediastatus()\n"));
1198
1199 mii_pollstat(&sc->sc_mii);
1200 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1201 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1202 }
1203
1204 static void
1205 xi_reset(sc)
1206 struct xi_softc *sc;
1207 {
1208 int s;
1209
1210 DPRINTF(XID_CONFIG, ("xi_reset()\n"));
1211
1212 s = splnet();
1213 xi_stop(sc);
1214 xi_full_reset(sc);
1215 xi_init(sc);
1216 splx(s);
1217 }
1218
1219 static void
1220 xi_watchdog(ifp)
1221 struct ifnet *ifp;
1222 {
1223 struct xi_softc *sc = ifp->if_softc;
1224
1225 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1226 ++ifp->if_oerrors;
1227
1228 xi_reset(sc);
1229 }
1230
1231 static void
1232 xi_stop(sc)
1233 register struct xi_softc *sc;
1234 {
1235 DPRINTF(XID_CONFIG, ("xi_stop()\n"));
1236
1237 /* Disable interrupts. */
1238 PAGE(sc, 0);
1239 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR, 0);
1240
1241 PAGE(sc, 1);
1242 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + IMR0, 0);
1243
1244 /* Power down, wait. */
1245 PAGE(sc, 4);
1246 bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + GP1, 0);
1247 DELAY(40000);
1248
1249 /* Cancel watchdog timer. */
1250 sc->sc_ethercom.ec_if.if_timer = 0;
1251 }
1252
1253 static void
1254 xi_init(sc)
1255 struct xi_softc *sc;
1256 {
1257 struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)sc;
1258 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1259 int s;
1260
1261 DPRINTF(XID_CONFIG, ("xi_init()\n"));
1262
1263 if ((psc->sc_resource & XI_RES_PCIC) == 0)
1264 xi_pcmcia_enable(psc);
1265
1266 s = splnet();
1267
1268 xi_set_address(sc);
1269
1270 /* Set current media. */
1271 mii_mediachg(&sc->sc_mii);
1272
1273 ifp->if_flags |= IFF_RUNNING;
1274 ifp->if_flags &= ~IFF_OACTIVE;
1275 splx(s);
1276 }
1277
1278 /*
1279 * Start outputting on the interface.
1280 * Always called as splnet().
1281 */
1282 static void
1283 xi_start(ifp)
1284 struct ifnet *ifp;
1285 {
1286 struct xi_softc *sc = ifp->if_softc;
1287 bus_space_tag_t bst = sc->sc_bst;
1288 bus_space_handle_t bsh = sc->sc_bsh;
1289 bus_addr_t offset = sc->sc_offset;
1290 unsigned int s, len, pad = 0;
1291 struct mbuf *m0, *m;
1292 u_int16_t space;
1293
1294 DPRINTF(XID_CONFIG, ("xi_start()\n"));
1295
1296 /* Don't transmit if interface is busy or not running. */
1297 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
1298 DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
1299 return;
1300 }
1301
1302 /* Peek at the next packet. */
1303 IFQ_POLL(&ifp->if_snd, m0);
1304 if (m0 == 0)
1305 return;
1306
1307 /* We need to use m->m_pkthdr.len, so require the header. */
1308 if (!(m0->m_flags & M_PKTHDR))
1309 panic("xi_start: no header mbuf");
1310
1311 len = m0->m_pkthdr.len;
1312
1313 /* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
1314 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
1315 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
1316
1317 PAGE(sc, 0);
1318 space = bus_space_read_2(bst, bsh, offset + TSO0) & 0x7fff;
1319 if (len + pad + 2 > space) {
1320 DPRINTF(XID_FIFO,
1321 ("xi: not enough space in output FIFO (%d > %d)\n",
1322 len + pad + 2, space));
1323 return;
1324 }
1325
1326 IFQ_DEQUEUE(&ifp->if_snd, m0);
1327
1328 #if NBPFILTER > 0
1329 if (ifp->if_bpf)
1330 bpf_mtap(ifp->if_bpf, m0);
1331 #endif
1332
1333 /*
1334 * Do the output at splhigh() so that an interrupt from another device
1335 * won't cause a FIFO underrun.
1336 */
1337 s = splhigh();
1338
1339 bus_space_write_2(bst, bsh, offset + TSO2, (u_int16_t)len + pad + 2);
1340 bus_space_write_2(bst, bsh, offset + EDP, (u_int16_t)len + pad);
1341 for (m = m0; m; ) {
1342 if (m->m_len > 1)
1343 bus_space_write_multi_2(bst, bsh, offset + EDP,
1344 mtod(m, u_int16_t *), m->m_len>>1);
1345 if (m->m_len & 1)
1346 bus_space_write_1(bst, bsh, offset + EDP,
1347 *(mtod(m, u_int8_t *) + m->m_len - 1));
1348 MFREE(m, m0);
1349 m = m0;
1350 }
1351 if (sc->sc_flags & XIFLAGS_MOHAWK)
1352 bus_space_write_1(bst, bsh, offset + CR, TX_PKT | ENABLE_INT);
1353 else {
1354 for (; pad > 1; pad -= 2)
1355 bus_space_write_2(bst, bsh, offset + EDP, 0);
1356 if (pad == 1)
1357 bus_space_write_1(bst, bsh, offset + EDP, 0);
1358 }
1359
1360 splx(s);
1361
1362 ifp->if_timer = 5;
1363 ++ifp->if_opackets;
1364 }
1365
1366 static int
1367 xi_ether_ioctl(ifp, cmd, data)
1368 struct ifnet *ifp;
1369 u_long cmd;
1370 caddr_t data;
1371 {
1372 struct ifaddr *ifa = (struct ifaddr *)data;
1373 struct xi_softc *sc = ifp->if_softc;
1374
1375
1376 DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
1377
1378 switch (cmd) {
1379 case SIOCSIFADDR:
1380 ifp->if_flags |= IFF_UP;
1381
1382 switch (ifa->ifa_addr->sa_family) {
1383 #ifdef INET
1384 case AF_INET:
1385 xi_init(sc);
1386 arp_ifinit(ifp, ifa);
1387 break;
1388 #endif /* INET */
1389
1390 #ifdef NS
1391 case AF_NS:
1392 {
1393 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1394
1395 if (ns_nullhost(*ina))
1396 ina->x_host = *(union ns_host *)
1397 LLADDR(ifp->if_sadl);
1398 else
1399 memcpy(LLADDR(ifp->if_sadl), ina->x_host.c_host,
1400 ifp->if_addrlen);
1401 /* Set new address. */
1402 xi_init(sc);
1403 break;
1404 }
1405 #endif /* NS */
1406
1407 default:
1408 xi_init(sc);
1409 break;
1410 }
1411 break;
1412
1413 default:
1414 return (EINVAL);
1415 }
1416
1417 return (0);
1418 }
1419
1420 static int
1421 xi_ioctl(ifp, command, data)
1422 struct ifnet *ifp;
1423 u_long command;
1424 caddr_t data;
1425 {
1426 struct xi_pcmcia_softc *psc = ifp->if_softc;
1427 struct xi_softc *sc = &psc->sc_xi;
1428 struct ifreq *ifr = (struct ifreq *)data;
1429 int s, error = 0;
1430
1431 DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
1432
1433 s = splnet();
1434
1435 switch (command) {
1436 case SIOCSIFADDR:
1437 error = xi_ether_ioctl(ifp, command, data);
1438 break;
1439
1440 case SIOCSIFFLAGS:
1441 sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1442
1443 PAGE(sc, 0x42);
1444 if ((ifp->if_flags & IFF_PROMISC) ||
1445 (ifp->if_flags & IFF_ALLMULTI))
1446 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
1447 sc->sc_offset + SWC1,
1448 SWC1_PROMISC | SWC1_MCAST_PROM);
1449 else
1450 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
1451 sc->sc_offset + SWC1, 0);
1452
1453 /*
1454 * If interface is marked up and not running, then start it.
1455 * If it is marked down and running, stop it.
1456 * XXX If it's up then re-initialize it. This is so flags
1457 * such as IFF_PROMISC are handled.
1458 */
1459 if (ifp->if_flags & IFF_UP) {
1460 xi_init(sc);
1461 } else {
1462 if (ifp->if_flags & IFF_RUNNING) {
1463 xi_pcmcia_disable(psc);
1464 xi_stop(sc);
1465 ifp->if_flags &= ~IFF_RUNNING;
1466 }
1467 }
1468 break;
1469
1470 case SIOCADDMULTI:
1471 case SIOCDELMULTI:
1472 sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1473 error = (command == SIOCADDMULTI) ?
1474 ether_addmulti(ifr, &sc->sc_ethercom) :
1475 ether_delmulti(ifr, &sc->sc_ethercom);
1476
1477 if (error == ENETRESET) {
1478 /*
1479 * Multicast list has changed; set the hardware
1480 * filter accordingly.
1481 */
1482 if (!sc->sc_all_mcasts &&
1483 !(ifp->if_flags & IFF_PROMISC))
1484 xi_set_address(sc);
1485
1486 /*
1487 * xi_set_address() can turn on all_mcasts if we run
1488 * out of space, so check it again rather than else {}.
1489 */
1490 if (sc->sc_all_mcasts)
1491 xi_init(sc);
1492 error = 0;
1493 }
1494 break;
1495
1496 case SIOCSIFMEDIA:
1497 case SIOCGIFMEDIA:
1498 error =
1499 ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
1500 break;
1501
1502 default:
1503 error = EINVAL;
1504 }
1505 splx(s);
1506 return (error);
1507 }
1508
1509 static void
1510 xi_set_address(sc)
1511 struct xi_softc *sc;
1512 {
1513 bus_space_tag_t bst = sc->sc_bst;
1514 bus_space_handle_t bsh = sc->sc_bsh;
1515 bus_addr_t offset = sc->sc_offset;
1516 struct ethercom *ether = &sc->sc_ethercom;
1517 #if 0
1518 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1519 #endif
1520 #if WORKING_MULTICAST
1521 struct ether_multistep step;
1522 struct ether_multi *enm;
1523 int page, pos, num;
1524 #endif
1525 int i;
1526
1527 DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
1528
1529 PAGE(sc, 0x50);
1530 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1531 bus_space_write_1(bst, bsh, offset + IA + i,
1532 sc->sc_enaddr[(sc->sc_flags & XIFLAGS_MOHAWK) ? 5-i : i]);
1533 }
1534
1535 if (ether->ec_multicnt > 0) {
1536 #ifdef WORKING_MULTICAST
1537 if (ether->ec_multicnt > 9) {
1538 #else
1539 {
1540 #endif
1541 PAGE(sc, 0x42);
1542 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
1543 sc->sc_offset + SWC1,
1544 SWC1_PROMISC | SWC1_MCAST_PROM);
1545 return;
1546 }
1547
1548 #ifdef WORKING_MULTICAST
1549
1550 ETHER_FIRST_MULTI(step, ether, enm);
1551
1552 pos = IA + 6;
1553 for (page = 0x50, num = ether->ec_multicnt; num > 0 && enm;
1554 num--) {
1555 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1556 sizeof(enm->enm_addrlo)) != 0) {
1557 /*
1558 * The multicast address is really a range;
1559 * it's easier just to accept all multicasts.
1560 * XXX should we be setting IFF_ALLMULTI here?
1561 */
1562 #if 0
1563 ifp->if_flags |= IFF_ALLMULTI;
1564 #endif
1565 sc->sc_all_mcasts=1;
1566 break;
1567 }
1568
1569 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1570 printf("%x:", enm->enm_addrlo[i]);
1571 bus_space_write_1(bst, bsh, offset + pos,
1572 enm->enm_addrlo[
1573 (sc->sc_flags & XIFLAGS_MOHAWK) ? 5-i : i]);
1574
1575 if (++pos > 15) {
1576 pos = IA;
1577 page++;
1578 PAGE(sc, page);
1579 }
1580 }
1581 printf("\n");
1582 ETHER_NEXT_MULTI(step, enm);
1583 }
1584 #endif
1585 }
1586 }
1587
1588 static void
1589 xi_cycle_power(sc)
1590 struct xi_softc *sc;
1591 {
1592 bus_space_tag_t bst = sc->sc_bst;
1593 bus_space_handle_t bsh = sc->sc_bsh;
1594 bus_addr_t offset = sc->sc_offset;
1595
1596 DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
1597
1598 PAGE(sc, 4);
1599 DELAY(1);
1600 bus_space_write_1(bst, bsh, offset + GP1, 0);
1601 DELAY(40000);
1602 if (sc->sc_flags & XIFLAGS_MOHAWK)
1603 bus_space_write_1(bst, bsh, offset + GP1, POWER_UP);
1604 else
1605 /* XXX What is bit 2 (aka AIC)? */
1606 bus_space_write_1(bst, bsh, offset + GP1, POWER_UP | 4);
1607 DELAY(20000);
1608 }
1609
1610 static void
1611 xi_full_reset(sc)
1612 struct xi_softc *sc;
1613 {
1614 bus_space_tag_t bst = sc->sc_bst;
1615 bus_space_handle_t bsh = sc->sc_bsh;
1616 bus_addr_t offset = sc->sc_offset;
1617
1618 DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
1619
1620 /* Do an as extensive reset as possible on all functions. */
1621 xi_cycle_power(sc);
1622 bus_space_write_1(bst, bsh, offset + CR, SOFT_RESET);
1623 DELAY(20000);
1624 bus_space_write_1(bst, bsh, offset + CR, 0);
1625 DELAY(20000);
1626 if (sc->sc_flags & XIFLAGS_MOHAWK) {
1627 PAGE(sc, 4);
1628 /*
1629 * Drive GP1 low to power up ML6692 and GP2 high to power up
1630 * the 10Mhz chip. XXX What chip is that? The phy?
1631 */
1632 bus_space_write_1(bst, bsh, offset + GP0,
1633 GP1_OUT | GP2_OUT | GP2_WR);
1634 }
1635 DELAY(500000);
1636
1637 /* Get revision information. XXX Symbolic constants. */
1638 sc->sc_rev = bus_space_read_1(bst, bsh, offset + BV) &
1639 ((sc->sc_flags & XIFLAGS_MOHAWK) ? 0x70 : 0x30) >> 4;
1640
1641 /* Media selection. XXX Maybe manual overriding too? */
1642 if (!(sc->sc_flags & XIFLAGS_MOHAWK)) {
1643 PAGE(sc, 4);
1644 /*
1645 * XXX I have no idea what this really does, it is from the
1646 * Linux driver.
1647 */
1648 bus_space_write_1(bst, bsh, offset + GP0, GP1_OUT);
1649 }
1650 DELAY(40000);
1651
1652 /* Setup the ethernet interrupt mask. */
1653 PAGE(sc, 1);
1654 #if 1
1655 bus_space_write_1(bst, bsh, offset + IMR0,
1656 ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
1657 ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
1658 #else
1659 bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
1660 #endif
1661 if (!(sc->sc_flags & XIFLAGS_DINGO)) {
1662 /* XXX What is this? Not for Dingo at least. */
1663 /* Unmask TX underrun detection */
1664 bus_space_write_1(bst, bsh, offset + IMR1, 1);
1665 }
1666
1667 /*
1668 * Disable source insertion.
1669 * XXX Dingo does not have this bit, but Linux does it unconditionally.
1670 */
1671 if (!(sc->sc_flags & XIFLAGS_DINGO)) {
1672 PAGE(sc, 0x42);
1673 bus_space_write_1(bst, bsh, offset + SWC0, 0x20);
1674 }
1675
1676 /* Set the local memory dividing line. */
1677 if (sc->sc_rev != 1) {
1678 PAGE(sc, 2);
1679 /* XXX Symbolic constant preferrable. */
1680 bus_space_write_2(bst, bsh, offset + RBS0, 0x2000);
1681 }
1682
1683 xi_set_address(sc);
1684
1685 /*
1686 * Apparently the receive byte pointer can be bad after a reset, so
1687 * we hardwire it correctly.
1688 */
1689 PAGE(sc, 0);
1690 bus_space_write_2(bst, bsh, offset + DO0, DO_CHG_OFFSET);
1691
1692 /* Setup ethernet MAC registers. XXX Symbolic constants. */
1693 PAGE(sc, 0x40);
1694 bus_space_write_1(bst, bsh, offset + RX0MSK,
1695 PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
1696 bus_space_write_1(bst, bsh, offset + TX0MSK,
1697 CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
1698 SQE | TX_ABORT | TX_OK);
1699 if (!(sc->sc_flags & XIFLAGS_DINGO))
1700 /* XXX From Linux, dunno what 0xb0 means. */
1701 bus_space_write_1(bst, bsh, offset + TX1MSK, 0xb0);
1702 bus_space_write_1(bst, bsh, offset + RXST0, 0);
1703 bus_space_write_1(bst, bsh, offset + TXST0, 0);
1704 bus_space_write_1(bst, bsh, offset + TXST1, 0);
1705
1706 /* Enable MII function if available. */
1707 if (LIST_FIRST(&sc->sc_mii.mii_phys)) {
1708 PAGE(sc, 2);
1709 bus_space_write_1(bst, bsh, offset + MSR,
1710 bus_space_read_1(bst, bsh, offset + MSR) | SELECT_MII);
1711 DELAY(20000);
1712 } else {
1713 PAGE(sc, 0);
1714
1715 /* XXX Do we need to do this? */
1716 PAGE(sc, 0x42);
1717 bus_space_write_1(bst, bsh, offset + SWC1, SWC1_AUTO_MEDIA);
1718 DELAY(50000);
1719
1720 /* XXX Linux probes the media here. */
1721 }
1722
1723 /* Configure the LED registers. */
1724 PAGE(sc, 2);
1725
1726 /* XXX This is not good for 10base2. */
1727 bus_space_write_1(bst, bsh, offset + LED,
1728 LED_TX_ACT << LED1_SHIFT | LED_10MB_LINK << LED0_SHIFT);
1729 if (sc->sc_flags & XIFLAGS_DINGO)
1730 bus_space_write_1(bst, bsh, offset + LED3,
1731 LED_100MB_LINK << LED3_SHIFT);
1732
1733 /* Enable receiver and go online. */
1734 PAGE(sc, 0x40);
1735 bus_space_write_1(bst, bsh, offset + CMD0, ENABLE_RX | ONLINE);
1736
1737 #if 0
1738 /* XXX Linux does this here - is it necessary? */
1739 PAGE(sc, 1);
1740 bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
1741 if (!(sc->sc_flags & XIFLAGS_DINGO)) {
1742 /* XXX What is this? Not for Dingo at least. */
1743 bus_space_write_1(bst, bsh, offset + IMR1, 1);
1744 }
1745 #endif
1746
1747 /* Enable interrupts. */
1748 PAGE(sc, 0);
1749 bus_space_write_1(bst, bsh, offset + CR, ENABLE_INT);
1750
1751 /* XXX This is pure magic for me, found in the Linux driver. */
1752 if ((sc->sc_flags & (XIFLAGS_DINGO | XIFLAGS_MODEM)) == XIFLAGS_MODEM) {
1753 if ((bus_space_read_1(bst, bsh, offset + 0x10) & 0x01) == 0)
1754 /* Unmask the master interrupt bit. */
1755 bus_space_write_1(bst, bsh, offset + 0x10, 0x11);
1756 }
1757
1758 /*
1759 * The Linux driver says this:
1760 * We should switch back to page 0 to avoid a bug in revision 0
1761 * where regs with offset below 8 can't be read after an access
1762 * to the MAC registers.
1763 */
1764 PAGE(sc, 0);
1765 }
1766