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