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