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