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