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