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