Use ether_ioctl for the default case and merge the cases which just
[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.16 2005/05/27 15:36:09 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/ifq_var.h>
82 #include <net/if_arp.h>
83 #include <net/if_media.h> 
84 #include <net/ethernet.h>
85 #include <net/bpf.h>
86
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
89
90 #include <machine/clock.h>
91
92 #include "if_epreg.h"
93 #include "if_epvar.h"
94 #include "../elink_layer/elink.h"
95
96 /* Exported variables */
97 devclass_t ep_devclass;
98
99 #if 0
100 static char *   ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
101 static int      if_media2ep_media[] = { 0, 0, 0, UTP, BNC, AUI };
102 #endif
103
104 static int      ep_media2if_media[] =
105         { IFM_10_T, IFM_10_5, IFM_NONE, IFM_10_2, IFM_NONE };
106
107 /* if functions */
108 static void     ep_if_init      (void *);
109 static int      ep_if_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
110 static void     ep_if_start     (struct ifnet *);
111 static void     ep_if_watchdog  (struct ifnet *);
112
113 /* if_media functions */
114 static int      ep_ifmedia_upd  (struct ifnet *);
115 static void     ep_ifmedia_sts  (struct ifnet *, struct ifmediareq *);
116
117 static void     epstop          (struct ep_softc *);
118 static void     epread          (struct ep_softc *);
119 static int      eeprom_rdy      (struct ep_softc *);
120
121 DECLARE_DUMMY_MODULE(if_ep);
122
123 #define EP_FTST(sc, f)  (sc->stat &   (f))
124 #define EP_FSET(sc, f)  (sc->stat |=  (f))
125 #define EP_FRST(sc, f)  (sc->stat &= ~(f))
126
127 static int
128 eeprom_rdy(sc)
129     struct ep_softc *sc;
130 {
131     int i;
132
133     for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++) {
134         DELAY(100);
135     }
136     if (i >= MAX_EEPROMBUSY) {
137         printf("ep%d: eeprom failed to come ready.\n", sc->unit);
138         return (0);
139     }
140     return (1);
141 }
142
143 /*
144  * get_e: gets a 16 bits word from the EEPROM. we must have set the window
145  * before
146  */
147 u_int16_t
148 get_e(sc, offset)
149     struct ep_softc *sc;
150     u_int16_t offset;
151 {
152     if (!eeprom_rdy(sc))
153         return (0);
154     outw(BASE + EP_W0_EEPROM_COMMAND, (EEPROM_CMD_RD << sc->epb.cmd_off) | offset);
155     if (!eeprom_rdy(sc))
156         return (0);
157     return (inw(BASE + EP_W0_EEPROM_DATA));
158 }
159
160 void
161 ep_get_macaddr(sc, addr)
162         struct ep_softc *       sc;
163         uint8_t *               addr;
164 {
165         int                     i;
166         u_int16_t *             macaddr = (u_int16_t *)addr;
167
168         GO_WINDOW(0);
169         for(i = EEPROM_NODE_ADDR_0; i <= EEPROM_NODE_ADDR_2; i++) {
170                 macaddr[i] = htons(get_e(sc, i));
171         }
172
173         return;
174 }
175
176 int
177 ep_alloc(device_t dev)
178 {
179         struct ep_softc *       sc = device_get_softc(dev);
180         int                     rid;
181         int                     error = 0;
182
183         rid = 0;
184         sc->iobase = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
185             RF_ACTIVE);
186         if (!sc->iobase) {
187                 device_printf(dev, "No I/O space?!\n");
188                 error = ENXIO;
189                 goto bad;
190         }
191
192         rid = 0;
193         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 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         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
300         ifq_set_ready(&ifp->if_snd);
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_addr);
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     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 = ifq_poll(&ifp->if_snd);
456     if (m == NULL)
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         ifq_dequeue(&ifp->if_snd);
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     m = ifq_dequeue(&ifp->if_snd);
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     BPF_MTAP(ifp, top);
519
520     ifp->if_timer = 2;
521     ifp->if_opackets++;
522     m_freem(top);
523
524     /*
525      * Is another packet coming in? We don't want to overflow the tiny RX
526      * fifo.
527      */
528 readcheck:
529     if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
530         /*
531          * we check if we have packets left, in that case we prepare to come
532          * back later
533          */
534         if (!ifq_is_empty(&ifp->if_snd))
535             outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
536         return;
537     }
538     goto startagain;
539 }
540
541 void
542 ep_intr(arg)
543     void *arg;
544 {
545     struct ep_softc *sc;
546     int status;
547     struct ifnet *ifp;
548     int x;
549
550     x = splbio();
551
552     sc = (struct ep_softc *)arg;
553
554      /*
555       * quick fix: Try to detect an interrupt when the card goes away.
556       */
557     if (sc->gone || inw(BASE + EP_STATUS) == 0xffff) {
558             splx(x);
559             return;
560     }
561
562     ifp = &sc->arpcom.ac_if;
563
564     outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
565
566 rescan:
567
568     while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
569
570         /* first acknowledge all interrupt sources */
571         outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
572
573         if (status & (S_RX_COMPLETE | S_RX_EARLY))
574             epread(sc);
575         if (status & S_TX_AVAIL) {
576             /* we need ACK */
577             ifp->if_timer = 0;
578             ifp->if_flags &= ~IFF_OACTIVE;
579             GO_WINDOW(1);
580             inw(BASE + EP_W1_FREE_TX);
581             ep_if_start(ifp);
582         }
583         if (status & S_CARD_FAILURE) {
584             ifp->if_timer = 0;
585 #ifdef EP_LOCAL_STATS
586             printf("\nep%d:\n\tStatus: %x\n", sc->unit, status);
587             GO_WINDOW(4);
588             printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
589             printf("\tStat: %x\n", sc->stat);
590             printf("\tIpackets=%d, Opackets=%d\n",
591                 ifp->if_ipackets, ifp->if_opackets);
592             printf("\tNOF=%d, NOMB=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
593                    sc->rx_no_first, sc->rx_no_mbuf, sc->rx_overrunf,
594                    sc->rx_overrunl, sc->tx_underrun);
595 #else
596
597 #ifdef DIAGNOSTIC
598             printf("ep%d: Status: %x (input buffer overflow)\n", sc->unit, status);
599 #else
600             ++ifp->if_ierrors;
601 #endif
602
603 #endif
604             ep_if_init(sc);
605             splx(x);
606             return;
607         }
608         if (status & S_TX_COMPLETE) {
609             ifp->if_timer = 0;
610             /* we  need ACK. we do it at the end */
611             /*
612              * We need to read TX_STATUS until we get a 0 status in order to
613              * turn off the interrupt flag.
614              */
615             while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
616                 if (status & TXS_SUCCES_INTR_REQ);
617                 else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
618                     outw(BASE + EP_COMMAND, TX_RESET);
619                     if (status & TXS_UNDERRUN) {
620 #ifdef EP_LOCAL_STATS
621                         sc->tx_underrun++;
622 #endif
623                     } else {
624                         if (status & TXS_JABBER);
625                         else    /* TXS_MAX_COLLISION - we shouldn't get here */
626                             ++ifp->if_collisions;
627                     }
628                     ++ifp->if_oerrors;
629                     outw(BASE + EP_COMMAND, TX_ENABLE);
630                     /*
631                      * To have a tx_avail_int but giving the chance to the
632                      * Reception
633                      */
634                     if (!ifq_is_empty(&ifp->if_snd))
635                         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
636                 }
637                 outb(BASE + EP_W1_TX_STATUS, 0x0);      /* pops up the next
638                                                          * status */
639             }                   /* while */
640             ifp->if_flags &= ~IFF_OACTIVE;
641             GO_WINDOW(1);
642             inw(BASE + EP_W1_FREE_TX);
643             ep_if_start(ifp);
644         }                       /* end TX_COMPLETE */
645     }
646
647     outw(BASE + EP_COMMAND, C_INTR_LATCH);      /* ACK int Latch */
648
649     if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
650         goto rescan;
651
652     /* re-enable Ints */
653     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
654
655     splx(x);
656 }
657
658 static void
659 epread(sc)
660     struct ep_softc *sc;
661 {
662     struct mbuf *top, *mcur, *m;
663     struct ifnet *ifp;
664     int lenthisone;
665
666     short rx_fifo2, status;
667     short rx_fifo;
668
669     ifp = &sc->arpcom.ac_if;
670     status = inw(BASE + EP_W1_RX_STATUS);
671
672 read_again:
673
674     if (status & ERR_RX) {
675         ++ifp->if_ierrors;
676         if (status & ERR_RX_OVERRUN) {
677             /*
678              * we can think the rx latency is actually greather than we
679              * expect
680              */
681 #ifdef EP_LOCAL_STATS
682             if (EP_FTST(sc, F_RX_FIRST))
683                 sc->rx_overrunf++;
684             else
685                 sc->rx_overrunl++;
686 #endif
687         }
688         goto out;
689     }
690     rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
691
692     if (EP_FTST(sc, F_RX_FIRST)) {
693         MGETHDR(m, MB_DONTWAIT, MT_DATA);
694         if (!m)
695             goto out;
696         if (rx_fifo >= MINCLSIZE)
697             MCLGET(m, MB_DONTWAIT);
698         sc->top = sc->mcur = top = m;
699 #define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
700 #define EOFF    (EROUND - sizeof(struct ether_header))
701         top->m_data += EOFF;
702
703         /* Read what should be the header. */
704         insw(BASE + EP_W1_RX_PIO_RD_1,
705              mtod(top, caddr_t), sizeof(struct ether_header) / 2);
706         top->m_len = sizeof(struct ether_header);
707         rx_fifo -= sizeof(struct ether_header);
708         sc->cur_len = rx_fifo2;
709     } else {
710         /* come here if we didn't have a complete packet last time */
711         top = sc->top;
712         m = sc->mcur;
713         sc->cur_len += rx_fifo2;
714     }
715
716     /* Reads what is left in the RX FIFO */
717     while (rx_fifo > 0) {
718         lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
719         if (lenthisone == 0) {  /* no room in this one */
720             mcur = m;
721             MGET(m, MB_DONTWAIT, MT_DATA);
722             if (!m)
723                 goto out;
724             if (rx_fifo >= MINCLSIZE)
725                 MCLGET(m, MB_DONTWAIT);
726             m->m_len = 0;
727             mcur->m_next = m;
728             lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
729         }
730         if (EP_FTST(sc, F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
731             insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
732                  lenthisone / 4);
733             m->m_len += (lenthisone & ~3);
734             if (lenthisone & 3)
735                 insb(BASE + EP_W1_RX_PIO_RD_1,
736                      mtod(m, caddr_t) + m->m_len,
737                      lenthisone & 3);
738             m->m_len += (lenthisone & 3);
739         } else {
740             insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
741                  lenthisone / 2);
742             m->m_len += lenthisone;
743             if (lenthisone & 1)
744                 *(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
745         }
746         rx_fifo -= lenthisone;
747     }
748
749     if (status & ERR_RX_INCOMPLETE) {   /* we haven't received the complete
750                                          * packet */
751         sc->mcur = m;
752 #ifdef EP_LOCAL_STATS
753         sc->rx_no_first++;      /* to know how often we come here */
754 #endif
755         EP_FRST(sc, F_RX_FIRST);
756         if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
757             /* we see if by now, the packet has completly arrived */
758             goto read_again;
759         }
760         outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
761         return;
762     }
763     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
764     ++ifp->if_ipackets;
765     EP_FSET(sc, F_RX_FIRST);
766     top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
767     top->m_pkthdr.len = sc->cur_len;
768
769     (*ifp->if_input)(ifp, top);
770     sc->top = 0;
771     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
772     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
773     return;
774
775 out:
776     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
777     if (sc->top) {
778         m_freem(sc->top);
779         sc->top = 0;
780 #ifdef EP_LOCAL_STATS
781         sc->rx_no_mbuf++;
782 #endif
783     }
784     EP_FSET(sc, F_RX_FIRST);
785     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
786     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
787 }
788
789 static int 
790 ep_ifmedia_upd(ifp)
791         struct ifnet *          ifp;
792 {
793         struct ep_softc *       sc = ifp->if_softc;
794         int                     i = 0, j;
795
796         GO_WINDOW(0);
797         outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
798         GO_WINDOW(4);
799         outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
800         GO_WINDOW(0);
801
802         switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) {
803                 case IFM_10_T:
804                         if (sc->ep_connectors & UTP) {
805                                 i = ACF_CONNECTOR_UTP;
806                                 GO_WINDOW(4);
807                                 outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
808                         }
809                         break;
810                 case IFM_10_2:
811                         if (sc->ep_connectors & BNC) {
812                                 i = ACF_CONNECTOR_BNC;
813                                 outw(BASE + EP_COMMAND, START_TRANSCEIVER);
814                                 DELAY(DELAY_MULTIPLE * 1000);
815                         }
816                         break;
817                 case IFM_10_5:
818                         if (sc->ep_connectors & AUI)
819                                 i = ACF_CONNECTOR_AUI;
820                         break;
821                 default:
822                         i = sc->ep_connector;
823                         device_printf(sc->dev,
824                                 "strange connector type in EEPROM: assuming AUI\n");
825         }
826
827         GO_WINDOW(0);
828         j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
829         outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
830
831         return (0);
832 }
833
834 static void
835 ep_ifmedia_sts(ifp, ifmr)
836         struct ifnet *          ifp;
837         struct ifmediareq *     ifmr;
838 {
839         struct ep_softc *       sc = ifp->if_softc;
840
841         ifmr->ifm_active = sc->ifmedia.ifm_media;
842
843         return;
844 }
845
846 static int
847 ep_if_ioctl(ifp, cmd, data, cr)
848         struct ifnet *          ifp;
849         u_long                  cmd;
850         caddr_t                 data;
851         struct ucred *          cr;
852 {
853         struct ep_softc *       sc = ifp->if_softc;
854         struct ifreq *          ifr = (struct ifreq *)data;
855         int s, error = 0;
856
857         s = splimp();
858
859         switch (cmd) {
860         case SIOCSIFFLAGS:
861                 if (((ifp->if_flags & IFF_UP) == 0) &&
862                     (ifp->if_flags & IFF_RUNNING)) {
863                         ifp->if_flags &= ~IFF_RUNNING;
864                         epstop(sc);
865                 } else {
866                         /* reinitialize card on any parameter change */
867                         ep_if_init(sc);
868                 }
869                 break;
870 #ifdef notdef
871         case SIOCGHWADDR:
872                 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
873                       sizeof(sc->sc_addr));
874                 break;
875 #endif
876         case SIOCADDMULTI:
877         case SIOCDELMULTI:
878                 /*
879                  * The Etherlink III has no programmable multicast
880                  * filter.  We always initialize the card to be
881                  * promiscuous to multicast, since we're always a
882                  * member of the ALL-SYSTEMS group, so there's no
883                  * need to process SIOC*MULTI requests.
884                  */
885                 error = 0;
886                 break;
887         case SIOCSIFMEDIA:
888         case SIOCGIFMEDIA:
889                 if (!sc->epb.mii_trans) {
890                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
891                 } else {
892                         error = EINVAL;
893                 }
894                 break;
895         default:
896                 error = ether_ioctl(ifp, cmd, data);
897                 break;
898         }
899
900         (void)splx(s);
901
902         return (error);
903 }
904
905 static void
906 ep_if_watchdog(ifp)
907     struct ifnet *ifp;
908 {
909     struct ep_softc *sc = ifp->if_softc;
910
911     /*
912     printf("ep: watchdog\n");
913
914     log(LOG_ERR, "%s: watchdog\n", ifp->if_xname);
915     ifp->if_oerrors++;
916     */
917
918     if (sc->gone) {
919         return;
920     }
921
922     ifp->if_flags &= ~IFF_OACTIVE;
923     ep_if_start(ifp);
924     ep_intr(ifp->if_softc);
925 }
926
927 static void
928 epstop(sc)
929     struct ep_softc *sc;
930 {
931     if (sc->gone) {
932         return;
933     }
934
935     outw(BASE + EP_COMMAND, RX_DISABLE);
936     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
937     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
938     outw(BASE + EP_COMMAND, TX_DISABLE);
939     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
940     outw(BASE + EP_COMMAND, RX_RESET);
941     outw(BASE + EP_COMMAND, TX_RESET);
942     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
943     outw(BASE + EP_COMMAND, C_INTR_LATCH);
944     outw(BASE + EP_COMMAND, SET_RD_0_MASK);
945     outw(BASE + EP_COMMAND, SET_INTR_MASK);
946     outw(BASE + EP_COMMAND, SET_RX_FILTER);
947 }