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