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