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
27 * Portions of this software were derived from Werner Koch's xirc2ps driver
28 * for Linux under the terms of the following license (from v1.30 of the
31 * Copyright (c) 1997 by Werner Koch (dd9jn)
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, and the entire permission notice in its entirety,
38 * including the disclaimer of warranties.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The name of the author may not be used to endorse or promote
43 * products derived from this software without specific prior
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56 * OF THE POSSIBILITY OF SUCH DAMAGE.
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
63 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
64 * following cards are currently known to work with the driver:
65 * Xircom CreditCard 10/100 (CE3)
66 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
67 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
68 * Xircom RealPort Ethernet 10
69 * Xircom RealPort Ethernet 10/100
70 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
71 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
72 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
74 * Some other cards *should* work, but support for them is either broken or in
75 * an unknown state at the moment. I'm always interested in hearing from
76 * people who own any of these cards:
77 * Xircom CreditCard 10Base-T (PS-CE2-10)
78 * Xircom CreditCard Ethernet + ModemII (CEM2)
79 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
81 * Thanks to all who assisted with the development and testing of the driver,
82 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
83 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
84 * out anyone who deserves a mention here.
86 * Special thanks to Ade Lovett for both hosting the mailing list and doing
87 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
90 * Author email: <scott@uk.freebsd.org>
91 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
95 #include <sys/param.h>
96 #include <sys/cdefs.h>
97 #include <sys/errno.h>
98 #include <sys/kernel.h>
100 #include <sys/socket.h>
101 #include <sys/sockio.h>
102 #include <sys/systm.h>
104 #include <sys/sysctl.h>
106 #include <sys/module.h>
109 #include <machine/bus.h>
110 #include <machine/resource.h>
111 #include <sys/rman.h>
113 #include <net/ethernet.h>
115 #include <net/if_arp.h>
116 #include <net/if_dl.h>
117 #include <net/if_media.h>
118 #include <net/if_mib.h>
120 #include <net/if_types.h>
122 #include <dev/xe/if_xereg.h>
123 #include <dev/xe/if_xevar.h>
126 * MII command structure
128 struct xe_mii_frame {
133 uint8_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 * Prototypes start here
149 static void xe_init(void *xscp);
150 static void xe_init_locked(struct xe_softc *scp);
151 static void xe_start(struct ifnet *ifp);
152 static void xe_start_locked(struct ifnet *ifp);
153 static int xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
154 static void xe_watchdog(void *arg);
155 static void xe_intr(void *xscp);
156 static void xe_txintr(struct xe_softc *scp, uint8_t txst1);
157 static void xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0,
159 static void xe_rxintr(struct xe_softc *scp, uint8_t rst0);
160 static int xe_media_change(struct ifnet *ifp);
161 static void xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp);
162 static void xe_setmedia(void *arg);
163 static void xe_reset(struct xe_softc *scp);
164 static void xe_enable_intr(struct xe_softc *scp);
165 static void xe_disable_intr(struct xe_softc *scp);
166 static void xe_set_multicast(struct xe_softc *scp);
167 static void xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx);
168 static void xe_mchash(struct xe_softc *scp, const uint8_t *addr);
169 static int xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp);
174 static void xe_mii_sync(struct xe_softc *scp);
175 static int xe_mii_init(struct xe_softc *scp);
176 static void xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt);
177 static int xe_mii_readreg(struct xe_softc *scp,
178 struct xe_mii_frame *frame);
179 static int xe_mii_writereg(struct xe_softc *scp,
180 struct xe_mii_frame *frame);
181 static uint16_t xe_phy_readreg(struct xe_softc *scp, uint16_t reg);
182 static void xe_phy_writereg(struct xe_softc *scp, uint16_t reg,
186 * Debugging functions
188 static void xe_mii_dump(struct xe_softc *scp);
190 static void xe_reg_dump(struct xe_softc *scp);
194 * Debug logging levels - set with hw.xe.debug sysctl
196 * 1 = More hardware details, probe/attach progress
197 * 2 = Most function calls, ioctls and media selection progress
198 * 3 = Everything - interrupts, packets in/out and multicast address setup
204 static SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
207 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0,
208 "if_xe debug level");
210 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
211 #define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
212 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
214 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
217 #define DEVPRINTF(level, arg)
218 #define DPRINTF(level, arg)
219 #define XE_MII_DUMP(scp)
221 #define XE_REG_DUMP(scp)
229 xe_attach(device_t dev)
231 struct xe_softc *scp = device_get_softc(dev);
234 DEVPRINTF(2, (dev, "attach\n"));
236 /* Initialise stuff... */
238 scp->ifp = if_alloc(IFT_ETHER);
239 if (scp->ifp == NULL)
241 scp->ifm = &scp->ifmedia;
242 scp->autoneg_status = XE_AUTONEG_NONE;
243 mtx_init(&scp->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
245 callout_init_mtx(&scp->wdog_timer, &scp->lock, 0);
247 /* Initialise the ifnet structure */
248 scp->ifp->if_softc = scp;
249 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
250 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
251 scp->ifp->if_linkmib = &scp->mibdata;
252 scp->ifp->if_linkmiblen = sizeof(scp->mibdata);
253 scp->ifp->if_start = xe_start;
254 scp->ifp->if_ioctl = xe_ioctl;
255 scp->ifp->if_init = xe_init;
256 scp->ifp->if_baudrate = 100000000;
257 IFQ_SET_MAXLEN(&scp->ifp->if_snd, ifqmaxlen);
259 /* Initialise the ifmedia structure */
260 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
261 callout_init_mtx(&scp->media_timer, &scp->lock, 0);
263 /* Add supported media types */
265 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
266 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
267 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
269 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
271 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
272 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
274 /* Default is to autoselect best supported media type */
275 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
277 /* Get the hardware into a known state */
282 /* Get hardware version numbers */
284 scp->version = XE_INB(XE_BOV);
286 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
288 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
290 /* Print some useful information */
291 device_printf(dev, "version 0x%02x/0x%02x%s%s\n", scp->version,
292 scp->srev, scp->mohawk ? ", 100Mbps capable" : "",
293 scp->modem ? ", with modem" : "");
295 XE_SELECT_PAGE(0x10);
297 "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
298 XE_INW(XE_DINGOID), XE_INW(XE_RevID), XE_INW(XE_VendorID)));
301 XE_SELECT_PAGE(0x45);
302 DEVPRINTF(1, (dev, "CE2 version = 0x%02x\n", XE_INB(XE_REV)));
305 /* Attach the interface */
306 ether_ifattach(scp->ifp, scp->enaddr);
308 err = bus_setup_intr(dev, scp->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
309 NULL, xe_intr, scp, &scp->intrhand);
311 ether_ifdetach(scp->ifp);
312 mtx_destroy(&scp->lock);
321 * Complete hardware intitialisation and enable output. Exits without doing
322 * anything if there's no address assigned to the card, or if media selection
323 * is in progress (the latter implies we've already run this function).
328 struct xe_softc *scp = xscp;
336 xe_init_locked(struct xe_softc *scp)
340 if (scp->autoneg_status != XE_AUTONEG_NONE)
343 DEVPRINTF(2, (scp->dev, "init\n"));
345 /* Reset transmitter flags */
348 scp->tx_timeouts = 0;
350 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
353 /* Soft reset the card */
355 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
362 * set GP1 and GP2 as outputs (bits 2 & 3)
363 * set GP1 low to power on the ML6692 (bit 0)
364 * set GP2 high to power on the 10Mhz chip (bit 1)
367 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
371 /* Shut off interrupts */
372 xe_disable_intr(scp);
374 /* Wait for everything to wake up */
379 scp->phy_ok = xe_mii_init(scp);
381 /* Disable 'source insertion' (not sure what that means) */
382 XE_SELECT_PAGE(0x42);
383 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
385 /* Set 8K/24K Tx/Rx buffer split */
386 if (scp->srev != 1) {
388 XE_OUTW(XE_RBS, 0x2000);
391 /* Enable early transmit mode on Mohawk/Dingo */
393 XE_SELECT_PAGE(0x03);
394 XE_OUTW(XE_TPT, scp->tx_thres);
395 XE_SELECT_PAGE(0x01);
396 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
399 /* Put MAC address in first 'individual address' register */
400 XE_SELECT_PAGE(0x50);
401 for (i = 0; i < ETHER_ADDR_LEN; i++)
402 XE_OUTB(0x08 + i, IF_LLADDR(scp->ifp)[scp->mohawk ? 5 - i : i]);
404 /* Set up multicast addresses */
405 xe_set_multicast(scp);
407 /* Fix the receive data offset -- reset can leave it off-by-one */
409 XE_OUTW(XE_DO, 0x2000);
411 /* Set interrupt masks */
413 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR |
416 /* Set MAC interrupt masks */
417 XE_SELECT_PAGE(0x40);
419 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR | XE_RX0M_ALIGN_ERROR |
420 XE_RX0M_LONG_PACKET));
422 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN |
423 XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
425 /* Clear MAC status registers */
426 XE_SELECT_PAGE(0x40);
427 XE_OUTB(XE_RST0, 0x00);
428 XE_OUTB(XE_TXST0, 0x00);
430 /* Enable receiver and put MAC online */
431 XE_SELECT_PAGE(0x40);
432 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
434 /* Set up IMR, enable interrupts */
437 /* Start media selection */
441 scp->ifp->if_drv_flags |= IFF_DRV_RUNNING;
442 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
443 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
447 * Start output on interface. Should be called at splimp() priority. Check
448 * that the output is idle (ie, IFF_DRV_OACTIVE is not set) before calling this
449 * function. If media selection is in progress we set IFF_DRV_OACTIVE ourselves
450 * and return immediately.
453 xe_start(struct ifnet *ifp)
455 struct xe_softc *scp = ifp->if_softc;
458 xe_start_locked(ifp);
463 xe_start_locked(struct ifnet *ifp)
465 struct xe_softc *scp = ifp->if_softc;
468 if (scp->autoneg_status != XE_AUTONEG_NONE) {
469 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
473 DEVPRINTF(3, (scp->dev, "start\n"));
476 * Loop while there are packets to be sent, and space to send
480 /* Suck a packet off the send queue */
481 IF_DEQUEUE(&ifp->if_snd, mbp);
485 * We are using the !OACTIVE flag to indicate
486 * to the outside world that we can accept an
487 * additional packet rather than that the
488 * transmitter is _actually_ active. Indeed,
489 * the transmitter may be active, but if we
490 * haven't filled all the buffers with data
491 * then we still want to accept more.
493 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
497 if (xe_pio_write_packet(scp, mbp) != 0) {
498 /* Push the packet back onto the queue */
499 IF_PREPEND(&ifp->if_snd, mbp);
500 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
504 /* Tap off here if there is a bpf listener */
507 /* In case we don't hear from the card again... */
516 * Process an ioctl request. Adapted from the ed driver.
519 xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
521 struct xe_softc *scp;
529 DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n",
532 * If the interface is marked up and stopped, then
533 * start it. If it is marked down and running, then
537 if (ifp->if_flags & IFF_UP) {
538 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
543 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
547 /* handle changes to PROMISC/ALLMULTI flags */
548 xe_set_multicast(scp);
554 DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
556 * Multicast list has (maybe) changed; set the
557 * hardware filters accordingly.
560 xe_set_multicast(scp);
566 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
568 * Someone wants to get/set media options.
570 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia,
574 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
575 error = ether_ioctl(ifp, command, data);
582 * Card interrupt handler.
584 * This function is probably more complicated than it needs to be, as it
585 * attempts to deal with the case where multiple packets get sent between
586 * interrupts. This is especially annoying when working out the collision
587 * stats. Not sure whether this case ever really happens or not (maybe on a
588 * slow/heavily loaded machine?) so it's probably best to leave this like it
591 * Note that the crappy PIO used to get packets on and off the card means that
592 * you will spend a lot of time in this routine -- I can get my P150 to spend
593 * 90% of its time servicing interrupts if I really hammer the network. Could
594 * fix this, but then you'd start dropping/losing packets. The moral of this
595 * story? If you want good network performance _and_ some cycles left over to
596 * get your work done, don't buy a Xircom card. Or convince them to tell me
597 * how to do memory-mapped I/O :)
600 xe_txintr(struct xe_softc *scp, uint8_t txst1)
603 uint8_t tpr, sent, coll;
607 /* Update packet count, accounting for rollover */
608 tpr = XE_INB(XE_TPR);
609 sent = -scp->tx_tpr + tpr;
611 /* Update statistics if we actually sent anything */
613 coll = txst1 & XE_TXST1_RETRY_COUNT;
615 scp->tx_queued -= sent;
616 ifp->if_opackets += sent;
617 ifp->if_collisions += coll;
620 * According to the Xircom manual, Dingo will
621 * sometimes manage to transmit a packet with
622 * triggering an interrupt. If this happens, we have
623 * sent > 1 and the collision count only reflects
624 * collisions on the last packet sent (the one that
625 * triggered the interrupt). Collision stats might
626 * therefore be a bit low, but there doesn't seem to
627 * be anything we can do about that.
633 scp->mibdata.dot3StatsSingleCollisionFrames++;
634 scp->mibdata.dot3StatsCollFrequencies[0]++;
637 scp->mibdata.dot3StatsMultipleCollisionFrames++;
638 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
642 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
645 /* Handle most MAC interrupts */
647 xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0, uint8_t txst1)
654 /* Carrier sense lost -- only in 10Mbit HDX mode */
655 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
656 /* XXX - Need to update media status here */
657 device_printf(scp->dev, "no carrier\n");
659 scp->mibdata.dot3StatsCarrierSenseErrors++;
662 /* Excessive collisions -- try sending again */
663 if (txst0 & XE_TXST0_16_COLLISIONS) {
664 ifp->if_collisions += 16;
666 scp->mibdata.dot3StatsExcessiveCollisions++;
667 scp->mibdata.dot3StatsMultipleCollisionFrames++;
668 scp->mibdata.dot3StatsCollFrequencies[15]++;
669 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
672 /* Transmit underrun -- increase early transmit threshold */
673 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
674 DEVPRINTF(1, (scp->dev, "transmit underrun"));
675 if (scp->tx_thres < ETHER_MAX_LEN) {
676 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
677 scp->tx_thres = ETHER_MAX_LEN;
678 DPRINTF(1, (": increasing transmit threshold to %u",
681 XE_OUTW(XE_TPT, scp->tx_thres);
686 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
689 /* Late collision -- just complain about it */
690 if (txst0 & XE_TXST0_LATE_COLLISION) {
691 device_printf(scp->dev, "late collision\n");
693 scp->mibdata.dot3StatsLateCollisions++;
696 /* SQE test failure -- just complain about it */
697 if (txst0 & XE_TXST0_SQE_FAIL) {
698 device_printf(scp->dev, "SQE test failure\n");
700 scp->mibdata.dot3StatsSQETestErrors++;
703 /* Packet too long -- what happens to these */
704 if (rst0 & XE_RST0_LONG_PACKET) {
705 device_printf(scp->dev, "received giant packet\n");
707 scp->mibdata.dot3StatsFrameTooLongs++;
710 /* CRC error -- packet dropped */
711 if (rst0 & XE_RST0_CRC_ERROR) {
712 device_printf(scp->dev, "CRC error\n");
714 scp->mibdata.dot3StatsFCSErrors++;
719 xe_rxintr(struct xe_softc *scp, uint8_t rst0)
726 /* Handle received packet(s) */
727 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
728 rsr = XE_INB(XE_RSR);
730 DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr,
733 /* Make sure packet is a good one */
734 if (rsr & XE_RSR_RX_OK) {
735 struct ether_header *ehp;
739 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
741 DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n",
750 * Allocate mbuf to hold received packet. If
751 * the mbuf header isn't big enough, we attach
752 * an mbuf cluster to hold the packet. Note
753 * the +=2 to align the packet data on a
754 * 32-bit boundary, and the +3 to allow for
755 * the possibility of reading one more byte
756 * than the actual packet length (we always
757 * read 16-bit words). XXX - Surely there's a
758 * better way to do this alignment?
760 MGETHDR(mbp, M_NOWAIT, MT_DATA);
766 if (len + 3 > MHLEN) {
767 MCLGET(mbp, M_NOWAIT);
768 if ((mbp->m_flags & M_EXT) == 0) {
776 ehp = mtod(mbp, struct ether_header *);
779 * Now get the packet in PIO mode, including
780 * the Ethernet header but omitting the
785 * Work around a bug in CE2 cards. There
786 * seems to be a problem with duplicated and
787 * extraneous bytes in the receive buffer, but
788 * without any real documentation for the CE2
789 * it's hard to tell for sure. XXX - Needs
790 * testing on CE2 hardware
792 if (scp->srev == 0) {
796 rhs = XE_INW(XE_RHSA);
799 rhs += 3; /* Skip control info */
804 if (rhs + len > 0x8000) {
807 for (i = 0; i < len; i++, rhs++) {
816 bus_read_multi_2(scp->port_res, XE_EDP,
817 (uint16_t *)ehp, (len + 1) >> 1);
819 bus_read_multi_2(scp->port_res, XE_EDP,
820 (uint16_t *)ehp, (len + 1) >> 1);
822 /* Deliver packet to upper layers */
823 mbp->m_pkthdr.rcvif = ifp;
824 mbp->m_pkthdr.len = mbp->m_len = len;
826 (*ifp->if_input)(ifp, mbp);
830 } else if (rsr & XE_RSR_ALIGN_ERROR) {
831 /* Packet alignment error -- drop packet */
832 device_printf(scp->dev, "alignment error\n");
833 scp->mibdata.dot3StatsAlignmentErrors++;
837 /* Skip to next packet, if there is one */
838 XE_OUTW(XE_DO, 0x8000);
841 /* Clear receiver overruns now we have some free buffer space */
842 if (rst0 & XE_RST0_RX_OVERRUN) {
843 DEVPRINTF(1, (scp->dev, "receive overrun\n"));
845 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
846 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
853 struct xe_softc *scp = (struct xe_softc *) xscp;
855 uint8_t psr, isr, rst0, txst0, txst1;
860 /* Disable interrupts */
864 /* Cache current register page */
867 /* Read ISR to see what caused this interrupt */
868 while ((isr = XE_INB(XE_ISR)) != 0) {
870 /* 0xff might mean the card is no longer around */
872 DEVPRINTF(3, (scp->dev,
873 "intr: interrupt received for missing card?\n"));
877 /* Read other status registers */
878 XE_SELECT_PAGE(0x40);
879 rst0 = XE_INB(XE_RST0);
881 txst0 = XE_INB(XE_TXST0);
882 txst1 = XE_INB(XE_TXST1);
883 XE_OUTB(XE_TXST0, 0);
884 XE_OUTB(XE_TXST1, 0);
887 DEVPRINTF(3, (scp->dev,
888 "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x\n", isr,
889 rst0, txst1, txst0));
891 if (isr & XE_ISR_TX_PACKET)
892 xe_txintr(scp, txst1);
894 if (isr & XE_ISR_MAC_INTR)
895 xe_macintr(scp, rst0, txst0, txst1);
897 xe_rxintr(scp, rst0);
900 /* Restore saved page */
903 /* Re-enable interrupts */
904 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
910 * Device timeout/watchdog routine. Called automatically if we queue a packet
911 * for transmission but don't get an interrupt within a specified timeout
912 * (usually 5 seconds). When this happens we assume the worst and reset the
916 xe_watchdog(void *arg)
918 struct xe_softc *scp = arg;
920 XE_ASSERT_LOCKED(scp);
922 if (scp->tx_timeout && --scp->tx_timeout == 0) {
923 device_printf(scp->dev, "watchdog timeout: resetting card\n");
925 scp->ifp->if_oerrors += scp->tx_queued;
930 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
934 * Change media selection.
937 xe_media_change(struct ifnet *ifp)
939 struct xe_softc *scp = ifp->if_softc;
941 DEVPRINTF(2, (scp->dev, "media_change\n"));
944 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER) {
950 * Some card/media combos aren't always possible -- filter
953 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
954 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok) {
966 * Return current media selection.
969 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp)
971 struct xe_softc *scp = ifp->if_softc;
973 DEVPRINTF(3, (scp->dev, "media_status\n"));
975 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
977 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
978 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
983 * Select active media.
986 xe_setmedia(void *xscp)
988 struct xe_softc *scp = xscp;
989 uint16_t bmcr, bmsr, anar, lpar;
991 DEVPRINTF(2, (scp->dev, "setmedia\n"));
993 XE_ASSERT_LOCKED(scp);
995 /* Cancel any pending timeout */
996 callout_stop(&scp->media_timer);
997 xe_disable_intr(scp);
1000 scp->media = IFM_ETHER;
1001 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
1003 case IFM_AUTO: /* Autoselect media */
1004 scp->media = IFM_ETHER|IFM_AUTO;
1007 * Autoselection is really awful. It goes something like this:
1009 * Wait until the transmitter goes idle (2sec timeout).
1011 * IF a 100Mbit PHY exists
1012 * Start NWAY autonegotiation (3.5sec timeout)
1014 * Select 100baseTX or 10baseT, whichever was detected
1017 * IF a 100Mbit PHY exists
1018 * Try to force a 100baseTX link (3sec timeout)
1027 * IF nothing selected so far
1028 * IF a 100Mbit PHY exists
1031 * Select 10baseT or 10base2, whichever is connected
1035 switch (scp->autoneg_status) {
1036 case XE_AUTONEG_NONE:
1037 DEVPRINTF(2, (scp->dev,
1038 "Waiting for idle transmitter\n"));
1039 scp->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1040 scp->autoneg_status = XE_AUTONEG_WAITING;
1042 case XE_AUTONEG_WAITING:
1043 if (scp->tx_queued != 0) {
1044 callout_reset(&scp->media_timer, hz / 2,
1049 DEVPRINTF(2, (scp->dev,
1050 "Starting autonegotiation\n"));
1051 bmcr = xe_phy_readreg(scp, PHY_BMCR);
1052 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1053 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1054 anar = xe_phy_readreg(scp, PHY_ANAR);
1055 anar &= ~(PHY_ANAR_100BT4 |
1056 PHY_ANAR_100BTXFULL | PHY_ANAR_10BTFULL);
1057 anar |= PHY_ANAR_100BTXHALF | PHY_ANAR_10BTHALF;
1058 xe_phy_writereg(scp, PHY_ANAR, anar);
1059 bmcr |= PHY_BMCR_AUTONEGENBL |
1060 PHY_BMCR_AUTONEGRSTR;
1061 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1062 scp->autoneg_status = XE_AUTONEG_STARTED;
1063 callout_reset(&scp->media_timer, hz * 7/2,
1067 scp->autoneg_status = XE_AUTONEG_FAIL;
1070 case XE_AUTONEG_STARTED:
1071 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1072 lpar = xe_phy_readreg(scp, PHY_LPAR);
1073 if (bmsr & (PHY_BMSR_AUTONEGCOMP | PHY_BMSR_LINKSTAT)) {
1074 DEVPRINTF(2, (scp->dev,
1075 "Autonegotiation complete!\n"));
1078 * XXX - Shouldn't have to do this,
1079 * but (on my hub at least) the
1080 * transmitter won't work after a
1081 * successful autoneg. So we see what
1082 * the negotiation result was and
1083 * force that mode. I'm sure there is
1084 * an easy fix for this.
1086 if (lpar & PHY_LPAR_100BTXHALF) {
1087 xe_phy_writereg(scp, PHY_BMCR,
1091 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1092 scp->media = IFM_ETHER | IFM_100_TX;
1093 scp->autoneg_status = XE_AUTONEG_NONE;
1096 * XXX - Bit of a hack going
1097 * on in here. This is
1098 * derived from Ken Hughes
1099 * patch to the Linux driver
1100 * to make it work with 10Mbit
1101 * _autonegotiated_ links on
1102 * CE3B cards. What's a CE3B
1103 * and how's it differ from a
1104 * plain CE3? these are the
1105 * things we need to find out.
1107 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1110 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1111 XE_SELECT_PAGE(0x42);
1112 XE_OUTB(XE_SWC1, 0x80);
1113 scp->media = IFM_ETHER | IFM_10_T;
1114 scp->autoneg_status = XE_AUTONEG_NONE;
1118 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1119 scp->autoneg_status = XE_AUTONEG_FAIL;
1123 DEVPRINTF(2, (scp->dev,
1124 "Autonegotiation failed; trying 100baseTX\n"));
1127 xe_phy_writereg(scp, PHY_BMCR,
1129 scp->autoneg_status = XE_AUTONEG_100TX;
1130 callout_reset(&scp->media_timer, hz * 3,
1134 scp->autoneg_status = XE_AUTONEG_FAIL;
1138 case XE_AUTONEG_100TX:
1139 (void)xe_phy_readreg(scp, PHY_BMSR);
1140 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1141 if (bmsr & PHY_BMSR_LINKSTAT) {
1142 DEVPRINTF(2, (scp->dev,
1143 "Got 100baseTX link!\n"));
1146 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1147 scp->media = IFM_ETHER | IFM_100_TX;
1148 scp->autoneg_status = XE_AUTONEG_NONE;
1150 DEVPRINTF(2, (scp->dev,
1151 "Autonegotiation failed; disabling PHY\n"));
1153 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1157 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1158 scp->autoneg_status = XE_AUTONEG_FAIL;
1164 * If we got down here _and_ autoneg_status is
1165 * XE_AUTONEG_FAIL, then either autonegotiation
1166 * failed, or never got started to begin with. In
1167 * either case, select a suitable 10Mbit media and
1168 * hope it works. We don't need to reset the card
1169 * again, since it will have been done already by the
1172 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1173 DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
1175 XE_SELECT_PAGE(0x42);
1176 XE_OUTB(XE_SWC1, 0x80);
1177 scp->media = IFM_ETHER | IFM_10_T;
1178 scp->autoneg_status = XE_AUTONEG_NONE;
1181 XE_OUTB(XE_GPR0, 4);
1183 XE_SELECT_PAGE(0x42);
1185 (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ?
1187 scp->media = IFM_ETHER | ((XE_INB(XE_ESR) &
1188 XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1189 scp->autoneg_status = XE_AUTONEG_NONE;
1195 * If a specific media has been requested, we just reset the
1196 * card and select it (one small exception -- if 100baseTX is
1197 * requested but there is no PHY, we fall back to 10baseT
1200 case IFM_100_TX: /* Force 100baseTX */
1202 DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
1203 XE_SELECT_PAGE(0x42);
1204 XE_OUTB(XE_SWC1, 0);
1205 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1207 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1208 scp->media |= IFM_100_TX;
1212 case IFM_10_T: /* Force 10baseT */
1213 DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
1215 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1219 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1221 XE_SELECT_PAGE(0x42);
1222 XE_OUTB(XE_SWC1, 0x80);
1223 scp->media |= IFM_10_T;
1226 DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
1227 XE_SELECT_PAGE(0x42);
1228 XE_OUTB(XE_SWC1, 0xc0);
1229 scp->media |= IFM_10_2;
1234 * Finally, the LEDs are set to match whatever media was
1235 * chosen and the transmitter is unblocked.
1237 DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
1239 switch (IFM_SUBTYPE(scp->media)) {
1242 XE_OUTB(XE_LED, 0x3b);
1244 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1247 XE_OUTB(XE_LED, 0x3a);
1251 /* Restart output? */
1252 xe_enable_intr(scp);
1253 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1254 xe_start_locked(scp->ifp);
1258 * Hard reset (power cycle) the card.
1261 xe_reset(struct xe_softc *scp)
1264 DEVPRINTF(2, (scp->dev, "reset\n"));
1266 XE_ASSERT_LOCKED(scp);
1270 XE_OUTB(XE_GPR1, 0);
1273 /* Power up again */
1275 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1277 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN | XE_GPR1_AIC);
1284 * Take interface offline. This is done by powering down the device, which I
1285 * assume means just shutting down the transceiver and Ethernet logic. This
1286 * requires a _hard_ reset to recover from, as we need to power up again.
1289 xe_stop(struct xe_softc *scp)
1292 DEVPRINTF(2, (scp->dev, "stop\n"));
1294 XE_ASSERT_LOCKED(scp);
1297 * Shut off interrupts.
1299 xe_disable_intr(scp);
1305 XE_OUTB(XE_GPR1, 0);
1309 * set GP1 and GP2 as outputs (bits 2 & 3)
1310 * set GP1 high to power on the ML6692 (bit 0)
1311 * set GP2 low to power on the 10Mhz chip (bit 1)
1314 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
1319 * ~IFF_DRV_RUNNING == interface down.
1321 scp->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1322 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1323 scp->tx_timeout = 0;
1324 callout_stop(&scp->wdog_timer);
1325 callout_stop(&scp->media_timer);
1329 * Enable interrupts from the card.
1332 xe_enable_intr(struct xe_softc *scp)
1335 DEVPRINTF(2, (scp->dev, "enable_intr\n"));
1338 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1339 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1340 if (!(XE_INB(0x10) & 0x01)) {
1341 XE_OUTB(0x10, 0x11); /* Unmask master int enable */
1347 * Disable interrupts from the card.
1350 xe_disable_intr(struct xe_softc *scp)
1353 DEVPRINTF(2, (scp->dev, "disable_intr\n"));
1356 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1357 if (scp->modem && !scp->dingo) { /* More magic */
1358 XE_OUTB(0x10, 0x10); /* Mask the master int enable */
1363 * Set up multicast filter and promiscuous modes.
1366 xe_set_multicast(struct xe_softc *scp)
1369 struct ifmultiaddr *maddr;
1372 DEVPRINTF(2, (scp->dev, "set_multicast\n"));
1375 XE_SELECT_PAGE(0x42);
1377 /* Handle PROMISC flag */
1378 if (ifp->if_flags & IFF_PROMISC) {
1379 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1382 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1384 /* Handle ALLMULTI flag */
1385 if (ifp->if_flags & IFF_ALLMULTI) {
1386 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1389 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1391 /* Iterate over multicast address list */
1393 if_maddr_rlock(ifp);
1394 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1395 if (maddr->ifma_addr->sa_family != AF_LINK)
1402 * First 9 use Individual Addresses for exact
1406 LLADDR((struct sockaddr_dl *)maddr->ifma_addr),
1408 else if (scp->mohawk)
1409 /* Use hash filter on Mohawk and Dingo */
1411 LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
1413 /* Nowhere else to put them on CE2 */
1416 if_maddr_runlock(ifp);
1418 DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
1420 /* Now do some cleanup and enable multicast handling as needed */
1422 /* Disable all multicast handling */
1423 XE_SELECT_PAGE(0x42);
1424 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) &
1425 ~(XE_SWC1_IA_ENABLE | XE_SWC1_ALLMULTI));
1427 XE_SELECT_PAGE(0x02);
1428 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1430 } else if (count < 10) {
1432 * Full in any unused Individual Addresses with our
1435 for (i = count + 1; i < 10; i++)
1436 xe_set_addr(scp, IF_LLADDR(scp->ifp), i);
1438 /* Enable Individual Address matching only */
1439 XE_SELECT_PAGE(0x42);
1440 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1443 XE_SELECT_PAGE(0x02);
1444 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1446 } else if (scp->mohawk) {
1447 /* Check whether hash table is full */
1448 XE_SELECT_PAGE(0x58);
1449 for (i = 0x08; i < 0x10; i++)
1450 if (XE_INB(i) != 0xff)
1454 * Hash table full - enable
1455 * promiscuous multicast matching
1457 XE_SELECT_PAGE(0x42);
1458 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) &
1459 ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1460 XE_SELECT_PAGE(0x02);
1461 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1463 /* Enable hash table and Individual Address matching */
1464 XE_SELECT_PAGE(0x42);
1465 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1467 XE_SELECT_PAGE(0x02);
1468 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1471 /* Enable promiscuous multicast matching */
1472 XE_SELECT_PAGE(0x42);
1473 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) |
1481 * Copy the Ethernet multicast address in addr to the on-chip registers for
1482 * Individual Address idx. Assumes that addr is really a multicast address
1483 * and that idx > 0 (slot 0 is always used for the card MAC address).
1486 xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx)
1492 * Individual Addresses are stored in registers 8-F of pages
1493 * 0x50-0x57. IA1 therefore starts at register 0xE on page
1494 * 0x50. The expressions below compute the starting page and
1495 * register for any IA index > 0.
1498 page = 0x50 + idx % 4 + idx / 4 * 3;
1499 reg = 0x0e - 2 * (idx % 4);
1501 DEVPRINTF(3, (scp->dev,
1502 "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n", idx + 1, page,
1506 * Copy the IA bytes. Note that the byte order is reversed
1507 * for Mohawk and Dingo wrt. CE2 hardware.
1509 XE_SELECT_PAGE(page);
1510 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1512 DPRINTF(3, (":%02x", addr[i]));
1514 DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
1516 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1517 if (++reg == 0x10) {
1519 XE_SELECT_PAGE(++page);
1526 * Set the appropriate bit in the multicast hash table for the supplied
1527 * Ethernet multicast address addr. Assumes that addr is really a multicast
1531 xe_mchash(struct xe_softc* scp, const uint8_t *addr)
1536 hash = ether_crc32_le(addr, ETHER_ADDR_LEN) & 0x3F;
1539 * Top 3 bits of hash give register - 8, bottom 3 give bit
1542 byte = hash >> 3 | 0x08;
1543 bit = 0x01 << (hash & 0x07);
1545 DEVPRINTF(3, (scp->dev,
1546 "set_hash: hash = 0x%02x, byte = 0x%02x, bit = 0x%02x\n", hash,
1549 XE_SELECT_PAGE(0x58);
1550 XE_OUTB(byte, XE_INB(byte) | bit);
1554 * Write an outgoing packet to the card using programmed I/O.
1557 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp)
1560 unsigned char wantbyte;
1562 uint8_t savebyte[2];
1564 /* Get total packet length */
1565 if (mbp->m_flags & M_PKTHDR)
1566 len = mbp->m_pkthdr.len;
1568 struct mbuf* mbp2 = mbp;
1569 for (len = 0; mbp2 != NULL;
1570 len += mbp2->m_len, mbp2 = mbp2->m_next);
1573 DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
1575 /* Packets < minimum length may need to be padded out */
1577 if (len < scp->tx_min) {
1578 pad = scp->tx_min - len;
1582 /* Check transmit buffer space */
1584 XE_OUTW(XE_TRS, len + 2); /* Only effective on rev. 1 CE2 cards */
1585 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1588 /* Send packet length to card */
1589 XE_OUTW(XE_EDP, len);
1592 * Write packet to card using PIO (code stolen from the ed driver)
1595 while (mbp != NULL) {
1598 data = mtod(mbp, caddr_t);
1599 if (wantbyte) { /* Finish the last word */
1600 savebyte[1] = *data;
1601 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1606 if (len > 1) { /* Output contiguous words */
1607 bus_write_multi_2(scp->port_res, XE_EDP,
1608 (uint16_t *)data, len >> 1);
1612 if (len == 1) { /* Save last byte, if needed */
1613 savebyte[0] = *data;
1621 * Send last byte of odd-length packets
1624 XE_OUTB(XE_EDP, savebyte[0]);
1627 * Can just tell CE3 cards to send; short packets will be
1628 * padded out with random cruft automatically. For CE2,
1629 * manually pad the packet with garbage; it will be sent when
1630 * the required number of bytes have been delivered to the
1634 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX |
1638 XE_OUTB(XE_EDP, 0xaa);
1641 XE_OUTW(XE_EDP, 0xdead);
1649 /**************************************************************
1651 * M I I F U N C T I O N S *
1653 **************************************************************/
1656 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1657 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1658 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1659 * XXX - some kind of common MII-handling code, shared by all drivers. But
1660 * XXX - that's a whole other mission.
1662 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1663 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1666 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1669 xe_mii_sync(struct xe_softc *scp)
1674 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1676 for (i = 0; i < 32; i++) {
1677 XE_MII_SET(XE_MII_CLK);
1679 XE_MII_CLR(XE_MII_CLK);
1685 * Look for a MII-compliant PHY. If we find one, reset it.
1688 xe_mii_init(struct xe_softc *scp)
1692 status = xe_phy_readreg(scp, PHY_BMSR);
1693 if ((status & 0xff00) != 0x7800) {
1694 DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
1697 DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
1700 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1702 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET)
1710 * Clock a series of bits through the MII.
1713 xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt)
1718 XE_MII_CLR(XE_MII_CLK);
1720 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1722 XE_MII_SET(XE_MII_WRD);
1724 XE_MII_CLR(XE_MII_WRD);
1727 XE_MII_CLR(XE_MII_CLK);
1729 XE_MII_SET(XE_MII_CLK);
1734 * Read an PHY register through the MII.
1737 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame)
1741 XE_ASSERT_LOCKED(scp);
1744 * Set up frame for RX.
1746 frame->mii_stdelim = XE_MII_STARTDELIM;
1747 frame->mii_opcode = XE_MII_READOP;
1748 frame->mii_turnaround = 0;
1749 frame->mii_data = 0;
1752 XE_OUTB(XE_GPR2, 0);
1755 * Turn on data xmit.
1757 XE_MII_SET(XE_MII_DIR);
1762 * Send command/address info.
1764 xe_mii_send(scp, frame->mii_stdelim, 2);
1765 xe_mii_send(scp, frame->mii_opcode, 2);
1766 xe_mii_send(scp, frame->mii_phyaddr, 5);
1767 xe_mii_send(scp, frame->mii_regaddr, 5);
1770 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1772 XE_MII_SET(XE_MII_CLK);
1775 /* Turn off xmit. */
1776 XE_MII_CLR(XE_MII_DIR);
1779 XE_MII_CLR(XE_MII_CLK);
1781 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1782 XE_MII_SET(XE_MII_CLK);
1786 * Now try reading data bits. If the ack failed, we still
1787 * need to clock through 16 cycles to keep the PHY(s) in sync.
1790 for(i = 0; i < 16; i++) {
1791 XE_MII_CLR(XE_MII_CLK);
1793 XE_MII_SET(XE_MII_CLK);
1799 for (i = 0x8000; i; i >>= 1) {
1800 XE_MII_CLR(XE_MII_CLK);
1803 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1804 frame->mii_data |= i;
1807 XE_MII_SET(XE_MII_CLK);
1812 XE_MII_CLR(XE_MII_CLK);
1814 XE_MII_SET(XE_MII_CLK);
1823 * Write to a PHY register through the MII.
1826 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame)
1829 XE_ASSERT_LOCKED(scp);
1832 * Set up frame for TX.
1834 frame->mii_stdelim = XE_MII_STARTDELIM;
1835 frame->mii_opcode = XE_MII_WRITEOP;
1836 frame->mii_turnaround = XE_MII_TURNAROUND;
1841 * Turn on data output.
1843 XE_MII_SET(XE_MII_DIR);
1847 xe_mii_send(scp, frame->mii_stdelim, 2);
1848 xe_mii_send(scp, frame->mii_opcode, 2);
1849 xe_mii_send(scp, frame->mii_phyaddr, 5);
1850 xe_mii_send(scp, frame->mii_regaddr, 5);
1851 xe_mii_send(scp, frame->mii_turnaround, 2);
1852 xe_mii_send(scp, frame->mii_data, 16);
1855 XE_MII_SET(XE_MII_CLK);
1857 XE_MII_CLR(XE_MII_CLK);
1863 XE_MII_CLR(XE_MII_DIR);
1869 * Read a register from the PHY.
1872 xe_phy_readreg(struct xe_softc *scp, uint16_t reg)
1874 struct xe_mii_frame frame;
1876 bzero((char *)&frame, sizeof(frame));
1878 frame.mii_phyaddr = 0;
1879 frame.mii_regaddr = reg;
1880 xe_mii_readreg(scp, &frame);
1882 return (frame.mii_data);
1886 * Write to a PHY register.
1889 xe_phy_writereg(struct xe_softc *scp, uint16_t reg, uint16_t data)
1891 struct xe_mii_frame frame;
1893 bzero((char *)&frame, sizeof(frame));
1895 frame.mii_phyaddr = 0;
1896 frame.mii_regaddr = reg;
1897 frame.mii_data = data;
1898 xe_mii_writereg(scp, &frame);
1902 * A bit of debugging code.
1905 xe_mii_dump(struct xe_softc *scp)
1909 device_printf(scp->dev, "MII registers: ");
1910 for (i = 0; i < 2; i++) {
1911 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1913 for (i = 4; i < 7; i++) {
1914 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1921 xe_reg_dump(struct xe_softc *scp)
1925 device_printf(scp->dev, "Common registers: ");
1926 for (i = 0; i < 8; i++) {
1927 printf(" %2.2x", XE_INB(i));
1931 for (page = 0; page <= 8; page++) {
1932 device_printf(scp->dev, "Register page %2.2x: ", page);
1933 XE_SELECT_PAGE(page);
1934 for (i = 8; i < 16; i++) {
1935 printf(" %2.2x", XE_INB(i));
1940 for (page = 0x10; page < 0x5f; page++) {
1941 if ((page >= 0x11 && page <= 0x3f) ||
1943 (page >= 0x43 && page <= 0x4f) ||
1946 device_printf(scp->dev, "Register page %2.2x: ", page);
1947 XE_SELECT_PAGE(page);
1948 for (i = 8; i < 16; i++) {
1949 printf(" %2.2x", XE_INB(i));
1957 xe_activate(device_t dev)
1959 struct xe_softc *sc = device_get_softc(dev);
1962 DEVPRINTF(2, (dev, "activate\n"));
1965 sc->port_rid = 0; /* 0 is managed by pccard */
1966 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1967 &sc->port_rid, 0ul, ~0ul, 16, RF_ACTIVE);
1968 } else if (sc->dingo) {
1970 * Find a 16 byte aligned ioport for the card.
1972 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1973 sc->port_rid = 1; /* 0 is managed by pccard */
1976 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1977 &sc->port_rid, start, 0x3ff, 16, RF_ACTIVE);
1978 if (sc->port_res == NULL)
1980 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1982 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1984 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1986 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
1987 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1988 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1989 } else if (sc->ce2) {
1991 * Find contiguous I/O port for the Ethernet function
1992 * on CEM2 and CEM3 cards. We allocate window 0
1993 * wherever pccard has decided it should be, then find
1994 * an available window adjacent to it for the second
1995 * function. Not sure that both windows are actually
1998 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
1999 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
2000 sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
2001 &sc->ce2_port_rid, 0ul, ~0ul, 8, RF_ACTIVE);
2002 if (sc->ce2_port_res == NULL) {
2004 "Cannot allocate I/O port for modem\n"));
2010 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
2012 for (i = 0; i < 2; i++) {
2013 start += (i == 0 ? 8 : -24);
2014 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
2015 &sc->port_rid, start, start + 15, 16, RF_ACTIVE);
2016 if (sc->port_res == NULL)
2018 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
2019 sc->port_rid) == start)
2022 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2024 sc->port_res = NULL;
2026 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
2027 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
2028 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
2031 if (!sc->port_res) {
2032 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
2038 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2040 if (sc->irq_res == NULL) {
2041 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
2050 xe_deactivate(device_t dev)
2052 struct xe_softc *sc = device_get_softc(dev);
2054 DEVPRINTF(2, (dev, "deactivate\n"));
2056 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
2057 sc->intrhand = NULL;
2059 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2061 sc->port_res = NULL;
2062 if (sc->ce2_port_res)
2063 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2065 sc->ce2_port_res = NULL;
2067 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,