if_fxp.c revision 13638
1/*
2 * Copyright (c) 1995, David Greenman
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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by David Greenman.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$Id: if_fxp.c,v 1.9 1996/01/23 21:47:03 se Exp $
33 */
34
35/*
36 * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
37 */
38
39#include "bpfilter.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/ioctl.h>
44#include <sys/mbuf.h>
45#include <sys/malloc.h>
46#include <sys/kernel.h>
47#include <sys/devconf.h>
48#include <sys/syslog.h>
49
50#include <net/if.h>
51#include <net/if_dl.h>
52#include <net/if_types.h>
53
54#ifdef INET
55#include <netinet/in.h>
56#include <netinet/in_systm.h>
57#include <netinet/in_var.h>
58#include <netinet/ip.h>
59#include <netinet/if_ether.h>
60#endif
61
62#ifdef IPX
63#include <netipx/ipx.h>
64#include <netipx/ipx_if.h>
65#endif
66
67#ifdef NS
68#include <netns/ns.h>
69#include <netns/ns_if.h>
70#endif
71
72#if NBPFILTER > 0
73#include <net/bpf.h>
74#include <net/bpfdesc.h>
75#endif
76
77#include <vm/vm.h>		/* for vtophys */
78#include <vm/vm_param.h>	/* for vtophys */
79#include <vm/pmap.h>		/* for vtophys */
80#include <machine/clock.h>	/* for DELAY */
81
82#include <pci/pcivar.h>
83#include <pci/if_fxpreg.h>
84
85struct fxp_softc {
86	struct arpcom arpcom;		/* per-interface network data */
87	caddr_t bpf;			/* BPF token */
88	struct fxp_csr *csr;		/* control/status registers */
89	struct fxp_cb_tx *cbl_base;	/* base of TxCB list */
90	struct fxp_cb_tx *cbl_first;	/* first active TxCB in list */
91	struct fxp_cb_tx *cbl_last;	/* last active TxCB in list */
92	struct mbuf *rfa_headm;		/* first mbuf in receive frame area */
93	struct mbuf *rfa_tailm;		/* last mbuf in receive frame area */
94	struct fxp_stats *fxp_stats;	/* Pointer to interface stats */
95	int tx_queued;			/* # of active TxCB's */
96	int promisc_mode;		/* promiscuous mode enabled */
97};
98
99#include "fxp.h"
100static struct fxp_softc *fxp_sc[NFXP];	/* XXX Yuck */
101
102static u_long fxp_count;
103
104/*
105 * Template for default configuration parameters.
106 * See struct fxp_cb_config for the bit definitions.
107 */
108static u_char fxp_cb_config_template[] = {
109	0x0, 0x0,		/* cb_status */
110	0x80, 0x2,		/* cb_command */
111	0xff, 0xff, 0xff, 0xff,	/* link_addr */
112	0x16,	/*  0 */
113	0x8,	/*  1 */
114	0x0,	/*  2 */
115	0x0,	/*  3 */
116	0x0,	/*  4 */
117	0x80,	/*  5 */
118	0xb2,	/*  6 */
119	0x3,	/*  7 */
120	0x1,	/*  8 */
121	0x0,	/*  9 */
122	0x26,	/* 10 */
123	0x0,	/* 11 */
124	0x60,	/* 12 */
125	0x0,	/* 13 */
126	0xf2,	/* 14 */
127	0x48,	/* 15 */
128	0x0,	/* 16 */
129	0x40,	/* 17 */
130	0xf3,	/* 18 */
131	0x0,	/* 19 */
132	0x3f,	/* 20 */
133	0x5,	/* 21 */
134	0x0, 0x0
135};
136
137static inline int fxp_scb_wait	__P((struct fxp_csr *));
138static char *fxp_probe		__P((pcici_t, pcidi_t));
139static void fxp_attach		__P((pcici_t, int));
140static int fxp_shutdown		__P((struct kern_devconf *, int));
141static void fxp_intr		__P((void *));
142static void fxp_start		__P((struct ifnet *));
143static int fxp_ioctl		__P((struct ifnet *, int, caddr_t));
144static void fxp_init		__P((struct ifnet *));
145static void fxp_stop		__P((struct fxp_softc *));
146static void fxp_watchdog	__P((struct ifnet *));
147static void fxp_get_macaddr	__P((struct fxp_softc *));
148static int fxp_add_rfabuf	__P((struct fxp_softc *, struct mbuf *));
149
150timeout_t fxp_stats_update;
151
152static struct pci_device fxp_device = {
153	"fxp",
154	fxp_probe,
155	fxp_attach,
156	&fxp_count,
157	fxp_shutdown
158};
159DATA_SET(pcidevice_set, fxp_device);
160
161/*
162 * Number of transmit control blocks. This determines the number
163 * of transmit buffers that can be chained in the CB list.
164 * This must be a power of two.
165 */
166#define FXP_NTXCB	64
167
168/*
169 * TxCB list index mask. This is used to do list wrap-around.
170 */
171#define FXP_TXCB_MASK	(FXP_NTXCB - 1)
172
173/*
174 * Number of DMA segments in a TxCB. Note that this is carefully
175 * chosen to make the total struct size an even power of two. It's
176 * critical that no TxCB be split across a page boundry since
177 * no attempt is made to allocate physically contiguous memory.
178 *
179 * XXX - don't forget to change the hard-coded constant in the
180 * fxp_cb_tx struct (defined in if_fxpreg.h), too!
181 */
182#define FXP_NTXSEG	29
183
184/*
185 * Number of receive frame area buffers. These are large so chose
186 * wisely.
187 */
188#define FXP_NRFABUFS	32
189
190/*
191 * Wait for the previous command to be accepted (but not necessarily
192 * completed).
193 */
194static inline int
195fxp_scb_wait(csr)
196	struct fxp_csr *csr;
197{
198	int i = 10000;
199
200	while ((csr->scb_command & FXP_SCB_COMMAND_MASK) && --i);
201	return (i);
202}
203
204/*
205 * Return identification string if this is device is ours.
206 */
207static char *
208fxp_probe(config_id, device_id)
209	pcici_t config_id;
210	pcidi_t device_id;
211{
212	if (((device_id & 0xffff) == FXP_VENDORID_INTEL) &&
213	    ((device_id >> 16) & 0xffff) == FXP_DEVICEID_i82557)
214		return ("Intel EtherExpress Pro/100B Fast Ethernet");
215
216	return NULL;
217}
218
219/*
220 * Allocate data structures and attach the device.
221 */
222static void
223fxp_attach(config_id, unit)
224	pcici_t config_id;
225	int unit;
226{
227	struct fxp_softc *sc;
228	struct ifnet *ifp;
229	vm_offset_t pbase;
230	int s, i;
231
232	sc = malloc(sizeof(struct fxp_softc), M_DEVBUF, M_NOWAIT);
233	if (sc == NULL)
234		return;
235	bzero(sc, sizeof(struct fxp_softc));
236
237	s = splimp();
238
239	/*
240	 * Map control/status registers.
241	 */
242	if (!pci_map_mem(config_id, FXP_PCI_MMBA,
243	    (vm_offset_t *)&sc->csr, &pbase)) {
244		printf("fxp%d: couldn't map memory\n", unit);
245		goto fail;
246	}
247
248	/*
249	 * Issue a software reset.
250	 */
251	sc->csr->port = 0;
252	DELAY(10);
253
254	/*
255	 * Allocate our interrupt.
256	 */
257	if (!pci_map_int(config_id, fxp_intr, sc, &net_imask)) {
258		printf("fxp%d: couldn't map interrupt\n", unit);
259		goto fail;
260	}
261
262	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
263	    M_DEVBUF, M_NOWAIT);
264	if (sc->cbl_base == NULL)
265		goto malloc_fail;
266
267	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT);
268	if (sc->fxp_stats == NULL)
269		goto malloc_fail;
270	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
271
272	/*
273	 * Pre-allocate our receive buffers.
274	 */
275	for (i = 0; i < FXP_NRFABUFS; i++) {
276		if (fxp_add_rfabuf(sc, NULL) != 0) {
277			goto malloc_fail;
278		}
279	}
280
281	fxp_sc[unit] = sc;
282
283	ifp = &sc->arpcom.ac_if;
284	ifp->if_unit = unit;
285	ifp->if_name = "fxp";
286	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
287	ifp->if_ioctl = fxp_ioctl;
288	ifp->if_output = ether_output;
289	ifp->if_start = fxp_start;
290	ifp->if_watchdog = fxp_watchdog;
291
292	fxp_get_macaddr(sc);
293	printf("fxp%d: Ethernet address %6D\n", unit,
294	    sc->arpcom.ac_enaddr, ":");
295
296	/*
297	 * Attach the interface.
298	 */
299	if_attach(ifp);
300#if NBPFILTER > 0
301	bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
302#endif
303	splx(s);
304	return;
305
306malloc_fail:
307	printf("fxp%d: Failed to malloc memory\n", unit);
308	(void) pci_unmap_int(config_id);
309	if (sc && sc->cbl_base)
310		free(sc->cbl_base, M_DEVBUF);
311	if (sc && sc->fxp_stats)
312		free(sc->fxp_stats, M_DEVBUF);
313	/* frees entire chain */
314	if (sc && sc->rfa_headm)
315		m_freem(sc->rfa_headm);
316fail:
317	if (sc)
318		free(sc, M_DEVBUF);
319	splx(s);
320}
321
322/*
323 * Read station (MAC) address from serial EEPROM. Basically, you
324 * manually shift in the read opcode (one bit at a time) and then
325 * shift in the address, and then you shift out the data (all of
326 * this one bit at a time). The word size is 16 bits, so you have
327 * to provide the address for every 16 bits of data. The MAC address
328 * is in the first 3 words (6 bytes total).
329 */
330static void
331fxp_get_macaddr(sc)
332	struct fxp_softc *sc;
333{
334	struct fxp_csr *csr;
335	u_short reg, *data;
336	int i, x;
337
338	csr = sc->csr;
339	data = (u_short *)sc->arpcom.ac_enaddr;
340
341	for (i = 0; i < 3; i++) {
342		csr->eeprom_control = FXP_EEPROM_EECS;
343		/*
344		 * Shift in read opcode.
345		 */
346		for (x = 3; x > 0; x--) {
347			if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
348				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
349			} else {
350				reg = FXP_EEPROM_EECS;
351			}
352			csr->eeprom_control = reg;
353			csr->eeprom_control = reg | FXP_EEPROM_EESK;
354			DELAY(1);
355			csr->eeprom_control = reg;
356			DELAY(1);
357		}
358		/*
359		 * Shift in address.
360		 */
361		for (x = 6; x > 0; x--) {
362			if (i & (1 << (x - 1))) {
363				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
364			} else {
365				reg = FXP_EEPROM_EECS;
366			}
367			csr->eeprom_control = reg;
368			csr->eeprom_control = reg | FXP_EEPROM_EESK;
369			DELAY(1);
370			csr->eeprom_control = reg;
371			DELAY(1);
372		}
373		reg = FXP_EEPROM_EECS;
374		data[i] = 0;
375		/*
376		 * Shift out data.
377		 */
378		for (x = 16; x > 0; x--) {
379			csr->eeprom_control = reg | FXP_EEPROM_EESK;
380			DELAY(1);
381			if (csr->eeprom_control & FXP_EEPROM_EEDO)
382				data[i] |= (1 << (x - 1));
383			csr->eeprom_control = reg;
384			DELAY(1);
385		}
386		csr->eeprom_control = 0;
387		DELAY(1);
388	}
389}
390
391/*
392 * Device shutdown routine. Usually called at system shutdown. The
393 * main purpose of this routine is to shut off receiver DMA so that
394 * kernel memory doesn't get clobbered during warmboot.
395 */
396static int
397fxp_shutdown(kdc, force)
398	struct kern_devconf *kdc;
399	int force;
400{
401	struct fxp_softc *sc = fxp_sc[kdc->kdc_unit];
402
403	fxp_stop(sc);
404
405	(void) dev_detach(kdc);
406	return 0;
407}
408
409/*
410 * Start packet transmission on the interface.
411 */
412static void
413fxp_start(ifp)
414	struct ifnet *ifp;
415{
416	struct fxp_softc *sc = (struct fxp_softc *)ifp;
417	struct fxp_csr *csr = sc->csr;
418	struct fxp_cb_tx *txp;
419	struct mbuf *m, *mb_head;
420	int segment;
421
422txloop:
423	/*
424	 * See if a TxCB is available. If not, indicate this to the
425	 * outside world and exit.
426	 */
427	if (sc->tx_queued >= FXP_NTXCB) {
428		ifp->if_flags |= IFF_OACTIVE;
429		return;
430	}
431	/*
432	 * Grab a packet to transmit.
433	 */
434	IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, mb_head);
435	if (mb_head == NULL) {
436		/*
437		 * No more packets to send.
438		 */
439		return;
440	}
441
442	/*
443	 * Get pointer to next available (unused) descriptor.
444	 */
445	txp = sc->cbl_last->next;
446
447	/*
448	 * Go through each of the mbufs in the chain and initialize
449	 * the transmit buffers descriptors with the physical address
450	 * and size of the mbuf.
451	 */
452tbdinit:
453	for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
454		if (m->m_len != 0) {
455			if (segment == FXP_NTXSEG)
456				break;
457			txp->tbd[segment].tb_addr =
458			    vtophys(mtod(m, vm_offset_t));
459			txp->tbd[segment].tb_size = m->m_len;
460			segment++;
461		}
462	}
463	if (m != NULL && segment == FXP_NTXSEG) {
464		struct mbuf *mn;
465
466		/*
467		 * We ran out of segments. We have to recopy this mbuf
468		 * chain first.
469		 */
470		MGETHDR(mn, M_DONTWAIT, MT_DATA);
471		if (mn == NULL) {
472			m_freem(mb_head);
473			return;
474		}
475		if (mb_head->m_pkthdr.len > MHLEN) {
476			MCLGET(mn, M_DONTWAIT);
477			if ((mn->m_flags & M_EXT) == 0) {
478				m_freem(mn);
479				m_freem(mb_head);
480				return;
481			}
482		}
483		m_copydata(mb_head, 0, mb_head->m_pkthdr.len, mtod(mn, caddr_t));
484		mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
485		m_freem(mb_head);
486		mb_head = mn;
487		goto tbdinit;
488	}
489
490	txp->tbd_number = segment;
491
492	/*
493	 * Finish the initialization of this TxCB.
494	 */
495	txp->cb_status = 0;
496	txp->cb_command =
497	    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S;
498	txp->tx_threshold = 16;	/* bytes*8 */
499	txp->mb_head = mb_head;
500
501	/*
502	 * Advance the end-of-list forward.
503	 */
504	sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
505	sc->cbl_last = txp;
506
507	/*
508	 * If no packets were previously queued then advance the first
509	 * pointer to this TxCB.
510	 */
511	if (sc->tx_queued++ == 0) {
512		sc->cbl_first = txp;
513	}
514
515	if (!fxp_scb_wait(csr)) {
516		/*
517		 * Hmmm, card has gone out to lunch
518		 */
519		fxp_init(ifp);
520		goto txloop;
521	}
522
523	/*
524	 * Resume transmission if suspended.
525	 */
526	csr->scb_command = FXP_SCB_COMMAND_CU_RESUME;
527
528#if NBPFILTER > 0
529	/*
530	 * Pass packet to bpf if there is a listener.
531	 */
532	if (sc->bpf != NULL)
533		bpf_mtap(sc->bpf, mb_head);
534#endif
535	/*
536	 * Set a 5 second timer just in case we don't hear from the
537	 * card again.
538	 */
539	ifp->if_timer = 5;
540
541	goto txloop;
542}
543
544/*
545 * Process interface interrupts. Returns 1 if the interrupt
546 * was handled, 0 if it wasn't.
547 */
548static void
549fxp_intr(arg)
550	void *arg;
551{
552	struct fxp_softc *sc = arg;
553	struct fxp_csr *csr = sc->csr;
554	struct ifnet *ifp = &sc->arpcom.ac_if;
555	u_char statack;
556
557	while ((statack = csr->scb_statack) != 0) {
558		/*
559		 * First ACK all the interrupts in this pass.
560		 */
561		csr->scb_statack = statack;
562
563		/*
564		 * Free any finished transmit mbuf chains.
565		 */
566		if (statack & FXP_SCB_STATACK_CNA) {
567			struct fxp_cb_tx *txp;
568
569			for (txp = sc->cbl_first;
570			    (txp->cb_status & FXP_CB_STATUS_C) &&
571			    txp->mb_head != NULL;
572			    txp = txp->next) {
573				m_freem(txp->mb_head);
574				txp->mb_head = NULL;
575				sc->tx_queued--;
576			}
577			sc->cbl_first = txp;
578			/*
579			 * We unconditionally clear IFF_OACTIVE since it
580			 * doesn't hurt to do so even if the tx queue is
581			 * still full - it will just get set again in
582			 * fxp_start(). If we get a CNA interrupt, it is
583			 * (almost?) certain that we've freed up space for
584			 * at least one more packet.
585			 */
586			ifp->if_flags &= ~IFF_OACTIVE;
587			/*
588			 * Clear watchdog timer. It may or may not be set
589			 * again in fxp_start().
590			 */
591			ifp->if_timer = 0;
592			fxp_start(ifp);
593		}
594		/*
595		 * Process receiver interrupts. If a no-resource (RNR)
596		 * condition exists, get whatever packets we can and
597		 * re-start the receiver.
598		 */
599		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
600			struct mbuf *m;
601			struct fxp_rfa *rfa;
602rcvloop:
603			m = sc->rfa_headm;
604			rfa = (struct fxp_rfa *)m->m_ext.ext_buf;
605
606			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
607				/*
608				 * Remove first packet from the chain.
609				 */
610				sc->rfa_headm = m->m_next;
611				m->m_next = NULL;
612
613				/*
614				 * Add a new buffer to the receive chain. If this
615				 * fails, the old buffer is recycled instead.
616				 */
617				if (fxp_add_rfabuf(sc, m) == 0) {
618					struct ether_header *eh;
619					u_short total_len;
620
621					total_len = rfa->actual_size & (MCLBYTES - 1);
622					m->m_pkthdr.rcvif = ifp;
623					m->m_pkthdr.len = m->m_len = total_len -
624					    sizeof(struct ether_header);
625					eh = mtod(m, struct ether_header *);
626#if NBPFILTER > 0
627					if (sc->bpf != NULL) {
628						bpf_tap(sc->bpf, mtod(m, caddr_t), total_len);
629						/*
630						 * Only pass this packet up if it is for us.
631						 */
632						if ((ifp->if_flags & IFF_PROMISC) &&
633						    (rfa->rfa_status & FXP_RFA_STATUS_IAMATCH) &&
634						    (eh->ether_dhost[0] & 1) == 0) {
635							m_freem(m);
636							goto rcvloop;
637						}
638					}
639#endif
640					m->m_data += sizeof(struct ether_header);
641					ether_input(ifp, eh, m);
642				}
643				goto rcvloop;
644			}
645			if (statack & FXP_SCB_STATACK_RNR) {
646				struct fxp_csr *csr = sc->csr;
647
648				(void) fxp_scb_wait(csr);
649				csr->scb_general = vtophys(sc->rfa_headm->m_ext.ext_buf);
650				csr->scb_command = FXP_SCB_COMMAND_RU_START;
651			}
652		}
653	}
654}
655
656/*
657 * Update packet in/out/collision statistics. The i82557 doesn't
658 * allow you to access these counters without doing a fairly
659 * expensive DMA to get _all_ of the statistics it maintains, so
660 * we do this operation here only once per second. The statistics
661 * counters in the kernel are updated from the previous dump-stats
662 * DMA and then a new dump-stats DMA is started. The on-chip
663 * counters are zeroed when the DMA completes. If we can't start
664 * the DMA immediately, we don't wait - we just prepare to read
665 * them again next time.
666 */
667void
668fxp_stats_update(arg)
669	void *arg;
670{
671	struct fxp_softc *sc = arg;
672	struct ifnet *ifp = &sc->arpcom.ac_if;
673	struct fxp_stats *sp = sc->fxp_stats;
674
675	ifp->if_opackets += sp->tx_good;
676	ifp->if_collisions += sp->tx_total_collisions;
677	ifp->if_ipackets += sp->rx_good;
678	ifp->if_ierrors +=
679	    sp->rx_crc_errors +
680	    sp->rx_alignment_errors +
681	    sp->rx_rnr_errors +
682	    sp->rx_overrun_errors +
683	    sp->rx_shortframes;
684	/*
685	 * If there is no pending command, start another stats
686	 * dump. Otherwise punt for now.
687	 */
688	if ((sc->csr->scb_command & FXP_SCB_COMMAND_MASK) == 0) {
689		/*
690		 * Start another stats dump. By waiting for it to be
691		 * accepted, we avoid having to do splhigh locking when
692		 * writing scb_command in other parts of the driver.
693		 */
694		sc->csr->scb_command = FXP_SCB_COMMAND_CU_DUMPRESET;
695		(void) fxp_scb_wait(sc->csr);
696	} else {
697		/*
698		 * A previous command is still waiting to be accepted.
699		 * Just zero our copy of the stats and wait for the
700		 * next timer event to update them.
701		 */
702		sp->tx_good = 0;
703		sp->tx_total_collisions = 0;
704
705		sp->rx_good = 0;
706		sp->rx_crc_errors = 0;
707		sp->rx_alignment_errors = 0;
708		sp->rx_rnr_errors = 0;
709		sp->rx_overrun_errors = 0;
710		sp->rx_shortframes = 0;;
711	}
712	/*
713	 * Schedule another timeout one second from now.
714	 */
715	timeout(fxp_stats_update, sc, hz);
716}
717
718/*
719 * Stop the interface. Cancels the statistics updater and resets
720 * the interface.
721 */
722static void
723fxp_stop(sc)
724	struct fxp_softc *sc;
725{
726	struct ifnet *ifp = &sc->arpcom.ac_if;
727	struct fxp_cb_tx *txp;
728	int i;
729
730	/*
731	 * Cancel stats updater.
732	 */
733	untimeout(fxp_stats_update, sc);
734
735	/*
736	 * Issue software reset
737	 */
738	sc->csr->port = 0;
739	DELAY(10);
740
741	/*
742	 * Release any xmit buffers.
743	 */
744	for (txp = sc->cbl_first; txp != NULL && txp->mb_head != NULL;
745	    txp = txp->next) {
746		m_freem(txp->mb_head);
747		txp->mb_head = NULL;
748	}
749	sc->tx_queued = 0;
750
751	/*
752	 * Free all the receive buffers then reallocate/reinitialize
753	 */
754	if (sc->rfa_headm != NULL)
755		m_freem(sc->rfa_headm);
756	sc->rfa_headm = NULL;
757	sc->rfa_tailm = NULL;
758	for (i = 0; i < FXP_NRFABUFS; i++) {
759		if (fxp_add_rfabuf(sc, NULL) != 0) {
760			/*
761			 * This "can't happen" - we're at splimp()
762			 * and we just freed all the buffers we need
763			 * above.
764			 */
765			panic("fxp_stop: no buffers!");
766		}
767	}
768
769	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
770	ifp->if_timer = 0;
771}
772
773/*
774 * Watchdog/transmission transmit timeout handler. Called when a
775 * transmission is started on the interface, but no interrupt is
776 * received before the timeout. This usually indicates that the
777 * card has wedged for some reason.
778 */
779static void
780fxp_watchdog(ifp)
781	struct ifnet *ifp;
782{
783	log(LOG_ERR, "fxp%d: device timeout\n", ifp->if_unit);
784	ifp->if_oerrors++;
785
786	fxp_init(ifp);
787}
788
789static void
790fxp_init(ifp)
791	struct ifnet *ifp;
792{
793	struct fxp_softc *sc = (struct fxp_softc *)ifp;
794	struct fxp_cb_config *cbp;
795	struct fxp_cb_ias *cb_ias;
796	struct fxp_cb_tx *txp;
797	struct fxp_csr *csr = sc->csr;
798	int i, s, mcast, prm;
799
800	s = splimp();
801	/*
802	 * Cancel any pending I/O
803	 */
804	fxp_stop(sc);
805
806	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
807	sc->promisc_mode = prm;
808	/*
809	 * Sleeze out here and enable reception of all multicasts if
810	 * multicasts are enabled. Ideally, we'd program the multicast
811	 * address filter to only accept specific multicasts.
812	 */
813	mcast = (ifp->if_flags & (IFF_MULTICAST|IFF_ALLMULTI)) ? 1 : 0;
814
815	/*
816	 * Initialize base of CBL and RFA memory. Loading with zero
817	 * sets it up for regular linear addressing.
818	 */
819	csr->scb_general = 0;
820	csr->scb_command = FXP_SCB_COMMAND_CU_BASE;
821
822	(void) fxp_scb_wait(csr);
823	csr->scb_command = FXP_SCB_COMMAND_RU_BASE;
824
825	/*
826	 * Initialize base of dump-stats buffer.
827	 */
828	(void) fxp_scb_wait(csr);
829	csr->scb_general = vtophys(sc->fxp_stats);
830	csr->scb_command = FXP_SCB_COMMAND_CU_DUMP_ADR;
831
832	/*
833	 * We temporarily use memory that contains the TxCB list to
834	 * construct the config CB. The TxCB list memory is rebuilt
835	 * later.
836	 */
837	cbp = (struct fxp_cb_config *) sc->cbl_base;
838
839	/*
840	 * This bcopy is kind of disgusting, but there are a bunch of must be
841	 * zero and must be one bits in this structure and this is the easiest
842	 * way to initialize them all to proper values.
843	 */
844	bcopy(fxp_cb_config_template, cbp, sizeof(struct fxp_cb_config));
845
846	cbp->cb_status =	0;
847	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
848	cbp->link_addr =	-1;	/* (no) next command */
849	cbp->byte_count =	22;	/* (22) bytes to config */
850	cbp->rx_fifo_limit =	8;	/* rx fifo threshold */
851	cbp->tx_fifo_limit =	0;	/* tx fifo threshold */
852	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
853	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
854	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
855	cbp->dma_bce =		1;	/* (enable) dma max counters */
856	cbp->late_scb =		0;	/* (don't) defer SCB update */
857	cbp->tno_int =		0;	/* (disable) tx not okay interrupt */
858	cbp->ci_int =		0;	/* (do) interrupt on CU not active */
859	cbp->save_bf =		prm;	/* save bad frames */
860	cbp->disc_short_rx =	!prm;	/* discard short packets */
861	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
862	cbp->mediatype =	1;	/* (MII) interface mode */
863	cbp->nsai =		1;	/* (don't) disable source addr insert */
864	cbp->preamble_length =	2;	/* (7 byte) preamble */
865	cbp->loopback =		0;	/* (don't) loopback */
866	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
867	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
868	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
869	cbp->promiscuous =	prm;	/* promiscuous mode */
870	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
871	cbp->crscdt =		0;	/* (CRS only) */
872	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
873	cbp->padding =		1;	/* (do) pad short tx packets */
874	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
875	cbp->force_fdx =	0;	/* (don't) force full duplex */
876	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
877	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
878	cbp->mc_all =		mcast;	/* accept all multicasts */
879
880	/*
881	 * Start the config command/DMA.
882	 */
883	(void) fxp_scb_wait(csr);
884	csr->scb_general = vtophys(cbp);
885	csr->scb_command = FXP_SCB_COMMAND_CU_START;
886	/* ...and wait for it to complete. */
887	while (!(cbp->cb_status & FXP_CB_STATUS_C));
888
889	/*
890	 * Now initialize the station address. Temporarily use the TxCB
891	 * memory area like we did above for the config CB.
892	 */
893	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
894	cb_ias->cb_status = 0;
895	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
896	cb_ias->link_addr = -1;
897	bcopy(sc->arpcom.ac_enaddr, (void *)cb_ias->macaddr,
898	    sizeof(sc->arpcom.ac_enaddr));
899
900	/*
901	 * Start the IAS (Individual Address Setup) command/DMA.
902	 */
903	(void) fxp_scb_wait(csr);
904	csr->scb_command = FXP_SCB_COMMAND_CU_START;
905	/* ...and wait for it to complete. */
906	while (!(cb_ias->cb_status & FXP_CB_STATUS_C));
907
908	/*
909	 * Initialize transmit control block (TxCB) list.
910	 */
911
912	txp = sc->cbl_base;
913	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
914	for (i = 0; i < FXP_NTXCB; i++) {
915		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
916		txp[i].cb_command = FXP_CB_COMMAND_NOP;
917		txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK]);
918		txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
919		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
920	}
921	/*
922	 * Set the stop flag on the first TxCB and start the control
923	 * unit. It will execute the NOP and then suspend.
924	 */
925	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
926	sc->cbl_first = sc->cbl_last = txp;
927	sc->tx_queued = 0;
928
929	(void) fxp_scb_wait(csr);
930	csr->scb_command = FXP_SCB_COMMAND_CU_START;
931
932	/*
933	 * Initialize receiver buffer area - RFA.
934	 */
935	(void) fxp_scb_wait(csr);
936	csr->scb_general = vtophys(sc->rfa_headm->m_ext.ext_buf);
937	csr->scb_command = FXP_SCB_COMMAND_RU_START;
938
939	ifp->if_flags |= IFF_RUNNING;
940	ifp->if_flags &= ~IFF_OACTIVE;
941	splx(s);
942
943	/*
944	 * Start stats updater.
945	 */
946	timeout(fxp_stats_update, sc, hz);
947}
948
949/*
950 * Add a buffer to the end of the RFA buffer list.
951 * Return 0 if successful, 1 for failure. A failure results in
952 * adding the 'oldm' (if non-NULL) on to the end of the list -
953 * tossing out it's old contents and recycling it.
954 * The RFA struct is stuck at the beginning of mbuf cluster and the
955 * data pointer is fixed up to point just past it.
956 */
957static int
958fxp_add_rfabuf(sc, oldm)
959	struct fxp_softc *sc;
960	struct mbuf *oldm;
961{
962	struct mbuf *m;
963	struct fxp_rfa *rfa, *p_rfa;
964
965	MGETHDR(m, M_DONTWAIT, MT_DATA);
966	if (m != NULL) {
967		MCLGET(m, M_DONTWAIT);
968		if ((m->m_flags & M_EXT) == 0) {
969			m_freem(m);
970			m = oldm;
971		}
972	} else {
973		m = oldm;
974	}
975	if (m == NULL)
976		return 1;
977	rfa = mtod(m, struct fxp_rfa *);
978	rfa->rfa_status = 0;
979	rfa->rfa_control = FXP_RFA_CONTROL_EL;
980	rfa->link_addr = -1;
981	rfa->rbd_addr = -1;
982	rfa->actual_size = 0;
983	rfa->size = MCLBYTES - sizeof(struct fxp_rfa);
984	m->m_data += sizeof(struct fxp_rfa);
985	/*
986	 * If there are other buffers already on the list, attach this
987	 * one to the end by fixing up the tail to point to this one.
988	 */
989	if (sc->rfa_headm != NULL) {
990		p_rfa = (struct fxp_rfa *) sc->rfa_tailm->m_ext.ext_buf;
991		sc->rfa_tailm->m_next = m;
992		p_rfa->link_addr = vtophys(rfa);
993		p_rfa->rfa_control &= ~FXP_RFA_CONTROL_EL;
994	} else {
995		sc->rfa_headm = m;
996	}
997	sc->rfa_tailm = m;
998
999	return (m == oldm);
1000}
1001
1002static int
1003fxp_ioctl(ifp, command, data)
1004	struct ifnet *ifp;
1005	int command;
1006	caddr_t data;
1007{
1008	struct ifaddr *ifa = (struct ifaddr *) data;
1009	struct fxp_softc *sc = (struct fxp_softc *)ifp;
1010	struct ifreq *ifr = (struct ifreq *) data;
1011	int s, error = 0;
1012
1013	s = splimp();
1014
1015	switch (command) {
1016
1017	case SIOCSIFADDR:
1018		ifp->if_flags |= IFF_UP;
1019
1020		switch (ifa->ifa_addr->sa_family) {
1021#ifdef INET
1022		case AF_INET:
1023			fxp_init(ifp);	/* before arpwhohas */
1024			arp_ifinit((struct arpcom *)ifp, ifa);
1025			break;
1026#endif
1027#ifdef IPX
1028		/*
1029		 * XXX - This code is probably wrong
1030		 */
1031		case AF_IPX:
1032			{
1033				register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1034
1035				if (ipx_nullhost(*ina))
1036					ina->x_host =
1037					    *(union ipx_host *) (sc->arpcom.ac_enaddr);
1038				else {
1039					bcopy((caddr_t) ina->x_host.c_host,
1040					      (caddr_t) sc->arpcom.ac_enaddr,
1041					      sizeof(sc->arpcom.ac_enaddr));
1042				}
1043
1044				/*
1045				 * Set new address
1046				 */
1047				fxp_init(ifp);
1048				break;
1049			}
1050#endif
1051#ifdef NS
1052		/*
1053		 * XXX - This code is probably wrong
1054		 */
1055		case AF_NS:
1056			{
1057				register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1058
1059				if (ns_nullhost(*ina))
1060					ina->x_host =
1061					    *(union ns_host *) (sc->arpcom.ac_enaddr);
1062				else {
1063					bcopy((caddr_t) ina->x_host.c_host,
1064					      (caddr_t) sc->arpcom.ac_enaddr,
1065					      sizeof(sc->arpcom.ac_enaddr));
1066				}
1067
1068				/*
1069				 * Set new address
1070				 */
1071				fxp_init(ifp);
1072				break;
1073			}
1074#endif
1075		default:
1076			fxp_init(ifp);
1077			break;
1078		}
1079		break;
1080
1081	case SIOCGIFADDR:
1082		{
1083			struct sockaddr *sa;
1084
1085			sa = (struct sockaddr *) & ifr->ifr_data;
1086			bcopy((caddr_t) sc->arpcom.ac_enaddr,
1087			      (caddr_t) sa->sa_data, sizeof(sc->arpcom.ac_enaddr));
1088		}
1089		break;
1090
1091	case SIOCSIFFLAGS:
1092
1093		/*
1094		 * If interface is marked up and not running, then start it.
1095		 * If it is marked down and running, stop it.
1096		 * XXX If it's up then re-initialize it. This is so flags
1097		 * such as IFF_PROMISC are handled.
1098		 */
1099		if (ifp->if_flags & IFF_UP) {
1100			fxp_init(ifp);
1101		} else {
1102			if (ifp->if_flags & IFF_RUNNING)
1103				fxp_stop(sc);
1104		}
1105		break;
1106
1107	case SIOCADDMULTI:
1108	case SIOCDELMULTI:
1109		/*
1110		 * Update out multicast list.
1111		 */
1112		error = (command == SIOCADDMULTI) ?
1113		    ether_addmulti(ifr, &sc->arpcom) :
1114		    ether_delmulti(ifr, &sc->arpcom);
1115
1116		if (error == ENETRESET) {
1117			/*
1118			 * Multicast list has changed; set the hardware filter
1119			 * accordingly.
1120			 */
1121			fxp_init(ifp);
1122
1123			error = 0;
1124		}
1125		break;
1126
1127	case SIOCSIFMTU:
1128		/*
1129		 * Set the interface MTU.
1130		 */
1131		if (ifr->ifr_mtu > ETHERMTU) {
1132			error = EINVAL;
1133		} else {
1134			ifp->if_mtu = ifr->ifr_mtu;
1135		}
1136		break;
1137
1138	default:
1139		error = EINVAL;
1140	}
1141	(void) splx(s);
1142	return (error);
1143}
1144