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