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