Deleted Added
sdiff udiff text old ( 52632 ) new ( 55723 )
full compact
1/*-
2 * Copyright (c) 1998, 1999 Scott Mitchell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 10 unchanged lines hidden (view full) ---

19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: if_xe.c,v 1.20 1999/06/13 19:17:40 scott Exp $
27 * $FreeBSD: head/sys/dev/xe/if_xe.c 55723 2000-01-10 08:05:53Z imp $
28 */
29
30/*
31 * XXX TODO XXX
32 *
33 * I've pushed this fairly far, but there are some things that need to be
34 * done here. I'm documenting them here in case I get destracted. -- imp
35 *
36 * xe_memwrite -- maybe better handled pccard layer?
37 * xe_cem56fix -- need to figure out how to map the extra stuff.
38 * xe_activate -- need to write it, and add some stuff to it. Look at if_sn
39 * for guidance. resources/interrupts.
40 * xe_deactivate -- turn off resources/interrupts.
41 */
42
43/*
44 * Portions of this software were derived from Werner Koch's xirc2ps driver
45 * for Linux under the terms of the following license (from v1.30 of the
46 * xirc2ps driver):
47 *
48 * Copyright (c) 1997 by Werner Koch (dd9jn)
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions

--- 59 unchanged lines hidden (view full) ---

111 * Author email: <scott@uk.freebsd.org>
112 */
113
114
115#ifndef XE_DEBUG
116#define XE_DEBUG 1 /* Increase for more voluminous output! */
117#endif
118
119#include <sys/param.h>
120#include <sys/cdefs.h>
121#include <sys/errno.h>
122#include <sys/kernel.h>
123#include <sys/malloc.h>
124#include <sys/mbuf.h>
125#include <sys/select.h>
126#include <sys/socket.h>
127#include <sys/sockio.h>
128#include <sys/systm.h>
129#include <sys/uio.h>
130#include <sys/conf.h>
131
132#include <sys/module.h>
133#include <sys/bus.h>
134
135#include <machine/bus.h>
136#include <machine/resource.h>
137#include <sys/rman.h>
138
139#include <net/ethernet.h>
140#include <net/if.h>
141#include <net/if_arp.h>
142#include <net/if_dl.h>
143#include <net/if_media.h>
144#include <net/if_mib.h>
145#include <net/bpf.h>
146
147#include <dev/xe/if_xereg.h>
148#include <dev/xe/if_xevar.h>
149
150#include <machine/clock.h>
151
152/*
153 * MII command structure
154 */
155struct xe_mii_frame {
156 u_int8_t mii_stdelim;
157 u_int8_t mii_opcode;
158 u_int8_t mii_phyaddr;
159 u_int8_t mii_regaddr;
160 u_int8_t mii_turnaround;
161 u_int16_t mii_data;
162};
163
164/*
165 * Media autonegotiation progress constants
166 */
167#define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
168#define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
169#define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
170#define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
171#define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
172
173
174/*
175 * Prototypes start here
176 */
177static int xe_probe (device_t dev);
178static int xe_attach (device_t dev);
179static int xe_detach (device_t dev);
180static int xe_activate (device_t dev);
181static void xe_deactivate (device_t dev);
182static void xe_init (void *xscp);
183static void xe_start (struct ifnet *ifp);
184static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data);
185static void xe_watchdog (struct ifnet *ifp);
186static int xe_media_change (struct ifnet *ifp);
187static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
188static timeout_t xe_setmedia;
189static void xe_hard_reset (struct xe_softc *scp);
190static void xe_soft_reset (struct xe_softc *scp);
191static void xe_stop (struct xe_softc *scp);
192static void xe_enable_intr (struct xe_softc *scp);
193static void xe_disable_intr (struct xe_softc *scp);
194static void xe_setmulti (struct xe_softc *scp);
195static void xe_setaddrs (struct xe_softc *scp);
196static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
197static u_int32_t xe_compute_crc (u_int8_t *data, int len);
198static int xe_compute_hashbit (u_int32_t crc);
199
200/*
201 * MII functions
202 */
203static void xe_mii_sync (struct xe_softc *scp);
204static int xe_mii_init (struct xe_softc *scp);

--- 11 unchanged lines hidden (view full) ---

