2 * Copyright (c) 1998, 1999 Scott Mitchell
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $Id: if_xe.c,v 1.20 1999/06/13 19:17:40 scott Exp $
27 * $FreeBSD: src/sys/dev/xe/if_xe.c,v 1.13.2.6 2003/02/05 22:03:57 mbr Exp $
28 * $DragonFly: src/sys/dev/netif/xe/if_xe.c,v 1.24 2005/07/13 17:10:25 joerg Exp $
34 * I've pushed this fairly far, but there are some things that need to be
35 * done here. I'm documenting them here in case I get destracted. -- imp
37 * xe_cem56fix -- need to figure out how to map the extra stuff.
41 * Portions of this software were derived from Werner Koch's xirc2ps driver
42 * for Linux under the terms of the following license (from v1.30 of the
45 * Copyright (c) 1997 by Werner Koch (dd9jn)
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, and the entire permission notice in its entirety,
52 * including the disclaimer of warranties.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. The name of the author may not be used to endorse or promote
57 * products derived from this software without specific prior
60 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
68 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
70 * OF THE POSSIBILITY OF SUCH DAMAGE.
74 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
75 * following cards are currently known to work with the driver:
76 * Xircom CreditCard 10/100 (CE3)
77 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
78 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
79 * Xircom RealPort Ethernet 10
80 * Xircom RealPort Ethernet 10/100
81 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
82 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
83 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
85 * Some other cards *should* work, but support for them is either broken or in
86 * an unknown state at the moment. I'm always interested in hearing from
87 * people who own any of these cards:
88 * Xircom CreditCard 10Base-T (PS-CE2-10)
89 * Xircom CreditCard Ethernet + ModemII (CEM2)
90 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
92 * Thanks to all who assisted with the development and testing of the driver,
93 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
94 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
95 * out anyone who deserves a mention here.
97 * Special thanks to Ade Lovett for both hosting the mailing list and doing
98 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
103 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
105 * Mailing list: http://www.lovett.com/lists/freebsd-xircom/
106 * or send "subscribe freebsd-xircom" to <majordomo@lovett.com>
108 * Author email: <scott@uk.freebsd.org>
112 #include <sys/param.h>
113 #include <sys/cdefs.h>
114 #include <sys/errno.h>
115 #include <sys/kernel.h>
116 #include <sys/mbuf.h>
117 #include <sys/select.h>
118 #include <sys/socket.h>
119 #include <sys/sockio.h>
120 #include <sys/systm.h>
122 #include <sys/thread2.h>
124 #include <sys/module.h>
127 #include <machine/bus.h>
128 #include <machine/resource.h>
129 #include <sys/rman.h>
131 #include <net/ethernet.h>
133 #include <net/ifq_var.h>
134 #include <net/if_arp.h>
135 #include <net/if_dl.h>
136 #include <net/if_media.h>
137 #include <net/if_mib.h>
140 #include <bus/pccard/pccardvar.h>
143 #include "if_xereg.h"
144 #include "if_xevar.h"
147 * MII command structure
149 struct xe_mii_frame {
150 u_int8_t mii_stdelim;
152 u_int8_t mii_phyaddr;
153 u_int8_t mii_regaddr;
154 u_int8_t mii_turnaround;
159 * Media autonegotiation progress constants
161 #define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
162 #define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
163 #define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
164 #define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
165 #define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
169 * Prototypes start here
171 static int xe_probe (device_t dev);
172 static int xe_attach (device_t dev);
173 static int xe_detach (device_t dev);
174 static int xe_activate (device_t dev);
175 static void xe_deactivate (device_t dev);
176 static void xe_init (void *xscp);
177 static void xe_start (struct ifnet *ifp);
178 static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *);
179 static void xe_watchdog (struct ifnet *ifp);
180 static int xe_media_change (struct ifnet *ifp);
181 static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
182 static timeout_t xe_setmedia;
183 static void xe_hard_reset (struct xe_softc *scp);
184 static void xe_soft_reset (struct xe_softc *scp);
185 static void xe_stop (struct xe_softc *scp);
186 static void xe_enable_intr (struct xe_softc *scp);
187 static void xe_disable_intr (struct xe_softc *scp);
188 static void xe_setmulti (struct xe_softc *scp);
189 static void xe_setaddrs (struct xe_softc *scp);
190 static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
192 static u_int32_t xe_compute_crc (u_int8_t *data, int len);
193 static int xe_compute_hashbit (u_int32_t crc);
199 static void xe_mii_sync (struct xe_softc *scp);
200 static int xe_mii_init (struct xe_softc *scp);
201 static void xe_mii_send (struct xe_softc *scp, u_int32_t bits, int cnt);
202 static int xe_mii_readreg (struct xe_softc *scp, struct xe_mii_frame *frame);
203 static int xe_mii_writereg (struct xe_softc *scp, struct xe_mii_frame *frame);
204 static u_int16_t xe_phy_readreg (struct xe_softc *scp, u_int16_t reg);
205 static void xe_phy_writereg (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
208 * Debug functions -- uncomment for VERY verbose dignostic information.
209 * Set to 1 for less verbose information
211 /* #define XE_DEBUG 2 */
213 #define XE_REG_DUMP(scp) xe_reg_dump((scp))
214 #define XE_MII_DUMP(scp) xe_mii_dump((scp))
215 static void xe_reg_dump (struct xe_softc *scp);
216 static void xe_mii_dump (struct xe_softc *scp);
218 #define XE_REG_DUMP(scp)
219 #define XE_MII_DUMP(scp)
223 * Fixing for RealPort cards - they need a little furtling to get the
227 xe_cem56fix(device_t dev)
229 struct xe_softc *sc = (struct xe_softc *) device_get_softc(dev);
231 bus_space_handle_t bsh;
237 device_printf(dev, "Hacking your Realport, master\n");
241 device_printf(dev, "Realport port 0x%0lx, size 0x%0lx\n",
242 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
243 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid));
247 r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 4 << 10, RF_ACTIVE);
250 device_printf(dev, "Can't map in attribute memory\n");
255 bsh = rman_get_bushandle(r);
256 bst = rman_get_bustag(r);
258 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
261 bus_space_write_1(bst, bsh, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL |
262 DINGO_ECOR_INT_ENABLE |
263 DINGO_ECOR_IOB_ENABLE |
264 DINGO_ECOR_ETH_ENABLE);
265 ioport = bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid);
266 bus_space_write_1(bst, bsh, DINGO_EBAR0, ioport & 0xff);
267 bus_space_write_1(bst, bsh, DINGO_EBAR1, (ioport >> 8) & 0xff);
269 bus_space_write_1(bst, bsh, DINGO_DCOR0, DINGO_DCOR0_SF_INT);
270 bus_space_write_1(bst, bsh, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL |
272 bus_space_write_1(bst, bsh, DINGO_DCOR2, 0x00);
273 bus_space_write_1(bst, bsh, DINGO_DCOR3, 0x00);
274 bus_space_write_1(bst, bsh, DINGO_DCOR4, 0x00);
276 bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
283 * PCMCIA probe routine.
284 * Probe and identify the device. Called by the slot manager when the card is
285 * inserted or the machine wakes up from suspend mode. Assmes that the slot
286 * structure has been initialised already.
289 xe_probe(device_t dev)
291 struct xe_softc *scp = (struct xe_softc *) device_get_softc(dev);
293 bus_space_handle_t bsh;
295 u_char ver_str[CISTPL_BUFSIZE>>1];
304 device_printf(dev, "Probing\n");
309 r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 4 << 10, RF_ACTIVE);
312 device_printf(dev, "Can't map in cis\n");
316 bsh = rman_get_bushandle(r);
317 bst = rman_get_bustag(r);
320 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
323 /* Grep through CIS looking for relevant tuples */
328 u_int8_t rev, media, prod;
330 switch (CISTPL_TYPE(buf)) {
332 case 0x15: /* Grab version string (needed to ID some weird CE2's) */
334 device_printf(dev, "Got version string (0x15)\n");
336 for (i = 0; i < CISTPL_LEN(buf); ver_str[i] = CISTPL_DATA(buf, i++));
338 ver_str[(CISTPL_BUFSIZE>>1) - 1] = CISTPL_LEN(buf);
342 case 0x20: /* Figure out what type of card we have */
344 device_printf(dev, "Got card ID (0x20)\n");
346 vendor = CISTPL_DATA(buf, 0) + (CISTPL_DATA(buf, 1) << 8);
347 rev = CISTPL_DATA(buf, 2);
348 media = CISTPL_DATA(buf, 3);
349 prod = CISTPL_DATA(buf, 4);
351 switch (vendor) { /* Get vendor ID */
353 scp->vendor = "Xircom"; break;
356 scp->vendor = "Compaq"; break;
358 scp->vendor = "Intel"; break;
360 scp->vendor = "Unknown";
363 if (!((prod & 0x40) && (media & 0x01))) {
365 device_printf(dev, "Not a PCMCIA Ethernet card!\n");
367 rc = ENODEV; /* Not a PCMCIA Ethernet device */
369 if (media & 0x10) { /* Ethernet/modem cards */
371 device_printf(dev, "Card is Ethernet/modem combo\n");
374 switch (prod & 0x0f) {
376 scp->card_type = "CEM"; break;
379 scp->card_type = "CEM2"; break;
382 scp->card_type = "CEM3"; break;
385 scp->card_type = "CEM33"; break;
388 scp->card_type = "CEM56M"; break;
390 case 7: /* Some kind of RealPort card */
393 scp->card_type = "CEM56"; break;
397 } else { /* Ethernet-only cards */
399 device_printf(dev, "Card is Ethernet only\n");
401 switch (prod & 0x0f) {
403 scp->card_type = "CE"; break;
406 scp->card_type = "CE2"; break;
409 scp->card_type = "CE3"; break;
418 case 0x22: /* Get MAC address */
419 if ((CISTPL_LEN(buf) == 8) &&
420 (CISTPL_DATA(buf, 0) == 0x04) &&
421 (CISTPL_DATA(buf, 1) == ETHER_ADDR_LEN)) {
423 device_printf(dev, "Got MAC address (0x22)\n");
425 for (i = 0; i < ETHER_ADDR_LEN; i++)
426 scp->arpcom.ac_enaddr[i] = CISTPL_DATA(buf, i+2);
434 if (CISTPL_TYPE(buf) == 0xff)
436 /* Skip to next tuple */
437 buf += ((CISTPL_LEN(buf) + 2) << 1);
442 bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
447 /* Die now if something went wrong above */
451 /* Check for certain strange CE2's that look like CE's */
452 if (strcmp(scp->card_type, "CE") == 0) {
453 u_char *str = ver_str;
455 device_printf(dev, "Checking for weird CE2 string\n");
457 str += strlen(str) + 1; /* Skip forward to 3rd version string */
458 str += strlen(str) + 1;
459 str += strlen(str) + 1;
460 for (i = 0; i < strlen(str) - 2; i++) {
461 if (bcmp(&str[i], "CE2", 3) ==0) { /* Look for "CE2" string */
462 scp->card_type = "CE2";
467 /* Reject unsupported cards */
468 if (strcmp(scp->card_type, "CE") == 0 || strcmp(scp->card_type, "CEM") == 0) {
469 device_printf(dev, "Sorry, your %s card is not supported :(\n",
479 * The device entry is being removed, probably because someone ejected the
480 * card. The interface should have been brought down manually before calling
481 * this function; if not you may well lose packets. In any case, I shut down
482 * the card and the interface, and hope for the best.
485 xe_detach(device_t dev) {
486 struct xe_softc *sc = device_get_softc(dev);
488 sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
489 callout_stop(&sc->xe_timer);
490 ether_ifdetach(&sc->arpcom.ac_if);
499 xe_attach (device_t dev) {
500 struct xe_softc *scp = device_get_softc(dev);
504 device_printf(dev, "attach\n");
507 if ((err = xe_activate(dev)) != 0)
510 /* Fill in some private data */
511 scp->ifp = &scp->arpcom.ac_if;
512 scp->ifm = &scp->ifmedia;
513 scp->autoneg_status = 0;
515 /* Hack RealPorts into submission */
516 if (scp->dingo && xe_cem56fix(dev) < 0) {
517 device_printf(dev, "Unable to fix your RealPort\n");
522 /* Hopefully safe to read this here */
524 scp->version = XE_INB(XE_BOV);
526 /* Initialise the ifnet structure */
527 scp->ifp->if_softc = scp;
528 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
529 scp->ifp->if_timer = 0;
530 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
531 scp->ifp->if_linkmib = &scp->mibdata;
532 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
533 scp->ifp->if_start = xe_start;
534 scp->ifp->if_ioctl = xe_ioctl;
535 scp->ifp->if_watchdog = xe_watchdog;
536 scp->ifp->if_init = xe_init;
537 ifq_set_maxlen(&scp->ifp->if_snd, IFQ_MAXLEN);
538 ifq_set_ready(&scp->ifp->if_snd);
540 /* Initialise the ifmedia structure */
541 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
542 callout_init(&scp->xe_timer);
545 * Fill in supported media types. Some cards _do_ support full duplex
546 * operation, but this driver doesn't, yet. Therefore we leave those modes
547 * out of the list. We support some form of autoselection in all cases.
550 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
551 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
554 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
555 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
557 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
559 /* Default is to autoselect best supported media type */
560 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
562 /* Print some useful information */
563 device_printf(dev, "%s %s, bonding version %#x%s%s\n",
567 scp->mohawk ? ", 100Mbps capable" : "",
568 scp->modem ? ", with modem" : "");
570 XE_SELECT_PAGE(0x10);
571 device_printf(dev, "DingoID = %#x, RevisionID = %#x, VendorID = %#x\n",
574 XE_INW(XE_VendorID));
577 XE_SELECT_PAGE(0x45);
578 device_printf(dev, "CE2 version = %#x\n", XE_INB(XE_REV));
581 /* Attach the interface */
582 ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr);
590 * Initialize device. Completes the reset procedure on the card and starts
591 * output. If there's an autonegotiation in progress we DON'T do anything;
592 * the media selection code will call us again when it's done.
595 xe_init(void *xscp) {
596 struct xe_softc *scp = xscp;
599 if_printf(scp->ifp, "init\n");
602 /* Reset transmitter flags */
605 scp->tx_collisions = 0;
606 scp->ifp->if_timer = 0;
610 XE_SELECT_PAGE(0x42);
611 XE_OUTB(XE_SWC0, 0x20); /* Disable source insertion (WTF is that?) */
614 * Set the 'local memory dividing line' -- splits the 32K card memory into
615 * 8K for transmit buffers and 24K for receive. This is done automatically
616 * on newer revision cards.
618 if (scp->srev != 1) {
620 XE_OUTW(XE_RBS, 0x2000);
623 /* Set up multicast addresses */
626 /* Fix the data offset register -- reset leaves it off-by-one */
628 XE_OUTW(XE_DO, 0x2000);
631 * Set MAC interrupt masks and clear status regs. The bit names are direct
632 * from the Linux code; I have no idea what most of them do.
634 XE_SELECT_PAGE(0x40); /* Bit 7..0 */
635 XE_OUTB(XE_RX0Msk, 0xff); /* ROK, RAB, rsv, RO, CRC, AE, PTL, MP */
636 XE_OUTB(XE_TX0Msk, 0xff); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
637 XE_OUTB(XE_TX0Msk+1, 0xb0); /* rsv, rsv, PTD, EXT, rsv, rsv, rsv, rsv */
638 XE_OUTB(XE_RST0, 0x00); /* ROK, RAB, REN, RO, CRC, AE, PTL, MP */
639 XE_OUTB(XE_TXST0, 0x00); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
640 XE_OUTB(XE_TXST1, 0x00); /* TEN, rsv, PTD, EXT, retry_counter:4 */
643 * Check for an in-progress autonegotiation. If one is active, just set
644 * IFF_RUNNING and return. The media selection code will call us again when
647 if (scp->autoneg_status) {
648 scp->ifp->if_flags |= IFF_RUNNING;
651 /* Enable receiver, put MAC online */
652 XE_SELECT_PAGE(0x40);
653 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
655 /* Set up IMR, enable interrupts */
658 /* Attempt to start output */
659 scp->ifp->if_flags |= IFF_RUNNING;
660 scp->ifp->if_flags &= ~IFF_OACTIVE;
669 * Start output on interface. We make two assumptions here:
670 * 1) that we are in a critical section _before_ this code
671 * is called *and* is returned to the appropriate priority after
673 * 2) that the IFF_OACTIVE flag is checked before this code is called
674 * (i.e. that the output part of the interface is idle)
677 xe_start(struct ifnet *ifp) {
678 struct xe_softc *scp = ifp->if_softc;
682 * Loop while there are packets to be sent, and space to send them.
685 mbp = ifq_poll(&ifp->if_snd); /* Suck a packet off the send queue */
689 * We are using the !OACTIVE flag to indicate to the outside world that
690 * we can accept an additional packet rather than that the transmitter
691 * is _actually_ active. Indeed, the transmitter may be active, but if
692 * we haven't filled all the buffers with data then we still want to
695 ifp->if_flags &= ~IFF_OACTIVE;
699 if (xe_pio_write_packet(scp, mbp) != 0) {
700 ifp->if_flags |= IFF_OACTIVE;
704 mbp = ifq_dequeue(&ifp->if_snd);
707 ifp->if_timer = 5; /* In case we don't hear from the card again */
716 * Process an ioctl request. Adapted from the ed driver.
719 xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr) {
720 struct xe_softc *scp;
732 * If the interface is marked up and stopped, then start it. If it is
733 * marked down and running, then stop it.
735 if (ifp->if_flags & IFF_UP) {
736 if (!(ifp->if_flags & IFF_RUNNING)) {
743 if (ifp->if_flags & IFF_RUNNING)
750 * Multicast list has (maybe) changed; set the hardware filter
751 * accordingly. This also serves to deal with promiscuous mode if we have
752 * a BPF listener active.
761 * Someone wants to get/set media options.
763 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
767 error = ether_ioctl(ifp, command, data);
778 * Card interrupt handler.
780 * This function is probably more complicated than it needs to be, as it
781 * attempts to deal with the case where multiple packets get sent between
782 * interrupts. This is especially annoying when working out the collision
783 * stats. Not sure whether this case ever really happens or not (maybe on a
784 * slow/heavily loaded machine?) so it's probably best to leave this like it
787 * Note that the crappy PIO used to get packets on and off the card means that
788 * you will spend a lot of time in this routine -- I can get my P150 to spend
789 * 90% of its time servicing interrupts if I really hammer the network. Could
790 * fix this, but then you'd start dropping/losing packets. The moral of this
791 * story? If you want good network performance _and_ some cycles left over to
792 * get your work done, don't buy a Xircom card. Or convince them to tell me
793 * how to do memory-mapped I/O :)
798 struct xe_softc *scp = (struct xe_softc *) xscp;
801 u_int16_t rx_bytes, rxs, txs;
802 u_int8_t psr, isr, esr, rsr;
804 ifp = &scp->arpcom.ac_if;
805 rx_bytes = 0; /* Bytes received on this interrupt */
806 result = 0; /* Set true if the interrupt is for us */
809 XE_OUTB(XE_CR, 0); /* Disable interrupts */
812 psr = XE_INB(XE_PR); /* Stash the current register page */
815 * Read ISR to see what caused this interrupt. Note that this clears the
816 * ISR on CE2 type cards.
818 if ((isr = XE_INB(XE_ISR)) && isr != 0xff) {
820 result = 1; /* This device did generate an int */
821 esr = XE_INB(XE_ESR); /* Read the other status registers */
822 XE_SELECT_PAGE(0x40);
823 rxs = XE_INB(XE_RST0);
824 XE_OUTB(XE_RST0, ~rxs & 0xff);
825 txs = XE_INB(XE_TXST0);
826 txs |= XE_INB(XE_TXST1) << 8;
827 XE_OUTB(XE_TXST0, 0);
828 XE_OUTB(XE_TXST1, 0);
832 if_printf(ifp, "ISR=%#2.2x ESR=%#2.2x RST=%#2.2x TXST=%#4.4x\n", isr, esr, rxs, txs);
836 * Handle transmit interrupts
838 if (isr & XE_ISR_TX_PACKET) {
839 u_int8_t new_tpr, sent;
841 if ((new_tpr = XE_INB(XE_TPR)) < scp->tx_tpr) /* Update packet count */
842 sent = (0xff - scp->tx_tpr) + new_tpr; /* TPR rolled over */
844 sent = new_tpr - scp->tx_tpr;
846 if (sent > 0) { /* Packets sent since last interrupt */
847 scp->tx_tpr = new_tpr;
848 scp->tx_queued -= sent;
849 ifp->if_opackets += sent;
850 ifp->if_collisions += scp->tx_collisions;
853 * Collision stats are a PITA. If multiples frames have been sent, we
854 * distribute any outstanding collision count equally amongst them.
855 * However, if we're missing interrupts we're quite likely to also
856 * miss some collisions; thus the total count will be off anyway.
857 * Likewise, if we miss a frame dropped due to excessive collisions
858 * any outstanding collisions count will be held against the next
859 * frame to be successfully sent. Hopefully it averages out in the
861 * XXX - This will screw up if tx_collisions/sent > 14. FIX IT!
863 switch (scp->tx_collisions) {
867 scp->mibdata.dot3StatsSingleCollisionFrames++;
868 scp->mibdata.dot3StatsCollFrequencies[0]++;
872 scp->mibdata.dot3StatsMultipleCollisionFrames++;
873 scp->mibdata.dot3StatsCollFrequencies[scp->tx_collisions-1]++;
875 else { /* Distribute across multiple frames */
876 scp->mibdata.dot3StatsMultipleCollisionFrames += sent;
878 dot3StatsCollFrequencies[scp->tx_collisions/sent] += sent - scp->tx_collisions%sent;
880 dot3StatsCollFrequencies[scp->tx_collisions/sent + 1] += scp->tx_collisions%sent;
883 scp->tx_collisions = 0;
886 ifp->if_flags &= ~IFF_OACTIVE;
888 if (txs & 0x0002) { /* Excessive collisions (packet dropped) */
889 ifp->if_collisions += 16;
891 scp->tx_collisions = 0;
892 scp->mibdata.dot3StatsExcessiveCollisions++;
893 scp->mibdata.dot3StatsMultipleCollisionFrames++;
894 scp->mibdata.dot3StatsCollFrequencies[15]++;
895 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
897 if (txs & 0x0040) /* Transmit aborted -- probably collisions */
898 scp->tx_collisions++;
902 * Handle receive interrupts
904 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
906 if ((rsr = XE_INB(XE_RSR)) & XE_RSR_RX_OK) {
907 struct ether_header *ehp;
911 len = XE_INW(XE_RBC);
918 * Limit the amount of time we spend in this loop, dropping packets if
919 * necessary. The Linux code does this with considerably more
920 * finesse, adjusting the threshold dynamically.
922 if ((rx_bytes += len) > 22000) {
924 scp->mibData.dot3StatsMissedFrames++;
925 XE_OUTW(XE_DO, 0x8000);
933 MGETHDR(mbp, MB_DONTWAIT, MT_DATA); /* Allocate a header mbuf */
935 mbp->m_pkthdr.rcvif = ifp;
936 mbp->m_pkthdr.len = mbp->m_len = len;
939 * If the mbuf header isn't big enough for the packet, attach an
940 * mbuf cluster to hold it. The +2 is to allow for the nasty little
941 * alignment hack below.
943 if (len + 2 > MHLEN) {
944 MCLGET(mbp, MB_DONTWAIT);
945 if ((mbp->m_flags & M_EXT) == 0) {
954 * The Ethernet header is 14 bytes long; thus the actual packet data
955 * won't be 32-bit aligned when it's dumped into the mbuf. We
956 * offset everything by 2 bytes to fix this. Apparently the
957 * alignment is important for NFS, damn its eyes.
960 ehp = mtod(mbp, struct ether_header *);
963 * Now get the packet, including the Ethernet header and trailer (?)
964 * We use programmed I/O, because we don't know how to do shared
965 * memory with these cards. So yes, it's real slow, and heavy on
966 * the interrupts (CPU on my P150 maxed out at ~950KBps incoming).
968 if (scp->srev == 0) { /* Workaround a bug in old cards */
972 rhs = XE_INW(XE_RHSA);
975 rhs += 3; /* Skip control info */
980 if (rhs + len > 0x8000) {
984 * XXX - This i-- seems very wrong, but it's what the Linux guys
985 * XXX - do. Need someone with an old CE2 to test this for me.
986 * XXX - 99/3/28: Changed the first i-- to an i++, maybe that'll
987 * XXX - fix it? It seems as though the previous version would
988 * XXX - have caused an infinite loop (what, another one?).
990 for (i = 0; i < len; i++, rhs++) {
991 ((char *)ehp)[i] = XE_INB(XE_EDP);
999 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
1000 (u_int16_t *) ehp, len >> 1);
1003 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
1004 (u_int16_t *) ehp, len >> 1);
1006 /* Deliver packet to upper layers */
1008 mbp->m_pkthdr.len = mbp->m_len = len;
1009 (*ifp->if_input)(ifp, mbp); /* Send the packet on its way */
1010 ifp->if_ipackets++; /* Success! */
1012 XE_OUTW(XE_DO, 0x8000); /* skip_rx_packet command */
1015 else if (rsr & XE_RSR_LONG_PACKET) { /* Packet length >1518 bytes */
1016 scp->mibdata.dot3StatsFrameTooLongs++;
1019 else if (rsr & XE_RSR_CRC_ERROR) { /* Bad checksum on packet */
1020 scp->mibdata.dot3StatsFCSErrors++;
1023 else if (rsr & XE_RSR_ALIGN_ERROR) { /* Packet alignment error */
1024 scp->mibdata.dot3StatsAlignmentErrors++;
1028 if (rxs & 0x10) { /* Receiver overrun */
1029 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
1031 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
1035 XE_SELECT_PAGE(psr); /* Restore saved page */
1036 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Re-enable interrupts */
1038 /* Could force an int here, instead of dropping packets? */
1039 /* XE_OUTB(XE_CR, XE_CR_ENABLE_INTR|XE_CE_FORCE_INTR); */
1046 * Device timeout/watchdog routine. Called automatically if we queue a packet
1047 * for transmission but don't get an interrupt within a specified timeout
1048 * (usually 5 seconds). When this happens we assume the worst and reset the
1052 xe_watchdog(struct ifnet *ifp) {
1053 struct xe_softc *scp = ifp->if_softc;
1055 if_printf(ifp, "watchdog timeout; resetting card\n");
1057 ifp->if_oerrors += scp->tx_queued;
1066 * Change media selection.
1069 xe_media_change(struct ifnet *ifp) {
1070 struct xe_softc *scp = ifp->if_softc;
1073 if_printf(ifp, "media_change\n");
1076 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
1080 * Some card/media combos aren't always possible -- filter those out here.
1082 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
1083 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
1093 * Return current media selection.
1096 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
1099 if_printf(ifp, "media_status\n");
1102 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
1109 * Select active media.
1111 static void xe_setmedia(void *xscp) {
1112 struct xe_softc *scp = xscp;
1113 u_int16_t bmcr, bmsr, anar, lpar;
1116 if_printf(scp->ifp, "setmedia\n");
1119 /* Cancel any pending timeout */
1120 callout_stop(&scp->xe_timer);
1121 xe_disable_intr(scp);
1124 scp->media = IFM_ETHER;
1125 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
1127 case IFM_AUTO: /* Autoselect media */
1128 scp->media = IFM_ETHER|IFM_AUTO;
1131 * Autoselection is really awful. It goes something like this:
1133 * Wait until the transmitter goes idle (2sec timeout).
1135 * IF a 100Mbit PHY exists
1136 * Start NWAY autonegotiation (3.5sec timeout)
1138 * Select 100baseTX or 10baseT, whichever was detected
1141 * IF a 100Mbit PHY exists
1142 * Try to force a 100baseTX link (3sec timeout)
1151 * IF nothing selected so far
1152 * IF a 100Mbit PHY exists
1155 * Select 10baseT or 10base2, whichever is connected
1159 switch (scp->autoneg_status) {
1161 case XE_AUTONEG_NONE:
1163 if_printf(scp->ifp, "Waiting for idle transmitter\n");
1165 scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
1166 scp->autoneg_status = XE_AUTONEG_WAITING;
1167 callout_reset(&scp->xe_timer, hz * 2, xe_setmedia, scp);
1170 case XE_AUTONEG_WAITING:
1174 if_printf(scp->ifp, "Starting autonegotiation\n");
1176 bmcr = xe_phy_readreg(scp, PHY_BMCR);
1177 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1178 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1179 anar = xe_phy_readreg(scp, PHY_ANAR);
1180 anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
1181 anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
1182 xe_phy_writereg(scp, PHY_ANAR, anar);
1183 bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
1184 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1185 scp->autoneg_status = XE_AUTONEG_STARTED;
1186 callout_reset(&scp->xe_timer, hz * 7 / 2, xe_setmedia, scp);
1190 scp->autoneg_status = XE_AUTONEG_FAIL;
1194 case XE_AUTONEG_STARTED:
1195 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1196 lpar = xe_phy_readreg(scp, PHY_LPAR);
1197 if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
1199 if_printf(scp->ifp, "Autonegotiation complete!\n");
1202 * XXX - Shouldn't have to do this, but (on my hub at least) the
1203 * XXX - transmitter won't work after a successful autoneg. So we see
1204 * XXX - what the negotiation result was and force that mode. I'm
1205 * XXX - sure there is an easy fix for this.
1207 if (lpar & PHY_LPAR_100BTXHALF) {
1208 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1211 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1212 scp->media = IFM_ETHER|IFM_100_TX;
1213 scp->autoneg_status = XE_AUTONEG_NONE;
1217 * XXX - Bit of a hack going on in here.
1218 * XXX - This is derived from Ken Hughes patch to the Linux driver
1219 * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
1220 * XXX - cards. What's a CE3B and how's it differ from a plain CE3?
1221 * XXX - these are the things we need to find out.
1223 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1226 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1227 XE_SELECT_PAGE(0x42);
1228 XE_OUTB(XE_SWC1, 0x80);
1229 scp->media = IFM_ETHER|IFM_10_T;
1230 scp->autoneg_status = XE_AUTONEG_NONE;
1232 /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/ /* Disable PHY? */
1233 /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1238 if_printf(scp->ifp, "Autonegotiation failed; trying 100baseTX\n");
1243 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1244 scp->autoneg_status = XE_AUTONEG_100TX;
1245 callout_reset(&scp->xe_timer, hz * 3, xe_setmedia, scp);
1249 scp->autoneg_status = XE_AUTONEG_FAIL;
1254 case XE_AUTONEG_100TX:
1255 (void)xe_phy_readreg(scp, PHY_BMSR);
1256 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1257 if (bmsr & PHY_BMSR_LINKSTAT) {
1259 if_printf(scp->ifp, "Got 100baseTX link!\n");
1263 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1264 scp->media = IFM_ETHER|IFM_100_TX;
1265 scp->autoneg_status = XE_AUTONEG_NONE;
1269 if_printf(scp->ifp, "Autonegotiation failed; disabling PHY\n");
1272 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1274 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY? */
1275 scp->autoneg_status = XE_AUTONEG_FAIL;
1281 * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1282 * either autonegotiation failed, or never got started to begin with. In
1283 * either case, select a suitable 10Mbit media and hope it works. We
1284 * don't need to reset the card again, since it will have been done
1285 * already by the big switch above.
1287 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1289 if_printf(scp->ifp, "Selecting 10baseX\n");
1292 XE_SELECT_PAGE(0x42);
1293 XE_OUTB(XE_SWC1, 0x80);
1294 scp->media = IFM_ETHER|IFM_10_T;
1295 scp->autoneg_status = XE_AUTONEG_NONE;
1299 XE_OUTB(XE_GPR0, 4);
1301 XE_SELECT_PAGE(0x42);
1302 XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
1303 scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1304 scp->autoneg_status = XE_AUTONEG_NONE;
1311 * If a specific media has been requested, we just reset the card and
1312 * select it (one small exception -- if 100baseTX is requested by there is
1313 * no PHY, we fall back to 10baseT operation).
1315 case IFM_100_TX: /* Force 100baseTX */
1319 if_printf(scp->ifp, "Selecting 100baseTX\n");
1321 XE_SELECT_PAGE(0x42);
1322 XE_OUTB(XE_SWC1, 0);
1323 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1325 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1326 scp->media |= IFM_100_TX;
1331 case IFM_10_T: /* Force 10baseT */
1334 if_printf(scp->ifp, "Selecting 10baseT\n");
1337 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1339 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY */
1341 XE_SELECT_PAGE(0x42);
1342 XE_OUTB(XE_SWC1, 0x80);
1343 scp->media |= IFM_10_T;
1349 if_printf(scp->ifp, "Selecting 10base2\n");
1351 XE_SELECT_PAGE(0x42);
1352 XE_OUTB(XE_SWC1, 0xc0);
1353 scp->media |= IFM_10_2;
1359 * Finally, the LEDs are set to match whatever media was chosen and the
1360 * transmitter is unblocked.
1363 if_printf(scp->ifp, "Setting LEDs\n");
1366 switch (IFM_SUBTYPE(scp->media)) {
1369 XE_OUTB(XE_LED, 0x3b);
1371 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1375 XE_OUTB(XE_LED, 0x3a);
1379 /* Restart output? */
1380 scp->ifp->if_flags &= ~IFF_OACTIVE;
1386 * Hard reset (power cycle) the card.
1389 xe_hard_reset(struct xe_softc *scp) {
1392 if_printf(scp->ifp, "hard_reset\n");
1398 * Power cycle the card.
1401 XE_OUTB(XE_GPR1, 0); /* Power off */
1405 XE_OUTB(XE_GPR1, 1); /* And back on again */
1407 XE_OUTB(XE_GPR1, 5); /* Also set AIC bit, whatever that is */
1416 * Soft reset the card. Also makes sure that the ML6692 and 10Mbit controller
1417 * are powered up, sets the silicon revision number in softc, disables
1418 * interrupts and checks for the prescence of a 100Mbit PHY. This should
1419 * leave us in a position where we can access the PHY and do media
1420 * selection. The function imposes a 0.5s delay while the hardware powers up.
1423 xe_soft_reset(struct xe_softc *scp) {
1426 if_printf(scp->ifp, "soft_reset\n");
1432 * Reset the card, (again).
1435 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
1442 * set GP1 and GP2 as outputs (bits 2 & 3)
1443 * set GP1 low to power on the ML6692 (bit 0)
1444 * set GP2 high to power on the 10Mhz chip (bit 1)
1447 XE_OUTB(XE_GPR0, 0x0e);
1451 * Wait for everything to wake up.
1456 * Get silicon revision number.
1460 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
1462 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
1464 if_printf(scp->ifp, "silicon revision = %d\n", scp->srev);
1468 * Shut off interrupts.
1470 xe_disable_intr(scp);
1476 scp->phy_ok = xe_mii_init(scp);
1486 * Take interface offline. This is done by powering down the device, which I
1487 * assume means just shutting down the transceiver and Ethernet logic. This
1488 * requires a _hard_ reset to recover from, as we need to power up again.
1491 xe_stop(struct xe_softc *scp) {
1494 if_printf(scp->ifp, "stop\n");
1500 * Shut off interrupts.
1502 xe_disable_intr(scp);
1508 XE_OUTB(XE_GPR1, 0);
1512 * ~IFF_RUNNING == interface down.
1514 scp->ifp->if_flags &= ~IFF_RUNNING;
1515 scp->ifp->if_flags &= ~IFF_OACTIVE;
1516 scp->ifp->if_timer = 0;
1523 * Enable Ethernet interrupts from the card.
1526 xe_enable_intr(struct xe_softc *scp) {
1528 if_printf(scp->ifp, "enable_intr\n");
1532 XE_OUTB(XE_IMR0, 0xff); /* Unmask everything */
1533 XE_OUTB(XE_IMR1, 0x01); /* Unmask TX underrun detection */
1537 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1538 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1539 if (!(XE_INB(0x10) & 0x01)) {
1540 XE_OUTB(0x10, 0x11); /* Unmask master int enable bit */
1547 * Disable all Ethernet interrupts from the card.
1550 xe_disable_intr(struct xe_softc *scp) {
1552 if_printf(scp->ifp, "disable_intr\n");
1556 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1557 if (scp->modem && !scp->dingo) { /* More magic (does this work?) */
1558 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1562 XE_OUTB(XE_IMR0, 0); /* Forbid all interrupts */
1563 XE_OUTB(XE_IMR1, 0);
1569 * Set up multicast filter and promiscuous mode
1572 xe_setmulti(struct xe_softc *scp) {
1574 struct ifmultiaddr *ifma;
1577 ifp = &scp->arpcom.ac_if;
1579 /* Get length of multicast list */
1581 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1584 if ((ifp->if_flags & IFF_PROMISC) || (ifp->if_flags & IFF_ALLMULTI) || (count > 9)) {
1586 * Go into promiscuous mode if either of the PROMISC or ALLMULTI flags are
1587 * set, or if we have been asked to deal with more than 9 multicast
1588 * addresses. To do this: set MPE and PME in SWC1
1590 XE_SELECT_PAGE(0x42);
1591 XE_OUTB(XE_SWC1, 0x06);
1593 else if ((ifp->if_flags & IFF_MULTICAST) && (count > 0)) {
1595 * Program the filters for up to 9 addresses
1597 XE_SELECT_PAGE(0x42);
1598 XE_OUTB(XE_SWC1, 0x01);
1599 XE_SELECT_PAGE(0x40);
1600 XE_OUTB(XE_CMD0, XE_CMD0_OFFLINE);
1601 /*xe_reg_dump(scp);*/
1603 /*xe_reg_dump(scp);*/
1604 XE_SELECT_PAGE(0x40);
1605 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
1609 * No multicast operation (default)
1611 XE_SELECT_PAGE(0x42);
1612 XE_OUTB(XE_SWC1, 0);
1619 * Set up all on-chip addresses (for multicast). AFAICS, there are 10
1620 * of these things; the first is our MAC address, the other 9 are mcast
1621 * addresses, padded with the MAC address if there aren't enough.
1622 * XXX - This doesn't work right, but I'm not sure why yet. We seem to be
1623 * XXX - doing much the same as the Linux code, which is weird enough that
1624 * XXX - it's probably right (despite my earlier comments to the contrary).
1627 xe_setaddrs(struct xe_softc *scp) {
1628 struct ifmultiaddr *ifma;
1630 u_int8_t page, slot, byte, i;
1632 ifma = LIST_FIRST(&scp->arpcom.ac_if.if_multiaddrs);
1634 XE_SELECT_PAGE(page = 0x50);
1636 for (slot = 0, byte = 8; slot < 10; slot++) {
1639 addr = (u_int8_t *)(&scp->arpcom.ac_enaddr);
1641 while (ifma != NULL && ifma->ifma_addr->sa_family != AF_LINK)
1642 ifma = LIST_NEXT(ifma, ifma_link);
1644 addr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
1646 addr = (u_int8_t *)(&scp->arpcom.ac_enaddr);
1649 for (i = 0; i < 6; i++, byte++) {
1652 printf(":%x", addr[i]);
1654 if_printf(scp->ifp, "individual addresses %d: %x", slot, addr[0]);
1660 XE_SELECT_PAGE(page);
1664 XE_OUTB(byte, addr[5 - i]);
1666 XE_OUTB(byte, addr[i]);
1678 * Write an outgoing packet to the card using programmed I/O.
1681 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
1683 u_int16_t len, pad, free, ok;
1685 u_int8_t savebyte[2], wantbyte;
1687 /* Get total packet length */
1688 for (len = 0, mbp2 = mbp; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
1690 /* Packets < minimum length may need to be padded out */
1692 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1693 pad = (ETHER_MIN_LEN - ETHER_CRC_LEN - len + 1) >> 1;
1694 len = ETHER_MIN_LEN - ETHER_CRC_LEN;
1697 /* Check transmit buffer space */
1699 XE_OUTW(XE_TRS, len+2);
1700 free = XE_INW(XE_TSO);
1703 if (free <= len + 2)
1706 /* Send packet length to card */
1707 XE_OUTW(XE_EDP, len);
1710 * Write packet to card using PIO (code stolen from the ed driver)
1713 while (mbp != NULL) {
1716 data = mtod(mbp, caddr_t);
1717 if (wantbyte) { /* Finish the last word */
1718 savebyte[1] = *data;
1719 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1724 if (len > 1) { /* Output contiguous words */
1725 bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1730 if (len == 1) { /* Save last byte, if necessary */
1731 savebyte[0] = *data;
1737 if (wantbyte) /* Last byte for odd-length packets */
1738 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1741 * For CE3 cards, just tell 'em to send -- apparently the card will pad out
1742 * short packets with random cruft. Otherwise, write nonsense words to fill
1743 * out the packet. I guess it is then sent automatically (?)
1746 XE_OUTB(XE_CR, XE_CR_TX_PACKET|XE_CR_ENABLE_INTR);
1749 XE_OUTW(XE_EDP, 0xdead);
1758 * Compute the 32-bit Ethernet CRC for the given buffer.
1761 xe_compute_crc(u_int8_t *data, int len) {
1762 u_int32_t crc = 0xffffffff;
1763 u_int32_t poly = 0x04c11db6;
1764 u_int8_t current, crc31, bit;
1767 for (i = 0; i < len; i++) {
1769 for (k = 1; k <= 8; k++) {
1770 if (crc & 0x80000000) {
1776 bit = crc31 ^ (current & 0x01);
1780 crc = (crc ^ poly)|1;
1789 * Convert a CRC into an index into the multicast hash table. What we do is
1790 * take the most-significant 6 bits of the CRC, reverse them, and use that as
1791 * the bit number in the hash table. Bits 5:3 of the result give the byte
1792 * within the table (0-7); bits 2:0 give the bit number within that byte (also
1793 * 0-7), ie. the number of shifts needed to get it into the lsb position.
1796 xe_compute_hashbit(u_int32_t crc) {
1797 u_int8_t hashbit = 0;
1800 for (i = 0; i < 6; i++) {
1802 if (crc & 0x80000000) {
1807 return (hashbit >> 2);
1814 /**************************************************************
1816 * M I I F U N C T I O N S *
1818 **************************************************************/
1821 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1822 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1823 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1824 * XXX - some kind of common MII-handling code, shared by all drivers. But
1825 * XXX - that's a whole other mission.
1827 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1828 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1832 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1835 xe_mii_sync(struct xe_softc *scp) {
1839 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1841 for (i = 0; i < 32; i++) {
1842 XE_MII_SET(XE_MII_CLK);
1844 XE_MII_CLR(XE_MII_CLK);
1851 * Look for a MII-compliant PHY. If we find one, reset it.
1854 xe_mii_init(struct xe_softc *scp) {
1857 status = xe_phy_readreg(scp, PHY_BMSR);
1858 if ((status & 0xff00) != 0x7800) {
1860 if_printf(scp->ifp, "no PHY found, %0x\n", status);
1866 if_printf(scp->ifp, "PHY OK!\n");
1870 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1872 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1880 * Clock a series of bits through the MII.
1883 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
1887 XE_MII_CLR(XE_MII_CLK);
1889 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1891 XE_MII_SET(XE_MII_WRD);
1893 XE_MII_CLR(XE_MII_WRD);
1896 XE_MII_CLR(XE_MII_CLK);
1898 XE_MII_SET(XE_MII_CLK);
1904 * Read an PHY register through the MII.
1907 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1913 * Set up frame for RX.
1915 frame->mii_stdelim = XE_MII_STARTDELIM;
1916 frame->mii_opcode = XE_MII_READOP;
1917 frame->mii_turnaround = 0;
1918 frame->mii_data = 0;
1921 XE_OUTB(XE_GPR2, 0);
1924 * Turn on data xmit.
1926 XE_MII_SET(XE_MII_DIR);
1931 * Send command/address info.
1933 xe_mii_send(scp, frame->mii_stdelim, 2);
1934 xe_mii_send(scp, frame->mii_opcode, 2);
1935 xe_mii_send(scp, frame->mii_phyaddr, 5);
1936 xe_mii_send(scp, frame->mii_regaddr, 5);
1939 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1941 XE_MII_SET(XE_MII_CLK);
1944 /* Turn off xmit. */
1945 XE_MII_CLR(XE_MII_DIR);
1948 XE_MII_CLR(XE_MII_CLK);
1950 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1951 XE_MII_SET(XE_MII_CLK);
1955 * Now try reading data bits. If the ack failed, we still
1956 * need to clock through 16 cycles to keep the PHY(s) in sync.
1959 for(i = 0; i < 16; i++) {
1960 XE_MII_CLR(XE_MII_CLK);
1962 XE_MII_SET(XE_MII_CLK);
1968 for (i = 0x8000; i; i >>= 1) {
1969 XE_MII_CLR(XE_MII_CLK);
1972 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1973 frame->mii_data |= i;
1976 XE_MII_SET(XE_MII_CLK);
1982 XE_MII_CLR(XE_MII_CLK);
1984 XE_MII_SET(XE_MII_CLK);
1996 * Write to a PHY register through the MII.
1999 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
2004 * Set up frame for TX.
2006 frame->mii_stdelim = XE_MII_STARTDELIM;
2007 frame->mii_opcode = XE_MII_WRITEOP;
2008 frame->mii_turnaround = XE_MII_TURNAROUND;
2013 * Turn on data output.
2015 XE_MII_SET(XE_MII_DIR);
2019 xe_mii_send(scp, frame->mii_stdelim, 2);
2020 xe_mii_send(scp, frame->mii_opcode, 2);
2021 xe_mii_send(scp, frame->mii_phyaddr, 5);
2022 xe_mii_send(scp, frame->mii_regaddr, 5);
2023 xe_mii_send(scp, frame->mii_turnaround, 2);
2024 xe_mii_send(scp, frame->mii_data, 16);
2027 XE_MII_SET(XE_MII_CLK);
2029 XE_MII_CLR(XE_MII_CLK);
2035 XE_MII_CLR(XE_MII_DIR);
2044 * Read a register from the PHY.
2047 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
2048 struct xe_mii_frame frame;
2050 bzero((char *)&frame, sizeof(frame));
2052 frame.mii_phyaddr = 0;
2053 frame.mii_regaddr = reg;
2054 xe_mii_readreg(scp, &frame);
2056 return(frame.mii_data);
2061 * Write to a PHY register.
2064 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
2065 struct xe_mii_frame frame;
2067 bzero((char *)&frame, sizeof(frame));
2069 frame.mii_phyaddr = 0;
2070 frame.mii_regaddr = reg;
2071 frame.mii_data = data;
2072 xe_mii_writereg(scp, &frame);
2080 * A bit of debugging code.
2083 xe_mii_dump(struct xe_softc *scp) {
2088 if_printf(scp->ifp, "MII registers: ");
2089 for (i = 0; i < 2; i++) {
2090 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
2092 for (i = 4; i < 7; i++) {
2093 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
2101 xe_reg_dump(struct xe_softc *scp) {
2106 if_printf(scp->ifp, "Common registers: ");
2107 for (i = 0; i < 8; i++) {
2108 printf(" %2.2x", XE_INB(i));
2112 for (page = 0; page <= 8; page++) {
2113 if_printf(scp->ifp, "Register page %2.2x: ", page);
2114 XE_SELECT_PAGE(page);
2115 for (i = 8; i < 16; i++) {
2116 printf(" %2.2x", XE_INB(i));
2121 for (page = 0x10; page < 0x5f; page++) {
2122 if ((page >= 0x11 && page <= 0x3f) ||
2124 (page >= 0x43 && page <= 0x4f) ||
2127 if_printf(scp->ifp, "Register page %2.2x: ", page);
2128 XE_SELECT_PAGE(page);
2129 for (i = 8; i < 16; i++) {
2130 printf(" %2.2x", XE_INB(i));
2140 xe_activate(device_t dev)
2142 struct xe_softc *sc = device_get_softc(dev);
2146 sc->port_rid = 0; /* 0 is managed by pccard */
2147 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
2148 &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
2151 * Find a 16 byte aligned ioport for the card.
2154 device_printf(dev, "Finding an aligned port for RealPort\n");
2155 #endif /* XE_DEBUG */
2156 sc->port_rid = 1; /* 0 is managed by pccard */
2159 sc->port_res = bus_alloc_resource(dev,
2160 SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
2162 if (sc->port_res == 0)
2163 break; /* we failed */
2164 if ((rman_get_start(sc->port_res) & 0xf) == 0)
2166 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2168 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
2171 device_printf(dev, "port 0x%0lx, size 0x%0lx\n",
2172 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
2173 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid));
2174 #endif /* XE_DEBUG */
2176 if (!sc->port_res) {
2178 device_printf(dev, "Cannot allocate ioport\n");
2184 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2188 device_printf(dev, "Cannot allocate irq\n");
2193 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, xe_intr, sc,
2194 &sc->intrhand, NULL);
2196 ether_ifdetach(&sc->arpcom.ac_if);
2201 sc->bst = rman_get_bustag(sc->port_res);
2202 sc->bsh = rman_get_bushandle(sc->port_res);
2207 xe_deactivate(device_t dev)
2209 struct xe_softc *sc = device_get_softc(dev);
2212 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
2215 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2219 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
2225 static device_method_t xe_pccard_methods[] = {
2226 /* Device interface */
2227 DEVMETHOD(device_probe, xe_probe),
2228 DEVMETHOD(device_attach, xe_attach),
2229 DEVMETHOD(device_detach, xe_detach),
2234 static driver_t xe_pccard_driver = {
2237 sizeof(struct xe_softc),
2240 devclass_t xe_devclass;
2242 DECLARE_DUMMY_MODULE(if_xe);
2243 DRIVER_MODULE(if_xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);