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