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