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