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