if_ep.c revision 5195
1/*
2 * Copyright (c) 1993 Herb Peyerl <hpeyerl@novatel.ca> All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer. 2. The name
8 * of the author may not be used to endorse or promote products derived from
9 * this software without specific prior written permission
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
14 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
15 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
16 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
17 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
18 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 *
22 * From: if_ep.c,v 1.9 1994/01/25 10:46:29 deraadt Exp $ $Id: if_ep.c,v 1.9
23 * 1994/05/02 22:27:33 ats Exp $
24 *
25 * October 26, 1994
26 *
27 * Modified by: Andres Vega Garcia
28 * INRIA - Sophia Antipolis, France
29 * e-mail: avega@sophia.inria.fr
30 * finger: avega@pax.inria.fr
31 *
32 *
33 * What is new:
34 *
35 *   1) We can recognize more than 1 board.
36 *
37 *   2) The problem which used to happen  with high trafic is corrected,
38 *      (No more need to 'down' and 'up' the interface).
39 *
40 *   3) In the transmission, we use the TX start threshold in a more dynamic
41 *      fashion (IMO the throughput is higher this way).
42 *
43 *   4) In the reception, we use the RX early threshold, that parameter is
44 *      adapted as the packets arrive (IMO the throughput is higher this way).
45 *
46 *   5) Supports EISA cards.
47 *
48 *   NB 0: The 32 bits acces is allowed for the EISA configured cards, thoung I
49 *   wasn't able to test the code added.
50 *
51 *   NB 1: I added the option EP_LOCAL_STATS, it can be temporary as IMO is just
52 *   used while working on this driver and the program that displays this
53 *   information (epstat).
54 *
55 *
56 * Some driver statistics can be viewed with the epstat utility.  In order to
57 * use this, you have to compile if_ep.c with
58 *
59 * 	-DEP_LOCAL_STATS
60 *
61 * which can be included in your machine config file (e.g. GENERICAH_EP)
62 * as an option (option EP_LOCAL_STATS).
63 *
64 *
65 * Modifications since FreeBSD 1.1.5.1 Release:
66 *
67 * This explanation concerns the epstart(), epread() and epintr() functions.
68 *
69 * =========================================================================
70 * 	epstart()
71 * =========================================================================
72 *
73 *
74 * 	Let's see what the idea is:
75 *
76 *
77 * Packet |------------------ LEN ---------------------|
78 *
79 * 	       A
80 * CPU	  |---------------|----------------------------|
81 *
82 *
83 * Card	                  |----------------------------|
84 *
85 *
86 *
87 * 	We suppose the Card is able to *write* bytes (send them to the media)
88 * at the speed S_CARD (bytes/s), and that is faster than the speed of the CPU,
89 * S_CPU, to write bytes to the TX FIFO, then, we have to write A bytes to the
90 * FIFO before enableing the transmision in the card. This way both, the card
91 * and the CPU must finish their writing at the same time.
92 *
93 *
94 * 	Let TX_RATE = S_CPU / S_CARD,  where TX_RATE <= 1
95 *
96 * 	We can find that:
97 *
98 * (1)		A = LEN * (1 - TX_RATE)
99 *
100 *
101 * 	Let TX_RATE_R be the *very real* value.
102 *
103 * 	If TX_RATE > TX_RATE_R
104 * 		We are supposing the CPU is faster than it really is and
105 * 		certainly the card will *finish* before the CPU, having a
106 * 		TX Underrun Error, then, in such a case, we have to do:
107 *
108 * 			TX_RATE -= STEP, where STEP is the step at which we
109 * 					 move TX_RATE
110 *
111 * 	If TX_RATE < TX_RATE_R
112 * 		We won't have the TX Underrun Error but it is possible that
113 * 		we don't use eficiently the TX START THRESH. feature.
114 * 		We prevent this by doing:
115 *
116 * 			TX_RATE += STEP every time we have sent succesfuly
117 * 					(without Underrun) a certain number
118 * 					of packets.
119 *
120 * 	Now, to avoid dealing with reals I used a FACTOR, then (1) will be
121 * transformed:
122 *
123 * 		Let tx_rate = FACTOR * TX_RATE, tx_rate is the parameter
124 * 						really used.
125 *
126 * 		A = (LEN * (FACTOR - tx_rate)) / FACTOR
127 *
128 * 	Actually FACTOR = 64
129 *
130 * (2)		A = (LEN * (64 - tx_rate)) >> 6
131 *
132 * 	As I want to have some margin, and
133 * 	as I have to write a number multiple of 4:
134 *
135 * 	A = tx_start_threshold = (((LEN * (64 - tx_rate)) >> 6) & ~3) + 16
136 *
137 *
138 * =========================================================================
139 * 	epread()
140 * =========================================================================
141 *
142 * 	I mantain an estimation of the RX packet's average length, and an
143 * estimation of the RX latency.
144 *
145 * 	Every time I receive a complete packet I compute the average packet's
146 * length, rx_avg_pkt:
147 *
148 * 	DELTA = LEN - rx_avg_pkt, where LEN is this packet's length
149 *
150 * 	if DELTA > 0
151 * 		rx_avg_pkt += AVG_UP * DELTA
152 * 	else
153 * 		rx_avg_pkt += AVG_DOWN *DELTA
154 *
155 *
156 * 	AVG_UP < AVG_DOWN
157 *
158 * 	In the first case, I'm interested in being conservative about the
159 * average packet'length, because if I let it go up *too fast*, a shorter packet
160 * than expected will probably cause an RX Overrun Error. But if I consider
161 * the next packet will be smaller than it will, I just will have to wait
162 * for the packet to complete reception.
163 *
164 * 	In the other case, I'm interested in leting the rx_avg_pkt follow
165 * the real packet's lenght closer, as it is important not to think the average
166 * packet is bigger than it realy is. If I don't do that, and the rx_avg_pkt
167 * goes down *too slow*, I'll find myself thinking the packets are big when they
168 * are really small and I'll have probably Rx Overrun Errors.
169 *
170 * 	Actualy:
171 *
172 * 		AVG_UP =   1/32
173 * 		AVG_DOWN = 1/8
174 *
175 *
176 * 	Every time I receive an incomplete packet I recompute the RX latency
177 * (rx_latency).
178 *
179 * 	I know that if rx_latency = 0, when I go read the bytes from the RX
180 * FIFO, I'll find as many bytes as I programmed in the RX Early Threshold, but
181 * if rx_latency > 0, I'll find more bytes.
182 *
183 * 	Let CUR_LAT be the RX latency seen by this packet.
184 *
185 * 	CUR_LAT = LEN - rx_early_threshold,	where LEN is the number of
186 * 						bytes I have just received.
187 *
188 * 	DELTA = CUR_LAT - rx_latency
189 *
190 *
191 * 	if DELTA >= 0
192 * 		rx_latency += LAT_UP * DELTA
193 * 	else
194 * 		rx_latency += LAT_DOWN * DELTA
195 *
196 *
197 * 	LAT_UP > LAT_DOWN
198 *
199 * 	In a similar way as for rx average packet's length, I try to be more
200 * conservative in the more critical case.
201 *
202 * 	In the first case, I have to follow closer the incremets of the RX
203 * latency, because if I don't, I can find myself thinking that we (CPU) are
204 * *enough fast* and wait up to the last minute to go read data to find we have
205 * had RX Overrun Error.
206 *
207 * 	In the other case I must be more conservative to avoid falling in
208 * the situation I have just described, because if I go down *to fast* I'll
209 * think we are enough fast and we'll wake up later than due.
210 *
211 * 	Actually:
212 *
213 * 		LAT_UP =   1/4
214 * 		LAT_DOWN = 1/32
215 *
216 * 	Finally, I compute the rx_early_threshold for the next packet as:
217 *
218 * 	rx_early_threshold = rx_avg_pkt - rx_latency
219 *
220 * 	But, as I want to have a margin and
221 * 	as I have to write a value multiple of 4.
222 *
223 *
224 * 	rx_early_threshold = (rx_avg_pkt - rx_latency - 16) & ~3
225 *
226 *
227 * 	But if I have to wait for the rest of an incomplete packet
228 * from which I have already received CUR_LEN bytes:
229 *
230 *
231 * 	rx_early_threshold = (rx_avg_pkt-CUR_LEN - rx_latency - 16) & ~3
232 *
233 *
234 * =========================================================================
235 * 	epintr()
236 * =========================================================================
237 *
238 * 	For this function I just tryed to do what is stated in the
239 * Etherlink III Technical Reference.
240 *
241 * 	It was here where I really solved the problem that used to happen with
242 * high traffic.
243 *
244 *
245 * 				Andres
246 * 				avega@pax.inria.fr
247 */
248
249#include "ep.h"
250#if NEP > 0
251
252#include "bpfilter.h"
253
254#include <sys/param.h>
255#if defined(__FreeBSD__)
256#include <sys/systm.h>
257#include <sys/kernel.h>
258#include <sys/devconf.h>
259#endif
260#include <sys/mbuf.h>
261#include <sys/socket.h>
262#include <sys/ioctl.h>
263#include <sys/errno.h>
264#include <sys/syslog.h>
265#if defined(__NetBSD__)
266#include <sys/select.h>
267#endif
268
269#include <net/if.h>
270#include <net/if_dl.h>
271#include <net/if_types.h>
272
273#ifdef INET
274#include <netinet/in.h>
275#include <netinet/in_systm.h>
276#include <netinet/in_var.h>
277#include <netinet/ip.h>
278#include <netinet/if_ether.h>
279#endif
280
281#ifdef NS
282#include <netns/ns.h>
283#include <netns/ns_if.h>
284#endif
285
286#if NBPFILTER > 0
287#include <net/bpf.h>
288#include <net/bpfdesc.h>
289#endif
290
291#include <i386/isa/isa.h>
292#include <i386/isa/isa_device.h>
293#include <i386/isa/icu.h>
294#include <i386/isa/if_epreg.h>
295
296static int epprobe __P((struct isa_device *));
297static int epattach __P((struct isa_device *));
298static int epioctl __P((struct ifnet * ifp, int, caddr_t));
299static void epmbuffill __P((caddr_t, int));
300static void epmbufempty __P((struct ep_softc *));
301
302void epinit __P((int));
303void epintr __P((int));
304void epread __P((struct ep_softc *));
305void epreset __P((int));
306void epstart __P((struct ifnet *));
307void epstop __P((int));
308void epwatchdog __P((int));
309
310static int send_ID_sequence __P((int));
311static int get_eeprom_data __P((int, int));
312
313struct ep_softc ep_softc[NEP];
314
315#define ep_ftst(f) (sc->stat&(f))
316#define ep_fset(f) (sc->stat|=(f))
317#define ep_frst(f) (sc->stat&=~(f))
318
319struct isa_driver epdriver = {
320    epprobe,
321    epattach,
322    "ep"
323};
324
325static struct kern_devconf kdc_ep[NEP] = { {
326      0, 0, 0,                /* filled in by dev_attach */
327      "ep", 0, { MDDT_ISA, 0, "net" },
328      isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
329      &kdc_isa0,              /* parent */
330      0,                      /* parentdata */
331      DC_BUSY,                /* network interfaces are always ``open'' */
332      "3Com 3C509 Ethernet adapter"
333} };
334
335static inline void
336ep_registerdev(struct isa_device *id)
337{
338      if(id->id_unit)
339              kdc_ep[id->id_unit] = kdc_ep[0];
340      kdc_ep[id->id_unit].kdc_unit = id->id_unit;
341      kdc_ep[id->id_unit].kdc_parentdata = id;
342      dev_attach(&kdc_ep[id->id_unit]);
343}
344
345int ep_current_tag = EP_LAST_TAG + 1;
346
347int ep_board[EP_MAX_BOARDS + 1];
348
349static int
350eeprom_rdy(is)
351    struct isa_device *is;
352{
353    int i;
354
355    for (i = 0; is_eeprom_busy(IS_BASE) && i < MAX_EEPROMBUSY; i++);
356    if (i >= MAX_EEPROMBUSY) {
357	printf("ep%d: eeprom failed to come ready.\n", is->id_unit);
358	return (0);
359    }
360    return (1);
361}
362
363static int
364ep_look_for_board_at(is)
365    struct isa_device *is;
366{
367    int data, i, j, io_base, id_port = EP_ID_PORT;
368    int nisa = 0, neisa = 0;
369
370    if (ep_current_tag == (EP_LAST_TAG + 1)) {
371	/* Come here just one time */
372
373	/* Look for the EISA boards, leave them activated */
374	for(j = 1; j < 16; j++) {
375	    io_base = (j * EP_EISA_START) | EP_EISA_W0;
376	    if (inw(io_base + EP_W0_MFG_ID) != MFG_ID)
377		continue;
378
379	    /* we must found 0x1f if the board is EISA configurated */
380	    if ((inw(io_base + EP_W0_ADDRESS_CFG) & 0x1f) != 0x1f)
381		continue;
382
383	    /* Reset and Enable the card */
384	    outb(io_base + EP_W0_CONFIG_CTRL, W0_P4_CMD_RESET_ADAPTER);
385	    DELAY(1000); /* we must wait at least 1 ms */
386	    outb(io_base + EP_W0_CONFIG_CTRL, W0_P4_CMD_ENABLE_ADAPTER);
387
388	    /*
389	     * Once activated, all the registers are mapped in the range
390	     * x000 - x00F, where x is the slot number.
391             */
392	    ep_board[neisa++] = j * EP_EISA_START;
393	}
394	ep_current_tag--;
395
396        /* Look for the ISA boards. Init and leave them actived */
397	outb(id_port, 0xc0);	/* Global reset */
398	DELAY(1000);
399	for (i = 0; i < EP_MAX_BOARDS; i++) {
400	    outb(id_port, 0);
401	    outb(id_port, 0);
402	    send_ID_sequence(id_port);
403
404	    data = get_eeprom_data(id_port, EEPROM_MFG_ID);
405	    if (data != MFG_ID)
406		break;
407
408	    /* resolve contention using the Ethernet address */
409	    for (j = 0; j < 3; j++)
410		data = get_eeprom_data(id_port, j);
411
412	    ep_board[neisa+nisa++] =
413		(get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x10 + 0x200;
414	    outb(id_port, ep_current_tag);	/* tags board */
415	    outb(id_port, ACTIVATE_ADAPTER_TO_CONFIG);
416	    ep_current_tag--;
417	}
418
419	ep_board[neisa+nisa] = 0;
420	if (neisa) {
421	    printf("%d 3C5x9 board(s) on EISA found at", neisa);
422	    for (j = 0; ep_board[j]; j++)
423		if (ep_board[j] >= EP_EISA_START)
424		    printf(" 0x%x", ep_board[j]);
425	    printf("\n");
426	}
427	if (nisa) {
428	    printf("%d 3C5x9 board(s) on ISA found at", nisa);
429	    for (j = 0; ep_board[j]; j++)
430		if (ep_board[j] < EP_EISA_START)
431		    printf(" 0x%x", ep_board[j]);
432	    printf("\n");
433	}
434    }
435
436    for (i = 0; ep_board[i] && ep_board[i] != IS_BASE; i++);
437    if (ep_board[i] == IS_BASE) {
438	if (inw(IS_BASE + EP_W0_EEPROM_COMMAND) & EEPROM_TST_MODE)
439	    printf("ep%d: 3c5x9 at 0x%x in test mode. Erase pencil mark!\n",
440		   is->id_unit, IS_BASE);
441	return (1);
442    }
443    return (0);
444}
445
446/*
447 * get_e: gets a 16 bits word from the EEPROM. we must have set the window
448 * before
449 */
450static int
451get_e(is, offset)
452    struct isa_device *is;
453    int offset;
454{
455    if (!eeprom_rdy(is))
456	return (0xffff);
457    outw(IS_BASE + EP_W0_EEPROM_COMMAND, EEPROM_CMD_RD | offset);
458    if (!eeprom_rdy(is))
459	return (0xffff);
460    return (inw(IS_BASE + EP_W0_EEPROM_DATA));
461}
462
463int
464epprobe(is)
465    struct isa_device *is;
466{
467    struct ep_softc *sc = &ep_softc[is->id_unit];
468    u_short k;
469    int i;
470
471    if (!ep_look_for_board_at(is))
472	return (0);
473    /*
474     * The iobase was found and MFG_ID was 0x6d50. PROD_ID should be
475     * 0x9[0-f]50
476     */
477    GO_WINDOW(0);
478    k = get_e(is, EEPROM_PROD_ID);
479    if ((k & 0xf0ff) != (PROD_ID & 0xf0ff)) {
480	printf("epprobe: ignoring model %04x\n", k);
481	return (0);
482    }
483
484    k = get_e(is, EEPROM_RESOURCE_CFG);
485    k >>= 12;
486    if (is->id_irq != (1 << ((k == 2) ? 9 : k))) {
487	printf("epprobe: interrupt number %d doesn't match\n",is->id_irq);
488	return (0);
489    }
490
491    if (BASE >= EP_EISA_START) /* we have an EISA board, we allow 32 bits access */
492	sc->stat = F_ACCESS_32_BITS;
493    else
494	sc->stat = 0;
495
496    /* By now, the adapter is already activated */
497
498    return (0x10);		/* 16 bytes of I/O space used. */
499}
500
501static char *ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
502
503static int
504epattach(is)
505    struct isa_device *is;
506{
507    struct ep_softc *sc = &ep_softc[is->id_unit];
508    struct ifnet *ifp = &sc->arpcom.ac_if;
509    u_short i, j, *p;
510    struct ifaddr *ifa;
511    struct sockaddr_dl *sdl;
512
513    /* BASE = IS_BASE; */
514    sc->ep_io_addr = is->id_iobase;
515
516    printf("ep%d: ", is->id_unit);
517
518    sc->ep_connectors = 0;
519    i = inw(IS_BASE + EP_W0_CONFIG_CTRL);
520    j = inw(IS_BASE + EP_W0_ADDRESS_CFG) >> 14;
521    if (i & IS_AUI) {
522	printf("aui");
523	sc->ep_connectors |= AUI;
524    }
525    if (i & IS_BNC) {
526	if (sc->ep_connectors)
527	    printf("/");
528	printf("bnc");
529	sc->ep_connectors |= BNC;
530    }
531    if (i & IS_UTP) {
532	if (sc->ep_connectors)
533	    printf("/");
534	printf("utp");
535	sc->ep_connectors |= UTP;
536    }
537    if (!(sc->ep_connectors & 7))
538	printf("no connectors!");
539    else
540	printf("[*%s*]", ep_conn_type[j]);
541
542    /*
543     * Read the station address from the eeprom
544     */
545    p = (u_short *) & sc->arpcom.ac_enaddr;
546    for (i = 0; i < 3; i++) {
547	GO_WINDOW(0);
548	p[i] = htons(get_e(is, i));
549	GO_WINDOW(2);
550	outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
551    }
552    printf(" address %s\n", ether_sprintf(sc->arpcom.ac_enaddr));
553
554    ifp->if_unit = is->id_unit;
555    ifp->if_name = "ep";
556    ifp->if_mtu = ETHERMTU;
557    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
558    ifp->if_init = epinit;
559    ifp->if_output = ether_output;
560    ifp->if_start = epstart;
561    ifp->if_ioctl = epioctl;
562    ifp->if_watchdog = epwatchdog;
563
564    if_attach(ifp);
565    ep_registerdev(is);
566
567    /*
568     * Fill the hardware address into ifa_addr if we find an AF_LINK entry.
569     * We need to do this so bpf's can get the hardware addr of this card.
570     * netstat likes this too!
571     */
572    ifa = ifp->if_addrlist;
573    while ((ifa != 0) && (ifa->ifa_addr != 0) &&
574	   (ifa->ifa_addr->sa_family != AF_LINK))
575	ifa = ifa->ifa_next;
576
577    if ((ifa != 0) && (ifa->ifa_addr != 0)) {
578	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
579	sdl->sdl_type = IFT_ETHER;
580	sdl->sdl_alen = ETHER_ADDR_LEN;
581	sdl->sdl_slen = 0;
582	bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
583    }
584    /* we give some initial parameters */
585    sc->rx_avg_pkt = 128;
586
587    /*
588     * NOTE: In all this I multiply everything by 64.
589     * W_s = the speed the CPU is able to write to the TX FIFO.
590     * T_s = the speed the board sends the info to the Ether.
591     * W_s/T_s = 16   (represents 16/64) =>    W_s = 25 % of T_s.
592     * This will give us for a packet of 1500 bytes
593     * tx_start_thresh=1125 and for a pkt of 64 bytes tx_start_threshold=48.
594     * We prefer to start thinking the CPU is much slower than the Ethernet
595     * transmission.
596     */
597    sc->tx_rate = TX_INIT_RATE;
598    sc->tx_counter = 0;
599    sc->rx_latency = RX_INIT_LATENCY;
600    sc->rx_early_thresh = RX_INIT_EARLY_THRESH;
601#ifdef EP_LOCAL_STATS
602    sc->rx_no_first = sc->rx_no_mbuf =
603	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
604	sc->tx_underrun = 0;
605#endif
606    ep_fset(F_RX_FIRST);
607    sc->top = sc->mcur = 0;
608
609#if NBPFILTER > 0
610    bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
611#endif
612    return 1;
613}
614
615
616/*
617 * The order in here seems important. Otherwise we may not receive
618 * interrupts. ?!
619 */
620void
621epinit(unit)
622    int unit;
623{
624    register struct ep_softc *sc = &ep_softc[unit];
625    register struct ifnet *ifp = &sc->arpcom.ac_if;
626    int s, i;
627
628    if (ifp->if_addrlist == (struct ifaddr *) 0)
629	return;
630
631    s = splimp();
632    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
633
634    GO_WINDOW(0);
635
636    /* Disable the card */
637    outw(BASE + EP_W0_CONFIG_CTRL, 0);
638
639    /* Enable the card */
640    outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
641
642    GO_WINDOW(2);
643
644    /* Reload the ether_addr. */
645    for (i = 0; i < 6; i++)
646	outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
647
648    outw(BASE + EP_COMMAND, RX_RESET);
649    outw(BASE + EP_COMMAND, TX_RESET);
650
651    /* Window 1 is operating window */
652    GO_WINDOW(1);
653    for (i = 0; i < 31; i++)
654	inb(BASE + EP_W1_TX_STATUS);
655
656    /* get rid of stray intr's */
657    outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
658
659    outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
660
661    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
662
663    outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
664	 FIL_GROUP | FIL_BRDCST);
665
666	/*
667	 * you can `ifconfig ep0 (bnc|aui)' to get the following
668	 * behaviour:
669	 *	bnc	disable AUI/UTP. enable BNC.
670	 *	aui	disable BNC. enable AUI. if the card has a UTP
671	 *		connector, that is enabled too. not sure, but it
672	 * 		seems you have to be careful to not plug things
673	 *		into both AUI & UTP.
674	 */
675#if defined(__NetBSD__)
676    if (!(ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & BNC)) {
677#else
678    if (!(ifp->if_flags & IFF_ALTPHYS) && (sc->ep_connectors & BNC)) {
679#endif
680	outw(BASE + EP_COMMAND, START_TRANSCEIVER);
681	DELAY(1000);
682    }
683#if defined(__NetBSD__)
684    if ((ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & UTP)) {
685#else
686    if ((ifp->if_flags & IFF_ALTPHYS) && (sc->ep_connectors & UTP)) {
687#endif
688	GO_WINDOW(4);
689	outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
690	GO_WINDOW(1);
691    }
692    outw(BASE + EP_COMMAND, RX_ENABLE);
693    outw(BASE + EP_COMMAND, TX_ENABLE);
694
695    ifp->if_flags |= IFF_RUNNING;
696    ifp->if_flags &= ~IFF_OACTIVE;	/* just in case */
697
698    sc->tx_rate = TX_INIT_RATE;
699    sc->tx_counter = 0;
700    sc->rx_latency = RX_INIT_LATENCY;
701    sc->rx_early_thresh = RX_INIT_EARLY_THRESH;
702#ifdef EP_LOCAL_STATS
703    sc->rx_no_first = sc->rx_no_mbuf =
704	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
705	sc->tx_underrun = 0;
706#endif
707    ep_fset(F_RX_FIRST);
708    ep_frst(F_RX_TRAILER);
709    if (sc->top) {
710	m_freem(sc->top);
711	sc->top = sc->mcur = 0;
712    }
713    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | sc->rx_early_thresh);
714
715    /*
716     * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
717     * any that we had in case we're being called from intr or somewhere
718     * else.
719     */
720    sc->last_mb = 0;
721    sc->next_mb = 0;
722    epmbuffill((caddr_t) sc, 0);
723
724    epstart(ifp);
725
726    splx(s);
727}
728
729static const char padmap[] = {0, 3, 2, 1};
730
731void
732epstart(ifp)
733    struct ifnet *ifp;
734{
735    register struct ep_softc *sc = &ep_softc[ifp->if_unit];
736    register u_int len;
737    register struct mbuf *m;
738    struct mbuf *top;
739    int s, pad;
740
741    s = splimp();
742    if (sc->arpcom.ac_if.if_flags & IFF_OACTIVE) {
743	splx(s);
744	return;
745    }
746startagain:
747    /* Sneak a peek at the next packet */
748    m = sc->arpcom.ac_if.if_snd.ifq_head;
749    if (m == 0) {
750	splx(s);
751	return;
752    }
753#if 0
754    len = m->m_pkthdr.len;
755#else
756    for (len = 0, top = m; m; m = m->m_next)
757	len += m->m_len;
758#endif
759
760    pad = padmap[len & 3];
761
762    /*
763     * The 3c509 automatically pads short packets to minimum ethernet length,
764     * but we drop packets that are too large. Perhaps we should truncate
765     * them instead?
766     */
767    if (len + pad > ETHER_MAX_LEN) {
768	/* packet is obviously too large: toss it */
769	++sc->arpcom.ac_if.if_oerrors;
770	IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
771	m_freem(m);
772	goto readcheck;
773    }
774    if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
775	/* no room in FIFO */
776	outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
777	sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
778	splx(s);
779	return;
780    }
781    IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
782
783    outw(BASE + EP_W1_TX_PIO_WR_1, len);
784    outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);	/* Second dword meaningless */
785
786    /* compute the Tx start threshold for this packet */
787    sc->tx_start_thresh = len =
788	(((len * (64 - sc->tx_rate)) >> 6) & ~3) + 16;
789    outw(BASE + EP_COMMAND, SET_TX_START_THRESH | len);
790
791    for (top = m; m != 0; m = m->m_next)
792	if(ep_ftst(F_ACCESS_32_BITS)) {
793	    outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
794		  m->m_len / 4);
795	    if (m->m_len & 3)
796		outsb(BASE + EP_W1_TX_PIO_WR_1,
797		      mtod(m, caddr_t) + m->m_len / 4,
798		      m->m_len & 3);
799	} else {
800	    outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
801	    if (m->m_len & 1)
802		outb(BASE + EP_W1_TX_PIO_WR_1,
803		     *(mtod(m, caddr_t) + m->m_len - 1));
804	}
805
806    while (pad--)
807	outb(BASE + EP_W1_TX_PIO_WR_1, 0);	/* Padding */
808
809#if NBPFILTER > 0
810    if (sc->bpf) {
811	bpf_mtap(sc->bpf, top);
812    }
813#endif
814
815    sc->arpcom.ac_if.if_opackets++;
816    m_freem(top);
817    /*
818     * Every 1024*4 packets we increment the tx_rate if we haven't had
819     * errors, that in the case it has abnormaly goten too low
820     */
821    if (!(++sc->tx_counter & (1024 * 4 - 1)) &&
822	sc->tx_rate < TX_INIT_MAX_RATE)
823	sc->tx_rate++;
824
825    /*
826     * Is another packet coming in? We don't want to overflow the tiny RX
827     * fifo.
828     */
829readcheck:
830    if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
831	/*
832	 * we check if we have packets left, in that case we prepare to come
833	 * back later
834	 */
835	if (sc->arpcom.ac_if.if_snd.ifq_head) {
836	    outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH |
837		 sc->tx_start_thresh);
838	}
839	splx(s);
840	return;
841    }
842    goto startagain;
843}
844
845void
846epintr(unit)
847    int unit;
848{
849    int i;
850    register int status;
851    register struct ep_softc *sc = &ep_softc[unit];
852    struct ifnet *ifp = &sc->arpcom.ac_if;
853    struct mbuf *m;
854
855    outw(BASE + EP_COMMAND, SET_INTR_MASK);	/* disable all Ints */
856    outw(BASE + EP_COMMAND, C_INTR_LATCH);	/* ACK int Latch */
857
858    while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
859	if (status & (S_RX_COMPLETE | S_RX_EARLY)) {
860	    /* we just need ACK for RX_EARLY */
861	    if (status & S_RX_EARLY)
862		outw(BASE + EP_COMMAND, C_RX_EARLY);
863	    epread(sc);
864	    continue;
865	}
866	if (status & S_TX_AVAIL) {
867	    /* we need ACK */
868	    outw(BASE + EP_COMMAND, C_TX_AVAIL);
869	    sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
870	    epstart(&sc->arpcom.ac_if);
871	}
872	if (status & S_CARD_FAILURE) {
873#ifdef EP_LOCAL_STATS
874	    printf("\nep%d:\n\tStatus: %x\n", unit, status);
875	    GO_WINDOW(4);
876	    printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
877	    printf("\tStat: %x\n", sc->stat);
878	    printf("\tIpackets=%d, Opackets=%d\n",
879		sc->arpcom.ac_if.if_ipackets, sc->arpcom.ac_if.if_opackets);
880	    printf("\tNOF=%d, NOMB=%d, BPFD=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
881		   sc->rx_no_first, sc->rx_no_mbuf, sc->rx_bpf_disc, sc->rx_overrunf,
882		   sc->rx_overrunl, sc->tx_underrun);
883#else
884	    printf("ep%d: Status: %x\n", unit, status);
885#endif
886	    epinit(unit);
887	    return;
888	}
889	if (status & S_TX_COMPLETE) {
890	    /* we  need ACK. we do it at the end */
891	    /*
892	     * We need to read TX_STATUS until we get a 0 status in order to
893	     * turn off the interrupt flag.
894	     */
895	    while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
896		if (status & TXS_SUCCES_INTR_REQ);
897		else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
898		    outw(BASE + EP_COMMAND, TX_RESET);
899		    if (status & TXS_UNDERRUN) {
900			if (sc->tx_rate > 1) {
901			    sc->tx_rate--;	/* Actually in steps of 1/64 */
902			    sc->tx_counter = 0;	/* We reset it */
903			}
904#ifdef EP_LOCAL_STATS
905			sc->tx_underrun++;
906#endif
907		    } else {
908			if (status & TXS_JABBER);
909			else	/* TXS_MAX_COLLISION - we shouldn't get here */
910			    ++sc->arpcom.ac_if.if_collisions;
911		    }
912		    ++sc->arpcom.ac_if.if_oerrors;
913		    outw(BASE + EP_COMMAND, TX_ENABLE);
914		    /*
915		     * To have a tx_avail_int but giving the chance to the
916		     * Reception
917		     */
918		    if (sc->arpcom.ac_if.if_snd.ifq_head) {
919			outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
920		    }
921		}
922		outb(BASE + EP_W1_TX_STATUS, 0x0);	/* pops up the next
923							 * status */
924	    }			/* while */
925	}			/* end TX_COMPLETE */
926    }
927    /* re-enable ints */
928    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
929}
930
931void
932epread(sc)
933    register struct ep_softc *sc;
934{
935    struct ether_header *eh;
936    struct mbuf *top, *mcur, *m;
937    int lenthisone;
938
939    short rx_fifo2, status;
940    register short delta;
941    register short rx_fifo;
942
943    status = inw(BASE + EP_W1_RX_STATUS);
944
945read_again:
946
947    if (status & ERR_RX) {
948	++sc->arpcom.ac_if.if_ierrors;
949	if (status & ERR_RX_OVERRUN) {
950	    /*
951	     * we can think the rx latency is actually greather than we
952	     * expect
953	     */
954#ifdef EP_LOCAL_STATS
955	    if (ep_ftst(F_RX_FIRST))
956		sc->rx_overrunf++;
957	    else
958		sc->rx_overrunl++;
959#endif
960	    if (sc->rx_latency < ETHERMTU)
961		sc->rx_latency += 16;
962	}
963	goto out;
964    }
965    rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
966
967    if (ep_ftst(F_RX_FIRST)) {
968	if (m = sc->mb[sc->next_mb]) {
969	    sc->mb[sc->next_mb] = 0;
970	    sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
971	    m->m_data = m->m_pktdat;
972	    m->m_flags = M_PKTHDR;
973	} else {
974	    MGETHDR(m, M_DONTWAIT, MT_DATA);
975	    if (!m)
976		goto out;
977	}
978	sc->top = sc->mcur = top = m;
979#define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
980#define EOFF    (EROUND - sizeof(struct ether_header))
981	top->m_data += EOFF;
982
983	/* Read what should be the header. */
984	insw(BASE + EP_W1_RX_PIO_RD_1,
985	     mtod(top, caddr_t), sizeof(struct ether_header) / 2);
986	top->m_len = sizeof(struct ether_header);
987	rx_fifo -= sizeof(struct ether_header);
988	sc->cur_len = rx_fifo2;
989    } else {
990	/* come here if we didn't have a complete packet last time */
991	top = sc->top;
992	m = sc->mcur;
993	sc->cur_len += rx_fifo2;
994	if (ep_ftst(F_RX_TRAILER))
995	    /* We don't read the trailer */
996	    rx_fifo -= sizeof(struct ether_header);
997    }
998
999    /* Reads what is left in the RX FIFO */
1000    while (rx_fifo > 0) {
1001	lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
1002	if (lenthisone == 0) {	/* no room in this one */
1003	    mcur = m;
1004	    if (m = sc->mb[sc->next_mb]) {
1005		sc->mb[sc->next_mb] = 0;
1006		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1007	    } else {
1008		MGET(m, M_DONTWAIT, MT_DATA);
1009		if (!m)
1010		    goto out;
1011	    }
1012
1013	    if (rx_fifo >= MINCLSIZE)
1014		MCLGET(m, M_DONTWAIT);
1015	    m->m_len = 0;
1016	    mcur->m_next = m;
1017	    lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
1018	}
1019	if (ep_ftst(F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
1020	    insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
1021		 lenthisone / 4);
1022	    m->m_len += (lenthisone & ~3);
1023	    if (lenthisone & 3)
1024		insb(BASE + EP_W1_RX_PIO_RD_1,
1025		     mtod(m, caddr_t) + m->m_len,
1026		     lenthisone & 3);
1027	    m->m_len += (lenthisone & 3);
1028	} else {
1029	    insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
1030		 lenthisone / 2);
1031	    m->m_len += lenthisone;
1032	    if (lenthisone & 1)
1033		*(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
1034	}
1035	rx_fifo -= lenthisone;
1036    }
1037
1038    if (ep_ftst(F_RX_TRAILER)) {/* reads the trailer */
1039	if (m = sc->mb[sc->next_mb]) {
1040	    sc->mb[sc->next_mb] = 0;
1041	    sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1042	    m->m_data = m->m_pktdat;
1043	    m->m_flags = M_PKTHDR;
1044	} else {
1045	    MGETHDR(m, M_DONTWAIT, MT_DATA);
1046	    if (!m)
1047		goto out;
1048	}
1049	insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t),
1050	     sizeof(struct ether_header));
1051	m->m_len = sizeof(struct ether_header);
1052	m->m_next = top;
1053	sc->top = top = m;
1054	/* XXX Accomodate for type and len from beginning of trailer */
1055	sc->cur_len -= (2 * sizeof(u_short));
1056	ep_frst(F_RX_TRAILER);
1057	goto all_pkt;
1058    }
1059
1060    if (status & ERR_RX_INCOMPLETE) {	/* we haven't received the complete
1061					 * packet */
1062	sc->mcur = m;
1063#ifdef EP_LOCAL_STATS
1064	sc->rx_no_first++;	/* to know how often we come here */
1065#endif
1066	/*
1067	 * Re-compute rx_latency, the factor used is 1/4 to go up and 1/32 to
1068	 * go down
1069	 */
1070	delta = rx_fifo2 - sc->rx_early_thresh;	/* last latency seen LLS */
1071	delta -= sc->rx_latency;/* LLS - estimated_latency */
1072	if (delta >= 0)
1073	    sc->rx_latency += (delta / 4);
1074	else
1075	    sc->rx_latency += (delta / 32);
1076	ep_frst(F_RX_FIRST);
1077	if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
1078	    /* we see if by now, the packet has completly arrived */
1079	    goto read_again;
1080	}
1081	/* compute rx_early_threshold */
1082	delta = (sc->rx_avg_pkt - sc->cur_len - sc->rx_latency - 16) & ~3;
1083	if (delta < MIN_RX_EARLY_THRESHL)
1084	    delta = MIN_RX_EARLY_THRESHL;
1085
1086	outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH |
1087	     (sc->rx_early_thresh = delta));
1088	return;
1089    }
1090all_pkt:
1091    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1092    /*
1093     * recompute average packet's length, the factor used is 1/8 to go down
1094     * and 1/32 to go up
1095     */
1096    delta = sc->cur_len - sc->rx_avg_pkt;
1097    if (delta > 0)
1098	sc->rx_avg_pkt += (delta / 32);
1099    else
1100	sc->rx_avg_pkt += (delta / 8);
1101    delta = (sc->rx_avg_pkt - sc->rx_latency - 16) & ~3;
1102    if (delta < MIN_RX_EARLY_THRESHF)
1103	delta = MIN_RX_EARLY_THRESHF;
1104    sc->rx_early_thresh = delta;
1105    ++sc->arpcom.ac_if.if_ipackets;
1106    ep_fset(F_RX_FIRST);
1107    ep_frst(F_RX_TRAILER);
1108    top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1109    top->m_pkthdr.len = sc->cur_len;
1110
1111#if NBPFILTER > 0
1112    if (sc->bpf) {
1113	bpf_mtap(sc->bpf, top);
1114
1115	/*
1116	 * Note that the interface cannot be in promiscuous mode if there are
1117	 * no BPF listeners.  And if we are in promiscuous mode, we have to
1118	 * check if this packet is really ours.
1119	 */
1120	eh = mtod(top, struct ether_header *);
1121	if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
1122	    (eh->ether_dhost[0] & 1) == 0 &&
1123	    bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1124		 sizeof(eh->ether_dhost)) != 0 &&
1125	    bcmp(eh->ether_dhost, etherbroadcastaddr,
1126		 sizeof(eh->ether_dhost)) != 0) {
1127	    if (sc->top) {
1128		m_freem(sc->top);
1129		sc->top = 0;
1130	    }
1131	    ep_fset(F_RX_FIRST);
1132	    ep_frst(F_RX_TRAILER);
1133#ifdef EP_LOCAL_STATS
1134	    sc->rx_bpf_disc++;
1135#endif
1136	    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1137	    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | delta);
1138	    return;
1139	}
1140    }
1141#endif
1142
1143    eh = mtod(top, struct ether_header *);
1144    m_adj(top, sizeof(struct ether_header));
1145    ether_input(&sc->arpcom.ac_if, eh, top);
1146    if (!sc->mb[sc->next_mb])
1147	epmbuffill((caddr_t) sc, 0);
1148    sc->top = 0;
1149    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1150    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | delta);
1151    return;
1152
1153out:
1154    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1155    if (sc->top) {
1156	m_freem(sc->top);
1157	sc->top = 0;
1158#ifdef EP_LOCAL_STATS
1159	sc->rx_no_mbuf++;
1160#endif
1161    }
1162    delta = (sc->rx_avg_pkt - sc->rx_latency - 16) & ~3;
1163    if (delta < MIN_RX_EARLY_THRESHF)
1164	delta = MIN_RX_EARLY_THRESHF;
1165    ep_fset(F_RX_FIRST);
1166    ep_frst(F_RX_TRAILER);
1167    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1168    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH |
1169	 (sc->rx_early_thresh = delta));
1170}
1171
1172/*
1173 * Look familiar?
1174 */
1175static int
1176epioctl(ifp, cmd, data)
1177    register struct ifnet *ifp;
1178    int cmd;
1179    caddr_t data;
1180{
1181    register struct ifaddr *ifa = (struct ifaddr *) data;
1182    struct ep_softc *sc = &ep_softc[ifp->if_unit];
1183    struct ifreq *ifr = (struct ifreq *) data;
1184    int s, error = 0;
1185
1186    switch (cmd) {
1187      case SIOCSIFADDR:
1188	ifp->if_flags |= IFF_UP;
1189	switch (ifa->ifa_addr->sa_family) {
1190#ifdef INET
1191	  case AF_INET:
1192	    epinit(ifp->if_unit);	/* before arpwhohas */
1193	    arp_ifinit((struct arpcom *)ifp, ifa);
1194	    break;
1195#endif
1196#ifdef NS
1197	  case AF_NS:
1198	    {
1199		register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1200
1201		if (ns_nullhost(*ina))
1202		    ina->x_host =
1203			*(union ns_host *) (sc->arpcom.ac_enaddr);
1204		else {
1205		    ifp->if_flags &= ~IFF_RUNNING;
1206		    bcopy((caddr_t) ina->x_host.c_host,
1207			  (caddr_t) sc->arpcom.ac_enaddr,
1208			  sizeof(sc->arpcom.ac_enaddr));
1209		}
1210		epinit(ifp->if_unit);
1211		break;
1212	    }
1213#endif
1214	  default:
1215	    epinit(ifp->if_unit);
1216	    break;
1217	}
1218	break;
1219      case SIOCSIFFLAGS:
1220	if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1221	    ifp->if_flags &= ~IFF_RUNNING;
1222	    epstop(ifp->if_unit);
1223	    epmbufempty(sc);
1224	    break;
1225	}
1226	if (ifp->if_flags & IFF_UP && (ifp->if_flags & IFF_RUNNING) == 0)
1227	    epinit(ifp->if_unit);
1228	break;
1229#ifdef notdef
1230      case SIOCGHWADDR:
1231	bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1232	      sizeof(sc->sc_addr));
1233	break;
1234#endif
1235	case SIOCSIFMTU:
1236
1237		/*
1238		 * Set the interface MTU.
1239		 */
1240		if (ifr->ifr_mtu > ETHERMTU) {
1241			error = EINVAL;
1242		} else {
1243			ifp->if_mtu = ifr->ifr_mtu;
1244		}
1245		break;
1246
1247      default:
1248		error = EINVAL;
1249    }
1250    return (error);
1251}
1252
1253void
1254epreset(unit)
1255    int unit;
1256{
1257    int s = splimp();
1258
1259    epstop(unit);
1260    epinit(unit);
1261    splx(s);
1262}
1263
1264void
1265epwatchdog(unit)
1266    int unit;
1267{
1268    struct ep_softc *sc = &ep_softc[unit];
1269
1270    log(LOG_ERR, "ep%d: watchdog\n", unit);
1271    ++sc->arpcom.ac_if.if_oerrors;
1272
1273    epreset(unit);
1274}
1275
1276void
1277epstop(unit)
1278    int unit;
1279{
1280    struct ep_softc *sc = &ep_softc[unit];
1281
1282    outw(BASE + EP_COMMAND, RX_DISABLE);
1283    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1284    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1285    outw(BASE + EP_COMMAND, TX_DISABLE);
1286    outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
1287    outw(BASE + EP_COMMAND, RX_RESET);
1288    outw(BASE + EP_COMMAND, TX_RESET);
1289    outw(BASE + EP_COMMAND, C_INTR_LATCH);
1290    outw(BASE + EP_COMMAND, SET_RD_0_MASK);
1291    outw(BASE + EP_COMMAND, SET_INTR_MASK);
1292    outw(BASE + EP_COMMAND, SET_RX_FILTER);
1293}
1294
1295
1296static int
1297send_ID_sequence(port)
1298    int port;
1299{
1300    int cx, al;
1301
1302    for (al = 0xff, cx = 0; cx < 255; cx++) {
1303	outb(port, al);
1304	al <<= 1;
1305	if (al & 0x100)
1306	    al ^= 0xcf;
1307    }
1308    return (1);
1309}
1310
1311
1312/*
1313 * We get eeprom data from the id_port given an offset into the eeprom.
1314 * Basically; after the ID_sequence is sent to all of the cards; they enter
1315 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
1316 * the eeprom data.  We then read the port 16 times and with every read; the
1317 * cards check for contention (ie: if one card writes a 0 bit and another
1318 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
1319 * compares the data on the bus; if there is a difference then that card goes
1320 * into ID_WAIT state again). In the meantime; one bit of data is returned in
1321 * the AX register which is conveniently returned to us by inb().  Hence; we
1322 * read 16 times getting one bit of data with each read.
1323 */
1324static int
1325get_eeprom_data(id_port, offset)
1326    int id_port;
1327    int offset;
1328{
1329    int i, data = 0;
1330    outb(id_port, 0x80 + offset);
1331    DELAY(1000);
1332    for (i = 0; i < 16; i++)
1333	data = (data << 1) | (inw(id_port) & 1);
1334    return (data);
1335}
1336
1337/*
1338 * We suppose this is always called inside a splimp(){...}splx() region
1339 */
1340static void
1341epmbuffill(sp, dummy_arg)
1342    caddr_t sp;
1343    int dummy_arg;
1344{
1345    struct ep_softc *sc = (struct ep_softc *) sp;
1346    int i;
1347
1348    i = sc->last_mb;
1349    do {
1350	if (sc->mb[i] == NULL)
1351	    MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
1352	if (sc->mb[i] == NULL)
1353	    break;
1354	i = (i + 1) % MAX_MBS;
1355    } while (i != sc->next_mb);
1356    sc->last_mb = i;
1357}
1358
1359static void
1360epmbufempty(sc)
1361    struct ep_softc *sc;
1362{
1363    int s, i;
1364
1365    s = splimp();
1366    for (i = 0; i < MAX_MBS; i++) {
1367	if (sc->mb[i]) {
1368	    m_freem(sc->mb[i]);
1369	    sc->mb[i] = NULL;
1370	}
1371    }
1372    sc->last_mb = sc->next_mb = 0;
1373    splx(s);
1374}
1375
1376#endif				/* NEP > 0 */
1377