Merge from vendor branch SENDMAIL:
[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.18 2005/05/31 08:12:48 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         if_printf(&sc->arpcom.ac_if, "eeprom failed to come ready.\n");
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         if_initname(&sc->arpcom.ac_if,
201                     device_get_name(dev), 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                         if_printf(&sc->arpcom.ac_if, "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
274         sc->gone = 0;
275
276         ep_get_macaddr(sc, ether_addr);
277
278         /*
279          * Setup the station address
280          */
281         p = (u_short*)ether_addr;
282         GO_WINDOW(2);
283         for (i = 0; i < 3; i++) {
284                 outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
285         }
286   
287         ifp = &sc->arpcom.ac_if;
288
289         ifp->if_softc = sc;
290         ifp->if_mtu = ETHERMTU;
291         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
292         ifp->if_start = ep_if_start;
293         ifp->if_ioctl = ep_if_ioctl;
294         ifp->if_watchdog = ep_if_watchdog;
295         ifp->if_init = ep_if_init;
296         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
297         ifq_set_ready(&ifp->if_snd);
298
299         if (!sc->epb.mii_trans) {
300                 ifmedia_init(&sc->ifmedia, 0, ep_ifmedia_upd, ep_ifmedia_sts);
301
302                 if (sc->ep_connectors & AUI)
303                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
304                 if (sc->ep_connectors & UTP)
305                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
306                 if (sc->ep_connectors & BNC)
307                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
308                 if (!sc->ep_connectors)
309                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
310         
311                 ifmedia_set(&sc->ifmedia, IFM_ETHER|ep_media2if_media[sc->ep_connector]);
312         
313                 ifm = &sc->ifmedia;
314                 ifm->ifm_media = ifm->ifm_cur->ifm_media;
315                 ep_ifmedia_upd(ifp);
316         }
317
318         ether_ifattach(ifp, ether_addr);
319
320 #ifdef EP_LOCAL_STATS
321         sc->rx_no_first = sc->rx_no_mbuf = sc->rx_bpf_disc =
322                 sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
323 #endif
324         EP_FSET(sc, F_RX_FIRST);
325         sc->top = sc->mcur = 0;
326
327         return 0;
328 }
329
330 /*
331  * The order in here seems important. Otherwise we may not receive
332  * interrupts. ?!
333  */
334 static void
335 ep_if_init(xsc)
336     void *xsc;
337 {
338     struct ep_softc *sc = xsc;
339     struct ifnet *ifp = &sc->arpcom.ac_if;
340     int s, i;
341
342     if (sc->gone)
343         return;
344
345         /*
346     if (ifp->if_addrlist == (struct ifaddr *) 0)
347         return;
348         */
349
350     s = splimp();
351     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
352
353     GO_WINDOW(0);
354     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
355     GO_WINDOW(4);
356     outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
357     GO_WINDOW(0);
358
359     /* Disable the card */
360     outw(BASE + EP_W0_CONFIG_CTRL, 0);
361
362     /* Enable the card */
363     outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
364
365     GO_WINDOW(2);
366
367     /* Reload the ether_addr. */
368     for (i = 0; i < 6; i++)
369         outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
370
371     outw(BASE + EP_COMMAND, RX_RESET);
372     outw(BASE + EP_COMMAND, TX_RESET);
373     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
374
375     /* Window 1 is operating window */
376     GO_WINDOW(1);
377     for (i = 0; i < 31; i++)
378         inb(BASE + EP_W1_TX_STATUS);
379
380     /* get rid of stray intr's */
381     outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
382
383     outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
384
385     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
386
387     if (ifp->if_flags & IFF_PROMISC)
388         outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
389          FIL_GROUP | FIL_BRDCST | FIL_ALL);
390     else
391         outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
392          FIL_GROUP | FIL_BRDCST);
393
394     if (!sc->epb.mii_trans) {
395         ep_ifmedia_upd(ifp);
396     }
397
398     outw(BASE + EP_COMMAND, RX_ENABLE);
399     outw(BASE + EP_COMMAND, TX_ENABLE);
400
401     ifp->if_flags |= IFF_RUNNING;
402     ifp->if_flags &= ~IFF_OACTIVE;      /* just in case */
403
404 #ifdef EP_LOCAL_STATS
405     sc->rx_no_first = sc->rx_no_mbuf =
406         sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
407 #endif
408     EP_FSET(sc, F_RX_FIRST);
409     if (sc->top) {
410         m_freem(sc->top);
411         sc->top = sc->mcur = 0;
412     }
413     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
414     outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
415
416     /*
417      * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
418      * any that we had in case we're being called from intr or somewhere
419      * else.
420      */
421
422     GO_WINDOW(1);
423     ep_if_start(ifp);
424
425     splx(s);
426 }
427
428 static const char padmap[] = {0, 3, 2, 1};
429
430 static void
431 ep_if_start(ifp)
432     struct ifnet *ifp;
433 {
434     struct ep_softc *sc = ifp->if_softc;
435     u_int len;
436     struct mbuf *m;
437     struct mbuf *top;
438     int s, pad;
439
440     if (sc->gone) {
441         return;
442     }
443
444     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
445     if (ifp->if_flags & IFF_OACTIVE) {
446         return;
447     }
448
449 startagain:
450     /* Sneak a peek at the next packet */
451     m = ifq_poll(&ifp->if_snd);
452     if (m == NULL)
453         return;
454
455     for (len = 0, top = m; m; m = m->m_next)
456         len += m->m_len;
457
458     pad = padmap[len & 3];
459
460     /*
461      * The 3c509 automatically pads short packets to minimum ethernet length,
462      * but we drop packets that are too large. Perhaps we should truncate
463      * them instead?
464      */
465     if (len + pad > ETHER_MAX_LEN) {
466         /* packet is obviously too large: toss it */
467         ++ifp->if_oerrors;
468         ifq_dequeue(&ifp->if_snd);
469         m_freem(m);
470         goto readcheck;
471     }
472     if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
473         /* no room in FIFO */
474         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
475         /* make sure */
476         if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
477             ifp->if_flags |= IFF_OACTIVE;
478             return;
479         }
480     } else {
481         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE);
482     }
483
484     m = ifq_dequeue(&ifp->if_snd);
485
486     s = splhigh();
487
488     outw(BASE + EP_W1_TX_PIO_WR_1, len); 
489     outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);        /* Second dword meaningless */
490
491     if (EP_FTST(sc, F_ACCESS_32_BITS)) {
492         for (top = m; m != 0; m = m->m_next) {
493             outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
494                   m->m_len / 4);
495             if (m->m_len & 3)
496                 outsb(BASE + EP_W1_TX_PIO_WR_1,
497                       mtod(m, caddr_t) + (m->m_len & (~3)),
498                       m->m_len & 3);
499         }
500     } else {
501         for (top = m; m != 0; m = m->m_next) {
502             outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
503             if (m->m_len & 1)
504                 outb(BASE + EP_W1_TX_PIO_WR_1,
505                      *(mtod(m, caddr_t) + m->m_len - 1));
506         }
507     }
508
509     while (pad--)
510         outb(BASE + EP_W1_TX_PIO_WR_1, 0);      /* Padding */
511
512     splx(s);
513
514     BPF_MTAP(ifp, top);
515
516     ifp->if_timer = 2;
517     ifp->if_opackets++;
518     m_freem(top);
519
520     /*
521      * Is another packet coming in? We don't want to overflow the tiny RX
522      * fifo.
523      */
524 readcheck:
525     if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
526         /*
527          * we check if we have packets left, in that case we prepare to come
528          * back later
529          */
530         if (!ifq_is_empty(&ifp->if_snd))
531             outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
532         return;
533     }
534     goto startagain;
535 }
536
537 void
538 ep_intr(arg)
539     void *arg;
540 {
541     struct ep_softc *sc;
542     int status;
543     struct ifnet *ifp;
544     int x;
545
546     x = splbio();
547
548     sc = (struct ep_softc *)arg;
549
550      /*
551       * quick fix: Try to detect an interrupt when the card goes away.
552       */
553     if (sc->gone || inw(BASE + EP_STATUS) == 0xffff) {
554             splx(x);
555             return;
556     }
557
558     ifp = &sc->arpcom.ac_if;
559
560     outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
561
562 rescan:
563
564     while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
565
566         /* first acknowledge all interrupt sources */
567         outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
568
569         if (status & (S_RX_COMPLETE | S_RX_EARLY))
570             epread(sc);
571         if (status & S_TX_AVAIL) {
572             /* we need ACK */
573             ifp->if_timer = 0;
574             ifp->if_flags &= ~IFF_OACTIVE;
575             GO_WINDOW(1);
576             inw(BASE + EP_W1_FREE_TX);
577             ep_if_start(ifp);
578         }
579         if (status & S_CARD_FAILURE) {
580             ifp->if_timer = 0;
581 #ifdef EP_LOCAL_STATS
582             printf("\n");
583             if_printf(ifp, "\n\tStatus: %x\n", status);
584             GO_WINDOW(4);
585             printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
586             printf("\tStat: %x\n", sc->stat);
587             printf("\tIpackets=%d, Opackets=%d\n",
588                 ifp->if_ipackets, ifp->if_opackets);
589             printf("\tNOF=%d, NOMB=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
590                    sc->rx_no_first, sc->rx_no_mbuf, sc->rx_overrunf,
591                    sc->rx_overrunl, sc->tx_underrun);
592 #else
593
594 #ifdef DIAGNOSTIC
595             if_printf(ifp, "Status: %x (input buffer overflow)\n", status);
596 #else
597             ++ifp->if_ierrors;
598 #endif
599
600 #endif
601             ep_if_init(sc);
602             splx(x);
603             return;
604         }
605         if (status & S_TX_COMPLETE) {
606             ifp->if_timer = 0;
607             /* we  need ACK. we do it at the end */
608             /*
609              * We need to read TX_STATUS until we get a 0 status in order to
610              * turn off the interrupt flag.
611              */
612             while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
613                 if (status & TXS_SUCCES_INTR_REQ);
614                 else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
615                     outw(BASE + EP_COMMAND, TX_RESET);
616                     if (status & TXS_UNDERRUN) {
617 #ifdef EP_LOCAL_STATS
618                         sc->tx_underrun++;
619 #endif
620                     } else {
621                         if (status & TXS_JABBER);
622                         else    /* TXS_MAX_COLLISION - we shouldn't get here */
623                             ++ifp->if_collisions;
624                     }
625                     ++ifp->if_oerrors;
626                     outw(BASE + EP_COMMAND, TX_ENABLE);
627                     /*
628                      * To have a tx_avail_int but giving the chance to the
629                      * Reception
630                      */
631                     if (!ifq_is_empty(&ifp->if_snd))
632                         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
633                 }
634                 outb(BASE + EP_W1_TX_STATUS, 0x0);      /* pops up the next
635                                                          * status */
636             }                   /* while */
637             ifp->if_flags &= ~IFF_OACTIVE;
638             GO_WINDOW(1);
639             inw(BASE + EP_W1_FREE_TX);
640             ep_if_start(ifp);
641         }                       /* end TX_COMPLETE */
642     }
643
644     outw(BASE + EP_COMMAND, C_INTR_LATCH);      /* ACK int Latch */
645
646     if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
647         goto rescan;
648
649     /* re-enable Ints */
650     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
651
652     splx(x);
653 }
654
655 static void
656 epread(sc)
657     struct ep_softc *sc;
658 {
659     struct mbuf *top, *mcur, *m;
660     struct ifnet *ifp;
661     int lenthisone;
662
663     short rx_fifo2, status;
664     short rx_fifo;
665
666     ifp = &sc->arpcom.ac_if;
667     status = inw(BASE + EP_W1_RX_STATUS);
668
669 read_again:
670
671     if (status & ERR_RX) {
672         ++ifp->if_ierrors;
673         if (status & ERR_RX_OVERRUN) {
674             /*
675              * we can think the rx latency is actually greather than we
676              * expect
677              */
678 #ifdef EP_LOCAL_STATS
679             if (EP_FTST(sc, F_RX_FIRST))
680                 sc->rx_overrunf++;
681             else
682                 sc->rx_overrunl++;
683 #endif
684         }
685         goto out;
686     }
687     rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
688
689     if (EP_FTST(sc, F_RX_FIRST)) {
690         MGETHDR(m, MB_DONTWAIT, MT_DATA);
691         if (!m)
692             goto out;
693         if (rx_fifo >= MINCLSIZE)
694             MCLGET(m, MB_DONTWAIT);
695         sc->top = sc->mcur = top = m;
696 #define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
697 #define EOFF    (EROUND - sizeof(struct ether_header))
698         top->m_data += EOFF;
699
700         /* Read what should be the header. */
701         insw(BASE + EP_W1_RX_PIO_RD_1,
702              mtod(top, caddr_t), sizeof(struct ether_header) / 2);
703         top->m_len = sizeof(struct ether_header);
704         rx_fifo -= sizeof(struct ether_header);
705         sc->cur_len = rx_fifo2;
706     } else {
707         /* come here if we didn't have a complete packet last time */
708         top = sc->top;
709         m = sc->mcur;
710         sc->cur_len += rx_fifo2;
711     }
712
713     /* Reads what is left in the RX FIFO */
714     while (rx_fifo > 0) {
715         lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
716         if (lenthisone == 0) {  /* no room in this one */
717             mcur = m;
718             MGET(m, MB_DONTWAIT, MT_DATA);
719             if (!m)
720                 goto out;
721             if (rx_fifo >= MINCLSIZE)
722                 MCLGET(m, MB_DONTWAIT);
723             m->m_len = 0;
724             mcur->m_next = m;
725             lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
726         }
727         if (EP_FTST(sc, F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
728             insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
729                  lenthisone / 4);
730             m->m_len += (lenthisone & ~3);
731             if (lenthisone & 3)
732                 insb(BASE + EP_W1_RX_PIO_RD_1,
733                      mtod(m, caddr_t) + m->m_len,
734                      lenthisone & 3);
735             m->m_len += (lenthisone & 3);
736         } else {
737             insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
738                  lenthisone / 2);
739             m->m_len += lenthisone;
740             if (lenthisone & 1)
741                 *(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
742         }
743         rx_fifo -= lenthisone;
744     }
745
746     if (status & ERR_RX_INCOMPLETE) {   /* we haven't received the complete
747                                          * packet */
748         sc->mcur = m;
749 #ifdef EP_LOCAL_STATS
750         sc->rx_no_first++;      /* to know how often we come here */
751 #endif
752         EP_FRST(sc, F_RX_FIRST);
753         if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
754             /* we see if by now, the packet has completly arrived */
755             goto read_again;
756         }
757         outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
758         return;
759     }
760     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
761     ++ifp->if_ipackets;
762     EP_FSET(sc, F_RX_FIRST);
763     top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
764     top->m_pkthdr.len = sc->cur_len;
765
766     (*ifp->if_input)(ifp, top);
767     sc->top = 0;
768     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
769     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
770     return;
771
772 out:
773     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
774     if (sc->top) {
775         m_freem(sc->top);
776         sc->top = 0;
777 #ifdef EP_LOCAL_STATS
778         sc->rx_no_mbuf++;
779 #endif
780     }
781     EP_FSET(sc, F_RX_FIRST);
782     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
783     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
784 }
785
786 static int 
787 ep_ifmedia_upd(ifp)
788         struct ifnet *          ifp;
789 {
790         struct ep_softc *       sc = ifp->if_softc;
791         int                     i = 0, j;
792
793         GO_WINDOW(0);
794         outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
795         GO_WINDOW(4);
796         outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
797         GO_WINDOW(0);
798
799         switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) {
800                 case IFM_10_T:
801                         if (sc->ep_connectors & UTP) {
802                                 i = ACF_CONNECTOR_UTP;
803                                 GO_WINDOW(4);
804                                 outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
805                         }
806                         break;
807                 case IFM_10_2:
808                         if (sc->ep_connectors & BNC) {
809                                 i = ACF_CONNECTOR_BNC;
810                                 outw(BASE + EP_COMMAND, START_TRANSCEIVER);
811                                 DELAY(DELAY_MULTIPLE * 1000);
812                         }
813                         break;
814                 case IFM_10_5:
815                         if (sc->ep_connectors & AUI)
816                                 i = ACF_CONNECTOR_AUI;
817                         break;
818                 default:
819                         i = sc->ep_connector;
820                         if_printf(ifp, "strange connector type in EEPROM: "
821                                   "assuming AUI\n");
822                         break;
823         }
824
825         GO_WINDOW(0);
826         j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
827         outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
828
829         return (0);
830 }
831
832 static void
833 ep_ifmedia_sts(ifp, ifmr)
834         struct ifnet *          ifp;
835         struct ifmediareq *     ifmr;
836 {
837         struct ep_softc *       sc = ifp->if_softc;
838
839         ifmr->ifm_active = sc->ifmedia.ifm_media;
840
841         return;
842 }
843
844 static int
845 ep_if_ioctl(ifp, cmd, data, cr)
846         struct ifnet *          ifp;
847         u_long                  cmd;
848         caddr_t                 data;
849         struct ucred *          cr;
850 {
851         struct ep_softc *       sc = ifp->if_softc;
852         struct ifreq *          ifr = (struct ifreq *)data;
853         int s, error = 0;
854
855         s = splimp();
856
857         switch (cmd) {
858         case SIOCSIFFLAGS:
859                 if (((ifp->if_flags & IFF_UP) == 0) &&
860                     (ifp->if_flags & IFF_RUNNING)) {
861                         ifp->if_flags &= ~IFF_RUNNING;
862                         epstop(sc);
863                 } else {
864                         /* reinitialize card on any parameter change */
865                         ep_if_init(sc);
866                 }
867                 break;
868 #ifdef notdef
869         case SIOCGHWADDR:
870                 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
871                       sizeof(sc->sc_addr));
872                 break;
873 #endif
874         case SIOCADDMULTI:
875         case SIOCDELMULTI:
876                 /*
877                  * The Etherlink III has no programmable multicast
878                  * filter.  We always initialize the card to be
879                  * promiscuous to multicast, since we're always a
880                  * member of the ALL-SYSTEMS group, so there's no
881                  * need to process SIOC*MULTI requests.
882                  */
883                 error = 0;
884                 break;
885         case SIOCSIFMEDIA:
886         case SIOCGIFMEDIA:
887                 if (!sc->epb.mii_trans) {
888                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
889                 } else {
890                         error = EINVAL;
891                 }
892                 break;
893         default:
894                 error = ether_ioctl(ifp, cmd, data);
895                 break;
896         }
897
898         (void)splx(s);
899
900         return (error);
901 }
902
903 static void
904 ep_if_watchdog(ifp)
905     struct ifnet *ifp;
906 {
907     struct ep_softc *sc = ifp->if_softc;
908
909     /*
910     if_printf(ifp, "watchdog\n");
911
912     log(LOG_ERR, "%s: watchdog\n", ifp->if_xname);
913     ifp->if_oerrors++;
914     */
915
916     if (sc->gone) {
917         return;
918     }
919
920     ifp->if_flags &= ~IFF_OACTIVE;
921     ep_if_start(ifp);
922     ep_intr(ifp->if_softc);
923 }
924
925 static void
926 epstop(sc)
927     struct ep_softc *sc;
928 {
929     if (sc->gone) {
930         return;
931     }
932
933     outw(BASE + EP_COMMAND, RX_DISABLE);
934     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
935     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
936     outw(BASE + EP_COMMAND, TX_DISABLE);
937     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
938     outw(BASE + EP_COMMAND, RX_RESET);
939     outw(BASE + EP_COMMAND, TX_RESET);
940     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
941     outw(BASE + EP_COMMAND, C_INTR_LATCH);
942     outw(BASE + EP_COMMAND, SET_RD_0_MASK);
943     outw(BASE + EP_COMMAND, SET_INTR_MASK);
944     outw(BASE + EP_COMMAND, SET_RX_FILTER);
945 }