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