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