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