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