2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-3-Clause
4 * Copyright (c) 1998, 1999, 2003 Scott Mitchell
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Portions of this software were derived from Werner Koch's xirc2ps driver
30 * for Linux under the terms of the following license (from v1.30 of the
33 * Copyright (c) 1997 by Werner Koch (dd9jn)
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, and the entire permission notice in its entirety,
40 * including the disclaimer of warranties.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote
45 * products derived from this software without specific prior
48 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
49 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
52 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58 * OF THE POSSIBILITY OF SUCH DAMAGE.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
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/malloc.h>
102 #include <sys/mbuf.h>
103 #include <sys/socket.h>
104 #include <sys/sockio.h>
105 #include <sys/systm.h>
107 #include <sys/sysctl.h>
109 #include <sys/module.h>
112 #include <machine/bus.h>
113 #include <machine/resource.h>
114 #include <sys/rman.h>
116 #include <net/ethernet.h>
118 #include <net/if_var.h>
119 #include <net/if_arp.h>
120 #include <net/if_dl.h>
121 #include <net/if_media.h>
122 #include <net/if_mib.h>
124 #include <net/if_types.h>
126 #include <dev/xe/if_xereg.h>
127 #include <dev/xe/if_xevar.h>
130 * MII command structure
132 struct xe_mii_frame {
137 uint8_t mii_turnaround;
142 * Media autonegotiation progress constants
144 #define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
145 #define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
146 #define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
147 #define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
148 #define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
151 * Prototypes start here
153 static void xe_init(void *xscp);
154 static void xe_init_locked(struct xe_softc *scp);
155 static void xe_start(struct ifnet *ifp);
156 static void xe_start_locked(struct ifnet *ifp);
157 static int xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
158 static void xe_watchdog(void *arg);
159 static void xe_intr(void *xscp);
160 static void xe_txintr(struct xe_softc *scp, uint8_t txst1);
161 static void xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0,
163 static void xe_rxintr(struct xe_softc *scp, uint8_t rst0);
164 static int xe_media_change(struct ifnet *ifp);
165 static void xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp);
166 static void xe_setmedia(void *arg);
167 static void xe_reset(struct xe_softc *scp);
168 static void xe_enable_intr(struct xe_softc *scp);
169 static void xe_disable_intr(struct xe_softc *scp);
170 static void xe_set_multicast(struct xe_softc *scp);
171 static void xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx);
172 static void xe_mchash(struct xe_softc *scp, const uint8_t *addr);
173 static int xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp);
178 static void xe_mii_sync(struct xe_softc *scp);
179 static int xe_mii_init(struct xe_softc *scp);
180 static void xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt);
181 static int xe_mii_readreg(struct xe_softc *scp,
182 struct xe_mii_frame *frame);
183 static int xe_mii_writereg(struct xe_softc *scp,
184 struct xe_mii_frame *frame);
185 static uint16_t xe_phy_readreg(struct xe_softc *scp, uint16_t reg);
186 static void xe_phy_writereg(struct xe_softc *scp, uint16_t reg,
190 * Debugging functions
192 static void xe_mii_dump(struct xe_softc *scp);
194 static void xe_reg_dump(struct xe_softc *scp);
198 * Debug logging levels - set with hw.xe.debug sysctl
200 * 1 = More hardware details, probe/attach progress
201 * 2 = Most function calls, ioctls and media selection progress
202 * 3 = Everything - interrupts, packets in/out and multicast address setup
208 static SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
211 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0,
212 "if_xe debug level");
214 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
215 #define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
216 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
218 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
221 #define DEVPRINTF(level, arg)
222 #define DPRINTF(level, arg)
223 #define XE_MII_DUMP(scp)
225 #define XE_REG_DUMP(scp)
233 xe_attach(device_t dev)
235 struct xe_softc *scp = device_get_softc(dev);
238 DEVPRINTF(2, (dev, "attach\n"));
240 /* Initialise stuff... */
242 scp->ifp = if_alloc(IFT_ETHER);
243 if (scp->ifp == NULL)
245 scp->ifm = &scp->ifmedia;
246 scp->autoneg_status = XE_AUTONEG_NONE;
247 mtx_init(&scp->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
249 callout_init_mtx(&scp->wdog_timer, &scp->lock, 0);
251 /* Initialise the ifnet structure */
252 scp->ifp->if_softc = scp;
253 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
254 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
255 scp->ifp->if_linkmib = &scp->mibdata;
256 scp->ifp->if_linkmiblen = sizeof(scp->mibdata);
257 scp->ifp->if_start = xe_start;
258 scp->ifp->if_ioctl = xe_ioctl;
259 scp->ifp->if_init = xe_init;
260 scp->ifp->if_baudrate = 100000000;
261 IFQ_SET_MAXLEN(&scp->ifp->if_snd, ifqmaxlen);
263 /* Initialise the ifmedia structure */
264 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
265 callout_init_mtx(&scp->media_timer, &scp->lock, 0);
267 /* Add supported media types */
269 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
270 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
271 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
273 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
275 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
276 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
278 /* Default is to autoselect best supported media type */
279 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
281 /* Get the hardware into a known state */
286 /* Get hardware version numbers */
288 scp->version = XE_INB(XE_BOV);
290 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
292 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
294 /* Print some useful information */
295 device_printf(dev, "version 0x%02x/0x%02x%s%s\n", scp->version,
296 scp->srev, scp->mohawk ? ", 100Mbps capable" : "",
297 scp->modem ? ", with modem" : "");
299 XE_SELECT_PAGE(0x10);
301 "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
302 XE_INW(XE_DINGOID), XE_INW(XE_RevID), XE_INW(XE_VendorID)));
305 XE_SELECT_PAGE(0x45);
306 DEVPRINTF(1, (dev, "CE2 version = 0x%02x\n", XE_INB(XE_REV)));
309 /* Attach the interface */
310 ether_ifattach(scp->ifp, scp->enaddr);
312 err = bus_setup_intr(dev, scp->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
313 NULL, xe_intr, scp, &scp->intrhand);
315 ether_ifdetach(scp->ifp);
316 mtx_destroy(&scp->lock);
325 * Complete hardware intitialisation and enable output. Exits without doing
326 * anything if there's no address assigned to the card, or if media selection
327 * is in progress (the latter implies we've already run this function).
332 struct xe_softc *scp = xscp;
340 xe_init_locked(struct xe_softc *scp)
344 if (scp->autoneg_status != XE_AUTONEG_NONE)
347 DEVPRINTF(2, (scp->dev, "init\n"));
349 /* Reset transmitter flags */
352 scp->tx_timeouts = 0;
354 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
357 /* Soft reset the card */
359 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
366 * set GP1 and GP2 as outputs (bits 2 & 3)
367 * set GP1 low to power on the ML6692 (bit 0)
368 * set GP2 high to power on the 10Mhz chip (bit 1)
371 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
375 /* Shut off interrupts */
376 xe_disable_intr(scp);
378 /* Wait for everything to wake up */
383 scp->phy_ok = xe_mii_init(scp);
385 /* Disable 'source insertion' (not sure what that means) */
386 XE_SELECT_PAGE(0x42);
387 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
389 /* Set 8K/24K Tx/Rx buffer split */
390 if (scp->srev != 1) {
392 XE_OUTW(XE_RBS, 0x2000);
395 /* Enable early transmit mode on Mohawk/Dingo */
397 XE_SELECT_PAGE(0x03);
398 XE_OUTW(XE_TPT, scp->tx_thres);
399 XE_SELECT_PAGE(0x01);
400 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
403 /* Put MAC address in first 'individual address' register */
404 XE_SELECT_PAGE(0x50);
405 for (i = 0; i < ETHER_ADDR_LEN; i++)
406 XE_OUTB(0x08 + i, IF_LLADDR(scp->ifp)[scp->mohawk ? 5 - i : i]);
408 /* Set up multicast addresses */
409 xe_set_multicast(scp);
411 /* Fix the receive data offset -- reset can leave it off-by-one */
413 XE_OUTW(XE_DO, 0x2000);
415 /* Set interrupt masks */
417 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR |
420 /* Set MAC interrupt masks */
421 XE_SELECT_PAGE(0x40);
423 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR | XE_RX0M_ALIGN_ERROR |
424 XE_RX0M_LONG_PACKET));
426 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN |
427 XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
429 /* Clear MAC status registers */
430 XE_SELECT_PAGE(0x40);
431 XE_OUTB(XE_RST0, 0x00);
432 XE_OUTB(XE_TXST0, 0x00);
434 /* Enable receiver and put MAC online */
435 XE_SELECT_PAGE(0x40);
436 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
438 /* Set up IMR, enable interrupts */
441 /* Start media selection */
445 scp->ifp->if_drv_flags |= IFF_DRV_RUNNING;
446 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
447 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
451 * Start output on interface. Should be called at splimp() priority. Check
452 * that the output is idle (ie, IFF_DRV_OACTIVE is not set) before calling this
453 * function. If media selection is in progress we set IFF_DRV_OACTIVE ourselves
454 * and return immediately.
457 xe_start(struct ifnet *ifp)
459 struct xe_softc *scp = ifp->if_softc;
462 xe_start_locked(ifp);
467 xe_start_locked(struct ifnet *ifp)
469 struct xe_softc *scp = ifp->if_softc;
472 if (scp->autoneg_status != XE_AUTONEG_NONE) {
473 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
477 DEVPRINTF(3, (scp->dev, "start\n"));
480 * Loop while there are packets to be sent, and space to send
484 /* Suck a packet off the send queue */
485 IF_DEQUEUE(&ifp->if_snd, mbp);
489 * We are using the !OACTIVE flag to indicate
490 * to the outside world that we can accept an
491 * additional packet rather than that the
492 * transmitter is _actually_ active. Indeed,
493 * the transmitter may be active, but if we
494 * haven't filled all the buffers with data
495 * then we still want to accept more.
497 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
501 if (xe_pio_write_packet(scp, mbp) != 0) {
502 /* Push the packet back onto the queue */
503 IF_PREPEND(&ifp->if_snd, mbp);
504 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
508 /* Tap off here if there is a bpf listener */
511 /* 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)
525 struct xe_softc *scp;
533 DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n",
536 * If the interface is marked up and stopped, then
537 * start it. If it is marked down and running, then
541 if (ifp->if_flags & IFF_UP) {
542 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
547 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
551 /* handle changes to PROMISC/ALLMULTI flags */
552 xe_set_multicast(scp);
558 DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
560 * Multicast list has (maybe) changed; set the
561 * hardware filters accordingly.
564 xe_set_multicast(scp);
570 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
572 * Someone wants to get/set media options.
574 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia,
578 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
579 error = ether_ioctl(ifp, command, data);
586 * Card interrupt handler.
588 * This function is probably more complicated than it needs to be, as it
589 * attempts to deal with the case where multiple packets get sent between
590 * interrupts. This is especially annoying when working out the collision
591 * stats. Not sure whether this case ever really happens or not (maybe on a
592 * slow/heavily loaded machine?) so it's probably best to leave this like it
595 * Note that the crappy PIO used to get packets on and off the card means that
596 * you will spend a lot of time in this routine -- I can get my P150 to spend
597 * 90% of its time servicing interrupts if I really hammer the network. Could
598 * fix this, but then you'd start dropping/losing packets. The moral of this
599 * story? If you want good network performance _and_ some cycles left over to
600 * get your work done, don't buy a Xircom card. Or convince them to tell me
601 * how to do memory-mapped I/O :)
604 xe_txintr(struct xe_softc *scp, uint8_t txst1)
607 uint8_t tpr, sent, coll;
611 /* Update packet count, accounting for rollover */
612 tpr = XE_INB(XE_TPR);
613 sent = -scp->tx_tpr + tpr;
615 /* Update statistics if we actually sent anything */
617 coll = txst1 & XE_TXST1_RETRY_COUNT;
619 scp->tx_queued -= sent;
620 if_inc_counter(ifp, IFCOUNTER_OPACKETS, sent);
621 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, coll);
624 * According to the Xircom manual, Dingo will
625 * sometimes manage to transmit a packet with
626 * triggering an interrupt. If this happens, we have
627 * sent > 1 and the collision count only reflects
628 * collisions on the last packet sent (the one that
629 * triggered the interrupt). Collision stats might
630 * therefore be a bit low, but there doesn't seem to
631 * be anything we can do about that.
637 scp->mibdata.dot3StatsSingleCollisionFrames++;
638 scp->mibdata.dot3StatsCollFrequencies[0]++;
641 scp->mibdata.dot3StatsMultipleCollisionFrames++;
642 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
646 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
649 /* Handle most MAC interrupts */
651 xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0, uint8_t txst1)
658 /* Carrier sense lost -- only in 10Mbit HDX mode */
659 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
660 /* XXX - Need to update media status here */
661 device_printf(scp->dev, "no carrier\n");
662 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
663 scp->mibdata.dot3StatsCarrierSenseErrors++;
666 /* Excessive collisions -- try sending again */
667 if (txst0 & XE_TXST0_16_COLLISIONS) {
668 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 16);
669 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
670 scp->mibdata.dot3StatsExcessiveCollisions++;
671 scp->mibdata.dot3StatsMultipleCollisionFrames++;
672 scp->mibdata.dot3StatsCollFrequencies[15]++;
673 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
676 /* Transmit underrun -- increase early transmit threshold */
677 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
678 DEVPRINTF(1, (scp->dev, "transmit underrun"));
679 if (scp->tx_thres < ETHER_MAX_LEN) {
680 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
681 scp->tx_thres = ETHER_MAX_LEN;
682 DPRINTF(1, (": increasing transmit threshold to %u",
685 XE_OUTW(XE_TPT, scp->tx_thres);
689 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
690 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
693 /* Late collision -- just complain about it */
694 if (txst0 & XE_TXST0_LATE_COLLISION) {
695 device_printf(scp->dev, "late collision\n");
696 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
697 scp->mibdata.dot3StatsLateCollisions++;
700 /* SQE test failure -- just complain about it */
701 if (txst0 & XE_TXST0_SQE_FAIL) {
702 device_printf(scp->dev, "SQE test failure\n");
703 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
704 scp->mibdata.dot3StatsSQETestErrors++;
707 /* Packet too long -- what happens to these */
708 if (rst0 & XE_RST0_LONG_PACKET) {
709 device_printf(scp->dev, "received giant packet\n");
710 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
711 scp->mibdata.dot3StatsFrameTooLongs++;
714 /* CRC error -- packet dropped */
715 if (rst0 & XE_RST0_CRC_ERROR) {
716 device_printf(scp->dev, "CRC error\n");
717 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
718 scp->mibdata.dot3StatsFCSErrors++;
723 xe_rxintr(struct xe_softc *scp, uint8_t rst0)
730 /* Handle received packet(s) */
731 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
732 rsr = XE_INB(XE_RSR);
734 DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr,
737 /* Make sure packet is a good one */
738 if (rsr & XE_RSR_RX_OK) {
739 struct ether_header *ehp;
743 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
745 DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n",
749 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
754 * Allocate mbuf to hold received packet. If
755 * the mbuf header isn't big enough, we attach
756 * an mbuf cluster to hold the packet. Note
757 * the +=2 to align the packet data on a
758 * 32-bit boundary, and the +3 to allow for
759 * the possibility of reading one more byte
760 * than the actual packet length (we always
761 * read 16-bit words). XXX - Surely there's a
762 * better way to do this alignment?
764 MGETHDR(mbp, M_NOWAIT, MT_DATA);
766 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
770 if (len + 3 > MHLEN) {
771 if (!(MCLGET(mbp, M_NOWAIT))) {
773 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
779 ehp = mtod(mbp, struct ether_header *);
782 * Now get the packet in PIO mode, including
783 * the Ethernet header but omitting the
788 * Work around a bug in CE2 cards. There
789 * seems to be a problem with duplicated and
790 * extraneous bytes in the receive buffer, but
791 * without any real documentation for the CE2
792 * it's hard to tell for sure. XXX - Needs
793 * testing on CE2 hardware
795 if (scp->srev == 0) {
799 rhs = XE_INW(XE_RHSA);
802 rhs += 3; /* Skip control info */
807 if (rhs + len > 0x8000) {
810 for (i = 0; i < len; i++, rhs++) {
819 bus_read_multi_2(scp->port_res, XE_EDP,
820 (uint16_t *)ehp, (len + 1) >> 1);
822 bus_read_multi_2(scp->port_res, XE_EDP,
823 (uint16_t *)ehp, (len + 1) >> 1);
825 /* Deliver packet to upper layers */
826 mbp->m_pkthdr.rcvif = ifp;
827 mbp->m_pkthdr.len = mbp->m_len = len;
829 (*ifp->if_input)(ifp, mbp);
831 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
833 } else if (rsr & XE_RSR_ALIGN_ERROR) {
834 /* Packet alignment error -- drop packet */
835 device_printf(scp->dev, "alignment error\n");
836 scp->mibdata.dot3StatsAlignmentErrors++;
837 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
840 /* Skip to next packet, if there is one */
841 XE_OUTW(XE_DO, 0x8000);
844 /* Clear receiver overruns now we have some free buffer space */
845 if (rst0 & XE_RST0_RX_OVERRUN) {
846 DEVPRINTF(1, (scp->dev, "receive overrun\n"));
847 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
848 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
849 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
856 struct xe_softc *scp = (struct xe_softc *) xscp;
858 uint8_t psr, isr, rst0, txst0, txst1;
863 /* Disable interrupts */
867 /* Cache current register page */
870 /* Read ISR to see what caused this interrupt */
871 while ((isr = XE_INB(XE_ISR)) != 0) {
873 /* 0xff might mean the card is no longer around */
875 DEVPRINTF(3, (scp->dev,
876 "intr: interrupt received for missing card?\n"));
880 /* Read other status registers */
881 XE_SELECT_PAGE(0x40);
882 rst0 = XE_INB(XE_RST0);
884 txst0 = XE_INB(XE_TXST0);
885 txst1 = XE_INB(XE_TXST1);
886 XE_OUTB(XE_TXST0, 0);
887 XE_OUTB(XE_TXST1, 0);
890 DEVPRINTF(3, (scp->dev,
891 "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x\n", isr,
892 rst0, txst1, txst0));
894 if (isr & XE_ISR_TX_PACKET)
895 xe_txintr(scp, txst1);
897 if (isr & XE_ISR_MAC_INTR)
898 xe_macintr(scp, rst0, txst0, txst1);
900 xe_rxintr(scp, rst0);
903 /* Restore saved page */
906 /* Re-enable interrupts */
907 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
913 * Device timeout/watchdog routine. Called automatically if we queue a packet
914 * for transmission but don't get an interrupt within a specified timeout
915 * (usually 5 seconds). When this happens we assume the worst and reset the
919 xe_watchdog(void *arg)
921 struct xe_softc *scp = arg;
923 XE_ASSERT_LOCKED(scp);
925 if (scp->tx_timeout && --scp->tx_timeout == 0) {
926 device_printf(scp->dev, "watchdog timeout: resetting card\n");
928 if_inc_counter(scp->ifp, IFCOUNTER_OERRORS, scp->tx_queued);
933 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
937 * Change media selection.
940 xe_media_change(struct ifnet *ifp)
942 struct xe_softc *scp = ifp->if_softc;
944 DEVPRINTF(2, (scp->dev, "media_change\n"));
947 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER) {
953 * Some card/media combos aren't always possible -- filter
956 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
957 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok) {
969 * Return current media selection.
972 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp)
974 struct xe_softc *scp = ifp->if_softc;
976 DEVPRINTF(3, (scp->dev, "media_status\n"));
978 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
980 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
981 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
986 * Select active media.
989 xe_setmedia(void *xscp)
991 struct xe_softc *scp = xscp;
992 uint16_t bmcr, bmsr, anar, lpar;
994 DEVPRINTF(2, (scp->dev, "setmedia\n"));
996 XE_ASSERT_LOCKED(scp);
998 /* Cancel any pending timeout */
999 callout_stop(&scp->media_timer);
1000 xe_disable_intr(scp);
1003 scp->media = IFM_ETHER;
1004 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
1006 case IFM_AUTO: /* Autoselect media */
1007 scp->media = IFM_ETHER|IFM_AUTO;
1010 * Autoselection is really awful. It goes something like this:
1012 * Wait until the transmitter goes idle (2sec timeout).
1014 * IF a 100Mbit PHY exists
1015 * Start NWAY autonegotiation (3.5sec timeout)
1017 * Select 100baseTX or 10baseT, whichever was detected
1020 * IF a 100Mbit PHY exists
1021 * Try to force a 100baseTX link (3sec timeout)
1030 * IF nothing selected so far
1031 * IF a 100Mbit PHY exists
1034 * Select 10baseT or 10base2, whichever is connected
1038 switch (scp->autoneg_status) {
1039 case XE_AUTONEG_NONE:
1040 DEVPRINTF(2, (scp->dev,
1041 "Waiting for idle transmitter\n"));
1042 scp->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1043 scp->autoneg_status = XE_AUTONEG_WAITING;
1045 case XE_AUTONEG_WAITING:
1046 if (scp->tx_queued != 0) {
1047 callout_reset(&scp->media_timer, hz / 2,
1052 DEVPRINTF(2, (scp->dev,
1053 "Starting autonegotiation\n"));
1054 bmcr = xe_phy_readreg(scp, PHY_BMCR);
1055 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1056 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1057 anar = xe_phy_readreg(scp, PHY_ANAR);
1058 anar &= ~(PHY_ANAR_100BT4 |
1059 PHY_ANAR_100BTXFULL | PHY_ANAR_10BTFULL);
1060 anar |= PHY_ANAR_100BTXHALF | PHY_ANAR_10BTHALF;
1061 xe_phy_writereg(scp, PHY_ANAR, anar);
1062 bmcr |= PHY_BMCR_AUTONEGENBL |
1063 PHY_BMCR_AUTONEGRSTR;
1064 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1065 scp->autoneg_status = XE_AUTONEG_STARTED;
1066 callout_reset(&scp->media_timer, hz * 7/2,
1070 scp->autoneg_status = XE_AUTONEG_FAIL;
1073 case XE_AUTONEG_STARTED:
1074 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1075 lpar = xe_phy_readreg(scp, PHY_LPAR);
1076 if (bmsr & (PHY_BMSR_AUTONEGCOMP | PHY_BMSR_LINKSTAT)) {
1077 DEVPRINTF(2, (scp->dev,
1078 "Autonegotiation complete!\n"));
1081 * XXX - Shouldn't have to do this,
1082 * but (on my hub at least) the
1083 * transmitter won't work after a
1084 * successful autoneg. So we see what
1085 * the negotiation result was and
1086 * force that mode. I'm sure there is
1087 * an easy fix for this.
1089 if (lpar & PHY_LPAR_100BTXHALF) {
1090 xe_phy_writereg(scp, PHY_BMCR,
1094 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1095 scp->media = IFM_ETHER | IFM_100_TX;
1096 scp->autoneg_status = XE_AUTONEG_NONE;
1099 * XXX - Bit of a hack going
1100 * on in here. This is
1101 * derived from Ken Hughes
1102 * patch to the Linux driver
1103 * to make it work with 10Mbit
1104 * _autonegotiated_ links on
1105 * CE3B cards. What's a CE3B
1106 * and how's it differ from a
1107 * plain CE3? these are the
1108 * things we need to find out.
1110 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1113 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1114 XE_SELECT_PAGE(0x42);
1115 XE_OUTB(XE_SWC1, 0x80);
1116 scp->media = IFM_ETHER | IFM_10_T;
1117 scp->autoneg_status = XE_AUTONEG_NONE;
1121 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1122 scp->autoneg_status = XE_AUTONEG_FAIL;
1126 DEVPRINTF(2, (scp->dev,
1127 "Autonegotiation failed; trying 100baseTX\n"));
1130 xe_phy_writereg(scp, PHY_BMCR,
1132 scp->autoneg_status = XE_AUTONEG_100TX;
1133 callout_reset(&scp->media_timer, hz * 3,
1137 scp->autoneg_status = XE_AUTONEG_FAIL;
1141 case XE_AUTONEG_100TX:
1142 (void)xe_phy_readreg(scp, PHY_BMSR);
1143 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1144 if (bmsr & PHY_BMSR_LINKSTAT) {
1145 DEVPRINTF(2, (scp->dev,
1146 "Got 100baseTX link!\n"));
1149 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1150 scp->media = IFM_ETHER | IFM_100_TX;
1151 scp->autoneg_status = XE_AUTONEG_NONE;
1153 DEVPRINTF(2, (scp->dev,
1154 "Autonegotiation failed; disabling PHY\n"));
1156 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1160 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1161 scp->autoneg_status = XE_AUTONEG_FAIL;
1167 * If we got down here _and_ autoneg_status is
1168 * XE_AUTONEG_FAIL, then either autonegotiation
1169 * failed, or never got started to begin with. In
1170 * either case, select a suitable 10Mbit media and
1171 * hope it works. We don't need to reset the card
1172 * again, since it will have been done already by the
1175 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1176 DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
1178 XE_SELECT_PAGE(0x42);
1179 XE_OUTB(XE_SWC1, 0x80);
1180 scp->media = IFM_ETHER | IFM_10_T;
1181 scp->autoneg_status = XE_AUTONEG_NONE;
1184 XE_OUTB(XE_GPR0, 4);
1186 XE_SELECT_PAGE(0x42);
1188 (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ?
1190 scp->media = IFM_ETHER | ((XE_INB(XE_ESR) &
1191 XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1192 scp->autoneg_status = XE_AUTONEG_NONE;
1198 * If a specific media has been requested, we just reset the
1199 * card and select it (one small exception -- if 100baseTX is
1200 * requested but there is no PHY, we fall back to 10baseT
1203 case IFM_100_TX: /* Force 100baseTX */
1205 DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
1206 XE_SELECT_PAGE(0x42);
1207 XE_OUTB(XE_SWC1, 0);
1208 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1210 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1211 scp->media |= IFM_100_TX;
1215 case IFM_10_T: /* Force 10baseT */
1216 DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
1218 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1222 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1224 XE_SELECT_PAGE(0x42);
1225 XE_OUTB(XE_SWC1, 0x80);
1226 scp->media |= IFM_10_T;
1229 DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
1230 XE_SELECT_PAGE(0x42);
1231 XE_OUTB(XE_SWC1, 0xc0);
1232 scp->media |= IFM_10_2;
1237 * Finally, the LEDs are set to match whatever media was
1238 * chosen and the transmitter is unblocked.
1240 DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
1242 switch (IFM_SUBTYPE(scp->media)) {
1245 XE_OUTB(XE_LED, 0x3b);
1247 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1250 XE_OUTB(XE_LED, 0x3a);
1254 /* Restart output? */
1255 xe_enable_intr(scp);
1256 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1257 xe_start_locked(scp->ifp);
1261 * Hard reset (power cycle) the card.
1264 xe_reset(struct xe_softc *scp)
1267 DEVPRINTF(2, (scp->dev, "reset\n"));
1269 XE_ASSERT_LOCKED(scp);
1273 XE_OUTB(XE_GPR1, 0);
1276 /* Power up again */
1278 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1280 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN | XE_GPR1_AIC);
1287 * Take interface offline. This is done by powering down the device, which I
1288 * assume means just shutting down the transceiver and Ethernet logic. This
1289 * requires a _hard_ reset to recover from, as we need to power up again.
1292 xe_stop(struct xe_softc *scp)
1295 DEVPRINTF(2, (scp->dev, "stop\n"));
1297 XE_ASSERT_LOCKED(scp);
1300 * Shut off interrupts.
1302 xe_disable_intr(scp);
1308 XE_OUTB(XE_GPR1, 0);
1312 * set GP1 and GP2 as outputs (bits 2 & 3)
1313 * set GP1 high to power on the ML6692 (bit 0)
1314 * set GP2 low to power on the 10Mhz chip (bit 1)
1317 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
1322 * ~IFF_DRV_RUNNING == interface down.
1324 scp->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1325 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1326 scp->tx_timeout = 0;
1327 callout_stop(&scp->wdog_timer);
1328 callout_stop(&scp->media_timer);
1332 * Enable interrupts from the card.
1335 xe_enable_intr(struct xe_softc *scp)
1338 DEVPRINTF(2, (scp->dev, "enable_intr\n"));
1341 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1342 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1343 if (!(XE_INB(0x10) & 0x01)) {
1344 XE_OUTB(0x10, 0x11); /* Unmask master int enable */
1350 * Disable interrupts from the card.
1353 xe_disable_intr(struct xe_softc *scp)
1356 DEVPRINTF(2, (scp->dev, "disable_intr\n"));
1359 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1360 if (scp->modem && !scp->dingo) { /* More magic */
1361 XE_OUTB(0x10, 0x10); /* Mask the master int enable */
1366 * Set up multicast filter and promiscuous modes.
1369 xe_set_multicast(struct xe_softc *scp)
1372 struct ifmultiaddr *maddr;
1375 DEVPRINTF(2, (scp->dev, "set_multicast\n"));
1378 XE_SELECT_PAGE(0x42);
1380 /* Handle PROMISC flag */
1381 if (ifp->if_flags & IFF_PROMISC) {
1382 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1385 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1387 /* Handle ALLMULTI flag */
1388 if (ifp->if_flags & IFF_ALLMULTI) {
1389 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1392 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1394 /* Iterate over multicast address list */
1396 if_maddr_rlock(ifp);
1397 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1398 if (maddr->ifma_addr->sa_family != AF_LINK)
1405 * First 9 use Individual Addresses for exact
1409 LLADDR((struct sockaddr_dl *)maddr->ifma_addr),
1411 else if (scp->mohawk)
1412 /* Use hash filter on Mohawk and Dingo */
1414 LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
1416 /* Nowhere else to put them on CE2 */
1419 if_maddr_runlock(ifp);
1421 DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
1423 /* Now do some cleanup and enable multicast handling as needed */
1425 /* Disable all multicast handling */
1426 XE_SELECT_PAGE(0x42);
1427 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) &
1428 ~(XE_SWC1_IA_ENABLE | XE_SWC1_ALLMULTI));
1430 XE_SELECT_PAGE(0x02);
1431 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1433 } else if (count < 10) {
1435 * Full in any unused Individual Addresses with our
1438 for (i = count + 1; i < 10; i++)
1439 xe_set_addr(scp, IF_LLADDR(scp->ifp), i);
1441 /* Enable Individual Address matching only */
1442 XE_SELECT_PAGE(0x42);
1443 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1446 XE_SELECT_PAGE(0x02);
1447 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1449 } else if (scp->mohawk) {
1450 /* Check whether hash table is full */
1451 XE_SELECT_PAGE(0x58);
1452 for (i = 0x08; i < 0x10; i++)
1453 if (XE_INB(i) != 0xff)
1457 * Hash table full - enable
1458 * promiscuous multicast matching
1460 XE_SELECT_PAGE(0x42);
1461 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) &
1462 ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1463 XE_SELECT_PAGE(0x02);
1464 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1466 /* Enable hash table and Individual Address matching */
1467 XE_SELECT_PAGE(0x42);
1468 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1470 XE_SELECT_PAGE(0x02);
1471 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1474 /* Enable promiscuous multicast matching */
1475 XE_SELECT_PAGE(0x42);
1476 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) |
1484 * Copy the Ethernet multicast address in addr to the on-chip registers for
1485 * Individual Address idx. Assumes that addr is really a multicast address
1486 * and that idx > 0 (slot 0 is always used for the card MAC address).
1489 xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx)
1495 * Individual Addresses are stored in registers 8-F of pages
1496 * 0x50-0x57. IA1 therefore starts at register 0xE on page
1497 * 0x50. The expressions below compute the starting page and
1498 * register for any IA index > 0.
1501 page = 0x50 + idx % 4 + idx / 4 * 3;
1502 reg = 0x0e - 2 * (idx % 4);
1504 DEVPRINTF(3, (scp->dev,
1505 "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n", idx + 1, page,
1509 * Copy the IA bytes. Note that the byte order is reversed
1510 * for Mohawk and Dingo wrt. CE2 hardware.
1512 XE_SELECT_PAGE(page);
1513 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1515 DPRINTF(3, (":%02x", addr[i]));
1517 DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
1519 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1520 if (++reg == 0x10) {
1522 XE_SELECT_PAGE(++page);
1529 * Set the appropriate bit in the multicast hash table for the supplied
1530 * Ethernet multicast address addr. Assumes that addr is really a multicast
1534 xe_mchash(struct xe_softc* scp, const uint8_t *addr)
1539 hash = ether_crc32_le(addr, ETHER_ADDR_LEN) & 0x3F;
1542 * Top 3 bits of hash give register - 8, bottom 3 give bit
1545 byte = hash >> 3 | 0x08;
1546 bit = 0x01 << (hash & 0x07);
1548 DEVPRINTF(3, (scp->dev,
1549 "set_hash: hash = 0x%02x, byte = 0x%02x, bit = 0x%02x\n", hash,
1552 XE_SELECT_PAGE(0x58);
1553 XE_OUTB(byte, XE_INB(byte) | bit);
1557 * Write an outgoing packet to the card using programmed I/O.
1560 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp)
1563 unsigned char wantbyte;
1565 uint8_t savebyte[2];
1567 /* Get total packet length */
1568 if (mbp->m_flags & M_PKTHDR)
1569 len = mbp->m_pkthdr.len;
1571 struct mbuf* mbp2 = mbp;
1572 for (len = 0; mbp2 != NULL;
1573 len += mbp2->m_len, mbp2 = mbp2->m_next);
1576 DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
1578 /* Packets < minimum length may need to be padded out */
1580 if (len < scp->tx_min) {
1581 pad = scp->tx_min - len;
1585 /* Check transmit buffer space */
1587 XE_OUTW(XE_TRS, len + 2); /* Only effective on rev. 1 CE2 cards */
1588 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1591 /* Send packet length to card */
1592 XE_OUTW(XE_EDP, len);
1595 * Write packet to card using PIO (code stolen from the ed driver)
1598 while (mbp != NULL) {
1601 data = mtod(mbp, caddr_t);
1602 if (wantbyte) { /* Finish the last word */
1603 savebyte[1] = *data;
1604 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1609 if (len > 1) { /* Output contiguous words */
1610 bus_write_multi_2(scp->port_res, XE_EDP,
1611 (uint16_t *)data, len >> 1);
1615 if (len == 1) { /* Save last byte, if needed */
1616 savebyte[0] = *data;
1624 * Send last byte of odd-length packets
1627 XE_OUTB(XE_EDP, savebyte[0]);
1630 * Can just tell CE3 cards to send; short packets will be
1631 * padded out with random cruft automatically. For CE2,
1632 * manually pad the packet with garbage; it will be sent when
1633 * the required number of bytes have been delivered to the
1637 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX |
1641 XE_OUTB(XE_EDP, 0xaa);
1644 XE_OUTW(XE_EDP, 0xdead);
1652 /**************************************************************
1654 * M I I F U N C T I O N S *
1656 **************************************************************/
1659 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1660 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1661 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1662 * XXX - some kind of common MII-handling code, shared by all drivers. But
1663 * XXX - that's a whole other mission.
1665 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1666 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1669 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1672 xe_mii_sync(struct xe_softc *scp)
1677 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1679 for (i = 0; i < 32; i++) {
1680 XE_MII_SET(XE_MII_CLK);
1682 XE_MII_CLR(XE_MII_CLK);
1688 * Look for a MII-compliant PHY. If we find one, reset it.
1691 xe_mii_init(struct xe_softc *scp)
1695 status = xe_phy_readreg(scp, PHY_BMSR);
1696 if ((status & 0xff00) != 0x7800) {
1697 DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
1700 DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
1703 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1705 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET)
1713 * Clock a series of bits through the MII.
1716 xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt)
1721 XE_MII_CLR(XE_MII_CLK);
1723 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1725 XE_MII_SET(XE_MII_WRD);
1727 XE_MII_CLR(XE_MII_WRD);
1730 XE_MII_CLR(XE_MII_CLK);
1732 XE_MII_SET(XE_MII_CLK);
1737 * Read an PHY register through the MII.
1740 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame)
1744 XE_ASSERT_LOCKED(scp);
1747 * Set up frame for RX.
1749 frame->mii_stdelim = XE_MII_STARTDELIM;
1750 frame->mii_opcode = XE_MII_READOP;
1751 frame->mii_turnaround = 0;
1752 frame->mii_data = 0;
1755 XE_OUTB(XE_GPR2, 0);
1758 * Turn on data xmit.
1760 XE_MII_SET(XE_MII_DIR);
1765 * Send command/address info.
1767 xe_mii_send(scp, frame->mii_stdelim, 2);
1768 xe_mii_send(scp, frame->mii_opcode, 2);
1769 xe_mii_send(scp, frame->mii_phyaddr, 5);
1770 xe_mii_send(scp, frame->mii_regaddr, 5);
1773 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1775 XE_MII_SET(XE_MII_CLK);
1778 /* Turn off xmit. */
1779 XE_MII_CLR(XE_MII_DIR);
1782 XE_MII_CLR(XE_MII_CLK);
1784 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1785 XE_MII_SET(XE_MII_CLK);
1789 * Now try reading data bits. If the ack failed, we still
1790 * need to clock through 16 cycles to keep the PHY(s) in sync.
1793 for(i = 0; i < 16; i++) {
1794 XE_MII_CLR(XE_MII_CLK);
1796 XE_MII_SET(XE_MII_CLK);
1802 for (i = 0x8000; i; i >>= 1) {
1803 XE_MII_CLR(XE_MII_CLK);
1806 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1807 frame->mii_data |= i;
1810 XE_MII_SET(XE_MII_CLK);
1815 XE_MII_CLR(XE_MII_CLK);
1817 XE_MII_SET(XE_MII_CLK);
1826 * Write to a PHY register through the MII.
1829 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame)
1832 XE_ASSERT_LOCKED(scp);
1835 * Set up frame for TX.
1837 frame->mii_stdelim = XE_MII_STARTDELIM;
1838 frame->mii_opcode = XE_MII_WRITEOP;
1839 frame->mii_turnaround = XE_MII_TURNAROUND;
1844 * Turn on data output.
1846 XE_MII_SET(XE_MII_DIR);
1850 xe_mii_send(scp, frame->mii_stdelim, 2);
1851 xe_mii_send(scp, frame->mii_opcode, 2);
1852 xe_mii_send(scp, frame->mii_phyaddr, 5);
1853 xe_mii_send(scp, frame->mii_regaddr, 5);
1854 xe_mii_send(scp, frame->mii_turnaround, 2);
1855 xe_mii_send(scp, frame->mii_data, 16);
1858 XE_MII_SET(XE_MII_CLK);
1860 XE_MII_CLR(XE_MII_CLK);
1866 XE_MII_CLR(XE_MII_DIR);
1872 * Read a register from the PHY.
1875 xe_phy_readreg(struct xe_softc *scp, uint16_t reg)
1877 struct xe_mii_frame frame;
1879 bzero((char *)&frame, sizeof(frame));
1881 frame.mii_phyaddr = 0;
1882 frame.mii_regaddr = reg;
1883 xe_mii_readreg(scp, &frame);
1885 return (frame.mii_data);
1889 * Write to a PHY register.
1892 xe_phy_writereg(struct xe_softc *scp, uint16_t reg, uint16_t data)
1894 struct xe_mii_frame frame;
1896 bzero((char *)&frame, sizeof(frame));
1898 frame.mii_phyaddr = 0;
1899 frame.mii_regaddr = reg;
1900 frame.mii_data = data;
1901 xe_mii_writereg(scp, &frame);
1905 * A bit of debugging code.
1908 xe_mii_dump(struct xe_softc *scp)
1912 device_printf(scp->dev, "MII registers: ");
1913 for (i = 0; i < 2; i++) {
1914 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1916 for (i = 4; i < 7; i++) {
1917 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1924 xe_reg_dump(struct xe_softc *scp)
1928 device_printf(scp->dev, "Common registers: ");
1929 for (i = 0; i < 8; i++) {
1930 printf(" %2.2x", XE_INB(i));
1934 for (page = 0; page <= 8; page++) {
1935 device_printf(scp->dev, "Register page %2.2x: ", page);
1936 XE_SELECT_PAGE(page);
1937 for (i = 8; i < 16; i++) {
1938 printf(" %2.2x", XE_INB(i));
1943 for (page = 0x10; page < 0x5f; page++) {
1944 if ((page >= 0x11 && page <= 0x3f) ||
1946 (page >= 0x43 && page <= 0x4f) ||
1949 device_printf(scp->dev, "Register page %2.2x: ", page);
1950 XE_SELECT_PAGE(page);
1951 for (i = 8; i < 16; i++) {
1952 printf(" %2.2x", XE_INB(i));
1960 xe_activate(device_t dev)
1962 struct xe_softc *sc = device_get_softc(dev);
1965 DEVPRINTF(2, (dev, "activate\n"));
1968 sc->port_rid = 0; /* 0 is managed by pccard */
1969 sc->port_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
1970 &sc->port_rid, 16, RF_ACTIVE);
1971 } else if (sc->dingo) {
1973 * Find a 16 byte aligned ioport for the card.
1975 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1976 sc->port_rid = 1; /* 0 is managed by pccard */
1979 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1980 &sc->port_rid, start, 0x3ff, 16, RF_ACTIVE);
1981 if (sc->port_res == NULL)
1983 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1985 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1987 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1989 DEVPRINTF(1, (dev, "RealPort port 0x%0jx, size 0x%0jx\n",
1990 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1991 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1992 } else if (sc->ce2) {
1994 * Find contiguous I/O port for the Ethernet function
1995 * on CEM2 and CEM3 cards. We allocate window 0
1996 * wherever pccard has decided it should be, then find
1997 * an available window adjacent to it for the second
1998 * function. Not sure that both windows are actually
2001 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
2002 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
2003 sc->ce2_port_res = bus_alloc_resource_anywhere(dev,
2004 SYS_RES_IOPORT, &sc->ce2_port_rid, 8, RF_ACTIVE);
2005 if (sc->ce2_port_res == NULL) {
2007 "Cannot allocate I/O port for modem\n"));
2013 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
2015 for (i = 0; i < 2; i++) {
2016 start += (i == 0 ? 8 : -24);
2017 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
2018 &sc->port_rid, start, start + 15, 16, RF_ACTIVE);
2019 if (sc->port_res == NULL)
2021 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
2022 sc->port_rid) == start)
2025 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2027 sc->port_res = NULL;
2029 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0jx, size 0x%0jx\n",
2030 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
2031 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
2034 if (!sc->port_res) {
2035 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
2041 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2043 if (sc->irq_res == NULL) {
2044 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
2053 xe_deactivate(device_t dev)
2055 struct xe_softc *sc = device_get_softc(dev);
2057 DEVPRINTF(2, (dev, "deactivate\n"));
2059 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
2060 sc->intrhand = NULL;
2062 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2064 sc->port_res = NULL;
2065 if (sc->ce2_port_res)
2066 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2068 sc->ce2_port_res = NULL;
2070 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,