Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / ep / if_ep.c
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  * $FreeBSD: src/sys/dev/ep/if_ep.c,v 1.95.2.3 2002/03/06 07:26:35 imp 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 /*
60  * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
61  *                             <mdodd@FreeBSD.org>
62  */
63
64 #include <sys/param.h>
65 #include <sys/kernel.h>
66 #include <sys/systm.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71
72 #include <sys/module.h>
73 #include <sys/bus.h>
74
75 #include <machine/bus.h>
76 #include <machine/resource.h>
77 #include <sys/rman.h> 
78
79 #include <net/if.h>
80 #include <net/if_arp.h>
81 #include <net/if_media.h> 
82 #include <net/ethernet.h>
83 #include <net/bpf.h>
84
85 #include <netinet/in.h>
86 #include <netinet/if_ether.h>
87
88 #include <machine/clock.h>
89
90 #include <dev/ep/if_epreg.h>
91 #include <dev/ep/if_epvar.h>
92 #include <i386/isa/elink.h>
93
94 /* Exported variables */
95 devclass_t ep_devclass;
96
97 #if 0
98 static char *   ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
99 static int      if_media2ep_media[] = { 0, 0, 0, UTP, BNC, AUI };
100 #endif
101
102 static int      ep_media2if_media[] =
103         { IFM_10_T, IFM_10_5, IFM_NONE, IFM_10_2, IFM_NONE };
104
105 /* if functions */
106 static void     ep_if_init      __P((void *));
107 static int      ep_if_ioctl     __P((struct ifnet *, u_long, caddr_t));
108 static void     ep_if_start     __P((struct ifnet *));
109 static void     ep_if_watchdog  __P((struct ifnet *));
110
111 /* if_media functions */
112 static int      ep_ifmedia_upd  __P((struct ifnet *));
113 static void     ep_ifmedia_sts  __P((struct ifnet *, struct ifmediareq *));
114
115 static void     epstop          __P((struct ep_softc *));
116 static void     epread          __P((struct ep_softc *));
117 static int      eeprom_rdy      __P((struct ep_softc *));
118
119 #define EP_FTST(sc, f)  (sc->stat &   (f))
120 #define EP_FSET(sc, f)  (sc->stat |=  (f))
121 #define EP_FRST(sc, f)  (sc->stat &= ~(f))
122
123 static int
124 eeprom_rdy(sc)
125     struct ep_softc *sc;
126 {
127     int i;
128
129     for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++) {
130         DELAY(100);
131     }
132     if (i >= MAX_EEPROMBUSY) {
133         printf("ep%d: eeprom failed to come ready.\n", sc->unit);
134         return (0);
135     }
136     return (1);
137 }
138
139 /*
140  * get_e: gets a 16 bits word from the EEPROM. we must have set the window
141  * before
142  */
143 u_int16_t
144 get_e(sc, offset)
145     struct ep_softc *sc;
146     u_int16_t offset;
147 {
148     if (!eeprom_rdy(sc))
149         return (0);
150     outw(BASE + EP_W0_EEPROM_COMMAND, (EEPROM_CMD_RD << sc->epb.cmd_off) | offset);
151     if (!eeprom_rdy(sc))
152         return (0);
153     return (inw(BASE + EP_W0_EEPROM_DATA));
154 }
155
156 void
157 ep_get_macaddr(sc, addr)
158         struct ep_softc *       sc;
159         u_char *                addr;
160 {
161         int                     i;
162         u_int16_t *             macaddr = (u_int16_t *)addr;
163
164         GO_WINDOW(0);
165         for(i = EEPROM_NODE_ADDR_0; i <= EEPROM_NODE_ADDR_2; i++) {
166                 macaddr[i] = htons(get_e(sc, i));
167         }
168
169         return;
170 }
171
172 int
173 ep_alloc(device_t dev)
174 {
175         struct ep_softc *       sc = device_get_softc(dev);
176         int                     rid;
177         int                     error = 0;
178
179         rid = 0;
180         sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
181                                         0, ~0, 1, RF_ACTIVE);
182         if (!sc->iobase) {
183                 device_printf(dev, "No I/O space?!\n");
184                 error = ENXIO;
185                 goto bad;
186         }
187
188         rid = 0;
189         sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
190                                      0, ~0, 1, RF_ACTIVE);
191         if (!sc->irq) {
192                 device_printf(dev, "No irq?!\n");
193                 error = ENXIO;
194                 goto bad;
195         }
196
197         sc->dev = dev;
198         sc->unit = device_get_unit(dev);
199         sc->stat = 0;   /* 16 bit access */
200
201         sc->ep_io_addr = rman_get_start(sc->iobase);
202
203         sc->ep_btag = rman_get_bustag(sc->iobase);
204         sc->ep_bhandle = rman_get_bushandle(sc->iobase);
205
206         sc->ep_connectors = 0;
207         sc->ep_connector = 0;
208
209         GO_WINDOW(0);
210         sc->epb.cmd_off = 0;
211         sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
212         sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
213
214 bad:
215         return (error);
216 }
217
218 void
219 ep_get_media(sc)
220         struct ep_softc *       sc;
221 {
222         u_int16_t               config;
223         
224         GO_WINDOW(0);
225         config = inw(BASE + EP_W0_CONFIG_CTRL);
226         if (config & IS_AUI)
227                 sc->ep_connectors |= AUI;
228         if (config & IS_BNC)
229                 sc->ep_connectors |= BNC;
230         if (config & IS_UTP)
231                 sc->ep_connectors |= UTP;
232
233         if (!(sc->ep_connectors & 7)) {
234                 if (bootverbose)
235                         device_printf(sc->dev, "no connectors!\n");
236         }
237
238         /*
239          * This works for most of the cards so we'll do it here.
240          * The cards that require something different can override
241          * this later on.
242          */
243         sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
244
245         return;
246 }
247
248 void
249 ep_free(device_t dev)
250 {
251         struct ep_softc *       sc = device_get_softc(dev);
252
253         if (sc->iobase)
254                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->iobase);
255         if (sc->irq)
256                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
257
258         return;
259 }
260         
261 int
262 ep_attach(sc)
263         struct ep_softc *       sc;
264 {
265         struct ifnet *          ifp = NULL;
266         struct ifmedia *        ifm = NULL;
267         u_short *               p;
268         int                     i;
269         int                     attached;
270
271         sc->gone = 0;
272
273         ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
274
275         /*
276          * Setup the station address
277          */
278         p = (u_short *)&sc->arpcom.ac_enaddr;
279         GO_WINDOW(2);
280         for (i = 0; i < 3; i++) {
281                 outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
282         }
283
284         device_printf(sc->dev, "Ethernet address %6D\n",
285                         sc->arpcom.ac_enaddr, ":");
286   
287         ifp = &sc->arpcom.ac_if;
288         attached = (ifp->if_softc != 0);
289
290         ifp->if_softc = sc;
291         ifp->if_unit = sc->unit;
292         ifp->if_name = "ep";
293         ifp->if_mtu = ETHERMTU;
294         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
295         ifp->if_output = ether_output;
296         ifp->if_start = ep_if_start;
297         ifp->if_ioctl = ep_if_ioctl;
298         ifp->if_watchdog = ep_if_watchdog;
299         ifp->if_init = ep_if_init;
300         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
301
302         if (!sc->epb.mii_trans) {
303                 ifmedia_init(&sc->ifmedia, 0, ep_ifmedia_upd, ep_ifmedia_sts);
304
305                 if (sc->ep_connectors & AUI)
306                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
307                 if (sc->ep_connectors & UTP)
308                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
309                 if (sc->ep_connectors & BNC)
310                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
311                 if (!sc->ep_connectors)
312                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
313         
314                 ifmedia_set(&sc->ifmedia, IFM_ETHER|ep_media2if_media[sc->ep_connector]);
315         
316                 ifm = &sc->ifmedia;
317                 ifm->ifm_media = ifm->ifm_cur->ifm_media;
318                 ep_ifmedia_upd(ifp);
319         }
320
321         if (!attached)
322                 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
323
324 #ifdef EP_LOCAL_STATS
325         sc->rx_no_first = sc->rx_no_mbuf = sc->rx_bpf_disc =
326                 sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
327 #endif
328         EP_FSET(sc, F_RX_FIRST);
329         sc->top = sc->mcur = 0;
330
331         return 0;
332 }
333
334 /*
335  * The order in here seems important. Otherwise we may not receive
336  * interrupts. ?!
337  */
338 static void
339 ep_if_init(xsc)
340     void *xsc;
341 {
342     struct ep_softc *sc = xsc;
343     register struct ifnet *ifp = &sc->arpcom.ac_if;
344     int s, i;
345
346     if (sc->gone)
347         return;
348
349         /*
350     if (ifp->if_addrlist == (struct ifaddr *) 0)
351         return;
352         */
353
354     s = splimp();
355     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
356
357     GO_WINDOW(0);
358     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
359     GO_WINDOW(4);
360     outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
361     GO_WINDOW(0);
362
363     /* Disable the card */
364     outw(BASE + EP_W0_CONFIG_CTRL, 0);
365
366     /* Enable the card */
367     outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
368
369     GO_WINDOW(2);
370
371     /* Reload the ether_addr. */
372     for (i = 0; i < 6; i++)
373         outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
374
375     outw(BASE + EP_COMMAND, RX_RESET);
376     outw(BASE + EP_COMMAND, TX_RESET);
377     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
378
379     /* Window 1 is operating window */
380     GO_WINDOW(1);
381     for (i = 0; i < 31; i++)
382         inb(BASE + EP_W1_TX_STATUS);
383
384     /* get rid of stray intr's */
385     outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
386
387     outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
388
389     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
390
391     if (ifp->if_flags & IFF_PROMISC)
392         outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
393          FIL_GROUP | FIL_BRDCST | FIL_ALL);
394     else
395         outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
396          FIL_GROUP | FIL_BRDCST);
397
398     if (!sc->epb.mii_trans) {
399         ep_ifmedia_upd(ifp);
400     }
401
402     outw(BASE + EP_COMMAND, RX_ENABLE);
403     outw(BASE + EP_COMMAND, TX_ENABLE);
404
405     ifp->if_flags |= IFF_RUNNING;
406     ifp->if_flags &= ~IFF_OACTIVE;      /* just in case */
407
408 #ifdef EP_LOCAL_STATS
409     sc->rx_no_first = sc->rx_no_mbuf =
410         sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
411 #endif
412     EP_FSET(sc, F_RX_FIRST);
413     if (sc->top) {
414         m_freem(sc->top);
415         sc->top = sc->mcur = 0;
416     }
417     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
418     outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
419
420     /*
421      * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
422      * any that we had in case we're being called from intr or somewhere
423      * else.
424      */
425
426     GO_WINDOW(1);
427     ep_if_start(ifp);
428
429     splx(s);
430 }
431
432 static const char padmap[] = {0, 3, 2, 1};
433
434 static void
435 ep_if_start(ifp)
436     struct ifnet *ifp;
437 {
438     struct ep_softc *sc = ifp->if_softc;
439     u_int len;
440     struct mbuf *m;
441     struct mbuf *top;
442     int s, pad;
443
444     if (sc->gone) {
445         return;
446     }
447
448     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
449     if (ifp->if_flags & IFF_OACTIVE) {
450         return;
451     }
452
453 startagain:
454     /* Sneak a peek at the next packet */
455     m = ifp->if_snd.ifq_head;
456     if (m == 0) {
457         return;
458     }
459     for (len = 0, top = m; m; m = m->m_next)
460         len += m->m_len;
461
462     pad = padmap[len & 3];
463
464     /*
465      * The 3c509 automatically pads short packets to minimum ethernet length,
466      * but we drop packets that are too large. Perhaps we should truncate
467      * them instead?
468      */
469     if (len + pad > ETHER_MAX_LEN) {
470         /* packet is obviously too large: toss it */
471         ++ifp->if_oerrors;
472         IF_DEQUEUE(&ifp->if_snd, m);
473         m_freem(m);
474         goto readcheck;
475     }
476     if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
477         /* no room in FIFO */
478         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
479         /* make sure */
480         if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
481             ifp->if_flags |= IFF_OACTIVE;
482             return;
483         }
484     } else {
485         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE);
486     }
487
488     IF_DEQUEUE(&ifp->if_snd, m);
489
490     s = splhigh();
491
492     outw(BASE + EP_W1_TX_PIO_WR_1, len); 
493     outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);        /* Second dword meaningless */
494
495     if (EP_FTST(sc, F_ACCESS_32_BITS)) {
496         for (top = m; m != 0; m = m->m_next) {
497             outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
498                   m->m_len / 4);
499             if (m->m_len & 3)
500                 outsb(BASE + EP_W1_TX_PIO_WR_1,
501                       mtod(m, caddr_t) + (m->m_len & (~3)),
502                       m->m_len & 3);
503         }
504     } else {
505         for (top = m; m != 0; m = m->m_next) {
506             outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
507             if (m->m_len & 1)
508                 outb(BASE + EP_W1_TX_PIO_WR_1,
509                      *(mtod(m, caddr_t) + m->m_len - 1));
510         }
511     }
512
513     while (pad--)
514         outb(BASE + EP_W1_TX_PIO_WR_1, 0);      /* Padding */
515
516     splx(s);
517
518     if (ifp->if_bpf) {
519         bpf_mtap(ifp, top);
520     }
521
522     ifp->if_timer = 2;
523     ifp->if_opackets++;
524     m_freem(top);
525
526     /*
527      * Is another packet coming in? We don't want to overflow the tiny RX
528      * fifo.
529      */
530 readcheck:
531     if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
532         /*
533          * we check if we have packets left, in that case we prepare to come
534          * back later
535          */
536         if (ifp->if_snd.ifq_head) {
537             outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
538         }
539         return;
540     }
541     goto startagain;
542 }
543
544 void
545 ep_intr(arg)
546     void *arg;
547 {
548     struct ep_softc *sc;
549     register int status;
550     struct ifnet *ifp;
551     int x;
552
553     x = splbio();
554
555     sc = (struct ep_softc *)arg;
556
557      /*
558       * quick fix: Try to detect an interrupt when the card goes away.
559       */
560     if (sc->gone || inw(BASE + EP_STATUS) == 0xffff) {
561             splx(x);
562             return;
563     }
564
565     ifp = &sc->arpcom.ac_if;
566
567     outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
568
569 rescan:
570
571     while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
572
573         /* first acknowledge all interrupt sources */
574         outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
575
576         if (status & (S_RX_COMPLETE | S_RX_EARLY))
577             epread(sc);
578         if (status & S_TX_AVAIL) {
579             /* we need ACK */
580             ifp->if_timer = 0;
581             ifp->if_flags &= ~IFF_OACTIVE;
582             GO_WINDOW(1);
583             inw(BASE + EP_W1_FREE_TX);
584             ep_if_start(ifp);
585         }
586         if (status & S_CARD_FAILURE) {
587             ifp->if_timer = 0;
588 #ifdef EP_LOCAL_STATS
589             printf("\nep%d:\n\tStatus: %x\n", sc->unit, status);
590             GO_WINDOW(4);
591             printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
592             printf("\tStat: %x\n", sc->stat);
593             printf("\tIpackets=%d, Opackets=%d\n",
594                 ifp->if_ipackets, ifp->if_opackets);
595             printf("\tNOF=%d, NOMB=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
596                    sc->rx_no_first, sc->rx_no_mbuf, sc->rx_overrunf,
597                    sc->rx_overrunl, sc->tx_underrun);
598 #else
599
600 #ifdef DIAGNOSTIC
601             printf("ep%d: Status: %x (input buffer overflow)\n", sc->unit, status);
602 #else
603             ++ifp->if_ierrors;
604 #endif
605
606 #endif
607             ep_if_init(sc);
608             splx(x);
609             return;
610         }
611         if (status & S_TX_COMPLETE) {
612             ifp->if_timer = 0;
613             /* we  need ACK. we do it at the end */
614             /*
615              * We need to read TX_STATUS until we get a 0 status in order to
616              * turn off the interrupt flag.
617              */
618             while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
619                 if (status & TXS_SUCCES_INTR_REQ);
620                 else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
621                     outw(BASE + EP_COMMAND, TX_RESET);
622                     if (status & TXS_UNDERRUN) {
623 #ifdef EP_LOCAL_STATS
624                         sc->tx_underrun++;
625 #endif
626                     } else {
627                         if (status & TXS_JABBER);
628                         else    /* TXS_MAX_COLLISION - we shouldn't get here */
629                             ++ifp->if_collisions;
630                     }
631                     ++ifp->if_oerrors;
632                     outw(BASE + EP_COMMAND, TX_ENABLE);
633                     /*
634                      * To have a tx_avail_int but giving the chance to the
635                      * Reception
636                      */
637                     if (ifp->if_snd.ifq_head) {
638                         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
639                     }
640                 }
641                 outb(BASE + EP_W1_TX_STATUS, 0x0);      /* pops up the next
642                                                          * status */
643             }                   /* while */
644             ifp->if_flags &= ~IFF_OACTIVE;
645             GO_WINDOW(1);
646             inw(BASE + EP_W1_FREE_TX);
647             ep_if_start(ifp);
648         }                       /* end TX_COMPLETE */
649     }
650
651     outw(BASE + EP_COMMAND, C_INTR_LATCH);      /* ACK int Latch */
652
653     if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
654         goto rescan;
655
656     /* re-enable Ints */
657     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
658
659     splx(x);
660 }
661
662 static void
663 epread(sc)
664     register struct ep_softc *sc;
665 {
666     struct ether_header *eh;
667     struct mbuf *top, *mcur, *m;
668     struct ifnet *ifp;
669     int lenthisone;
670
671     short rx_fifo2, status;
672     register short rx_fifo;
673
674     ifp = &sc->arpcom.ac_if;
675     status = inw(BASE + EP_W1_RX_STATUS);
676
677 read_again:
678
679     if (status & ERR_RX) {
680         ++ifp->if_ierrors;
681         if (status & ERR_RX_OVERRUN) {
682             /*
683              * we can think the rx latency is actually greather than we
684              * expect
685              */
686 #ifdef EP_LOCAL_STATS
687             if (EP_FTST(sc, F_RX_FIRST))
688                 sc->rx_overrunf++;
689             else
690                 sc->rx_overrunl++;
691 #endif
692         }
693         goto out;
694     }
695     rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
696
697     if (EP_FTST(sc, F_RX_FIRST)) {
698         MGETHDR(m, M_DONTWAIT, MT_DATA);
699         if (!m)
700             goto out;
701         if (rx_fifo >= MINCLSIZE)
702             MCLGET(m, M_DONTWAIT);
703         sc->top = sc->mcur = top = m;
704 #define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
705 #define EOFF    (EROUND - sizeof(struct ether_header))
706         top->m_data += EOFF;
707
708         /* Read what should be the header. */
709         insw(BASE + EP_W1_RX_PIO_RD_1,
710              mtod(top, caddr_t), sizeof(struct ether_header) / 2);
711         top->m_len = sizeof(struct ether_header);
712         rx_fifo -= sizeof(struct ether_header);
713         sc->cur_len = rx_fifo2;
714     } else {
715         /* come here if we didn't have a complete packet last time */
716         top = sc->top;
717         m = sc->mcur;
718         sc->cur_len += rx_fifo2;
719     }
720
721     /* Reads what is left in the RX FIFO */
722     while (rx_fifo > 0) {
723         lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
724         if (lenthisone == 0) {  /* no room in this one */
725             mcur = m;
726             MGET(m, M_DONTWAIT, MT_DATA);
727             if (!m)
728                 goto out;
729             if (rx_fifo >= MINCLSIZE)
730                 MCLGET(m, M_DONTWAIT);
731             m->m_len = 0;
732             mcur->m_next = m;
733             lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
734         }
735         if (EP_FTST(sc, F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
736             insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
737                  lenthisone / 4);
738             m->m_len += (lenthisone & ~3);
739             if (lenthisone & 3)
740                 insb(BASE + EP_W1_RX_PIO_RD_1,
741                      mtod(m, caddr_t) + m->m_len,
742                      lenthisone & 3);
743             m->m_len += (lenthisone & 3);
744         } else {
745             insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
746                  lenthisone / 2);
747             m->m_len += lenthisone;
748             if (lenthisone & 1)
749                 *(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
750         }
751         rx_fifo -= lenthisone;
752     }
753
754     if (status & ERR_RX_INCOMPLETE) {   /* we haven't received the complete
755                                          * packet */
756         sc->mcur = m;
757 #ifdef EP_LOCAL_STATS
758         sc->rx_no_first++;      /* to know how often we come here */
759 #endif
760         EP_FRST(sc, F_RX_FIRST);
761         if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
762             /* we see if by now, the packet has completly arrived */
763             goto read_again;
764         }
765         outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
766         return;
767     }
768     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
769     ++ifp->if_ipackets;
770     EP_FSET(sc, F_RX_FIRST);
771     top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
772     top->m_pkthdr.len = sc->cur_len;
773
774     eh = mtod(top, struct ether_header *);
775     m_adj(top, sizeof(struct ether_header));
776     ether_input(ifp, eh, top);
777     sc->top = 0;
778     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
779     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
780     return;
781
782 out:
783     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
784     if (sc->top) {
785         m_freem(sc->top);
786         sc->top = 0;
787 #ifdef EP_LOCAL_STATS
788         sc->rx_no_mbuf++;
789 #endif
790     }
791     EP_FSET(sc, F_RX_FIRST);
792     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
793     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
794 }
795
796 static int 
797 ep_ifmedia_upd(ifp)
798         struct ifnet *          ifp;
799 {
800         struct ep_softc *       sc = ifp->if_softc;
801         int                     i = 0, j;
802
803         GO_WINDOW(0);
804         outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
805         GO_WINDOW(4);
806         outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
807         GO_WINDOW(0);
808
809         switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) {
810                 case IFM_10_T:
811                         if (sc->ep_connectors & UTP) {
812                                 i = ACF_CONNECTOR_UTP;
813                                 GO_WINDOW(4);
814                                 outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
815                         }
816                         break;
817                 case IFM_10_2:
818                         if (sc->ep_connectors & BNC) {
819                                 i = ACF_CONNECTOR_BNC;
820                                 outw(BASE + EP_COMMAND, START_TRANSCEIVER);
821                                 DELAY(DELAY_MULTIPLE * 1000);
822                         }
823                         break;
824                 case IFM_10_5:
825                         if (sc->ep_connectors & AUI)
826                                 i = ACF_CONNECTOR_AUI;
827                         break;
828                 default:
829                         i = sc->ep_connector;
830                         device_printf(sc->dev,
831                                 "strange connector type in EEPROM: assuming AUI\n");
832         }
833
834         GO_WINDOW(0);
835         j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
836         outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
837
838         return (0);
839 }
840
841 static void
842 ep_ifmedia_sts(ifp, ifmr)
843         struct ifnet *          ifp;
844         struct ifmediareq *     ifmr;
845 {
846         struct ep_softc *       sc = ifp->if_softc;
847
848         ifmr->ifm_active = sc->ifmedia.ifm_media;
849
850         return;
851 }
852
853 static int
854 ep_if_ioctl(ifp, cmd, data)
855         struct ifnet *          ifp;
856         u_long                  cmd;
857         caddr_t                 data;
858 {
859         struct ep_softc *       sc = ifp->if_softc;
860         struct ifreq *          ifr = (struct ifreq *)data;
861         int s, error = 0;
862
863         s = splimp();
864
865         switch (cmd) {
866         case SIOCSIFADDR:
867         case SIOCGIFADDR:
868         case SIOCSIFMTU:
869                 error = ether_ioctl(ifp, cmd, data);
870         break;
871
872         case SIOCSIFFLAGS:
873                 if (((ifp->if_flags & IFF_UP) == 0) &&
874                     (ifp->if_flags & IFF_RUNNING)) {
875                         ifp->if_flags &= ~IFF_RUNNING;
876                         epstop(sc);
877                 } else {
878                         /* reinitialize card on any parameter change */
879                         ep_if_init(sc);
880                 }
881                 break;
882 #ifdef notdef
883         case SIOCGHWADDR:
884                 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
885                       sizeof(sc->sc_addr));
886                 break;
887 #endif
888         case SIOCADDMULTI:
889         case SIOCDELMULTI:
890                 /*
891                  * The Etherlink III has no programmable multicast
892                  * filter.  We always initialize the card to be
893                  * promiscuous to multicast, since we're always a
894                  * member of the ALL-SYSTEMS group, so there's no
895                  * need to process SIOC*MULTI requests.
896                  */
897                 error = 0;
898                 break;
899         case SIOCSIFMEDIA:
900         case SIOCGIFMEDIA:
901                 if (!sc->epb.mii_trans) {
902                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
903                 } else {
904                         error = EINVAL;
905                 }
906                 break;
907         default:
908                 error = EINVAL;
909                 break;
910         }
911
912         (void)splx(s);
913
914         return (error);
915 }
916
917 static void
918 ep_if_watchdog(ifp)
919     struct ifnet *ifp;
920 {
921     struct ep_softc *sc = ifp->if_softc;
922
923     /*
924     printf("ep: watchdog\n");
925
926     log(LOG_ERR, "ep%d: watchdog\n", ifp->if_unit);
927     ifp->if_oerrors++;
928     */
929
930     if (sc->gone) {
931         return;
932     }
933
934     ifp->if_flags &= ~IFF_OACTIVE;
935     ep_if_start(ifp);
936     ep_intr(ifp->if_softc);
937 }
938
939 static void
940 epstop(sc)
941     struct ep_softc *sc;
942 {
943     if (sc->gone) {
944         return;
945     }
946
947     outw(BASE + EP_COMMAND, RX_DISABLE);
948     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
949     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
950     outw(BASE + EP_COMMAND, TX_DISABLE);
951     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
952     outw(BASE + EP_COMMAND, RX_RESET);
953     outw(BASE + EP_COMMAND, TX_RESET);
954     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
955     outw(BASE + EP_COMMAND, C_INTR_LATCH);
956     outw(BASE + EP_COMMAND, SET_RD_0_MASK);
957     outw(BASE + EP_COMMAND, SET_INTR_MASK);
958     outw(BASE + EP_COMMAND, SET_RX_FILTER);
959 }