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