]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ep/if_ep.c
This commit was generated by cvs2svn to compensate for changes in r152390,
[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/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         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
308
309         if (!sc->epb.mii_trans) {
310                 ifmedia_init(&sc->ifmedia, 0, ep_ifmedia_upd, ep_ifmedia_sts);
311
312                 if (sc->ep_connectors & AUI)
313                         ifmedia_add(&sc->ifmedia,
314                             IFM_ETHER | IFM_10_5, 0, NULL);
315                 if (sc->ep_connectors & UTP)
316                         ifmedia_add(&sc->ifmedia,
317                             IFM_ETHER | IFM_10_T, 0, NULL);
318                 if (sc->ep_connectors & BNC)
319                         ifmedia_add(&sc->ifmedia,
320                             IFM_ETHER | IFM_10_2, 0, NULL);
321                 if (!sc->ep_connectors)
322                         ifmedia_add(&sc->ifmedia,
323                             IFM_ETHER | IFM_NONE, 0, NULL);
324
325                 ifmedia_set(&sc->ifmedia,
326                     IFM_ETHER | ep_media2if_media[sc->ep_connector]);
327
328                 ifm = &sc->ifmedia;
329                 ifm->ifm_media = ifm->ifm_cur->ifm_media;
330                 ep_ifmedia_upd(ifp);
331         }
332         ether_ifattach(ifp, sc->eaddr);
333
334 #ifdef EP_LOCAL_STATS
335         sc->rx_no_first = sc->rx_no_mbuf = sc->rx_bpf_disc =
336             sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
337 #endif
338         EP_FSET(sc, F_RX_FIRST);
339         sc->top = sc->mcur = 0;
340
341         epstop(sc);
342
343         return (0);
344 }
345
346 int
347 ep_detach(device_t dev)
348 {
349         struct ep_softc *sc;
350         struct ifnet *ifp;
351
352         sc = device_get_softc(dev);
353         ifp = sc->ifp;
354         EP_ASSERT_UNLOCKED(sc);
355         EP_LOCK(sc);
356         if (bus_child_present(dev))
357                 epstop(sc);
358         sc->gone = 1;
359         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
360         EP_UNLOCK(sc);
361         ether_ifdetach(ifp);
362         ep_free(dev);
363
364         if_free(ifp);
365         EP_LOCK_DESTROY(sc);
366
367         return (0);
368 }
369
370 static void
371 epinit(void *xsc)
372 {
373         struct ep_softc *sc = xsc;
374         EP_LOCK(sc);
375         epinit_locked(sc);
376         EP_UNLOCK(sc);
377 }
378
379 /*
380  * The order in here seems important. Otherwise we may not receive
381  * interrupts. ?!
382  */
383 static void
384 epinit_locked(struct ep_softc *sc)
385 {
386         struct ifnet *ifp = sc->ifp;
387         int i;
388
389         if (sc->gone)
390                 return;
391
392         EP_ASSERT_LOCKED(sc);
393         EP_BUSY_WAIT(sc);
394
395         GO_WINDOW(sc, 0);
396         CSR_WRITE_2(sc, EP_COMMAND, STOP_TRANSCEIVER);
397         GO_WINDOW(sc, 4);
398         CSR_WRITE_2(sc, EP_W4_MEDIA_TYPE, DISABLE_UTP);
399         GO_WINDOW(sc, 0);
400
401         /* Disable the card */
402         CSR_WRITE_2(sc, EP_W0_CONFIG_CTRL, 0);
403
404         /* Enable the card */
405         CSR_WRITE_2(sc, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
406
407         GO_WINDOW(sc, 2);
408         /* Reload the ether_addr. */
409         ep_setup_station(sc, IF_LLADDR(sc->ifp));
410
411         CSR_WRITE_2(sc, EP_COMMAND, RX_RESET);
412         CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
413         EP_BUSY_WAIT(sc);
414
415         /* Window 1 is operating window */
416         GO_WINDOW(sc, 1);
417         for (i = 0; i < 31; i++)
418                 CSR_READ_1(sc, EP_W1_TX_STATUS);
419
420         /* get rid of stray intr's */
421         CSR_WRITE_2(sc, EP_COMMAND, ACK_INTR | 0xff);
422
423         CSR_WRITE_2(sc, EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
424         CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK | S_5_INTS);
425
426         if (ifp->if_flags & IFF_PROMISC)
427                 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
428                     FIL_MULTICAST | FIL_BRDCST | FIL_PROMISC);
429         else
430                 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
431                     FIL_MULTICAST | FIL_BRDCST);
432
433         if (!sc->epb.mii_trans)
434                 ep_ifmedia_upd(ifp);
435
436         CSR_WRITE_2(sc, EP_COMMAND, RX_ENABLE);
437         CSR_WRITE_2(sc, EP_COMMAND, TX_ENABLE);
438
439         ifp->if_drv_flags |= IFF_DRV_RUNNING;
440         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;  /* just in case */
441
442 #ifdef EP_LOCAL_STATS
443         sc->rx_no_first = sc->rx_no_mbuf =
444             sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
445 #endif
446         EP_FSET(sc, F_RX_FIRST);
447         if (sc->top) {
448                 m_freem(sc->top);
449                 sc->top = sc->mcur = 0;
450         }
451         CSR_WRITE_2(sc, EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
452         CSR_WRITE_2(sc, EP_COMMAND, SET_TX_START_THRESH | 16);
453
454         GO_WINDOW(sc, 1);
455         epstart_locked(ifp);
456 }
457
458 static void
459 epstart(struct ifnet *ifp)
460 {
461         struct ep_softc *sc;
462         sc = ifp->if_softc;
463         EP_LOCK(sc);
464         epstart_locked(ifp);
465         EP_UNLOCK(sc);
466 }
467         
468 static void
469 epstart_locked(struct ifnet *ifp)
470 {
471         struct ep_softc *sc;
472         u_int len;
473         struct mbuf *m, *m0;
474         int pad;
475
476         sc = ifp->if_softc;
477         if (sc->gone)
478                 return;
479         EP_ASSERT_LOCKED(sc);
480         EP_BUSY_WAIT(sc);
481         if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
482                 return;
483 startagain:
484         /* Sneak a peek at the next packet */
485         IF_DEQUEUE(&ifp->if_snd, m0);
486         if (m0 == NULL)
487                 return;
488         for (len = 0, m = m0; m != NULL; m = m->m_next)
489                 len += m->m_len;
490
491         pad = (4 - len) & 3;
492
493         /*
494          * The 3c509 automatically pads short packets to minimum
495          * ethernet length, but we drop packets that are too large.
496          * Perhaps we should truncate them instead?
497          */
498         if (len + pad > ETHER_MAX_LEN) {
499                 /* packet is obviously too large: toss it */
500                 ifp->if_oerrors++;
501                 m_freem(m0);
502                 goto readcheck;
503         }
504         if (CSR_READ_2(sc, EP_W1_FREE_TX) < len + pad + 4) {
505                 /* no room in FIFO */
506                 CSR_WRITE_2(sc, EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
507                 /* make sure */
508                 if (CSR_READ_2(sc, EP_W1_FREE_TX) < len + pad + 4) {
509                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
510                         IF_PREPEND(&ifp->if_snd, m0);
511                         goto done;
512                 }
513         } else
514                 CSR_WRITE_2(sc, EP_COMMAND,
515                     SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE);
516
517         /* XXX 4.x and earlier would splhigh here */
518
519         CSR_WRITE_2(sc, EP_W1_TX_PIO_WR_1, len);
520         /* Second dword meaningless */
521         CSR_WRITE_2(sc, EP_W1_TX_PIO_WR_1, 0x0);
522
523         if (EP_FTST(sc, F_ACCESS_32_BITS)) {
524                 for (m = m0; m != NULL; m = m->m_next) {
525                         if (m->m_len > 3)
526                                 CSR_WRITE_MULTI_4(sc, EP_W1_TX_PIO_WR_1,
527                                     mtod(m, uint32_t *), m->m_len / 4);
528                         if (m->m_len & 3)
529                                 CSR_WRITE_MULTI_1(sc, EP_W1_TX_PIO_WR_1,
530                                     mtod(m, uint8_t *)+(m->m_len & (~3)),
531                                     m->m_len & 3);
532                 }
533         } else {
534                 for (m = m0; m != NULL; m = m->m_next) {
535                         if (m->m_len > 1)
536                                 CSR_WRITE_MULTI_2(sc, EP_W1_TX_PIO_WR_1,
537                                     mtod(m, uint16_t *), m->m_len / 2);
538                         if (m->m_len & 1)
539                                 CSR_WRITE_1(sc, EP_W1_TX_PIO_WR_1,
540                                     *(mtod(m, uint8_t *)+m->m_len - 1));
541                 }
542         }
543
544         while (pad--)
545                 CSR_WRITE_1(sc, EP_W1_TX_PIO_WR_1, 0);  /* Padding */
546
547         /* XXX and drop splhigh here */
548
549         BPF_MTAP(ifp, m0);
550
551         ifp->if_timer = 2;
552         ifp->if_opackets++;
553         m_freem(m0);
554
555         /*
556          * Is another packet coming in? We don't want to overflow
557          * the tiny RX fifo.
558          */
559 readcheck:
560         if (CSR_READ_2(sc, EP_W1_RX_STATUS) & RX_BYTES_MASK) {
561                 /*
562                  * we check if we have packets left, in that case
563                  * we prepare to come back later
564                  */
565                 if (ifp->if_snd.ifq_head)
566                         CSR_WRITE_2(sc, EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
567                 goto done;
568         }
569         goto startagain;
570 done:;
571         return;
572 }
573
574 void
575 ep_intr(void *arg)
576 {
577         struct ep_softc *sc;
578         int status;
579         struct ifnet *ifp;
580
581         sc = (struct ep_softc *) arg;
582         EP_LOCK(sc);
583         /* XXX 4.x splbio'd here to reduce interruptability */
584
585         /*
586          * quick fix: Try to detect an interrupt when the card goes away.
587          */
588         if (sc->gone || CSR_READ_2(sc, EP_STATUS) == 0xffff) {
589                 EP_UNLOCK(sc);
590                 return;
591         }
592         ifp = sc->ifp;
593
594         CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK);     /* disable all Ints */
595
596 rescan:
597
598         while ((status = CSR_READ_2(sc, EP_STATUS)) & S_5_INTS) {
599
600                 /* first acknowledge all interrupt sources */
601                 CSR_WRITE_2(sc, EP_COMMAND, ACK_INTR | (status & S_MASK));
602
603                 if (status & (S_RX_COMPLETE | S_RX_EARLY))
604                         epread(sc);
605                 if (status & S_TX_AVAIL) {
606                         /* we need ACK */
607                         ifp->if_timer = 0;
608                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
609                         GO_WINDOW(sc, 1);
610                         CSR_READ_2(sc, EP_W1_FREE_TX);
611                         epstart_locked(ifp);
612                 }
613                 if (status & S_CARD_FAILURE) {
614                         ifp->if_timer = 0;
615 #ifdef EP_LOCAL_STATS
616                         device_printf(sc->dev, "\n\tStatus: %x\n", status);
617                         GO_WINDOW(sc, 4);
618                         printf("\tFIFO Diagnostic: %x\n",
619                             CSR_READ_2(sc, EP_W4_FIFO_DIAG));
620                         printf("\tStat: %x\n", sc->stat);
621                         printf("\tIpackets=%d, Opackets=%d\n",
622                             ifp->if_ipackets, ifp->if_opackets);
623                         printf("\tNOF=%d, NOMB=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
624                             sc->rx_no_first, sc->rx_no_mbuf, sc->rx_overrunf,
625                             sc->rx_overrunl, sc->tx_underrun);
626 #else
627
628 #ifdef DIAGNOSTIC
629                         device_printf(sc->dev,
630                             "Status: %x (input buffer overflow)\n", status);
631 #else
632                         ++ifp->if_ierrors;
633 #endif
634
635 #endif
636                         epinit_locked(sc);
637                         EP_UNLOCK(sc);
638                         return;
639                 }
640                 if (status & S_TX_COMPLETE) {
641                         ifp->if_timer = 0;
642                         /*
643                          * We need ACK. We do it at the end.
644                          *
645                          * We need to read TX_STATUS until we get a
646                          * 0 status in order to turn off the interrupt flag.
647                          */
648                         while ((status = CSR_READ_1(sc, EP_W1_TX_STATUS)) &
649                             TXS_COMPLETE) {
650                                 if (status & TXS_SUCCES_INTR_REQ)
651                                         ;       /* nothing */
652                                 else if (status &
653                                     (TXS_UNDERRUN | TXS_JABBER |
654                                     TXS_MAX_COLLISION)) {
655                                         CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
656                                         if (status & TXS_UNDERRUN) {
657 #ifdef EP_LOCAL_STATS
658                                                 sc->tx_underrun++;
659 #endif
660                                         } else {
661                                                 if (status & TXS_JABBER);
662                                                 else
663                                                         ++ifp->if_collisions;
664                                                         /* TXS_MAX_COLLISION
665                                                          * we shouldn't get
666                                                          * here
667                                                          */
668                                         }
669                                         ++ifp->if_oerrors;
670                                         CSR_WRITE_2(sc, EP_COMMAND, TX_ENABLE);
671                                         /*
672                                          * To have a tx_avail_int but giving
673                                          * the chance to the Reception
674                                          */
675                                         if (ifp->if_snd.ifq_head)
676                                                 CSR_WRITE_2(sc, EP_COMMAND,
677                                                     SET_TX_AVAIL_THRESH | 8);
678                                 }
679                                 /* pops up the next status */
680                                 CSR_WRITE_1(sc, EP_W1_TX_STATUS, 0x0);
681                         }       /* while */
682                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
683                         GO_WINDOW(sc, 1);
684                         CSR_READ_2(sc, EP_W1_FREE_TX);
685                         epstart_locked(ifp);
686                 }       /* end TX_COMPLETE */
687         }
688
689         CSR_WRITE_2(sc, EP_COMMAND, C_INTR_LATCH);      /* ACK int Latch */
690
691         if ((status = CSR_READ_2(sc, EP_STATUS)) & S_5_INTS)
692                 goto rescan;
693
694         /* re-enable Ints */
695         CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK | S_5_INTS);
696         EP_UNLOCK(sc);
697 }
698
699 static void
700 epread(struct ep_softc *sc)
701 {
702         struct mbuf *top, *mcur, *m;
703         struct ifnet *ifp;
704         int lenthisone;
705         short rx_fifo2, status;
706         short rx_fifo;
707
708 /* XXX Must be called with sc locked */
709
710         ifp = sc->ifp;
711         status = CSR_READ_2(sc, EP_W1_RX_STATUS);
712
713 read_again:
714
715         if (status & ERR_RX) {
716                 ++ifp->if_ierrors;
717                 if (status & ERR_RX_OVERRUN) {
718                         /*
719                          * We can think the rx latency is actually
720                          * greather than we expect
721                          */
722 #ifdef EP_LOCAL_STATS
723                         if (EP_FTST(sc, F_RX_FIRST))
724                                 sc->rx_overrunf++;
725                         else
726                                 sc->rx_overrunl++;
727 #endif
728                 }
729                 goto out;
730         }
731         rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
732
733         if (EP_FTST(sc, F_RX_FIRST)) {
734                 MGETHDR(m, M_DONTWAIT, MT_DATA);
735                 if (!m)
736                         goto out;
737                 if (rx_fifo >= MINCLSIZE)
738                         MCLGET(m, M_DONTWAIT);
739                 sc->top = sc->mcur = top = m;
740 #define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
741 #define EOFF    (EROUND - sizeof(struct ether_header))
742                 top->m_data += EOFF;
743
744                 /* Read what should be the header. */
745                 CSR_READ_MULTI_2(sc, EP_W1_RX_PIO_RD_1,
746                     mtod(top, uint16_t *), sizeof(struct ether_header) / 2);
747                 top->m_len = sizeof(struct ether_header);
748                 rx_fifo -= sizeof(struct ether_header);
749                 sc->cur_len = rx_fifo2;
750         } else {
751                 /* come here if we didn't have a complete packet last time */
752                 top = sc->top;
753                 m = sc->mcur;
754                 sc->cur_len += rx_fifo2;
755         }
756
757         /* Reads what is left in the RX FIFO */
758         while (rx_fifo > 0) {
759                 lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
760                 if (lenthisone == 0) {  /* no room in this one */
761                         mcur = m;
762                         MGET(m, M_DONTWAIT, MT_DATA);
763                         if (!m)
764                                 goto out;
765                         if (rx_fifo >= MINCLSIZE)
766                                 MCLGET(m, M_DONTWAIT);
767                         m->m_len = 0;
768                         mcur->m_next = m;
769                         lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
770                 }
771                 if (EP_FTST(sc, F_ACCESS_32_BITS)) {
772                         /* default for EISA configured cards */
773                         CSR_READ_MULTI_4(sc, EP_W1_RX_PIO_RD_1,
774                             (uint32_t *)(mtod(m, caddr_t)+m->m_len),
775                             lenthisone / 4);
776                         m->m_len += (lenthisone & ~3);
777                         if (lenthisone & 3)
778                                 CSR_READ_MULTI_1(sc, EP_W1_RX_PIO_RD_1,
779                                     mtod(m, caddr_t)+m->m_len, lenthisone & 3);
780                         m->m_len += (lenthisone & 3);
781                 } else {
782                         CSR_READ_MULTI_2(sc, EP_W1_RX_PIO_RD_1,
783                             (uint16_t *)(mtod(m, caddr_t)+m->m_len),
784                             lenthisone / 2);
785                         m->m_len += lenthisone;
786                         if (lenthisone & 1)
787                                 *(mtod(m, caddr_t)+m->m_len - 1) =
788                                     CSR_READ_1(sc, EP_W1_RX_PIO_RD_1);
789                 }
790                 rx_fifo -= lenthisone;
791         }
792
793         if (status & ERR_RX_INCOMPLETE) {
794                 /* we haven't received the complete packet */
795                 sc->mcur = m;
796 #ifdef EP_LOCAL_STATS
797                 /* to know how often we come here */
798                 sc->rx_no_first++;
799 #endif
800                 EP_FRST(sc, F_RX_FIRST);
801                 status = CSR_READ_2(sc, EP_W1_RX_STATUS);
802                 if (!status & ERR_RX_INCOMPLETE) {
803                         /*
804                          * We see if by now, the packet has completly
805                          * arrived
806                          */
807                         goto read_again;
808                 }
809                 CSR_WRITE_2(sc, EP_COMMAND,
810                     SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
811                 return;
812         }
813         CSR_WRITE_2(sc, EP_COMMAND, RX_DISCARD_TOP_PACK);
814         ++ifp->if_ipackets;
815         EP_FSET(sc, F_RX_FIRST);
816         top->m_pkthdr.rcvif = sc->ifp;
817         top->m_pkthdr.len = sc->cur_len;
818
819         /*
820          * Drop locks before calling if_input() since it may re-enter
821          * ep_start() in the netisr case.  This would result in a
822          * lock reversal.  Better performance might be obtained by
823          * chaining all packets received, dropping the lock, and then
824          * calling if_input() on each one.
825          */
826         EP_UNLOCK(sc);
827         (*ifp->if_input) (ifp, top);
828         EP_LOCK(sc);
829         sc->top = 0;
830         EP_BUSY_WAIT(sc);
831         CSR_WRITE_2(sc, EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
832         return;
833
834 out:
835         CSR_WRITE_2(sc, EP_COMMAND, RX_DISCARD_TOP_PACK);
836         if (sc->top) {
837                 m_freem(sc->top);
838                 sc->top = 0;
839 #ifdef EP_LOCAL_STATS
840                 sc->rx_no_mbuf++;
841 #endif
842         }
843         EP_FSET(sc, F_RX_FIRST);
844         EP_BUSY_WAIT(sc);
845         CSR_WRITE_2(sc, EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
846 }
847
848 static int
849 ep_ifmedia_upd(struct ifnet *ifp)
850 {
851         struct ep_softc *sc = ifp->if_softc;
852         int i = 0, j;
853
854         GO_WINDOW(sc, 0);
855         CSR_WRITE_2(sc, EP_COMMAND, STOP_TRANSCEIVER);
856         GO_WINDOW(sc, 4);
857         CSR_WRITE_2(sc, EP_W4_MEDIA_TYPE, DISABLE_UTP);
858         GO_WINDOW(sc, 0);
859
860         switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) {
861         case IFM_10_T:
862                 if (sc->ep_connectors & UTP) {
863                         i = ACF_CONNECTOR_UTP;
864                         GO_WINDOW(sc, 4);
865                         CSR_WRITE_2(sc, EP_W4_MEDIA_TYPE, ENABLE_UTP);
866                 }
867                 break;
868         case IFM_10_2:
869                 if (sc->ep_connectors & BNC) {
870                         i = ACF_CONNECTOR_BNC;
871                         CSR_WRITE_2(sc, EP_COMMAND, START_TRANSCEIVER);
872                         DELAY(DELAY_MULTIPLE * 1000);
873                 }
874                 break;
875         case IFM_10_5:
876                 if (sc->ep_connectors & AUI)
877                         i = ACF_CONNECTOR_AUI;
878                 break;
879         default:
880                 i = sc->ep_connector;
881                 device_printf(sc->dev,
882                     "strange connector type in EEPROM: assuming AUI\n");
883         }
884
885         GO_WINDOW(sc, 0);
886         j = CSR_READ_2(sc, EP_W0_ADDRESS_CFG) & 0x3fff;
887         CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
888
889         return (0);
890 }
891
892 static void
893 ep_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
894 {
895         struct ep_softc *sc = ifp->if_softc;
896
897         ifmr->ifm_active = sc->ifmedia.ifm_media;
898 }
899
900 static int
901 epioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
902 {
903         struct ep_softc *sc = ifp->if_softc;
904         struct ifreq *ifr = (struct ifreq *) data;
905         int error = 0;
906
907         switch (cmd) {
908         case SIOCSIFFLAGS:
909                 EP_LOCK(sc);
910                 if (((ifp->if_flags & IFF_UP) == 0) &&
911                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
912                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
913                         epstop(sc);
914                 } else
915                         /* reinitialize card on any parameter change */
916                         epinit_locked(sc);
917                 EP_UNLOCK(sc);
918                 break;
919 #ifdef notdef
920         case SIOCGHWADDR:
921                 bcopy((caddr_t)sc->sc_addr, (caddr_t)&ifr->ifr_data,
922                     sizeof(sc->sc_addr));
923                 break;
924 #endif
925         case SIOCADDMULTI:
926         case SIOCDELMULTI:
927                 /*
928                  * The Etherlink III has no programmable multicast
929                  * filter.  We always initialize the card to be
930                  * promiscuous to multicast, since we're always a
931                  * member of the ALL-SYSTEMS group, so there's no
932                  * need to process SIOC*MULTI requests.
933                  */
934                 error = 0;
935                 break;
936         case SIOCSIFMEDIA:
937         case SIOCGIFMEDIA:
938                 if (!sc->epb.mii_trans)
939                         error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
940                 else
941                         error = EINVAL;
942                 break;
943         default:
944                 error = ether_ioctl(ifp, cmd, data);
945                 break;
946         }
947         return (error);
948 }
949
950 static void
951 epwatchdog(struct ifnet *ifp)
952 {
953         struct ep_softc *sc = ifp->if_softc;
954
955         if (sc->gone)
956                 return;
957         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
958         epstart(ifp);
959         ep_intr(ifp->if_softc);
960 }
961
962 static void
963 epstop(struct ep_softc *sc)
964 {
965         CSR_WRITE_2(sc, EP_COMMAND, RX_DISABLE);
966         CSR_WRITE_2(sc, EP_COMMAND, RX_DISCARD_TOP_PACK);
967         EP_BUSY_WAIT(sc);
968
969         CSR_WRITE_2(sc, EP_COMMAND, TX_DISABLE);
970         CSR_WRITE_2(sc, EP_COMMAND, STOP_TRANSCEIVER);
971         DELAY(800);
972
973         CSR_WRITE_2(sc, EP_COMMAND, RX_RESET);
974         EP_BUSY_WAIT(sc);
975         CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
976         EP_BUSY_WAIT(sc);
977
978         CSR_WRITE_2(sc, EP_COMMAND, C_INTR_LATCH);
979         CSR_WRITE_2(sc, EP_COMMAND, SET_RD_0_MASK);
980         CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK);
981         CSR_WRITE_2(sc, EP_COMMAND, SET_RX_FILTER);
982 }