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