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