216#define XE_MII_DUMP(scp) xe_mii_dump((scp))
217static void xe_reg_dump (struct xe_softc *scp);
218static void xe_mii_dump (struct xe_softc *scp);
219#else
220#define XE_REG_DUMP(scp)
221#define XE_MII_DUMP(scp)
222#endif
223
224/*
225 * Two routines to read from/write to the attribute memory
226 * the write portion is used only for fixing up the RealPort cards,
227 * the reader portion was needed for debugging info, and duplicated some
228 * code in xe_card_init(), so it appears here instead with suitable
229 * modifications to xe_card_init()
230 * -aDe Lovett
231 */
232static int
233xe_memwrite(device_t dev, off_t offset, u_char byte)
234{
235 /* XXX */
236 return (-1);
237}
238
239/*
240 * Hacking for RealPort cards
241 */
242static int
243xe_cem56fix(struct xe_softc *scp)
244{
245#if XXX /* Need to revisit */
246 int ioport, fail;
247
248 /* allocate a new I/O slot for the ethernet */
249 /* XXX: ctrl->mapio() always appears to return 0 (success), so
250 * this may cause problems if another device is listening
251 * on 0x300 already. In this case, you should choose a
252 * known free I/O port address in the kernel config line
253 * for the driver. It will be picked up here and used
254 * instead of the autodetected value.
255 */
256 slt->io[1].window = 1;
257 slt->io[1].flags = IODF_WS|IODF_16BIT|IODF_ZEROWS|IODF_ACTIVE;
258 slt->io[1].size = 0x10;
259
260#ifdef XE_IOBASE
261
262 device_printf(scp->dev, "user requested ioport 0x%x\n", XE_IOBASE );
263 ioport = XE_IOBASE;
264 slt->io[1].start = ioport;
265 fail = ctrl->mapio(slt, 1);
266
267#else
268
269 for (ioport = 0x300; ioport < 0x400; ioport += 0x10) {
270 slt->io[1].start = ioport;
271 if ((fail = ctrl->mapio( slt, 1 )) == 0)
272 break;
273 }
274
275#endif
276
277 /* did we find one? */
278 if (fail) {
279 device_printf(scp->dev, "xe_cem56fix: no free address space\n");
280 return -1;
281 }
282
283
284 /* munge the id_iobase entry for use by the rest of the driver */
285#if XE_DEBUG > 1
286 device_printf(scp->dev, "using 0x%x for RealPort ethernet\n", ioport);
287#endif
288#if 0
289 scp->dev->id_iobase = ioport;
290 scp->dev->id_alive = 0x10;
291#endif
292
293 /* magic to set up the ethernet */
294 xe_memwrite( scp->dev, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL|
295 DINGO_ECOR_INT_ENABLE|DINGO_ECOR_IOB_ENABLE|
296 DINGO_ECOR_ETH_ENABLE );
297 xe_memwrite( scp->dev, DINGO_EBAR0, ioport & 0xff );
298 xe_memwrite( scp->dev, DINGO_EBAR1, (ioport >> 8) & 0xff );
299
300 xe_memwrite( scp->dev, DINGO_DCOR0, DINGO_DCOR0_SF_INT );
301 xe_memwrite( scp->dev, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL|DINGO_DCOR1_EEDIO );
302 xe_memwrite( scp->dev, DINGO_DCOR2, 0x00 );
303 xe_memwrite( scp->dev, DINGO_DCOR3, 0x00 );
304 xe_memwrite( scp->dev, DINGO_DCOR4, 0x00 );
305
306 /* success! */
307 return 0;
308#else
309 return -1;
310#endif /* XXX */
311}
312
313/*
314 * PCMCIA probe routine.
315 * Probe and identify the device. Called by the slot manager when the card is
316 * inserted or the machine wakes up from suspend mode. Assmes that the slot
317 * structure has been initialised already.
318 */
319static int
320xe_probe(device_t dev)
321{
322 struct xe_softc *scp = (struct xe_softc *) device_get_softc(dev);
323 u_char *buf;
324 u_char ver_str[CISTPL_BUFSIZE>>1];
325 off_t offs;
326 int success, rc, i;
327 int rid;
328 struct resource *r;
329
330 success = 0;
331
332#ifdef XE_DEBUG
333 device_printf(dev, "xe: Probing\n");
334#endif
335
336 /* Map in the CIS */
337 /* XXX This CANNOT work as it needs RF_PCCARD_ATTR support */
338 r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 4 << 10, RF_ACTIVE);
339 if (!r) {
340#ifdef XE_DEBUG
341 device_printf(dev, "Can't map in cis\n");
342#endif
343 return ENOMEM;
344 }
345 buf = (u_char *) rman_get_start(r);
346
347 /* Grep through CIS looking for relevant tuples */
348 offs = 0;
349 do {
350 u_int16_t vendor;
351 u_int8_t rev, media, prod;
352
353 switch (CISTPL_TYPE(buf)) {
354
355 case 0x15: /* Grab version string (needed to ID some weird CE2's) */
356#if XE_DEBUG > 1
357 device_printf(dev, "Got version string (0x15)\n");
358#endif
359 for (i = 0; i < CISTPL_LEN(buf); ver_str[i] = CISTPL_DATA(buf, i++));
360 ver_str[i] = '\0';
361 ver_str[(CISTPL_BUFSIZE>>1) - 1] = CISTPL_LEN(buf);
362 success++;
363 break;
364
365 case 0x20: /* Figure out what type of card we have */
366#if XE_DEBUG > 1
367 device_printf(dev, "Got card ID (0x20)\n");
368#endif
369 vendor = CISTPL_DATA(buf, 0) + (CISTPL_DATA(buf, 1) << 8);
370 rev = CISTPL_DATA(buf, 2);
371 media = CISTPL_DATA(buf, 3);
372 prod = CISTPL_DATA(buf, 4);
373
374 switch (vendor) { /* Get vendor ID */
375 case 0x0105:
376 scp->vendor = "Xircom"; break;
377 case 0x0138:
378 case 0x0183:
379 scp->vendor = "Compaq"; break;
380 case 0x0089:
381 scp->vendor = "Intel"; break;
382 default:
383 scp->vendor = "Unknown";
384 }
385
386 if (!((prod & 0x40) && (media & 0x01))) {
387#if XE_DEBUG > 1
388 device_printf(dev, "Not a PCMCIA Ethernet card!\n");
389#endif
390 rc = ENODEV; /* Not a PCMCIA Ethernet device */
391 } else {
392 if (media & 0x10) { /* Ethernet/modem cards */
393#if XE_DEBUG > 1
394 device_printf(dev, "Card is Ethernet/modem combo\n");
395#endif
396 scp->modem = 1;
397 switch (prod & 0x0f) {
398 case 1:
399 scp->card_type = "CEM"; break;
400 case 2:
401 scp->ce2 = 1;
402 scp->card_type = "CEM2"; break;
403 case 3:
404 scp->ce2 = 1;
405 scp->card_type = "CEM3"; break;
406 case 4:
407 scp->ce2 = 1;
408 scp->card_type = "CEM33"; break;
409 case 5:
410 scp->mohawk = 1;
411 scp->card_type = "CEM56M"; break;
412 case 6:
413 case 7: /* Some kind of RealPort card */
414 scp->mohawk = 1;
415 scp->dingo = 1;
416 scp->card_type = "CEM56"; break;
417 default:
418 rc = ENODEV;
419 }
420 } else { /* Ethernet-only cards */
421#if XE_DEBUG > 1
422 device_printf(dev, "Card is Ethernet only\n");
423#endif
424 switch (prod & 0x0f) {
425 case 1:
426 scp->card_type = "CE"; break;
427 case 2:
428 scp->ce2 = 1;
429 scp->card_type = "CE2"; break;
430 case 3:
431 scp->mohawk = 1;
432 scp->card_type = "CE3"; break;
433 default:
434 rc = ENODEV;
435 }
436 }
437 }
438 success++;
439 break;
440
441 case 0x22: /* Get MAC address */
442 if ((CISTPL_LEN(buf) == 8) &&
443 (CISTPL_DATA(buf, 0) == 0x04) &&
444 (CISTPL_DATA(buf, 1) == ETHER_ADDR_LEN)) {
445#if XE_DEBUG > 1
446 device_printf(dev, "Got MAC address (0x22)\n");
447#endif
448 for (i = 0; i < ETHER_ADDR_LEN; i++)
449 scp->arpcom.ac_enaddr[i] = CISTPL_DATA(buf, i+2);
450 }
451 success++;
452 break;
453 default:
454 break;
455 }
456
457 if (CISTPL_TYPE(buf) == 0xff)
458 break;
459 /* Skip to next tuple */
460 buf += ((CISTPL_LEN(buf) + 2) << 1);
461
462 } while (1);
463
464 /* unmap the cis */
465 bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
466
467 /* Die now if something went wrong above */
468 if (success < 3)
469 return ENXIO;
470
471 /* Check for certain strange CE2's that look like CE's */
472 if (strcmp(scp->card_type, "CE") == 0) {
473 u_char *str = ver_str;
474#if XE_DEBUG > 1
475 device_printf(dev, "Checking for weird CE2 string\n");
476#endif
477 str += strlen(str) + 1; /* Skip forward to 3rd version string */
478 str += strlen(str) + 1;
479 str += strlen(str) + 1;
480 for (i = 0; i < strlen(str) - 2; i++) {
481 if (bcmp(&str[i], "CE2", 3) ==0) { /* Look for "CE2" string */
482 scp->card_type = "CE2";
483 }
484 }
485 }
486
487 /* Reject unsupported cards */
488 if (strcmp(scp->card_type, "CE") == 0 || strcmp(scp->card_type, "CEM") == 0) {
489 device_printf(dev, "Sorry, your %s card is not supported :(\n",
490 scp->card_type);
491 return ENODEV;
492 }
493
494 /* Success */
495 return 0;
496}
497
498/*
499 * The device entry is being removed, probably because someone ejected the
500 * card. The interface should have been brought down manually before calling
501 * this function; if not you may well lose packets. In any case, I shut down
502 * the card and the interface, and hope for the best.
503 */
504static int
505xe_detach(device_t dev) {
506 struct xe_softc *sc = device_get_softc(dev);
507
508 sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
509 if_detach(&sc->arpcom.ac_if);
510 xe_deactivate(dev);
511 return 0;
512}
513
514/*
515 * Attach a device.
516 */
517static int
518xe_attach (device_t dev) {
519 struct xe_softc *scp = device_get_softc(dev);
520
521#ifdef XE_DEBUG
522 device_printf(dev, "attach\n");
523#endif
524
525 xe_activate(dev);
526
527 /* Fill in some private data */
528 scp->ifp = &scp->arpcom.ac_if;
529 scp->ifm = &scp->ifmedia;
530 scp->autoneg_status = 0;
531
532 /* Hack RealPorts into submission */
533 if (scp->dingo && xe_cem56fix(scp) < 0) {
534 device_printf(dev, "Unable to fix your RealPort\n");
535 xe_deactivate(dev);
536 return ENODEV;
537 }
538
539 /* Hopefully safe to read this here */
540 XE_SELECT_PAGE(4);
541 scp->version = XE_INB(XE_BOV);
542
543 scp->dev = dev;
544 /* Initialise the ifnet structure */
545 if (!scp->ifp->if_name) {
546 scp->ifp->if_softc = scp;
547 scp->ifp->if_name = "xe";
548 scp->ifp->if_unit = device_get_unit(dev);
549 scp->ifp->if_timer = 0;
550 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
551 scp->ifp->if_linkmib = &scp->mibdata;
552 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
553 scp->ifp->if_output = ether_output;
554 scp->ifp->if_start = xe_start;
555 scp->ifp->if_ioctl = xe_ioctl;
556 scp->ifp->if_watchdog = xe_watchdog;

--- 19 unchanged lines hidden (view full) ---

576 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
577 }
578 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
579
580 /* Default is to autoselect best supported media type */
581 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
582
583 /* Print some useful information */
584 device_printf(dev, "%s %s, bonding version %#x%s%s\n",
585 scp->vendor,
586 scp->card_type,
587 scp->version,
588 scp->mohawk ? ", 100Mbps capable" : "",
589 scp->modem ? ", with modem" : "");
590 if (scp->mohawk) {
591 XE_SELECT_PAGE(0x10);
592 device_printf(dev, "DingoID = %#x, RevisionID = %#x, VendorID = %#x\n",
593 XE_INW(XE_DINGOID),
594 XE_INW(XE_RevID),
595 XE_INW(XE_VendorID));
596 }
597 if (scp->ce2) {
598 XE_SELECT_PAGE(0x45);
599 device_printf(dev, "CE2 version = %#x\n", XE_INB(XE_REV));
600 }
601
602 /* Print MAC address */
603 device_printf(dev, "Ethernet address %6D\n", scp->arpcom.ac_enaddr, ":");
604
605 /* Attach the interface */
606 if_attach(scp->ifp);
607 ether_ifattach(scp->ifp);
608
609 /* BPF is in the kernel, call the attach for it */
610#if XE_DEBUG > 1
611 device_printf(dev, "BPF listener attached\n");
612#endif
613 bpfattach(scp->ifp, DLT_EN10MB, sizeof(struct ether_header));
614
615 /* Done */
616 return 1;
617}
618
619
620/*
621 * Initialize device. Completes the reset procedure on the card and starts
622 * output. If there's an autonegotiation in progress we DON'T do anything;
623 * the media selection code will call us again when it's done.
624 */
625static void
626xe_init(void *xscp) {
627 struct xe_softc *scp = xscp;
628 int s;
629
630#ifdef XE_DEBUG
631 device_printf(scp->dev, "init\n");
632#endif
633
634 if (TAILQ_EMPTY(&scp->ifp->if_addrhead)) return;
635
636 /* Reset transmitter flags */
637 scp->tx_queued = 0;
638 scp->tx_tpr = 0;
639 scp->tx_collisions = 0;
640 scp->ifp->if_timer = 0;
641

--- 65 unchanged lines hidden (view full) ---

707 * 2) that the IFF_OACTIVE flag is checked before this code is called
708 * (i.e. that the output part of the interface is idle)
709 */
710static void
711xe_start(struct ifnet *ifp) {
712 struct xe_softc *scp = ifp->if_softc;
713 struct mbuf *mbp;
714
715 /*
716 * Loop while there are packets to be sent, and space to send them.
717 */
718 while (1) {
719 IF_DEQUEUE(&ifp->if_snd, mbp); /* Suck a packet off the send queue */
720
721 if (mbp == NULL) {
722 /*

--- 11 unchanged lines hidden (view full) ---

734 IF_PREPEND(&ifp->if_snd, mbp); /* Push the packet back onto the queue */
735 ifp->if_flags |= IFF_OACTIVE;
736 return;
737 }
738
739 /* Tap off here if there is a bpf listener */
740 if (ifp->if_bpf) {
741#if XE_DEBUG > 1
742 device_printf(scp->dev, "sending output packet to BPF\n");
743#endif
744 bpf_mtap(ifp, mbp);
745 }
746
747 ifp->if_timer = 5; /* In case we don't hear from the card again */
748 scp->tx_queued++;
749
750 m_freem(mbp);

--- 7 unchanged lines hidden (view full) ---

758static int
759xe_ioctl (register struct ifnet *ifp, u_long command, caddr_t data) {
760 struct xe_softc *scp;
761 int s, error;
762
763 scp = ifp->if_softc;
764 error = 0;
765
766 s = splimp();
767
768 switch (command) {
769
770 case SIOCSIFADDR:
771 case SIOCGIFADDR:
772 case SIOCSIFMTU:
773 error = ether_ioctl(ifp, command, data);

--- 41 unchanged lines hidden (view full) ---

815
816 (void)splx(s);
817
818 return error;
819}
820
821
822/*
823 * Card interrupt handler.
824 *
825 * This function is probably more complicated than it needs to be, as it
826 * attempts to deal with the case where multiple packets get sent between
827 * interrupts. This is especially annoying when working out the collision
828 * stats. Not sure whether this case ever really happens or not (maybe on a
829 * slow/heavily loaded machine?) so it's probably best to leave this like it
830 * is.
831 *
832 * Note that the crappy PIO used to get packets on and off the card means that
833 * you will spend a lot of time in this routine -- I can get my P150 to spend
834 * 90% of its time servicing interrupts if I really hammer the network. Could
835 * fix this, but then you'd start dropping/losing packets. The moral of this
836 * story? If you want good network performance _and_ some cycles left over to
837 * get your work done, don't buy a Xircom card. Or convince them to tell me
838 * how to do memory-mapped I/O :)
839 */
840static void
841xe_intr(void *xscp)
842{
843 struct xe_softc *scp = (struct xe_softc *) xscp;
844 struct ifnet *ifp;
845 int result;
846 u_int16_t rx_bytes, rxs, txs;
847 u_int8_t psr, isr, esr, rsr;
848
849 ifp = &scp->arpcom.ac_if;
850 rx_bytes = 0; /* Bytes received on this interrupt */
851 result = 0; /* Set true if the interrupt is for us */
852
853 if (scp->mohawk) {
854 XE_OUTB(XE_CR, 0); /* Disable interrupts */
855 }
856
857 psr = XE_INB(XE_PR); /* Stash the current register page */
858
859 /*
860 * Read ISR to see what caused this interrupt. Note that this clears the

--- 175 unchanged lines hidden (view full) ---

1036 ((char *)ehp)[i] = XE_INB(XE_EDP);
1037 if (rhs == 0x8000) {
1038 rhs = 0;
1039 i--;
1040 }
1041 }
1042 }
1043 else
1044 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
1045 (u_int16_t *) ehp, len >> 1);
1046 }
1047 else
1048 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
1049 (u_int16_t *) ehp, len >> 1);
1050
1051 /*
1052 * Check if there's a BPF listener on this interface. If so, hand
1053 * off the raw packet to bpf.
1054 */
1055 if (ifp->if_bpf) {
1056#if XE_DEBUG > 1
1057 device_printf(scp->dev, "passing input packet to BPF\n");
1058#endif
1059 bpf_mtap(ifp, mbp);
1060
1061 /*
1062 * Note that the interface cannot be in promiscuous mode if there
1063 * are no BPF listeners. And if we are in promiscuous mode, we
1064 * have to check if this packet is really ours.
1065 */

--- 35 unchanged lines hidden (view full) ---

1101 }
1102
1103 XE_SELECT_PAGE(psr); /* Restore saved page */
1104 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Re-enable interrupts */
1105
1106 /* Could force an int here, instead of dropping packets? */
1107 /* XE_OUTB(XE_CR, XE_CR_ENABLE_INTR|XE_CE_FORCE_INTR); */
1108
1109 return;
1110}
1111
1112
1113/*
1114 * Device timeout/watchdog routine. Called automatically if we queue a packet
1115 * for transmission but don't get an interrupt within a specified timeout
1116 * (usually 5 seconds). When this happens we assume the worst and reset the
1117 * card.
1118 */
1119static void
1120xe_watchdog(struct ifnet *ifp) {
1121 struct xe_softc *scp = ifp->if_softc;
1122
1123 device_printf(scp->dev, "watchdog timeout; resetting card\n");
1124 scp->tx_timeouts++;
1125 ifp->if_oerrors += scp->tx_queued;
1126 xe_stop(scp);
1127 xe_hard_reset(scp);
1128 xe_setmedia(scp);
1129 xe_init(scp);
1130}
1131

--- 44 unchanged lines hidden (view full) ---

1176/*
1177 * Select active media.
1178 */
1179static void xe_setmedia(void *xscp) {
1180 struct xe_softc *scp = xscp;
1181 u_int16_t bmcr, bmsr, anar, lpar;
1182
1183#ifdef XE_DEBUG
1184 device_printf(scp->dev, "setmedia\n");
1185#endif
1186
1187 /* Cancel any pending timeout */
1188 untimeout(xe_setmedia, scp, scp->chand);
1189 xe_disable_intr(scp);
1190
1191 /* Select media */
1192 scp->media = IFM_ETHER;

--- 30 unchanged lines hidden (view full) ---

1223 * Select 10baseT or 10base2, whichever is connected
1224 * ENDIF
1225 * ENDIF
1226 */
1227 switch (scp->autoneg_status) {
1228
1229 case XE_AUTONEG_NONE:
1230#if XE_DEBUG > 1
1231 device_printf(scp->dev, "Waiting for idle transmitter\n");
1232#endif
1233 scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
1234 scp->autoneg_status = XE_AUTONEG_WAITING;
1235 scp->chand = timeout(xe_setmedia, scp, hz * 2);
1236 return;
1237
1238 case XE_AUTONEG_WAITING:
1239 xe_soft_reset(scp);
1240 if (scp->phy_ok) {
1241#if XE_DEBUG > 1
1242 device_printf(scp->dev, "Starting autonegotiation\n");
1243#endif
1244 bmcr = xe_phy_readreg(scp, PHY_BMCR);
1245 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1246 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1247 anar = xe_phy_readreg(scp, PHY_ANAR);
1248 anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
1249 anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
1250 xe_phy_writereg(scp, PHY_ANAR, anar);

--- 8 unchanged lines hidden (view full) ---

1259 }
1260 break;
1261
1262 case XE_AUTONEG_STARTED:
1263 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1264 lpar = xe_phy_readreg(scp, PHY_LPAR);
1265 if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
1266#if XE_DEBUG > 1
1267 device_printf(scp->dev, "Autonegotiation complete!\n");
1268#endif
1269 /*
1270 * XXX - Shouldn't have to do this, but (on my hub at least) the
1271 * XXX - transmitter won't work after a successful autoneg. So we see
1272 * XXX - what the negotiation result was and force that mode. I'm
1273 * XXX - sure there is an easy fix for this.
1274 */
1275 if (lpar & PHY_LPAR_100BTXHALF) {

--- 22 unchanged lines hidden (view full) ---

1298 scp->autoneg_status = XE_AUTONEG_NONE;
1299 /* END HACK */
1300 /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/ /* Disable PHY? */
1301 /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1302 }
1303 }
1304 else {
1305#if XE_DEBUG > 1
1306 device_printf(scp->dev, "Autonegotiation failed; trying 100baseTX\n");
1307#endif
1308 XE_MII_DUMP(scp);
1309 xe_soft_reset(scp);
1310 if (scp->phy_ok) {
1311 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1312 scp->autoneg_status = XE_AUTONEG_100TX;
1313 scp->chand = timeout(xe_setmedia, scp, hz * 3);
1314 return;

--- 4 unchanged lines hidden (view full) ---

1319 }
1320 break;
1321
1322 case XE_AUTONEG_100TX:
1323 (void)xe_phy_readreg(scp, PHY_BMSR);
1324 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1325 if (bmsr & PHY_BMSR_LINKSTAT) {
1326#if XE_DEBUG > 1
1327 device_printf(scp->dev, "Got 100baseTX link!\n");
1328#endif
1329 XE_MII_DUMP(scp);
1330 XE_SELECT_PAGE(2);
1331 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1332 scp->media = IFM_ETHER|IFM_100_TX;
1333 scp->autoneg_status = XE_AUTONEG_NONE;
1334 }
1335 else {
1336#if XE_DEBUG > 1
1337 device_printf(scp->dev, "Autonegotiation failed; disabling PHY\n");
1338#endif
1339 XE_MII_DUMP(scp);
1340 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1341 XE_SELECT_PAGE(2);
1342 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY? */
1343 scp->autoneg_status = XE_AUTONEG_FAIL;
1344 }
1345 break;
1346 }
1347
1348 /*
1349 * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1350 * either autonegotiation failed, or never got started to begin with. In
1351 * either case, select a suitable 10Mbit media and hope it works. We
1352 * don't need to reset the card again, since it will have been done
1353 * already by the big switch above.
1354 */
1355 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1356#if XE_DEBUG > 1
1357 device_printf(scp->dev, "Selecting 10baseX\n");
1358#endif
1359 if (scp->mohawk) {
1360 XE_SELECT_PAGE(0x42);
1361 XE_OUTB(XE_SWC1, 0x80);
1362 scp->media = IFM_ETHER|IFM_10_T;
1363 scp->autoneg_status = XE_AUTONEG_NONE;
1364 }
1365 else {

--- 13 unchanged lines hidden (view full) ---

1379 * If a specific media has been requested, we just reset the card and
1380 * select it (one small exception -- if 100baseTX is requested by there is
1381 * no PHY, we fall back to 10baseT operation).
1382 */
1383 case IFM_100_TX: /* Force 100baseTX */
1384 xe_soft_reset(scp);
1385 if (scp->phy_ok) {
1386#if XE_DEBUG > 1
1387 device_printf(scp->dev, "Selecting 100baseTX\n");
1388#endif
1389 XE_SELECT_PAGE(0x42);
1390 XE_OUTB(XE_SWC1, 0);
1391 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1392 XE_SELECT_PAGE(2);
1393 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1394 scp->media |= IFM_100_TX;
1395 break;
1396 }
1397 /* FALLTHROUGH */
1398
1399 case IFM_10_T: /* Force 10baseT */
1400 xe_soft_reset(scp);
1401#if XE_DEBUG > 1
1402 device_printf(csp->dev, "Selecting 10baseT\n");
1403#endif
1404 if (scp->phy_ok) {
1405 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1406 XE_SELECT_PAGE(2);
1407 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY */
1408 }
1409 XE_SELECT_PAGE(0x42);
1410 XE_OUTB(XE_SWC1, 0x80);
1411 scp->media |= IFM_10_T;
1412 break;
1413
1414 case IFM_10_2:
1415 xe_soft_reset(scp);
1416#if XE_DEBUG > 1
1417 device_printf(scp->dev, "Selecting 10base2\n");
1418#endif
1419 XE_SELECT_PAGE(0x42);
1420 XE_OUTB(XE_SWC1, 0xc0);
1421 scp->media |= IFM_10_2;
1422 break;
1423 }
1424
1425
1426 /*
1427 * Finally, the LEDs are set to match whatever media was chosen and the
1428 * transmitter is unblocked.
1429 */
1430#if XE_DEBUG > 1
1431 device_printf(scp->dev, "Setting LEDs\n");
1432#endif
1433 XE_SELECT_PAGE(2);
1434 switch (IFM_SUBTYPE(scp->media)) {
1435 case IFM_100_TX:
1436 case IFM_10_T:
1437 XE_OUTB(XE_LED, 0x3b);
1438 if (scp->dingo)
1439 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */

--- 13 unchanged lines hidden (view full) ---

1453/*
1454 * Hard reset (power cycle) the card.
1455 */
1456static void
1457xe_hard_reset(struct xe_softc *scp) {
1458 int s;
1459
1460#ifdef XE_DEBUG
1461 device_printf(scp->dev, "hard_reset\n");
1462#endif
1463
1464 s = splimp();
1465
1466 /*
1467 * Power cycle the card.
1468 */
1469 XE_SELECT_PAGE(4);
1470 XE_OUTB(XE_GPR1, 0); /* Power off */
1471 DELAY(40000);

--- 16 unchanged lines hidden (view full) ---

1488 * leave us in a position where we can access the PHY and do media
1489 * selection. The function imposes a 0.5s delay while the hardware powers up.
1490 */
1491static void
1492xe_soft_reset(struct xe_softc *scp) {
1493 int s;
1494
1495#ifdef XE_DEBUG
1496 device_printf(scp->dev, "soft_reset\n");
1497#endif
1498
1499 s = splimp();
1500
1501 /*
1502 * Reset the card, (again).
1503 */
1504 XE_SELECT_PAGE(0);
1505 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
1506 DELAY(40000);

--- 19 unchanged lines hidden (view full) ---

1526 * Get silicon revision number.
1527 */
1528 XE_SELECT_PAGE(4);
1529 if (scp->mohawk)
1530 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
1531 else
1532 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
1533#ifdef XE_DEBUG
1534 device_printf(scp->dev, "silicon revision = %d\n", scp->srev);
1535#endif
1536
1537 /*
1538 * Shut off interrupts.
1539 */
1540 xe_disable_intr(scp);
1541
1542 /*

--- 14 unchanged lines hidden (view full) ---

1557 * assume means just shutting down the transceiver and Ethernet logic. This
1558 * requires a _hard_ reset to recover from, as we need to power up again.
1559 */
1560static void
1561xe_stop(struct xe_softc *scp) {
1562 int s;
1563
1564#ifdef XE_DEBUG
1565 device_printf(scp->dev, "stop\n");
1566#endif
1567
1568 s = splimp();
1569
1570 /*
1571 * Shut off interrupts.
1572 */
1573 xe_disable_intr(scp);
1574
1575 /*

--- 15 unchanged lines hidden (view full) ---

1591
1592
1593/*
1594 * Enable Ethernet interrupts from the card.
1595 */
1596static void
1597xe_enable_intr(struct xe_softc *scp) {
1598#ifdef XE_DEBUG
1599 device_printf(scp->dev, "enable_intr\n");
1600#endif
1601
1602 XE_SELECT_PAGE(1);
1603 XE_OUTB(XE_IMR0, 0xff); /* Unmask everything */
1604 XE_OUTB(XE_IMR1, 0x01); /* Unmask TX underrun detection */
1605 DELAY(1);
1606
1607 XE_SELECT_PAGE(0);

--- 7 unchanged lines hidden (view full) ---

1615
1616
1617/*
1618 * Disable all Ethernet interrupts from the card.
1619 */
1620static void
1621xe_disable_intr(struct xe_softc *scp) {
1622#ifdef XE_DEBUG
1623 device_printf(scp->dev, "disable_intr\n");
1624#endif
1625
1626 XE_SELECT_PAGE(0);
1627 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1628 if (scp->modem && !scp->dingo) { /* More magic (does this work?) */
1629 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1630 }
1631

--- 84 unchanged lines hidden (view full) ---

1716 addr = (u_int8_t *)(&scp->arpcom.ac_enaddr);
1717 }
1718
1719 for (i = 0; i < 6; i++, byte++) {
1720#if XE_DEBUG > 2
1721 if (i)
1722 printf(":%x", addr[i]);
1723 else
1724 device_printf(scp->dev, "individual addresses %d: %x", slot, addr[0]);
1725#endif
1726
1727 if (byte > 15) {
1728 page++;
1729 byte = 8;
1730 XE_SELECT_PAGE(page);
1731 }
1732

--- 54 unchanged lines hidden (view full) ---

1787 if (wantbyte) { /* Finish the last word */
1788 savebyte[1] = *data;
1789 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1790 data++;
1791 len--;
1792 wantbyte = 0;
1793 }
1794 if (len > 1) { /* Output contiguous words */
1795 bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1796 len >> 1);
1797 data += len & ~1;
1798 len &= 1;
1799 }
1800 if (len == 1) { /* Save last byte, if necessary */
1801 savebyte[0] = *data;
1802 wantbyte = 1;
1803 }
1804 }

--- 13 unchanged lines hidden (view full) ---

1818 while (pad > 0) {
1819 XE_OUTW(XE_EDP, 0xdead);
1820 pad--;
1821 }
1822
1823 return 0;
1824}
1825
1826/*
1827 * Compute the 32-bit Ethernet CRC for the given buffer.
1828 */
1829static u_int32_t
1830xe_compute_crc(u_int8_t *data, int len) {
1831 u_int32_t crc = 0xffffffff;
1832 u_int32_t poly = 0x04c11db6;
1833 u_int8_t current, crc31, bit;
1834 int i, k;

--- 84 unchanged lines hidden (view full) ---

1919 */
1920static int
1921xe_mii_init(struct xe_softc *scp) {
1922 u_int16_t status;
1923
1924 status = xe_phy_readreg(scp, PHY_BMSR);
1925 if ((status & 0xff00) != 0x7800) {
1926#if XE_DEBUG > 1
1927 device_printf(scp->dev, "no PHY found, %0x\n", status);
1928#endif
1929 return 0;
1930 }
1931 else {
1932#if XE_DEBUG > 1
1933 device_printf(scp->dev, "PHY OK!\n");
1934#endif
1935
1936 /* Reset the PHY */
1937 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1938 DELAY(500);
1939 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1940 XE_MII_DUMP(scp);
1941 return 1;

--- 206 unchanged lines hidden (view full) ---

2148 * A bit of debugging code.
2149 */
2150static void
2151xe_mii_dump(struct xe_softc *scp) {
2152 int i, s;
2153
2154 s = splimp();
2155
2156 device_printf(scp->dev, "MII registers: ");
2157 for (i = 0; i < 2; i++) {
2158 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
2159 }
2160 for (i = 4; i < 7; i++) {
2161 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
2162 }
2163 printf("\n");
2164
2165 (void)splx(s);
2166}
2167
2168static void
2169xe_reg_dump(struct xe_softc *scp) {
2170 int page, i, s;
2171
2172 s = splimp();
2173
2174 device_printf(scp->dev, "Common registers: ");
2175 for (i = 0; i < 8; i++) {
2176 printf(" %2.2x", XE_INB(i));
2177 }
2178 printf("\n");
2179
2180 for (page = 0; page <= 8; page++) {
2181 device_printf(scp->dev, "Register page %2.2x: ", page);
2182 XE_SELECT_PAGE(page);
2183 for (i = 8; i < 16; i++) {
2184 printf(" %2.2x", XE_INB(i));
2185 }
2186 printf("\n");
2187 }
2188
2189 for (page = 0x10; page < 0x5f; page++) {
2190 if ((page >= 0x11 && page <= 0x3f) ||
2191 (page == 0x41) ||
2192 (page >= 0x43 && page <= 0x4f) ||
2193 (page >= 0x59))
2194 continue;
2195 device_printf(scp->dev, "Register page %2.2x: ", page);
2196 XE_SELECT_PAGE(page);
2197 for (i = 8; i < 16; i++) {
2198 printf(" %2.2x", XE_INB(i));
2199 }
2200 printf("\n");
2201 }
2202
2203 (void)splx(s);
2204}
2205#endif
2206
2207int
2208xe_activate(device_t dev)
2209{
2210 struct xe_softc *sc = device_get_softc(dev);
2211 int err;
2212
2213 sc->port_rid = 0;
2214 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
2215 0, ~0, 16, RF_ACTIVE);
2216 if (!sc->port_res) {
2217#if XE_DEBUG > 0
2218 device_printf(dev, "Cannot allocate ioport\n");
2219#endif
2220 return ENOMEM;
2221 }
2222
2223 sc->irq_rid = 0;
2224 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
2225 0, ~0, 1, RF_ACTIVE);
2226 if (!sc->irq_res) {
2227#if XE_DEBUG > 0
2228 device_printf(dev, "Cannot allocate irq\n");
2229#endif
2230 xe_deactivate(dev);
2231 return ENOMEM;
2232 }
2233 if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, xe_intr, sc,
2234 &sc->intrhand)) != 0) {
2235 xe_deactivate(dev);
2236 return err;
2237 }
2238
2239 sc->bst = rman_get_bustag(sc->port_res);
2240 sc->bsh = rman_get_bushandle(sc->port_res);
2241 return (0);
2242}
2243
2244void
2245xe_deactivate(device_t dev)
2246{
2247 struct xe_softc *sc = device_get_softc(dev);
2248
2249 if (sc->intrhand)
2250 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
2251 sc->intrhand = 0;
2252 if (sc->port_res)
2253 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2254 sc->port_res);
2255 sc->port_res = 0;
2256 if (sc->irq_res)
2257 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
2258 sc->irq_res);
2259 sc->irq_res = 0;
2260 return;
2261}
2262
2263static device_method_t xe_pccard_methods[] = {
2264 /* Device interface */
2265 DEVMETHOD(device_probe, xe_probe),
2266 DEVMETHOD(device_attach, xe_attach),
2267 DEVMETHOD(device_detach, xe_detach),
2268
2269 { 0, 0 }
2270};
2271
2272static driver_t xe_pccard_driver = {
2273 "xe",
2274 xe_pccard_methods,
2275 sizeof(struct xe_softc),
2276};
2277
2278devclass_t xe_devclass;
2279
2280DRIVER_MODULE(xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);