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