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