seeq8005.c revision 1.10
1/* $NetBSD: seeq8005.c,v 1.10 2001/03/24 13:40:41 bjh21 Exp $ */
2
3/*
4 * Copyright (c) 2000 Ben Harris
5 * Copyright (c) 1995 Mark Brinicombe
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Mark Brinicombe.
19 * 4. The name of the company nor the name of the author may be used to
20 *    endorse or promote products derived from this software without specific
21 *    prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35/*
36 * seeq8005.c - SEEQ 8005 device driver
37 */
38/*
39 * This driver currently supports the following chip:
40 * SEEQ 8005 Advanced Ethernet Data Link Controller
41 */
42/*
43 * This driver is based on the arm32 ea(4) driver, hence the names of many
44 * of the functions.
45 */
46/*
47 * Bugs/possible improvements:
48 *	- Does not currently support DMA
49 *	- Does not currently support multicasts
50 *	- Does not transmit multiple packets in one go
51 *	- Does not support big-endian hosts
52 *	- Does not support 8-bit busses
53 */
54
55#include "opt_inet.h"
56#include "opt_ns.h"
57
58#include <sys/types.h>
59#include <sys/param.h>
60
61__RCSID("$NetBSD: seeq8005.c,v 1.10 2001/03/24 13:40:41 bjh21 Exp $");
62
63#include <sys/systm.h>
64#include <sys/endian.h>
65#include <sys/errno.h>
66#include <sys/ioctl.h>
67#include <sys/mbuf.h>
68#include <sys/socket.h>
69#include <sys/syslog.h>
70#include <sys/device.h>
71
72#include <net/if.h>
73#include <net/if_dl.h>
74#include <net/if_types.h>
75#include <net/if_ether.h>
76
77#ifdef INET
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/in_var.h>
81#include <netinet/ip.h>
82#include <netinet/if_inarp.h>
83#endif
84
85#ifdef NS
86#include <netns/ns.h>
87#include <netns/ns_if.h>
88#endif
89
90#include "bpfilter.h"
91#if NBPFILTER > 0
92#include <net/bpf.h>
93#include <net/bpfdesc.h>
94#endif
95
96#include <machine/bus.h>
97#include <machine/intr.h>
98
99#include <dev/ic/seeq8005reg.h>
100#include <dev/ic/seeq8005var.h>
101
102#ifndef SEEQ_TIMEOUT
103#define SEEQ_TIMEOUT	60
104#endif
105
106#define SEEQ_TX_BUFFER_SIZE	0x4000
107#define SEEQ_RX_BUFFER_SIZE	0xC000
108
109/*#define SEEQ_TX_DEBUG*/
110/*#define SEEQ_RX_DEBUG*/
111/*#define SEEQ_DEBUG*/
112/*#define SEEQ_PACKET_DEBUG*/
113
114/* for debugging convenience */
115#ifdef SEEQ_DEBUG
116#define dprintf(x) printf x
117#else
118#define dprintf(x)
119#endif
120
121/*
122 * prototypes
123 */
124
125static int ea_init(struct ifnet *);
126static int ea_ioctl(struct ifnet *, u_long, caddr_t);
127static void ea_start(struct ifnet *);
128static void ea_watchdog(struct ifnet *);
129static void ea_chipreset(struct seeq8005_softc *);
130static void ea_ramtest(struct seeq8005_softc *);
131static int ea_stoptx(struct seeq8005_softc *);
132static int ea_stoprx(struct seeq8005_softc *);
133static void ea_stop(struct ifnet *, int);
134static void ea_await_fifo_empty(struct seeq8005_softc *);
135static void ea_await_fifo_full(struct seeq8005_softc *);
136static void ea_writebuf(struct seeq8005_softc *, u_char *, u_int, size_t);
137static void ea_readbuf(struct seeq8005_softc *, u_char *, u_int, size_t);
138static void ea_select_buffer(struct seeq8005_softc *, int);
139static void ea_set_address(struct seeq8005_softc *, int, const u_int8_t *);
140static void earead(struct seeq8005_softc *, int, int);
141static struct mbuf *eaget(struct seeq8005_softc *, int, int, struct ifnet *);
142static void eagetpackets(struct seeq8005_softc *);
143static void eatxpacket(struct seeq8005_softc *);
144static void ea_mc_reset(struct seeq8005_softc *);
145
146
147#ifdef SEEQ_PACKET_DEBUG
148void ea_dump_buffer(struct seeq8005_softc *, int);
149#endif
150
151
152#ifdef SEEQ_PACKET_DEBUG
153/*
154 * Dump the interface buffer
155 */
156
157void
158ea_dump_buffer(struct seeq8005_softc *sc, u_int offset)
159{
160	bus_space_tag_t iot = sc->sc_iot;
161	bus_space_handle_t ioh = sc->sc_ioh;
162	u_int addr;
163	int loop, ctrl, ptr;
164	size_t size;
165
166	addr = offset;
167
168	do {
169		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
170				 sc->sc_command | SEEQ_CMD_FIFO_READ);
171		bus_space_write_2(iot, ioh, SEEQ_CONFIG1,
172				  sc->sc_config1 | SEEQ_BUFCODE_LOCAL_MEM);
173		bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr);
174
175		ptr = bus_space_read_2(iot, ioh, SEEQ_BUFWIN);
176		ctrl = bus_space_read_2(iot, ioh, SEEQ_BUFWIN);
177		ptr = ((ptr & 0xff) << 8) | ((ptr >> 8) & 0xff);
178
179		if (ptr == 0) break;
180		size = ptr - addr;
181
182		printf("addr=%04x size=%04x ", addr, size);
183		printf("cmd=%02x st=%02x\n", ctrl & 0xff, ctrl >> 8);
184
185		for (loop = 0; loop < size - 4; loop += 2)
186			printf("%04x ",
187			       bus_space_read_2(iot, ioh, SEEQ_BUFWIN));
188		printf("\n");
189		addr = ptr;
190	} while (size != 0);
191}
192#endif
193
194/*
195 * Attach chip.
196 */
197
198void
199seeq8005_attach(struct seeq8005_softc *sc, const u_int8_t *myaddr)
200{
201	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
202	u_int id;
203
204	printf(" address %s", ether_sprintf(myaddr));
205
206	/* Stop the board. */
207
208	ea_chipreset(sc);
209
210	/* Get the product ID */
211
212	ea_select_buffer(sc, SEEQ_BUFCODE_PRODUCTID);
213	id = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN);
214
215	if ((id & 0xf0) == 0xa0) {
216		sc->sc_flags |= SEEQ8005_80C04;
217		printf(", SEEQ 80C04 rev %02x", id);
218	} else
219		printf(", SEEQ 8005");
220
221	/* Initialise ifnet structure. */
222
223	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
224	ifp->if_softc = sc;
225	ifp->if_start = ea_start;
226	ifp->if_ioctl = ea_ioctl;
227	ifp->if_init = ea_init;
228	ifp->if_stop = ea_stop;
229	ifp->if_watchdog = ea_watchdog;
230	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
231	IFQ_SET_READY(&ifp->if_snd);
232
233	/* Now we can attach the interface. */
234
235	if_attach(ifp);
236	ether_ifattach(ifp, myaddr);
237
238	/* Test the RAM */
239	ea_ramtest(sc);
240
241	printf("\n");
242}
243
244
245/*
246 * Test the RAM on the ethernet card.
247 */
248
249void
250ea_ramtest(struct seeq8005_softc *sc)
251{
252	bus_space_tag_t iot = sc->sc_iot;
253	bus_space_handle_t ioh = sc->sc_ioh;
254	int loop;
255	u_int sum = 0;
256
257/*	dprintf(("ea_ramtest()\n"));*/
258
259	/*
260	 * Test the buffer memory on the board.
261	 * Write simple pattens to it and read them back.
262	 */
263
264	/* Set up the whole buffer RAM for writing */
265
266	ea_select_buffer(sc, SEEQ_BUFCODE_TX_EAP);
267	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (SEEQ_MAX_BUFFER_SIZE >> 8) - 1);
268	bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
269	bus_space_write_2(iot, ioh, SEEQ_RX_PTR, SEEQ_MAX_BUFFER_SIZE - 2);
270
271#define SEEQ_RAMTEST_LOOP(value)						\
272do {									\
273	/* Set the write start address and write a pattern */		\
274	ea_writebuf(sc, NULL, 0x0000, 0);				\
275	for (loop = 0; loop < SEEQ_MAX_BUFFER_SIZE; loop += 2)		\
276		bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (value));	\
277									\
278	/* Set the read start address and verify the pattern */		\
279	ea_readbuf(sc, NULL, 0x0000, 0);				\
280	for (loop = 0; loop < SEEQ_MAX_BUFFER_SIZE; loop += 2)		\
281		if (bus_space_read_2(iot, ioh, SEEQ_BUFWIN) != (value)) \
282			++sum;						\
283	if (sum != 0)							\
284		dprintf(("sum=%d\n", sum));				\
285} while (/*CONSTCOND*/0)
286
287	SEEQ_RAMTEST_LOOP(loop);
288	SEEQ_RAMTEST_LOOP(loop ^ (SEEQ_MAX_BUFFER_SIZE - 1));
289	SEEQ_RAMTEST_LOOP(0xaa55);
290	SEEQ_RAMTEST_LOOP(0x55aa);
291
292	/* Report */
293
294	if (sum > 0)
295		printf("%s: buffer RAM failed self test, %d faults\n",
296		       sc->sc_dev.dv_xname, sum);
297}
298
299
300/*
301 * Stop the tx interface.
302 *
303 * Returns 0 if the tx was already stopped or 1 if it was active
304 */
305
306static int
307ea_stoptx(struct seeq8005_softc *sc)
308{
309	bus_space_tag_t iot = sc->sc_iot;
310	bus_space_handle_t ioh = sc->sc_ioh;
311	int timeout;
312	int status;
313
314	dprintf(("ea_stoptx()\n"));
315
316	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
317	if (!(status & SEEQ_STATUS_TX_ON))
318		return 0;
319
320	/* Stop any tx and wait for confirmation */
321	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
322			  sc->sc_command | SEEQ_CMD_TX_OFF);
323
324	timeout = 20000;
325	do {
326		status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
327	} while ((status & SEEQ_STATUS_TX_ON) && --timeout > 0);
328	if (timeout == 0)
329		dprintf(("ea_stoptx: timeout waiting for tx termination\n"));
330
331	/* Clear any pending tx interrupt */
332	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
333		   sc->sc_command | SEEQ_CMD_TX_INTACK);
334	return 1;
335}
336
337
338/*
339 * Stop the rx interface.
340 *
341 * Returns 0 if the tx was already stopped or 1 if it was active
342 */
343
344static int
345ea_stoprx(struct seeq8005_softc *sc)
346{
347	bus_space_tag_t iot = sc->sc_iot;
348	bus_space_handle_t ioh = sc->sc_ioh;
349	int timeout;
350	int status;
351
352	dprintf(("ea_stoprx()\n"));
353
354	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
355	if (!(status & SEEQ_STATUS_RX_ON))
356		return 0;
357
358	/* Stop any rx and wait for confirmation */
359
360	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
361			  sc->sc_command | SEEQ_CMD_RX_OFF);
362
363	timeout = 20000;
364	do {
365		status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
366	} while ((status & SEEQ_STATUS_RX_ON) && --timeout > 0);
367	if (timeout == 0)
368		dprintf(("ea_stoprx: timeout waiting for rx termination\n"));
369
370	/* Clear any pending rx interrupt */
371
372	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
373		   sc->sc_command | SEEQ_CMD_RX_INTACK);
374	return 1;
375}
376
377
378/*
379 * Stop interface.
380 * Stop all IO and shut the interface down
381 */
382
383static void
384ea_stop(struct ifnet *ifp, int disable)
385{
386	struct seeq8005_softc *sc = ifp->if_softc;
387	bus_space_tag_t iot = sc->sc_iot;
388	bus_space_handle_t ioh = sc->sc_ioh;
389
390	dprintf(("ea_stop()\n"));
391
392	/* Stop all IO */
393	ea_stoptx(sc);
394	ea_stoprx(sc);
395
396	/* Disable rx and tx interrupts */
397	sc->sc_command &= (SEEQ_CMD_RX_INTEN | SEEQ_CMD_TX_INTEN);
398
399	/* Clear any pending interrupts */
400	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
401			  sc->sc_command | SEEQ_CMD_RX_INTACK |
402			  SEEQ_CMD_TX_INTACK | SEEQ_CMD_DMA_INTACK |
403			  SEEQ_CMD_BW_INTACK);
404	dprintf(("st=%08x", bus_space_read_2(iot, ioh, SEEQ_STATUS)));
405
406	/* Cancel any watchdog timer */
407       	sc->sc_ethercom.ec_if.if_timer = 0;
408}
409
410
411/*
412 * Reset the chip
413 * Following this the software registers are reset
414 */
415
416static void
417ea_chipreset(struct seeq8005_softc *sc)
418{
419	bus_space_tag_t iot = sc->sc_iot;
420	bus_space_handle_t ioh = sc->sc_ioh;
421
422	dprintf(("ea_chipreset()\n"));
423
424	/* Reset the controller. Min of 4us delay here */
425
426	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, SEEQ_CFG2_RESET);
427	delay(4);
428
429	sc->sc_command = 0;
430	sc->sc_config1 = 0;
431	sc->sc_config2 = 0;
432}
433
434
435/*
436 * If the DMA FIFO's in write mode, wait for it to empty.  Needed when
437 * switching the FIFO from write to read.  We also use it when changing
438 * the address for writes.
439 */
440static void
441ea_await_fifo_empty(struct seeq8005_softc *sc)
442{
443	bus_space_tag_t iot = sc->sc_iot;
444	bus_space_handle_t ioh = sc->sc_ioh;
445	int timeout;
446
447	timeout = 20000;
448	if ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
449	     SEEQ_STATUS_FIFO_DIR) != 0)
450		return; /* FIFO is reading anyway. */
451	while ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
452		SEEQ_STATUS_FIFO_EMPTY) == 0 &&
453	       --timeout > 0)
454		continue;
455}
456
457/*
458 * Wait for the DMA FIFO to fill before reading from it.
459 */
460static void
461ea_await_fifo_full(struct seeq8005_softc *sc)
462{
463	bus_space_tag_t iot = sc->sc_iot;
464	bus_space_handle_t ioh = sc->sc_ioh;
465	int timeout;
466
467	timeout = 20000;
468	while ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
469		SEEQ_STATUS_FIFO_FULL) == 0 &&
470	       --timeout > 0)
471		continue;
472}
473
474/*
475 * write to the buffer memory on the interface
476 *
477 * The buffer address is set to ADDR.
478 * If len != 0 then data is copied from the address starting at buf
479 * to the interface buffer.
480 * BUF must be usable as a u_int16_t *.
481 * If LEN is odd, it must be safe to overwrite one extra byte.
482 */
483
484static void
485ea_writebuf(struct seeq8005_softc *sc, u_char *buf, u_int addr, size_t len)
486{
487	bus_space_tag_t iot = sc->sc_iot;
488	bus_space_handle_t ioh = sc->sc_ioh;
489
490	dprintf(("writebuf: st=%04x\n",
491		 bus_space_read_2(iot, ioh, SEEQ_STATUS)));
492
493#ifdef DIAGNOSTIC
494	if (__predict_false(!ALIGNED_POINTER(buf, u_int16_t)))
495		panic("%s: unaligned writebuf", sc->sc_dev.dv_xname);
496#endif
497	if (__predict_false(addr >= SEEQ_MAX_BUFFER_SIZE))
498		panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
499
500	/* Assume that copying too much is safe. */
501	if (len % 2 != 0)
502		len++;
503
504	ea_await_fifo_empty(sc);
505
506	ea_select_buffer(sc, SEEQ_BUFCODE_LOCAL_MEM);
507	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
508			  sc->sc_command | SEEQ_CMD_FIFO_WRITE);
509       	bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr);
510
511	if (len > 0)
512		bus_space_write_multi_2(iot, ioh, SEEQ_BUFWIN,
513					(u_int16_t *)buf, len / 2);
514	/* Leave FIFO to empty in the background */
515}
516
517
518/*
519 * read from the buffer memory on the interface
520 *
521 * The buffer address is set to ADDR.
522 * If len != 0 then data is copied from the interface buffer to the
523 * address starting at buf.
524 * BUF must be usable as a u_int16_t *.
525 * If LEN is odd, it must be safe to overwrite one extra byte.
526 */
527
528static void
529ea_readbuf(struct seeq8005_softc *sc, u_char *buf, u_int addr, size_t len)
530{
531
532	bus_space_tag_t iot = sc->sc_iot;
533	bus_space_handle_t ioh = sc->sc_ioh;
534
535	dprintf(("readbuf: st=%04x addr=%04x len=%d\n",
536		 bus_space_read_2(iot, ioh, SEEQ_STATUS), addr, len));
537
538#ifdef DIAGNOSTIC
539	if (!ALIGNED_POINTER(buf, u_int16_t))
540		panic("%s: unaligned readbuf", sc->sc_dev.dv_xname);
541#endif
542	if (addr >= SEEQ_MAX_BUFFER_SIZE)
543		panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
544
545	/* Assume that copying too much is safe. */
546	if (len % 2 != 0)
547		len++;
548
549	ea_await_fifo_empty(sc);
550
551	ea_select_buffer(sc, SEEQ_BUFCODE_LOCAL_MEM);
552	bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr);
553	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
554			  sc->sc_command | SEEQ_CMD_FIFO_READ);
555
556	ea_await_fifo_full(sc);
557
558	if (len > 0)
559		bus_space_read_multi_2(iot, ioh, SEEQ_BUFWIN,
560				       (u_int16_t *)buf, len / 2);
561}
562
563static void
564ea_select_buffer(struct seeq8005_softc *sc, int bufcode)
565{
566
567	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_CONFIG1,
568			  sc->sc_config1 | bufcode);
569}
570
571/* Must be called at splnet */
572static void
573ea_set_address(struct seeq8005_softc *sc, int which, u_int8_t const *ea)
574{
575	int i;
576
577	ea_select_buffer(sc, SEEQ_BUFCODE_STATION_ADDR0 + which);
578	for (i = 0; i < ETHER_ADDR_LEN; ++i)
579		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN,
580				  ea[i]);
581}
582
583/*
584 * Initialize interface.
585 *
586 * This should leave the interface in a state for packet reception and
587 * transmission.
588 */
589
590static int
591ea_init(struct ifnet *ifp)
592{
593	struct seeq8005_softc *sc = ifp->if_softc;
594	bus_space_tag_t iot = sc->sc_iot;
595	bus_space_handle_t ioh = sc->sc_ioh;
596	int s;
597
598	dprintf(("ea_init()\n"));
599
600	s = splnet();
601
602	/* First, reset the board. */
603
604	ea_chipreset(sc);
605
606	/* Set up defaults for the registers */
607
608	sc->sc_command = 0x00;
609	sc->sc_config1 = 0x00; /* XXX DMA settings? */
610#if BYTE_ORDER == BIG_ENDIAN
611	sc->sc_config2 = SEEQ_CFG2_BYTESWAP
612#else
613	sc->sc_config2 = 0;
614#endif
615
616	bus_space_write_2(iot, ioh, SEEQ_COMMAND, sc->sc_command);
617	bus_space_write_2(iot, ioh, SEEQ_CONFIG1, sc->sc_config1);
618	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
619
620	/* Split board memory into Rx and Tx. */
621	ea_select_buffer(sc, SEEQ_BUFCODE_TX_EAP);
622	bus_space_write_2(iot, ioh, SEEQ_BUFWIN,
623			  (SEEQ_TX_BUFFER_SIZE >> 8) - 1);
624
625	/* Write the station address - the receiver must be off */
626	ea_set_address(sc, 0, LLADDR(ifp->if_sadl));
627
628	/* Configure rx. */
629	dprintf(("Configuring rx...\n"));
630	if (ifp->if_flags & IFF_PROMISC)
631		sc->sc_config1 = SEEQ_CFG1_PROMISCUOUS;
632	else if (ifp->if_flags & IFF_ALLMULTI)
633		sc->sc_config1 = SEEQ_CFG1_MULTICAST;
634	else
635		sc->sc_config1 = SEEQ_CFG1_BROADCAST;
636	sc->sc_config1 |= SEEQ_CFG1_STATION_ADDR0;
637	bus_space_write_2(iot, ioh, SEEQ_CONFIG1, sc->sc_config1);
638
639	/* Setup the Rx pointers */
640	sc->sc_rx_ptr = SEEQ_TX_BUFFER_SIZE;
641
642	bus_space_write_2(iot, ioh, SEEQ_RX_PTR, sc->sc_rx_ptr);
643	bus_space_write_2(iot, ioh, SEEQ_RX_END, sc->sc_rx_ptr >> 8);
644
645
646	/* Place a NULL header at the beginning of the receive area */
647	ea_writebuf(sc, NULL, sc->sc_rx_ptr, 0);
648
649	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
650	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
651
652
653	/* Turn on Rx */
654	sc->sc_command |= SEEQ_CMD_RX_INTEN;
655	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
656			  sc->sc_command | SEEQ_CMD_RX_ON);
657
658
659	/* Configure TX. */
660	dprintf(("Configuring tx...\n"));
661
662	bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
663
664	sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
665	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
666
667
668	/* Place a NULL header at the beginning of the transmit area */
669	ea_writebuf(sc, NULL, 0x0000, 0);
670
671	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
672	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
673
674	sc->sc_command |= SEEQ_CMD_TX_INTEN;
675	bus_space_write_2(iot, ioh, SEEQ_COMMAND, sc->sc_command);
676
677	/* TX_ON gets set by ea_txpacket when there's something to transmit. */
678
679
680	/* Set flags appropriately. */
681	ifp->if_flags |= IFF_RUNNING;
682	ifp->if_flags &= ~IFF_OACTIVE;
683
684	dprintf(("init: st=%04x\n",
685		 bus_space_read_2(iot, ioh, SEEQ_STATUS)));
686
687
688	/* And start output. */
689	ea_start(ifp);
690
691	splx(s);
692	return 0;
693}
694
695
696/*
697 * Start output on interface. Get datagrams from the queue and output them,
698 * giving the receiver a chance between datagrams. Call only from splnet or
699 * interrupt level!
700 */
701
702static void
703ea_start(struct ifnet *ifp)
704{
705	struct seeq8005_softc *sc = ifp->if_softc;
706	int s;
707
708	s = splnet();
709#ifdef SEEQ_TX_DEBUG
710	dprintf(("ea_start()...\n"));
711#endif
712
713	/* Don't do anything if output is active. */
714
715	if (ifp->if_flags & IFF_OACTIVE)
716		return;
717
718	/* Mark interface as output active */
719
720	ifp->if_flags |= IFF_OACTIVE;
721
722	/* tx packets */
723
724	eatxpacket(sc);
725	splx(s);
726}
727
728
729/*
730 * Transfer a packet to the interface buffer and start transmission
731 *
732 * Called at splnet()
733 */
734
735void
736eatxpacket(struct seeq8005_softc *sc)
737{
738	bus_space_tag_t iot = sc->sc_iot;
739	bus_space_handle_t ioh = sc->sc_ioh;
740	struct mbuf *m, *m0;
741	struct ifnet *ifp;
742	int len, nextpacket;
743	u_int8_t hdr[4];
744
745	ifp = &sc->sc_ethercom.ec_if;
746
747	/* Dequeue the next packet. */
748	IFQ_DEQUEUE(&ifp->if_snd, m0);
749
750	/* If there's nothing to send, return. */
751	if (!m0) {
752		ifp->if_flags &= ~IFF_OACTIVE;
753		sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
754		bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
755#ifdef SEEQ_TX_DEBUG
756		dprintf(("tx finished\n"));
757#endif
758		return;
759	}
760
761#if NBPFILTER > 0
762	/* Give the packet to the bpf, if any. */
763	if (ifp->if_bpf)
764		bpf_mtap(ifp->if_bpf, m0);
765#endif
766
767#ifdef SEEQ_TX_DEBUG
768	dprintf(("Tx new packet\n"));
769#endif
770
771	sc->sc_config2 &= ~SEEQ_CFG2_OUTPUT;
772	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
773
774	/*
775	 * Copy the frame to the start of the transmit area on the card,
776	 * leaving four bytes for the transmit header.
777	 */
778	len = 0;
779	for (m = m0; m; m = m->m_next) {
780		if (m->m_len == 0)
781			continue;
782		ea_writebuf(sc, mtod(m, caddr_t), 4 + len, m->m_len);
783		len += m->m_len;
784	}
785	m_freem(m0);
786
787
788	/* If packet size is odd round up to the next 16 bit boundry */
789	if (len % 2)
790		++len;
791
792	len = max(len, ETHER_MIN_LEN);
793
794	if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN))
795		log(LOG_WARNING, "%s: oversize packet = %d bytes\n",
796		    sc->sc_dev.dv_xname, len);
797
798#if 0 /*def SEEQ_TX_DEBUG*/
799	dprintf(("ea: xfr pkt length=%d...\n", len));
800
801	dprintf(("%s-->", ether_sprintf(sc->sc_pktbuf+6)));
802	dprintf(("%s\n", ether_sprintf(sc->sc_pktbuf)));
803#endif
804
805/*	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));*/
806
807	/* Follow it with a NULL packet header */
808	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
809	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
810
811
812	/* Write the packet header */
813
814	nextpacket = len + 4;
815	hdr[0] = (nextpacket >> 8) & 0xff;
816	hdr[1] = nextpacket & 0xff;
817	hdr[2] = SEEQ_PKTCMD_TX | SEEQ_PKTCMD_DATA_FOLLOWS |
818		SEEQ_TXCMD_XMIT_SUCCESS_INT | SEEQ_TXCMD_COLLISION_INT;
819	hdr[3] = 0; /* Status byte -- will be update by hardware. */
820	ea_writebuf(sc, hdr, 0x0000, 4);
821
822	bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
823
824/*	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));*/
825
826#ifdef SEEQ_PACKET_DEBUG
827	ea_dump_buffer(sc, 0);
828#endif
829
830
831	/* Now transmit the datagram. */
832/*	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));*/
833	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
834			  sc->sc_command | SEEQ_CMD_TX_ON);
835#ifdef SEEQ_TX_DEBUG
836	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));
837	dprintf(("tx: queued\n"));
838#endif
839}
840
841
842/*
843 * Ethernet controller interrupt.
844 */
845
846int
847seeq8005intr(void *arg)
848{
849	struct seeq8005_softc *sc = arg;
850	bus_space_tag_t iot = sc->sc_iot;
851	bus_space_handle_t ioh = sc->sc_ioh;
852	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
853	int status, s, handled;
854	u_int8_t txhdr[4];
855	u_int txstatus;
856
857	handled = 0;
858	dprintf(("eaintr: "));
859
860
861	/* Get the controller status */
862	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
863        dprintf(("st=%04x ", status));
864
865
866	/* Tx interrupt ? */
867	if (status & SEEQ_STATUS_TX_INT) {
868		dprintf(("txint "));
869		handled = 1;
870
871		/* Acknowledge the interrupt */
872		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
873				  sc->sc_command | SEEQ_CMD_TX_INTACK);
874
875		ea_readbuf(sc, txhdr, 0x0000, 4);
876
877#ifdef SEEQ_TX_DEBUG
878		dprintf(("txstatus=%02x %02x %02x %02x\n",
879			 txhdr[0], txhdr[1], txhdr[2], txhdr[3]));
880#endif
881		txstatus = txhdr[3];
882
883		/*
884		 * Did it succeed ? Did we collide ?
885		 *
886		 * The exact proceedure here is not clear. We should get
887		 * an interrupt on a sucessfull tx or on a collision.
888		 * The done flag is set after successfull tx or 16 collisions
889		 * We should thus get a interrupt for each of collision
890		 * and the done bit should not be set. However it does appear
891		 * to be set at the same time as the collision bit ...
892		 *
893		 * So we will count collisions and output errors and will
894		 * assume that if the done bit is set the packet was
895		 * transmitted. Stats may be wrong if 16 collisions occur on
896		 * a packet as the done flag should be set but the packet
897		 * may not have been transmitted. so the output count might
898		 * not require incrementing if the 16 collisions flags is
899		 * set. I don;t know abou this until it happens.
900		 */
901
902		if (txstatus & SEEQ_TXSTAT_COLLISION)
903			ifp->if_collisions++;
904		else if (txstatus &
905		    (SEEQ_TXSTAT_BABBLE | SEEQ_TXSTAT_COLLISION16))
906			ifp->if_oerrors++;
907
908		if (txstatus & SEEQ_PKTSTAT_DONE) {
909			ifp->if_opackets++;
910
911			/* Tx next packet */
912
913			s = splnet();
914			eatxpacket(sc);
915			splx(s);
916		}
917	}
918
919
920	/* Rx interrupt ? */
921	if (status & SEEQ_STATUS_RX_INT) {
922		dprintf(("rxint "));
923		handled = 1;
924
925		/* Acknowledge the interrupt */
926		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
927				  sc->sc_command | SEEQ_CMD_RX_INTACK);
928
929		/* Install a watchdog timer needed atm to fixed rx lockups */
930		ifp->if_timer = SEEQ_TIMEOUT;
931
932		/* Processes the received packets */
933		eagetpackets(sc);
934
935
936#if 0
937		/* Make sure the receiver is on */
938		if ((status & SEEQ_STATUS_RX_ON) == 0) {
939			bus_space_write_2(iot, ioh, SEEQ_COMMAND,
940					  sc->sc_command | SEEQ_CMD_RX_ON);
941			printf("rxintr: rx is off st=%04x\n",status);
942		}
943#endif
944	}
945
946#ifdef SEEQ_DEBUG
947	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
948        dprintf(("st=%04x\n", status));
949#endif
950
951	return handled;
952}
953
954
955void
956eagetpackets(struct seeq8005_softc *sc)
957{
958	bus_space_tag_t iot = sc->sc_iot;
959	bus_space_handle_t ioh = sc->sc_ioh;
960	u_int addr;
961	int len;
962	int ctrl;
963	int ptr;
964	int pack;
965	int status;
966	u_int8_t rxhdr[4];
967	struct ifnet *ifp;
968
969	ifp = &sc->sc_ethercom.ec_if;
970
971
972	/* We start from the last rx pointer position */
973	addr = sc->sc_rx_ptr;
974	sc->sc_config2 &= ~SEEQ_CFG2_OUTPUT;
975	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
976
977	do {
978		/* Read rx header */
979		ea_readbuf(sc, rxhdr, addr, 4);
980
981		/* Split the packet header */
982		ptr = (rxhdr[0] << 8) | rxhdr[1];
983		ctrl = rxhdr[2];
984		status = rxhdr[3];
985
986#ifdef SEEQ_RX_DEBUG
987		dprintf(("addr=%04x ptr=%04x ctrl=%02x status=%02x\n",
988			 addr, ptr, ctrl, status));
989#endif
990
991
992		/* Zero packet ptr ? then must be null header so exit */
993		if (ptr == 0) break;
994
995
996		/* Get packet length */
997       		len = (ptr - addr) - 4;
998
999		if (len < 0)
1000			len += SEEQ_RX_BUFFER_SIZE;
1001
1002#ifdef SEEQ_RX_DEBUG
1003		dprintf(("len=%04x\n", len));
1004#endif
1005
1006
1007		/* Has the packet rx completed ? if not then exit */
1008		if ((status & SEEQ_PKTSTAT_DONE) == 0)
1009			break;
1010
1011		/*
1012		 * Did we have any errors? then note error and go to
1013		 * next packet
1014		 */
1015		if (__predict_false(status & 0x0f)) {
1016			++ifp->if_ierrors;
1017			log(LOG_WARNING,
1018			    "%s: rx packet error (%02x) - dropping packet\n",
1019			    sc->sc_dev.dv_xname, status & 0x0f);
1020			sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
1021			bus_space_write_2(iot, ioh, SEEQ_CONFIG2,
1022					  sc->sc_config2);
1023			ea_init(ifp);
1024			return;
1025		}
1026
1027		/*
1028		 * Is the packet too big ? - this will probably be trapped
1029		 * above as a receive error
1030		 */
1031		if (__predict_false(len > (ETHER_MAX_LEN - ETHER_CRC_LEN))) {
1032			++ifp->if_ierrors;
1033			log(LOG_WARNING, "%s: rx packet size error len=%d\n",
1034			    sc->sc_dev.dv_xname, len);
1035			sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
1036			bus_space_write_2(iot, ioh, SEEQ_CONFIG2,
1037					  sc->sc_config2);
1038			ea_init(ifp);
1039			return;
1040		}
1041
1042		ifp->if_ipackets++;
1043		/* Pass data up to upper levels. */
1044		earead(sc, addr + 4, len);
1045
1046		addr = ptr;
1047		++pack;
1048	} while (len != 0);
1049
1050	sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
1051	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
1052
1053#ifdef SEEQ_RX_DEBUG
1054	dprintf(("new rx ptr=%04x\n", addr));
1055#endif
1056
1057
1058	/* Store new rx pointer */
1059	sc->sc_rx_ptr = addr;
1060	bus_space_write_2(iot, ioh, SEEQ_RX_END, sc->sc_rx_ptr >> 8);
1061
1062	/* Make sure the receiver is on */
1063	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
1064			  sc->sc_command | SEEQ_CMD_RX_ON);
1065
1066}
1067
1068
1069/*
1070 * Pass a packet up to the higher levels.
1071 */
1072
1073static void
1074earead(struct seeq8005_softc *sc, int addr, int len)
1075{
1076	struct mbuf *m;
1077	struct ifnet *ifp;
1078
1079	ifp = &sc->sc_ethercom.ec_if;
1080
1081	/* Pull packet off interface. */
1082	m = eaget(sc, addr, len, ifp);
1083	if (m == 0)
1084		return;
1085
1086#ifdef SEEQ_RX_DEBUG
1087	dprintf(("%s-->", ether_sprintf(eh->ether_shost)));
1088	dprintf(("%s\n", ether_sprintf(eh->ether_dhost)));
1089#endif
1090
1091#if NBPFILTER > 0
1092	/*
1093	 * Check if there's a BPF listener on this interface.
1094	 * If so, hand off the raw packet to bpf.
1095	 */
1096	if (ifp->if_bpf)
1097		bpf_mtap(ifp->if_bpf, m);
1098#endif
1099
1100	(*ifp->if_input)(ifp, m);
1101}
1102
1103/*
1104 * Pull read data off a interface.  Len is length of data, with local net
1105 * header stripped.  We copy the data into mbufs.  When full cluster sized
1106 * units are present we copy into clusters.
1107 */
1108
1109struct mbuf *
1110eaget(struct seeq8005_softc *sc, int addr, int totlen, struct ifnet *ifp)
1111{
1112        struct mbuf *top, **mp, *m;
1113        int len;
1114        u_int cp, epkt;
1115
1116        cp = addr;
1117        epkt = cp + totlen;
1118
1119        MGETHDR(m, M_DONTWAIT, MT_DATA);
1120        if (m == 0)
1121                return 0;
1122        m->m_pkthdr.rcvif = ifp;
1123        m->m_pkthdr.len = totlen;
1124        m->m_len = MHLEN;
1125        top = 0;
1126        mp = &top;
1127
1128        while (totlen > 0) {
1129                if (top) {
1130                        MGET(m, M_DONTWAIT, MT_DATA);
1131                        if (m == 0) {
1132                                m_freem(top);
1133                                return 0;
1134                        }
1135                        m->m_len = MLEN;
1136                }
1137                len = min(totlen, epkt - cp);
1138                if (len >= MINCLSIZE) {
1139                        MCLGET(m, M_DONTWAIT);
1140                        if (m->m_flags & M_EXT)
1141                                m->m_len = len = min(len, MCLBYTES);
1142                        else
1143                                len = m->m_len;
1144                } else {
1145                        /*
1146                         * Place initial small packet/header at end of mbuf.
1147                         */
1148                        if (len < m->m_len) {
1149                                if (top == 0 && len + max_linkhdr <= m->m_len)
1150                                        m->m_data += max_linkhdr;
1151                                m->m_len = len;
1152                        } else
1153                                len = m->m_len;
1154                }
1155		if (top == 0) {
1156			/* Make sure the payload is aligned */
1157			caddr_t newdata = (caddr_t)
1158			    ALIGN(m->m_data + sizeof(struct ether_header)) -
1159			    sizeof(struct ether_header);
1160			len -= newdata - m->m_data;
1161			m->m_len = len;
1162			m->m_data = newdata;
1163		}
1164                ea_readbuf(sc, mtod(m, u_char *),
1165			   cp < SEEQ_MAX_BUFFER_SIZE ? cp : cp - SEEQ_RX_BUFFER_SIZE,
1166			   len);
1167                cp += len;
1168                *mp = m;
1169                mp = &m->m_next;
1170                totlen -= len;
1171                if (cp == epkt)
1172                        cp = addr;
1173        }
1174
1175        return top;
1176}
1177
1178/*
1179 * Process an ioctl request.  Mostly boilerplate.
1180 */
1181static int
1182ea_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1183{
1184	struct seeq8005_softc *sc = ifp->if_softc;
1185	int s, error = 0;
1186
1187	s = splnet();
1188	switch (cmd) {
1189
1190	default:
1191		error = ether_ioctl(ifp, cmd, data);
1192		if (error == ENETRESET) {
1193			/*
1194			 * Multicast list has changed; set the hardware filter
1195			 * accordingly.
1196			 */
1197			ea_mc_reset(sc);
1198			error = 0;
1199		}
1200		break;
1201	}
1202
1203	splx(s);
1204	return error;
1205}
1206
1207/* Must be called at splnet() */
1208static void
1209ea_mc_reset(struct seeq8005_softc *sc)
1210{
1211	struct ether_multi *enm;
1212	struct ether_multistep step;
1213	int naddr, maxaddrs;
1214
1215	naddr = 0;
1216	maxaddrs = (sc->sc_flags & SEEQ8005_80C04) ? 5 : 0;
1217	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1218	while (enm != NULL) {
1219		/* Have we got space? */
1220		if (naddr >= maxaddrs ||
1221		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
1222			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
1223			ea_ioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS, NULL);
1224			return;
1225		}
1226		ea_set_address(sc, naddr, enm->enm_addrlo);
1227		sc->sc_config1 |= SEEQ_CFG1_STATION_ADDR0 << naddr;
1228		naddr++;
1229		ETHER_NEXT_MULTI(step, enm);
1230	}
1231	for (; naddr < maxaddrs; naddr++)
1232		sc->sc_config1 &= ~(SEEQ_CFG1_STATION_ADDR0 << naddr);
1233	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_CONFIG1,
1234			  sc->sc_config1);
1235}
1236
1237/*
1238 * Device timeout routine.
1239 *
1240 * Ok I am not sure exactly how the device timeout should work....
1241 * Currently what will happens is that that the device timeout is only
1242 * set when a packet it received. This indicates we are on an active
1243 * network and thus we should expect more packets. If non arrive in
1244 * in the timeout period then we reinitialise as we may have jammed.
1245 * We zero the timeout at this point so that we don't end up with
1246 * an endless stream of timeouts if the network goes down.
1247 */
1248
1249static void
1250ea_watchdog(struct ifnet *ifp)
1251{
1252	struct seeq8005_softc *sc = ifp->if_softc;
1253
1254	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1255	ifp->if_oerrors++;
1256	dprintf(("ea_watchdog: "));
1257	dprintf(("st=%04x\n",
1258		 bus_space_read_2(sc->sc_iot, sc->sc_ioh, SEEQ_STATUS)));
1259
1260	/* Kick the interface */
1261
1262	ea_init(ifp);
1263
1264/*	ifp->if_timer = SEEQ_TIMEOUT;*/
1265	ifp->if_timer = 0;
1266}
1267
1268/* End of if_ea.c */
1269