if_fxp.c revision 12633
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.3 1995/12/05 02:01:51 davidg Exp $
33 */
34
35/*
36 * Intel EtherExpress Pro/100 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 <machine/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 char *fxp_probe		__P((pcici_t, pcidi_t));
138static void fxp_attach		__P((pcici_t, int));
139static int fxp_shutdown		__P((struct kern_devconf *, int));
140static int fxp_intr		__P((void *));
141static void fxp_start		__P((struct ifnet *));
142static int fxp_ioctl		__P((struct ifnet *, int, caddr_t));
143static void fxp_init		__P((struct ifnet *));
144static void fxp_stop		__P((struct fxp_softc *));
145static void fxp_watchdog	__P((struct ifnet *));
146static void fxp_get_macaddr	__P((struct fxp_softc *));
147static int fxp_add_rfabuf	__P((struct fxp_softc *, struct mbuf *));
148
149timeout_t fxp_stats_update;
150
151static struct pci_device fxp_device = {
152	"fxp",
153	fxp_probe,
154	fxp_attach,
155	&fxp_count,
156	fxp_shutdown
157};
158DATA_SET(pcidevice_set, fxp_device);
159
160/*
161 * Number of transmit control blocks. This determines the number
162 * of transmit buffers that can be chained in the CB list.
163 * This must be a power of two.
164 */
165#define FXP_NTXCB	64
166
167/*
168 * TxCB list index mask. This is used to do list wrap-around.
169 */
170#define FXP_TXCB_MASK	(FXP_NTXCB - 1)
171
172/*
173 * Number of DMA segments in a TxCB. Note that this is carefully
174 * chosen to make the total struct size an even power of two. It's
175 * critical that no TxCB be split across a page boundry since
176 * no attempt is made to allocate physically contiguous memory.
177 *
178 * XXX - don't forget to change the hard-coded constant in the
179 * fxp_cb_tx struct (defined in if_fxpreg.h), too!
180 */
181#define FXP_NTXSEG	29
182
183/*
184 * Number of receive frame area buffers. These are large so chose
185 * wisely.
186 */
187#define FXP_NRFABUFS	32
188
189/*
190 * Wait for the previous command to be accepted (but not necessarily
191 * completed).
192 */
193static inline void
194fxp_scb_wait(csr)
195	struct fxp_csr *csr;
196{
197	int i = 10000;
198
199	while ((csr->scb_command & FXP_SCB_COMMAND_MASK) && --i);
200}
201
202/*
203 * Return identification string if this is device is ours.
204 */
205static char *
206fxp_probe(config_id, device_id)
207	pcici_t config_id;
208	pcidi_t device_id;
209{
210	if (((device_id & 0xffff) == FXP_VENDORID_INTEL) &&
211	    ((device_id >> 16) & 0xffff) == FXP_DEVICEID_i82557)
212		return ("Intel EtherExpress Pro/100 Fast Ethernet");
213
214	return NULL;
215}
216
217/*
218 * Allocate data structures and attach the device.
219 */
220static void
221fxp_attach(config_id, unit)
222	pcici_t config_id;
223	int unit;
224{
225	struct fxp_softc *sc;
226	struct ifnet *ifp;
227	vm_offset_t pbase;
228	int s, i;
229
230	sc = malloc(sizeof(struct fxp_softc), M_DEVBUF, M_NOWAIT);
231	if (sc == NULL)
232		return;
233	bzero(sc, sizeof(struct fxp_softc));
234
235	s = splimp();
236
237	/*
238	 * Map control/status registers.
239	 */
240	if (!pci_map_mem(config_id, FXP_PCI_MMBA,
241	    (vm_offset_t *)&sc->csr, &pbase)) {
242		printf("fxp%d: couldn't map memory\n", unit);
243		goto fail;
244	}
245
246	/*
247	 * Issue a software reset.
248	 */
249	sc->csr->port = 0;
250	DELAY(10);
251
252	/*
253	 * Allocate our interrupt.
254	 */
255	if (!pci_map_int(config_id, fxp_intr, sc, &net_imask)) {
256		printf("fxp%d: couldn't map interrupt\n", unit);
257		goto fail;
258	}
259
260	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
261	    M_DEVBUF, M_NOWAIT);
262	if (sc->cbl_base == NULL)
263		goto malloc_fail;
264
265	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT);
266	if (sc->fxp_stats == NULL)
267		goto malloc_fail;
268	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
269
270	/*
271	 * Pre-allocate our receive buffers.
272	 */
273	for (i = 0; i < FXP_NRFABUFS; i++) {
274		if (fxp_add_rfabuf(sc, NULL) != 0) {
275			goto malloc_fail;
276		}
277	}
278
279	fxp_sc[unit] = sc;
280
281	ifp = &sc->arpcom.ac_if;
282	ifp->if_unit = unit;
283	ifp->if_name = "fxp";
284	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
285	ifp->if_ioctl = fxp_ioctl;
286	ifp->if_output = ether_output;
287	ifp->if_start = fxp_start;
288	ifp->if_watchdog = fxp_watchdog;
289
290	fxp_get_macaddr(sc);
291	printf("fxp%d: Ethernet address %s\n", unit,
292	    ether_sprintf(sc->arpcom.ac_enaddr));
293
294	/*
295	 * Attach the interface.
296	 */
297	if_attach(ifp);
298#if NBPFILTER > 0
299	bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
300#endif
301	splx(s);
302	return;
303
304malloc_fail:
305	printf("fxp%d: Failed to malloc memory\n", unit);
306	(void) pci_unmap_int(config_id);
307	if (sc && sc->cbl_base)
308		free(sc->cbl_base, M_DEVBUF);
309	if (sc && sc->fxp_stats)
310		free(sc->fxp_stats, M_DEVBUF);
311	/* frees entire chain */
312	if (sc && sc->rfa_headm)
313		m_freem(sc->rfa_headm);
314fail:
315	if (sc)
316		free(sc, M_DEVBUF);
317	splx(s);
318}
319
320/*
321 * Read station (MAC) address from serial EEPROM. Basically, you
322 * manually shift in the read opcode (one bit at a time) and then
323 * shift in the address, and then you shift out the data (all of
324 * this one bit at a time). The word size is 16 bits, so you have
325 * to provide the address for every 16 bits of data. The MAC address
326 * is in the first 3 words (6 bytes total).
327 */
328static void
329fxp_get_macaddr(sc)
330	struct fxp_softc *sc;
331{
332	struct fxp_csr *csr;
333	u_short reg, *data;
334	int i, x;
335
336	csr = sc->csr;
337	data = (u_short *)sc->arpcom.ac_enaddr;
338
339	for (i = 0; i < 3; i++) {
340		csr->eeprom_control = FXP_EEPROM_EECS;
341		/*
342		 * Shift in read opcode.
343		 */
344		for (x = 3; x > 0; x--) {
345			if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
346				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
347			} else {
348				reg = FXP_EEPROM_EECS;
349			}
350			csr->eeprom_control = reg;
351			csr->eeprom_control = reg | FXP_EEPROM_EESK;
352			DELAY(1);
353			csr->eeprom_control = reg;
354			DELAY(1);
355		}
356		/*
357		 * Shift in address.
358		 */
359		for (x = 6; x > 0; x--) {
360			if (i & (1 << (x - 1))) {
361				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
362			} else {
363				reg = FXP_EEPROM_EECS;
364			}
365			csr->eeprom_control = reg;
366			csr->eeprom_control = reg | FXP_EEPROM_EESK;
367			DELAY(1);
368			csr->eeprom_control = reg;
369			DELAY(1);
370		}
371		reg = FXP_EEPROM_EECS;
372		data[i] = 0;
373		/*
374		 * Shift out data.
375		 */
376		for (x = 16; x > 0; x--) {
377			csr->eeprom_control = reg | FXP_EEPROM_EESK;
378			DELAY(1);
379			if (csr->eeprom_control & FXP_EEPROM_EEDO)
380				data[i] |= (1 << (x - 1));
381			csr->eeprom_control = reg;
382			DELAY(1);
383		}
384		csr->eeprom_control = 0;
385		DELAY(1);
386	}
387}
388
389/*
390 * Device shutdown routine. Usually called at system shutdown. The
391 * main purpose of this routine is to shut off receiver DMA so that
392 * kernel memory doesn't get clobbered during warmboot.
393 */
394static int
395fxp_shutdown(kdc, force)
396	struct kern_devconf *kdc;
397	int force;
398{
399	struct fxp_softc *sc = fxp_sc[kdc->kdc_unit];
400
401	/*
402	 * Cancel stats updater.
403	 */
404	untimeout(fxp_stats_update, sc);
405	/*
406	 * Issue software reset.
407	 */
408	sc->csr->port = 0;
409
410	(void) dev_detach(kdc);
411	return 0;
412}
413
414/*
415 * Start packet transmission on the interface.
416 */
417static void
418fxp_start(ifp)
419	struct ifnet *ifp;
420{
421	struct fxp_softc *sc = (struct fxp_softc *)ifp;
422	struct fxp_csr *csr = sc->csr;
423	struct fxp_cb_tx *txp;
424	struct mbuf *m, *mb_head;
425	int segment;
426
427txloop:
428	/*
429	 * See if a TxCB is available. If not, indicate this to the
430	 * outside world and exit.
431	 */
432	if (sc->tx_queued >= FXP_NTXCB) {
433		ifp->if_flags |= IFF_OACTIVE;
434		return;
435	}
436	/*
437	 * Grab a packet to transmit.
438	 */
439	IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, mb_head);
440	if (mb_head == NULL) {
441		/*
442		 * No more packets to send.
443		 */
444		return;
445	}
446
447	/*
448	 * Get pointer to next available (unused) descriptor.
449	 */
450	txp = sc->cbl_last->next;
451
452	/*
453	 * Go through each of the mbufs in the chain and initialize
454	 * the transmit buffers descriptors with the physical address
455	 * and size of the mbuf.
456	 */
457tbdinit:
458	for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
459		if (m->m_len != 0) {
460			if (segment == FXP_NTXSEG)
461				break;
462			txp->tbd[segment].tb_addr =
463			    vtophys(mtod(m, vm_offset_t));
464			txp->tbd[segment].tb_size = m->m_len;
465			segment++;
466		}
467	}
468	if (m != NULL && segment == FXP_NTXSEG) {
469		struct mbuf *mn;
470
471		/*
472		 * We ran out of segments. We have to recopy this mbuf
473		 * chain first.
474		 */
475		MGETHDR(mn, M_DONTWAIT, MT_DATA);
476		if (mn == NULL) {
477			m_freem(mb_head);
478			return;
479		}
480		if (mb_head->m_pkthdr.len > MHLEN) {
481			MCLGET(mn, M_DONTWAIT);
482			if ((mn->m_flags & M_EXT) == 0) {
483				m_freem(mn);
484				m_freem(mb_head);
485				return;
486			}
487		}
488		m_copydata(mb_head, 0, mb_head->m_pkthdr.len, mtod(mn, caddr_t));
489		mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
490		m_freem(mb_head);
491		mb_head = mn;
492		goto tbdinit;
493	}
494
495	txp->tbd_number = segment;
496
497	/*
498	 * Finish the initialization of this TxCB.
499	 */
500	txp->cb_status = 0;
501	txp->cb_command =
502	    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S;
503	txp->tx_threshold = 16;	/* bytes*8 */
504	txp->mb_head = mb_head;
505
506	/*
507	 * Advance the end-of-list forward.
508	 */
509	sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
510	sc->cbl_last = txp;
511
512	/*
513	 * If no packets were previously queued then advance the first
514	 * pointer to this TxCB.
515	 */
516	if (sc->tx_queued++ == 0) {
517		sc->cbl_first = txp;
518	}
519
520	/*
521	 * Resume transmission if suspended.
522	 */
523	fxp_scb_wait(csr);
524	csr->scb_command = FXP_SCB_COMMAND_CU_RESUME;
525
526#if NBPFILTER > 0
527	/*
528	 * Pass packet to bpf if there is a listener.
529	 */
530	if (sc->bpf != NULL)
531		bpf_mtap(sc->bpf, mb_head);
532#endif
533	/*
534	 * Set a 5 second timer just in case we don't hear from the
535	 * card again.
536	 */
537	ifp->if_timer = 5;
538
539	goto txloop;
540}
541
542/*
543 * Process interface interrupts. Returns 1 if the interrupt
544 * was handled, 0 if it wasn't.
545 */
546static int
547fxp_intr(arg)
548	void *arg;
549{
550	struct fxp_softc *sc = arg;
551	struct fxp_csr *csr = sc->csr;
552	struct ifnet *ifp = &sc->arpcom.ac_if;
553	int found = 0;
554	u_char statack;
555
556	while ((statack = csr->scb_statack) != 0) {
557		found = 1;
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				ifp->if_ierrors++;
649				fxp_scb_wait(csr);
650				csr->scb_general = vtophys(sc->rfa_headm->m_ext.ext_buf);
651				csr->scb_command = FXP_SCB_COMMAND_RU_START;
652			}
653		}
654	}
655
656	return found;
657}
658
659/*
660 * Update packet in/out/collision statistics. The i82557 doesn't
661 * allow you to access these counters without doing a fairly
662 * expensive DMA to get _all_ of the statistics it maintains, so
663 * we do this operation here only once per second. The statistics
664 * counters in the kernel are updated from the previous dump-stats
665 * DMA and then a new dump-stats DMA is started. The on-chip
666 * counters are zeroed when the DMA completes. If we can't start
667 * the DMA immediately, we don't wait - we just prepare to read
668 * them again next time.
669 */
670void
671fxp_stats_update(arg)
672	void *arg;
673{
674	struct fxp_softc *sc = arg;
675	struct ifnet *ifp = &sc->arpcom.ac_if;
676	struct fxp_stats *sp = sc->fxp_stats;
677
678	ifp->if_opackets += sp->tx_good;
679	ifp->if_collisions += sp->tx_total_collisions;
680	ifp->if_ipackets += sp->rx_good;
681	/*
682	 * If there is a pending command, don't wait for it to
683	 * be accepted - we'll pick up the stats the next time
684	 * around. Make sure we don't count the stats twice
685	 * however.
686	 */
687	if ((sc->csr->scb_command & FXP_SCB_COMMAND_MASK) == 0) {
688		/*
689		 * Start another stats dump. By waiting for it to be
690		 * accepted, we avoid having to do splhigh locking when
691		 * writing scb_command in other parts of the driver.
692		 */
693		sc->csr->scb_command = FXP_SCB_COMMAND_CU_DUMPRESET;
694		fxp_scb_wait(sc);
695	} else {
696		/*
697		 * A previous command is still waiting to be accepted.
698		 * Just zero our copy of the stats and wait for the
699		 * next timer event to pdate them.
700		 */
701		sp->tx_good = 0;
702		sp->tx_total_collisions = 0;
703		sp->rx_good = 0;
704	}
705	/*
706	 * Schedule another timeout one second from now.
707	 */
708	timeout(fxp_stats_update, sc, hz);
709}
710
711/*
712 * Stop the interface. Cancels the statistics updater and resets
713 * the interface.
714 */
715static void
716fxp_stop(sc)
717	struct fxp_softc *sc;
718{
719	struct ifnet *ifp = &sc->arpcom.ac_if;
720
721	/*
722	 * Cancel stats updater.
723	 */
724	untimeout(fxp_stats_update, sc);
725	sc->csr->port = 0;
726	DELAY(10);
727
728	ifp->if_flags &= ~IFF_RUNNING;
729}
730
731/*
732 * Watchdog/transmission transmit timeout handler. Called when a
733 * transmission is started on the interface, but no interrupt is
734 * received before the timeout. This usually indicates that the
735 * card has wedged for some reason.
736 */
737static void
738fxp_watchdog(ifp)
739	struct ifnet *ifp;
740{
741	log(LOG_ERR, "fxp%d: device timeout\n", ifp->if_unit);
742	ifp->if_oerrors++;
743
744	fxp_init(ifp);
745}
746
747static void
748fxp_init(ifp)
749	struct ifnet *ifp;
750{
751	struct fxp_softc *sc = (struct fxp_softc *)ifp;
752	struct fxp_cb_config *cbp;
753	struct fxp_cb_ias *cb_ias;
754	struct fxp_cb_tx *txp;
755	struct fxp_csr *csr = sc->csr;
756	int i, s, mcast, prm;
757
758	/*
759	 * Cancel stats updater.
760	 */
761	untimeout(fxp_stats_update, sc);
762
763	s = splimp();
764	/*
765	 * Issue software reset and wait 10us for the card to recover.
766	 */
767	csr->port = 0;
768	DELAY(10);
769
770	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
771	sc->promisc_mode = prm;
772	/*
773	 * Sleeze out here and enable reception of all multicasts if
774	 * multicasts are enabled. Ideally, we'd program the multicast
775	 * address filter to only accept specific multicasts.
776	 */
777	mcast = (ifp->if_flags & (IFF_MULTICAST|IFF_ALLMULTI)) ? 1 : 0;
778
779	/*
780	 * Initialize base of CBL and RFA memory. Loading with zero
781	 * sets it up for regular linear addressing.
782	 */
783	csr->scb_general = 0;
784	csr->scb_command = FXP_SCB_COMMAND_CU_BASE;
785
786	fxp_scb_wait(csr);
787	csr->scb_command = FXP_SCB_COMMAND_RU_BASE;
788
789	/*
790	 * Initialize base of dump-stats buffer.
791	 */
792	fxp_scb_wait(csr);
793	csr->scb_general = vtophys(sc->fxp_stats);
794	csr->scb_command = FXP_SCB_COMMAND_CU_DUMP_ADR;
795
796	/*
797	 * We temporarily use memory that contains the TxCB list to
798	 * construct the config CB. The TxCB list memory is rebuilt
799	 * later.
800	 */
801	cbp = (struct fxp_cb_config *) sc->cbl_base;
802
803	/*
804	 * This bcopy is kind of disgusting, but there are a bunch of must be
805	 * zero and must be one bits in this structure and this is the easiest
806	 * way to initialize them all to proper values.
807	 */
808	bcopy(fxp_cb_config_template, cbp, sizeof(struct fxp_cb_config));
809
810	cbp->cb_status =	0;
811	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
812	cbp->link_addr =	-1;	/* (no) next command */
813	cbp->byte_count =	22;	/* (22) bytes to config */
814	cbp->rx_fifo_limit =	8;	/* rx fifo threshold */
815	cbp->tx_fifo_limit =	0;	/* tx fifo threshold */
816	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
817	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
818	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
819	cbp->dma_bce =		1;	/* (enable) dma max counters */
820	cbp->late_scb =		0;	/* (don't) defer SCB update */
821	cbp->tno_int =		0;	/* (disable) tx not okay interrupt */
822	cbp->ci_int =		0;	/* (do) interrupt on CU not active */
823	cbp->save_bf =		prm;	/* save bad frames */
824	cbp->disc_short_rx =	!prm;	/* discard short packets */
825	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
826	cbp->mediatype =	1;	/* (MII) interface mode */
827	cbp->nsai =		1;	/* (don't) disable source addr insert */
828	cbp->preamble_length =	2;	/* (7 byte) preamble */
829	cbp->loopback =		0;	/* (don't) loopback */
830	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
831	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
832	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
833	cbp->promiscuous =	prm;	/* promiscuous mode */
834	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
835	cbp->crscdt =		0;	/* (CRS only) */
836	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
837	cbp->padding =		1;	/* (do) pad short tx packets */
838	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
839	cbp->force_fdx =	0;	/* (don't) force full duplex */
840	cbp->fdx_pin_en =	0;	/* (ignore) FDX# pin */
841	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
842	cbp->mc_all =		mcast;	/* accept all multicasts */
843
844	/*
845	 * Start the config command/DMA.
846	 */
847	fxp_scb_wait(csr);
848	csr->scb_general = vtophys(cbp);
849	csr->scb_command = FXP_SCB_COMMAND_CU_START;
850	/* ...and wait for it to complete. */
851	while (!(cbp->cb_status & FXP_CB_STATUS_C));
852
853	/*
854	 * Now initialize the station address. Temporarily use the TxCB
855	 * memory area like we did above for the config CB.
856	 */
857	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
858	cb_ias->cb_status = 0;
859	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
860	cb_ias->link_addr = -1;
861	bcopy(sc->arpcom.ac_enaddr, (void *)cb_ias->macaddr,
862	    sizeof(sc->arpcom.ac_enaddr));
863
864	/*
865	 * Start the IAS (Individual Address Setup) command/DMA.
866	 */
867	fxp_scb_wait(csr);
868	csr->scb_command = FXP_SCB_COMMAND_CU_START;
869	/* ...and wait for it to complete. */
870	while (!(cb_ias->cb_status & FXP_CB_STATUS_C));
871
872	/*
873	 * Initialize transmit control block (TxCB) list.
874	 */
875
876	txp = sc->cbl_base;
877	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
878	for (i = 0; i < FXP_NTXCB; i++) {
879		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
880		txp[i].cb_command = FXP_CB_COMMAND_NOP;
881		txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK]);
882		txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
883		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
884	}
885	/*
886	 * Set the stop flag on the first TxCB and start the control
887	 * unit. It will execute the NOP and then suspend.
888	 */
889	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
890	sc->cbl_first = sc->cbl_last = txp;
891	sc->tx_queued = 0;
892
893	fxp_scb_wait(csr);
894	csr->scb_command = FXP_SCB_COMMAND_CU_START;
895
896	/*
897	 * Initialize receiver buffer area - RFA.
898	 */
899	fxp_scb_wait(csr);
900	csr->scb_general = vtophys(sc->rfa_headm->m_ext.ext_buf);
901	csr->scb_command = FXP_SCB_COMMAND_RU_START;
902
903	ifp->if_flags |= IFF_RUNNING;
904	ifp->if_flags &= ~IFF_OACTIVE;
905	splx(s);
906
907	/*
908	 * Start stats updater.
909	 */
910	timeout(fxp_stats_update, sc, hz);
911}
912
913/*
914 * Add a buffer to the end of the RFA buffer list.
915 * Return 0 if successful, 1 for failure. A failure results in
916 * adding the 'oldm' (if non-NULL) on to the end of the list -
917 * tossing out it's old contents and recycling it.
918 * The RFA struct is stuck at the beginning of mbuf cluster and the
919 * data pointer is fixed up to point just past it.
920 */
921static int
922fxp_add_rfabuf(sc, oldm)
923	struct fxp_softc *sc;
924	struct mbuf *oldm;
925{
926	struct mbuf *m;
927	struct fxp_rfa *rfa, *p_rfa;
928
929	MGETHDR(m, M_DONTWAIT, MT_DATA);
930	if (m != NULL) {
931		MCLGET(m, M_DONTWAIT);
932		if ((m->m_flags & M_EXT) == 0) {
933			m_freem(m);
934			m = oldm;
935		}
936	} else {
937		m = oldm;
938	}
939	if (m == NULL)
940		return 1;
941	rfa = mtod(m, struct fxp_rfa *);
942	rfa->rfa_status = 0;
943	rfa->rfa_control = FXP_RFA_CONTROL_EL;
944	rfa->link_addr = -1;
945	rfa->rbd_addr = -1;
946	rfa->actual_size = 0;
947	rfa->size = MCLBYTES - sizeof(struct fxp_rfa);
948	m->m_data += sizeof(struct fxp_rfa);
949	/*
950	 * If there are other buffers already on the list, attach this
951	 * one to the end by fixing up the tail to point to this one.
952	 */
953	if (sc->rfa_headm != NULL) {
954		p_rfa = (struct fxp_rfa *) sc->rfa_tailm->m_ext.ext_buf;
955		sc->rfa_tailm->m_next = m;
956		p_rfa->link_addr = vtophys(rfa);
957		p_rfa->rfa_control &= ~FXP_RFA_CONTROL_EL;
958	} else {
959		sc->rfa_headm = m;
960	}
961	sc->rfa_tailm = m;
962
963	return (m == oldm);
964}
965
966static int
967fxp_ioctl(ifp, command, data)
968	struct ifnet *ifp;
969	int command;
970	caddr_t data;
971{
972	struct ifaddr *ifa = (struct ifaddr *) data;
973	struct fxp_softc *sc = (struct fxp_softc *)ifp;
974	struct ifreq *ifr = (struct ifreq *) data;
975	int s, error = 0;
976
977	s = splimp();
978
979	switch (command) {
980
981	case SIOCSIFADDR:
982		ifp->if_flags |= IFF_UP;
983
984		switch (ifa->ifa_addr->sa_family) {
985#ifdef INET
986		case AF_INET:
987			fxp_init(ifp);	/* before arpwhohas */
988			arp_ifinit((struct arpcom *)ifp, ifa);
989			break;
990#endif
991#ifdef IPX
992		/*
993		 * XXX - This code is probably wrong
994		 */
995		case AF_IPX:
996			{
997				register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
998
999				if (ipx_nullhost(*ina))
1000					ina->x_host =
1001					    *(union ipx_host *) (sc->arpcom.ac_enaddr);
1002				else {
1003					bcopy((caddr_t) ina->x_host.c_host,
1004					      (caddr_t) sc->arpcom.ac_enaddr,
1005					      sizeof(sc->arpcom.ac_enaddr));
1006				}
1007
1008				/*
1009				 * Set new address
1010				 */
1011				fxp_init(ifp);
1012				break;
1013			}
1014#endif
1015#ifdef NS
1016		/*
1017		 * XXX - This code is probably wrong
1018		 */
1019		case AF_NS:
1020			{
1021				register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1022
1023				if (ns_nullhost(*ina))
1024					ina->x_host =
1025					    *(union ns_host *) (sc->arpcom.ac_enaddr);
1026				else {
1027					bcopy((caddr_t) ina->x_host.c_host,
1028					      (caddr_t) sc->arpcom.ac_enaddr,
1029					      sizeof(sc->arpcom.ac_enaddr));
1030				}
1031
1032				/*
1033				 * Set new address
1034				 */
1035				fxp_init(ifp);
1036				break;
1037			}
1038#endif
1039		default:
1040			fxp_init(ifp);
1041			break;
1042		}
1043		break;
1044
1045	case SIOCGIFADDR:
1046		{
1047			struct sockaddr *sa;
1048
1049			sa = (struct sockaddr *) & ifr->ifr_data;
1050			bcopy((caddr_t) sc->arpcom.ac_enaddr,
1051			      (caddr_t) sa->sa_data, sizeof(sc->arpcom.ac_enaddr));
1052		}
1053		break;
1054
1055	case SIOCSIFFLAGS:
1056
1057		/*
1058		 * If interface is marked up and not running, then start it.
1059		 * If it is marked down and running, stop it.
1060		 * XXX If it's up then re-initialize it. This is so flags
1061		 * such as IFF_PROMISC are handled.
1062		 */
1063		if (ifp->if_flags & IFF_UP) {
1064			fxp_init(ifp);
1065		} else {
1066			if (ifp->if_flags & IFF_RUNNING)
1067				fxp_stop(sc);
1068		}
1069		break;
1070
1071	case SIOCADDMULTI:
1072	case SIOCDELMULTI:
1073		/*
1074		 * Update out multicast list.
1075		 */
1076		error = (command == SIOCADDMULTI) ?
1077		    ether_addmulti(ifr, &sc->arpcom) :
1078		    ether_delmulti(ifr, &sc->arpcom);
1079
1080		if (error == ENETRESET) {
1081			/*
1082			 * Multicast list has changed; set the hardware filter
1083			 * accordingly.
1084			 */
1085			fxp_init(ifp);
1086
1087			error = 0;
1088		}
1089		break;
1090
1091	case SIOCSIFMTU:
1092		/*
1093		 * Set the interface MTU.
1094		 */
1095		if (ifr->ifr_mtu > ETHERMTU) {
1096			error = EINVAL;
1097		} else {
1098			ifp->if_mtu = ifr->ifr_mtu;
1099		}
1100		break;
1101
1102	default:
1103		error = EINVAL;
1104	}
1105	(void) splx(s);
1106	return (error);
1107}
1108