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