Deleted Added
sdiff udiff text old ( 50026 ) new ( 50084 )
full compact
1/*
2 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Herb Peyerl.
16 * 4. The name of Herb Peyerl may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * if_ep.c,v 1.19 1995/01/24 20:53:45 davidg Exp
31 */
32
33/*
34 * Modified from the FreeBSD 1.1.5.1 version by:
35 * Andres Vega Garcia
36 * INRIA - Sophia Antipolis, France
37 * avega@sophia.inria.fr
38 */
39
40/*
41 * $Id: if_ep.c,v 1.82 1999/08/18 06:11:58 mdodd Exp $
42 *
43 * Promiscuous mode added and interrupt logic slightly changed
44 * to reduce the number of adapter failures. Transceiver select
45 * logic changed to use value from EEPROM. Autoconfiguration
46 * features added.
47 * Done by:
48 * Serge Babkin
49 * Chelindbank (Chelyabinsk, Russia)
50 * babkin@hq.icb.chel.su
51 */
52
53/*
54 * Pccard support for 3C589 by:
55 * HAMADA Naoki
56 * nao@tom-yam.or.jp
57 */
58
59#include "ep.h"
60#if NEP > 0
61
62#include "bpf.h"
63#include "opt_inet.h"
64#include "opt_ipx.h"
65
66#include <sys/param.h>
67#if defined(__FreeBSD__)
68#include <sys/kernel.h>
69#include <sys/systm.h>
70#endif
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/socket.h>
74#include <sys/sockio.h>
75#if defined(__NetBSD__)
76#include <sys/select.h>
77#endif
78
79#include <net/ethernet.h>
80#include <net/if.h>
81
82#include <netinet/in.h>
83#include <netinet/if_ether.h>
84
85#if NBPF > 0
86#include <net/bpf.h>
87#endif
88
89#if defined(__FreeBSD__)
90#include <machine/clock.h>
91#endif
92
93#include <i386/isa/isa_device.h>
94#include <i386/isa/if_epreg.h>
95#include <i386/isa/elink.h>
96
97/* DELAY_MULTIPLE: How much to boost "base" delays, except
98 * for the inter-bit delays in get_eeprom_data. A cyrix Media GX needed this.
99 */
100#define DELAY_MULTIPLE 10
101#define BIT_DELAY_MULTIPLE 10
102
103/* Exported variables */
104u_long ep_unit;
105int ep_boards;
106struct ep_board ep_board[EP_MAX_BOARDS + 1];
107
108static int eeprom_rdy __P((struct ep_softc *sc));
109
110static int ep_isa_probe __P((struct isa_device *));
111static struct ep_board * ep_look_for_board_at __P((struct isa_device *is));
112static int ep_isa_attach __P((struct isa_device *));
113static int epioctl __P((struct ifnet * ifp, u_long, caddr_t));
114
115static void epinit __P((struct ep_softc *));
116static ointhand2_t epintr;
117static void epread __P((struct ep_softc *));
118void epreset __P((int));
119static void epstart __P((struct ifnet *));
120static void epstop __P((struct ep_softc *));
121static void epwatchdog __P((struct ifnet *));
122
123#if 0
124static int send_ID_sequence __P((int));
125#endif
126static int get_eeprom_data __P((int, int));
127
128static struct ep_softc* ep_softc[NEP];
129static int ep_current_tag = EP_LAST_TAG + 1;
130static char *ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
131
132#define ep_ftst(f) (sc->stat&(f))
133#define ep_fset(f) (sc->stat|=(f))
134#define ep_frst(f) (sc->stat&=~(f))
135
136struct isa_driver epdriver = {
137 ep_isa_probe,
138 ep_isa_attach,
139 "ep",
140 0
141};
142
143#include "card.h"
144
145#if NCARD > 0
146#include <sys/select.h>
147#include <sys/module.h>
148#include <pccard/cardinfo.h>
149#include <pccard/slot.h>
150
151/*
152 * PC-Card (PCMCIA) specific code.
153 */
154static int ep_pccard_init __P((struct pccard_devinfo *));
155static int ep_pccard_attach __P((struct pccard_devinfo *));
156static void ep_unload __P((struct pccard_devinfo *));
157static int card_intr __P((struct pccard_devinfo *));
158
159PCCARD_MODULE(ep, ep_pccard_init, ep_unload, card_intr, 0, net_imask);
160
161/*
162 * Initialize the device - called from Slot manager.
163 */
164static int
165ep_pccard_init(devi)
166 struct pccard_devinfo *devi;
167{
168 struct isa_device *is = &devi->isahd;
169 struct ep_softc *sc = ep_softc[is->id_unit];
170 struct ep_board *epb;
171 int i;
172
173 epb = &ep_board[is->id_unit];
174
175 if (sc == 0) {
176 if ((sc = ep_alloc(is->id_unit, epb)) == 0) {
177 return (ENXIO);
178 }
179 ep_unit++;
180 }
181
182 /* get_e() requires these. */
183 sc->ep_io_addr = is->id_iobase;
184 sc->unit = is->id_unit;
185 epb->cmd_off = 0;
186 if (is->id_flags & EP_FLAGS_100TX)
187 epb->cmd_off = 2;
188
189 epb->epb_addr = is->id_iobase;
190 epb->epb_used = 1;
191 epb->prod_id = get_e(sc, EEPROM_PROD_ID);
192 epb->mii_trans = 0;
193
194 /* product id */
195 switch (epb->prod_id) {
196 case 0x6055: /* 3C556 */
197 case 0x4057: /* 3C574 */
198 epb->mii_trans = 1;
199 break;
200 case 0x9058: /* 3C589 */
201 break;
202 default:
203 printf("ep%d: failed to come ready.\n", is->id_unit);
204 return (ENXIO);
205 }
206
207 epb->res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
208 for (i = 0; i < 3; i++)
209 sc->epb->eth_addr[i] = get_e(sc, EEPROM_NODE_ADDR_0 + i);
210
211 if (ep_pccard_attach(devi) == 0)
212 return (ENXIO);
213
214 sc->arpcom.ac_if.if_snd.ifq_maxlen = ifqmaxlen;
215 return (0);
216}
217
218static int
219ep_pccard_attach(devi)
220 struct pccard_devinfo *devi;
221{
222 struct isa_device *is = &devi->isahd;
223 struct ep_softc *sc = ep_softc[is->id_unit];
224 u_short config;
225
226 sc->ep_connectors = 0;
227 config = inw(IS_BASE + EP_W0_CONFIG_CTRL);
228 if (config & IS_BNC) {
229 sc->ep_connectors |= BNC;
230 }
231 if (config & IS_UTP) {
232 sc->ep_connectors |= UTP;
233 }
234 if (!(sc->ep_connectors & 7))
235 printf("ep%d: No connectors or MII.\n", is->id_unit);
236 sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
237
238 /* ROM size = 0, ROM base = 0 */
239 /* For now, ignore AUTO SELECT feature of 3C589B and later. */
240 outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
241
242 /* Fake IRQ must be 3 */
243 outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb->res_cfg & 0x0fff) | 0x3000);
244
245 outw(BASE + EP_W0_PRODUCT_ID, sc->epb->prod_id);
246
247 if (sc->epb->mii_trans) {
248 /*
249 * turn on the MII tranceiver
250 */
251 GO_WINDOW(3);
252 outw(BASE + EP_W3_OPTIONS, 0x8040);
253 DELAY(1000);
254 outw(BASE + EP_W3_OPTIONS, 0xc040);
255 outw(BASE + EP_COMMAND, RX_RESET);
256 outw(BASE + EP_COMMAND, TX_RESET);
257 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
258 DELAY(1000);
259 outw(BASE + EP_W3_OPTIONS, 0x8040);
260 }
261
262 ep_attach(sc);
263
264 return 1;
265}
266
267static void
268ep_unload(devi)
269 struct pccard_devinfo *devi;
270{
271 struct ep_softc *sc = ep_softc[devi->isahd.id_unit];
272
273 if (sc->gone) {
274 printf("ep%d: already unloaded\n", devi->isahd.id_unit);
275 return;
276 }
277 sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
278 sc->gone = 1;
279 printf("ep%d: unload\n", devi->isahd.id_unit);
280}
281
282/*
283 * card_intr - Shared interrupt called from
284 * front end of PC-Card handler.
285 */
286static int
287card_intr(devi)
288 struct pccard_devinfo *devi;
289{
290 epintr(devi->isahd.id_unit);
291 return(1);
292}
293#endif /* NCARD > 0 */
294
295static int
296eeprom_rdy(sc)
297 struct ep_softc *sc;
298{
299 int i;
300
301 for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
302 continue;
303 if (i >= MAX_EEPROMBUSY) {
304 printf("ep%d: eeprom failed to come ready.\n", sc->unit);
305 return (0);
306 }
307 return (1);
308}
309
310static struct ep_board *
311ep_look_for_board_at(is)
312 struct isa_device *is;
313{
314 int data, i, j, id_port = ELINK_ID_PORT;
315 int count = 0;
316
317 if (ep_current_tag == (EP_LAST_TAG + 1)) {
318 /* Come here just one time */
319
320 ep_current_tag--;
321
322 /* Look for the ISA boards. Init and leave them actived */
323 outb(id_port, 0);
324 outb(id_port, 0);
325
326 elink_idseq(0xCF);
327
328 elink_reset();
329 DELAY(DELAY_MULTIPLE * 10000);
330 for (i = 0; i < EP_MAX_BOARDS; i++) {
331 outb(id_port, 0);
332 outb(id_port, 0);
333 elink_idseq(0xCF);
334
335 data = get_eeprom_data(id_port, EEPROM_MFG_ID);
336 if (data != MFG_ID)
337 break;
338
339 /* resolve contention using the Ethernet address */
340
341 for (j = 0; j < 3; j++)
342 get_eeprom_data(id_port, j);
343
344 /* and save this address for later use */
345
346 for (j = 0; j < 3; j++)
347 ep_board[ep_boards].eth_addr[j] = get_eeprom_data(id_port, j);
348
349 ep_board[ep_boards].res_cfg =
350 get_eeprom_data(id_port, EEPROM_RESOURCE_CFG);
351
352 ep_board[ep_boards].prod_id =
353 get_eeprom_data(id_port, EEPROM_PROD_ID);
354
355 ep_board[ep_boards].epb_used = 0;
356#ifdef PC98
357 ep_board[ep_boards].epb_addr =
358 (get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x100 + 0x40d0;
359#else
360 ep_board[ep_boards].epb_addr =
361 (get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x10 + 0x200;
362
363 if (ep_board[ep_boards].epb_addr > 0x3E0)
364 /* Board in EISA configuration mode */
365 continue;
366#endif /* PC98 */
367
368 outb(id_port, ep_current_tag); /* tags board */
369 outb(id_port, ACTIVATE_ADAPTER_TO_CONFIG);
370 ep_boards++;
371 count++;
372 ep_current_tag--;
373 }
374
375 ep_board[ep_boards].epb_addr = 0;
376 if (count) {
377 printf("%d 3C5x9 board(s) on ISA found at", count);
378 for (j = 0; ep_board[j].epb_addr; j++)
379 if (ep_board[j].epb_addr <= 0x3E0)
380 printf(" 0x%x", ep_board[j].epb_addr);
381 printf("\n");
382 }
383 }
384
385 /* we have two cases:
386 *
387 * 1. Device was configured with 'port ?'
388 * In this case we search for the first unused card in list
389 *
390 * 2. Device was configured with 'port xxx'
391 * In this case we search for the unused card with that address
392 *
393 */
394
395 if (IS_BASE == -1) { /* port? */
396 for (i = 0; ep_board[i].epb_addr && ep_board[i].epb_used; i++)
397 ;
398 if (ep_board[i].epb_addr == 0)
399 return 0;
400
401 IS_BASE = ep_board[i].epb_addr;
402 ep_board[i].epb_used = 1;
403
404 return &ep_board[i];
405 } else {
406 for (i = 0;
407 ep_board[i].epb_addr && ep_board[i].epb_addr != IS_BASE;
408 i++)
409 ;
410
411 if (ep_board[i].epb_used || ep_board[i].epb_addr != IS_BASE)
412 return 0;
413
414 if (inw(IS_BASE + EP_W0_EEPROM_COMMAND) & EEPROM_TST_MODE) {
415 printf("ep%d: 3c5x9 at 0x%x in PnP mode. Disable PnP mode!\n",
416 is->id_unit, IS_BASE);
417 }
418 ep_board[i].epb_used = 1;
419
420 return &ep_board[i];
421 }
422}
423
424/*
425 * get_e: gets a 16 bits word from the EEPROM. we must have set the window
426 * before
427 */
428u_int16_t
429get_e(sc, offset)
430 struct ep_softc *sc;
431 int offset;
432{
433 if (!eeprom_rdy(sc))
434 return (0xffff);
435 outw(BASE + EP_W0_EEPROM_COMMAND, (EEPROM_CMD_RD << sc->epb->cmd_off) | offset);
436 if (!eeprom_rdy(sc))
437 return (0xffff);
438 return (inw(BASE + EP_W0_EEPROM_DATA));
439}
440
441struct ep_softc *
442ep_alloc(unit, epb)
443 int unit;
444 struct ep_board *epb;
445{
446 struct ep_softc *sc;
447
448 if (unit >= NEP) {
449 printf("ep: unit number (%d) too high\n", unit);
450 return NULL;
451 }
452
453 /*
454 * Allocate a storage area for us
455 */
456 if (ep_softc[unit]) {
457 printf("ep%d: unit number already allocated to another "
458 "adaptor\n", unit);
459 return NULL;
460 }
461
462 sc = malloc(sizeof(struct ep_softc), M_DEVBUF, M_NOWAIT);
463 if (!sc) {
464 printf("ep%d: cannot malloc!\n", unit);
465 return NULL;
466 }
467 bzero(sc, sizeof(struct ep_softc));
468 ep_softc[unit] = sc;
469 sc->unit = unit;
470 sc->ep_io_addr = epb->epb_addr;
471 sc->epb = epb;
472
473 return(sc);
474}
475
476void
477ep_free(sc)
478 struct ep_softc *sc;
479{
480 ep_softc[sc->unit] = NULL;
481 free(sc, M_DEVBUF);
482 return;
483}
484
485int
486ep_isa_probe(is)
487 struct isa_device *is;
488{
489 struct ep_softc *sc;
490 struct ep_board *epb;
491 u_short k;
492
493 if ((epb = ep_look_for_board_at(is)) == 0)
494 return (0);
495
496 /*
497 * Allocate a storage area for us
498 */
499 sc = ep_alloc(ep_unit, epb);
500 if (!sc)
501 return (0);
502
503 is->id_unit = ep_unit++;
504
505 /*
506 * The iobase was found and MFG_ID was 0x6d50. PROD_ID should be
507 * 0x9[0-f]50 (IBM-PC)
508 * 0x9[0-f]5[0-f] (PC-98)
509 */
510 GO_WINDOW(0);
511 k = sc->epb->prod_id;
512#ifdef PC98
513 if ((k & 0xf0f0) != (PROD_ID & 0xf0f0)) {
514#else
515 if ((k & 0xf0ff) != (PROD_ID & 0xf0ff)) {
516#endif
517 printf("ep_isa_probe: ignoring model %04x\n", k);
518 ep_free(sc);
519 return (0);
520 }
521
522 k = sc->epb->res_cfg;
523
524 k >>= 12;
525
526 /* Now we have two cases again:
527 *
528 * 1. Device was configured with 'irq?'
529 * In this case we use irq read from the board
530 *
531 * 2. Device was configured with 'irq xxx'
532 * In this case we set up the board to use specified interrupt
533 *
534 */
535
536 if (is->id_irq == 0) { /* irq? */
537 is->id_irq = 1 << ((k == 2) ? 9 : k);
538 }
539
540 sc->stat = 0; /* 16 bit access */
541
542 /* By now, the adapter is already activated */
543
544 return (EP_IOSIZE); /* 16 bytes of I/O space used. */
545}
546
547static int
548ep_isa_attach(is)
549 struct isa_device *is;
550{
551 struct ep_softc *sc = ep_softc[is->id_unit];
552 u_short config;
553 int irq;
554
555 is->id_ointr = epintr;
556 sc->ep_connectors = 0;
557 config = inw(IS_BASE + EP_W0_CONFIG_CTRL);
558 if (config & IS_AUI) {
559 sc->ep_connectors |= AUI;
560 }
561 if (config & IS_BNC) {
562 sc->ep_connectors |= BNC;
563 }
564 if (config & IS_UTP) {
565 sc->ep_connectors |= UTP;
566 }
567 if (!(sc->ep_connectors & 7))
568 printf("no connectors!");
569 sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
570 /*
571 * Write IRQ value to board
572 */
573
574 irq = ffs(is->id_irq) - 1;
575 if (irq == -1) {
576 printf(" invalid irq... cannot attach\n");
577 return 0;
578 }
579
580 GO_WINDOW(0);
581 SET_IRQ(BASE, irq);
582
583 ep_attach(sc);
584 return 1;
585}
586
587int
588ep_attach(sc)
589 struct ep_softc *sc;
590{
591 struct ifnet *ifp = &sc->arpcom.ac_if;
592 u_short *p;
593 int i;
594 int attached;
595
596 sc->gone = 0;
597 attached = (ifp->if_softc != 0);
598
599 printf("ep%d: ", sc->unit);
600 /*
601 * Current media type
602 */
603 if (sc->ep_connectors & AUI) {
604 printf("aui");
605 if (sc->ep_connectors & ~AUI)
606 printf("/");
607 }
608 if (sc->ep_connectors & UTP) {
609 printf("utp");
610 if (sc->ep_connectors & BNC)
611 printf("/");
612 }
613 if (sc->ep_connectors & BNC) {
614 printf("bnc");
615 }
616
617 printf("[*%s*]", ep_conn_type[sc->ep_connector]);
618
619 /*
620 * Setup the station address
621 */
622 p = (u_short *) & sc->arpcom.ac_enaddr;
623 GO_WINDOW(2);
624 for (i = 0; i < 3; i++) {
625 p[i] = htons(sc->epb->eth_addr[i]);
626 outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
627 }
628 printf(" address %6D\n", sc->arpcom.ac_enaddr, ":");
629
630 ifp->if_softc = sc;
631 ifp->if_unit = sc->unit;
632 ifp->if_name = "ep";
633 ifp->if_mtu = ETHERMTU;
634 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
635 ifp->if_output = ether_output;
636 ifp->if_start = epstart;
637 ifp->if_ioctl = epioctl;
638 ifp->if_watchdog = epwatchdog;
639
640 if (!attached) {
641 if_attach(ifp);
642 ether_ifattach(ifp);
643 }
644
645#ifdef EP_LOCAL_STATS
646 sc->rx_no_first = sc->rx_no_mbuf =
647 sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
648 sc->tx_underrun = 0;
649#endif
650 ep_fset(F_RX_FIRST);
651 sc->top = sc->mcur = 0;
652
653#if NBPF > 0
654 if (!attached) {
655 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
656 }
657#endif
658 return 0;
659}
660
661
662/*
663 * The order in here seems important. Otherwise we may not receive
664 * interrupts. ?!
665 */
666static void
667epinit(sc)
668 struct ep_softc *sc;
669{
670 register struct ifnet *ifp = &sc->arpcom.ac_if;
671 int s, i, j;
672
673 if (sc->gone)
674 return;
675
676 /*
677 if (ifp->if_addrlist == (struct ifaddr *) 0)
678 return;
679 */
680
681 s = splimp();
682 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
683
684 GO_WINDOW(0);
685 outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
686 GO_WINDOW(4);
687 outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
688 GO_WINDOW(0);
689
690 /* Disable the card */
691 outw(BASE + EP_W0_CONFIG_CTRL, 0);
692
693 /* Enable the card */
694 outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
695
696 GO_WINDOW(2);
697
698 /* Reload the ether_addr. */
699 for (i = 0; i < 6; i++)
700 outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
701
702 outw(BASE + EP_COMMAND, RX_RESET);
703 outw(BASE + EP_COMMAND, TX_RESET);
704 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
705
706 /* Window 1 is operating window */
707 GO_WINDOW(1);
708 for (i = 0; i < 31; i++)
709 inb(BASE + EP_W1_TX_STATUS);
710
711 /* get rid of stray intr's */
712 outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
713
714 outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
715
716 outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
717
718 if (ifp->if_flags & IFF_PROMISC)
719 outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
720 FIL_GROUP | FIL_BRDCST | FIL_ALL);
721 else
722 outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
723 FIL_GROUP | FIL_BRDCST);
724
725 /*
726 * S.B.
727 *
728 * Now behavior was slightly changed:
729 *
730 * if any of flags link[0-2] is used and its connector is
731 * physically present the following connectors are used:
732 *
733 * link0 - AUI * highest precedence
734 * link1 - BNC
735 * link2 - UTP * lowest precedence
736 *
737 * If none of them is specified then
738 * connector specified in the EEPROM is used
739 * (if present on card or AUI if not).
740 *
741 */
742
743 /* Set the xcvr. */
744 if (ifp->if_flags & IFF_LINK0 && sc->ep_connectors & AUI) {
745 i = ACF_CONNECTOR_AUI;
746 } else if (ifp->if_flags & IFF_LINK1 && sc->ep_connectors & BNC) {
747 i = ACF_CONNECTOR_BNC;
748 } else if (ifp->if_flags & IFF_LINK2 && sc->ep_connectors & UTP) {
749 i = ACF_CONNECTOR_UTP;
750 } else {
751 i = sc->ep_connector;
752 }
753 GO_WINDOW(0);
754 j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
755 outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
756
757 switch(i) {
758 case ACF_CONNECTOR_UTP:
759 if (sc->ep_connectors & UTP) {
760 GO_WINDOW(4);
761 outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
762 }
763 break;
764 case ACF_CONNECTOR_BNC:
765 if (sc->ep_connectors & BNC) {
766 outw(BASE + EP_COMMAND, START_TRANSCEIVER);
767 DELAY(DELAY_MULTIPLE * 1000);
768 }
769 break;
770 case ACF_CONNECTOR_AUI:
771 /* nothing to do */
772 break;
773 default:
774 printf("ep%d: strange connector type in EEPROM: assuming AUI\n",
775 sc->unit);
776 break;
777 }
778
779 outw(BASE + EP_COMMAND, RX_ENABLE);
780 outw(BASE + EP_COMMAND, TX_ENABLE);
781
782 ifp->if_flags |= IFF_RUNNING;
783 ifp->if_flags &= ~IFF_OACTIVE; /* just in case */
784
785#ifdef EP_LOCAL_STATS
786 sc->rx_no_first = sc->rx_no_mbuf =
787 sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
788 sc->tx_underrun = 0;
789#endif
790 ep_fset(F_RX_FIRST);
791 if (sc->top) {
792 m_freem(sc->top);
793 sc->top = sc->mcur = 0;
794 }
795 outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
796 outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
797
798 /*
799 * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
800 * any that we had in case we're being called from intr or somewhere
801 * else.
802 */
803
804 GO_WINDOW(1);
805 epstart(ifp);
806
807 splx(s);
808}
809
810static const char padmap[] = {0, 3, 2, 1};
811
812static void
813epstart(ifp)
814 struct ifnet *ifp;
815{
816 register struct ep_softc *sc = ifp->if_softc;
817 register u_int len;
818 register struct mbuf *m;
819 struct mbuf *top;
820 int s, pad;
821
822 if (sc->gone) {
823 return;
824 }
825
826 s = splimp();
827 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
828 if (ifp->if_flags & IFF_OACTIVE) {
829 splx(s);
830 return;
831 }
832startagain:
833 /* Sneak a peek at the next packet */
834 m = ifp->if_snd.ifq_head;
835 if (m == 0) {
836 splx(s);
837 return;
838 }
839 for (len = 0, top = m; m; m = m->m_next)
840 len += m->m_len;
841
842 pad = padmap[len & 3];
843
844 /*
845 * The 3c509 automatically pads short packets to minimum ethernet length,
846 * but we drop packets that are too large. Perhaps we should truncate
847 * them instead?
848 */
849 if (len + pad > ETHER_MAX_LEN) {
850 /* packet is obviously too large: toss it */
851 ++ifp->if_oerrors;
852 IF_DEQUEUE(&ifp->if_snd, m);
853 m_freem(m);
854 goto readcheck;
855 }
856 if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
857 /* no room in FIFO */
858 outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
859 /* make sure */
860 if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
861 ifp->if_flags |= IFF_OACTIVE;
862 splx(s);
863 return;
864 }
865 }
866 IF_DEQUEUE(&ifp->if_snd, m);
867
868 outw(BASE + EP_W1_TX_PIO_WR_1, len);
869 outw(BASE + EP_W1_TX_PIO_WR_1, 0x0); /* Second dword meaningless */
870
871 for (top = m; m != 0; m = m->m_next)
872 if (ep_ftst(F_ACCESS_32_BITS)) {
873 outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
874 m->m_len / 4);
875 if (m->m_len & 3)
876 outsb(BASE + EP_W1_TX_PIO_WR_1,
877 mtod(m, caddr_t) + (m->m_len & (~3)),
878 m->m_len & 3);
879 } else {
880 outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
881 if (m->m_len & 1)
882 outb(BASE + EP_W1_TX_PIO_WR_1,
883 *(mtod(m, caddr_t) + m->m_len - 1));
884 }
885
886 while (pad--)
887 outb(BASE + EP_W1_TX_PIO_WR_1, 0); /* Padding */
888
889#if NBPF > 0
890 if (ifp->if_bpf) {
891 bpf_mtap(ifp, top);
892 }
893#endif
894
895 ifp->if_timer = 2;
896 ifp->if_opackets++;
897 m_freem(top);
898
899 /*
900 * Is another packet coming in? We don't want to overflow the tiny RX
901 * fifo.
902 */
903readcheck:
904 if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
905 /*
906 * we check if we have packets left, in that case we prepare to come
907 * back later
908 */
909 if (ifp->if_snd.ifq_head) {
910 outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
911 }
912 splx(s);
913 return;
914 }
915 goto startagain;
916}
917
918static void
919epintr(unit)
920 int unit;
921{
922 register struct ep_softc *sc = ep_softc[unit];
923
924 if (sc->gone) {
925 return;
926 }
927
928 ep_intr(sc);
929}
930
931void
932ep_intr(arg)
933 void *arg;
934{
935 struct ep_softc *sc;
936 register int status;
937 struct ifnet *ifp;
938 int x;
939
940 x = splbio();
941
942 sc = (struct ep_softc *)arg;
943
944 ifp = &sc->arpcom.ac_if;
945
946 outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
947
948rescan:
949
950 while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
951
952 /* first acknowledge all interrupt sources */
953 outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
954
955 if (status & (S_RX_COMPLETE | S_RX_EARLY)) {
956 epread(sc);
957 continue;
958 }
959 if (status & S_TX_AVAIL) {
960 /* we need ACK */
961 ifp->if_timer = 0;
962 ifp->if_flags &= ~IFF_OACTIVE;
963 GO_WINDOW(1);
964 inw(BASE + EP_W1_FREE_TX);
965 epstart(ifp);
966 }
967 if (status & S_CARD_FAILURE) {
968 ifp->if_timer = 0;
969#ifdef EP_LOCAL_STATS
970 printf("\nep%d:\n\tStatus: %x\n", sc->unit, status);
971 GO_WINDOW(4);
972 printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
973 printf("\tStat: %x\n", sc->stat);
974 printf("\tIpackets=%d, Opackets=%d\n",
975 ifp->if_ipackets, ifp->if_opackets);
976 printf("\tNOF=%d, NOMB=%d, BPFD=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
977 sc->rx_no_first, sc->rx_no_mbuf, sc->rx_bpf_disc, sc->rx_overrunf,
978 sc->rx_overrunl, sc->tx_underrun);
979#else
980
981#ifdef DIAGNOSTIC
982 printf("ep%d: Status: %x (input buffer overflow)\n", sc->unit, status);
983#else
984 ++ifp->if_ierrors;
985#endif
986
987#endif
988 epinit(sc);
989 splx(x);
990 return;
991 }
992 if (status & S_TX_COMPLETE) {
993 ifp->if_timer = 0;
994 /* we need ACK. we do it at the end */
995 /*
996 * We need to read TX_STATUS until we get a 0 status in order to
997 * turn off the interrupt flag.
998 */
999 while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
1000 if (status & TXS_SUCCES_INTR_REQ);
1001 else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
1002 outw(BASE + EP_COMMAND, TX_RESET);
1003 if (status & TXS_UNDERRUN) {
1004#ifdef EP_LOCAL_STATS
1005 sc->tx_underrun++;
1006#endif
1007 } else {
1008 if (status & TXS_JABBER);
1009 else /* TXS_MAX_COLLISION - we shouldn't get here */
1010 ++ifp->if_collisions;
1011 }
1012 ++ifp->if_oerrors;
1013 outw(BASE + EP_COMMAND, TX_ENABLE);
1014 /*
1015 * To have a tx_avail_int but giving the chance to the
1016 * Reception
1017 */
1018 if (ifp->if_snd.ifq_head) {
1019 outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
1020 }
1021 }
1022 outb(BASE + EP_W1_TX_STATUS, 0x0); /* pops up the next
1023 * status */
1024 } /* while */
1025 ifp->if_flags &= ~IFF_OACTIVE;
1026 GO_WINDOW(1);
1027 inw(BASE + EP_W1_FREE_TX);
1028 epstart(ifp);
1029 } /* end TX_COMPLETE */
1030 }
1031
1032 outw(BASE + EP_COMMAND, C_INTR_LATCH); /* ACK int Latch */
1033
1034 if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
1035 goto rescan;
1036
1037 /* re-enable Ints */
1038 outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
1039
1040 splx(x);
1041}
1042
1043static void
1044epread(sc)
1045 register struct ep_softc *sc;
1046{
1047 struct ether_header *eh;
1048 struct mbuf *top, *mcur, *m;
1049 struct ifnet *ifp;
1050 int lenthisone;
1051
1052 short rx_fifo2, status;
1053 register short rx_fifo;
1054
1055 ifp = &sc->arpcom.ac_if;
1056 status = inw(BASE + EP_W1_RX_STATUS);
1057
1058read_again:
1059
1060 if (status & ERR_RX) {
1061 ++ifp->if_ierrors;
1062 if (status & ERR_RX_OVERRUN) {
1063 /*
1064 * we can think the rx latency is actually greather than we
1065 * expect
1066 */
1067#ifdef EP_LOCAL_STATS
1068 if (ep_ftst(F_RX_FIRST))
1069 sc->rx_overrunf++;
1070 else
1071 sc->rx_overrunl++;
1072#endif
1073 }
1074 goto out;
1075 }
1076 rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
1077
1078 if (ep_ftst(F_RX_FIRST)) {
1079 MGETHDR(m, M_DONTWAIT, MT_DATA);
1080 if (!m)
1081 goto out;
1082 if (rx_fifo >= MINCLSIZE)
1083 MCLGET(m, M_DONTWAIT);
1084 sc->top = sc->mcur = top = m;
1085#define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1086#define EOFF (EROUND - sizeof(struct ether_header))
1087 top->m_data += EOFF;
1088
1089 /* Read what should be the header. */
1090 insw(BASE + EP_W1_RX_PIO_RD_1,
1091 mtod(top, caddr_t), sizeof(struct ether_header) / 2);
1092 top->m_len = sizeof(struct ether_header);
1093 rx_fifo -= sizeof(struct ether_header);
1094 sc->cur_len = rx_fifo2;
1095 } else {
1096 /* come here if we didn't have a complete packet last time */
1097 top = sc->top;
1098 m = sc->mcur;
1099 sc->cur_len += rx_fifo2;
1100 }
1101
1102 /* Reads what is left in the RX FIFO */
1103 while (rx_fifo > 0) {
1104 lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
1105 if (lenthisone == 0) { /* no room in this one */
1106 mcur = m;
1107 MGET(m, M_DONTWAIT, MT_DATA);
1108 if (!m)
1109 goto out;
1110 if (rx_fifo >= MINCLSIZE)
1111 MCLGET(m, M_DONTWAIT);
1112 m->m_len = 0;
1113 mcur->m_next = m;
1114 lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
1115 }
1116 if (ep_ftst(F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
1117 insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
1118 lenthisone / 4);
1119 m->m_len += (lenthisone & ~3);
1120 if (lenthisone & 3)
1121 insb(BASE + EP_W1_RX_PIO_RD_1,
1122 mtod(m, caddr_t) + m->m_len,
1123 lenthisone & 3);
1124 m->m_len += (lenthisone & 3);
1125 } else {
1126 insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
1127 lenthisone / 2);
1128 m->m_len += lenthisone;
1129 if (lenthisone & 1)
1130 *(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
1131 }
1132 rx_fifo -= lenthisone;
1133 }
1134
1135 if (status & ERR_RX_INCOMPLETE) { /* we haven't received the complete
1136 * packet */
1137 sc->mcur = m;
1138#ifdef EP_LOCAL_STATS
1139 sc->rx_no_first++; /* to know how often we come here */
1140#endif
1141 ep_frst(F_RX_FIRST);
1142 if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
1143 /* we see if by now, the packet has completly arrived */
1144 goto read_again;
1145 }
1146 outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
1147 return;
1148 }
1149 outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1150 ++ifp->if_ipackets;
1151 ep_fset(F_RX_FIRST);
1152 top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1153 top->m_pkthdr.len = sc->cur_len;
1154
1155#if NBPF > 0
1156 if (ifp->if_bpf) {
1157 bpf_mtap(ifp, top);
1158
1159 /*
1160 * Note that the interface cannot be in promiscuous mode if there are
1161 * no BPF listeners. And if we are in promiscuous mode, we have to
1162 * check if this packet is really ours.
1163 */
1164 eh = mtod(top, struct ether_header *);
1165 if ((ifp->if_flags & IFF_PROMISC) &&
1166 (eh->ether_dhost[0] & 1) == 0 &&
1167 bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1168 sizeof(eh->ether_dhost)) != 0 &&
1169 bcmp(eh->ether_dhost, etherbroadcastaddr,
1170 sizeof(eh->ether_dhost)) != 0) {
1171 if (sc->top) {
1172 m_freem(sc->top);
1173 sc->top = 0;
1174 }
1175 ep_fset(F_RX_FIRST);
1176#ifdef EP_LOCAL_STATS
1177 sc->rx_bpf_disc++;
1178#endif
1179 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1180 outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
1181 return;
1182 }
1183 }
1184#endif
1185
1186 eh = mtod(top, struct ether_header *);
1187 m_adj(top, sizeof(struct ether_header));
1188 ether_input(ifp, eh, top);
1189 sc->top = 0;
1190 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1191 outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
1192 return;
1193
1194out:
1195 outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1196 if (sc->top) {
1197 m_freem(sc->top);
1198 sc->top = 0;
1199#ifdef EP_LOCAL_STATS
1200 sc->rx_no_mbuf++;
1201#endif
1202 }
1203 ep_fset(F_RX_FIRST);
1204 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1205 outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
1206}
1207
1208/*
1209 * Look familiar?
1210 */
1211static int
1212epioctl(ifp, cmd, data)
1213 register struct ifnet *ifp;
1214 u_long cmd;
1215 caddr_t data;
1216{
1217 struct ep_softc *sc = ifp->if_softc;
1218 int s, error = 0;
1219
1220 s = splimp();
1221
1222 switch (cmd) {
1223 case SIOCSIFADDR:
1224 case SIOCGIFADDR:
1225 case SIOCSIFMTU:
1226 error = ether_ioctl(ifp, cmd, data);
1227 break;
1228
1229 case SIOCSIFFLAGS:
1230
1231 if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1232 ifp->if_flags &= ~IFF_RUNNING;
1233 epstop(sc);
1234 break;
1235 } else {
1236 /* reinitialize card on any parameter change */
1237 epinit(sc);
1238 break;
1239 }
1240
1241 /* NOTREACHED */
1242 break;
1243#ifdef notdef
1244 case SIOCGHWADDR:
1245 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1246 sizeof(sc->sc_addr));
1247 break;
1248#endif
1249 case SIOCADDMULTI:
1250 case SIOCDELMULTI:
1251 /*
1252 * The Etherlink III has no programmable multicast
1253 * filter. We always initialize the card to be
1254 * promiscuous to multicast, since we're always a
1255 * member of the ALL-SYSTEMS group, so there's no
1256 * need to process SIOC*MULTI requests.
1257 */
1258 error = 0;
1259 break;
1260 default:
1261 error = EINVAL;
1262 }
1263
1264 splx(s);
1265
1266 return (error);
1267}
1268
1269static void
1270epwatchdog(ifp)
1271 struct ifnet *ifp;
1272{
1273 struct ep_softc *sc = ifp->if_softc;
1274
1275 /*
1276 printf("ep: watchdog\n");
1277
1278 log(LOG_ERR, "ep%d: watchdog\n", ifp->if_unit);
1279 ifp->if_oerrors++;
1280 */
1281
1282 if (sc->gone) {
1283 return;
1284 }
1285
1286 ifp->if_flags &= ~IFF_OACTIVE;
1287 epstart(ifp);
1288 ep_intr(ifp->if_softc);
1289}
1290
1291static void
1292epstop(sc)
1293 struct ep_softc *sc;
1294{
1295 if (sc->gone) {
1296 return;
1297 }
1298
1299 outw(BASE + EP_COMMAND, RX_DISABLE);
1300 outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1301 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1302 outw(BASE + EP_COMMAND, TX_DISABLE);
1303 outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
1304 outw(BASE + EP_COMMAND, RX_RESET);
1305 outw(BASE + EP_COMMAND, TX_RESET);
1306 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1307 outw(BASE + EP_COMMAND, C_INTR_LATCH);
1308 outw(BASE + EP_COMMAND, SET_RD_0_MASK);
1309 outw(BASE + EP_COMMAND, SET_INTR_MASK);
1310 outw(BASE + EP_COMMAND, SET_RX_FILTER);
1311}
1312
1313
1314#if 0
1315static int
1316send_ID_sequence(port)
1317 int port;
1318{
1319 int cx, al;
1320
1321 for (al = 0xff, cx = 0; cx < 255; cx++) {
1322 outb(port, al);
1323 al <<= 1;
1324 if (al & 0x100)
1325 al ^= 0xcf;
1326 }
1327 return (1);
1328}
1329#endif
1330
1331
1332/*
1333 * We get eeprom data from the id_port given an offset into the eeprom.
1334 * Basically; after the ID_sequence is sent to all of the cards; they enter
1335 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
1336 * the eeprom data. We then read the port 16 times and with every read; the
1337 * cards check for contention (ie: if one card writes a 0 bit and another
1338 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
1339 * compares the data on the bus; if there is a difference then that card goes
1340 * into ID_WAIT state again). In the meantime; one bit of data is returned in
1341 * the AX register which is conveniently returned to us by inb(). Hence; we
1342 * read 16 times getting one bit of data with each read.
1343 */
1344
1345static int
1346get_eeprom_data(id_port, offset)
1347 int id_port;
1348 int offset;
1349{
1350 int i, data = 0;
1351 outb(id_port, 0x80 + offset);
1352 for (i = 0; i < 16; i++) {
1353 DELAY(BIT_DELAY_MULTIPLE * 1000);
1354 data = (data << 1) | (inw(id_port) & 1);
1355 }
1356 return (data);
1357}
1358
1359#endif /* NEP > 0 */