2 * Copyright (c) 1998, 1999, 2003 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.39 2003/10/14 22:51:35 rsm Exp $
31 * Portions of this software were derived from Werner Koch's xirc2ps driver
32 * for Linux under the terms of the following license (from v1.30 of the
35 * Copyright (c) 1997 by Werner Koch (dd9jn)
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, and the entire permission notice in its entirety,
42 * including the disclaimer of warranties.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. The name of the author may not be used to endorse or promote
47 * products derived from this software without specific prior
50 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
51 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
54 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
60 * OF THE POSSIBILITY OF SUCH DAMAGE.
64 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
65 * following cards are currently known to work with the driver:
66 * Xircom CreditCard 10/100 (CE3)
67 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
68 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
69 * Xircom RealPort Ethernet 10
70 * Xircom RealPort Ethernet 10/100
71 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
72 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
73 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
75 * Some other cards *should* work, but support for them is either broken or in
76 * an unknown state at the moment. I'm always interested in hearing from
77 * people who own any of these cards:
78 * Xircom CreditCard 10Base-T (PS-CE2-10)
79 * Xircom CreditCard Ethernet + ModemII (CEM2)
80 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
82 * Thanks to all who assisted with the development and testing of the driver,
83 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
84 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
85 * out anyone who deserves a mention here.
87 * Special thanks to Ade Lovett for both hosting the mailing list and doing
88 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
91 * Author email: <scott@uk.freebsd.org>
92 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
96 #include <sys/param.h>
97 #include <sys/errno.h>
98 #include <sys/kernel.h>
100 #include <sys/select.h>
101 #include <sys/socket.h>
102 #include <sys/sockio.h>
103 #include <sys/sysctl.h>
104 #include <sys/systm.h>
106 #include <sys/serialize.h>
107 #include <sys/module.h>
109 #include <sys/rman.h>
110 #include <sys/thread2.h>
111 #include <sys/interrupt.h>
113 #include <net/ethernet.h>
115 #include <net/ifq_var.h>
116 #include <net/if_arp.h>
117 #include <net/if_dl.h>
118 #include <net/if_media.h>
119 #include <net/if_mib.h>
122 #include "if_xereg.h"
123 #include "if_xevar.h"
126 * MII command structure
128 struct xe_mii_frame {
129 u_int8_t mii_stdelim;
131 u_int8_t mii_phyaddr;
132 u_int8_t mii_regaddr;
133 u_int8_t mii_turnaround;
138 * Media autonegotiation progress constants
140 #define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
141 #define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
142 #define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
143 #define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
144 #define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
147 * Multicast hashing CRC constants
149 #define XE_CRC_POLY 0x04c11db6
152 * Prototypes start here
154 static void xe_init (void *xscp);
155 static void xe_intr (void *xscp);
156 static void xe_start (struct ifnet *ifp);
157 static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *);
158 static void xe_watchdog (struct ifnet *ifp);
159 static int xe_media_change (struct ifnet *ifp);
160 static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
161 static timeout_t xe_setmedia;
162 static timeout_t xe_setmedia_serialized;
163 static void xe_reset (struct xe_softc *scp);
164 static void xe_stop (struct xe_softc *scp);
165 static void xe_enable_intr (struct xe_softc *scp);
166 static void xe_disable_intr (struct xe_softc *scp);
167 static void xe_set_multicast (struct xe_softc *scp);
168 static void xe_set_addr (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
169 static void xe_set_hash (struct xe_softc *scp, u_int8_t* addr);
170 static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
175 static void xe_mii_sync (struct xe_softc *scp);
176 static int xe_mii_init (struct xe_softc *scp);
177 static void xe_mii_send (struct xe_softc *scp, u_int32_t bits, int cnt);
178 static int xe_mii_readreg (struct xe_softc *scp, struct xe_mii_frame *frame);
179 static int xe_mii_writereg (struct xe_softc *scp, struct xe_mii_frame *frame);
180 static u_int16_t xe_phy_readreg (struct xe_softc *scp, u_int16_t reg);
181 static void xe_phy_writereg (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
183 /* Debugging functions */
184 static void xe_reg_dump (struct xe_softc *scp) __unused;
185 static void xe_mii_dump (struct xe_softc *scp);
192 SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "xe parameters");
195 * Debug logging levels - set with hw.xe.debug sysctl
197 * 1 = More hardware details, probe/attach progress
198 * 2 = Most function calls, ioctls and media selection progress
199 * 3 = Everything - interrupts, packets in/out and multicast address setup
202 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0, "xe debug level");
204 #define DPRINTF(level, arg) if (xe_debug >= (level)) kprintf arg
205 #define IFPRINTF(level, arg) if (xe_debug >= (level)) if_printf arg
206 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
207 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
208 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
210 #else /* !XE_DEBUG */
212 #define DPRINTF(level, arg)
213 #define IFPRINTF(level, arg)
214 #define DEVPRINTF(level, arg)
215 #define XE_REG_DUMP(scp)
216 #define XE_MII_DUMP(scp)
218 #endif /* XE_DEBUG */
221 * The device entry is being removed, probably because someone ejected the
222 * card. The interface should have been brought down manually before calling
223 * this function; if not you may well lose packets. In any case, I shut down
224 * the card and the interface, and hope for the best.
227 xe_detach(device_t dev)
229 struct xe_softc *sc = device_get_softc(dev);
230 struct ifnet *ifp = &sc->arpcom.ac_if;
232 lwkt_serialize_enter(ifp->if_serializer);
234 ifp->if_flags &= ~IFF_RUNNING;
235 callout_stop(&sc->xe_timer);
236 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
238 lwkt_serialize_exit(ifp->if_serializer);
250 xe_attach (device_t dev)
252 struct xe_softc *scp = device_get_softc(dev);
255 DEVPRINTF(2, (dev, "attach\n"));
257 /* Fill in some private data */
258 scp->ifp = &scp->arpcom.ac_if;
259 scp->ifm = &scp->ifmedia;
260 scp->autoneg_status = XE_AUTONEG_NONE;
262 /* Initialise the ifnet structure */
263 scp->ifp->if_softc = scp;
264 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
265 scp->ifp->if_timer = 0;
266 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
267 scp->ifp->if_linkmib = &scp->mibdata;
268 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
269 scp->ifp->if_start = xe_start;
270 scp->ifp->if_ioctl = xe_ioctl;
271 scp->ifp->if_watchdog = xe_watchdog;
272 scp->ifp->if_init = xe_init;
273 scp->ifp->if_baudrate = 100000000;
274 ifq_set_maxlen(&scp->ifp->if_snd, IFQ_MAXLEN);
275 ifq_set_ready(&scp->ifp->if_snd);
277 /* Initialise the ifmedia structure */
278 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
279 callout_init(&scp->xe_timer);
281 /* Add supported media types */
283 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
284 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
285 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
287 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
289 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
290 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
292 /* Default is to autoselect best supported media type */
293 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
295 /* Get the hardware into a known state */
298 /* Get hardware version numbers */
300 scp->version = XE_INB(XE_BOV);
302 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
304 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
306 /* Print some useful information */
307 device_printf(dev, "%s %s, version 0x%02x/0x%02x%s%s\n",
312 scp->mohawk ? ", 100Mbps capable" : "",
313 scp->modem ? ", with modem" : "");
316 XE_SELECT_PAGE(0x10);
317 DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
320 XE_INW(XE_VendorID)));
323 XE_SELECT_PAGE(0x45);
324 DEVPRINTF(1, (dev, "CE2 version = 0x%#02x\n", XE_INB(XE_REV)));
327 /* Attach the interface */
328 ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr, NULL);
330 err = bus_setup_intr(dev, scp->irq_res, INTR_MPSAFE,
331 xe_intr, scp, &scp->intrhand,
332 scp->ifp->if_serializer);
334 device_printf(dev, "Setup intr failed\n");
335 ether_ifdetach(scp->ifp);
340 scp->ifp->if_cpuid = ithread_cpuid(rman_get_start(scp->irq_res));
341 KKASSERT(scp->ifp->if_cpuid >= 0 && scp->ifp->if_cpuid < ncpus);
349 * Complete hardware intitialisation and enable output. Exits without doing
350 * anything if there's no address assigned to the card, or if media selection
351 * is in progress (the latter implies we've already run this function).
354 xe_init(void *xscp) {
355 struct xe_softc *scp = xscp;
358 if (scp->autoneg_status != XE_AUTONEG_NONE) return;
360 IFPRINTF(2, (scp->ifp, "init\n"));
364 /* Reset transmitter flags */
367 scp->tx_timeouts = 0;
369 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
370 scp->ifp->if_timer = 0;
372 /* Soft reset the card */
374 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
381 * set GP1 and GP2 as outputs (bits 2 & 3)
382 * set GP1 low to power on the ML6692 (bit 0)
383 * set GP2 high to power on the 10Mhz chip (bit 1)
386 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP2_OUT);
389 /* Shut off interrupts */
390 xe_disable_intr(scp);
392 /* Wait for everything to wake up */
397 scp->phy_ok = xe_mii_init(scp);
399 /* Disable 'source insertion' (not sure what that means) */
400 XE_SELECT_PAGE(0x42);
401 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
403 /* Set 8K/24K Tx/Rx buffer split */
404 if (scp->srev != 1) {
406 XE_OUTW(XE_RBS, 0x2000);
409 /* Enable early transmit mode on Mohawk/Dingo */
411 XE_SELECT_PAGE(0x03);
412 XE_OUTW(XE_TPT, scp->tx_thres);
413 XE_SELECT_PAGE(0x01);
414 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
417 /* Put MAC address in first 'individual address' register */
418 XE_SELECT_PAGE(0x50);
419 for (i = 0; i < 6; i++)
420 XE_OUTB(0x08 + i, scp->arpcom.ac_enaddr[scp->mohawk ? 5 - i : i]);
422 /* Set up multicast addresses */
423 xe_set_multicast(scp);
425 /* Fix the receive data offset -- reset can leave it off-by-one */
427 XE_OUTW(XE_DO, 0x2000);
429 /* Set interrupt masks */
431 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR | XE_IMR0_RX_PACKET);
433 /* Set MAC interrupt masks */
434 XE_SELECT_PAGE(0x40);
436 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR
437 | XE_RX0M_ALIGN_ERROR | XE_RX0M_LONG_PACKET));
439 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN
440 | XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
442 /* Clear MAC status registers */
443 XE_SELECT_PAGE(0x40);
444 XE_OUTB(XE_RST0, 0x00);
445 XE_OUTB(XE_TXST0, 0x00);
447 /* Enable receiver and put MAC online */
448 XE_SELECT_PAGE(0x40);
449 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
451 /* Set up IMR, enable interrupts */
454 /* Start media selection */
455 xe_setmedia_serialized(scp);
458 scp->ifp->if_flags |= IFF_RUNNING;
459 scp->ifp->if_flags &= ~IFF_OACTIVE;
466 * Start output on interface. Should be called at splimp() priority. Check
467 * that the output is idle (ie, IFF_OACTIVE is not set) before calling this
468 * function. If media selection is in progress we set IFF_OACTIVE ourselves
469 * and return immediately.
472 xe_start(struct ifnet *ifp) {
473 struct xe_softc *scp = ifp->if_softc;
476 if (scp->autoneg_status != XE_AUTONEG_NONE) {
477 ifp->if_flags |= IFF_OACTIVE;
481 IFPRINTF(3, (ifp, "start\n"));
484 * Loop while there are packets to be sent, and space to send them.
487 /* Suck a packet off the send queue */
488 mbp = ifq_dequeue(&ifp->if_snd, NULL);
491 * We are using the !OACTIVE flag to indicate to the outside
492 * world that we can accept an additional packet rather than
493 * that the transmitter is _actually_ active. Indeed, the
494 * transmitter may be active, but if we haven't filled all
495 * the buffers with data then we still want to accept more.
497 ifp->if_flags &= ~IFF_OACTIVE;
501 if (xe_pio_write_packet(scp, mbp) != 0) {
502 ifp->if_flags |= IFF_OACTIVE;
503 ifq_prepend(&ifp->if_snd, mbp);
509 ifp->if_timer = 5; /* In case we don't hear from the card again */
518 * Process an ioctl request. Adapted from the ed driver.
521 xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr) {
522 struct xe_softc *scp;
533 IFPRINTF(2, (ifp, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
535 * If the interface is marked up and stopped, then start it. If it is
536 * marked down and running, then stop it.
538 if (ifp->if_flags & IFF_UP) {
539 if (!(ifp->if_flags & IFF_RUNNING)) {
545 if (ifp->if_flags & IFF_RUNNING)
548 /* FALL THROUGH (handle changes to PROMISC/ALLMULTI flags) */
552 IFPRINTF(2, (ifp, "ioctl: SIOC{ADD,DEL}MULTI\n"));
554 * Multicast list has (maybe) changed; set the hardware filters
557 xe_set_multicast(scp);
563 IFPRINTF(3, (ifp, "ioctl: bounce to ifmedia_ioctl\n"));
565 * Someone wants to get/set media options.
567 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
571 IFPRINTF(3, (ifp, "ioctl: bounce to ether_ioctl\n"));
572 error = ether_ioctl(ifp, command, data);
583 * Card interrupt handler.
585 * This function is probably more complicated than it needs to be, as it
586 * attempts to deal with the case where multiple packets get sent between
587 * interrupts. This is especially annoying when working out the collision
588 * stats. Not sure whether this case ever really happens or not (maybe on a
589 * slow/heavily loaded machine?) so it's probably best to leave this like it
592 * Note that the crappy PIO used to get packets on and off the card means that
593 * you will spend a lot of time in this routine -- I can get my P150 to spend
594 * 90% of its time servicing interrupts if I really hammer the network. Could
595 * fix this, but then you'd start dropping/losing packets. The moral of this
596 * story? If you want good network performance _and_ some cycles left over to
597 * get your work done, don't buy a Xircom card. Or convince them to tell me
598 * how to do memory-mapped I/O :)
603 struct xe_softc *scp = (struct xe_softc *) xscp;
605 u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
607 ifp = &scp->arpcom.ac_if;
609 /* Disable interrupts */
613 /* Cache current register page */
616 /* Read ISR to see what caused this interrupt */
617 while ((isr = XE_INB(XE_ISR)) != 0) {
618 /* 0xff might mean the card is no longer around */
620 IFPRINTF(3, (ifp, "intr: interrupt received for missing card?\n"));
624 /* Read other status registers */
625 XE_SELECT_PAGE(0x40);
626 rst0 = XE_INB(XE_RST0);
628 txst0 = XE_INB(XE_TXST0);
629 txst1 = XE_INB(XE_TXST1);
630 coll = txst1 & XE_TXST1_RETRY_COUNT;
631 XE_OUTB(XE_TXST0, 0);
632 XE_OUTB(XE_TXST1, 0);
636 "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x, COLL=0x%01x\n",
637 isr, rst0, txst1, txst0, coll));
639 /* Handle transmitted packet(s) */
640 if (isr & XE_ISR_TX_PACKET) {
643 /* Update packet count, accounting for rollover */
644 tpr = XE_INB(XE_TPR);
645 sent = -scp->tx_tpr + tpr;
647 /* Update statistics if we actually sent anything */
650 scp->tx_queued -= sent;
651 ifp->if_opackets += sent;
652 ifp->if_collisions += coll;
655 * According to the Xircom manual, Dingo will sometimes manage to
656 * transmit a packet with triggering an interrupt. If this happens,
657 * we have sent > 1 and the collision count only reflects collisions
658 * on the last packet sent (the one that triggered the interrupt).
659 * Collision stats might therefore be a bit low, but there doesn't
660 * seem to be anything we can do about that.
666 scp->mibdata.dot3StatsSingleCollisionFrames++;
667 scp->mibdata.dot3StatsCollFrequencies[0]++;
670 scp->mibdata.dot3StatsMultipleCollisionFrames++;
671 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
675 ifp->if_flags &= ~IFF_OACTIVE;
678 /* Handle most MAC interrupts */
679 if (isr & XE_ISR_MAC_INTR) {
681 /* Carrier sense lost -- only in 10Mbit HDX mode */
682 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
683 /* XXX - Need to update media status here */
684 device_printf(scp->dev, "no carrier\n");
686 scp->mibdata.dot3StatsCarrierSenseErrors++;
689 /* Excessive collisions -- try sending again */
690 if (txst0 & XE_TXST0_16_COLLISIONS) {
691 ifp->if_collisions += 16;
693 scp->mibdata.dot3StatsExcessiveCollisions++;
694 scp->mibdata.dot3StatsMultipleCollisionFrames++;
695 scp->mibdata.dot3StatsCollFrequencies[15]++;
696 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
698 /* Transmit underrun -- increase early transmit threshold */
699 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
700 IFPRINTF(1, (ifp, "transmit underrun"));
701 if (scp->tx_thres < ETHER_MAX_LEN) {
702 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
703 scp->tx_thres = ETHER_MAX_LEN;
704 DPRINTF(1, (": increasing transmit threshold to %u", scp->tx_thres));
706 XE_OUTW(XE_TPT, scp->tx_thres);
711 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
713 /* Late collision -- just complain about it */
714 if (txst0 & XE_TXST0_LATE_COLLISION) {
715 if_printf(ifp, "late collision\n");
717 scp->mibdata.dot3StatsLateCollisions++;
719 /* SQE test failure -- just complain about it */
720 if (txst0 & XE_TXST0_SQE_FAIL) {
721 if_printf(ifp, "SQE test failure\n");
723 scp->mibdata.dot3StatsSQETestErrors++;
725 /* Packet too long -- what happens to these */
726 if (rst0 & XE_RST0_LONG_PACKET) {
727 if_printf(ifp, "received giant packet\n");
729 scp->mibdata.dot3StatsFrameTooLongs++;
731 /* CRC error -- packet dropped */
732 if (rst0 & XE_RST0_CRC_ERROR) {
733 if_printf(ifp, "CRC error\n");
735 scp->mibdata.dot3StatsFCSErrors++;
739 /* Handle received packet(s) */
740 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
741 rsr = XE_INB(XE_RSR);
742 IFPRINTF(3, (ifp, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
744 /* Make sure packet is a good one */
745 if (rsr & XE_RSR_RX_OK) {
746 struct ether_header *ehp;
750 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
752 IFPRINTF(3, (ifp, "intr: receive length = %d\n", len));
760 * Allocate mbuf to hold received packet. If the mbuf header isn't
761 * big enough, we attach an mbuf cluster to hold the packet. Note the
762 * +=2 to align the packet data on a 32-bit boundary, and the +3 to
763 * allow for the possibility of reading one more byte than the actual
764 * packet length (we always read 16-bit words).
765 * XXX - Surely there's a better way to do this alignment?
767 MGETHDR(mbp, MB_DONTWAIT, MT_DATA);
773 if (len + 3 > MHLEN) {
774 MCLGET(mbp, MB_DONTWAIT);
775 if ((mbp->m_flags & M_EXT) == 0) {
783 ehp = mtod(mbp, struct ether_header *);
786 * Now get the packet in PIO mode, including the Ethernet header but
787 * omitting the trailing CRC.
791 * Work around a bug in CE2 cards. There seems to be a problem with
792 * duplicated and extraneous bytes in the receive buffer, but without
793 * any real documentation for the CE2 it's hard to tell for sure.
794 * XXX - Needs testing on CE2 hardware
796 if (scp->srev == 0) {
800 rhs = XE_INW(XE_RHSA);
803 rhs += 3; /* Skip control info */
808 if (rhs + len > 0x8000) {
811 for (i = 0; i < len; i++, rhs++) {
812 ((char *)ehp)[i] = XE_INB(XE_EDP);
820 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
821 (u_int16_t *) ehp, (len + 1) >> 1);
824 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
825 (u_int16_t *) ehp, (len + 1) >> 1);
827 /* Deliver packet to upper layers */
828 mbp->m_pkthdr.rcvif = ifp;
829 mbp->m_pkthdr.len = mbp->m_len = len;
830 ifp->if_input(ifp, mbp);
833 else if (rsr & XE_RSR_ALIGN_ERROR) {
834 /* Packet alignment error -- drop packet */
835 if_printf(ifp, "alignment error\n");
836 scp->mibdata.dot3StatsAlignmentErrors++;
840 /* Skip to next packet, if there is one */
841 XE_OUTW(XE_DO, 0x8000);
844 /* Clear receiver overruns now we have some free buffer space */
845 if (rst0 & XE_RST0_RX_OVERRUN) {
846 IFPRINTF(1, (ifp, "receive overrun\n"));
848 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
849 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
853 /* Restore saved page */
856 /* Re-enable interrupts */
857 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
862 * Device timeout/watchdog routine. Called automatically if we queue a packet
863 * for transmission but don't get an interrupt within a specified timeout
864 * (usually 5 seconds). When this happens we assume the worst and reset the
868 xe_watchdog(struct ifnet *ifp) {
869 struct xe_softc *scp = ifp->if_softc;
871 if_printf(ifp, "watchdog timeout; resetting card\n");
873 ifp->if_oerrors += scp->tx_queued;
881 * Change media selection.
884 xe_media_change(struct ifnet *ifp) {
885 struct xe_softc *scp = ifp->if_softc;
887 IFPRINTF(2, (ifp, "media_change\n"));
889 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
893 * Some card/media combos aren't always possible -- filter those out here.
895 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
896 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
899 xe_setmedia_serialized(scp);
906 * Return current media selection.
909 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
911 IFPRINTF(3, (ifp, "media_status\n"));
913 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
914 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
915 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
920 xe_setmedia(void *xscp)
922 struct xe_softc *scp = xscp;
924 lwkt_serialize_enter(scp->arpcom.ac_if.if_serializer);
925 xe_setmedia_serialized(xscp);
926 lwkt_serialize_exit(scp->arpcom.ac_if.if_serializer);
930 * Select active media.
934 xe_setmedia_serialized(void *xscp)
936 struct xe_softc *scp = xscp;
937 u_int16_t bmcr, bmsr, anar, lpar;
939 IFPRINTF(2, (scp->ifp, "setmedia\n"));
941 /* Cancel any pending timeout */
942 callout_stop(&scp->xe_timer);
943 xe_disable_intr(scp);
946 scp->media = IFM_ETHER;
947 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
949 case IFM_AUTO: /* Autoselect media */
950 scp->media = IFM_ETHER|IFM_AUTO;
953 * Autoselection is really awful. It goes something like this:
955 * Wait until the transmitter goes idle (2sec timeout).
957 * IF a 100Mbit PHY exists
958 * Start NWAY autonegotiation (3.5sec timeout)
960 * Select 100baseTX or 10baseT, whichever was detected
963 * IF a 100Mbit PHY exists
964 * Try to force a 100baseTX link (3sec timeout)
973 * IF nothing selected so far
974 * IF a 100Mbit PHY exists
977 * Select 10baseT or 10base2, whichever is connected
981 switch (scp->autoneg_status) {
983 case XE_AUTONEG_NONE:
984 IFPRINTF(2, (scp->ifp, "Waiting for idle transmitter\n"));
985 scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
986 scp->autoneg_status = XE_AUTONEG_WAITING;
989 case XE_AUTONEG_WAITING:
990 if (scp->tx_queued != 0) {
991 callout_reset(&scp->xe_timer, hz / 2, xe_setmedia, scp);
995 IFPRINTF(2, (scp->ifp, "Starting autonegotiation\n"));
996 bmcr = xe_phy_readreg(scp, PHY_BMCR);
997 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
998 xe_phy_writereg(scp, PHY_BMCR, bmcr);
999 anar = xe_phy_readreg(scp, PHY_ANAR);
1000 anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
1001 anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
1002 xe_phy_writereg(scp, PHY_ANAR, anar);
1003 bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
1004 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1005 scp->autoneg_status = XE_AUTONEG_STARTED;
1006 callout_reset(&scp->xe_timer, hz * 7 / 2, xe_setmedia, scp);
1010 scp->autoneg_status = XE_AUTONEG_FAIL;
1014 case XE_AUTONEG_STARTED:
1015 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1016 lpar = xe_phy_readreg(scp, PHY_LPAR);
1017 if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
1018 IFPRINTF(2, (scp->ifp, "Autonegotiation complete!\n"));
1020 * XXX - Shouldn't have to do this, but (on my hub at least) the
1021 * XXX - transmitter won't work after a successful autoneg. So we see
1022 * XXX - what the negotiation result was and force that mode. I'm
1023 * XXX - sure there is an easy fix for this.
1025 if (lpar & PHY_LPAR_100BTXHALF) {
1026 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1029 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1030 scp->media = IFM_ETHER|IFM_100_TX;
1031 scp->autoneg_status = XE_AUTONEG_NONE;
1035 * XXX - Bit of a hack going on in here.
1036 * XXX - This is derived from Ken Hughes patch to the Linux driver
1037 * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
1038 * XXX - cards. What's a CE3B and how's it differ from a plain CE3?
1039 * XXX - these are the things we need to find out.
1041 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1044 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1045 XE_SELECT_PAGE(0x42);
1046 XE_OUTB(XE_SWC1, 0x80);
1047 scp->media = IFM_ETHER|IFM_10_T;
1048 scp->autoneg_status = XE_AUTONEG_NONE;
1050 /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/ /* Disable PHY? */
1051 /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1055 IFPRINTF(2, (scp->ifp, "Autonegotiation failed; trying 100baseTX\n"));
1058 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1059 scp->autoneg_status = XE_AUTONEG_100TX;
1060 callout_reset(&scp->xe_timer, hz * 3, xe_setmedia, scp);
1064 scp->autoneg_status = XE_AUTONEG_FAIL;
1069 case XE_AUTONEG_100TX:
1070 xe_phy_readreg(scp, PHY_BMSR);
1071 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1072 if (bmsr & PHY_BMSR_LINKSTAT) {
1073 IFPRINTF(2, (scp->ifp, "Got 100baseTX link!\n"));
1076 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1077 scp->media = IFM_ETHER|IFM_100_TX;
1078 scp->autoneg_status = XE_AUTONEG_NONE;
1081 IFPRINTF(2, (scp->ifp, "Autonegotiation failed; disabling PHY\n"));
1083 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1085 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY? */
1086 scp->autoneg_status = XE_AUTONEG_FAIL;
1092 * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1093 * either autonegotiation failed, or never got started to begin with. In
1094 * either case, select a suitable 10Mbit media and hope it works. We
1095 * don't need to reset the card again, since it will have been done
1096 * already by the big switch above.
1098 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1099 IFPRINTF(2, (scp->ifp, "Selecting 10baseX\n"));
1101 XE_SELECT_PAGE(0x42);
1102 XE_OUTB(XE_SWC1, 0x80);
1103 scp->media = IFM_ETHER|IFM_10_T;
1104 scp->autoneg_status = XE_AUTONEG_NONE;
1108 XE_OUTB(XE_GPR0, 4);
1110 XE_SELECT_PAGE(0x42);
1111 XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
1112 scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1113 scp->autoneg_status = XE_AUTONEG_NONE;
1120 * If a specific media has been requested, we just reset the card and
1121 * select it (one small exception -- if 100baseTX is requested by there is
1122 * no PHY, we fall back to 10baseT operation).
1124 case IFM_100_TX: /* Force 100baseTX */
1126 IFPRINTF(2, (scp->ifp, "Selecting 100baseTX\n"));
1127 XE_SELECT_PAGE(0x42);
1128 XE_OUTB(XE_SWC1, 0);
1129 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1131 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1132 scp->media |= IFM_100_TX;
1137 case IFM_10_T: /* Force 10baseT */
1138 IFPRINTF(2, (scp->ifp, "Selecting 10baseT\n"));
1140 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1142 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY */
1144 XE_SELECT_PAGE(0x42);
1145 XE_OUTB(XE_SWC1, 0x80);
1146 scp->media |= IFM_10_T;
1150 IFPRINTF(2, (scp->ifp, "Selecting 10base2\n"));
1151 XE_SELECT_PAGE(0x42);
1152 XE_OUTB(XE_SWC1, 0xc0);
1153 scp->media |= IFM_10_2;
1159 * Finally, the LEDs are set to match whatever media was chosen and the
1160 * transmitter is unblocked.
1162 IFPRINTF(2, (scp->ifp, "Setting LEDs\n"));
1164 switch (IFM_SUBTYPE(scp->media)) {
1167 XE_OUTB(XE_LED, 0x3b);
1169 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1173 XE_OUTB(XE_LED, 0x3a);
1177 /* Restart output? */
1178 xe_enable_intr(scp);
1179 scp->ifp->if_flags &= ~IFF_OACTIVE;
1180 if_devstart(scp->ifp);
1185 * Hard reset (power cycle) the card.
1188 xe_reset(struct xe_softc *scp) {
1189 IFPRINTF(2, (scp->ifp, "hard_reset\n"));
1195 XE_OUTB(XE_GPR1, 0);
1198 /* Power up again */
1200 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1202 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
1212 * Take interface offline. This is done by powering down the device, which I
1213 * assume means just shutting down the transceiver and Ethernet logic. This
1214 * requires a _hard_ reset to recover from, as we need to power up again.
1217 xe_stop(struct xe_softc *scp) {
1218 IFPRINTF(2, (scp->ifp, "stop\n"));
1223 * Shut off interrupts.
1225 xe_disable_intr(scp);
1231 XE_OUTB(XE_GPR1, 0);
1235 * set GP1 and GP2 as outputs (bits 2 & 3)
1236 * set GP1 high to power on the ML6692 (bit 0)
1237 * set GP2 low to power on the 10Mhz chip (bit 1)
1240 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP1_OUT);
1244 * ~IFF_RUNNING == interface down.
1246 scp->ifp->if_flags &= ~IFF_RUNNING;
1247 scp->ifp->if_flags &= ~IFF_OACTIVE;
1248 scp->ifp->if_timer = 0;
1255 * Enable interrupts from the card.
1258 xe_enable_intr(struct xe_softc *scp) {
1259 IFPRINTF(2, (scp->ifp, "enable_intr\n"));
1262 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1263 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1264 if (!(XE_INB(0x10) & 0x01)) {
1265 XE_OUTB(0x10, 0x11); /* Unmask master int enable bit */
1272 * Disable interrupts from the card.
1275 xe_disable_intr(struct xe_softc *scp) {
1276 IFPRINTF(2, (scp->ifp, "disable_intr\n"));
1279 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1280 if (scp->modem && !scp->dingo) { /* More magic */
1281 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1287 * Set up multicast filter and promiscuous modes.
1290 xe_set_multicast(struct xe_softc *scp) {
1292 struct ifmultiaddr *ifma;
1295 ifp = &scp->arpcom.ac_if;
1297 IFPRINTF(2, (ifp, "set_multicast\n"));
1299 XE_SELECT_PAGE(0x42);
1301 /* Handle PROMISC flag */
1302 if (ifp->if_flags & IFF_PROMISC) {
1303 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1307 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1309 /* Handle ALLMULTI flag */
1310 if (ifp->if_flags & IFF_ALLMULTI) {
1311 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1315 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1317 /* Iterate over multicast address list */
1319 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1320 if (ifma->ifma_addr->sa_family != AF_LINK)
1326 /* First 9 use Individual Addresses for exact matching */
1327 xe_set_addr(scp, LLADDR((struct sockaddr_dl *)ifma->ifma_addr), count);
1330 /* Use hash filter on Mohawk and Dingo */
1331 xe_set_hash(scp, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1333 /* Nowhere else to put them on CE2 */
1337 IFPRINTF(2, (ifp, "set_multicast: count = %u\n", count));
1339 /* Now do some cleanup and enable multicast handling as needed */
1341 /* Disable all multicast handling */
1343 XE_SELECT_PAGE(0x42);
1344 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~(XE_SWC1_IA_ENABLE|XE_SWC1_ALLMULTI));
1346 XE_SELECT_PAGE(0x02);
1347 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1350 else if (count < 10) {
1351 /* Full in any unused Individual Addresses with our MAC address */
1352 for (i = count + 1; i < 10; i++)
1353 xe_set_addr(scp, (u_int8_t *)(&scp->arpcom.ac_enaddr), i);
1354 /* Enable Individual Address matching only */
1355 XE_SELECT_PAGE(0x42);
1356 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1358 XE_SELECT_PAGE(0x02);
1359 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1364 /* Check whether hash table is full */
1365 XE_SELECT_PAGE(0x58);
1366 for (i = 0x08; i < 0x10; i++)
1367 if (XE_INB(i) != 0xff)
1370 /* Hash table full - enable promiscuous multicast matching */
1371 XE_SELECT_PAGE(0x42);
1372 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1373 XE_SELECT_PAGE(0x02);
1374 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1377 /* Enable hash table and Individual Address matching */
1378 XE_SELECT_PAGE(0x42);
1379 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1380 XE_SELECT_PAGE(0x02);
1381 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1385 /* Enable promiscuous multicast matching */
1386 XE_SELECT_PAGE(0x42);
1387 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1395 * Copy the Ethernet multicast address in addr to the on-chip registers for
1396 * Individual Address idx. Assumes that addr is really a multicast address
1397 * and that idx > 0 (slot 0 is always used for the card MAC address).
1400 xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
1405 * Individual Addresses are stored in registers 8-F of pages 0x50-0x57. IA1
1406 * therefore starts at register 0xE on page 0x50. The expressions below
1407 * compute the starting page and register for any IA index > 0.
1410 page = 0x50 + idx%4 + idx/4*3;
1411 reg = 0x0e - 2 * (idx%4);
1413 IFPRINTF(3, (scp->ifp, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
1417 * Copy the IA bytes. Note that the byte order is reversed for Mohawk and
1418 * Dingo wrt. CE2 hardware.
1420 XE_SELECT_PAGE(page);
1421 for (i = 0; i < 6; i++) {
1424 DPRINTF(3, (":%02x", addr[i]));
1426 IFPRINTF(3, (scp->ifp, "set_addr: %02x", addr[0]));
1429 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1430 if (++reg == 0x10) {
1432 XE_SELECT_PAGE(++page);
1440 * Set the appropriate bit in the multicast hash table for the supplied
1441 * Ethernet multicast address addr. Assumes that addr is really a multicast
1445 xe_set_hash(struct xe_softc* scp, u_int8_t* addr) {
1446 u_int32_t crc = 0xffffffff;
1447 u_int8_t bit, byte, crc31, idx;
1450 /* Compute CRC of the address -- standard Ethernet CRC function */
1451 for (i = 0; i < 6; i++) {
1453 for (j = 1; j <= 8; j++) {
1454 if (crc & 0x80000000)
1458 bit = crc31 ^ (byte & 0x01);
1462 crc = (crc ^ XE_CRC_POLY)|1;
1466 IFPRINTF(3, (scp->ifp, "set_hash: CRC = 0x%08x\n", crc));
1468 /* Hash table index = 6 msbs of CRC, reversed */
1469 for (i = 0, idx = 0; i < 6; i++) {
1471 if (crc & 0x80000000) {
1477 /* Top 3 bits of idx give register - 8, bottom 3 give bit within register */
1478 byte = idx >> 3 | 0x08;
1479 bit = 0x01 << (idx & 0x07);
1481 IFPRINTF(3, (scp->ifp,
1482 "set_hash: idx = 0x%02x, byte = 0x%02x, bit = 0x%02x\n",
1485 XE_SELECT_PAGE(0x58);
1486 XE_OUTB(byte, XE_INB(byte) | bit);
1491 * Write an outgoing packet to the card using programmed I/O.
1494 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
1498 u_int8_t savebyte[2];
1500 /* Get total packet length */
1501 if (mbp->m_flags & M_PKTHDR)
1502 len = mbp->m_pkthdr.len;
1504 struct mbuf* mbp2 = mbp;
1505 for (len = 0; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
1508 IFPRINTF(3, (scp->ifp, "pio_write_packet: len = %u\n", len));
1510 /* Packets < minimum length may need to be padded out */
1512 if (len < scp->tx_min) {
1513 pad = scp->tx_min - len;
1517 /* Check transmit buffer space */
1519 XE_OUTW(XE_TRS, len+2); /* Only effective on rev. 1 CE2 cards */
1520 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1523 /* Send packet length to card */
1524 XE_OUTW(XE_EDP, len);
1527 * Write packet to card using PIO (code stolen from the ed driver)
1530 while (mbp != NULL) {
1533 data = mtod(mbp, caddr_t);
1534 if (wantbyte) { /* Finish the last word */
1535 savebyte[1] = *data;
1536 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1541 if (len > 1) { /* Output contiguous words */
1542 bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1547 if (len == 1) { /* Save last byte, if necessary */
1548 savebyte[0] = *data;
1556 * Send last byte of odd-length packets
1559 XE_OUTB(XE_EDP, savebyte[0]);
1562 * Can just tell CE3 cards to send; short packets will be padded out with
1563 * random cruft automatically. For CE2, manually pad the packet with
1564 * garbage; it will be sent when the required number or bytes have been
1565 * delivered to the card.
1568 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX | XE_CR_ENABLE_INTR);
1571 XE_OUTB(XE_EDP, 0xaa);
1574 XE_OUTW(XE_EDP, 0xdead);
1583 /**************************************************************
1585 * M I I F U N C T I O N S *
1587 **************************************************************/
1590 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1591 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1592 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1593 * XXX - some kind of common MII-handling code, shared by all drivers. But
1594 * XXX - that's a whole other mission.
1596 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1597 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1601 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1604 xe_mii_sync(struct xe_softc *scp) {
1608 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1610 for (i = 0; i < 32; i++) {
1611 XE_MII_SET(XE_MII_CLK);
1613 XE_MII_CLR(XE_MII_CLK);
1620 * Look for a MII-compliant PHY. If we find one, reset it.
1623 xe_mii_init(struct xe_softc *scp) {
1626 status = xe_phy_readreg(scp, PHY_BMSR);
1627 if ((status & 0xff00) != 0x7800) {
1628 IFPRINTF(2, (scp->ifp, "no PHY found, %0x\n", status));
1632 IFPRINTF(2, (scp->ifp, "PHY OK!\n"));
1635 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1637 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1645 * Clock a series of bits through the MII.
1648 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
1652 XE_MII_CLR(XE_MII_CLK);
1654 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1656 XE_MII_SET(XE_MII_WRD);
1658 XE_MII_CLR(XE_MII_WRD);
1661 XE_MII_CLR(XE_MII_CLK);
1663 XE_MII_SET(XE_MII_CLK);
1669 * Read an PHY register through the MII.
1672 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1678 * Set up frame for RX.
1680 frame->mii_stdelim = XE_MII_STARTDELIM;
1681 frame->mii_opcode = XE_MII_READOP;
1682 frame->mii_turnaround = 0;
1683 frame->mii_data = 0;
1686 XE_OUTB(XE_GPR2, 0);
1689 * Turn on data xmit.
1691 XE_MII_SET(XE_MII_DIR);
1696 * Send command/address info.
1698 xe_mii_send(scp, frame->mii_stdelim, 2);
1699 xe_mii_send(scp, frame->mii_opcode, 2);
1700 xe_mii_send(scp, frame->mii_phyaddr, 5);
1701 xe_mii_send(scp, frame->mii_regaddr, 5);
1704 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1706 XE_MII_SET(XE_MII_CLK);
1709 /* Turn off xmit. */
1710 XE_MII_CLR(XE_MII_DIR);
1713 XE_MII_CLR(XE_MII_CLK);
1715 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1716 XE_MII_SET(XE_MII_CLK);
1720 * Now try reading data bits. If the ack failed, we still
1721 * need to clock through 16 cycles to keep the PHY(s) in sync.
1724 for(i = 0; i < 16; i++) {
1725 XE_MII_CLR(XE_MII_CLK);
1727 XE_MII_SET(XE_MII_CLK);
1733 for (i = 0x8000; i; i >>= 1) {
1734 XE_MII_CLR(XE_MII_CLK);
1737 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1738 frame->mii_data |= i;
1741 XE_MII_SET(XE_MII_CLK);
1747 XE_MII_CLR(XE_MII_CLK);
1749 XE_MII_SET(XE_MII_CLK);
1761 * Write to a PHY register through the MII.
1764 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1769 * Set up frame for TX.
1771 frame->mii_stdelim = XE_MII_STARTDELIM;
1772 frame->mii_opcode = XE_MII_WRITEOP;
1773 frame->mii_turnaround = XE_MII_TURNAROUND;
1778 * Turn on data output.
1780 XE_MII_SET(XE_MII_DIR);
1784 xe_mii_send(scp, frame->mii_stdelim, 2);
1785 xe_mii_send(scp, frame->mii_opcode, 2);
1786 xe_mii_send(scp, frame->mii_phyaddr, 5);
1787 xe_mii_send(scp, frame->mii_regaddr, 5);
1788 xe_mii_send(scp, frame->mii_turnaround, 2);
1789 xe_mii_send(scp, frame->mii_data, 16);
1792 XE_MII_SET(XE_MII_CLK);
1794 XE_MII_CLR(XE_MII_CLK);
1800 XE_MII_CLR(XE_MII_DIR);
1809 * Read a register from the PHY.
1812 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
1813 struct xe_mii_frame frame;
1815 bzero((char *)&frame, sizeof(frame));
1817 frame.mii_phyaddr = 0;
1818 frame.mii_regaddr = reg;
1819 xe_mii_readreg(scp, &frame);
1821 return(frame.mii_data);
1826 * Write to a PHY register.
1829 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
1830 struct xe_mii_frame frame;
1832 bzero((char *)&frame, sizeof(frame));
1834 frame.mii_phyaddr = 0;
1835 frame.mii_regaddr = reg;
1836 frame.mii_data = data;
1837 xe_mii_writereg(scp, &frame);
1844 * A bit of debugging code.
1847 xe_mii_dump(struct xe_softc *scp) {
1852 if_printf(scp->ifp, "MII registers: ");
1853 for (i = 0; i < 2; i++) {
1854 kprintf(" %d:%04x", i, xe_phy_readreg(scp, i));
1856 for (i = 4; i < 7; i++) {
1857 kprintf(" %d:%04x", i, xe_phy_readreg(scp, i));
1865 xe_reg_dump(struct xe_softc *scp) {
1870 if_printf(scp->ifp, "Common registers: ");
1871 for (i = 0; i < 8; i++) {
1872 kprintf(" %2.2x", XE_INB(i));
1876 for (page = 0; page <= 8; page++) {
1877 if_printf(scp->ifp, "Register page %2.2x: ", page);
1878 XE_SELECT_PAGE(page);
1879 for (i = 8; i < 16; i++) {
1880 kprintf(" %2.2x", XE_INB(i));
1885 for (page = 0x10; page < 0x5f; page++) {
1886 if ((page >= 0x11 && page <= 0x3f) ||
1888 (page >= 0x43 && page <= 0x4f) ||
1891 if_printf(scp->ifp, "Register page %2.2x: ", page);
1892 XE_SELECT_PAGE(page);
1893 for (i = 8; i < 16; i++) {
1894 kprintf(" %2.2x", XE_INB(i));
1903 xe_activate(device_t dev)
1905 struct xe_softc *sc = device_get_softc(dev);
1908 DEVPRINTF(2, (dev, "activate\n"));
1911 sc->port_rid = 0; /* 0 is managed by pccard */
1912 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1913 &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
1914 } else if (sc->dingo) {
1916 * Find a 16 byte aligned ioport for the card.
1918 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1919 sc->port_rid = 1; /* 0 is managed by pccard */
1922 sc->port_res = bus_alloc_resource(dev,
1923 SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
1925 if (sc->port_res == 0)
1926 break; /* we failed */
1927 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1929 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1931 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1933 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
1934 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1935 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1939 * Find contiguous I/O port for the Ethernet function on CEM2 and
1940 * CEM3 cards. We allocate window 0 wherever pccard has decided
1941 * it should be, then find an available window adjacent to it for
1942 * the second function. Not sure that both windows are actually
1945 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
1946 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
1947 sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1948 &sc->ce2_port_rid, 0, ~0,
1950 if (!sc->ce2_port_res) {
1951 device_printf(dev, "Cannot allocate I/O port for modem\n");
1956 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
1958 for (i = 0; i < 2; i++) {
1959 start += (i == 0 ? 8 : -24);
1960 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1961 &sc->port_rid, start,
1962 start + 18, 18, RF_ACTIVE);
1963 if (sc->port_res == 0)
1964 continue; /* Failed, try again if possible */
1965 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
1966 sc->port_rid) == start)
1967 break; /* Success! */
1969 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1973 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
1974 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1975 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1977 if (!sc->port_res) {
1978 device_printf(dev, "Cannot allocate ioport\n");
1983 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1986 device_printf(dev, "Cannot allocate irq\n");
1991 sc->bst = rman_get_bustag(sc->port_res);
1992 sc->bsh = rman_get_bushandle(sc->port_res);
1997 xe_deactivate(device_t dev)
1999 struct xe_softc *sc = device_get_softc(dev);
2001 DEVPRINTF(2, (dev, "deactivate\n"));
2002 xe_disable_intr(sc);
2005 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2008 if (sc->ce2_port_res)
2009 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2011 sc->ce2_port_res = 0;
2013 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,