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