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