1/*	$NetBSD: if_el.c,v 1.87 2011/11/19 22:51:23 tls Exp $	*/
2
3/*
4 * Copyright (c) 1994, Matthew E. Kimmel.  Permission is hereby granted
5 * to use, copy, modify and distribute this software provided that both
6 * the copyright notice and this permission notice appear in all copies
7 * of the software, derivative works or modified versions, and any
8 * portions thereof.
9 */
10
11/*
12 * 3COM Etherlink 3C501 device driver
13 */
14
15/*
16 * Bugs/possible improvements:
17 *	- Does not currently support DMA
18 *	- Does not currently support multicasts
19 */
20
21#include <sys/cdefs.h>
22__KERNEL_RCSID(0, "$NetBSD: if_el.c,v 1.87 2011/11/19 22:51:23 tls Exp $");
23
24#include "opt_inet.h"
25
26#include <sys/param.h>
27#include <sys/systm.h>
28#include <sys/errno.h>
29#include <sys/ioctl.h>
30#include <sys/mbuf.h>
31#include <sys/socket.h>
32#include <sys/syslog.h>
33#include <sys/device.h>
34#include <sys/rnd.h>
35
36#include <net/if.h>
37#include <net/if_dl.h>
38#include <net/if_types.h>
39
40#include <net/if_ether.h>
41
42#ifdef INET
43#include <netinet/in.h>
44#include <netinet/in_systm.h>
45#include <netinet/in_var.h>
46#include <netinet/ip.h>
47#include <netinet/if_inarp.h>
48#endif
49
50
51#include <net/bpf.h>
52#include <net/bpfdesc.h>
53
54#include <sys/cpu.h>
55#include <sys/intr.h>
56#include <sys/bus.h>
57
58#include <dev/isa/isavar.h>
59#include <dev/isa/if_elreg.h>
60
61/* for debugging convenience */
62#ifdef EL_DEBUG
63#define DPRINTF(x) printf x
64#else
65#define DPRINTF(x)
66#endif
67
68/*
69 * per-line info and status
70 */
71struct el_softc {
72	struct device sc_dev;
73	void *sc_ih;
74
75	struct ethercom sc_ethercom;	/* ethernet common */
76	bus_space_tag_t sc_iot;		/* bus space identifier */
77	bus_space_handle_t sc_ioh;	/* i/o handle */
78
79	krndsource_t rnd_source;
80};
81
82/*
83 * prototypes
84 */
85int elintr(void *);
86void elinit(struct el_softc *);
87int elioctl(struct ifnet *, u_long, void *);
88void elstart(struct ifnet *);
89void elwatchdog(struct ifnet *);
90void elreset(struct el_softc *);
91void elstop(struct el_softc *);
92static int el_xmit(struct el_softc *);
93void elread(struct el_softc *, int);
94struct mbuf *elget(struct el_softc *sc, int);
95static inline void el_hardreset(struct el_softc *);
96
97int elprobe(device_t, cfdata_t, void *);
98void elattach(device_t, device_t, void *);
99
100CFATTACH_DECL(el, sizeof(struct el_softc),
101    elprobe, elattach, NULL, NULL);
102
103/*
104 * Probe routine.
105 *
106 * See if the card is there and at the right place.
107 * (XXX - cgd -- needs help)
108 */
109int
110elprobe(device_t parent, cfdata_t match, void *aux)
111{
112	struct isa_attach_args *ia = aux;
113	bus_space_tag_t iot = ia->ia_iot;
114	bus_space_handle_t ioh;
115	int iobase;
116	u_int8_t station_addr[ETHER_ADDR_LEN];
117	u_int8_t i;
118	int rval;
119
120	rval = 0;
121
122	if (ia->ia_nio < 1)
123		return (0);
124	if (ia->ia_nirq < 1)
125		return (0);
126
127	if (ISA_DIRECT_CONFIG(ia))
128		return (0);
129
130	iobase = ia->ia_io[0].ir_addr;
131
132	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
133		return (0);
134	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
135		return (0);
136
137	/* First check the base. */
138	if (iobase < 0x200 || iobase > 0x3f0)
139		return 0;
140
141	/* Map i/o space. */
142	if (bus_space_map(iot, iobase, 16, 0, &ioh))
143		return 0;
144
145	/*
146	 * Now attempt to grab the station address from the PROM and see if it
147	 * contains the 3com vendor code.
148	 */
149	DPRINTF(("Probing 3c501 at 0x%x...\n", iobase));
150
151	/* Reset the board. */
152	DPRINTF(("Resetting board...\n"));
153	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
154	delay(5);
155	bus_space_write_1(iot, ioh, EL_AC, 0);
156
157	/* Now read the address. */
158	DPRINTF(("Reading station address...\n"));
159	for (i = 0; i < ETHER_ADDR_LEN; i++) {
160		bus_space_write_1(iot, ioh, EL_GPBL, i);
161		station_addr[i] = bus_space_read_1(iot, ioh, EL_EAW);
162	}
163	DPRINTF(("Address is %s\n", ether_sprintf(station_addr)));
164
165	/*
166	 * If the vendor code is ok, return a 1.  We'll assume that whoever
167	 * configured this system is right about the IRQ.
168	 */
169	if (station_addr[0] != 0x02 || station_addr[1] != 0x60 ||
170	    station_addr[2] != 0x8c) {
171		DPRINTF(("Bad vendor code.\n"));
172		goto out;
173	}
174	DPRINTF(("Vendor code ok.\n"));
175
176	ia->ia_nio = 1;
177	ia->ia_io[0].ir_size = 16;
178
179	ia->ia_nirq = 1;
180
181	ia->ia_niomem = 0;
182	ia->ia_ndrq = 0;
183
184	rval = 1;
185
186 out:
187	bus_space_unmap(iot, ioh, 16);
188	return rval;
189}
190
191/*
192 * Attach the interface to the kernel data structures.  By the time this is
193 * called, we know that the card exists at the given I/O address.  We still
194 * assume that the IRQ given is correct.
195 */
196void
197elattach(device_t parent, device_t self, void *aux)
198{
199	struct el_softc *sc = (void *)self;
200	struct isa_attach_args *ia = aux;
201	bus_space_tag_t iot = ia->ia_iot;
202	bus_space_handle_t ioh;
203	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
204	u_int8_t myaddr[ETHER_ADDR_LEN];
205	u_int8_t i;
206
207	printf("\n");
208
209	DPRINTF(("Attaching %s...\n", device_xname(&sc->sc_dev)));
210
211	/* Map i/o space. */
212	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 16, 0, &ioh)) {
213		aprint_error_dev(self, "can't map i/o space\n");
214		return;
215	}
216
217	sc->sc_iot = iot;
218	sc->sc_ioh = ioh;
219
220	/* Reset the board. */
221	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
222	delay(5);
223	bus_space_write_1(iot, ioh, EL_AC, 0);
224
225	/* Now read the address. */
226	for (i = 0; i < ETHER_ADDR_LEN; i++) {
227		bus_space_write_1(iot, ioh, EL_GPBL, i);
228		myaddr[i] = bus_space_read_1(iot, ioh, EL_EAW);
229	}
230
231	/* Stop the board. */
232	elstop(sc);
233
234	/* Initialize ifnet structure. */
235	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
236	ifp->if_softc = sc;
237	ifp->if_start = elstart;
238	ifp->if_ioctl = elioctl;
239	ifp->if_watchdog = elwatchdog;
240	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
241	IFQ_SET_READY(&ifp->if_snd);
242
243	/* Now we can attach the interface. */
244	DPRINTF(("Attaching interface...\n"));
245	if_attach(ifp);
246	ether_ifattach(ifp, myaddr);
247
248	/* Print out some information for the user. */
249	printf("%s: address %s\n", device_xname(self), ether_sprintf(myaddr));
250
251	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
252	    IST_EDGE, IPL_NET, elintr, sc);
253
254	DPRINTF(("Attaching to random...\n"));
255	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev),
256			  RND_TYPE_NET, 0);
257
258	DPRINTF(("elattach() finished.\n"));
259}
260
261/*
262 * Reset interface.
263 */
264void
265elreset(struct el_softc *sc)
266{
267	int s;
268
269	DPRINTF(("elreset()\n"));
270	s = splnet();
271	elstop(sc);
272	elinit(sc);
273	splx(s);
274}
275
276/*
277 * Stop interface.
278 */
279void
280elstop(struct el_softc *sc)
281{
282
283	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EL_AC, 0);
284}
285
286/*
287 * Do a hardware reset of the board, and upload the ethernet address again in
288 * case the board forgets.
289 */
290static inline void
291el_hardreset(struct el_softc *sc)
292{
293	bus_space_tag_t iot = sc->sc_iot;
294	bus_space_handle_t ioh = sc->sc_ioh;
295	int i;
296
297	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
298	delay(5);
299	bus_space_write_1(iot, ioh, EL_AC, 0);
300
301	for (i = 0; i < ETHER_ADDR_LEN; i++)
302		bus_space_write_1(iot, ioh, i,
303		    CLLADDR(sc->sc_ethercom.ec_if.if_sadl)[i]);
304}
305
306/*
307 * Initialize interface.
308 */
309void
310elinit(struct el_softc *sc)
311{
312	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
313	bus_space_tag_t iot = sc->sc_iot;
314	bus_space_handle_t ioh = sc->sc_ioh;
315
316	/* First, reset the board. */
317	el_hardreset(sc);
318
319	/* Configure rx. */
320	DPRINTF(("Configuring rx...\n"));
321	if (ifp->if_flags & IFF_PROMISC)
322		bus_space_write_1(iot, ioh, EL_RXC,
323		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
324		    EL_RXC_DOFLOW | EL_RXC_PROMISC);
325	else
326		bus_space_write_1(iot, ioh, EL_RXC,
327		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
328		    EL_RXC_DOFLOW | EL_RXC_ABROAD);
329	bus_space_write_1(iot, ioh, EL_RBC, 0);
330
331	/* Configure TX. */
332	DPRINTF(("Configuring tx...\n"));
333	bus_space_write_1(iot, ioh, EL_TXC, 0);
334
335	/* Start reception. */
336	DPRINTF(("Starting reception...\n"));
337	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
338
339	/* Set flags appropriately. */
340	ifp->if_flags |= IFF_RUNNING;
341	ifp->if_flags &= ~IFF_OACTIVE;
342
343	/* And start output. */
344	elstart(ifp);
345}
346
347/*
348 * Start output on interface.  Get datagrams from the queue and output them,
349 * giving the receiver a chance between datagrams.  Call only from splnet or
350 * interrupt level!
351 */
352void
353elstart(struct ifnet *ifp)
354{
355	struct el_softc *sc = ifp->if_softc;
356	bus_space_tag_t iot = sc->sc_iot;
357	bus_space_handle_t ioh = sc->sc_ioh;
358	struct mbuf *m, *m0;
359	int s, i, off, retries;
360
361	DPRINTF(("elstart()...\n"));
362	s = splnet();
363
364	/* Don't do anything if output is active. */
365	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
366		splx(s);
367		return;
368	}
369
370	ifp->if_flags |= IFF_OACTIVE;
371
372	/*
373	 * The main loop.  They warned me against endless loops, but would I
374	 * listen?  NOOO....
375	 */
376	for (;;) {
377		/* Dequeue the next datagram. */
378		IFQ_DEQUEUE(&ifp->if_snd, m0);
379
380		/* If there's nothing to send, return. */
381		if (m0 == 0)
382			break;
383
384		/* Give the packet to the bpf, if any. */
385		bpf_mtap(ifp, m0);
386
387		/* Disable the receiver. */
388		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
389		bus_space_write_1(iot, ioh, EL_RBC, 0);
390
391		/* Transfer datagram to board. */
392		DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
393		off = EL_BUFSIZ - max(m0->m_pkthdr.len,
394		    ETHER_MIN_LEN - ETHER_CRC_LEN);
395#ifdef DIAGNOSTIC
396		if ((off & 0xffff) != off)
397			printf("%s: bogus off 0x%x\n",
398			    device_xname(&sc->sc_dev), off);
399#endif
400		bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
401		bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
402
403		/* Copy the datagram to the buffer. */
404		for (m = m0; m != 0; m = m->m_next)
405			bus_space_write_multi_1(iot, ioh, EL_BUF,
406			    mtod(m, u_int8_t *), m->m_len);
407		for (i = 0;
408		    i < ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len; i++)
409			bus_space_write_1(iot, ioh, EL_BUF, 0);
410
411		m_freem(m0);
412
413		/* Now transmit the datagram. */
414		retries = 0;
415		for (;;) {
416			bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
417			bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
418			if (el_xmit(sc)) {
419				ifp->if_oerrors++;
420				break;
421			}
422			/* Check out status. */
423			i = bus_space_read_1(iot, ioh, EL_TXS);
424			DPRINTF(("tx status=0x%x\n", i));
425			if ((i & EL_TXS_READY) == 0) {
426				DPRINTF(("el: err txs=%x\n", i));
427				if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
428					ifp->if_collisions++;
429					if ((i & EL_TXC_DCOLL16) == 0 &&
430					    retries < 15) {
431						retries++;
432						bus_space_write_1(iot, ioh,
433						    EL_AC, EL_AC_HOST);
434					}
435				} else {
436					ifp->if_oerrors++;
437					break;
438				}
439			} else {
440				ifp->if_opackets++;
441				break;
442			}
443		}
444
445		/*
446		 * Now give the card a chance to receive.
447		 * Gotta love 3c501s...
448		 */
449		(void)bus_space_read_1(iot, ioh, EL_AS);
450		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
451		splx(s);
452		/* Interrupt here. */
453		s = splnet();
454	}
455
456	(void)bus_space_read_1(iot, ioh, EL_AS);
457	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
458	ifp->if_flags &= ~IFF_OACTIVE;
459	splx(s);
460}
461
462/*
463 * This function actually attempts to transmit a datagram downloaded to the
464 * board.  Call at splnet or interrupt, after downloading data!  Returns 0 on
465 * success, non-0 on failure.
466 */
467static int
468el_xmit(struct el_softc *sc)
469{
470	bus_space_tag_t iot = sc->sc_iot;
471	bus_space_handle_t ioh = sc->sc_ioh;
472	int i;
473
474	/*
475	 * XXX
476	 * This busy-waits for the tx completion.  Can we get an interrupt
477	 * instead?
478	 */
479
480	DPRINTF(("el: xmit..."));
481	bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
482	i = 20000;
483	while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
484		i--;
485	if (i == 0) {
486		DPRINTF(("tx not ready\n"));
487		return -1;
488	}
489	DPRINTF(("%d cycles.\n", 20000 - i));
490	return 0;
491}
492
493/*
494 * Controller interrupt.
495 */
496int
497elintr(void *arg)
498{
499	struct el_softc *sc = arg;
500	bus_space_tag_t iot = sc->sc_iot;
501	bus_space_handle_t ioh = sc->sc_ioh;
502	u_int8_t rxstat;
503	int len;
504
505	DPRINTF(("elintr: "));
506
507	/* Check board status. */
508	if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
509		(void)bus_space_read_1(iot, ioh, EL_RXC);
510		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
511		return 0;
512	}
513
514	for (;;) {
515		rxstat = bus_space_read_1(iot, ioh, EL_RXS);
516		if (rxstat & EL_RXS_STALE)
517			break;
518
519		/* If there's an overflow, reinit the board. */
520		if ((rxstat & EL_RXS_NOFLOW) == 0) {
521			DPRINTF(("overflow.\n"));
522			el_hardreset(sc);
523			/* Put board back into receive mode. */
524			if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
525				bus_space_write_1(iot, ioh, EL_RXC,
526				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
527				    EL_RXC_DOFLOW | EL_RXC_PROMISC);
528			else
529				bus_space_write_1(iot, ioh, EL_RXC,
530				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
531				    EL_RXC_DOFLOW | EL_RXC_ABROAD);
532			(void)bus_space_read_1(iot, ioh, EL_AS);
533			bus_space_write_1(iot, ioh, EL_RBC, 0);
534			break;
535		}
536
537		/* Incoming packet. */
538		len = bus_space_read_1(iot, ioh, EL_RBL);
539		len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
540		DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
541		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
542
543		/* Pass data up to upper levels. */
544		elread(sc, len);
545
546		/* Is there another packet? */
547		if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
548			break;
549
550		rnd_add_uint32(&sc->rnd_source, rxstat);
551
552		DPRINTF(("<rescan> "));
553	}
554
555	(void)bus_space_read_1(iot, ioh, EL_RXC);
556	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
557	return 1;
558}
559
560/*
561 * Pass a packet to the higher levels.
562 */
563void
564elread(struct el_softc *sc, int len)
565{
566	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
567	struct mbuf *m;
568
569	if (len <= sizeof(struct ether_header) ||
570	    len > ETHER_MAX_LEN) {
571		printf("%s: invalid packet size %d; dropping\n",
572		    device_xname(&sc->sc_dev), len);
573		ifp->if_ierrors++;
574		return;
575	}
576
577	/* Pull packet off interface. */
578	m = elget(sc, len);
579	if (m == 0) {
580		ifp->if_ierrors++;
581		return;
582	}
583
584	ifp->if_ipackets++;
585
586	/*
587	 * Check if there's a BPF listener on this interface.
588	 * If so, hand off the raw packet to BPF.
589	 */
590	bpf_mtap(ifp, m);
591
592	(*ifp->if_input)(ifp, m);
593}
594
595/*
596 * Pull read data off a interface.  Len is length of data, with local net
597 * header stripped.  We copy the data into mbufs.  When full cluster sized
598 * units are present we copy into clusters.
599 */
600struct mbuf *
601elget(struct el_softc *sc, int totlen)
602{
603	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
604	bus_space_tag_t iot = sc->sc_iot;
605	bus_space_handle_t ioh = sc->sc_ioh;
606	struct mbuf *m, *m0, *newm;
607	int len;
608
609	MGETHDR(m0, M_DONTWAIT, MT_DATA);
610	if (m0 == 0)
611		return (0);
612	m0->m_pkthdr.rcvif = ifp;
613	m0->m_pkthdr.len = totlen;
614	len = MHLEN;
615	m = m0;
616
617	bus_space_write_1(iot, ioh, EL_GPBL, 0);
618	bus_space_write_1(iot, ioh, EL_GPBH, 0);
619
620	while (totlen > 0) {
621		if (totlen >= MINCLSIZE) {
622			MCLGET(m, M_DONTWAIT);
623			if ((m->m_flags & M_EXT) == 0)
624				goto bad;
625			len = MCLBYTES;
626		}
627
628		m->m_len = len = min(totlen, len);
629		bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
630
631		totlen -= len;
632		if (totlen > 0) {
633			MGET(newm, M_DONTWAIT, MT_DATA);
634			if (newm == 0)
635				goto bad;
636			len = MLEN;
637			m = m->m_next = newm;
638		}
639	}
640
641	bus_space_write_1(iot, ioh, EL_RBC, 0);
642	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
643
644	return (m0);
645
646bad:
647	m_freem(m0);
648	return (0);
649}
650
651/*
652 * Process an ioctl request. This code needs some work - it looks pretty ugly.
653 */
654int
655elioctl(struct ifnet *ifp, u_long cmd, void *data)
656{
657	struct el_softc *sc = ifp->if_softc;
658	struct ifaddr *ifa = (struct ifaddr *)data;
659	int s, error = 0;
660
661	s = splnet();
662
663	switch (cmd) {
664
665	case SIOCINITIFADDR:
666		ifp->if_flags |= IFF_UP;
667
668		elinit(sc);
669		switch (ifa->ifa_addr->sa_family) {
670#ifdef INET
671		case AF_INET:
672			arp_ifinit(ifp, ifa);
673			break;
674#endif
675		default:
676			break;
677		}
678		break;
679
680	case SIOCSIFFLAGS:
681		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
682			break;
683		/* XXX re-use ether_ioctl() */
684		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
685		case IFF_RUNNING:
686			/*
687			 * If interface is marked down and it is running, then
688			 * stop it.
689			 */
690			elstop(sc);
691			ifp->if_flags &= ~IFF_RUNNING;
692			break;
693		case IFF_UP:
694			/*
695			 * If interface is marked up and it is stopped, then
696			 * start it.
697			 */
698			elinit(sc);
699			break;
700		default:
701			/*
702			 * Some other important flag might have changed, so
703			 * reset.
704			 */
705			elreset(sc);
706			break;
707		}
708		break;
709
710	default:
711		error = ether_ioctl(ifp, cmd, data);
712		break;
713	}
714
715	splx(s);
716	return error;
717}
718
719/*
720 * Device timeout routine.
721 */
722void
723elwatchdog(struct ifnet *ifp)
724{
725	struct el_softc *sc = ifp->if_softc;
726
727	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
728	sc->sc_ethercom.ec_if.if_oerrors++;
729
730	elreset(sc);
731}
732