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