ac35d64d17b49a047be642f9d1963e7e2613d20a
[dragonfly.git] / sys / dev / netif / xe / if_xe.c
1 /*-
2  * Copyright (c) 1998, 1999, 2003 Scott Mitchell
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  *      $Id: if_xe.c,v 1.20 1999/06/13 19:17:40 scott Exp $
27  * $FreeBSD: src/sys/dev/xe/if_xe.c,v 1.39 2003/10/14 22:51:35 rsm Exp $
28  * $DragonFly: src/sys/dev/netif/xe/if_xe.c,v 1.35 2008/05/14 11:59:23 sephe Exp $
29  */
30
31 /*
32  * Portions of this software were derived from Werner Koch's xirc2ps driver
33  * for Linux under the terms of the following license (from v1.30 of the
34  * xirc2ps driver):
35  *
36  * Copyright (c) 1997 by Werner Koch (dd9jn)
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, and the entire permission notice in its entirety,
43  *    including the disclaimer of warranties.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. The name of the author may not be used to endorse or promote
48  *    products derived from this software without specific prior
49  *    written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
52  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61  * OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63
64 /*              
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)
75  *
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?)
82  *
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.
87  *
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
90  * the web pages.
91  *
92  * Author email: <scott@uk.freebsd.org>
93  * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
94  */
95
96
97 #include <sys/param.h>
98 #include <sys/cdefs.h>
99 #include <sys/errno.h>
100 #include <sys/kernel.h>
101 #include <sys/mbuf.h>
102 #include <sys/select.h>
103 #include <sys/socket.h>
104 #include <sys/sockio.h>
105 #include <sys/sysctl.h>
106 #include <sys/systm.h>
107 #include <sys/uio.h>
108 #include <sys/serialize.h>
109 #include <sys/module.h>
110 #include <sys/bus.h>
111 #include <sys/rman.h>
112 #include <sys/thread2.h>
113 #include <sys/interrupt.h>
114  
115 #include <net/ethernet.h>
116 #include <net/if.h>
117 #include <net/ifq_var.h>
118 #include <net/if_arp.h>
119 #include <net/if_dl.h>
120 #include <net/if_media.h>
121 #include <net/if_mib.h>
122 #include <net/bpf.h>
123
124 #include "if_xereg.h"
125 #include "if_xevar.h"
126
127 /*
128  * MII command structure
129  */
130 struct xe_mii_frame {
131   u_int8_t  mii_stdelim;
132   u_int8_t  mii_opcode;
133   u_int8_t  mii_phyaddr;
134   u_int8_t  mii_regaddr;
135   u_int8_t  mii_turnaround;
136   u_int16_t mii_data;
137 };
138
139 /*
140  * Media autonegotiation progress constants
141  */
142 #define XE_AUTONEG_NONE         0       /* No autonegotiation in progress */
143 #define XE_AUTONEG_WAITING      1       /* Waiting for transmitter to go idle */
144 #define XE_AUTONEG_STARTED      2       /* Waiting for autonegotiation to complete */
145 #define XE_AUTONEG_100TX        3       /* Trying to force 100baseTX link */
146 #define XE_AUTONEG_FAIL         4       /* Autonegotiation failed */
147
148 /*
149  * Multicast hashing CRC constants
150  */
151 #define XE_CRC_POLY  0x04c11db6
152
153 /*
154  * Prototypes start here
155  */
156 static void      xe_init                (void *xscp);
157 static void      xe_intr                (void *xscp);
158 static void      xe_start               (struct ifnet *ifp);
159 static int       xe_ioctl               (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *);
160 static void      xe_watchdog            (struct ifnet *ifp);
161 static int       xe_media_change        (struct ifnet *ifp);
162 static void      xe_media_status        (struct ifnet *ifp, struct ifmediareq *mrp);
163 static timeout_t xe_setmedia;
164 static timeout_t xe_setmedia_serialized;
165 static void      xe_reset               (struct xe_softc *scp);
166 static void      xe_stop                (struct xe_softc *scp);
167 static void      xe_enable_intr         (struct xe_softc *scp);
168 static void      xe_disable_intr        (struct xe_softc *scp);
169 static void      xe_set_multicast       (struct xe_softc *scp);
170 static void      xe_set_addr            (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
171 static void      xe_set_hash            (struct xe_softc *scp, u_int8_t* addr);
172 static int       xe_pio_write_packet    (struct xe_softc *scp, struct mbuf *mbp);
173
174 /*
175  * MII functions
176  */
177 static void      xe_mii_sync            (struct xe_softc *scp);
178 static int       xe_mii_init            (struct xe_softc *scp);
179 static void      xe_mii_send            (struct xe_softc *scp, u_int32_t bits, int cnt);
180 static int       xe_mii_readreg         (struct xe_softc *scp, struct xe_mii_frame *frame);
181 static int       xe_mii_writereg        (struct xe_softc *scp, struct xe_mii_frame *frame);
182 static u_int16_t xe_phy_readreg         (struct xe_softc *scp, u_int16_t reg);
183 static void      xe_phy_writereg        (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
184
185 /* Debugging functions */
186 static void      xe_reg_dump            (struct xe_softc *scp) __unused;
187 static void      xe_mii_dump            (struct xe_softc *scp);
188
189 #define XE_DEBUG
190
191 #ifdef XE_DEBUG
192
193 /* sysctl vars */
194 SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "xe parameters");
195
196 /*
197  * Debug logging levels - set with hw.xe.debug sysctl
198  * 0 = None
199  * 1 = More hardware details, probe/attach progress
200  * 2 = Most function calls, ioctls and media selection progress
201  * 3 = Everything - interrupts, packets in/out and multicast address setup
202  */
203 int     xe_debug = 1;
204 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0, "xe debug level");
205
206 #define DPRINTF(level, arg)     if (xe_debug >= (level)) kprintf arg
207 #define IFPRINTF(level, arg)    if (xe_debug >= (level)) if_printf arg
208 #define DEVPRINTF(level, arg)   if (xe_debug >= (level)) device_printf arg
209 #define XE_MII_DUMP(scp)        if (xe_debug >= 3) xe_mii_dump(scp)
210 #define XE_REG_DUMP(scp)        if (xe_debug >= 3) xe_reg_dump(scp)
211
212 #else   /* !XE_DEBUG */
213
214 #define DPRINTF(level, arg)
215 #define IFPRINTF(level, arg)
216 #define DEVPRINTF(level, arg)
217 #define XE_REG_DUMP(scp)
218 #define XE_MII_DUMP(scp)
219
220 #endif  /* XE_DEBUG */
221
222 /*
223  * The device entry is being removed, probably because someone ejected the
224  * card.  The interface should have been brought down manually before calling
225  * this function; if not you may well lose packets.  In any case, I shut down
226  * the card and the interface, and hope for the best.
227  */
228 int
229 xe_detach(device_t dev)
230 {
231   struct xe_softc *sc = device_get_softc(dev);
232   struct ifnet *ifp = &sc->arpcom.ac_if;
233
234   lwkt_serialize_enter(ifp->if_serializer);
235
236   ifp->if_flags &= ~IFF_RUNNING; 
237   callout_stop(&sc->xe_timer);
238   bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
239
240   lwkt_serialize_exit(ifp->if_serializer);
241
242   ether_ifdetach(ifp);
243   xe_deactivate(dev);
244
245   return 0;
246 }
247
248 /*
249  * Attach a device.
250  */
251 int
252 xe_attach (device_t dev)
253 {
254   struct xe_softc *scp = device_get_softc(dev);
255   int err;
256
257   DEVPRINTF(2, (dev, "attach\n"));
258
259   /* Fill in some private data */
260   scp->ifp = &scp->arpcom.ac_if;
261   scp->ifm = &scp->ifmedia;
262   scp->autoneg_status = XE_AUTONEG_NONE;
263
264   /* Initialise the ifnet structure */
265   scp->ifp->if_softc = scp;
266   if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
267   scp->ifp->if_timer = 0;
268   scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
269   scp->ifp->if_linkmib = &scp->mibdata;
270   scp->ifp->if_linkmiblen = sizeof scp->mibdata;
271   scp->ifp->if_start = xe_start;
272   scp->ifp->if_ioctl = xe_ioctl;
273   scp->ifp->if_watchdog = xe_watchdog;
274   scp->ifp->if_init = xe_init;
275   scp->ifp->if_baudrate = 100000000;
276   ifq_set_maxlen(&scp->ifp->if_snd, IFQ_MAXLEN);
277   ifq_set_ready(&scp->ifp->if_snd);
278
279   /* Initialise the ifmedia structure */
280   ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
281   callout_init(&scp->xe_timer);
282
283   /* Add supported media types */
284   if (scp->mohawk) {
285     ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
286     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
287     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
288   }
289   ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
290   if (scp->ce2)
291     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
292   ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
293
294   /* Default is to autoselect best supported media type */
295   ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
296
297   /* Get the hardware into a known state */
298   xe_reset(scp);
299
300   /* Get hardware version numbers */
301   XE_SELECT_PAGE(4);
302   scp->version = XE_INB(XE_BOV);
303   if (scp->mohawk)
304     scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
305   else
306     scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
307
308   /* Print some useful information */
309   device_printf(dev, "%s %s, version 0x%02x/0x%02x%s%s\n",
310          scp->vendor,
311          scp->card_type,
312          scp->version,
313          scp->srev,
314          scp->mohawk ? ", 100Mbps capable" : "",
315          scp->modem ?  ", with modem"      : "");
316
317   if (scp->mohawk) {
318     XE_SELECT_PAGE(0x10);
319     DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
320                   XE_INW(XE_DINGOID),
321                   XE_INW(XE_RevID),
322                   XE_INW(XE_VendorID)));
323   }
324   if (scp->ce2) {
325     XE_SELECT_PAGE(0x45);
326     DEVPRINTF(1, (dev, "CE2 version = 0x%#02x\n", XE_INB(XE_REV)));
327   }
328
329   /* Attach the interface */
330   ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr, NULL);
331
332   err = bus_setup_intr(dev, scp->irq_res, INTR_NETSAFE,
333                        xe_intr, scp, &scp->intrhand,
334                        scp->ifp->if_serializer);
335   if (err) {
336     device_printf(dev, "Setup intr failed\n");
337     ether_ifdetach(scp->ifp);
338     xe_deactivate(dev);
339     return err;
340   }
341
342   scp->ifp->if_cpuid = ithread_cpuid(rman_get_start(scp->irq_res));
343   KKASSERT(scp->ifp->if_cpuid >= 0 && scp->ifp->if_cpuid < ncpus);
344
345   /* Done */
346   return 0;
347 }
348
349
350 /*
351  * Complete hardware intitialisation and enable output.  Exits without doing
352  * anything if there's no address assigned to the card, or if media selection
353  * is in progress (the latter implies we've already run this function).
354  */
355 static void
356 xe_init(void *xscp) {
357   struct xe_softc *scp = xscp;
358   u_int i;
359
360   if (scp->autoneg_status != XE_AUTONEG_NONE) return;
361
362   IFPRINTF(2, (scp->ifp, "init\n"));
363
364   crit_enter();
365
366   /* Reset transmitter flags */
367   scp->tx_queued = 0;
368   scp->tx_tpr = 0;
369   scp->tx_timeouts = 0;
370   scp->tx_thres = 64;
371   scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
372   scp->ifp->if_timer = 0;
373
374   /* Soft reset the card */
375   XE_SELECT_PAGE(0);
376   XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
377   DELAY(40000);
378   XE_OUTB(XE_CR, 0);
379   DELAY(40000);
380  
381   if (scp->mohawk) {
382     /*
383      * set GP1 and GP2 as outputs (bits 2 & 3)
384      * set GP1 low to power on the ML6692 (bit 0)
385      * set GP2 high to power on the 10Mhz chip (bit 1)
386      */
387     XE_SELECT_PAGE(4);
388     XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP2_OUT);
389   }
390
391   /* Shut off interrupts */
392   xe_disable_intr(scp);
393
394   /* Wait for everything to wake up */
395   DELAY(500000);
396
397   /* Check for PHY */
398   if (scp->mohawk)
399     scp->phy_ok = xe_mii_init(scp);
400
401   /* Disable 'source insertion' (not sure what that means) */
402   XE_SELECT_PAGE(0x42);
403   XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
404
405   /* Set 8K/24K Tx/Rx buffer split */
406   if (scp->srev != 1) {
407     XE_SELECT_PAGE(2);
408     XE_OUTW(XE_RBS, 0x2000);
409   }
410
411   /* Enable early transmit mode on Mohawk/Dingo */
412   if (scp->mohawk) {
413     XE_SELECT_PAGE(0x03);
414     XE_OUTW(XE_TPT, scp->tx_thres);
415     XE_SELECT_PAGE(0x01);
416     XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
417   }
418
419   /* Put MAC address in first 'individual address' register */
420   XE_SELECT_PAGE(0x50);
421   for (i = 0; i < 6; i++)
422     XE_OUTB(0x08 + i, scp->arpcom.ac_enaddr[scp->mohawk ? 5 - i : i]);
423
424   /* Set up multicast addresses */
425   xe_set_multicast(scp);
426
427   /* Fix the receive data offset -- reset can leave it off-by-one */
428   XE_SELECT_PAGE(0);
429   XE_OUTW(XE_DO, 0x2000);
430
431   /* Set interrupt masks */
432   XE_SELECT_PAGE(1);
433   XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR | XE_IMR0_RX_PACKET);
434
435   /* Set MAC interrupt masks */
436   XE_SELECT_PAGE(0x40);
437   XE_OUTB(XE_RX0Msk,
438           ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR
439             | XE_RX0M_ALIGN_ERROR | XE_RX0M_LONG_PACKET));
440   XE_OUTB(XE_TX0Msk,
441           ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN
442             | XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
443
444   /* Clear MAC status registers */
445   XE_SELECT_PAGE(0x40);
446   XE_OUTB(XE_RST0, 0x00);
447   XE_OUTB(XE_TXST0, 0x00);
448
449   /* Enable receiver and put MAC online */
450   XE_SELECT_PAGE(0x40);
451   XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
452  
453   /* Set up IMR, enable interrupts */
454   xe_enable_intr(scp);
455
456   /* Start media selection */
457   xe_setmedia_serialized(scp);
458
459   /* Enable output */
460   scp->ifp->if_flags |= IFF_RUNNING;
461   scp->ifp->if_flags &= ~IFF_OACTIVE;
462
463   crit_exit();
464 }
465
466
467 /*
468  * Start output on interface.  Should be called at splimp() priority.  Check
469  * that the output is idle (ie, IFF_OACTIVE is not set) before calling this
470  * function.  If media selection is in progress we set IFF_OACTIVE ourselves
471  * and return immediately.
472  */
473 static void
474 xe_start(struct ifnet *ifp) {
475   struct xe_softc *scp = ifp->if_softc;
476   struct mbuf *mbp;
477
478   if (scp->autoneg_status != XE_AUTONEG_NONE) {
479     ifp->if_flags |= IFF_OACTIVE;
480     return;
481   }
482
483   IFPRINTF(3, (ifp, "start\n"));
484
485   /*
486    * Loop while there are packets to be sent, and space to send them.
487    */
488   while (1) {
489     /* Suck a packet off the send queue */
490     mbp = ifq_dequeue(&ifp->if_snd, NULL);
491     if (mbp == NULL) {
492       /*
493        * We are using the !OACTIVE flag to indicate to the outside
494        * world that we can accept an additional packet rather than
495        * that the transmitter is _actually_ active. Indeed, the
496        * transmitter may be active, but if we haven't filled all
497        * the buffers with data then we still want to accept more.
498        */
499       ifp->if_flags &= ~IFF_OACTIVE;
500       return;
501     }
502
503     if (xe_pio_write_packet(scp, mbp) != 0) {
504       ifp->if_flags |= IFF_OACTIVE;
505       ifq_prepend(&ifp->if_snd, mbp);
506       return;
507     }
508
509     BPF_MTAP(ifp, mbp);
510
511     ifp->if_timer = 5; /* In case we don't hear from the card again */
512     scp->tx_queued++;
513
514     m_freem(mbp);
515   }
516 }
517
518
519 /*
520  * Process an ioctl request.  Adapted from the ed driver.
521  */
522 static int
523 xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr) {
524   struct xe_softc *scp;
525   int           error;
526
527   scp = ifp->if_softc;
528   error = 0;
529
530   crit_enter();
531
532   switch (command) {
533
534   case SIOCSIFFLAGS:
535     IFPRINTF(2, (ifp, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
536     /*
537      * If the interface is marked up and stopped, then start it.  If it is
538      * marked down and running, then stop it.
539      */
540     if (ifp->if_flags & IFF_UP) {
541       if (!(ifp->if_flags & IFF_RUNNING)) {
542         xe_reset(scp);
543         xe_init(scp);
544       }
545     }
546     else {
547       if (ifp->if_flags & IFF_RUNNING)
548         xe_stop(scp);
549     }
550     /* FALL THROUGH  (handle changes to PROMISC/ALLMULTI flags) */
551
552   case SIOCADDMULTI:
553   case SIOCDELMULTI:
554     IFPRINTF(2, (ifp, "ioctl: SIOC{ADD,DEL}MULTI\n"));
555     /*
556      * Multicast list has (maybe) changed; set the hardware filters
557      * accordingly.
558      */
559     xe_set_multicast(scp);
560     error = 0;
561     break;
562
563   case SIOCSIFMEDIA:
564   case SIOCGIFMEDIA:
565     IFPRINTF(3, (ifp, "ioctl: bounce to ifmedia_ioctl\n"));
566     /*
567      * Someone wants to get/set media options.
568      */
569     error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
570     break;
571
572   default:
573     IFPRINTF(3, (ifp, "ioctl: bounce to ether_ioctl\n"));
574     error = ether_ioctl(ifp, command, data);
575     break;
576   }
577
578   crit_exit();
579
580   return error;
581 }
582
583
584 /*
585  * Card interrupt handler.
586  *
587  * This function is probably more complicated than it needs to be, as it
588  * attempts to deal with the case where multiple packets get sent between
589  * interrupts.  This is especially annoying when working out the collision
590  * stats.  Not sure whether this case ever really happens or not (maybe on a
591  * slow/heavily loaded machine?) so it's probably best to leave this like it
592  * is.
593  *
594  * Note that the crappy PIO used to get packets on and off the card means that 
595  * you will spend a lot of time in this routine -- I can get my P150 to spend
596  * 90% of its time servicing interrupts if I really hammer the network.  Could 
597  * fix this, but then you'd start dropping/losing packets.  The moral of this
598  * story?  If you want good network performance _and_ some cycles left over to 
599  * get your work done, don't buy a Xircom card.  Or convince them to tell me
600  * how to do memory-mapped I/O :)
601  */
602 static void
603 xe_intr(void *xscp) 
604 {
605   struct xe_softc *scp = (struct xe_softc *) xscp;
606   struct ifnet *ifp;
607   u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
608
609   ifp = &scp->arpcom.ac_if;
610
611   /* Disable interrupts */
612   if (scp->mohawk)
613     XE_OUTB(XE_CR, 0);
614
615   /* Cache current register page */
616   psr = XE_INB(XE_PR);
617
618   /* Read ISR to see what caused this interrupt */
619   while ((isr = XE_INB(XE_ISR)) != 0) {
620     /* 0xff might mean the card is no longer around */
621     if (isr == 0xff) {
622       IFPRINTF(3, (ifp, "intr: interrupt received for missing card?\n"));
623       break;
624     }
625
626     /* Read other status registers */
627     XE_SELECT_PAGE(0x40);
628     rst0 = XE_INB(XE_RST0);
629     XE_OUTB(XE_RST0, 0);
630     txst0 = XE_INB(XE_TXST0);
631     txst1 = XE_INB(XE_TXST1);
632     coll = txst1 & XE_TXST1_RETRY_COUNT;
633     XE_OUTB(XE_TXST0, 0);
634     XE_OUTB(XE_TXST1, 0);
635     XE_SELECT_PAGE(0);
636
637     IFPRINTF(3, (ifp,
638                  "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x, COLL=0x%01x\n",
639                  isr, rst0, txst1, txst0, coll));
640
641     /* Handle transmitted packet(s) */
642     if (isr & XE_ISR_TX_PACKET) {
643       u_int8_t tpr, sent;
644
645       /* Update packet count, accounting for rollover */
646       tpr = XE_INB(XE_TPR);
647       sent = -scp->tx_tpr + tpr;
648
649       /* Update statistics if we actually sent anything */
650       if (sent > 0) {
651         scp->tx_tpr = tpr;
652         scp->tx_queued -= sent;
653         ifp->if_opackets += sent;
654         ifp->if_collisions += coll;
655
656         /*
657          * According to the Xircom manual, Dingo will sometimes manage to
658          * transmit a packet with triggering an interrupt.  If this happens,
659          * we have sent > 1 and the collision count only reflects collisions
660          * on the last packet sent (the one that triggered the interrupt).
661          * Collision stats might therefore be a bit low, but there doesn't
662          * seem to be anything we can do about that.
663          */
664         switch (coll) {
665         case 0:
666           break;
667         case 1:
668           scp->mibdata.dot3StatsSingleCollisionFrames++;
669           scp->mibdata.dot3StatsCollFrequencies[0]++;
670           break;
671         default:
672           scp->mibdata.dot3StatsMultipleCollisionFrames++;
673           scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
674         }
675       }
676       ifp->if_timer = 0;
677       ifp->if_flags &= ~IFF_OACTIVE;
678     }
679
680     /* Handle most MAC interrupts */
681     if (isr & XE_ISR_MAC_INTR) {
682 #if 0
683       /* Carrier sense lost -- only in 10Mbit HDX mode */
684       if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
685         /* XXX - Need to update media status here */
686         device_printf(scp->dev, "no carrier\n");
687         ifp->if_oerrors++;
688         scp->mibdata.dot3StatsCarrierSenseErrors++;
689       }
690 #endif
691       /* Excessive collisions -- try sending again */
692       if (txst0 & XE_TXST0_16_COLLISIONS) {
693         ifp->if_collisions += 16;
694         ifp->if_oerrors++;
695         scp->mibdata.dot3StatsExcessiveCollisions++;
696         scp->mibdata.dot3StatsMultipleCollisionFrames++;
697         scp->mibdata.dot3StatsCollFrequencies[15]++;
698         XE_OUTB(XE_CR, XE_CR_RESTART_TX);
699       }
700       /* Transmit underrun -- increase early transmit threshold */
701       if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
702         IFPRINTF(1, (ifp, "transmit underrun"));
703         if (scp->tx_thres < ETHER_MAX_LEN) {
704           if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
705             scp->tx_thres = ETHER_MAX_LEN;
706           DPRINTF(1, (": increasing transmit threshold to %u", scp->tx_thres));
707           XE_SELECT_PAGE(0x3);
708           XE_OUTW(XE_TPT, scp->tx_thres);
709           XE_SELECT_PAGE(0x0);
710         }
711         DPRINTF(1, ("\n"));
712         ifp->if_oerrors++;
713         scp->mibdata.dot3StatsInternalMacTransmitErrors++;
714       }
715       /* Late collision -- just complain about it */
716       if (txst0 & XE_TXST0_LATE_COLLISION) {
717         if_printf(ifp, "late collision\n");
718         ifp->if_oerrors++;
719         scp->mibdata.dot3StatsLateCollisions++;
720       }
721       /* SQE test failure -- just complain about it */
722       if (txst0 & XE_TXST0_SQE_FAIL) {
723         if_printf(ifp, "SQE test failure\n");
724         ifp->if_oerrors++;
725         scp->mibdata.dot3StatsSQETestErrors++;
726       }
727       /* Packet too long -- what happens to these */
728       if (rst0 & XE_RST0_LONG_PACKET) {
729         if_printf(ifp, "received giant packet\n");
730         ifp->if_ierrors++;
731         scp->mibdata.dot3StatsFrameTooLongs++;
732       }
733       /* CRC error -- packet dropped */
734       if (rst0 & XE_RST0_CRC_ERROR) {
735         if_printf(ifp, "CRC error\n");
736         ifp->if_ierrors++;
737         scp->mibdata.dot3StatsFCSErrors++;
738       }
739     }
740
741     /* Handle received packet(s) */
742     while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
743       rsr = XE_INB(XE_RSR);
744       IFPRINTF(3, (ifp, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
745
746       /* Make sure packet is a good one */
747       if (rsr & XE_RSR_RX_OK) {
748         struct ether_header *ehp;
749         struct mbuf *mbp;
750         u_int16_t len;
751
752         len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
753
754         IFPRINTF(3, (ifp, "intr: receive length = %d\n", len));
755
756         if (len == 0) {
757           ifp->if_iqdrops++;
758           continue;
759         }
760
761         /*
762          * Allocate mbuf to hold received packet.  If the mbuf header isn't
763          * big enough, we attach an mbuf cluster to hold the packet.  Note the
764          * +=2 to align the packet data on a 32-bit boundary, and the +3 to
765          * allow for the possibility of reading one more byte than the actual
766          * packet length (we always read 16-bit words).
767          * XXX - Surely there's a better way to do this alignment?
768          */
769         MGETHDR(mbp, MB_DONTWAIT, MT_DATA);
770         if (mbp == NULL) {
771           ifp->if_iqdrops++;
772           continue;
773         }
774
775         if (len + 3 > MHLEN) {
776           MCLGET(mbp, MB_DONTWAIT);
777           if ((mbp->m_flags & M_EXT) == 0) {
778             m_freem(mbp);
779             ifp->if_iqdrops++;
780             continue;
781           }
782         }
783
784         mbp->m_data += 2;
785         ehp = mtod(mbp, struct ether_header *);
786
787         /*
788          * Now get the packet in PIO mode, including the Ethernet header but
789          * omitting the trailing CRC.
790          */
791
792         /*
793          * Work around a bug in CE2 cards.  There seems to be a problem with
794          * duplicated and extraneous bytes in the receive buffer, but without
795          * any real documentation for the CE2 it's hard to tell for sure.
796          * XXX - Needs testing on CE2 hardware
797          */
798         if (scp->srev == 0) {
799           u_short rhs;
800
801           XE_SELECT_PAGE(5);
802           rhs = XE_INW(XE_RHSA);
803           XE_SELECT_PAGE(0);
804
805           rhs += 3;                      /* Skip control info */
806
807           if (rhs >= 0x8000)
808             rhs = 0;
809
810           if (rhs + len > 0x8000) {
811             int i;
812
813             for (i = 0; i < len; i++, rhs++) {
814               ((char *)ehp)[i] = XE_INB(XE_EDP);
815               if (rhs == 0x8000) {
816                 rhs = 0;
817                 i--;
818               }
819             }
820           }
821           else
822             bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP, 
823                                    (u_int16_t *) ehp, (len + 1) >> 1);
824         }
825         else
826           bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP, 
827                                  (u_int16_t *) ehp, (len + 1) >> 1);
828
829         /* Deliver packet to upper layers */
830         mbp->m_pkthdr.rcvif = ifp;
831         mbp->m_pkthdr.len = mbp->m_len = len;
832         ifp->if_input(ifp, mbp);
833         ifp->if_ipackets++;
834       }
835       else if (rsr & XE_RSR_ALIGN_ERROR) {
836         /* Packet alignment error -- drop packet */
837         if_printf(ifp, "alignment error\n");
838         scp->mibdata.dot3StatsAlignmentErrors++;
839         ifp->if_ierrors++;
840       }
841
842       /* Skip to next packet, if there is one */
843       XE_OUTW(XE_DO, 0x8000);
844     }
845
846     /* Clear receiver overruns now we have some free buffer space */
847     if (rst0 & XE_RST0_RX_OVERRUN) {
848       IFPRINTF(1, (ifp, "receive overrun\n"));
849       ifp->if_ierrors++;
850       scp->mibdata.dot3StatsInternalMacReceiveErrors++;
851       XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
852     }
853   }
854
855   /* Restore saved page */
856   XE_SELECT_PAGE(psr);
857
858   /* Re-enable interrupts */
859   XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
860 }
861
862
863 /*
864  * Device timeout/watchdog routine.  Called automatically if we queue a packet 
865  * for transmission but don't get an interrupt within a specified timeout
866  * (usually 5 seconds).  When this happens we assume the worst and reset the
867  * card.
868  */
869 static void
870 xe_watchdog(struct ifnet *ifp) {
871   struct xe_softc *scp = ifp->if_softc;
872
873   if_printf(ifp, "watchdog timeout; resetting card\n");
874   scp->tx_timeouts++;
875   ifp->if_oerrors += scp->tx_queued;
876   xe_stop(scp);
877   xe_reset(scp);
878   xe_init(scp);
879 }
880
881
882 /*
883  * Change media selection.
884  */
885 static int
886 xe_media_change(struct ifnet *ifp) {
887   struct xe_softc *scp = ifp->if_softc;
888
889   IFPRINTF(2, (ifp, "media_change\n"));
890
891   if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
892     return(EINVAL);
893
894   /*
895    * Some card/media combos aren't always possible -- filter those out here.
896    */
897   if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
898        IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
899     return (EINVAL);
900
901   xe_setmedia_serialized(scp);
902
903   return 0;
904 }
905
906
907 /*
908  * Return current media selection.
909  */
910 static void
911 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
912
913   IFPRINTF(3, (ifp, "media_status\n"));
914
915   /* XXX - This is clearly wrong.  Will fix once I have CE2 working */
916   mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
917   mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
918 }
919
920 static 
921 void
922 xe_setmedia(void *xscp) 
923 {
924     struct xe_softc *scp = xscp;
925
926     lwkt_serialize_enter(scp->arpcom.ac_if.if_serializer);
927     xe_setmedia_serialized(xscp);
928     lwkt_serialize_exit(scp->arpcom.ac_if.if_serializer);
929 }
930
931 /*
932  * Select active media.
933  */
934 static 
935 void
936 xe_setmedia_serialized(void *xscp) 
937 {
938   struct xe_softc *scp = xscp;
939   u_int16_t bmcr, bmsr, anar, lpar;
940
941   IFPRINTF(2, (scp->ifp, "setmedia\n"));
942
943   /* Cancel any pending timeout */
944   callout_stop(&scp->xe_timer);
945   xe_disable_intr(scp);
946
947   /* Select media */
948   scp->media = IFM_ETHER;
949   switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
950
951    case IFM_AUTO:       /* Autoselect media */
952     scp->media = IFM_ETHER|IFM_AUTO;
953
954     /*
955      * Autoselection is really awful.  It goes something like this:
956      *
957      * Wait until the transmitter goes idle (2sec timeout).
958      * Reset card
959      *   IF a 100Mbit PHY exists
960      *     Start NWAY autonegotiation (3.5sec timeout)
961      *     IF that succeeds
962      *       Select 100baseTX or 10baseT, whichever was detected
963      *     ELSE
964      *       Reset card
965      *       IF a 100Mbit PHY exists
966      *         Try to force a 100baseTX link (3sec timeout)
967      *         IF that succeeds
968      *           Select 100baseTX
969      *         ELSE
970      *           Disable the PHY
971      *         ENDIF
972      *       ENDIF
973      *     ENDIF
974      *   ENDIF
975      * IF nothing selected so far
976      *   IF a 100Mbit PHY exists
977      *     Select 10baseT
978      *   ELSE
979      *     Select 10baseT or 10base2, whichever is connected
980      *   ENDIF
981      * ENDIF
982      */
983     switch (scp->autoneg_status) {
984
985     case XE_AUTONEG_NONE:
986       IFPRINTF(2, (scp->ifp, "Waiting for idle transmitter\n"));
987       scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
988       scp->autoneg_status = XE_AUTONEG_WAITING;
989       /* FALL THROUGH */
990
991     case XE_AUTONEG_WAITING:
992       if (scp->tx_queued != 0) {
993         callout_reset(&scp->xe_timer, hz / 2, xe_setmedia, scp);
994         return;
995       }
996       if (scp->phy_ok) {
997         IFPRINTF(2, (scp->ifp, "Starting autonegotiation\n"));
998         bmcr = xe_phy_readreg(scp, PHY_BMCR);
999         bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1000         xe_phy_writereg(scp, PHY_BMCR, bmcr);
1001         anar = xe_phy_readreg(scp, PHY_ANAR);
1002         anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
1003         anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
1004         xe_phy_writereg(scp, PHY_ANAR, anar);
1005         bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
1006         xe_phy_writereg(scp, PHY_BMCR, bmcr);
1007         scp->autoneg_status = XE_AUTONEG_STARTED;
1008         callout_reset(&scp->xe_timer, hz * 7 / 2, xe_setmedia, scp);
1009         return;
1010       }
1011       else {
1012         scp->autoneg_status = XE_AUTONEG_FAIL;
1013       }
1014       break;
1015
1016      case XE_AUTONEG_STARTED:
1017       bmsr = xe_phy_readreg(scp, PHY_BMSR);
1018       lpar = xe_phy_readreg(scp, PHY_LPAR);
1019       if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
1020         IFPRINTF(2, (scp->ifp, "Autonegotiation complete!\n"));
1021         /*
1022          * XXX - Shouldn't have to do this, but (on my hub at least) the
1023          * XXX - transmitter won't work after a successful autoneg.  So we see 
1024          * XXX - what the negotiation result was and force that mode.  I'm
1025          * XXX - sure there is an easy fix for this.
1026          */
1027         if (lpar & PHY_LPAR_100BTXHALF) {
1028           xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1029           XE_MII_DUMP(scp);
1030           XE_SELECT_PAGE(2);
1031           XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1032           scp->media = IFM_ETHER|IFM_100_TX;
1033           scp->autoneg_status = XE_AUTONEG_NONE;
1034         }
1035         else {
1036           /*
1037            * XXX - Bit of a hack going on in here.
1038            * XXX - This is derived from Ken Hughes patch to the Linux driver
1039            * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
1040            * XXX - cards.  What's a CE3B and how's it differ from a plain CE3?
1041            * XXX - these are the things we need to find out.
1042            */
1043           xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1044           XE_SELECT_PAGE(2);
1045           /* BEGIN HACK */
1046           XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1047           XE_SELECT_PAGE(0x42);
1048           XE_OUTB(XE_SWC1, 0x80);
1049           scp->media = IFM_ETHER|IFM_10_T;
1050           scp->autoneg_status = XE_AUTONEG_NONE;
1051           /* END HACK */
1052           /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/  /* Disable PHY? */
1053           /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1054         }
1055       }
1056       else {
1057         IFPRINTF(2, (scp->ifp, "Autonegotiation failed; trying 100baseTX\n"));
1058         XE_MII_DUMP(scp);
1059         if (scp->phy_ok) {
1060           xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1061           scp->autoneg_status = XE_AUTONEG_100TX;
1062           callout_reset(&scp->xe_timer, hz * 3, xe_setmedia, scp);
1063           return;
1064         }
1065         else {
1066           scp->autoneg_status = XE_AUTONEG_FAIL;
1067         }
1068       }
1069       break;
1070
1071      case XE_AUTONEG_100TX:
1072       xe_phy_readreg(scp, PHY_BMSR);
1073       bmsr = xe_phy_readreg(scp, PHY_BMSR);
1074       if (bmsr & PHY_BMSR_LINKSTAT) {
1075         IFPRINTF(2, (scp->ifp, "Got 100baseTX link!\n"));
1076         XE_MII_DUMP(scp);
1077         XE_SELECT_PAGE(2);
1078         XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1079         scp->media = IFM_ETHER|IFM_100_TX;
1080         scp->autoneg_status = XE_AUTONEG_NONE;
1081       }
1082       else {
1083         IFPRINTF(2, (scp->ifp, "Autonegotiation failed; disabling PHY\n"));
1084         XE_MII_DUMP(scp);
1085         xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1086         XE_SELECT_PAGE(2);
1087         XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);        /* Disable PHY? */
1088         scp->autoneg_status = XE_AUTONEG_FAIL;
1089       }
1090       break;
1091     }
1092
1093     /*
1094      * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1095      * either autonegotiation failed, or never got started to begin with.  In
1096      * either case, select a suitable 10Mbit media and hope it works.  We
1097      * don't need to reset the card again, since it will have been done
1098      * already by the big switch above.
1099      */
1100     if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1101       IFPRINTF(2, (scp->ifp, "Selecting 10baseX\n"));
1102       if (scp->mohawk) {
1103         XE_SELECT_PAGE(0x42);
1104         XE_OUTB(XE_SWC1, 0x80);
1105         scp->media = IFM_ETHER|IFM_10_T;
1106         scp->autoneg_status = XE_AUTONEG_NONE;
1107       }
1108       else {
1109         XE_SELECT_PAGE(4);
1110         XE_OUTB(XE_GPR0, 4);
1111         DELAY(50000);
1112         XE_SELECT_PAGE(0x42);
1113         XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
1114         scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1115         scp->autoneg_status = XE_AUTONEG_NONE;
1116       }
1117     }
1118     break;
1119
1120
1121     /*
1122      * If a specific media has been requested, we just reset the card and
1123      * select it (one small exception -- if 100baseTX is requested by there is 
1124      * no PHY, we fall back to 10baseT operation).
1125      */
1126    case IFM_100_TX:     /* Force 100baseTX */
1127     if (scp->phy_ok) {
1128       IFPRINTF(2, (scp->ifp, "Selecting 100baseTX\n"));
1129       XE_SELECT_PAGE(0x42);
1130       XE_OUTB(XE_SWC1, 0);
1131       xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1132       XE_SELECT_PAGE(2);
1133       XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1134       scp->media |= IFM_100_TX;
1135       break;
1136     }
1137     /* FALLTHROUGH */
1138
1139    case IFM_10_T:       /* Force 10baseT */
1140     IFPRINTF(2, (scp->ifp, "Selecting 10baseT\n"));
1141     if (scp->phy_ok) {
1142       xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1143       XE_SELECT_PAGE(2);
1144       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);  /* Disable PHY */
1145     }
1146     XE_SELECT_PAGE(0x42);
1147     XE_OUTB(XE_SWC1, 0x80);
1148     scp->media |= IFM_10_T;
1149     break;
1150
1151    case IFM_10_2:
1152     IFPRINTF(2, (scp->ifp, "Selecting 10base2\n"));
1153     XE_SELECT_PAGE(0x42);
1154     XE_OUTB(XE_SWC1, 0xc0);
1155     scp->media |= IFM_10_2;
1156     break;
1157   }
1158
1159
1160   /*
1161    * Finally, the LEDs are set to match whatever media was chosen and the
1162    * transmitter is unblocked. 
1163    */
1164   IFPRINTF(2, (scp->ifp, "Setting LEDs\n"));
1165   XE_SELECT_PAGE(2);
1166   switch (IFM_SUBTYPE(scp->media)) {
1167    case IFM_100_TX:
1168    case IFM_10_T:
1169     XE_OUTB(XE_LED, 0x3b);
1170     if (scp->dingo)
1171       XE_OUTB(0x0b, 0x04);      /* 100Mbit LED */
1172     break;
1173
1174    case IFM_10_2:
1175     XE_OUTB(XE_LED, 0x3a);
1176     break;
1177   }
1178
1179   /* Restart output? */
1180   xe_enable_intr(scp);
1181   scp->ifp->if_flags &= ~IFF_OACTIVE;
1182   if_devstart(scp->ifp);
1183 }
1184
1185
1186 /*
1187  * Hard reset (power cycle) the card.
1188  */
1189 static void
1190 xe_reset(struct xe_softc *scp) {
1191   IFPRINTF(2, (scp->ifp, "hard_reset\n"));
1192
1193   crit_enter();
1194
1195   /* Power down */
1196   XE_SELECT_PAGE(4);
1197   XE_OUTB(XE_GPR1, 0);
1198   DELAY(40000);
1199
1200   /* Power up again */
1201   if (scp->mohawk)
1202     XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1203   else
1204     XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
1205
1206   DELAY(40000);
1207   XE_SELECT_PAGE(0);
1208
1209   crit_exit();
1210 }
1211
1212
1213 /*
1214  * Take interface offline.  This is done by powering down the device, which I
1215  * assume means just shutting down the transceiver and Ethernet logic.  This
1216  * requires a _hard_ reset to recover from, as we need to power up again.
1217  */
1218 static void
1219 xe_stop(struct xe_softc *scp) {
1220   IFPRINTF(2, (scp->ifp, "stop\n"));
1221
1222   crit_enter();
1223
1224   /*
1225    * Shut off interrupts.
1226    */
1227   xe_disable_intr(scp);
1228
1229   /*
1230    * Power down.
1231    */
1232   XE_SELECT_PAGE(4);
1233   XE_OUTB(XE_GPR1, 0);
1234   XE_SELECT_PAGE(0);
1235   if (scp->mohawk) {
1236     /*
1237      * set GP1 and GP2 as outputs (bits 2 & 3)
1238      * set GP1 high to power on the ML6692 (bit 0)
1239      * set GP2 low to power on the 10Mhz chip (bit 1)
1240      */
1241     XE_SELECT_PAGE(4);
1242     XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP1_OUT);
1243   }
1244
1245   /*
1246    * ~IFF_RUNNING == interface down.
1247    */
1248   scp->ifp->if_flags &= ~IFF_RUNNING;
1249   scp->ifp->if_flags &= ~IFF_OACTIVE;
1250   scp->ifp->if_timer = 0;
1251
1252   crit_exit();
1253 }
1254
1255
1256 /*
1257  * Enable interrupts from the card.
1258  */
1259 static void
1260 xe_enable_intr(struct xe_softc *scp) {
1261   IFPRINTF(2, (scp->ifp, "enable_intr\n"));
1262
1263   XE_SELECT_PAGE(0);
1264   XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);    /* Enable interrupts */
1265   if (scp->modem && !scp->dingo) {      /* This bit is just magic */
1266     if (!(XE_INB(0x10) & 0x01)) {
1267       XE_OUTB(0x10, 0x11);              /* Unmask master int enable bit */
1268     }
1269   }
1270 }
1271
1272
1273 /*
1274  * Disable interrupts from the card.
1275  */
1276 static void
1277 xe_disable_intr(struct xe_softc *scp) {
1278   IFPRINTF(2, (scp->ifp, "disable_intr\n"));
1279
1280   XE_SELECT_PAGE(0);
1281   XE_OUTB(XE_CR, 0);                    /* Disable interrupts */
1282   if (scp->modem && !scp->dingo) {      /* More magic */
1283     XE_OUTB(0x10, 0x10);                /* Mask the master int enable bit */
1284   }
1285 }
1286
1287
1288 /*
1289  * Set up multicast filter and promiscuous modes.
1290  */
1291 static void
1292 xe_set_multicast(struct xe_softc *scp) {
1293   struct ifnet *ifp;
1294   struct ifmultiaddr *ifma;
1295   u_int count, i;
1296
1297   ifp = &scp->arpcom.ac_if;
1298
1299   IFPRINTF(2, (ifp, "set_multicast\n"));
1300
1301   XE_SELECT_PAGE(0x42);
1302
1303   /* Handle PROMISC flag */
1304   if (ifp->if_flags & IFF_PROMISC) {
1305     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1306     return;
1307   }
1308   else
1309     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1310
1311   /* Handle ALLMULTI flag */
1312   if (ifp->if_flags & IFF_ALLMULTI) {
1313     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1314     return;
1315   }
1316   else
1317     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1318
1319   /* Iterate over multicast address list */
1320   count = 0;
1321   LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1322     if (ifma->ifma_addr->sa_family != AF_LINK)
1323       continue;
1324
1325     count++;
1326
1327     if (count < 10)
1328       /* First 9 use Individual Addresses for exact matching */
1329       xe_set_addr(scp, LLADDR((struct sockaddr_dl *)ifma->ifma_addr), count);
1330     else
1331       if (scp->mohawk)
1332         /* Use hash filter on Mohawk and Dingo */
1333         xe_set_hash(scp, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1334       else
1335         /* Nowhere else to put them on CE2 */
1336         break;
1337   }
1338
1339   IFPRINTF(2, (ifp, "set_multicast: count = %u\n", count));
1340
1341   /* Now do some cleanup and enable multicast handling as needed */
1342   if (count == 0) {
1343     /* Disable all multicast handling */
1344
1345     XE_SELECT_PAGE(0x42);
1346     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~(XE_SWC1_IA_ENABLE|XE_SWC1_ALLMULTI));
1347     if (scp->mohawk) {
1348       XE_SELECT_PAGE(0x02);
1349       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1350     }
1351   }
1352   else if (count < 10) {
1353     /* Full in any unused Individual Addresses with our MAC address */
1354     for (i = count + 1; i < 10; i++)
1355       xe_set_addr(scp, (u_int8_t *)(&scp->arpcom.ac_enaddr), i);
1356     /* Enable Individual Address matching only */
1357     XE_SELECT_PAGE(0x42);
1358     XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1359     if (scp->mohawk) {
1360       XE_SELECT_PAGE(0x02);
1361       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1362     }
1363   }    
1364   else {
1365     if (scp->mohawk) {
1366       /* Check whether hash table is full */
1367       XE_SELECT_PAGE(0x58);
1368       for (i = 0x08; i < 0x10; i++)
1369         if (XE_INB(i) != 0xff)
1370           break;
1371       if (i == 0x10) {
1372         /* Hash table full - enable promiscuous multicast matching */
1373         XE_SELECT_PAGE(0x42);
1374         XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1375         XE_SELECT_PAGE(0x02);
1376         XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1377       }
1378       else {
1379         /* Enable hash table and Individual Address matching */
1380         XE_SELECT_PAGE(0x42);
1381         XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1382         XE_SELECT_PAGE(0x02);
1383         XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1384       }
1385     }
1386     else {
1387       /* Enable promiscuous multicast matching */
1388       XE_SELECT_PAGE(0x42);
1389       XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1390     }
1391   }
1392   XE_SELECT_PAGE(0);
1393 }
1394
1395
1396 /*
1397  * Copy the Ethernet multicast address in addr to the on-chip registers for
1398  * Individual Address idx.  Assumes that addr is really a multicast address
1399  * and that idx > 0 (slot 0 is always used for the card MAC address).
1400  */
1401 static void
1402 xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
1403   uint8_t page, reg;
1404   u_int i;
1405
1406   /*
1407    * Individual Addresses are stored in registers 8-F of pages 0x50-0x57.  IA1
1408    * therefore starts at register 0xE on page 0x50.  The expressions below
1409    * compute the starting page and register for any IA index > 0.
1410    */
1411   --idx;
1412   page = 0x50 + idx%4 + idx/4*3;
1413   reg = 0x0e - 2 * (idx%4);
1414
1415   IFPRINTF(3, (scp->ifp, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
1416                idx+1, page, reg));
1417
1418   /*
1419    * Copy the IA bytes.  Note that the byte order is reversed for Mohawk and
1420    * Dingo wrt. CE2 hardware.
1421    */
1422   XE_SELECT_PAGE(page);
1423   for (i = 0; i < 6; i++) {
1424 #ifdef XE_DEBUG
1425     if (i > 0) {
1426       DPRINTF(3, (":%02x", addr[i]));
1427     } else {
1428       IFPRINTF(3, (scp->ifp, "set_addr: %02x", addr[0]));
1429     }
1430 #endif
1431     XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1432     if (++reg == 0x10) {
1433       reg = 0x08;
1434       XE_SELECT_PAGE(++page);
1435     }
1436   }
1437   DPRINTF(3, ("\n"));
1438 }
1439
1440
1441 /*
1442  * Set the appropriate bit in the multicast hash table for the supplied
1443  * Ethernet multicast address addr.  Assumes that addr is really a multicast
1444  * address.
1445  */
1446 static void
1447 xe_set_hash(struct xe_softc* scp, u_int8_t* addr) {
1448   u_int32_t crc = 0xffffffff;
1449   u_int8_t bit, byte, crc31, idx;
1450   u_int i, j;
1451
1452   /* Compute CRC of the address -- standard Ethernet CRC function */
1453   for (i = 0; i < 6; i++) {
1454     byte = addr[i];
1455     for (j = 1; j <= 8; j++) {
1456       if (crc & 0x80000000)
1457         crc31 = 0x01;
1458       else
1459         crc31 = 0;
1460       bit = crc31 ^ (byte & 0x01);
1461       crc <<= 1;
1462       byte >>= 1;
1463       if (bit)
1464         crc = (crc ^ XE_CRC_POLY)|1;
1465     }
1466   }
1467
1468   IFPRINTF(3, (scp->ifp, "set_hash: CRC = 0x%08x\n", crc));
1469
1470   /* Hash table index = 6 msbs of CRC, reversed */
1471   for (i = 0, idx = 0; i < 6; i++) {
1472     idx >>= 1;
1473     if (crc & 0x80000000) {
1474       idx |= 0x20;
1475     }
1476     crc <<= 1;
1477   }
1478  
1479   /* Top 3 bits of idx give register - 8, bottom 3 give bit within register */
1480   byte = idx >> 3 | 0x08;
1481   bit = 0x01 << (idx & 0x07);
1482
1483   IFPRINTF(3, (scp->ifp,
1484                "set_hash: idx = 0x%02x, byte = 0x%02x, bit = 0x%02x\n",
1485                idx, byte, bit));
1486
1487   XE_SELECT_PAGE(0x58);
1488   XE_OUTB(byte, XE_INB(byte) | bit);
1489 }
1490
1491
1492 /*
1493  * Write an outgoing packet to the card using programmed I/O.
1494  */
1495 static int
1496 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
1497   u_int len, pad;
1498   u_char wantbyte;
1499   u_int8_t *data;
1500   u_int8_t savebyte[2];
1501
1502   /* Get total packet length */
1503   if (mbp->m_flags & M_PKTHDR)
1504     len = mbp->m_pkthdr.len;
1505   else {
1506     struct mbuf* mbp2 = mbp;
1507     for (len = 0; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
1508   }
1509
1510   IFPRINTF(3, (scp->ifp, "pio_write_packet: len = %u\n", len));
1511
1512   /* Packets < minimum length may need to be padded out */
1513   pad = 0;
1514   if (len < scp->tx_min) {
1515     pad = scp->tx_min - len;
1516     len = scp->tx_min;
1517   }
1518
1519   /* Check transmit buffer space */
1520   XE_SELECT_PAGE(0);
1521   XE_OUTW(XE_TRS, len+2);       /* Only effective on rev. 1 CE2 cards */
1522   if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1523     return 1;
1524
1525   /* Send packet length to card */
1526   XE_OUTW(XE_EDP, len);
1527
1528   /*
1529    * Write packet to card using PIO (code stolen from the ed driver)
1530    */
1531   wantbyte = 0;
1532   while (mbp != NULL) {
1533     len = mbp->m_len;
1534     if (len > 0) {
1535       data = mtod(mbp, caddr_t);
1536       if (wantbyte) {           /* Finish the last word */
1537         savebyte[1] = *data;
1538         XE_OUTW(XE_EDP, *(u_short *)savebyte);
1539         data++;
1540         len--;
1541         wantbyte = 0;
1542       }
1543       if (len > 1) {            /* Output contiguous words */
1544         bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1545          len >> 1);
1546         data += len & ~1;
1547         len &= 1;
1548       }
1549       if (len == 1) {           /* Save last byte, if necessary */
1550         savebyte[0] = *data;
1551         wantbyte = 1;
1552       }
1553     }
1554     mbp = mbp->m_next;
1555   }
1556
1557   /*
1558    * Send last byte of odd-length packets
1559    */
1560   if (wantbyte)
1561     XE_OUTB(XE_EDP, savebyte[0]);
1562
1563   /*
1564    * Can just tell CE3 cards to send; short packets will be padded out with
1565    * random cruft automatically.  For CE2, manually pad the packet with
1566    * garbage; it will be sent when the required number or bytes have been
1567    * delivered to the card.
1568    */
1569   if (scp->mohawk)
1570     XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX | XE_CR_ENABLE_INTR);
1571   else if (pad > 0) {
1572     if (pad & 0x01)
1573       XE_OUTB(XE_EDP, 0xaa);
1574     pad >>= 1;
1575     while (pad > 0) {
1576       XE_OUTW(XE_EDP, 0xdead);
1577       pad--;
1578     }
1579   }
1580
1581   return 0;
1582 }
1583
1584
1585 /**************************************************************
1586  *                                                            *
1587  *                  M I I  F U N C T I O N S                  *
1588  *                                                            *
1589  **************************************************************/
1590
1591 /*
1592  * Alternative MII/PHY handling code adapted from the xl driver.  It doesn't
1593  * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1594  * XXX - this stuff shouldn't be here.  It should all be abstracted off to
1595  * XXX - some kind of common MII-handling code, shared by all drivers.  But
1596  * XXX - that's a whole other mission.
1597  */
1598 #define XE_MII_SET(x)   XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1599 #define XE_MII_CLR(x)   XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1600
1601
1602 /*
1603  * Sync the PHYs by setting data bit and strobing the clock 32 times.
1604  */
1605 static void
1606 xe_mii_sync(struct xe_softc *scp) {
1607   int i;
1608
1609   XE_SELECT_PAGE(2);
1610   XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1611
1612   for (i = 0; i < 32; i++) {
1613     XE_MII_SET(XE_MII_CLK);
1614     DELAY(1);
1615     XE_MII_CLR(XE_MII_CLK);
1616     DELAY(1);
1617   }
1618 }
1619
1620
1621 /*
1622  * Look for a MII-compliant PHY.  If we find one, reset it.
1623  */
1624 static int
1625 xe_mii_init(struct xe_softc *scp) {
1626   u_int16_t status;
1627
1628   status = xe_phy_readreg(scp, PHY_BMSR);
1629   if ((status & 0xff00) != 0x7800) {
1630     IFPRINTF(2, (scp->ifp, "no PHY found, %0x\n", status));
1631     return 0;
1632   }
1633   else {
1634     IFPRINTF(2, (scp->ifp, "PHY OK!\n"));
1635
1636     /* Reset the PHY */
1637     xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1638     DELAY(500);
1639     while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1640     XE_MII_DUMP(scp);
1641     return 1;
1642   }
1643 }
1644
1645
1646 /*
1647  * Clock a series of bits through the MII.
1648  */
1649 static void
1650 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
1651   int i;
1652
1653   XE_SELECT_PAGE(2);
1654   XE_MII_CLR(XE_MII_CLK);
1655   
1656   for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1657     if (bits & i) {
1658       XE_MII_SET(XE_MII_WRD);
1659     } else {
1660       XE_MII_CLR(XE_MII_WRD);
1661     }
1662     DELAY(1);
1663     XE_MII_CLR(XE_MII_CLK);
1664     DELAY(1);
1665     XE_MII_SET(XE_MII_CLK);
1666   }
1667 }
1668
1669
1670 /*
1671  * Read an PHY register through the MII.
1672  */
1673 static int
1674 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1675   int i, ack;
1676
1677   crit_enter();
1678
1679   /*
1680    * Set up frame for RX.
1681    */
1682   frame->mii_stdelim = XE_MII_STARTDELIM;
1683   frame->mii_opcode = XE_MII_READOP;
1684   frame->mii_turnaround = 0;
1685   frame->mii_data = 0;
1686         
1687   XE_SELECT_PAGE(2);
1688   XE_OUTB(XE_GPR2, 0);
1689
1690   /*
1691    * Turn on data xmit.
1692    */
1693   XE_MII_SET(XE_MII_DIR);
1694
1695   xe_mii_sync(scp);
1696
1697   /*    
1698    * Send command/address info.
1699    */
1700   xe_mii_send(scp, frame->mii_stdelim, 2);
1701   xe_mii_send(scp, frame->mii_opcode, 2);
1702   xe_mii_send(scp, frame->mii_phyaddr, 5);
1703   xe_mii_send(scp, frame->mii_regaddr, 5);
1704
1705   /* Idle bit */
1706   XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1707   DELAY(1);
1708   XE_MII_SET(XE_MII_CLK);
1709   DELAY(1);
1710
1711   /* Turn off xmit. */
1712   XE_MII_CLR(XE_MII_DIR);
1713
1714   /* Check for ack */
1715   XE_MII_CLR(XE_MII_CLK);
1716   DELAY(1);
1717   ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1718   XE_MII_SET(XE_MII_CLK);
1719   DELAY(1);
1720
1721   /*
1722    * Now try reading data bits. If the ack failed, we still
1723    * need to clock through 16 cycles to keep the PHY(s) in sync.
1724    */
1725   if (ack) {
1726     for(i = 0; i < 16; i++) {
1727       XE_MII_CLR(XE_MII_CLK);
1728       DELAY(1);
1729       XE_MII_SET(XE_MII_CLK);
1730       DELAY(1);
1731     }
1732     goto fail;
1733   }
1734
1735   for (i = 0x8000; i; i >>= 1) {
1736     XE_MII_CLR(XE_MII_CLK);
1737     DELAY(1);
1738     if (!ack) {
1739       if (XE_INB(XE_GPR2) & XE_MII_RDD)
1740         frame->mii_data |= i;
1741       DELAY(1);
1742     }
1743     XE_MII_SET(XE_MII_CLK);
1744     DELAY(1);
1745   }
1746
1747 fail:
1748
1749   XE_MII_CLR(XE_MII_CLK);
1750   DELAY(1);
1751   XE_MII_SET(XE_MII_CLK);
1752   DELAY(1);
1753
1754   crit_exit();
1755
1756   if (ack)
1757     return(1);
1758   return(0);
1759 }
1760
1761
1762 /*
1763  * Write to a PHY register through the MII.
1764  */
1765 static int
1766 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1767
1768   crit_enter();
1769
1770   /*
1771    * Set up frame for TX.
1772    */
1773   frame->mii_stdelim = XE_MII_STARTDELIM;
1774   frame->mii_opcode = XE_MII_WRITEOP;
1775   frame->mii_turnaround = XE_MII_TURNAROUND;
1776         
1777   XE_SELECT_PAGE(2);
1778
1779   /*            
1780    * Turn on data output.
1781    */
1782   XE_MII_SET(XE_MII_DIR);
1783
1784   xe_mii_sync(scp);
1785
1786   xe_mii_send(scp, frame->mii_stdelim, 2);
1787   xe_mii_send(scp, frame->mii_opcode, 2);
1788   xe_mii_send(scp, frame->mii_phyaddr, 5);
1789   xe_mii_send(scp, frame->mii_regaddr, 5);
1790   xe_mii_send(scp, frame->mii_turnaround, 2);
1791   xe_mii_send(scp, frame->mii_data, 16);
1792
1793   /* Idle bit. */
1794   XE_MII_SET(XE_MII_CLK);
1795   DELAY(1);
1796   XE_MII_CLR(XE_MII_CLK);
1797   DELAY(1);
1798
1799   /*
1800    * Turn off xmit.
1801    */
1802   XE_MII_CLR(XE_MII_DIR);
1803
1804   crit_exit();
1805
1806   return(0);
1807 }
1808
1809
1810 /*
1811  * Read a register from the PHY.
1812  */
1813 static u_int16_t
1814 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
1815   struct xe_mii_frame frame;
1816
1817   bzero((char *)&frame, sizeof(frame));
1818
1819   frame.mii_phyaddr = 0;
1820   frame.mii_regaddr = reg;
1821   xe_mii_readreg(scp, &frame);
1822
1823   return(frame.mii_data);
1824 }
1825
1826
1827 /*
1828  * Write to a PHY register.
1829  */
1830 static void
1831 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
1832   struct xe_mii_frame frame;
1833
1834   bzero((char *)&frame, sizeof(frame));
1835
1836   frame.mii_phyaddr = 0;
1837   frame.mii_regaddr = reg;
1838   frame.mii_data = data;
1839   xe_mii_writereg(scp, &frame);
1840
1841   return;
1842 }
1843
1844
1845 /*
1846  * A bit of debugging code.
1847  */
1848 static void
1849 xe_mii_dump(struct xe_softc *scp) {
1850   int i;
1851
1852   crit_enter();
1853
1854   if_printf(scp->ifp, "MII registers: ");
1855   for (i = 0; i < 2; i++) {
1856     kprintf(" %d:%04x", i, xe_phy_readreg(scp, i));
1857   }
1858   for (i = 4; i < 7; i++) {
1859     kprintf(" %d:%04x", i, xe_phy_readreg(scp, i));
1860   }
1861   kprintf("\n");
1862
1863   crit_exit();
1864 }
1865
1866 static void
1867 xe_reg_dump(struct xe_softc *scp) {
1868   int page, i;
1869
1870   crit_enter();
1871
1872   if_printf(scp->ifp, "Common registers: ");
1873   for (i = 0; i < 8; i++) {
1874     kprintf(" %2.2x", XE_INB(i));
1875   }
1876   kprintf("\n");
1877
1878   for (page = 0; page <= 8; page++) {
1879     if_printf(scp->ifp, "Register page %2.2x: ", page);
1880     XE_SELECT_PAGE(page);
1881     for (i = 8; i < 16; i++) {
1882       kprintf(" %2.2x", XE_INB(i));
1883     }
1884     kprintf("\n");
1885   }
1886
1887   for (page = 0x10; page < 0x5f; page++) {
1888     if ((page >= 0x11 && page <= 0x3f) ||
1889         (page == 0x41) ||
1890         (page >= 0x43 && page <= 0x4f) ||
1891         (page >= 0x59))
1892       continue;
1893     if_printf(scp->ifp, "Register page %2.2x: ", page);
1894     XE_SELECT_PAGE(page);
1895     for (i = 8; i < 16; i++) {
1896       kprintf(" %2.2x", XE_INB(i));
1897     }
1898     kprintf("\n");
1899   }
1900
1901   crit_exit();
1902 }
1903
1904 int
1905 xe_activate(device_t dev)
1906 {
1907         struct xe_softc *sc = device_get_softc(dev);
1908         int start, i;
1909
1910         DEVPRINTF(2, (dev, "activate\n"));
1911
1912         if (!sc->modem) {
1913                 sc->port_rid = 0;       /* 0 is managed by pccard */
1914                 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1915                     &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
1916         } else if (sc->dingo) {
1917                 /*
1918                  * Find a 16 byte aligned ioport for the card.
1919                  */
1920                 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1921                 sc->port_rid = 1;       /* 0 is managed by pccard */
1922                 start = 0x100;
1923                 do {
1924                         sc->port_res = bus_alloc_resource(dev,
1925                             SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
1926                             RF_ACTIVE);
1927                         if (sc->port_res == 0)
1928                                 break;          /* we failed */
1929                         if ((rman_get_start(sc->port_res) & 0xf) == 0)
1930                                 break;          /* good */
1931                         bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
1932                             sc->port_res);
1933                         start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1934                 } while (1);
1935                 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
1936                     bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1937                     bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1938         }
1939         else if (sc->ce2) {
1940             /*
1941              * Find contiguous I/O port for the Ethernet function on CEM2 and
1942              * CEM3 cards.  We allocate window 0 wherever pccard has decided
1943              * it should be, then find an available window adjacent to it for
1944              * the second function.  Not sure that both windows are actually
1945              * needed.
1946              */
1947             DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
1948             sc->ce2_port_rid = 0;       /* 0 is managed by pccard */
1949             sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1950                                                   &sc->ce2_port_rid, 0, ~0,
1951                                                   8, RF_ACTIVE);
1952             if (!sc->ce2_port_res) {
1953                 device_printf(dev, "Cannot allocate I/O port for modem\n");
1954                 return ENOMEM;
1955             }
1956
1957             sc->port_rid = 1;
1958             start = bus_get_resource_start(dev, SYS_RES_IOPORT,
1959                                            sc->ce2_port_rid);
1960             for (i = 0; i < 2; i++) {
1961                 start += (i == 0 ? 8 : -24);
1962                 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1963                                                   &sc->port_rid, start,
1964                                                   start + 18, 18, RF_ACTIVE);
1965                 if (sc->port_res == 0)
1966                     continue;   /* Failed, try again if possible */
1967                 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
1968                                            sc->port_rid) == start)
1969                     break;      /* Success! */
1970
1971                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1972                                      sc->port_res);
1973                 sc->port_res = 0;
1974             }
1975             DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
1976                     bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1977                     bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1978         }
1979         if (!sc->port_res) {
1980                 device_printf(dev, "Cannot allocate ioport\n");
1981                 return ENOMEM;
1982         }
1983
1984         sc->irq_rid = 0;
1985         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 
1986             RF_ACTIVE);
1987         if (!sc->irq_res) {
1988                 device_printf(dev, "Cannot allocate irq\n");
1989                 xe_deactivate(dev);
1990                 return ENOMEM;
1991         }
1992
1993         sc->bst = rman_get_bustag(sc->port_res);
1994         sc->bsh = rman_get_bushandle(sc->port_res);
1995         return (0);
1996 }
1997
1998 void
1999 xe_deactivate(device_t dev)
2000 {
2001         struct xe_softc *sc = device_get_softc(dev);
2002
2003         DEVPRINTF(2, (dev, "deactivate\n"));
2004         xe_disable_intr(sc);
2005
2006         if (sc->port_res)
2007                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
2008                     sc->port_res);
2009         sc->port_res = 0;
2010         if (sc->ce2_port_res)
2011             bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2012                                  sc->ce2_port_res);
2013         sc->ce2_port_res = 0;
2014         if (sc->irq_res)
2015                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 
2016                     sc->irq_res);
2017         sc->irq_res = 0;
2018 }