if_xi.c revision 1.90 1 /* $NetBSD: if_xi.c,v 1.90 2019/05/28 07:41:49 msaitoh Exp $ */
2 /* OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp */
3
4 /*
5 * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 */
21
22 /*
23 * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
24 * All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by Niklas Hallqvist,
37 * Brandon Creighton and Job de Haas.
38 * 4. The name of the author may not be used to endorse or promote products
39 * derived from this software without specific prior written permission
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
50 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 */
52
53 /*
54 * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
55 */
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: if_xi.c,v 1.90 2019/05/28 07:41:49 msaitoh Exp $");
59
60 #include "opt_inet.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/device.h>
65 #include <sys/ioctl.h>
66 #include <sys/mbuf.h>
67 #include <sys/malloc.h>
68 #include <sys/socket.h>
69 #include <sys/kernel.h>
70 #include <sys/proc.h>
71
72 #include <net/if.h>
73 #include <net/if_dl.h>
74 #include <net/if_media.h>
75 #include <net/if_types.h>
76 #include <net/if_ether.h>
77 #include <net/bpf.h>
78
79 #ifdef INET
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/in_var.h>
83 #include <netinet/ip.h>
84 #include <netinet/if_inarp.h>
85 #endif
86
87 /*
88 * Maximum number of bytes to read per interrupt. Linux recommends
89 * somewhere between 2000-22000.
90 * XXX This is currently a hard maximum.
91 */
92 #define MAX_BYTES_INTR 12000
93
94 #include <dev/mii/mii.h>
95 #include <dev/mii/miivar.h>
96
97 #include <dev/pcmcia/pcmciareg.h>
98 #include <dev/pcmcia/pcmciavar.h>
99 #include <dev/pcmcia/pcmciadevs.h>
100
101 #include <dev/pcmcia/if_xireg.h>
102 #include <dev/pcmcia/if_xivar.h>
103
104 #ifdef __GNUC__
105 #define INLINE inline
106 #else
107 #define INLINE
108 #endif /* __GNUC__ */
109
110 #define XIDEBUG
111 #define XIDEBUG_VALUE 0
112
113 #ifdef XIDEBUG
114 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
115
116 #define XID_CONFIG 0x01
117 #define XID_MII 0x02
118 #define XID_INTR 0x04
119 #define XID_FIFO 0x08
120 #define XID_MCAST 0x10
121
122 #ifdef XIDEBUG_VALUE
123 int xidebug = XIDEBUG_VALUE;
124 #else
125 int xidebug = 0;
126 #endif
127 #else
128 #define DPRINTF(cat, x) (void)0
129 #endif
130
131 #define STATIC
132
133 STATIC int xi_enable(struct xi_softc *);
134 STATIC void xi_disable(struct xi_softc *);
135 STATIC void xi_cycle_power(struct xi_softc *);
136 STATIC int xi_ether_ioctl(struct ifnet *, u_long, void *);
137 STATIC void xi_full_reset(struct xi_softc *);
138 STATIC void xi_init(struct xi_softc *);
139 STATIC int xi_ioctl(struct ifnet *, u_long, void *);
140 STATIC int xi_mdi_read(device_t, int, int, uint16_t *);
141 STATIC int xi_mdi_write(device_t, int, int, uint16_t);
142 STATIC int xi_mediachange(struct ifnet *);
143 STATIC uint16_t xi_get(struct xi_softc *);
144 STATIC void xi_reset(struct xi_softc *);
145 STATIC void xi_set_address(struct xi_softc *);
146 STATIC void xi_start(struct ifnet *);
147 STATIC void xi_statchg(struct ifnet *);
148 STATIC void xi_stop(struct xi_softc *);
149 STATIC void xi_watchdog(struct ifnet *);
150
151 void
152 xi_attach(struct xi_softc *sc, uint8_t *myea)
153 {
154 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
155 struct mii_data * const mii = &sc->sc_mii;
156 #ifdef XIDEBUG
157 uint16_t bmsr;
158 #endif
159 #if 0
160 /*
161 * Configuration as advised by DINGO documentation.
162 * Dingo has some extra configuration registers in the CCR space.
163 */
164 if (sc->sc_chipset >= XI_CHIPSET_DINGO) {
165 struct pcmcia_mem_handle pcmh;
166 int ccr_window;
167 bus_size_t ccr_offset;
168
169 /* get access to the DINGO CCR space */
170 if (pcmcia_mem_alloc(psc->sc_pf, PCMCIA_CCR_SIZE_DINGO,
171 &pcmh)) {
172 DPRINTF(XID_CONFIG, ("xi: bad mem alloc\n"));
173 goto fail;
174 }
175 if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR,
176 psc->sc_pf->ccr_base, PCMCIA_CCR_SIZE_DINGO,
177 &pcmh, &ccr_offset, &ccr_window)) {
178 DPRINTF(XID_CONFIG, ("xi: bad mem map\n"));
179 pcmcia_mem_free(psc->sc_pf, &pcmh);
180 goto fail;
181 }
182
183 /* enable the second function - usually modem */
184 bus_space_write_1(pcmh.memt, pcmh.memh,
185 ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
186 bus_space_write_1(pcmh.memt, pcmh.memh,
187 ccr_offset + PCMCIA_CCR_DCOR1,
188 PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
189 bus_space_write_1(pcmh.memt, pcmh.memh,
190 ccr_offset + PCMCIA_CCR_DCOR2, 0);
191 bus_space_write_1(pcmh.memt, pcmh.memh,
192 ccr_offset + PCMCIA_CCR_DCOR3, 0);
193 bus_space_write_1(pcmh.memt, pcmh.memh,
194 ccr_offset + PCMCIA_CCR_DCOR4, 0);
195
196 /* We don't need them anymore and can free them (I think). */
197 pcmcia_mem_unmap(psc->sc_pf, ccr_window);
198 pcmcia_mem_free(psc->sc_pf, &pcmh);
199 }
200 #endif
201
202 /* Reset and initialize the card. */
203 xi_full_reset(sc);
204
205 device_printf(sc->sc_dev, "MAC address %s\n", ether_sprintf(myea));
206
207 ifp = &sc->sc_ethercom.ec_if;
208 /* Initialize the ifnet structure. */
209 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
210 ifp->if_softc = sc;
211 ifp->if_start = xi_start;
212 ifp->if_ioctl = xi_ioctl;
213 ifp->if_watchdog = xi_watchdog;
214 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
215 IFQ_SET_READY(&ifp->if_snd);
216
217 /* 802.1q capability */
218 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
219
220 /* Attach the interface. */
221 if_attach(ifp);
222 if_deferred_start_init(ifp, NULL);
223 ether_ifattach(ifp, myea);
224
225 /*
226 * Initialize our media structures and probe the MII.
227 */
228 mii->mii_ifp = ifp;
229 mii->mii_readreg = xi_mdi_read;
230 mii->mii_writereg = xi_mdi_write;
231 mii->mii_statchg = xi_statchg;
232 sc->sc_ethercom.ec_mii = mii;
233 ifmedia_init(&mii->mii_media, 0, xi_mediachange, ether_mediastatus);
234 #ifdef XIDEBUG
235 xi_mdi_read(sc->sc_dev, 0, 1, &bmsr);
236 DPRINTF(XID_MII | XID_CONFIG, ("xi: bmsr %x\n", bmsr));
237 #endif
238
239 mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
240 MII_OFFSET_ANY, 0);
241 if (LIST_FIRST(&mii->mii_phys) == NULL)
242 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_AUTO, 0, NULL);
243 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
244
245 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
246 RND_TYPE_NET, RND_FLAG_DEFAULT);
247 }
248
249 int
250 xi_detach(device_t self, int flags)
251 {
252 struct xi_softc *sc = device_private(self);
253 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
254
255 DPRINTF(XID_CONFIG, ("xi_detach()\n"));
256
257 xi_disable(sc);
258
259 rnd_detach_source(&sc->sc_rnd_source);
260
261 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
262 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
263 ether_ifdetach(ifp);
264 if_detach(ifp);
265
266 return 0;
267 }
268
269 int
270 xi_intr(void *arg)
271 {
272 struct xi_softc *sc = arg;
273 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
274 uint8_t esr, rsr, isr, rx_status;
275 uint16_t tx_status, recvcount = 0, tempint;
276
277 DPRINTF(XID_CONFIG, ("xi_intr()\n"));
278
279 if (sc->sc_enabled == 0 || !device_is_active(sc->sc_dev))
280 return (0);
281
282 ifp->if_timer = 0; /* turn watchdog timer off */
283
284 PAGE(sc, 0);
285 if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
286 /* Disable interrupt (Linux does it). */
287 bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, 0);
288 }
289
290 esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, ESR);
291 isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, ISR0);
292 rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, RSR);
293
294 /* Check to see if card has been ejected. */
295 if (isr == 0xff) {
296 #ifdef DIAGNOSTIC
297 printf("%s: interrupt for dead card\n",
298 device_xname(sc->sc_dev));
299 #endif
300 goto end;
301 }
302 DPRINTF(XID_INTR, ("xi: isr=%02x\n", isr));
303
304 PAGE(sc, 0x40);
305 rx_status =
306 bus_space_read_1(sc->sc_bst, sc->sc_bsh, RXST0);
307 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RXST0, ~rx_status & 0xff);
308 tx_status =
309 bus_space_read_1(sc->sc_bst, sc->sc_bsh, TXST0);
310 tx_status |=
311 bus_space_read_1(sc->sc_bst, sc->sc_bsh, TXST1) << 8;
312 bus_space_write_1(sc->sc_bst, sc->sc_bsh, TXST0, 0);
313 bus_space_write_1(sc->sc_bst, sc->sc_bsh, TXST1, 0);
314 DPRINTF(XID_INTR, ("xi: rx_status=%02x tx_status=%04x\n", rx_status,
315 tx_status));
316
317 PAGE(sc, 0);
318 while (esr & FULL_PKT_RCV) {
319 if (!(rsr & RSR_RX_OK))
320 break;
321
322 /* Compare bytes read this interrupt to hard maximum. */
323 if (recvcount > MAX_BYTES_INTR) {
324 DPRINTF(XID_INTR,
325 ("xi: too many bytes this interrupt\n"));
326 ifp->if_iqdrops++;
327 /* Drop packet. */
328 bus_space_write_2(sc->sc_bst, sc->sc_bsh, DO0,
329 DO_SKIP_RX_PKT);
330 }
331 tempint = xi_get(sc); /* XXX doesn't check the error! */
332 recvcount += tempint;
333 ifp->if_ibytes += tempint;
334 esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, ESR);
335 rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, RSR);
336 }
337
338 /* Packet too long? */
339 if (rsr & RSR_TOO_LONG) {
340 ifp->if_ierrors++;
341 DPRINTF(XID_INTR, ("xi: packet too long\n"));
342 }
343
344 /* CRC error? */
345 if (rsr & RSR_CRCERR) {
346 ifp->if_ierrors++;
347 DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
348 }
349
350 /* Alignment error? */
351 if (rsr & RSR_ALIGNERR) {
352 ifp->if_ierrors++;
353 DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
354 }
355
356 /* Check for rx overrun. */
357 if (rx_status & RX_OVERRUN) {
358 ifp->if_ierrors++;
359 bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, CLR_RX_OVERRUN);
360 DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
361 }
362
363 /* Try to start more packets transmitting. */
364 if_schedule_deferred_start(ifp);
365
366 /* Detected excessive collisions? */
367 if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
368 DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
369 bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, RESTART_TX);
370 ifp->if_oerrors++;
371 }
372
373 if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
374 ifp->if_oerrors++;
375
376 /* have handled the interrupt */
377 rnd_add_uint32(&sc->sc_rnd_source, tx_status);
378
379 end:
380 /* Reenable interrupts. */
381 PAGE(sc, 0);
382 bus_space_write_1(sc->sc_bst, sc->sc_bsh, CR, ENABLE_INT);
383
384 return (1);
385 }
386
387 /*
388 * Pull a packet from the card into an mbuf chain.
389 */
390 STATIC uint16_t
391 xi_get(struct xi_softc *sc)
392 {
393 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
394 struct mbuf *top, **mp, *m;
395 uint16_t pktlen, len, recvcount = 0;
396 uint8_t *data;
397
398 DPRINTF(XID_CONFIG, ("xi_get()\n"));
399
400 PAGE(sc, 0);
401 pktlen =
402 bus_space_read_2(sc->sc_bst, sc->sc_bsh, RBC0) & RBC_COUNT_MASK;
403
404 DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
405
406 if (pktlen == 0) {
407 /*
408 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
409 * when MPE is set. It is not known why.
410 */
411 return (0);
412 }
413
414 /* XXX should this be incremented now ? */
415 recvcount += pktlen;
416
417 MGETHDR(m, M_DONTWAIT, MT_DATA);
418 if (m == NULL)
419 return (recvcount);
420 m_set_rcvif(m, ifp);
421 m->m_pkthdr.len = pktlen;
422 len = MHLEN;
423 top = NULL;
424 mp = ⊤
425
426 while (pktlen > 0) {
427 if (top) {
428 MGET(m, M_DONTWAIT, MT_DATA);
429 if (m == NULL) {
430 m_freem(top);
431 return (recvcount);
432 }
433 len = MLEN;
434 }
435 if (pktlen >= MINCLSIZE) {
436 MCLGET(m, M_DONTWAIT);
437 if (!(m->m_flags & M_EXT)) {
438 m_freem(m);
439 m_freem(top);
440 return (recvcount);
441 }
442 len = MCLBYTES;
443 }
444 if (top == NULL) {
445 char *newdata = (char *)ALIGN(m->m_data +
446 sizeof(struct ether_header)) -
447 sizeof(struct ether_header);
448 len -= newdata - m->m_data;
449 m->m_data = newdata;
450 }
451 len = uimin(pktlen, len);
452 data = mtod(m, uint8_t *);
453 if (len > 1) {
454 len &= ~1;
455 bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh, EDP,
456 (uint16_t *)data, len>>1);
457 } else
458 *data = bus_space_read_1(sc->sc_bst, sc->sc_bsh, EDP);
459 m->m_len = len;
460 pktlen -= len;
461 *mp = m;
462 mp = &m->m_next;
463 }
464
465 /* Skip Rx packet. */
466 bus_space_write_2(sc->sc_bst, sc->sc_bsh, DO0, DO_SKIP_RX_PKT);
467
468 if (top == NULL)
469 return recvcount;
470
471 /* Trim the CRC off the end of the packet. */
472 m_adj(top, -ETHER_CRC_LEN);
473
474 if_percpuq_enqueue(ifp->if_percpuq, top);
475 return (recvcount);
476 }
477
478 /*
479 * Serial management for the MII.
480 * The DELAY's below stem from the fact that the maximum frequency
481 * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
482 * go much faster than that.
483 */
484
485 /* Let the MII serial management be idle for one period. */
486 static INLINE void xi_mdi_idle(struct xi_softc *);
487 static INLINE void
488 xi_mdi_idle(struct xi_softc *sc)
489 {
490 bus_space_tag_t bst = sc->sc_bst;
491 bus_space_handle_t bsh = sc->sc_bsh;
492
493 /* Drive MDC low... */
494 bus_space_write_1(bst, bsh, GP2, MDC_LOW);
495 DELAY(1);
496
497 /* and high again. */
498 bus_space_write_1(bst, bsh, GP2, MDC_HIGH);
499 DELAY(1);
500 }
501
502 /* Pulse out one bit of data. */
503 static INLINE void xi_mdi_pulse(struct xi_softc *, int);
504 static INLINE void
505 xi_mdi_pulse(struct xi_softc *sc, int data)
506 {
507 bus_space_tag_t bst = sc->sc_bst;
508 bus_space_handle_t bsh = sc->sc_bsh;
509 uint8_t bit = data ? MDIO_HIGH : MDIO_LOW;
510
511 /* First latch the data bit MDIO with clock bit MDC low...*/
512 bus_space_write_1(bst, bsh, GP2, bit | MDC_LOW);
513 DELAY(1);
514
515 /* then raise the clock again, preserving the data bit. */
516 bus_space_write_1(bst, bsh, GP2, bit | MDC_HIGH);
517 DELAY(1);
518 }
519
520 /* Probe one bit of data. */
521 static INLINE int xi_mdi_probe(struct xi_softc *sc);
522 static INLINE int
523 xi_mdi_probe(struct xi_softc *sc)
524 {
525 bus_space_tag_t bst = sc->sc_bst;
526 bus_space_handle_t bsh = sc->sc_bsh;
527 uint8_t x;
528
529 /* Pull clock bit MDCK low... */
530 bus_space_write_1(bst, bsh, GP2, MDC_LOW);
531 DELAY(1);
532
533 /* Read data and drive clock high again. */
534 x = bus_space_read_1(bst, bsh, GP2);
535 bus_space_write_1(bst, bsh, GP2, MDC_HIGH);
536 DELAY(1);
537
538 return (x & MDIO);
539 }
540
541 /* Pulse out a sequence of data bits. */
542 static INLINE void xi_mdi_pulse_bits(struct xi_softc *, uint32_t, int);
543 static INLINE void
544 xi_mdi_pulse_bits(struct xi_softc *sc, uint32_t data, int len)
545 {
546 uint32_t mask;
547
548 for (mask = 1 << (len - 1); mask; mask >>= 1)
549 xi_mdi_pulse(sc, data & mask);
550 }
551
552 /* Read a PHY register. */
553 STATIC int
554 xi_mdi_read(device_t self, int phy, int reg, uint16_t *val)
555 {
556 struct xi_softc *sc = device_private(self);
557 int i;
558 uint32_t mask;
559 uint16_t data = 0;
560
561 PAGE(sc, 2);
562 for (i = 0; i < 32; i++) /* Synchronize. */
563 xi_mdi_pulse(sc, 1);
564 xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
565 xi_mdi_pulse_bits(sc, phy, 5); /* PHY address */
566 xi_mdi_pulse_bits(sc, reg, 5); /* PHY register */
567 xi_mdi_idle(sc); /* Turn around. */
568 xi_mdi_probe(sc); /* Drop initial zero bit. */
569
570 for (mask = 1 << 15; mask; mask >>= 1) {
571 if (xi_mdi_probe(sc))
572 data |= mask;
573 }
574 xi_mdi_idle(sc);
575
576 DPRINTF(XID_MII,
577 ("xi_mdi_read: phy %d reg %d -> %04hx\n", phy, reg, data));
578
579 *val = data;
580 return 0;
581 }
582
583 /* Write a PHY register. */
584 STATIC int
585 xi_mdi_write(device_t self, int phy, int reg, uint16_t val)
586 {
587 struct xi_softc *sc = device_private(self);
588 int i;
589
590 PAGE(sc, 2);
591 for (i = 0; i < 32; i++) /* Synchronize. */
592 xi_mdi_pulse(sc, 1);
593 xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
594 xi_mdi_pulse_bits(sc, phy, 5); /* PHY address */
595 xi_mdi_pulse_bits(sc, reg, 5); /* PHY register */
596 xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
597 xi_mdi_pulse_bits(sc, val, 16); /* Write the data */
598 xi_mdi_idle(sc); /* Idle away. */
599
600 DPRINTF(XID_MII,
601 ("xi_mdi_write: phy %d reg %d val %04hx\n", phy, reg, val));
602
603 return 0;
604 }
605
606 STATIC void
607 xi_statchg(struct ifnet *ifp)
608 {
609 /* XXX Update ifp->if_baudrate */
610 }
611
612 /*
613 * Change media according to request.
614 */
615 STATIC int
616 xi_mediachange(struct ifnet *ifp)
617 {
618 int s;
619
620 DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
621
622 if (ifp->if_flags & IFF_UP) {
623 s = splnet();
624 xi_init(ifp->if_softc);
625 splx(s);
626 }
627 return (0);
628 }
629
630 STATIC void
631 xi_reset(struct xi_softc *sc)
632 {
633 int s;
634
635 DPRINTF(XID_CONFIG, ("xi_reset()\n"));
636
637 s = splnet();
638 xi_stop(sc);
639 xi_init(sc);
640 splx(s);
641 }
642
643 STATIC void
644 xi_watchdog(struct ifnet *ifp)
645 {
646 struct xi_softc *sc = ifp->if_softc;
647
648 printf("%s: device timeout\n", device_xname(sc->sc_dev));
649 ++ifp->if_oerrors;
650
651 xi_reset(sc);
652 }
653
654 STATIC void
655 xi_stop(register struct xi_softc *sc)
656 {
657 bus_space_tag_t bst = sc->sc_bst;
658 bus_space_handle_t bsh = sc->sc_bsh;
659
660 DPRINTF(XID_CONFIG, ("xi_stop()\n"));
661
662 PAGE(sc, 0x40);
663 bus_space_write_1(bst, bsh, CMD0, DISABLE_RX);
664
665 /* Disable interrupts. */
666 PAGE(sc, 0);
667 bus_space_write_1(bst, bsh, CR, 0);
668
669 PAGE(sc, 1);
670 bus_space_write_1(bst, bsh, IMR0, 0);
671
672 /* Cancel watchdog timer. */
673 sc->sc_ethercom.ec_if.if_timer = 0;
674 }
675
676 STATIC int
677 xi_enable(struct xi_softc *sc)
678 {
679 int error;
680
681 if (!sc->sc_enabled) {
682 error = (*sc->sc_enable)(sc);
683 if (error)
684 return (error);
685 sc->sc_enabled = 1;
686 xi_full_reset(sc);
687 }
688 return (0);
689 }
690
691 STATIC void
692 xi_disable(struct xi_softc *sc)
693 {
694
695 if (sc->sc_enabled) {
696 sc->sc_enabled = 0;
697 (*sc->sc_disable)(sc);
698 }
699 }
700
701 STATIC void
702 xi_init(struct xi_softc *sc)
703 {
704 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
705 bus_space_tag_t bst = sc->sc_bst;
706 bus_space_handle_t bsh = sc->sc_bsh;
707
708 DPRINTF(XID_CONFIG, ("xi_init()\n"));
709
710 /* Setup the ethernet interrupt mask. */
711 PAGE(sc, 1);
712 bus_space_write_1(bst, bsh, IMR0,
713 ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
714 ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
715 if (sc->sc_chipset < XI_CHIPSET_DINGO) {
716 /* XXX What is this? Not for Dingo at least. */
717 /* Unmask TX underrun detection */
718 bus_space_write_1(bst, bsh, IMR1, 1);
719 }
720
721 /* Enable interrupts. */
722 PAGE(sc, 0);
723 bus_space_write_1(bst, bsh, CR, ENABLE_INT);
724
725 xi_set_address(sc);
726
727 PAGE(sc, 0x40);
728 bus_space_write_1(bst, bsh, CMD0, ENABLE_RX | ONLINE);
729
730 PAGE(sc, 0);
731
732 /* Set current media. */
733 mii_mediachg(&sc->sc_mii);
734
735 ifp->if_flags |= IFF_RUNNING;
736 ifp->if_flags &= ~IFF_OACTIVE;
737
738 xi_start(ifp);
739 }
740
741 /*
742 * Start outputting on the interface.
743 * Always called as splnet().
744 */
745 STATIC void
746 xi_start(struct ifnet *ifp)
747 {
748 struct xi_softc *sc = ifp->if_softc;
749 bus_space_tag_t bst = sc->sc_bst;
750 bus_space_handle_t bsh = sc->sc_bsh;
751 unsigned int s, len, pad = 0;
752 struct mbuf *m0, *m;
753 uint16_t space;
754
755 DPRINTF(XID_CONFIG, ("xi_start()\n"));
756
757 /* Don't transmit if interface is busy or not running. */
758 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
759 DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
760 return;
761 }
762
763 /* Peek at the next packet. */
764 IFQ_POLL(&ifp->if_snd, m0);
765 if (m0 == 0)
766 return;
767
768 /* We need to use m->m_pkthdr.len, so require the header. */
769 if (!(m0->m_flags & M_PKTHDR))
770 panic("xi_start: no header mbuf");
771
772 len = m0->m_pkthdr.len;
773
774 #if 1
775 /* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
776 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
777 pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
778 #else
779 pad = 0;
780 #endif
781
782 PAGE(sc, 0);
783
784 bus_space_write_2(bst, bsh, TRS, (uint16_t)len + pad + 2);
785 space = bus_space_read_2(bst, bsh, TSO) & 0x7fff;
786 if (len + pad + 2 > space) {
787 DPRINTF(XID_FIFO,
788 ("xi: not enough space in output FIFO (%d > %d)\n",
789 len + pad + 2, space));
790 return;
791 }
792
793 IFQ_DEQUEUE(&ifp->if_snd, m0);
794
795 bpf_mtap(ifp, m0, BPF_D_OUT);
796
797 /*
798 * Do the output at splhigh() so that an interrupt from another device
799 * won't cause a FIFO underrun.
800 */
801 s = splhigh();
802
803 bus_space_write_2(bst, bsh, EDP, (uint16_t)len + pad);
804 for (m = m0; m; ) {
805 if (m->m_len > 1)
806 bus_space_write_multi_2(bst, bsh, EDP,
807 mtod(m, uint16_t *), m->m_len>>1);
808 if (m->m_len & 1) {
809 DPRINTF(XID_CONFIG, ("xi: XXX odd!\n"));
810 bus_space_write_1(bst, bsh, EDP,
811 *(mtod(m, uint8_t *) + m->m_len - 1));
812 }
813 m = m0 = m_free(m);
814 }
815 DPRINTF(XID_CONFIG, ("xi: len=%d pad=%d total=%d\n", len, pad, len+pad+4));
816 if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
817 bus_space_write_1(bst, bsh, CR, TX_PKT | ENABLE_INT);
818 else {
819 for (; pad > 1; pad -= 2)
820 bus_space_write_2(bst, bsh, EDP, 0);
821 if (pad == 1)
822 bus_space_write_1(bst, bsh, EDP, 0);
823 }
824
825 splx(s);
826
827 ifp->if_timer = 5;
828 ++ifp->if_opackets;
829 }
830
831 STATIC int
832 xi_ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
833 {
834 struct ifaddr *ifa = (struct ifaddr *)data;
835 struct xi_softc *sc = ifp->if_softc;
836 int error;
837
838 DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
839
840 switch (cmd) {
841 case SIOCINITIFADDR:
842 if ((error = xi_enable(sc)) != 0)
843 break;
844
845 ifp->if_flags |= IFF_UP;
846
847 xi_init(sc);
848 switch (ifa->ifa_addr->sa_family) {
849 #ifdef INET
850 case AF_INET:
851 arp_ifinit(ifp, ifa);
852 break;
853 #endif /* INET */
854
855
856 default:
857 break;
858 }
859 break;
860
861 default:
862 return (EINVAL);
863 }
864
865 return (0);
866 }
867
868 STATIC int
869 xi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
870 {
871 struct xi_softc *sc = ifp->if_softc;
872 int s, error = 0;
873
874 DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
875
876 s = splnet();
877
878 switch (cmd) {
879 case SIOCINITIFADDR:
880 error = xi_ether_ioctl(ifp, cmd, data);
881 break;
882
883 case SIOCSIFFLAGS:
884 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
885 break;
886 /* XXX re-use ether_ioctl() */
887 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
888 case IFF_RUNNING:
889 /*
890 * If interface is marked down and it is running,
891 * stop it.
892 */
893 xi_stop(sc);
894 ifp->if_flags &= ~IFF_RUNNING;
895 xi_disable(sc);
896 break;
897 case IFF_UP:
898 /*
899 * If interface is marked up and it is stopped,
900 * start it.
901 */
902 if ((error = xi_enable(sc)) != 0)
903 break;
904 xi_init(sc);
905 break;
906 case IFF_UP | IFF_RUNNING:
907 /*
908 * Reset the interface to pick up changes in any
909 * other flags that affect hardware registers.
910 */
911 xi_set_address(sc);
912 break;
913 case 0:
914 break;
915 }
916 break;
917
918 case SIOCADDMULTI:
919 case SIOCDELMULTI:
920 if (sc->sc_enabled == 0) {
921 error = EIO;
922 break;
923 }
924 /*FALLTHROUGH*/
925 default:
926 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
927 /*
928 * Multicast list has changed; set the hardware
929 * filter accordingly.
930 */
931 if (ifp->if_flags & IFF_RUNNING)
932 xi_set_address(sc);
933 error = 0;
934 }
935 break;
936 }
937
938 splx(s);
939 return (error);
940 }
941
942 STATIC void
943 xi_set_address(struct xi_softc *sc)
944 {
945 bus_space_tag_t bst = sc->sc_bst;
946 bus_space_handle_t bsh = sc->sc_bsh;
947 struct ethercom *ec = &sc->sc_ethercom;
948 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
949 struct ether_multistep step;
950 struct ether_multi *enm;
951 int page, num;
952 int i;
953 uint8_t x;
954 const uint8_t *enaddr;
955 uint8_t indaddr[64];
956
957 DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
958
959 enaddr = (const uint8_t *)CLLADDR(ifp->if_sadl);
960 if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
961 for (i = 0; i < 6; i++)
962 indaddr[i] = enaddr[5 - i];
963 else
964 for (i = 0; i < 6; i++)
965 indaddr[i] = enaddr[i];
966 num = 1;
967
968 if (ec->ec_multicnt > 9) {
969 ifp->if_flags |= IFF_ALLMULTI;
970 goto done;
971 }
972
973 ETHER_LOCK(ec);
974 ETHER_FIRST_MULTI(step, ec, enm);
975 for (; enm; num++) {
976 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
977 sizeof(enm->enm_addrlo)) != 0) {
978 /*
979 * The multicast address is really a range;
980 * it's easier just to accept all multicasts.
981 * XXX should we be setting IFF_ALLMULTI here?
982 */
983 ifp->if_flags |= IFF_ALLMULTI;
984 ETHER_UNLOCK(ec);
985 goto done;
986 }
987 if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
988 for (i = 0; i < 6; i++)
989 indaddr[num * 6 + i] = enm->enm_addrlo[5 - i];
990 else
991 for (i = 0; i < 6; i++)
992 indaddr[num * 6 + i] = enm->enm_addrlo[i];
993 ETHER_NEXT_MULTI(step, enm);
994 }
995 ETHER_UNLOCK(ec);
996 ifp->if_flags &= ~IFF_ALLMULTI;
997
998 done:
999 if (num < 10)
1000 memset(&indaddr[num * 6], 0xff, 6 * (10 - num));
1001
1002 for (page = 0; page < 8; page++) {
1003 #ifdef XIDEBUG
1004 if (xidebug & XID_MCAST) {
1005 printf("page %d before:", page);
1006 for (i = 0; i < 8; i++)
1007 printf(" %02x", indaddr[page * 8 + i]);
1008 printf("\n");
1009 }
1010 #endif
1011
1012 PAGE(sc, 0x50 + page);
1013 bus_space_write_region_1(bst, bsh, IA, &indaddr[page * 8],
1014 page == 7 ? 4 : 8);
1015 /*
1016 * XXX
1017 * Without this delay, the address registers on my CE2 get
1018 * trashed the first and I have to cycle it. I have no idea
1019 * why. - mycroft, 2004/08/09
1020 */
1021 DELAY(50);
1022
1023 #ifdef XIDEBUG
1024 if (xidebug & XID_MCAST) {
1025 bus_space_read_region_1(bst, bsh, IA,
1026 &indaddr[page * 8], page == 7 ? 4 : 8);
1027 printf("page %d after: ", page);
1028 for (i = 0; i < 8; i++)
1029 printf(" %02x", indaddr[page * 8 + i]);
1030 printf("\n");
1031 }
1032 #endif
1033 }
1034
1035 PAGE(sc, 0x42);
1036 x = SWC1_IND_ADDR;
1037 if (ifp->if_flags & IFF_PROMISC)
1038 x |= SWC1_PROMISC;
1039 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))
1040 x |= SWC1_MCAST_PROM;
1041 if (!LIST_FIRST(&sc->sc_mii.mii_phys))
1042 x |= SWC1_AUTO_MEDIA;
1043 bus_space_write_1(sc->sc_bst, sc->sc_bsh, SWC1, x);
1044 }
1045
1046 STATIC void
1047 xi_cycle_power(struct xi_softc *sc)
1048 {
1049 bus_space_tag_t bst = sc->sc_bst;
1050 bus_space_handle_t bsh = sc->sc_bsh;
1051
1052 DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
1053
1054 PAGE(sc, 4);
1055 DELAY(1);
1056 bus_space_write_1(bst, bsh, GP1, 0);
1057 tsleep(&xi_cycle_power, PWAIT, "xipwr1", hz * 40 / 1000);
1058 if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
1059 bus_space_write_1(bst, bsh, GP1, POWER_UP);
1060 else
1061 /* XXX What is bit 2 (aka AIC)? */
1062 bus_space_write_1(bst, bsh, GP1, POWER_UP | 4);
1063 tsleep(&xi_cycle_power, PWAIT, "xipwr2", hz * 20 / 1000);
1064 }
1065
1066 STATIC void
1067 xi_full_reset(struct xi_softc *sc)
1068 {
1069 bus_space_tag_t bst = sc->sc_bst;
1070 bus_space_handle_t bsh = sc->sc_bsh;
1071 uint8_t x;
1072
1073 DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
1074
1075 /* Do an as extensive reset as possible on all functions. */
1076 xi_cycle_power(sc);
1077 bus_space_write_1(bst, bsh, CR, SOFT_RESET);
1078 tsleep(&xi_full_reset, PWAIT, "xirst1", hz * 20 / 1000);
1079 bus_space_write_1(bst, bsh, CR, 0);
1080 tsleep(&xi_full_reset, PWAIT, "xirst2", hz * 20 / 1000);
1081 PAGE(sc, 4);
1082 if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
1083 /*
1084 * Drive GP1 low to power up ML6692 and GP2 high to power up
1085 * the 10MHz chip. XXX What chip is that? The phy?
1086 */
1087 bus_space_write_1(bst, bsh, GP0, GP1_OUT | GP2_OUT | GP2_WR);
1088 }
1089 tsleep(&xi_full_reset, PWAIT, "xirst3", hz * 500 / 1000);
1090
1091 /* Get revision information. XXX Symbolic constants. */
1092 sc->sc_rev = bus_space_read_1(bst, bsh, BV) &
1093 ((sc->sc_chipset >= XI_CHIPSET_MOHAWK) ? 0x70 : 0x30) >> 4;
1094 DPRINTF(XID_CONFIG, ("xi: rev=%02x\n", sc->sc_rev));
1095
1096 /* Media selection. XXX Maybe manual overriding too? */
1097 if (sc->sc_chipset < XI_CHIPSET_MOHAWK) {
1098 /*
1099 * XXX I have no idea what this really does, it is from the
1100 * Linux driver.
1101 */
1102 bus_space_write_1(bst, bsh, GP0, GP1_OUT);
1103 }
1104 tsleep(&xi_full_reset, PWAIT, "xirst4", hz * 40 / 1000);
1105
1106 /*
1107 * Disable source insertion.
1108 * XXX Dingo does not have this bit, but Linux does it unconditionally.
1109 */
1110 if (sc->sc_chipset < XI_CHIPSET_DINGO) {
1111 PAGE(sc, 0x42);
1112 bus_space_write_1(bst, bsh, SWC0, 0x20);
1113 }
1114
1115 /* Set the local memory dividing line. */
1116 if (sc->sc_rev != 1) {
1117 PAGE(sc, 2);
1118 /* XXX Symbolic constant preferrable. */
1119 bus_space_write_2(bst, bsh, RBS0, 0x2000);
1120 }
1121
1122 /*
1123 * Apparently the receive byte pointer can be bad after a reset, so
1124 * we hardwire it correctly.
1125 */
1126 PAGE(sc, 0);
1127 bus_space_write_2(bst, bsh, DO0, DO_CHG_OFFSET);
1128
1129 /* Setup ethernet MAC registers. XXX Symbolic constants. */
1130 PAGE(sc, 0x40);
1131 bus_space_write_1(bst, bsh, RX0MSK,
1132 PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
1133 bus_space_write_1(bst, bsh, TX0MSK,
1134 CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
1135 SQE | TX_ABORT | TX_OK);
1136 if (sc->sc_chipset < XI_CHIPSET_DINGO)
1137 /* XXX From Linux, dunno what 0xb0 means. */
1138 bus_space_write_1(bst, bsh, TX1MSK, 0xb0);
1139 bus_space_write_1(bst, bsh, RXST0, 0);
1140 bus_space_write_1(bst, bsh, TXST0, 0);
1141 bus_space_write_1(bst, bsh, TXST1, 0);
1142
1143 PAGE(sc, 2);
1144
1145 /* Enable MII function if available. */
1146 x = 0;
1147 if (LIST_FIRST(&sc->sc_mii.mii_phys))
1148 x |= SELECT_MII;
1149 bus_space_write_1(bst, bsh, MSR, x);
1150 tsleep(&xi_full_reset, PWAIT, "xirst5", hz * 20 / 1000);
1151
1152 /* Configure the LED registers. */
1153 /* XXX This is not good for 10base2. */
1154 bus_space_write_1(bst, bsh, LED,
1155 (LED_TX_ACT << LED1_SHIFT) | (LED_10MB_LINK << LED0_SHIFT));
1156 if (sc->sc_chipset >= XI_CHIPSET_DINGO)
1157 bus_space_write_1(bst, bsh, LED3, LED_100MB_LINK << LED3_SHIFT);
1158
1159 /*
1160 * The Linux driver says this:
1161 * We should switch back to page 0 to avoid a bug in revision 0
1162 * where regs with offset below 8 can't be read after an access
1163 * to the MAC registers.
1164 */
1165 PAGE(sc, 0);
1166 }
1167