Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / netif / ep / if_ep.c
1 /*
2  * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Herb Peyerl.
16  * 4. The name of Herb Peyerl may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      if_ep.c,v 1.19 1995/01/24 20:53:45 davidg Exp
31  */
32
33 /*
34  *      Modified from the FreeBSD 1.1.5.1 version by:
35  *                      Andres Vega Garcia
36  *                      INRIA - Sophia Antipolis, France
37  *                      avega@sophia.inria.fr
38  */
39
40 /*
41  * $FreeBSD: src/sys/dev/ep/if_ep.c,v 1.95.2.3 2002/03/06 07:26:35 imp Exp $
42  * $DragonFly: src/sys/dev/netif/ep/if_ep.c,v 1.24 2006/01/28 14:05:57 sephe 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 #include <sys/thread2.h>
73
74 #include <sys/module.h>
75 #include <sys/bus.h>
76
77 #include <machine/bus.h>
78 #include <machine/resource.h>
79 #include <sys/rman.h> 
80
81 #include <net/if.h>
82 #include <net/ifq_var.h>
83 #include <net/if_arp.h>
84 #include <net/if_media.h> 
85 #include <net/ethernet.h>
86 #include <net/bpf.h>
87
88 #include <netinet/in.h>
89 #include <netinet/if_ether.h>
90
91 #include <machine/clock.h>
92
93 #include "if_epreg.h"
94 #include "if_epvar.h"
95 #include "../elink_layer/elink.h"
96
97 /* Exported variables */
98 devclass_t ep_devclass;
99
100 #if 0
101 static char *   ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
102 static int      if_media2ep_media[] = { 0, 0, 0, UTP, BNC, AUI };
103 #endif
104
105 static int      ep_media2if_media[] =
106         { IFM_10_T, IFM_10_5, IFM_NONE, IFM_10_2, IFM_NONE };
107
108 /* if functions */
109 static void     ep_if_init      (void *);
110 static int      ep_if_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
111 static void     ep_if_start     (struct ifnet *);
112 static void     ep_if_watchdog  (struct ifnet *);
113
114 /* if_media functions */
115 static int      ep_ifmedia_upd  (struct ifnet *);
116 static void     ep_ifmedia_sts  (struct ifnet *, struct ifmediareq *);
117
118 static void     epstop          (struct ep_softc *);
119 static void     epread          (struct ep_softc *);
120 static int      eeprom_rdy      (struct ep_softc *);
121
122 DECLARE_DUMMY_MODULE(if_ep);
123
124 #define EP_FTST(sc, f)  (sc->stat &   (f))
125 #define EP_FSET(sc, f)  (sc->stat |=  (f))
126 #define EP_FRST(sc, f)  (sc->stat &= ~(f))
127
128 static int
129 eeprom_rdy(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(struct ep_softc *sc, int offset)
149 {
150     if (!eeprom_rdy(sc))
151         return (0);
152     outw(BASE + EP_W0_EEPROM_COMMAND, (EEPROM_CMD_RD << sc->epb.cmd_off) | offset);
153     if (!eeprom_rdy(sc))
154         return (0);
155     return (inw(BASE + EP_W0_EEPROM_DATA));
156 }
157
158 void
159 ep_get_macaddr(struct ep_softc *sc, uint8_t *addr)
160 {
161         int                     i;
162         u_int16_t *             macaddr = (u_int16_t *)addr;
163
164         GO_WINDOW(0);
165         for(i = EEPROM_NODE_ADDR_0; i <= EEPROM_NODE_ADDR_2; i++) {
166                 macaddr[i] = htons(get_e(sc, i));
167         }
168
169         return;
170 }
171
172 int
173 ep_alloc(device_t dev)
174 {
175         struct ep_softc *       sc = device_get_softc(dev);
176         int                     rid;
177         int                     error = 0;
178
179         rid = 0;
180         sc->iobase = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
181             RF_ACTIVE);
182         if (!sc->iobase) {
183                 device_printf(dev, "No I/O space?!\n");
184                 error = ENXIO;
185                 goto bad;
186         }
187
188         rid = 0;
189         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
190         if (!sc->irq) {
191                 device_printf(dev, "No irq?!\n");
192                 error = ENXIO;
193                 goto bad;
194         }
195
196         if_initname(&sc->arpcom.ac_if,
197                     device_get_name(dev), device_get_unit(dev));
198         sc->stat = 0;   /* 16 bit access */
199
200         sc->ep_io_addr = rman_get_start(sc->iobase);
201
202         sc->ep_btag = rman_get_bustag(sc->iobase);
203         sc->ep_bhandle = rman_get_bushandle(sc->iobase);
204
205         sc->ep_connectors = 0;
206         sc->ep_connector = 0;
207
208         GO_WINDOW(0);
209         sc->epb.cmd_off = 0;
210         sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
211         sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
212
213 bad:
214         return (error);
215 }
216
217 void
218 ep_get_media(struct ep_softc *sc)
219 {
220         u_int16_t               config;
221         
222         GO_WINDOW(0);
223         config = inw(BASE + EP_W0_CONFIG_CTRL);
224         if (config & IS_AUI)
225                 sc->ep_connectors |= AUI;
226         if (config & IS_BNC)
227                 sc->ep_connectors |= BNC;
228         if (config & IS_UTP)
229                 sc->ep_connectors |= UTP;
230
231         if (!(sc->ep_connectors & 7)) {
232                 if (bootverbose)
233                         if_printf(&sc->arpcom.ac_if, "no connectors!\n");
234         }
235
236         /*
237          * This works for most of the cards so we'll do it here.
238          * The cards that require something different can override
239          * this later on.
240          */
241         sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
242
243         return;
244 }
245
246 void
247 ep_free(device_t dev)
248 {
249         struct ep_softc *       sc = device_get_softc(dev);
250
251         if (sc->iobase)
252                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->iobase);
253         if (sc->irq)
254                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
255
256         return;
257 }
258         
259 int
260 ep_attach(struct ep_softc *sc)
261 {
262         struct ifnet *          ifp = NULL;
263         struct ifmedia *        ifm = NULL;
264         u_short *               p;
265         uint8_t                 ether_addr[ETHER_ADDR_LEN];
266         int                     i;
267
268         sc->gone = 0;
269
270         ep_get_macaddr(sc, ether_addr);
271
272         /*
273          * Setup the station address
274          */
275         p = (u_short*)ether_addr;
276         GO_WINDOW(2);
277         for (i = 0; i < 3; i++) {
278                 outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
279         }
280   
281         ifp = &sc->arpcom.ac_if;
282
283         ifp->if_softc = sc;
284         ifp->if_mtu = ETHERMTU;
285         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
286         ifp->if_start = ep_if_start;
287         ifp->if_ioctl = ep_if_ioctl;
288         ifp->if_watchdog = ep_if_watchdog;
289         ifp->if_init = ep_if_init;
290         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
291         ifq_set_ready(&ifp->if_snd);
292
293         if (!sc->epb.mii_trans) {
294                 ifmedia_init(&sc->ifmedia, 0, ep_ifmedia_upd, ep_ifmedia_sts);
295
296                 if (sc->ep_connectors & AUI)
297                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
298                 if (sc->ep_connectors & UTP)
299                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
300                 if (sc->ep_connectors & BNC)
301                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
302                 if (!sc->ep_connectors)
303                         ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
304         
305                 ifmedia_set(&sc->ifmedia, IFM_ETHER|ep_media2if_media[sc->ep_connector]);
306         
307                 ifm = &sc->ifmedia;
308                 ifm->ifm_media = ifm->ifm_cur->ifm_media;
309                 ep_ifmedia_upd(ifp);
310         }
311
312         ether_ifattach(ifp, ether_addr, NULL);
313
314 #ifdef EP_LOCAL_STATS
315         sc->rx_no_first = sc->rx_no_mbuf = sc->rx_bpf_disc =
316                 sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
317 #endif
318         EP_FSET(sc, F_RX_FIRST);
319         sc->top = sc->mcur = 0;
320
321         return 0;
322 }
323
324 /*
325  * The order in here seems important. Otherwise we may not receive
326  * interrupts. ?!
327  */
328 static void
329 ep_if_init(void *xsc)
330 {
331     struct ep_softc *sc = xsc;
332     struct ifnet *ifp = &sc->arpcom.ac_if;
333     int i;
334
335     if (sc->gone)
336         return;
337
338     crit_enter();
339
340     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
341
342     GO_WINDOW(0);
343     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
344     GO_WINDOW(4);
345     outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
346     GO_WINDOW(0);
347
348     /* Disable the card */
349     outw(BASE + EP_W0_CONFIG_CTRL, 0);
350
351     /* Enable the card */
352     outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
353
354     GO_WINDOW(2);
355
356     /* Reload the ether_addr. */
357     for (i = 0; i < 6; i++)
358         outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
359
360     outw(BASE + EP_COMMAND, RX_RESET);
361     outw(BASE + EP_COMMAND, TX_RESET);
362     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
363
364     /* Window 1 is operating window */
365     GO_WINDOW(1);
366     for (i = 0; i < 31; i++)
367         inb(BASE + EP_W1_TX_STATUS);
368
369     /* get rid of stray intr's */
370     outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
371
372     outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
373
374     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
375
376     if (ifp->if_flags & IFF_PROMISC)
377         outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
378          FIL_GROUP | FIL_BRDCST | FIL_ALL);
379     else
380         outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
381          FIL_GROUP | FIL_BRDCST);
382
383     if (!sc->epb.mii_trans) {
384         ep_ifmedia_upd(ifp);
385     }
386
387     outw(BASE + EP_COMMAND, RX_ENABLE);
388     outw(BASE + EP_COMMAND, TX_ENABLE);
389
390     ifp->if_flags |= IFF_RUNNING;
391     ifp->if_flags &= ~IFF_OACTIVE;      /* just in case */
392
393 #ifdef EP_LOCAL_STATS
394     sc->rx_no_first = sc->rx_no_mbuf =
395         sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
396 #endif
397     EP_FSET(sc, F_RX_FIRST);
398     if (sc->top) {
399         m_freem(sc->top);
400         sc->top = sc->mcur = 0;
401     }
402     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
403     outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
404
405     /*
406      * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
407      * any that we had in case we're being called from intr or somewhere
408      * else.
409      */
410
411     GO_WINDOW(1);
412     ep_if_start(ifp);
413
414     crit_exit();
415 }
416
417 static const char padmap[] = {0, 3, 2, 1};
418
419 static void
420 ep_if_start(struct ifnet *ifp)
421 {
422     struct ep_softc *sc = ifp->if_softc;
423     u_int len;
424     struct mbuf *m;
425     struct mbuf *top;
426     int pad;
427
428     if (sc->gone) {
429         return;
430     }
431
432     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
433     if (ifp->if_flags & IFF_OACTIVE) {
434         return;
435     }
436
437     crit_enter();
438
439 startagain:
440     /* Sneak a peek at the next packet */
441     m = ifq_poll(&ifp->if_snd);
442     if (m == NULL) {
443         crit_exit();
444         return;
445     }
446
447     for (len = 0, top = m; m; m = m->m_next)
448         len += m->m_len;
449     m = top;
450
451     pad = padmap[len & 3];
452
453     /*
454      * The 3c509 automatically pads short packets to minimum ethernet length,
455      * but we drop packets that are too large. Perhaps we should truncate
456      * them instead?
457      */
458     if (len + pad > ETHER_MAX_LEN) {
459         /* packet is obviously too large: toss it */
460         ++ifp->if_oerrors;
461         ifq_dequeue(&ifp->if_snd, m);
462         m_freem(m);
463         goto readcheck;
464     }
465     if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
466         /* no room in FIFO */
467         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
468         /* make sure */
469         if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
470             ifp->if_flags |= IFF_OACTIVE;
471             crit_exit();
472             return;
473         }
474     } else {
475         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE);
476     }
477
478     ifq_dequeue(&ifp->if_snd, m);
479
480     outw(BASE + EP_W1_TX_PIO_WR_1, len); 
481     outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);        /* Second dword meaningless */
482
483     if (EP_FTST(sc, F_ACCESS_32_BITS)) {
484         for (top = m; m != 0; m = m->m_next) {
485             outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
486                   m->m_len / 4);
487             if (m->m_len & 3)
488                 outsb(BASE + EP_W1_TX_PIO_WR_1,
489                       mtod(m, caddr_t) + (m->m_len & (~3)),
490                       m->m_len & 3);
491         }
492     } else {
493         for (top = m; m != 0; m = m->m_next) {
494             outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
495             if (m->m_len & 1)
496                 outb(BASE + EP_W1_TX_PIO_WR_1,
497                      *(mtod(m, caddr_t) + m->m_len - 1));
498         }
499     }
500
501     while (pad--)
502         outb(BASE + EP_W1_TX_PIO_WR_1, 0);      /* Padding */
503
504     BPF_MTAP(ifp, top);
505
506     ifp->if_timer = 2;
507     ifp->if_opackets++;
508     m_freem(top);
509
510     /*
511      * Is another packet coming in? We don't want to overflow the tiny RX
512      * fifo.
513      */
514 readcheck:
515     if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
516         /*
517          * we check if we have packets left, in that case we prepare to come
518          * back later
519          */
520         if (!ifq_is_empty(&ifp->if_snd))
521             outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
522
523         crit_exit();
524         return;
525     }
526     goto startagain;
527 }
528
529 void
530 ep_intr(void *arg)
531 {
532     struct ep_softc *sc = arg;
533     struct ifnet *ifp = &sc->arpcom.ac_if;
534     int status;
535
536      /*
537       * quick fix: Try to detect an interrupt when the card goes away.
538       */
539     if (sc->gone || inw(BASE + EP_STATUS) == 0xffff) {
540             return;
541     }
542
543     outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
544
545 rescan:
546
547     while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
548
549         /* first acknowledge all interrupt sources */
550         outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
551
552         if (status & (S_RX_COMPLETE | S_RX_EARLY))
553             epread(sc);
554         if (status & S_TX_AVAIL) {
555             /* we need ACK */
556             ifp->if_timer = 0;
557             ifp->if_flags &= ~IFF_OACTIVE;
558             GO_WINDOW(1);
559             inw(BASE + EP_W1_FREE_TX);
560             ep_if_start(ifp);
561         }
562         if (status & S_CARD_FAILURE) {
563             ifp->if_timer = 0;
564 #ifdef EP_LOCAL_STATS
565             printf("\n");
566             if_printf(ifp, "\n\tStatus: %x\n", status);
567             GO_WINDOW(4);
568             printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
569             printf("\tStat: %x\n", sc->stat);
570             printf("\tIpackets=%d, Opackets=%d\n",
571                 ifp->if_ipackets, ifp->if_opackets);
572             printf("\tNOF=%d, NOMB=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
573                    sc->rx_no_first, sc->rx_no_mbuf, sc->rx_overrunf,
574                    sc->rx_overrunl, sc->tx_underrun);
575 #else
576
577 #ifdef DIAGNOSTIC
578             if_printf(ifp, "Status: %x (input buffer overflow)\n", status);
579 #else
580             ++ifp->if_ierrors;
581 #endif
582
583 #endif
584             ep_if_init(sc);
585             return;
586         }
587         if (status & S_TX_COMPLETE) {
588             ifp->if_timer = 0;
589             /* we  need ACK. we do it at the end */
590             /*
591              * We need to read TX_STATUS until we get a 0 status in order to
592              * turn off the interrupt flag.
593              */
594             while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
595                 if (status & TXS_SUCCES_INTR_REQ);
596                 else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
597                     outw(BASE + EP_COMMAND, TX_RESET);
598                     if (status & TXS_UNDERRUN) {
599 #ifdef EP_LOCAL_STATS
600                         sc->tx_underrun++;
601 #endif
602                     } else {
603                         if (status & TXS_JABBER);
604                         else    /* TXS_MAX_COLLISION - we shouldn't get here */
605                             ++ifp->if_collisions;
606                     }
607                     ++ifp->if_oerrors;
608                     outw(BASE + EP_COMMAND, TX_ENABLE);
609                     /*
610                      * To have a tx_avail_int but giving the chance to the
611                      * Reception
612                      */
613                     if (!ifq_is_empty(&ifp->if_snd))
614                         outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
615                 }
616                 outb(BASE + EP_W1_TX_STATUS, 0x0);      /* pops up the next
617                                                          * status */
618             }                   /* while */
619             ifp->if_flags &= ~IFF_OACTIVE;
620             GO_WINDOW(1);
621             inw(BASE + EP_W1_FREE_TX);
622             ep_if_start(ifp);
623         }                       /* end TX_COMPLETE */
624     }
625
626     outw(BASE + EP_COMMAND, C_INTR_LATCH);      /* ACK int Latch */
627
628     if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
629         goto rescan;
630
631     /* re-enable Ints */
632     outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
633 }
634
635 static void
636 epread(struct ep_softc *sc)
637 {
638     struct mbuf *top, *mcur, *m;
639     struct ifnet *ifp;
640     int lenthisone;
641
642     short rx_fifo2, status;
643     short rx_fifo;
644
645     ifp = &sc->arpcom.ac_if;
646     status = inw(BASE + EP_W1_RX_STATUS);
647
648 read_again:
649
650     if (status & ERR_RX) {
651         ++ifp->if_ierrors;
652         if (status & ERR_RX_OVERRUN) {
653             /*
654              * we can think the rx latency is actually greather than we
655              * expect
656              */
657 #ifdef EP_LOCAL_STATS
658             if (EP_FTST(sc, F_RX_FIRST))
659                 sc->rx_overrunf++;
660             else
661                 sc->rx_overrunl++;
662 #endif
663         }
664         goto out;
665     }
666     rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
667
668     if (EP_FTST(sc, F_RX_FIRST)) {
669         m = m_getl(rx_fifo, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
670         if (!m)
671             goto out;
672         sc->top = sc->mcur = top = m;
673 #define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
674 #define EOFF    (EROUND - sizeof(struct ether_header))
675         top->m_data += EOFF;
676
677         /* Read what should be the header. */
678         insw(BASE + EP_W1_RX_PIO_RD_1,
679              mtod(top, caddr_t), sizeof(struct ether_header) / 2);
680         top->m_len = sizeof(struct ether_header);
681         rx_fifo -= sizeof(struct ether_header);
682         sc->cur_len = rx_fifo2;
683     } else {
684         /* come here if we didn't have a complete packet last time */
685         top = sc->top;
686         m = sc->mcur;
687         sc->cur_len += rx_fifo2;
688     }
689
690     /* Reads what is left in the RX FIFO */
691     while (rx_fifo > 0) {
692         lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
693         if (lenthisone == 0) {  /* no room in this one */
694             mcur = m;
695             m = m_getl(rx_fifo, MB_DONTWAIT, MT_DATA, 0, NULL);
696             if (!m)
697                 goto out;
698             m->m_len = 0;
699             mcur->m_next = m;
700             lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
701         }
702         if (EP_FTST(sc, F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
703             insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
704                  lenthisone / 4);
705             m->m_len += (lenthisone & ~3);
706             if (lenthisone & 3)
707                 insb(BASE + EP_W1_RX_PIO_RD_1,
708                      mtod(m, caddr_t) + m->m_len,
709                      lenthisone & 3);
710             m->m_len += (lenthisone & 3);
711         } else {
712             insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
713                  lenthisone / 2);
714             m->m_len += lenthisone;
715             if (lenthisone & 1)
716                 *(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
717         }
718         rx_fifo -= lenthisone;
719     }
720
721     if (status & ERR_RX_INCOMPLETE) {   /* we haven't received the complete
722                                          * packet */
723         sc->mcur = m;
724 #ifdef EP_LOCAL_STATS
725         sc->rx_no_first++;      /* to know how often we come here */
726 #endif
727         EP_FRST(sc, F_RX_FIRST);
728         if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
729             /* we see if by now, the packet has completly arrived */
730             goto read_again;
731         }
732         outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
733         return;
734     }
735     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
736     ++ifp->if_ipackets;
737     EP_FSET(sc, F_RX_FIRST);
738     top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
739     top->m_pkthdr.len = sc->cur_len;
740
741     ifp->if_input(ifp, top);
742     sc->top = 0;
743     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
744     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
745     return;
746
747 out:
748     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
749     if (sc->top) {
750         m_freem(sc->top);
751         sc->top = 0;
752 #ifdef EP_LOCAL_STATS
753         sc->rx_no_mbuf++;
754 #endif
755     }
756     EP_FSET(sc, F_RX_FIRST);
757     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
758     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
759 }
760
761 static int 
762 ep_ifmedia_upd(struct ifnet *ifp)
763 {
764         struct ep_softc *       sc = ifp->if_softc;
765         int                     i = 0, j;
766
767         GO_WINDOW(0);
768         outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
769         GO_WINDOW(4);
770         outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
771         GO_WINDOW(0);
772
773         switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) {
774                 case IFM_10_T:
775                         if (sc->ep_connectors & UTP) {
776                                 i = ACF_CONNECTOR_UTP;
777                                 GO_WINDOW(4);
778                                 outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
779                         }
780                         break;
781                 case IFM_10_2:
782                         if (sc->ep_connectors & BNC) {
783                                 i = ACF_CONNECTOR_BNC;
784                                 outw(BASE + EP_COMMAND, START_TRANSCEIVER);
785                                 DELAY(DELAY_MULTIPLE * 1000);
786                         }
787                         break;
788                 case IFM_10_5:
789                         if (sc->ep_connectors & AUI)
790                                 i = ACF_CONNECTOR_AUI;
791                         break;
792                 default:
793                         i = sc->ep_connector;
794                         if_printf(ifp, "strange connector type in EEPROM: "
795                                   "assuming AUI\n");
796                         break;
797         }
798
799         GO_WINDOW(0);
800         j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
801         outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
802
803         return (0);
804 }
805
806 static void
807 ep_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
808 {
809         struct ep_softc *       sc = ifp->if_softc;
810
811         ifmr->ifm_active = sc->ifmedia.ifm_media;
812
813         return;
814 }
815
816 static int
817 ep_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
818 {
819         struct ep_softc *       sc = ifp->if_softc;
820         struct ifreq *          ifr = (struct ifreq *)data;
821         int error = 0;
822
823         crit_enter();
824
825         switch (cmd) {
826         case SIOCSIFFLAGS:
827                 if (((ifp->if_flags & IFF_UP) == 0) &&
828                     (ifp->if_flags & IFF_RUNNING)) {
829                         ifp->if_flags &= ~IFF_RUNNING;
830                         epstop(sc);
831                 } else {
832                         /* reinitialize card on any parameter change */
833                         ep_if_init(sc);
834                 }
835                 break;
836 #ifdef notdef
837         case SIOCGHWADDR:
838                 bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
839                       sizeof(sc->sc_addr));
840                 break;
841 #endif
842         case SIOCADDMULTI:
843         case SIOCDELMULTI:
844                 /*
845                  * The Etherlink III has no programmable multicast
846                  * filter.  We always initialize the card to be
847                  * promiscuous to multicast, since we're always a
848                  * member of the ALL-SYSTEMS group, so there's no
849                  * need to process SIOC*MULTI requests.
850                  */
851                 error = 0;
852                 break;
853         case SIOCSIFMEDIA:
854         case SIOCGIFMEDIA:
855                 if (!sc->epb.mii_trans) {
856                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
857                 } else {
858                         error = EINVAL;
859                 }
860                 break;
861         default:
862                 error = ether_ioctl(ifp, cmd, data);
863                 break;
864         }
865
866         crit_exit();
867
868         return (error);
869 }
870
871 static void
872 ep_if_watchdog(struct ifnet *ifp)
873 {
874     struct ep_softc *sc = ifp->if_softc;
875
876     /*
877     if_printf(ifp, "watchdog\n");
878
879     log(LOG_ERR, "%s: watchdog\n", ifp->if_xname);
880     ifp->if_oerrors++;
881     */
882
883     if (sc->gone) {
884         return;
885     }
886
887     ifp->if_flags &= ~IFF_OACTIVE;
888     ep_if_start(ifp);
889     ep_intr(ifp->if_softc);
890 }
891
892 static void
893 epstop(struct ep_softc *sc)
894 {
895     if (sc->gone) {
896         return;
897     }
898
899     outw(BASE + EP_COMMAND, RX_DISABLE);
900     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
901     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
902     outw(BASE + EP_COMMAND, TX_DISABLE);
903     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
904     outw(BASE + EP_COMMAND, RX_RESET);
905     outw(BASE + EP_COMMAND, TX_RESET);
906     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
907     outw(BASE + EP_COMMAND, C_INTR_LATCH);
908     outw(BASE + EP_COMMAND, SET_RD_0_MASK);
909     outw(BASE + EP_COMMAND, SET_INTR_MASK);
910     outw(BASE + EP_COMMAND, SET_RX_FILTER);
911 }