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