if: Move IFF_OACTIVE bit into ifaltq; prepare multiple TX queues support
[dragonfly.git] / sys / dev / netif / xe / if_xe.c
CommitLineData
984263bc 1/*-
7261a835 2 * Copyright (c) 1998, 1999, 2003 Scott Mitchell
984263bc
MD
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 $
7261a835 27 * $FreeBSD: src/sys/dev/xe/if_xe.c,v 1.39 2003/10/14 22:51:35 rsm Exp $
984263bc
MD
28 */
29
30/*
31 * Portions of this software were derived from Werner Koch's xirc2ps driver
32 * for Linux under the terms of the following license (from v1.30 of the
33 * xirc2ps driver):
34 *
35 * Copyright (c) 1997 by Werner Koch (dd9jn)
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, and the entire permission notice in its entirety,
42 * including the disclaimer of warranties.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. The name of the author may not be used to endorse or promote
47 * products derived from this software without specific prior
48 * written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
51 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
54 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
60 * OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63/*
64 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
65 * following cards are currently known to work with the driver:
66 * Xircom CreditCard 10/100 (CE3)
67 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
68 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
69 * Xircom RealPort Ethernet 10
70 * Xircom RealPort Ethernet 10/100
71 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
72 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
73 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
74 *
75 * Some other cards *should* work, but support for them is either broken or in
76 * an unknown state at the moment. I'm always interested in hearing from
77 * people who own any of these cards:
78 * Xircom CreditCard 10Base-T (PS-CE2-10)
79 * Xircom CreditCard Ethernet + ModemII (CEM2)
80 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
81 *
82 * Thanks to all who assisted with the development and testing of the driver,
83 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
84 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
85 * out anyone who deserves a mention here.
86 *
87 * Special thanks to Ade Lovett for both hosting the mailing list and doing
88 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
89 * the web pages.
90 *
984263bc 91 * Author email: <scott@uk.freebsd.org>
7261a835 92 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
984263bc
MD
93 */
94
95
96#include <sys/param.h>
984263bc
MD
97#include <sys/errno.h>
98#include <sys/kernel.h>
99#include <sys/mbuf.h>
100#include <sys/select.h>
101#include <sys/socket.h>
102#include <sys/sockio.h>
7261a835 103#include <sys/sysctl.h>
984263bc
MD
104#include <sys/systm.h>
105#include <sys/uio.h>
78195a76 106#include <sys/serialize.h>
984263bc
MD
107#include <sys/module.h>
108#include <sys/bus.h>
984263bc 109#include <sys/rman.h>
1f7ab7c9 110#include <sys/thread2.h>
9db4b353 111#include <sys/interrupt.h>
984263bc
MD
112
113#include <net/ethernet.h>
114#include <net/if.h>
1a3869d2 115#include <net/ifq_var.h>
984263bc
MD
116#include <net/if_arp.h>
117#include <net/if_dl.h>
118#include <net/if_media.h>
119#include <net/if_mib.h>
120#include <net/bpf.h>
121
1f2de5d4
MD
122#include "if_xereg.h"
123#include "if_xevar.h"
984263bc 124
984263bc
MD
125/*
126 * MII command structure
127 */
128struct xe_mii_frame {
129 u_int8_t mii_stdelim;
130 u_int8_t mii_opcode;
131 u_int8_t mii_phyaddr;
132 u_int8_t mii_regaddr;
133 u_int8_t mii_turnaround;
134 u_int16_t mii_data;
135};
136
137/*
138 * Media autonegotiation progress constants
139 */
140#define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
141#define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
142#define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
143#define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
144#define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
145
7261a835
SZ
146/*
147 * Multicast hashing CRC constants
148 */
149#define XE_CRC_POLY 0x04c11db6
984263bc
MD
150
151/*
152 * Prototypes start here
153 */
984263bc 154static void xe_init (void *xscp);
7cdc3d6e 155static void xe_intr (void *xscp);
984263bc 156static void xe_start (struct ifnet *ifp);
bd4539cc 157static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *);
984263bc
MD
158static void xe_watchdog (struct ifnet *ifp);
159static int xe_media_change (struct ifnet *ifp);
160static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
161static timeout_t xe_setmedia;
78195a76 162static timeout_t xe_setmedia_serialized;
7261a835 163static void xe_reset (struct xe_softc *scp);
984263bc
MD
164static void xe_stop (struct xe_softc *scp);
165static void xe_enable_intr (struct xe_softc *scp);
166static void xe_disable_intr (struct xe_softc *scp);
7261a835
SZ
167static void xe_set_multicast (struct xe_softc *scp);
168static void xe_set_addr (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
169static void xe_set_hash (struct xe_softc *scp, u_int8_t* addr);
984263bc 170static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
984263bc
MD
171
172/*
173 * MII functions
174 */
175static void xe_mii_sync (struct xe_softc *scp);
176static int xe_mii_init (struct xe_softc *scp);
177static void xe_mii_send (struct xe_softc *scp, u_int32_t bits, int cnt);
178static int xe_mii_readreg (struct xe_softc *scp, struct xe_mii_frame *frame);
179static int xe_mii_writereg (struct xe_softc *scp, struct xe_mii_frame *frame);
180static u_int16_t xe_phy_readreg (struct xe_softc *scp, u_int16_t reg);
181static void xe_phy_writereg (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
182
7261a835 183/* Debugging functions */
cdf89432 184static void xe_reg_dump (struct xe_softc *scp) __unused;
984263bc 185static void xe_mii_dump (struct xe_softc *scp);
7261a835
SZ
186
187#define XE_DEBUG
188
189#ifdef XE_DEBUG
190
191/* sysctl vars */
192SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "xe parameters");
193
194/*
195 * Debug logging levels - set with hw.xe.debug sysctl
196 * 0 = None
197 * 1 = More hardware details, probe/attach progress
198 * 2 = Most function calls, ioctls and media selection progress
199 * 3 = Everything - interrupts, packets in/out and multicast address setup
200 */
201int xe_debug = 1;
202SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0, "xe debug level");
203
e3869ec7 204#define DPRINTF(level, arg) if (xe_debug >= (level)) kprintf arg
7261a835
SZ
205#define IFPRINTF(level, arg) if (xe_debug >= (level)) if_printf arg
206#define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
207#define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
208#define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
209
210#else /* !XE_DEBUG */
211
212#define DPRINTF(level, arg)
213#define IFPRINTF(level, arg)
214#define DEVPRINTF(level, arg)
984263bc
MD
215#define XE_REG_DUMP(scp)
216#define XE_MII_DUMP(scp)
7261a835
SZ
217
218#endif /* XE_DEBUG */
984263bc 219
984263bc
MD
220/*
221 * The device entry is being removed, probably because someone ejected the
222 * card. The interface should have been brought down manually before calling
223 * this function; if not you may well lose packets. In any case, I shut down
224 * the card and the interface, and hope for the best.
225 */
f572e449
JS
226int
227xe_detach(device_t dev)
228{
984263bc 229 struct xe_softc *sc = device_get_softc(dev);
cdf89432 230 struct ifnet *ifp = &sc->arpcom.ac_if;
984263bc 231
cdf89432 232 lwkt_serialize_enter(ifp->if_serializer);
7cdc3d6e 233
cdf89432 234 ifp->if_flags &= ~IFF_RUNNING;
74a0ee08 235 callout_stop(&sc->xe_timer);
7cdc3d6e
JS
236 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
237
cdf89432 238 lwkt_serialize_exit(ifp->if_serializer);
7cdc3d6e 239
cdf89432 240 ether_ifdetach(ifp);
984263bc 241 xe_deactivate(dev);
cdf89432 242
984263bc
MD
243 return 0;
244}
245
246/*
247 * Attach a device.
248 */
f572e449
JS
249int
250xe_attach (device_t dev)
251{
984263bc 252 struct xe_softc *scp = device_get_softc(dev);
7cdc3d6e 253 int err;
984263bc 254
7261a835 255 DEVPRINTF(2, (dev, "attach\n"));
984263bc 256
984263bc
MD
257 /* Fill in some private data */
258 scp->ifp = &scp->arpcom.ac_if;
259 scp->ifm = &scp->ifmedia;
7261a835 260 scp->autoneg_status = XE_AUTONEG_NONE;
984263bc 261
984263bc 262 /* Initialise the ifnet structure */
3e4a09e7 263 scp->ifp->if_softc = scp;
2682bd2f 264 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
3e4a09e7
MD
265 scp->ifp->if_timer = 0;
266 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
267 scp->ifp->if_linkmib = &scp->mibdata;
268 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
3e4a09e7
MD
269 scp->ifp->if_start = xe_start;
270 scp->ifp->if_ioctl = xe_ioctl;
271 scp->ifp->if_watchdog = xe_watchdog;
272 scp->ifp->if_init = xe_init;
7261a835 273 scp->ifp->if_baudrate = 100000000;
1a3869d2
JS
274 ifq_set_maxlen(&scp->ifp->if_snd, IFQ_MAXLEN);
275 ifq_set_ready(&scp->ifp->if_snd);
984263bc
MD
276
277 /* Initialise the ifmedia structure */
278 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
74a0ee08 279 callout_init(&scp->xe_timer);
984263bc 280
7261a835 281 /* Add supported media types */
984263bc
MD
282 if (scp->mohawk) {
283 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
7261a835
SZ
284 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
285 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
984263bc 286 }
7261a835
SZ
287 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
288 if (scp->ce2)
984263bc 289 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
984263bc
MD
290 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
291
292 /* Default is to autoselect best supported media type */
293 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
294
7261a835
SZ
295 /* Get the hardware into a known state */
296 xe_reset(scp);
297
298 /* Get hardware version numbers */
299 XE_SELECT_PAGE(4);
300 scp->version = XE_INB(XE_BOV);
301 if (scp->mohawk)
302 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
303 else
304 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
305
984263bc 306 /* Print some useful information */
7261a835 307 device_printf(dev, "%s %s, version 0x%02x/0x%02x%s%s\n",
984263bc
MD
308 scp->vendor,
309 scp->card_type,
310 scp->version,
7261a835 311 scp->srev,
984263bc
MD
312 scp->mohawk ? ", 100Mbps capable" : "",
313 scp->modem ? ", with modem" : "");
7261a835 314
984263bc
MD
315 if (scp->mohawk) {
316 XE_SELECT_PAGE(0x10);
7261a835
SZ
317 DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
318 XE_INW(XE_DINGOID),
319 XE_INW(XE_RevID),
320 XE_INW(XE_VendorID)));
984263bc
MD
321 }
322 if (scp->ce2) {
323 XE_SELECT_PAGE(0x45);
7261a835 324 DEVPRINTF(1, (dev, "CE2 version = 0x%#02x\n", XE_INB(XE_REV)));
984263bc
MD
325 }
326
984263bc 327 /* Attach the interface */
78195a76 328 ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr, NULL);
984263bc 329
95893fe4 330 err = bus_setup_intr(dev, scp->irq_res, INTR_MPSAFE,
78195a76 331 xe_intr, scp, &scp->intrhand,
9db4b353 332 scp->ifp->if_serializer);
7cdc3d6e 333 if (err) {
7261a835 334 device_printf(dev, "Setup intr failed\n");
9db4b353 335 ether_ifdetach(scp->ifp);
7cdc3d6e
JS
336 xe_deactivate(dev);
337 return err;
338 }
339
28e81a28 340 scp->ifp->if_cpuid = rman_get_cpuid(scp->irq_res);
9db4b353
SZ
341 KKASSERT(scp->ifp->if_cpuid >= 0 && scp->ifp->if_cpuid < ncpus);
342
984263bc
MD
343 /* Done */
344 return 0;
345}
346
347
348/*
7261a835
SZ
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).
984263bc
MD
352 */
353static void
354xe_init(void *xscp) {
355 struct xe_softc *scp = xscp;
7261a835 356 u_int i;
984263bc 357
7261a835
SZ
358 if (scp->autoneg_status != XE_AUTONEG_NONE) return;
359
360 IFPRINTF(2, (scp->ifp, "init\n"));
361
362 crit_enter();
984263bc 363
984263bc
MD
364 /* Reset transmitter flags */
365 scp->tx_queued = 0;
366 scp->tx_tpr = 0;
7261a835
SZ
367 scp->tx_timeouts = 0;
368 scp->tx_thres = 64;
369 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
984263bc
MD
370 scp->ifp->if_timer = 0;
371
7261a835
SZ
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);
984263bc 398
7261a835 399 /* Disable 'source insertion' (not sure what that means) */
984263bc 400 XE_SELECT_PAGE(0x42);
7261a835 401 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
984263bc 402
7261a835 403 /* Set 8K/24K Tx/Rx buffer split */
984263bc
MD
404 if (scp->srev != 1) {
405 XE_SELECT_PAGE(2);
406 XE_OUTW(XE_RBS, 0x2000);
407 }
408
7261a835
SZ
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
984263bc 422 /* Set up multicast addresses */
7261a835 423 xe_set_multicast(scp);
984263bc 424
7261a835 425 /* Fix the receive data offset -- reset can leave it off-by-one */
984263bc
MD
426 XE_SELECT_PAGE(0);
427 XE_OUTW(XE_DO, 0x2000);
428
7261a835
SZ
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);
984263bc 453
7261a835 454 /* Start media selection */
78195a76 455 xe_setmedia_serialized(scp);
984263bc 456
7261a835
SZ
457 /* Enable output */
458 scp->ifp->if_flags |= IFF_RUNNING;
9ed293e0 459 ifq_clr_oactive(&scp->ifp->if_snd);
984263bc 460
9228feed 461 crit_exit();
984263bc
MD
462}
463
464
465/*
7261a835 466 * Start output on interface. Should be called at splimp() priority. Check
9ed293e0
SZ
467 * that the output is idle (ie, OACTIVE is not set) before calling this
468 * function. If media selection is in progress we set OACTIVE ourselves
7261a835 469 * and return immediately.
984263bc
MD
470 */
471static void
472xe_start(struct ifnet *ifp) {
473 struct xe_softc *scp = ifp->if_softc;
474 struct mbuf *mbp;
475
7261a835 476 if (scp->autoneg_status != XE_AUTONEG_NONE) {
9ed293e0 477 ifq_set_oactive(&ifp->if_snd);
7261a835
SZ
478 return;
479 }
480
481 IFPRINTF(3, (ifp, "start\n"));
482
984263bc
MD
483 /*
484 * Loop while there are packets to be sent, and space to send them.
485 */
486 while (1) {
9db4b353
SZ
487 /* Suck a packet off the send queue */
488 mbp = ifq_dequeue(&ifp->if_snd, NULL);
984263bc
MD
489 if (mbp == NULL) {
490 /*
9db4b353
SZ
491 * We are using the !OACTIVE flag to indicate to the outside
492 * world that we can accept an additional packet rather than
493 * that the transmitter is _actually_ active. Indeed, the
494 * transmitter may be active, but if we haven't filled all
495 * the buffers with data then we still want to accept more.
984263bc 496 */
9ed293e0 497 ifq_clr_oactive(&ifp->if_snd);
984263bc
MD
498 return;
499 }
500
501 if (xe_pio_write_packet(scp, mbp) != 0) {
9ed293e0 502 ifq_set_oactive(&ifp->if_snd);
9db4b353 503 ifq_prepend(&ifp->if_snd, mbp);
984263bc
MD
504 return;
505 }
506
7600679e 507 BPF_MTAP(ifp, mbp);
984263bc 508
9db4b353 509 ifp->if_timer = 5; /* In case we don't hear from the card again */
984263bc
MD
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
bd4539cc 521xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr) {
984263bc 522 struct xe_softc *scp;
9228feed 523 int error;
984263bc
MD
524
525 scp = ifp->if_softc;
526 error = 0;
527
9228feed 528 crit_enter();
984263bc
MD
529
530 switch (command) {
531
7261a835
SZ
532 case SIOCSIFFLAGS:
533 IFPRINTF(2, (ifp, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
984263bc
MD
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)) {
7261a835 540 xe_reset(scp);
984263bc
MD
541 xe_init(scp);
542 }
543 }
544 else {
545 if (ifp->if_flags & IFF_RUNNING)
546 xe_stop(scp);
547 }
7261a835 548 /* FALL THROUGH (handle changes to PROMISC/ALLMULTI flags) */
984263bc 549
7261a835
SZ
550 case SIOCADDMULTI:
551 case SIOCDELMULTI:
552 IFPRINTF(2, (ifp, "ioctl: SIOC{ADD,DEL}MULTI\n"));
984263bc 553 /*
7261a835
SZ
554 * Multicast list has (maybe) changed; set the hardware filters
555 * accordingly.
984263bc 556 */
7261a835 557 xe_set_multicast(scp);
984263bc
MD
558 error = 0;
559 break;
560
7261a835
SZ
561 case SIOCSIFMEDIA:
562 case SIOCGIFMEDIA:
563 IFPRINTF(3, (ifp, "ioctl: bounce to ifmedia_ioctl\n"));
984263bc
MD
564 /*
565 * Someone wants to get/set media options.
566 */
567 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
568 break;
569
7261a835
SZ
570 default:
571 IFPRINTF(3, (ifp, "ioctl: bounce to ether_ioctl\n"));
4cde4dd5
JS
572 error = ether_ioctl(ifp, command, data);
573 break;
984263bc
MD
574 }
575
9228feed 576 crit_exit();
984263bc
MD
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;
7261a835 605 u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
984263bc
MD
606
607 ifp = &scp->arpcom.ac_if;
984263bc 608
7261a835
SZ
609 /* Disable interrupts */
610 if (scp->mohawk)
611 XE_OUTB(XE_CR, 0);
984263bc 612
7261a835
SZ
613 /* Cache current register page */
614 psr = XE_INB(XE_PR);
984263bc 615
7261a835
SZ
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 }
984263bc 623
7261a835 624 /* Read other status registers */
984263bc 625 XE_SELECT_PAGE(0x40);
7261a835
SZ
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;
984263bc
MD
631 XE_OUTB(XE_TXST0, 0);
632 XE_OUTB(XE_TXST1, 0);
633 XE_SELECT_PAGE(0);
634
7261a835
SZ
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));
984263bc 638
7261a835 639 /* Handle transmitted packet(s) */
984263bc 640 if (isr & XE_ISR_TX_PACKET) {
7261a835
SZ
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;
984263bc 646
7261a835
SZ
647 /* Update statistics if we actually sent anything */
648 if (sent > 0) {
649 scp->tx_tpr = tpr;
984263bc
MD
650 scp->tx_queued -= sent;
651 ifp->if_opackets += sent;
7261a835 652 ifp->if_collisions += coll;
984263bc
MD
653
654 /*
7261a835
SZ
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.
984263bc 661 */
7261a835
SZ
662 switch (coll) {
663 case 0:
984263bc 664 break;
7261a835 665 case 1:
984263bc
MD
666 scp->mibdata.dot3StatsSingleCollisionFrames++;
667 scp->mibdata.dot3StatsCollFrequencies[0]++;
668 break;
7261a835
SZ
669 default:
670 scp->mibdata.dot3StatsMultipleCollisionFrames++;
671 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
984263bc 672 }
984263bc
MD
673 }
674 ifp->if_timer = 0;
9ed293e0 675 ifq_clr_oactive(&ifp->if_snd);
984263bc 676 }
984263bc 677
7261a835
SZ
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 }
984263bc 738
7261a835 739 /* Handle received packet(s) */
984263bc 740 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
7261a835
SZ
741 rsr = XE_INB(XE_RSR);
742 IFPRINTF(3, (ifp, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
984263bc 743
7261a835
SZ
744 /* Make sure packet is a good one */
745 if (rsr & XE_RSR_RX_OK) {
984263bc
MD
746 struct ether_header *ehp;
747 struct mbuf *mbp;
748 u_int16_t len;
749
7261a835 750 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
984263bc 751
7261a835
SZ
752 IFPRINTF(3, (ifp, "intr: receive length = %d\n", len));
753
754 if (len == 0) {
755 ifp->if_iqdrops++;
984263bc 756 continue;
7261a835 757 }
984263bc 758
984263bc 759 /*
7261a835
SZ
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?
984263bc 766 */
7261a835
SZ
767 MGETHDR(mbp, MB_DONTWAIT, MT_DATA);
768 if (mbp == NULL) {
984263bc 769 ifp->if_iqdrops++;
984263bc
MD
770 continue;
771 }
984263bc 772
7261a835
SZ
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;
984263bc
MD
779 }
780 }
781
7261a835
SZ
782 mbp->m_data += 2;
783 ehp = mtod(mbp, struct ether_header *);
984263bc 784
7261a835
SZ
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--;
984263bc
MD
816 }
817 }
984263bc
MD
818 }
819 else
820 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
7261a835 821 (u_int16_t *) ehp, (len + 1) >> 1);
984263bc 822 }
7261a835
SZ
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;
78195a76 830 ifp->if_input(ifp, mbp);
7261a835 831 ifp->if_ipackets++;
984263bc 832 }
7261a835
SZ
833 else if (rsr & XE_RSR_ALIGN_ERROR) {
834 /* Packet alignment error -- drop packet */
835 if_printf(ifp, "alignment error\n");
984263bc
MD
836 scp->mibdata.dot3StatsAlignmentErrors++;
837 ifp->if_ierrors++;
838 }
7261a835
SZ
839
840 /* Skip to next packet, if there is one */
841 XE_OUTW(XE_DO, 0x8000);
984263bc 842 }
7261a835
SZ
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"));
984263bc 847 ifp->if_ierrors++;
7261a835 848 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
984263bc
MD
849 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
850 }
851 }
852
7261a835
SZ
853 /* Restore saved page */
854 XE_SELECT_PAGE(psr);
984263bc 855
7261a835
SZ
856 /* Re-enable interrupts */
857 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
984263bc
MD
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
2682bd2f 871 if_printf(ifp, "watchdog timeout; resetting card\n");
984263bc
MD
872 scp->tx_timeouts++;
873 ifp->if_oerrors += scp->tx_queued;
874 xe_stop(scp);
7261a835 875 xe_reset(scp);
984263bc
MD
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
7261a835 887 IFPRINTF(2, (ifp, "media_change\n"));
984263bc
MD
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
78195a76 899 xe_setmedia_serialized(scp);
984263bc
MD
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
7261a835 911 IFPRINTF(3, (ifp, "media_status\n"));
984263bc 912
7261a835
SZ
913 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
914 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
984263bc 915 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
984263bc
MD
916}
917
78195a76
MD
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}
984263bc
MD
928
929/*
930 * Select active media.
931 */
78195a76
MD
932static
933void
934xe_setmedia_serialized(void *xscp)
935{
984263bc
MD
936 struct xe_softc *scp = xscp;
937 u_int16_t bmcr, bmsr, anar, lpar;
938
7261a835 939 IFPRINTF(2, (scp->ifp, "setmedia\n"));
984263bc
MD
940
941 /* Cancel any pending timeout */
74a0ee08 942 callout_stop(&scp->xe_timer);
984263bc
MD
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
7261a835
SZ
983 case XE_AUTONEG_NONE:
984 IFPRINTF(2, (scp->ifp, "Waiting for idle transmitter\n"));
9ed293e0 985 ifq_set_oactive(&scp->arpcom.ac_if.if_snd);
984263bc 986 scp->autoneg_status = XE_AUTONEG_WAITING;
7261a835 987 /* FALL THROUGH */
984263bc 988
7261a835
SZ
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 }
984263bc 994 if (scp->phy_ok) {
7261a835 995 IFPRINTF(2, (scp->ifp, "Starting autonegotiation\n"));
984263bc
MD
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;
74a0ee08 1006 callout_reset(&scp->xe_timer, hz * 7 / 2, xe_setmedia, scp);
984263bc
MD
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)) {
7261a835 1018 IFPRINTF(2, (scp->ifp, "Autonegotiation complete!\n"));
984263bc
MD
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 {
7261a835 1055 IFPRINTF(2, (scp->ifp, "Autonegotiation failed; trying 100baseTX\n"));
984263bc 1056 XE_MII_DUMP(scp);
984263bc
MD
1057 if (scp->phy_ok) {
1058 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1059 scp->autoneg_status = XE_AUTONEG_100TX;
74a0ee08 1060 callout_reset(&scp->xe_timer, hz * 3, xe_setmedia, scp);
984263bc
MD
1061 return;
1062 }
1063 else {
1064 scp->autoneg_status = XE_AUTONEG_FAIL;
1065 }
1066 }
1067 break;
1068
1069 case XE_AUTONEG_100TX:
7b9f668c 1070 xe_phy_readreg(scp, PHY_BMSR);
984263bc
MD
1071 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1072 if (bmsr & PHY_BMSR_LINKSTAT) {
7261a835 1073 IFPRINTF(2, (scp->ifp, "Got 100baseTX link!\n"));
984263bc
MD
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 {
7261a835 1081 IFPRINTF(2, (scp->ifp, "Autonegotiation failed; disabling PHY\n"));
984263bc
MD
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) {
7261a835 1099 IFPRINTF(2, (scp->ifp, "Selecting 10baseX\n"));
984263bc
MD
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 */
984263bc 1125 if (scp->phy_ok) {
7261a835 1126 IFPRINTF(2, (scp->ifp, "Selecting 100baseTX\n"));
984263bc
MD
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 */
7261a835 1138 IFPRINTF(2, (scp->ifp, "Selecting 10baseT\n"));
984263bc
MD
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:
7261a835 1150 IFPRINTF(2, (scp->ifp, "Selecting 10base2\n"));
984263bc
MD
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 */
7261a835 1162 IFPRINTF(2, (scp->ifp, "Setting LEDs\n"));
984263bc
MD
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? */
7261a835 1178 xe_enable_intr(scp);
9ed293e0 1179 ifq_clr_oactive(&scp->ifp->if_snd);
9db4b353 1180 if_devstart(scp->ifp);
984263bc
MD
1181}
1182
1183
1184/*
1185 * Hard reset (power cycle) the card.
1186 */
1187static void
7261a835
SZ
1188xe_reset(struct xe_softc *scp) {
1189 IFPRINTF(2, (scp->ifp, "hard_reset\n"));
984263bc 1190
9228feed 1191 crit_enter();
984263bc 1192
7261a835 1193 /* Power down */
984263bc 1194 XE_SELECT_PAGE(4);
7261a835 1195 XE_OUTB(XE_GPR1, 0);
984263bc
MD
1196 DELAY(40000);
1197
7261a835 1198 /* Power up again */
984263bc 1199 if (scp->mohawk)
7261a835 1200 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
984263bc 1201 else
7261a835 1202 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
984263bc 1203
984263bc 1204 DELAY(40000);
984263bc
MD
1205 XE_SELECT_PAGE(0);
1206
9228feed 1207 crit_exit();
984263bc
MD
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) {
7261a835 1218 IFPRINTF(2, (scp->ifp, "stop\n"));
984263bc 1219
9228feed 1220 crit_enter();
984263bc
MD
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);
7261a835
SZ
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 }
984263bc
MD
1242
1243 /*
1244 * ~IFF_RUNNING == interface down.
1245 */
1246 scp->ifp->if_flags &= ~IFF_RUNNING;
9ed293e0 1247 ifq_clr_oactive(&scp->ifp->if_snd);
984263bc
MD
1248 scp->ifp->if_timer = 0;
1249
9228feed 1250 crit_exit();
984263bc
MD
1251}
1252
1253
1254/*
7261a835 1255 * Enable interrupts from the card.
984263bc
MD
1256 */
1257static void
1258xe_enable_intr(struct xe_softc *scp) {
7261a835 1259 IFPRINTF(2, (scp->ifp, "enable_intr\n"));
984263bc
MD
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/*
7261a835 1272 * Disable interrupts from the card.
984263bc
MD
1273 */
1274static void
1275xe_disable_intr(struct xe_softc *scp) {
7261a835 1276 IFPRINTF(2, (scp->ifp, "disable_intr\n"));
984263bc
MD
1277
1278 XE_SELECT_PAGE(0);
1279 XE_OUTB(XE_CR, 0); /* Disable interrupts */
7261a835 1280 if (scp->modem && !scp->dingo) { /* More magic */
984263bc
MD
1281 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1282 }
984263bc
MD
1283}
1284
1285
1286/*
7261a835 1287 * Set up multicast filter and promiscuous modes.
984263bc
MD
1288 */
1289static void
7261a835 1290xe_set_multicast(struct xe_softc *scp) {
984263bc 1291 struct ifnet *ifp;
c1126983 1292 struct ifmultiaddr *ifma;
7261a835 1293 u_int count, i;
984263bc
MD
1294
1295 ifp = &scp->arpcom.ac_if;
984263bc 1296
7261a835
SZ
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 */
c1126983 1318 count = 0;
441d34b2 1319 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
7261a835
SZ
1320 if (ifma->ifma_addr->sa_family != AF_LINK)
1321 continue;
1322
c1126983 1323 count++;
984263bc 1324
7261a835
SZ
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;
984263bc 1335 }
7261a835
SZ
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
984263bc 1343 XE_SELECT_PAGE(0x42);
7261a835
SZ
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 }
984263bc 1349 }
7261a835
SZ
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 */
984263bc 1355 XE_SELECT_PAGE(0x42);
7261a835
SZ
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 }
984263bc
MD
1389 }
1390 XE_SELECT_PAGE(0);
1391}
1392
1393
1394/*
7261a835
SZ
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).
984263bc
MD
1398 */
1399static void
7261a835
SZ
1400xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
1401 uint8_t page, reg;
1402 u_int i;
984263bc 1403
7261a835
SZ
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);
984263bc 1412
7261a835
SZ
1413 IFPRINTF(3, (scp->ifp, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
1414 idx+1, page, reg));
984263bc 1415
7261a835
SZ
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]));
984263bc 1427 }
984263bc 1428#endif
7261a835
SZ
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}
984263bc 1437
984263bc 1438
7261a835
SZ
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;
984263bc 1456 else
7261a835
SZ
1457 crc31 = 0;
1458 bit = crc31 ^ (byte & 0x01);
1459 crc <<= 1;
1460 byte >>= 1;
1461 if (bit)
1462 crc = (crc ^ XE_CRC_POLY)|1;
984263bc 1463 }
984263bc
MD
1464 }
1465
7261a835
SZ
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);
984263bc
MD
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) {
7261a835
SZ
1495 u_int len, pad;
1496 u_char wantbyte;
984263bc 1497 u_int8_t *data;
7261a835 1498 u_int8_t savebyte[2];
984263bc
MD
1499
1500 /* Get total packet length */
7261a835
SZ
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));
984263bc
MD
1509
1510 /* Packets < minimum length may need to be padded out */
1511 pad = 0;
7261a835
SZ
1512 if (len < scp->tx_min) {
1513 pad = scp->tx_min - len;
1514 len = scp->tx_min;
984263bc
MD
1515 }
1516
1517 /* Check transmit buffer space */
1518 XE_SELECT_PAGE(0);
7261a835
SZ
1519 XE_OUTW(XE_TRS, len+2); /* Only effective on rev. 1 CE2 cards */
1520 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
984263bc
MD
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 }
984263bc
MD
1554
1555 /*
7261a835
SZ
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.
984263bc
MD
1566 */
1567 if (scp->mohawk)
7261a835
SZ
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;
984263bc
MD
1573 while (pad > 0) {
1574 XE_OUTW(XE_EDP, 0xdead);
1575 pad--;
1576 }
984263bc 1577 }
984263bc 1578
7261a835 1579 return 0;
984263bc
MD
1580}
1581
984263bc
MD
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) {
f96d6c88 1605 int i;
984263bc
MD
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) {
7261a835 1628 IFPRINTF(2, (scp->ifp, "no PHY found, %0x\n", status));
984263bc
MD
1629 return 0;
1630 }
1631 else {
7261a835 1632 IFPRINTF(2, (scp->ifp, "PHY OK!\n"));
984263bc
MD
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) {
9228feed 1673 int i, ack;
984263bc 1674
9228feed 1675 crit_enter();
984263bc
MD
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
9228feed 1752 crit_exit();
984263bc
MD
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) {
984263bc 1765
9228feed 1766 crit_enter();
984263bc
MD
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
9228feed 1802 crit_exit();
984263bc
MD
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
984263bc
MD
1843/*
1844 * A bit of debugging code.
1845 */
1846static void
1847xe_mii_dump(struct xe_softc *scp) {
9228feed 1848 int i;
984263bc 1849
9228feed 1850 crit_enter();
984263bc 1851
2682bd2f 1852 if_printf(scp->ifp, "MII registers: ");
984263bc 1853 for (i = 0; i < 2; i++) {
e3869ec7 1854 kprintf(" %d:%04x", i, xe_phy_readreg(scp, i));
984263bc
MD
1855 }
1856 for (i = 4; i < 7; i++) {
e3869ec7 1857 kprintf(" %d:%04x", i, xe_phy_readreg(scp, i));
984263bc 1858 }
e3869ec7 1859 kprintf("\n");
984263bc 1860
9228feed 1861 crit_exit();
984263bc
MD
1862}
1863
1864static void
1865xe_reg_dump(struct xe_softc *scp) {
9228feed 1866 int page, i;
984263bc 1867
9228feed 1868 crit_enter();
984263bc 1869
2682bd2f 1870 if_printf(scp->ifp, "Common registers: ");
984263bc 1871 for (i = 0; i < 8; i++) {
e3869ec7 1872 kprintf(" %2.2x", XE_INB(i));
984263bc 1873 }
e3869ec7 1874 kprintf("\n");
984263bc
MD
1875
1876 for (page = 0; page <= 8; page++) {
2682bd2f 1877 if_printf(scp->ifp, "Register page %2.2x: ", page);
984263bc
MD
1878 XE_SELECT_PAGE(page);
1879 for (i = 8; i < 16; i++) {
e3869ec7 1880 kprintf(" %2.2x", XE_INB(i));
984263bc 1881 }
e3869ec7 1882 kprintf("\n");
984263bc
MD
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;
2682bd2f 1891 if_printf(scp->ifp, "Register page %2.2x: ", page);
984263bc
MD
1892 XE_SELECT_PAGE(page);
1893 for (i = 8; i < 16; i++) {
e3869ec7 1894 kprintf(" %2.2x", XE_INB(i));
984263bc 1895 }
e3869ec7 1896 kprintf("\n");
984263bc
MD
1897 }
1898
9228feed 1899 crit_exit();
984263bc 1900}
984263bc
MD
1901
1902int
1903xe_activate(device_t dev)
1904{
1905 struct xe_softc *sc = device_get_softc(dev);
7261a835
SZ
1906 int start, i;
1907
1908 DEVPRINTF(2, (dev, "activate\n"));
984263bc 1909
7261a835 1910 if (!sc->modem) {
984263bc
MD
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);
7261a835 1914 } else if (sc->dingo) {
984263bc
MD
1915 /*
1916 * Find a 16 byte aligned ioport for the card.
1917 */
7261a835 1918 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
984263bc
MD
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);
7261a835
SZ
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",
984263bc 1974 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
7261a835 1975 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
984263bc
MD
1976 }
1977 if (!sc->port_res) {
984263bc 1978 device_printf(dev, "Cannot allocate ioport\n");
984263bc
MD
1979 return ENOMEM;
1980 }
1981
1982 sc->irq_rid = 0;
4e6d744d
JS
1983 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1984 RF_ACTIVE);
984263bc 1985 if (!sc->irq_res) {
984263bc 1986 device_printf(dev, "Cannot allocate irq\n");
984263bc
MD
1987 xe_deactivate(dev);
1988 return ENOMEM;
1989 }
984263bc
MD
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);
7cdc3d6e 2000
7261a835
SZ
2001 DEVPRINTF(2, (dev, "deactivate\n"));
2002 xe_disable_intr(sc);
2003
984263bc
MD
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;
7261a835
SZ
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;
984263bc
MD
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;
984263bc 2016}