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 $
28 * $DragonFly: src/sys/dev/netif/xe/if_xe.c,v 1.31 2005/12/31 14:08:00 sephe Exp $
32 * Portions of this software were derived from Werner Koch's xirc2ps driver
33 * for Linux under the terms of the following license (from v1.30 of the
36 * Copyright (c) 1997 by Werner Koch (dd9jn)
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, and the entire permission notice in its entirety,
43 * including the disclaimer of warranties.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The name of the author may not be used to endorse or promote
48 * products derived from this software without specific prior
51 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
52 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61 * OF THE POSSIBILITY OF SUCH DAMAGE.
65 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
66 * following cards are currently known to work with the driver:
67 * Xircom CreditCard 10/100 (CE3)
68 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
69 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
70 * Xircom RealPort Ethernet 10
71 * Xircom RealPort Ethernet 10/100
72 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
73 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
74 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
76 * Some other cards *should* work, but support for them is either broken or in
77 * an unknown state at the moment. I'm always interested in hearing from
78 * people who own any of these cards:
79 * Xircom CreditCard 10Base-T (PS-CE2-10)
80 * Xircom CreditCard Ethernet + ModemII (CEM2)
81 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
83 * Thanks to all who assisted with the development and testing of the driver,
84 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
85 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
86 * out anyone who deserves a mention here.
88 * Special thanks to Ade Lovett for both hosting the mailing list and doing
89 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
92 * Author email: <scott@uk.freebsd.org>
93 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
97 #include <sys/param.h>
98 #include <sys/cdefs.h>
99 #include <sys/errno.h>
100 #include <sys/kernel.h>
101 #include <sys/mbuf.h>
102 #include <sys/select.h>
103 #include <sys/socket.h>
104 #include <sys/sockio.h>
105 #include <sys/sysctl.h>
106 #include <sys/systm.h>
108 #include <sys/serialize.h>
109 #include <sys/thread2.h>
111 #include <sys/module.h>
114 #include <machine/bus.h>
115 #include <machine/resource.h>
116 #include <sys/rman.h>
118 #include <net/ethernet.h>
120 #include <net/ifq_var.h>
121 #include <net/if_arp.h>
122 #include <net/if_dl.h>
123 #include <net/if_media.h>
124 #include <net/if_mib.h>
127 #include "if_xereg.h"
128 #include "if_xevar.h"
131 * MII command structure
133 struct xe_mii_frame {
134 u_int8_t mii_stdelim;
136 u_int8_t mii_phyaddr;
137 u_int8_t mii_regaddr;
138 u_int8_t mii_turnaround;
143 * Media autonegotiation progress constants
145 #define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
146 #define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
147 #define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
148 #define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
149 #define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
152 * Multicast hashing CRC constants
154 #define XE_CRC_POLY 0x04c11db6
157 * Prototypes start here
159 static void xe_init (void *xscp);
160 static void xe_intr (void *xscp);
161 static void xe_start (struct ifnet *ifp);
162 static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *);
163 static void xe_watchdog (struct ifnet *ifp);
164 static int xe_media_change (struct ifnet *ifp);
165 static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
166 static timeout_t xe_setmedia;
167 static timeout_t xe_setmedia_serialized;
168 static void xe_reset (struct xe_softc *scp);
169 static void xe_stop (struct xe_softc *scp);
170 static void xe_enable_intr (struct xe_softc *scp);
171 static void xe_disable_intr (struct xe_softc *scp);
172 static void xe_set_multicast (struct xe_softc *scp);
173 static void xe_set_addr (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
174 static void xe_set_hash (struct xe_softc *scp, u_int8_t* addr);
175 static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
180 static void xe_mii_sync (struct xe_softc *scp);
181 static int xe_mii_init (struct xe_softc *scp);
182 static void xe_mii_send (struct xe_softc *scp, u_int32_t bits, int cnt);
183 static int xe_mii_readreg (struct xe_softc *scp, struct xe_mii_frame *frame);
184 static int xe_mii_writereg (struct xe_softc *scp, struct xe_mii_frame *frame);
185 static u_int16_t xe_phy_readreg (struct xe_softc *scp, u_int16_t reg);
186 static void xe_phy_writereg (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
188 /* Debugging functions */
189 static void xe_reg_dump (struct xe_softc *scp) __unused;
190 static void xe_mii_dump (struct xe_softc *scp);
197 SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "xe parameters");
200 * Debug logging levels - set with hw.xe.debug sysctl
202 * 1 = More hardware details, probe/attach progress
203 * 2 = Most function calls, ioctls and media selection progress
204 * 3 = Everything - interrupts, packets in/out and multicast address setup
207 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0, "xe debug level");
209 #define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
210 #define IFPRINTF(level, arg) if (xe_debug >= (level)) if_printf arg
211 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
212 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
213 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
215 #else /* !XE_DEBUG */
217 #define DPRINTF(level, arg)
218 #define IFPRINTF(level, arg)
219 #define DEVPRINTF(level, arg)
220 #define XE_REG_DUMP(scp)
221 #define XE_MII_DUMP(scp)
223 #endif /* XE_DEBUG */
226 * The device entry is being removed, probably because someone ejected the
227 * card. The interface should have been brought down manually before calling
228 * this function; if not you may well lose packets. In any case, I shut down
229 * the card and the interface, and hope for the best.
232 xe_detach(device_t dev)
234 struct xe_softc *sc = device_get_softc(dev);
235 struct ifnet *ifp = &sc->arpcom.ac_if;
237 lwkt_serialize_enter(ifp->if_serializer);
239 ifp->if_flags &= ~IFF_RUNNING;
240 callout_stop(&sc->xe_timer);
241 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
243 lwkt_serialize_exit(ifp->if_serializer);
255 xe_attach (device_t dev)
257 struct xe_softc *scp = device_get_softc(dev);
260 DEVPRINTF(2, (dev, "attach\n"));
262 /* Fill in some private data */
263 scp->ifp = &scp->arpcom.ac_if;
264 scp->ifm = &scp->ifmedia;
265 scp->autoneg_status = XE_AUTONEG_NONE;
267 /* Initialise the ifnet structure */
268 scp->ifp->if_softc = scp;
269 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
270 scp->ifp->if_timer = 0;
271 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
272 scp->ifp->if_linkmib = &scp->mibdata;
273 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
274 scp->ifp->if_start = xe_start;
275 scp->ifp->if_ioctl = xe_ioctl;
276 scp->ifp->if_watchdog = xe_watchdog;
277 scp->ifp->if_init = xe_init;
278 scp->ifp->if_baudrate = 100000000;
279 ifq_set_maxlen(&scp->ifp->if_snd, IFQ_MAXLEN);
280 ifq_set_ready(&scp->ifp->if_snd);
282 /* Initialise the ifmedia structure */
283 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
284 callout_init(&scp->xe_timer);
286 /* Add supported media types */
288 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
289 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
290 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
292 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
294 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
295 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
297 /* Default is to autoselect best supported media type */
298 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
300 /* Get the hardware into a known state */
303 /* Get hardware version numbers */
305 scp->version = XE_INB(XE_BOV);
307 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
309 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
311 /* Print some useful information */
312 device_printf(dev, "%s %s, version 0x%02x/0x%02x%s%s\n",
317 scp->mohawk ? ", 100Mbps capable" : "",
318 scp->modem ? ", with modem" : "");
321 XE_SELECT_PAGE(0x10);
322 DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
325 XE_INW(XE_VendorID)));
328 XE_SELECT_PAGE(0x45);
329 DEVPRINTF(1, (dev, "CE2 version = 0x%#02x\n", XE_INB(XE_REV)));
332 /* Attach the interface */
333 ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr, NULL);
335 err = bus_setup_intr(dev, scp->irq_res, INTR_NETSAFE,
336 xe_intr, scp, &scp->intrhand,
337 scp->arpcom.ac_if.if_serializer);
339 device_printf(dev, "Setup intr failed\n");
340 ether_ifdetach(&scp->arpcom.ac_if);
351 * Complete hardware intitialisation and enable output. Exits without doing
352 * anything if there's no address assigned to the card, or if media selection
353 * is in progress (the latter implies we've already run this function).
356 xe_init(void *xscp) {
357 struct xe_softc *scp = xscp;
360 if (scp->autoneg_status != XE_AUTONEG_NONE) return;
362 IFPRINTF(2, (scp->ifp, "init\n"));
366 /* Reset transmitter flags */
369 scp->tx_timeouts = 0;
371 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
372 scp->ifp->if_timer = 0;
374 /* Soft reset the card */
376 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
383 * set GP1 and GP2 as outputs (bits 2 & 3)
384 * set GP1 low to power on the ML6692 (bit 0)
385 * set GP2 high to power on the 10Mhz chip (bit 1)
388 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP2_OUT);
391 /* Shut off interrupts */
392 xe_disable_intr(scp);
394 /* Wait for everything to wake up */
399 scp->phy_ok = xe_mii_init(scp);
401 /* Disable 'source insertion' (not sure what that means) */
402 XE_SELECT_PAGE(0x42);
403 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
405 /* Set 8K/24K Tx/Rx buffer split */
406 if (scp->srev != 1) {
408 XE_OUTW(XE_RBS, 0x2000);
411 /* Enable early transmit mode on Mohawk/Dingo */
413 XE_SELECT_PAGE(0x03);
414 XE_OUTW(XE_TPT, scp->tx_thres);
415 XE_SELECT_PAGE(0x01);
416 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
419 /* Put MAC address in first 'individual address' register */
420 XE_SELECT_PAGE(0x50);
421 for (i = 0; i < 6; i++)
422 XE_OUTB(0x08 + i, scp->arpcom.ac_enaddr[scp->mohawk ? 5 - i : i]);
424 /* Set up multicast addresses */
425 xe_set_multicast(scp);
427 /* Fix the receive data offset -- reset can leave it off-by-one */
429 XE_OUTW(XE_DO, 0x2000);
431 /* Set interrupt masks */
433 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR | XE_IMR0_RX_PACKET);
435 /* Set MAC interrupt masks */
436 XE_SELECT_PAGE(0x40);
438 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR
439 | XE_RX0M_ALIGN_ERROR | XE_RX0M_LONG_PACKET));
441 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN
442 | XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
444 /* Clear MAC status registers */
445 XE_SELECT_PAGE(0x40);
446 XE_OUTB(XE_RST0, 0x00);
447 XE_OUTB(XE_TXST0, 0x00);
449 /* Enable receiver and put MAC online */
450 XE_SELECT_PAGE(0x40);
451 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
453 /* Set up IMR, enable interrupts */
456 /* Start media selection */
457 xe_setmedia_serialized(scp);
460 scp->ifp->if_flags |= IFF_RUNNING;
461 scp->ifp->if_flags &= ~IFF_OACTIVE;
468 * Start output on interface. Should be called at splimp() priority. Check
469 * that the output is idle (ie, IFF_OACTIVE is not set) before calling this
470 * function. If media selection is in progress we set IFF_OACTIVE ourselves
471 * and return immediately.
474 xe_start(struct ifnet *ifp) {
475 struct xe_softc *scp = ifp->if_softc;
478 if (scp->autoneg_status != XE_AUTONEG_NONE) {
479 ifp->if_flags |= IFF_OACTIVE;
483 IFPRINTF(3, (ifp, "start\n"));
486 * Loop while there are packets to be sent, and space to send them.
489 mbp = ifq_poll(&ifp->if_snd); /* Suck a packet off the send queue */
493 * We are using the !OACTIVE flag to indicate to the outside world that
494 * we can accept an additional packet rather than that the transmitter
495 * is _actually_ active. Indeed, the transmitter may be active, but if
496 * we haven't filled all the buffers with data then we still want to
499 ifp->if_flags &= ~IFF_OACTIVE;
503 if (xe_pio_write_packet(scp, mbp) != 0) {
504 ifp->if_flags |= IFF_OACTIVE;
508 ifq_dequeue(&ifp->if_snd, mbp);
511 ifp->if_timer = 5; /* In case we don't hear from the card again */
520 * Process an ioctl request. Adapted from the ed driver.
523 xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr) {
524 struct xe_softc *scp;
535 IFPRINTF(2, (ifp, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
537 * If the interface is marked up and stopped, then start it. If it is
538 * marked down and running, then stop it.
540 if (ifp->if_flags & IFF_UP) {
541 if (!(ifp->if_flags & IFF_RUNNING)) {
547 if (ifp->if_flags & IFF_RUNNING)
550 /* FALL THROUGH (handle changes to PROMISC/ALLMULTI flags) */
554 IFPRINTF(2, (ifp, "ioctl: SIOC{ADD,DEL}MULTI\n"));
556 * Multicast list has (maybe) changed; set the hardware filters
559 xe_set_multicast(scp);
565 IFPRINTF(3, (ifp, "ioctl: bounce to ifmedia_ioctl\n"));
567 * Someone wants to get/set media options.
569 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
573 IFPRINTF(3, (ifp, "ioctl: bounce to ether_ioctl\n"));
574 error = ether_ioctl(ifp, command, data);
585 * Card interrupt handler.
587 * This function is probably more complicated than it needs to be, as it
588 * attempts to deal with the case where multiple packets get sent between
589 * interrupts. This is especially annoying when working out the collision
590 * stats. Not sure whether this case ever really happens or not (maybe on a
591 * slow/heavily loaded machine?) so it's probably best to leave this like it
594 * Note that the crappy PIO used to get packets on and off the card means that
595 * you will spend a lot of time in this routine -- I can get my P150 to spend
596 * 90% of its time servicing interrupts if I really hammer the network. Could
597 * fix this, but then you'd start dropping/losing packets. The moral of this
598 * story? If you want good network performance _and_ some cycles left over to
599 * get your work done, don't buy a Xircom card. Or convince them to tell me
600 * how to do memory-mapped I/O :)
605 struct xe_softc *scp = (struct xe_softc *) xscp;
607 u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
609 ifp = &scp->arpcom.ac_if;
611 /* Disable interrupts */
615 /* Cache current register page */
618 /* Read ISR to see what caused this interrupt */
619 while ((isr = XE_INB(XE_ISR)) != 0) {
620 /* 0xff might mean the card is no longer around */
622 IFPRINTF(3, (ifp, "intr: interrupt received for missing card?\n"));
626 /* Read other status registers */
627 XE_SELECT_PAGE(0x40);
628 rst0 = XE_INB(XE_RST0);
630 txst0 = XE_INB(XE_TXST0);
631 txst1 = XE_INB(XE_TXST1);
632 coll = txst1 & XE_TXST1_RETRY_COUNT;
633 XE_OUTB(XE_TXST0, 0);
634 XE_OUTB(XE_TXST1, 0);
638 "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x, COLL=0x%01x\n",
639 isr, rst0, txst1, txst0, coll));
641 /* Handle transmitted packet(s) */
642 if (isr & XE_ISR_TX_PACKET) {
645 /* Update packet count, accounting for rollover */
646 tpr = XE_INB(XE_TPR);
647 sent = -scp->tx_tpr + tpr;
649 /* Update statistics if we actually sent anything */
652 scp->tx_queued -= sent;
653 ifp->if_opackets += sent;
654 ifp->if_collisions += coll;
657 * According to the Xircom manual, Dingo will sometimes manage to
658 * transmit a packet with triggering an interrupt. If this happens,
659 * we have sent > 1 and the collision count only reflects collisions
660 * on the last packet sent (the one that triggered the interrupt).
661 * Collision stats might therefore be a bit low, but there doesn't
662 * seem to be anything we can do about that.
668 scp->mibdata.dot3StatsSingleCollisionFrames++;
669 scp->mibdata.dot3StatsCollFrequencies[0]++;
672 scp->mibdata.dot3StatsMultipleCollisionFrames++;
673 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
677 ifp->if_flags &= ~IFF_OACTIVE;
680 /* Handle most MAC interrupts */
681 if (isr & XE_ISR_MAC_INTR) {
683 /* Carrier sense lost -- only in 10Mbit HDX mode */
684 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
685 /* XXX - Need to update media status here */
686 device_printf(scp->dev, "no carrier\n");
688 scp->mibdata.dot3StatsCarrierSenseErrors++;
691 /* Excessive collisions -- try sending again */
692 if (txst0 & XE_TXST0_16_COLLISIONS) {
693 ifp->if_collisions += 16;
695 scp->mibdata.dot3StatsExcessiveCollisions++;
696 scp->mibdata.dot3StatsMultipleCollisionFrames++;
697 scp->mibdata.dot3StatsCollFrequencies[15]++;
698 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
700 /* Transmit underrun -- increase early transmit threshold */
701 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
702 IFPRINTF(1, (ifp, "transmit underrun"));
703 if (scp->tx_thres < ETHER_MAX_LEN) {
704 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
705 scp->tx_thres = ETHER_MAX_LEN;
706 DPRINTF(1, (": increasing transmit threshold to %u", scp->tx_thres));
708 XE_OUTW(XE_TPT, scp->tx_thres);
713 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
715 /* Late collision -- just complain about it */
716 if (txst0 & XE_TXST0_LATE_COLLISION) {
717 if_printf(ifp, "late collision\n");
719 scp->mibdata.dot3StatsLateCollisions++;
721 /* SQE test failure -- just complain about it */
722 if (txst0 & XE_TXST0_SQE_FAIL) {
723 if_printf(ifp, "SQE test failure\n");
725 scp->mibdata.dot3StatsSQETestErrors++;
727 /* Packet too long -- what happens to these */
728 if (rst0 & XE_RST0_LONG_PACKET) {
729 if_printf(ifp, "received giant packet\n");
731 scp->mibdata.dot3StatsFrameTooLongs++;
733 /* CRC error -- packet dropped */
734 if (rst0 & XE_RST0_CRC_ERROR) {
735 if_printf(ifp, "CRC error\n");
737 scp->mibdata.dot3StatsFCSErrors++;
741 /* Handle received packet(s) */
742 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
743 rsr = XE_INB(XE_RSR);
744 IFPRINTF(3, (ifp, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
746 /* Make sure packet is a good one */
747 if (rsr & XE_RSR_RX_OK) {
748 struct ether_header *ehp;
752 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
754 IFPRINTF(3, (ifp, "intr: receive length = %d\n", len));
762 * Allocate mbuf to hold received packet. If the mbuf header isn't
763 * big enough, we attach an mbuf cluster to hold the packet. Note the
764 * +=2 to align the packet data on a 32-bit boundary, and the +3 to
765 * allow for the possibility of reading one more byte than the actual
766 * packet length (we always read 16-bit words).
767 * XXX - Surely there's a better way to do this alignment?
769 MGETHDR(mbp, MB_DONTWAIT, MT_DATA);
775 if (len + 3 > MHLEN) {
776 MCLGET(mbp, MB_DONTWAIT);
777 if ((mbp->m_flags & M_EXT) == 0) {
785 ehp = mtod(mbp, struct ether_header *);
788 * Now get the packet in PIO mode, including the Ethernet header but
789 * omitting the trailing CRC.
793 * Work around a bug in CE2 cards. There seems to be a problem with
794 * duplicated and extraneous bytes in the receive buffer, but without
795 * any real documentation for the CE2 it's hard to tell for sure.
796 * XXX - Needs testing on CE2 hardware
798 if (scp->srev == 0) {
802 rhs = XE_INW(XE_RHSA);
805 rhs += 3; /* Skip control info */
810 if (rhs + len > 0x8000) {
813 for (i = 0; i < len; i++, rhs++) {
814 ((char *)ehp)[i] = XE_INB(XE_EDP);
822 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
823 (u_int16_t *) ehp, (len + 1) >> 1);
826 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
827 (u_int16_t *) ehp, (len + 1) >> 1);
829 /* Deliver packet to upper layers */
830 mbp->m_pkthdr.rcvif = ifp;
831 mbp->m_pkthdr.len = mbp->m_len = len;
832 ifp->if_input(ifp, mbp);
835 else if (rsr & XE_RSR_ALIGN_ERROR) {
836 /* Packet alignment error -- drop packet */
837 if_printf(ifp, "alignment error\n");
838 scp->mibdata.dot3StatsAlignmentErrors++;
842 /* Skip to next packet, if there is one */
843 XE_OUTW(XE_DO, 0x8000);
846 /* Clear receiver overruns now we have some free buffer space */
847 if (rst0 & XE_RST0_RX_OVERRUN) {
848 IFPRINTF(1, (ifp, "receive overrun\n"));
850 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
851 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
855 /* Restore saved page */
858 /* Re-enable interrupts */
859 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
864 * Device timeout/watchdog routine. Called automatically if we queue a packet
865 * for transmission but don't get an interrupt within a specified timeout
866 * (usually 5 seconds). When this happens we assume the worst and reset the
870 xe_watchdog(struct ifnet *ifp) {
871 struct xe_softc *scp = ifp->if_softc;
873 if_printf(ifp, "watchdog timeout; resetting card\n");
875 ifp->if_oerrors += scp->tx_queued;
883 * Change media selection.
886 xe_media_change(struct ifnet *ifp) {
887 struct xe_softc *scp = ifp->if_softc;
889 IFPRINTF(2, (ifp, "media_change\n"));
891 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
895 * Some card/media combos aren't always possible -- filter those out here.
897 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
898 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
901 xe_setmedia_serialized(scp);
908 * Return current media selection.
911 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
913 IFPRINTF(3, (ifp, "media_status\n"));
915 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
916 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
917 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
922 xe_setmedia(void *xscp)
924 struct xe_softc *scp = xscp;
926 lwkt_serialize_enter(scp->arpcom.ac_if.if_serializer);
927 xe_setmedia_serialized(xscp);
928 lwkt_serialize_exit(scp->arpcom.ac_if.if_serializer);
932 * Select active media.
936 xe_setmedia_serialized(void *xscp)
938 struct xe_softc *scp = xscp;
939 u_int16_t bmcr, bmsr, anar, lpar;
941 IFPRINTF(2, (scp->ifp, "setmedia\n"));
943 /* Cancel any pending timeout */
944 callout_stop(&scp->xe_timer);
945 xe_disable_intr(scp);
948 scp->media = IFM_ETHER;
949 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
951 case IFM_AUTO: /* Autoselect media */
952 scp->media = IFM_ETHER|IFM_AUTO;
955 * Autoselection is really awful. It goes something like this:
957 * Wait until the transmitter goes idle (2sec timeout).
959 * IF a 100Mbit PHY exists
960 * Start NWAY autonegotiation (3.5sec timeout)
962 * Select 100baseTX or 10baseT, whichever was detected
965 * IF a 100Mbit PHY exists
966 * Try to force a 100baseTX link (3sec timeout)
975 * IF nothing selected so far
976 * IF a 100Mbit PHY exists
979 * Select 10baseT or 10base2, whichever is connected
983 switch (scp->autoneg_status) {
985 case XE_AUTONEG_NONE:
986 IFPRINTF(2, (scp->ifp, "Waiting for idle transmitter\n"));
987 scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
988 scp->autoneg_status = XE_AUTONEG_WAITING;
991 case XE_AUTONEG_WAITING:
992 if (scp->tx_queued != 0) {
993 callout_reset(&scp->xe_timer, hz / 2, xe_setmedia, scp);
997 IFPRINTF(2, (scp->ifp, "Starting autonegotiation\n"));
998 bmcr = xe_phy_readreg(scp, PHY_BMCR);
999 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1000 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1001 anar = xe_phy_readreg(scp, PHY_ANAR);
1002 anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
1003 anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
1004 xe_phy_writereg(scp, PHY_ANAR, anar);
1005 bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
1006 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1007 scp->autoneg_status = XE_AUTONEG_STARTED;
1008 callout_reset(&scp->xe_timer, hz * 7 / 2, xe_setmedia, scp);
1012 scp->autoneg_status = XE_AUTONEG_FAIL;
1016 case XE_AUTONEG_STARTED:
1017 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1018 lpar = xe_phy_readreg(scp, PHY_LPAR);
1019 if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
1020 IFPRINTF(2, (scp->ifp, "Autonegotiation complete!\n"));
1022 * XXX - Shouldn't have to do this, but (on my hub at least) the
1023 * XXX - transmitter won't work after a successful autoneg. So we see
1024 * XXX - what the negotiation result was and force that mode. I'm
1025 * XXX - sure there is an easy fix for this.
1027 if (lpar & PHY_LPAR_100BTXHALF) {
1028 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1031 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1032 scp->media = IFM_ETHER|IFM_100_TX;
1033 scp->autoneg_status = XE_AUTONEG_NONE;
1037 * XXX - Bit of a hack going on in here.
1038 * XXX - This is derived from Ken Hughes patch to the Linux driver
1039 * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
1040 * XXX - cards. What's a CE3B and how's it differ from a plain CE3?
1041 * XXX - these are the things we need to find out.
1043 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1046 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1047 XE_SELECT_PAGE(0x42);
1048 XE_OUTB(XE_SWC1, 0x80);
1049 scp->media = IFM_ETHER|IFM_10_T;
1050 scp->autoneg_status = XE_AUTONEG_NONE;
1052 /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/ /* Disable PHY? */
1053 /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1057 IFPRINTF(2, (scp->ifp, "Autonegotiation failed; trying 100baseTX\n"));
1060 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1061 scp->autoneg_status = XE_AUTONEG_100TX;
1062 callout_reset(&scp->xe_timer, hz * 3, xe_setmedia, scp);
1066 scp->autoneg_status = XE_AUTONEG_FAIL;
1071 case XE_AUTONEG_100TX:
1072 (void)xe_phy_readreg(scp, PHY_BMSR);
1073 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1074 if (bmsr & PHY_BMSR_LINKSTAT) {
1075 IFPRINTF(2, (scp->ifp, "Got 100baseTX link!\n"));
1078 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1079 scp->media = IFM_ETHER|IFM_100_TX;
1080 scp->autoneg_status = XE_AUTONEG_NONE;
1083 IFPRINTF(2, (scp->ifp, "Autonegotiation failed; disabling PHY\n"));
1085 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1087 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY? */
1088 scp->autoneg_status = XE_AUTONEG_FAIL;
1094 * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1095 * either autonegotiation failed, or never got started to begin with. In
1096 * either case, select a suitable 10Mbit media and hope it works. We
1097 * don't need to reset the card again, since it will have been done
1098 * already by the big switch above.
1100 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1101 IFPRINTF(2, (scp->ifp, "Selecting 10baseX\n"));
1103 XE_SELECT_PAGE(0x42);
1104 XE_OUTB(XE_SWC1, 0x80);
1105 scp->media = IFM_ETHER|IFM_10_T;
1106 scp->autoneg_status = XE_AUTONEG_NONE;
1110 XE_OUTB(XE_GPR0, 4);
1112 XE_SELECT_PAGE(0x42);
1113 XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
1114 scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1115 scp->autoneg_status = XE_AUTONEG_NONE;
1122 * If a specific media has been requested, we just reset the card and
1123 * select it (one small exception -- if 100baseTX is requested by there is
1124 * no PHY, we fall back to 10baseT operation).
1126 case IFM_100_TX: /* Force 100baseTX */
1128 IFPRINTF(2, (scp->ifp, "Selecting 100baseTX\n"));
1129 XE_SELECT_PAGE(0x42);
1130 XE_OUTB(XE_SWC1, 0);
1131 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1133 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1134 scp->media |= IFM_100_TX;
1139 case IFM_10_T: /* Force 10baseT */
1140 IFPRINTF(2, (scp->ifp, "Selecting 10baseT\n"));
1142 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1144 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY */
1146 XE_SELECT_PAGE(0x42);
1147 XE_OUTB(XE_SWC1, 0x80);
1148 scp->media |= IFM_10_T;
1152 IFPRINTF(2, (scp->ifp, "Selecting 10base2\n"));
1153 XE_SELECT_PAGE(0x42);
1154 XE_OUTB(XE_SWC1, 0xc0);
1155 scp->media |= IFM_10_2;
1161 * Finally, the LEDs are set to match whatever media was chosen and the
1162 * transmitter is unblocked.
1164 IFPRINTF(2, (scp->ifp, "Setting LEDs\n"));
1166 switch (IFM_SUBTYPE(scp->media)) {
1169 XE_OUTB(XE_LED, 0x3b);
1171 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1175 XE_OUTB(XE_LED, 0x3a);
1179 /* Restart output? */
1180 xe_enable_intr(scp);
1181 scp->ifp->if_flags &= ~IFF_OACTIVE;
1187 * Hard reset (power cycle) the card.
1190 xe_reset(struct xe_softc *scp) {
1191 IFPRINTF(2, (scp->ifp, "hard_reset\n"));
1197 XE_OUTB(XE_GPR1, 0);
1200 /* Power up again */
1202 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1204 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
1214 * Take interface offline. This is done by powering down the device, which I
1215 * assume means just shutting down the transceiver and Ethernet logic. This
1216 * requires a _hard_ reset to recover from, as we need to power up again.
1219 xe_stop(struct xe_softc *scp) {
1220 IFPRINTF(2, (scp->ifp, "stop\n"));
1225 * Shut off interrupts.
1227 xe_disable_intr(scp);
1233 XE_OUTB(XE_GPR1, 0);
1237 * set GP1 and GP2 as outputs (bits 2 & 3)
1238 * set GP1 high to power on the ML6692 (bit 0)
1239 * set GP2 low to power on the 10Mhz chip (bit 1)
1242 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP1_OUT);
1246 * ~IFF_RUNNING == interface down.
1248 scp->ifp->if_flags &= ~IFF_RUNNING;
1249 scp->ifp->if_flags &= ~IFF_OACTIVE;
1250 scp->ifp->if_timer = 0;
1257 * Enable interrupts from the card.
1260 xe_enable_intr(struct xe_softc *scp) {
1261 IFPRINTF(2, (scp->ifp, "enable_intr\n"));
1264 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1265 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1266 if (!(XE_INB(0x10) & 0x01)) {
1267 XE_OUTB(0x10, 0x11); /* Unmask master int enable bit */
1274 * Disable interrupts from the card.
1277 xe_disable_intr(struct xe_softc *scp) {
1278 IFPRINTF(2, (scp->ifp, "disable_intr\n"));
1281 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1282 if (scp->modem && !scp->dingo) { /* More magic */
1283 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1289 * Set up multicast filter and promiscuous modes.
1292 xe_set_multicast(struct xe_softc *scp) {
1294 struct ifmultiaddr *ifma;
1297 ifp = &scp->arpcom.ac_if;
1299 IFPRINTF(2, (ifp, "set_multicast\n"));
1301 XE_SELECT_PAGE(0x42);
1303 /* Handle PROMISC flag */
1304 if (ifp->if_flags & IFF_PROMISC) {
1305 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1309 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1311 /* Handle ALLMULTI flag */
1312 if (ifp->if_flags & IFF_ALLMULTI) {
1313 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1317 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1319 /* Iterate over multicast address list */
1321 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1322 if (ifma->ifma_addr->sa_family != AF_LINK)
1328 /* First 9 use Individual Addresses for exact matching */
1329 xe_set_addr(scp, LLADDR((struct sockaddr_dl *)ifma->ifma_addr), count);
1332 /* Use hash filter on Mohawk and Dingo */
1333 xe_set_hash(scp, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1335 /* Nowhere else to put them on CE2 */
1339 IFPRINTF(2, (ifp, "set_multicast: count = %u\n", count));
1341 /* Now do some cleanup and enable multicast handling as needed */
1343 /* Disable all multicast handling */
1345 XE_SELECT_PAGE(0x42);
1346 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~(XE_SWC1_IA_ENABLE|XE_SWC1_ALLMULTI));
1348 XE_SELECT_PAGE(0x02);
1349 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1352 else if (count < 10) {
1353 /* Full in any unused Individual Addresses with our MAC address */
1354 for (i = count + 1; i < 10; i++)
1355 xe_set_addr(scp, (u_int8_t *)(&scp->arpcom.ac_enaddr), i);
1356 /* Enable Individual Address matching only */
1357 XE_SELECT_PAGE(0x42);
1358 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1360 XE_SELECT_PAGE(0x02);
1361 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1366 /* Check whether hash table is full */
1367 XE_SELECT_PAGE(0x58);
1368 for (i = 0x08; i < 0x10; i++)
1369 if (XE_INB(i) != 0xff)
1372 /* Hash table full - enable promiscuous multicast matching */
1373 XE_SELECT_PAGE(0x42);
1374 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1375 XE_SELECT_PAGE(0x02);
1376 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1379 /* Enable hash table and Individual Address matching */
1380 XE_SELECT_PAGE(0x42);
1381 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1382 XE_SELECT_PAGE(0x02);
1383 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1387 /* Enable promiscuous multicast matching */
1388 XE_SELECT_PAGE(0x42);
1389 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1397 * Copy the Ethernet multicast address in addr to the on-chip registers for
1398 * Individual Address idx. Assumes that addr is really a multicast address
1399 * and that idx > 0 (slot 0 is always used for the card MAC address).
1402 xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
1407 * Individual Addresses are stored in registers 8-F of pages 0x50-0x57. IA1
1408 * therefore starts at register 0xE on page 0x50. The expressions below
1409 * compute the starting page and register for any IA index > 0.
1412 page = 0x50 + idx%4 + idx/4*3;
1413 reg = 0x0e - 2 * (idx%4);
1415 IFPRINTF(3, (scp->ifp, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
1419 * Copy the IA bytes. Note that the byte order is reversed for Mohawk and
1420 * Dingo wrt. CE2 hardware.
1422 XE_SELECT_PAGE(page);
1423 for (i = 0; i < 6; i++) {
1426 DPRINTF(3, (":%02x", addr[i]));
1428 IFPRINTF(3, (scp->ifp, "set_addr: %02x", addr[0]));
1431 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1432 if (++reg == 0x10) {
1434 XE_SELECT_PAGE(++page);
1442 * Set the appropriate bit in the multicast hash table for the supplied
1443 * Ethernet multicast address addr. Assumes that addr is really a multicast
1447 xe_set_hash(struct xe_softc* scp, u_int8_t* addr) {
1448 u_int32_t crc = 0xffffffff;
1449 u_int8_t bit, byte, crc31, idx;
1452 /* Compute CRC of the address -- standard Ethernet CRC function */
1453 for (i = 0; i < 6; i++) {
1455 for (j = 1; j <= 8; j++) {
1456 if (crc & 0x80000000)
1460 bit = crc31 ^ (byte & 0x01);
1464 crc = (crc ^ XE_CRC_POLY)|1;
1468 IFPRINTF(3, (scp->ifp, "set_hash: CRC = 0x%08x\n", crc));
1470 /* Hash table index = 6 msbs of CRC, reversed */
1471 for (i = 0, idx = 0; i < 6; i++) {
1473 if (crc & 0x80000000) {
1479 /* Top 3 bits of idx give register - 8, bottom 3 give bit within register */
1480 byte = idx >> 3 | 0x08;
1481 bit = 0x01 << (idx & 0x07);
1483 IFPRINTF(3, (scp->ifp,
1484 "set_hash: idx = 0x%02x, byte = 0x%02x, bit = 0x%02x\n",
1487 XE_SELECT_PAGE(0x58);
1488 XE_OUTB(byte, XE_INB(byte) | bit);
1493 * Write an outgoing packet to the card using programmed I/O.
1496 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
1500 u_int8_t savebyte[2];
1502 /* Get total packet length */
1503 if (mbp->m_flags & M_PKTHDR)
1504 len = mbp->m_pkthdr.len;
1506 struct mbuf* mbp2 = mbp;
1507 for (len = 0; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
1510 IFPRINTF(3, (scp->ifp, "pio_write_packet: len = %u\n", len));
1512 /* Packets < minimum length may need to be padded out */
1514 if (len < scp->tx_min) {
1515 pad = scp->tx_min - len;
1519 /* Check transmit buffer space */
1521 XE_OUTW(XE_TRS, len+2); /* Only effective on rev. 1 CE2 cards */
1522 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1525 /* Send packet length to card */
1526 XE_OUTW(XE_EDP, len);
1529 * Write packet to card using PIO (code stolen from the ed driver)
1532 while (mbp != NULL) {
1535 data = mtod(mbp, caddr_t);
1536 if (wantbyte) { /* Finish the last word */
1537 savebyte[1] = *data;
1538 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1543 if (len > 1) { /* Output contiguous words */
1544 bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1549 if (len == 1) { /* Save last byte, if necessary */
1550 savebyte[0] = *data;
1558 * Send last byte of odd-length packets
1561 XE_OUTB(XE_EDP, savebyte[0]);
1564 * Can just tell CE3 cards to send; short packets will be padded out with
1565 * random cruft automatically. For CE2, manually pad the packet with
1566 * garbage; it will be sent when the required number or bytes have been
1567 * delivered to the card.
1570 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX | XE_CR_ENABLE_INTR);
1573 XE_OUTB(XE_EDP, 0xaa);
1576 XE_OUTW(XE_EDP, 0xdead);
1585 /**************************************************************
1587 * M I I F U N C T I O N S *
1589 **************************************************************/
1592 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1593 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1594 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1595 * XXX - some kind of common MII-handling code, shared by all drivers. But
1596 * XXX - that's a whole other mission.
1598 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1599 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1603 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1606 xe_mii_sync(struct xe_softc *scp) {
1610 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1612 for (i = 0; i < 32; i++) {
1613 XE_MII_SET(XE_MII_CLK);
1615 XE_MII_CLR(XE_MII_CLK);
1622 * Look for a MII-compliant PHY. If we find one, reset it.
1625 xe_mii_init(struct xe_softc *scp) {
1628 status = xe_phy_readreg(scp, PHY_BMSR);
1629 if ((status & 0xff00) != 0x7800) {
1630 IFPRINTF(2, (scp->ifp, "no PHY found, %0x\n", status));
1634 IFPRINTF(2, (scp->ifp, "PHY OK!\n"));
1637 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1639 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1647 * Clock a series of bits through the MII.
1650 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
1654 XE_MII_CLR(XE_MII_CLK);
1656 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1658 XE_MII_SET(XE_MII_WRD);
1660 XE_MII_CLR(XE_MII_WRD);
1663 XE_MII_CLR(XE_MII_CLK);
1665 XE_MII_SET(XE_MII_CLK);
1671 * Read an PHY register through the MII.
1674 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1680 * Set up frame for RX.
1682 frame->mii_stdelim = XE_MII_STARTDELIM;
1683 frame->mii_opcode = XE_MII_READOP;
1684 frame->mii_turnaround = 0;
1685 frame->mii_data = 0;
1688 XE_OUTB(XE_GPR2, 0);
1691 * Turn on data xmit.
1693 XE_MII_SET(XE_MII_DIR);
1698 * Send command/address info.
1700 xe_mii_send(scp, frame->mii_stdelim, 2);
1701 xe_mii_send(scp, frame->mii_opcode, 2);
1702 xe_mii_send(scp, frame->mii_phyaddr, 5);
1703 xe_mii_send(scp, frame->mii_regaddr, 5);
1706 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1708 XE_MII_SET(XE_MII_CLK);
1711 /* Turn off xmit. */
1712 XE_MII_CLR(XE_MII_DIR);
1715 XE_MII_CLR(XE_MII_CLK);
1717 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1718 XE_MII_SET(XE_MII_CLK);
1722 * Now try reading data bits. If the ack failed, we still
1723 * need to clock through 16 cycles to keep the PHY(s) in sync.
1726 for(i = 0; i < 16; i++) {
1727 XE_MII_CLR(XE_MII_CLK);
1729 XE_MII_SET(XE_MII_CLK);
1735 for (i = 0x8000; i; i >>= 1) {
1736 XE_MII_CLR(XE_MII_CLK);
1739 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1740 frame->mii_data |= i;
1743 XE_MII_SET(XE_MII_CLK);
1749 XE_MII_CLR(XE_MII_CLK);
1751 XE_MII_SET(XE_MII_CLK);
1763 * Write to a PHY register through the MII.
1766 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1771 * Set up frame for TX.
1773 frame->mii_stdelim = XE_MII_STARTDELIM;
1774 frame->mii_opcode = XE_MII_WRITEOP;
1775 frame->mii_turnaround = XE_MII_TURNAROUND;
1780 * Turn on data output.
1782 XE_MII_SET(XE_MII_DIR);
1786 xe_mii_send(scp, frame->mii_stdelim, 2);
1787 xe_mii_send(scp, frame->mii_opcode, 2);
1788 xe_mii_send(scp, frame->mii_phyaddr, 5);
1789 xe_mii_send(scp, frame->mii_regaddr, 5);
1790 xe_mii_send(scp, frame->mii_turnaround, 2);
1791 xe_mii_send(scp, frame->mii_data, 16);
1794 XE_MII_SET(XE_MII_CLK);
1796 XE_MII_CLR(XE_MII_CLK);
1802 XE_MII_CLR(XE_MII_DIR);
1811 * Read a register from the PHY.
1814 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
1815 struct xe_mii_frame frame;
1817 bzero((char *)&frame, sizeof(frame));
1819 frame.mii_phyaddr = 0;
1820 frame.mii_regaddr = reg;
1821 xe_mii_readreg(scp, &frame);
1823 return(frame.mii_data);
1828 * Write to a PHY register.
1831 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
1832 struct xe_mii_frame frame;
1834 bzero((char *)&frame, sizeof(frame));
1836 frame.mii_phyaddr = 0;
1837 frame.mii_regaddr = reg;
1838 frame.mii_data = data;
1839 xe_mii_writereg(scp, &frame);
1846 * A bit of debugging code.
1849 xe_mii_dump(struct xe_softc *scp) {
1854 if_printf(scp->ifp, "MII registers: ");
1855 for (i = 0; i < 2; i++) {
1856 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1858 for (i = 4; i < 7; i++) {
1859 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1867 xe_reg_dump(struct xe_softc *scp) {
1872 if_printf(scp->ifp, "Common registers: ");
1873 for (i = 0; i < 8; i++) {
1874 printf(" %2.2x", XE_INB(i));
1878 for (page = 0; page <= 8; page++) {
1879 if_printf(scp->ifp, "Register page %2.2x: ", page);
1880 XE_SELECT_PAGE(page);
1881 for (i = 8; i < 16; i++) {
1882 printf(" %2.2x", XE_INB(i));
1887 for (page = 0x10; page < 0x5f; page++) {
1888 if ((page >= 0x11 && page <= 0x3f) ||
1890 (page >= 0x43 && page <= 0x4f) ||
1893 if_printf(scp->ifp, "Register page %2.2x: ", page);
1894 XE_SELECT_PAGE(page);
1895 for (i = 8; i < 16; i++) {
1896 printf(" %2.2x", XE_INB(i));
1905 xe_activate(device_t dev)
1907 struct xe_softc *sc = device_get_softc(dev);
1910 DEVPRINTF(2, (dev, "activate\n"));
1913 sc->port_rid = 0; /* 0 is managed by pccard */
1914 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1915 &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
1916 } else if (sc->dingo) {
1918 * Find a 16 byte aligned ioport for the card.
1920 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1921 sc->port_rid = 1; /* 0 is managed by pccard */
1924 sc->port_res = bus_alloc_resource(dev,
1925 SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
1927 if (sc->port_res == 0)
1928 break; /* we failed */
1929 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1931 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1933 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1935 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
1936 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1937 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1941 * Find contiguous I/O port for the Ethernet function on CEM2 and
1942 * CEM3 cards. We allocate window 0 wherever pccard has decided
1943 * it should be, then find an available window adjacent to it for
1944 * the second function. Not sure that both windows are actually
1947 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
1948 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
1949 sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1950 &sc->ce2_port_rid, 0, ~0,
1952 if (!sc->ce2_port_res) {
1953 device_printf(dev, "Cannot allocate I/O port for modem\n");
1958 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
1960 for (i = 0; i < 2; i++) {
1961 start += (i == 0 ? 8 : -24);
1962 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1963 &sc->port_rid, start,
1964 start + 18, 18, RF_ACTIVE);
1965 if (sc->port_res == 0)
1966 continue; /* Failed, try again if possible */
1967 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
1968 sc->port_rid) == start)
1969 break; /* Success! */
1971 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1975 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
1976 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1977 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1979 if (!sc->port_res) {
1980 device_printf(dev, "Cannot allocate ioport\n");
1985 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1988 device_printf(dev, "Cannot allocate irq\n");
1993 sc->bst = rman_get_bustag(sc->port_res);
1994 sc->bsh = rman_get_bushandle(sc->port_res);
1999 xe_deactivate(device_t dev)
2001 struct xe_softc *sc = device_get_softc(dev);
2003 DEVPRINTF(2, (dev, "deactivate\n"));
2004 xe_disable_intr(sc);
2007 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2010 if (sc->ce2_port_res)
2011 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2013 sc->ce2_port_res = 0;
2015 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,