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