if_fxp.c revision 113252
1/*-
2 * Copyright (c) 1995, David Greenman
3 * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30/*
31 * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/fxp/if_fxp.c 113252 2003-04-08 13:05:11Z mux $");
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/endian.h>
40#include <sys/mbuf.h>
41		/* #include <sys/mutex.h> */
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/sysctl.h>
45
46#include <net/if.h>
47#include <net/if_dl.h>
48#include <net/if_media.h>
49
50#include <net/bpf.h>
51#include <sys/sockio.h>
52#include <sys/bus.h>
53#include <machine/bus.h>
54#include <sys/rman.h>
55#include <machine/resource.h>
56
57#include <net/ethernet.h>
58#include <net/if_arp.h>
59
60#include <machine/clock.h>	/* for DELAY */
61
62#include <net/if_types.h>
63#include <net/if_vlan_var.h>
64
65#ifdef FXP_IP_CSUM_WAR
66#include <netinet/in.h>
67#include <netinet/in_systm.h>
68#include <netinet/ip.h>
69#include <machine/in_cksum.h>
70#endif
71
72#include <pci/pcivar.h>
73#include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
74
75#include <dev/mii/mii.h>
76#include <dev/mii/miivar.h>
77
78#include <dev/fxp/if_fxpreg.h>
79#include <dev/fxp/if_fxpvar.h>
80#include <dev/fxp/rcvbundl.h>
81
82MODULE_DEPEND(fxp, miibus, 1, 1, 1);
83#include "miibus_if.h"
84
85/*
86 * NOTE!  On the Alpha, we have an alignment constraint.  The
87 * card DMAs the packet immediately following the RFA.  However,
88 * the first thing in the packet is a 14-byte Ethernet header.
89 * This means that the packet is misaligned.  To compensate,
90 * we actually offset the RFA 2 bytes into the cluster.  This
91 * alignes the packet after the Ethernet header at a 32-bit
92 * boundary.  HOWEVER!  This means that the RFA is misaligned!
93 */
94#define	RFA_ALIGNMENT_FUDGE	2
95
96/*
97 * Set initial transmit threshold at 64 (512 bytes). This is
98 * increased by 64 (512 bytes) at a time, to maximum of 192
99 * (1536 bytes), if an underrun occurs.
100 */
101static int tx_threshold = 64;
102
103/*
104 * The configuration byte map has several undefined fields which
105 * must be one or must be zero.  Set up a template for these bits
106 * only, (assuming a 82557 chip) leaving the actual configuration
107 * to fxp_init.
108 *
109 * See struct fxp_cb_config for the bit definitions.
110 */
111static u_char fxp_cb_config_template[] = {
112	0x0, 0x0,		/* cb_status */
113	0x0, 0x0,		/* cb_command */
114	0x0, 0x0, 0x0, 0x0,	/* link_addr */
115	0x0,	/*  0 */
116	0x0,	/*  1 */
117	0x0,	/*  2 */
118	0x0,	/*  3 */
119	0x0,	/*  4 */
120	0x0,	/*  5 */
121	0x32,	/*  6 */
122	0x0,	/*  7 */
123	0x0,	/*  8 */
124	0x0,	/*  9 */
125	0x6,	/* 10 */
126	0x0,	/* 11 */
127	0x0,	/* 12 */
128	0x0,	/* 13 */
129	0xf2,	/* 14 */
130	0x48,	/* 15 */
131	0x0,	/* 16 */
132	0x40,	/* 17 */
133	0xf0,	/* 18 */
134	0x0,	/* 19 */
135	0x3f,	/* 20 */
136	0x5	/* 21 */
137};
138
139struct fxp_ident {
140	u_int16_t	devid;
141	char 		*name;
142};
143
144/*
145 * Claim various Intel PCI device identifiers for this driver.  The
146 * sub-vendor and sub-device field are extensively used to identify
147 * particular variants, but we don't currently differentiate between
148 * them.
149 */
150static struct fxp_ident fxp_ident_table[] = {
151    { 0x1029,		"Intel 82559 PCI/CardBus Pro/100" },
152    { 0x1030,		"Intel 82559 Pro/100 Ethernet" },
153    { 0x1031,		"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
154    { 0x1032,		"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
155    { 0x1033,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
156    { 0x1034,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
157    { 0x1035,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
158    { 0x1036,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
159    { 0x1037,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
160    { 0x1038,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
161    { 0x1039,		"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
162    { 0x103A,		"Intel 82801DB (ICH4) Pro/100 Ethernet" },
163    { 0x103B,		"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
164    { 0x103C,		"Intel 82801DB (ICH4) Pro/100 Ethernet" },
165    { 0x103D,		"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
166    { 0x103E,		"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
167    { 0x1059,		"Intel 82551QM Pro/100 M Mobile Connection" },
168    { 0x1209,		"Intel 82559ER Embedded 10/100 Ethernet" },
169    { 0x1229,		"Intel 82557/8/9 EtherExpress Pro/100(B) Ethernet" },
170    { 0x2449,		"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
171    { 0,		NULL },
172};
173
174#ifdef FXP_IP_CSUM_WAR
175#define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
176#else
177#define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
178#endif
179
180static int		fxp_probe(device_t dev);
181static int		fxp_attach(device_t dev);
182static int		fxp_detach(device_t dev);
183static int		fxp_shutdown(device_t dev);
184static int		fxp_suspend(device_t dev);
185static int		fxp_resume(device_t dev);
186
187static void		fxp_intr(void *xsc);
188static void 		fxp_init(void *xsc);
189static void 		fxp_tick(void *xsc);
190static void		fxp_powerstate_d0(device_t dev);
191static void 		fxp_start(struct ifnet *ifp);
192static void		fxp_stop(struct fxp_softc *sc);
193static void 		fxp_release(struct fxp_softc *sc);
194static int		fxp_ioctl(struct ifnet *ifp, u_long command,
195			    caddr_t data);
196static void 		fxp_watchdog(struct ifnet *ifp);
197static int		fxp_add_rfabuf(struct fxp_softc *sc,
198    			    struct fxp_rx *rxp);
199static int		fxp_mc_addrs(struct fxp_softc *sc);
200static void		fxp_mc_setup(struct fxp_softc *sc);
201static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
202			    int autosize);
203static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
204			    u_int16_t data);
205static void		fxp_autosize_eeprom(struct fxp_softc *sc);
206static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
207			    int offset, int words);
208static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
209			    int offset, int words);
210static int		fxp_ifmedia_upd(struct ifnet *ifp);
211static void		fxp_ifmedia_sts(struct ifnet *ifp,
212			    struct ifmediareq *ifmr);
213static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
214static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
215			    struct ifmediareq *ifmr);
216static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
217static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
218			    int value);
219static void		fxp_load_ucode(struct fxp_softc *sc);
220static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
221			    int low, int high);
222static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
223static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
224static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
225static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
226static __inline void	fxp_dma_wait(volatile u_int16_t *status,
227			    struct fxp_softc *sc);
228
229static device_method_t fxp_methods[] = {
230	/* Device interface */
231	DEVMETHOD(device_probe,		fxp_probe),
232	DEVMETHOD(device_attach,	fxp_attach),
233	DEVMETHOD(device_detach,	fxp_detach),
234	DEVMETHOD(device_shutdown,	fxp_shutdown),
235	DEVMETHOD(device_suspend,	fxp_suspend),
236	DEVMETHOD(device_resume,	fxp_resume),
237
238	/* MII interface */
239	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
240	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
241
242	{ 0, 0 }
243};
244
245static driver_t fxp_driver = {
246	"fxp",
247	fxp_methods,
248	sizeof(struct fxp_softc),
249};
250
251static devclass_t fxp_devclass;
252
253DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
254DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
255DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
256
257static int fxp_rnr;
258SYSCTL_INT(_hw, OID_AUTO, fxp_rnr, CTLFLAG_RW, &fxp_rnr, 0, "fxp rnr events");
259
260/*
261 * Wait for the previous command to be accepted (but not necessarily
262 * completed).
263 */
264static __inline void
265fxp_scb_wait(struct fxp_softc *sc)
266{
267	int i = 10000;
268
269	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
270		DELAY(2);
271	if (i == 0)
272		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
273		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
274		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
275		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
276		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
277}
278
279static __inline void
280fxp_scb_cmd(struct fxp_softc *sc, int cmd)
281{
282
283	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
284		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
285		fxp_scb_wait(sc);
286	}
287	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
288}
289
290static __inline void
291fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
292{
293	int i = 10000;
294
295	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i)
296		DELAY(2);
297	if (i == 0)
298		device_printf(sc->dev, "DMA timeout\n");
299}
300
301/*
302 * Return identification string if this is device is ours.
303 */
304static int
305fxp_probe(device_t dev)
306{
307	u_int16_t devid;
308	struct fxp_ident *ident;
309
310	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
311		devid = pci_get_device(dev);
312		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
313			if (ident->devid == devid) {
314				device_set_desc(dev, ident->name);
315				return (0);
316			}
317		}
318	}
319	return (ENXIO);
320}
321
322static void
323fxp_powerstate_d0(device_t dev)
324{
325#if __FreeBSD_version >= 430002
326	u_int32_t iobase, membase, irq;
327
328	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
329		/* Save important PCI config data. */
330		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
331		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
332		irq = pci_read_config(dev, PCIR_INTLINE, 4);
333
334		/* Reset the power state. */
335		device_printf(dev, "chip is in D%d power mode "
336		    "-- setting to D0\n", pci_get_powerstate(dev));
337
338		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
339
340		/* Restore PCI config data. */
341		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
342		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
343		pci_write_config(dev, PCIR_INTLINE, irq, 4);
344	}
345#endif
346}
347
348static void
349fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
350{
351	u_int32_t *addr;
352
353	if (error)
354		return;
355
356	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
357	addr = arg;
358	*addr = segs->ds_addr;
359}
360
361static int
362fxp_attach(device_t dev)
363{
364	int error = 0;
365	struct fxp_softc *sc = device_get_softc(dev);
366	struct ifnet *ifp;
367	struct fxp_rx *rxp;
368	u_int32_t val;
369	u_int16_t data, myea[ETHER_ADDR_LEN / 2];
370	int i, rid, m1, m2, prefer_iomap, maxtxseg;
371	int s;
372
373	bzero(sc, sizeof(*sc));
374	sc->dev = dev;
375	callout_handle_init(&sc->stat_ch);
376	sysctl_ctx_init(&sc->sysctl_ctx);
377	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
378	    MTX_DEF | MTX_RECURSE);
379
380	s = splimp();
381
382	/*
383	 * Enable bus mastering. Enable memory space too, in case
384	 * BIOS/Prom forgot about it.
385	 */
386	val = pci_read_config(dev, PCIR_COMMAND, 2);
387	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
388	pci_write_config(dev, PCIR_COMMAND, val, 2);
389	val = pci_read_config(dev, PCIR_COMMAND, 2);
390
391	fxp_powerstate_d0(dev);
392
393	/*
394	 * Figure out which we should try first - memory mapping or i/o mapping?
395	 * We default to memory mapping. Then we accept an override from the
396	 * command line. Then we check to see which one is enabled.
397	 */
398	m1 = PCIM_CMD_MEMEN;
399	m2 = PCIM_CMD_PORTEN;
400	prefer_iomap = 0;
401	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
402	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
403		m1 = PCIM_CMD_PORTEN;
404		m2 = PCIM_CMD_MEMEN;
405	}
406
407	if (val & m1) {
408		sc->rtp =
409		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
410		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
411		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
412	                                     0, ~0, 1, RF_ACTIVE);
413	}
414	if (sc->mem == NULL && (val & m2)) {
415		sc->rtp =
416		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
417		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
418		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
419                                            0, ~0, 1, RF_ACTIVE);
420	}
421
422	if (!sc->mem) {
423		device_printf(dev, "could not map device registers\n");
424		error = ENXIO;
425		goto fail;
426        }
427	if (bootverbose) {
428		device_printf(dev, "using %s space register mapping\n",
429		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
430	}
431
432	sc->sc_st = rman_get_bustag(sc->mem);
433	sc->sc_sh = rman_get_bushandle(sc->mem);
434
435	/*
436	 * Allocate our interrupt.
437	 */
438	rid = 0;
439	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
440				 RF_SHAREABLE | RF_ACTIVE);
441	if (sc->irq == NULL) {
442		device_printf(dev, "could not map interrupt\n");
443		error = ENXIO;
444		goto fail;
445	}
446
447	/*
448	 * Reset to a stable state.
449	 */
450	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
451	DELAY(10);
452
453	/*
454	 * Find out how large of an SEEPROM we have.
455	 */
456	fxp_autosize_eeprom(sc);
457
458	/*
459	 * Determine whether we must use the 503 serial interface.
460	 */
461	fxp_read_eeprom(sc, &data, 6, 1);
462	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
463	    (data & FXP_PHY_SERIAL_ONLY))
464		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
465
466	/*
467	 * Create the sysctl tree
468	 */
469	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
470	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
471	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
472	if (sc->sysctl_tree == NULL)
473		goto fail;
474	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
475	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
476	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
477	    "FXP driver receive interrupt microcode bundling delay");
478	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
479	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
480	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
481	    "FXP driver receive interrupt microcode bundle size limit");
482
483	/*
484	 * Pull in device tunables.
485	 */
486	sc->tunable_int_delay = TUNABLE_INT_DELAY;
487	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
488	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
489	    "int_delay", &sc->tunable_int_delay);
490	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
491	    "bundle_max", &sc->tunable_bundle_max);
492
493	/*
494	 * Find out the chip revision; lump all 82557 revs together.
495	 */
496	fxp_read_eeprom(sc, &data, 5, 1);
497	if ((data >> 8) == 1)
498		sc->revision = FXP_REV_82557;
499	else
500		sc->revision = pci_get_revid(dev);
501
502	/*
503	 * Enable workarounds for certain chip revision deficiencies.
504	 *
505	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
506	 * some systems based a normal 82559 design, have a defect where
507	 * the chip can cause a PCI protocol violation if it receives
508	 * a CU_RESUME command when it is entering the IDLE state.  The
509	 * workaround is to disable Dynamic Standby Mode, so the chip never
510	 * deasserts CLKRUN#, and always remains in an active state.
511	 *
512	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
513	 */
514	i = pci_get_device(dev);
515	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
516	    sc->revision >= FXP_REV_82559_A0) {
517		fxp_read_eeprom(sc, &data, 10, 1);
518		if (data & 0x02) {			/* STB enable */
519			u_int16_t cksum;
520			int i;
521
522			device_printf(dev,
523			    "Disabling dynamic standby mode in EEPROM\n");
524			data &= ~0x02;
525			fxp_write_eeprom(sc, &data, 10, 1);
526			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
527			cksum = 0;
528			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
529				fxp_read_eeprom(sc, &data, i, 1);
530				cksum += data;
531			}
532			i = (1 << sc->eeprom_size) - 1;
533			cksum = 0xBABA - cksum;
534			fxp_read_eeprom(sc, &data, i, 1);
535			fxp_write_eeprom(sc, &cksum, i, 1);
536			device_printf(dev,
537			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
538			    i, data, cksum);
539#if 1
540			/*
541			 * If the user elects to continue, try the software
542			 * workaround, as it is better than nothing.
543			 */
544			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
545#endif
546		}
547	}
548
549	/*
550	 * If we are not a 82557 chip, we can enable extended features.
551	 */
552	if (sc->revision != FXP_REV_82557) {
553		/*
554		 * If MWI is enabled in the PCI configuration, and there
555		 * is a valid cacheline size (8 or 16 dwords), then tell
556		 * the board to turn on MWI.
557		 */
558		if (val & PCIM_CMD_MWRICEN &&
559		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
560			sc->flags |= FXP_FLAG_MWI_ENABLE;
561
562		/* turn on the extended TxCB feature */
563		sc->flags |= FXP_FLAG_EXT_TXCB;
564
565		/* enable reception of long frames for VLAN */
566		sc->flags |= FXP_FLAG_LONG_PKT_EN;
567	}
568
569	/*
570	 * Enable use of extended RFDs and TCBs for 82550
571	 * and later chips. Note: we need extended TXCB support
572	 * too, but that's already enabled by the code above.
573	 * Be careful to do this only on the right devices.
574	 */
575
576	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C) {
577		sc->rfa_size = sizeof (struct fxp_rfa);
578		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
579		sc->flags |= FXP_FLAG_EXT_RFA;
580	} else {
581		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
582		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
583	}
584
585	/*
586	 * Allocate DMA tags and DMA safe memory.
587	 */
588	maxtxseg = sc->flags & FXP_FLAG_EXT_RFA ? FXP_NTXSEG - 1 : FXP_NTXSEG;
589	error = bus_dma_tag_create(NULL, 2, 0, BUS_SPACE_MAXADDR_32BIT,
590	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * maxtxseg,
591	    maxtxseg, MCLBYTES, 0, &sc->fxp_mtag);
592	if (error) {
593		device_printf(dev, "could not allocate dma tag\n");
594		goto fail;
595	}
596
597	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
598	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_stats), 1,
599	    sizeof(struct fxp_stats), 0, &sc->fxp_stag);
600	if (error) {
601		device_printf(dev, "could not allocate dma tag\n");
602		goto fail;
603	}
604
605	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
606	    BUS_DMA_NOWAIT, &sc->fxp_smap);
607	if (error)
608		goto failmem;
609	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
610	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
611	if (error) {
612		device_printf(dev, "could not map the stats buffer\n");
613		goto fail;
614	}
615	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
616
617	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
618	    BUS_SPACE_MAXADDR, NULL, NULL, FXP_TXCB_SZ, 1,
619	    FXP_TXCB_SZ, 0, &sc->cbl_tag);
620	if (error) {
621		device_printf(dev, "could not allocate dma tag\n");
622		goto fail;
623	}
624
625	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
626	    BUS_DMA_NOWAIT, &sc->cbl_map);
627	if (error)
628		goto failmem;
629	bzero(sc->fxp_desc.cbl_list, FXP_TXCB_SZ);
630
631	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
632	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
633	    &sc->fxp_desc.cbl_addr, 0);
634	if (error) {
635		device_printf(dev, "could not map DMA memory\n");
636		goto fail;
637	}
638
639	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
640	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_cb_mcs), 1,
641	    sizeof(struct fxp_cb_mcs), 0, &sc->mcs_tag);
642	if (error) {
643		device_printf(dev, "could not allocate dma tag\n");
644		goto fail;
645	}
646
647	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
648	    BUS_DMA_NOWAIT, &sc->mcs_map);
649	if (error)
650		goto failmem;
651	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
652	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
653	if (error) {
654		device_printf(dev, "can't map the multicast setup command\n");
655		goto fail;
656	}
657
658	/*
659	 * Pre-allocate the TX DMA maps.
660	 */
661	for (i = 0; i < FXP_NTXCB; i++) {
662		error = bus_dmamap_create(sc->fxp_mtag, 0,
663		    &sc->fxp_desc.tx_list[i].tx_map);
664		if (error) {
665			device_printf(dev, "can't create DMA map for TX\n");
666			goto fail;
667		}
668	}
669	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
670	if (error) {
671		device_printf(dev, "can't create spare DMA map\n");
672		goto fail;
673	}
674
675	/*
676	 * Pre-allocate our receive buffers.
677	 */
678	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
679	for (i = 0; i < FXP_NRFABUFS; i++) {
680		rxp = &sc->fxp_desc.rx_list[i];
681		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
682		if (error) {
683			device_printf(dev, "can't create DMA map for RX\n");
684			goto fail;
685		}
686		if (fxp_add_rfabuf(sc, rxp) != 0)
687			goto failmem;
688	}
689
690	/*
691	 * Read MAC address.
692	 */
693	fxp_read_eeprom(sc, myea, 0, 3);
694	sc->arpcom.ac_enaddr[0] = myea[0] & 0xff;
695	sc->arpcom.ac_enaddr[1] = myea[0] >> 8;
696	sc->arpcom.ac_enaddr[2] = myea[1] & 0xff;
697	sc->arpcom.ac_enaddr[3] = myea[1] >> 8;
698	sc->arpcom.ac_enaddr[4] = myea[2] & 0xff;
699	sc->arpcom.ac_enaddr[5] = myea[2] >> 8;
700	device_printf(dev, "Ethernet address %6D%s\n",
701	    sc->arpcom.ac_enaddr, ":",
702	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
703	if (bootverbose) {
704		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
705		    pci_get_vendor(dev), pci_get_device(dev),
706		    pci_get_subvendor(dev), pci_get_subdevice(dev),
707		    pci_get_revid(dev));
708		fxp_read_eeprom(sc, &data, 10, 1);
709		device_printf(dev, "Dynamic Standby mode is %s\n",
710		    data & 0x02 ? "enabled" : "disabled");
711	}
712
713	/*
714	 * If this is only a 10Mbps device, then there is no MII, and
715	 * the PHY will use a serial interface instead.
716	 *
717	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
718	 * doesn't have a programming interface of any sort.  The
719	 * media is sensed automatically based on how the link partner
720	 * is configured.  This is, in essence, manual configuration.
721	 */
722	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
723		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
724		    fxp_serial_ifmedia_sts);
725		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
726		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
727	} else {
728		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
729		    fxp_ifmedia_sts)) {
730	                device_printf(dev, "MII without any PHY!\n");
731			error = ENXIO;
732			goto fail;
733		}
734	}
735
736	ifp = &sc->arpcom.ac_if;
737	ifp->if_unit = device_get_unit(dev);
738	ifp->if_name = "fxp";
739	ifp->if_output = ether_output;
740	ifp->if_baudrate = 100000000;
741	ifp->if_init = fxp_init;
742	ifp->if_softc = sc;
743	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
744	ifp->if_ioctl = fxp_ioctl;
745	ifp->if_start = fxp_start;
746	ifp->if_watchdog = fxp_watchdog;
747
748	/* Enable checksum offload for 82550 or better chips */
749
750	if (sc->flags & FXP_FLAG_EXT_RFA) {
751		ifp->if_hwassist = FXP_CSUM_FEATURES;
752		ifp->if_capabilities = IFCAP_HWCSUM;
753		ifp->if_capenable = ifp->if_capabilities;
754	}
755
756	/*
757	 * Tell the upper layer(s) we support long frames.
758	 */
759	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
760	ifp->if_capabilities |= IFCAP_VLAN_MTU;
761
762	/*
763	 * Let the system queue as many packets as we have available
764	 * TX descriptors.
765	 */
766	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
767
768	/*
769	 * Attach the interface.
770	 */
771	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
772
773	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
774	    fxp_intr, sc, &sc->ih);
775	if (error) {
776		device_printf(dev, "could not setup irq\n");
777		goto fail;
778	}
779
780	splx(s);
781	return (0);
782
783failmem:
784	device_printf(dev, "Failed to malloc memory\n");
785	error = ENOMEM;
786fail:
787	splx(s);
788	fxp_release(sc);
789	return (error);
790}
791
792/*
793 * release all resources
794 */
795static void
796fxp_release(struct fxp_softc *sc)
797{
798	struct fxp_rx *rxp;
799	struct fxp_tx *txp;
800	int i;
801
802	for (i = 0; i < FXP_NRFABUFS; i++) {
803		rxp = &sc->fxp_desc.rx_list[i];
804		if (rxp->rx_mbuf != NULL) {
805			bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
806			    BUS_DMASYNC_POSTREAD);
807			bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
808			m_freem(rxp->rx_mbuf);
809		}
810		bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
811	}
812	bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
813
814	for (i = 0; i < FXP_NTXCB; i++) {
815		txp = &sc->fxp_desc.tx_list[i];
816		if (txp->tx_mbuf != NULL) {
817			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
818			    BUS_DMASYNC_POSTWRITE);
819			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
820			m_freem(txp->tx_mbuf);
821		}
822		bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
823	}
824
825	bus_generic_detach(sc->dev);
826	if (sc->miibus)
827		device_delete_child(sc->dev, sc->miibus);
828
829	if (sc->fxp_desc.cbl_list) {
830		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
831		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
832		    sc->cbl_map);
833	}
834	if (sc->fxp_stats) {
835		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
836		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
837	}
838	if (sc->mcsp) {
839		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
840		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
841	}
842	if (sc->ih)
843		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
844	if (sc->irq)
845		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
846	if (sc->mem)
847		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
848	if (sc->fxp_mtag)
849		bus_dma_tag_destroy(sc->fxp_mtag);
850	if (sc->fxp_stag)
851		bus_dma_tag_destroy(sc->fxp_stag);
852	if (sc->cbl_tag)
853		bus_dma_tag_destroy(sc->cbl_tag);
854	if (sc->mcs_tag)
855		bus_dma_tag_destroy(sc->mcs_tag);
856
857        sysctl_ctx_free(&sc->sysctl_ctx);
858
859	mtx_destroy(&sc->sc_mtx);
860}
861
862/*
863 * Detach interface.
864 */
865static int
866fxp_detach(device_t dev)
867{
868	struct fxp_softc *sc = device_get_softc(dev);
869	int s;
870
871	/* disable interrupts */
872	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
873
874	s = splimp();
875
876	/*
877	 * Stop DMA and drop transmit queue.
878	 */
879	fxp_stop(sc);
880
881	/*
882	 * Close down routes etc.
883	 */
884	ether_ifdetach(&sc->arpcom.ac_if);
885
886	/*
887	 * Free all media structures.
888	 */
889	ifmedia_removeall(&sc->sc_media);
890
891	splx(s);
892
893	/* Release our allocated resources. */
894	fxp_release(sc);
895
896	return (0);
897}
898
899/*
900 * Device shutdown routine. Called at system shutdown after sync. The
901 * main purpose of this routine is to shut off receiver DMA so that
902 * kernel memory doesn't get clobbered during warmboot.
903 */
904static int
905fxp_shutdown(device_t dev)
906{
907	/*
908	 * Make sure that DMA is disabled prior to reboot. Not doing
909	 * do could allow DMA to corrupt kernel memory during the
910	 * reboot before the driver initializes.
911	 */
912	fxp_stop((struct fxp_softc *) device_get_softc(dev));
913	return (0);
914}
915
916/*
917 * Device suspend routine.  Stop the interface and save some PCI
918 * settings in case the BIOS doesn't restore them properly on
919 * resume.
920 */
921static int
922fxp_suspend(device_t dev)
923{
924	struct fxp_softc *sc = device_get_softc(dev);
925	int i, s;
926
927	s = splimp();
928
929	fxp_stop(sc);
930
931	for (i = 0; i < 5; i++)
932		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
933	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
934	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
935	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
936	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
937
938	sc->suspended = 1;
939
940	splx(s);
941	return (0);
942}
943
944/*
945 * Device resume routine.  Restore some PCI settings in case the BIOS
946 * doesn't, re-enable busmastering, and restart the interface if
947 * appropriate.
948 */
949static int
950fxp_resume(device_t dev)
951{
952	struct fxp_softc *sc = device_get_softc(dev);
953	struct ifnet *ifp = &sc->sc_if;
954	u_int16_t pci_command;
955	int i, s;
956
957	s = splimp();
958
959	fxp_powerstate_d0(dev);
960
961	/* better way to do this? */
962	for (i = 0; i < 5; i++)
963		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
964	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
965	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
966	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
967	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
968
969	/* reenable busmastering */
970	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
971	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
972	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
973
974	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
975	DELAY(10);
976
977	/* reinitialize interface if necessary */
978	if (ifp->if_flags & IFF_UP)
979		fxp_init(sc);
980
981	sc->suspended = 0;
982
983	splx(s);
984	return (0);
985}
986
987static void
988fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
989{
990	u_int16_t reg;
991	int x;
992
993	/*
994	 * Shift in data.
995	 */
996	for (x = 1 << (length - 1); x; x >>= 1) {
997		if (data & x)
998			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
999		else
1000			reg = FXP_EEPROM_EECS;
1001		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1002		DELAY(1);
1003		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1004		DELAY(1);
1005		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1006		DELAY(1);
1007	}
1008}
1009
1010/*
1011 * Read from the serial EEPROM. Basically, you manually shift in
1012 * the read opcode (one bit at a time) and then shift in the address,
1013 * and then you shift out the data (all of this one bit at a time).
1014 * The word size is 16 bits, so you have to provide the address for
1015 * every 16 bits of data.
1016 */
1017static u_int16_t
1018fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1019{
1020	u_int16_t reg, data;
1021	int x;
1022
1023	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1024	/*
1025	 * Shift in read opcode.
1026	 */
1027	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1028	/*
1029	 * Shift in address.
1030	 */
1031	data = 0;
1032	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1033		if (offset & x)
1034			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1035		else
1036			reg = FXP_EEPROM_EECS;
1037		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1038		DELAY(1);
1039		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1040		DELAY(1);
1041		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1042		DELAY(1);
1043		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1044		data++;
1045		if (autosize && reg == 0) {
1046			sc->eeprom_size = data;
1047			break;
1048		}
1049	}
1050	/*
1051	 * Shift out data.
1052	 */
1053	data = 0;
1054	reg = FXP_EEPROM_EECS;
1055	for (x = 1 << 15; x; x >>= 1) {
1056		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1057		DELAY(1);
1058		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1059			data |= x;
1060		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1061		DELAY(1);
1062	}
1063	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1064	DELAY(1);
1065
1066	return (data);
1067}
1068
1069static void
1070fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
1071{
1072	int i;
1073
1074	/*
1075	 * Erase/write enable.
1076	 */
1077	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1078	fxp_eeprom_shiftin(sc, 0x4, 3);
1079	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
1080	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1081	DELAY(1);
1082	/*
1083	 * Shift in write opcode, address, data.
1084	 */
1085	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1086	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
1087	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
1088	fxp_eeprom_shiftin(sc, data, 16);
1089	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1090	DELAY(1);
1091	/*
1092	 * Wait for EEPROM to finish up.
1093	 */
1094	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1095	DELAY(1);
1096	for (i = 0; i < 1000; i++) {
1097		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1098			break;
1099		DELAY(50);
1100	}
1101	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1102	DELAY(1);
1103	/*
1104	 * Erase/write disable.
1105	 */
1106	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1107	fxp_eeprom_shiftin(sc, 0x4, 3);
1108	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
1109	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1110	DELAY(1);
1111}
1112
1113/*
1114 * From NetBSD:
1115 *
1116 * Figure out EEPROM size.
1117 *
1118 * 559's can have either 64-word or 256-word EEPROMs, the 558
1119 * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1120 * talks about the existance of 16 to 256 word EEPROMs.
1121 *
1122 * The only known sizes are 64 and 256, where the 256 version is used
1123 * by CardBus cards to store CIS information.
1124 *
1125 * The address is shifted in msb-to-lsb, and after the last
1126 * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1127 * after which follows the actual data. We try to detect this zero, by
1128 * probing the data-out bit in the EEPROM control register just after
1129 * having shifted in a bit. If the bit is zero, we assume we've
1130 * shifted enough address bits. The data-out should be tri-state,
1131 * before this, which should translate to a logical one.
1132 */
1133static void
1134fxp_autosize_eeprom(struct fxp_softc *sc)
1135{
1136
1137	/* guess maximum size of 256 words */
1138	sc->eeprom_size = 8;
1139
1140	/* autosize */
1141	(void) fxp_eeprom_getword(sc, 0, 1);
1142}
1143
1144static void
1145fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1146{
1147	int i;
1148
1149	for (i = 0; i < words; i++)
1150		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1151}
1152
1153static void
1154fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1155{
1156	int i;
1157
1158	for (i = 0; i < words; i++)
1159		fxp_eeprom_putword(sc, offset + i, data[i]);
1160}
1161
1162static void
1163fxp_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1164    bus_size_t mapsize, int error)
1165{
1166	struct fxp_softc *sc;
1167	struct fxp_cb_tx *txp;
1168	int i;
1169
1170	if (error)
1171		return;
1172
1173	KASSERT(nseg <= FXP_NTXSEG, ("too many DMA segments"));
1174
1175	sc = arg;
1176	txp = sc->fxp_desc.tx_last->tx_next->tx_cb;
1177	for (i = 0; i < nseg; i++) {
1178		KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large"));
1179		/*
1180		 * If this is an 82550/82551, then we're using extended
1181		 * TxCBs _and_ we're using checksum offload. This means
1182		 * that the TxCB is really an IPCB. One major difference
1183		 * between the two is that with plain extended TxCBs,
1184		 * the bottom half of the TxCB contains two entries from
1185		 * the TBD array, whereas IPCBs contain just one entry:
1186		 * one entry (8 bytes) has been sacrificed for the TCP/IP
1187		 * checksum offload control bits. So to make things work
1188		 * right, we have to start filling in the TBD array
1189		 * starting from a different place depending on whether
1190		 * the chip is an 82550/82551 or not.
1191		 */
1192		if (sc->flags & FXP_FLAG_EXT_RFA) {
1193			txp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
1194			txp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
1195		} else {
1196			txp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
1197			txp->tbd[i].tb_size = htole32(segs[i].ds_len);
1198		}
1199	}
1200	txp->tbd_number = nseg;
1201}
1202
1203/*
1204 * Start packet transmission on the interface.
1205 */
1206static void
1207fxp_start(struct ifnet *ifp)
1208{
1209	struct fxp_softc *sc = ifp->if_softc;
1210	struct fxp_tx *txp;
1211	struct mbuf *mb_head;
1212	int error;
1213
1214	/*
1215	 * See if we need to suspend xmit until the multicast filter
1216	 * has been reprogrammed (which can only be done at the head
1217	 * of the command chain).
1218	 */
1219	if (sc->need_mcsetup) {
1220		return;
1221	}
1222
1223	txp = NULL;
1224
1225	/*
1226	 * We're finished if there is nothing more to add to the list or if
1227	 * we're all filled up with buffers to transmit.
1228	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
1229	 *       a NOP command when needed.
1230	 */
1231	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1232
1233		/*
1234		 * Grab a packet to transmit.
1235		 */
1236		IF_DEQUEUE(&ifp->if_snd, mb_head);
1237
1238		/*
1239		 * Get pointer to next available tx desc.
1240		 */
1241		txp = sc->fxp_desc.tx_last->tx_next;
1242
1243		/*
1244		 * Deal with TCP/IP checksum offload. Note that
1245		 * in order for TCP checksum offload to work,
1246		 * the pseudo header checksum must have already
1247		 * been computed and stored in the checksum field
1248		 * in the TCP header. The stack should have
1249		 * already done this for us.
1250		 */
1251
1252		if (mb_head->m_pkthdr.csum_flags) {
1253			if (mb_head->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1254				txp->tx_cb->ipcb_ip_activation_high =
1255				    FXP_IPCB_HARDWAREPARSING_ENABLE;
1256				txp->tx_cb->ipcb_ip_schedule =
1257				    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1258				if (mb_head->m_pkthdr.csum_flags & CSUM_TCP)
1259					txp->tx_cb->ipcb_ip_schedule |=
1260					    FXP_IPCB_TCP_PACKET;
1261			}
1262#ifdef FXP_IP_CSUM_WAR
1263		/*
1264		 * XXX The 82550 chip appears to have trouble
1265		 * dealing with IP header checksums in very small
1266		 * datagrams, namely fragments from 1 to 3 bytes
1267		 * in size. For example, say you want to transmit
1268		 * a UDP packet of 1473 bytes. The packet will be
1269		 * fragmented over two IP datagrams, the latter
1270		 * containing only one byte of data. The 82550 will
1271		 * botch the header checksum on the 1-byte fragment.
1272		 * As long as the datagram contains 4 or more bytes
1273		 * of data, you're ok.
1274		 *
1275                 * The following code attempts to work around this
1276		 * problem: if the datagram is less than 38 bytes
1277		 * in size (14 bytes ether header, 20 bytes IP header,
1278		 * plus 4 bytes of data), we punt and compute the IP
1279		 * header checksum by hand. This workaround doesn't
1280		 * work very well, however, since it can be fooled
1281		 * by things like VLAN tags and IP options that make
1282		 * the header sizes/offsets vary.
1283		 */
1284
1285			if (mb_head->m_pkthdr.csum_flags & CSUM_IP) {
1286				if (mb_head->m_pkthdr.len < 38) {
1287					struct ip *ip;
1288					mb_head->m_data += ETHER_HDR_LEN;
1289					ip = mtod(mb_head, struct ip *);
1290					ip->ip_sum = in_cksum(mb_head,
1291					    ip->ip_hl << 2);
1292					mb_head->m_data -= ETHER_HDR_LEN;
1293				} else {
1294					txp->tx_cb->ipcb_ip_activation_high =
1295					    FXP_IPCB_HARDWAREPARSING_ENABLE;
1296					txp->tx_cb->ipcb_ip_schedule |=
1297					    FXP_IPCB_IP_CHECKSUM_ENABLE;
1298				}
1299			}
1300#endif
1301		}
1302
1303		/*
1304		 * Go through each of the mbufs in the chain and initialize
1305		 * the transmit buffer descriptors with the physical address
1306		 * and size of the mbuf.
1307		 */
1308		error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1309		    mb_head, fxp_dma_map_txbuf, sc, 0);
1310
1311		if (error && error != EFBIG) {
1312			device_printf(sc->dev, "can't map mbuf (error %d)\n",
1313			    error);
1314			m_freem(mb_head);
1315			break;
1316		}
1317
1318		if (error) {
1319			struct mbuf *mn;
1320
1321			/*
1322			 * We ran out of segments. We have to recopy this
1323			 * mbuf chain first. Bail out if we can't get the
1324			 * new buffers.
1325			 */
1326			MGETHDR(mn, M_DONTWAIT, MT_DATA);
1327			if (mn == NULL) {
1328				m_freem(mb_head);
1329				break;
1330			}
1331			if (mb_head->m_pkthdr.len > MHLEN) {
1332				MCLGET(mn, M_DONTWAIT);
1333				if ((mn->m_flags & M_EXT) == 0) {
1334					m_freem(mn);
1335					m_freem(mb_head);
1336					break;
1337				}
1338			}
1339			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1340			    mtod(mn, caddr_t));
1341			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
1342			m_freem(mb_head);
1343			mb_head = mn;
1344			error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1345			    mb_head, fxp_dma_map_txbuf, sc, 0);
1346			if (error) {
1347				device_printf(sc->dev,
1348				    "can't map mbuf (error %d)\n", error);
1349				m_freem(mb_head);
1350				break;
1351			}
1352		}
1353
1354		bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1355		    BUS_DMASYNC_PREWRITE);
1356
1357		txp->tx_mbuf = mb_head;
1358		txp->tx_cb->cb_status = 0;
1359		txp->tx_cb->byte_count = 0;
1360		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1361			txp->tx_cb->cb_command =
1362			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
1363			    FXP_CB_COMMAND_S);
1364		} else {
1365			txp->tx_cb->cb_command =
1366			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
1367			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1368			/*
1369			 * Set a 5 second timer just in case we don't hear
1370			 * from the card again.
1371			 */
1372			ifp->if_timer = 5;
1373		}
1374		txp->tx_cb->tx_threshold = tx_threshold;
1375
1376		/*
1377		 * Advance the end of list forward.
1378		 */
1379
1380#ifdef __alpha__
1381		/*
1382		 * On platforms which can't access memory in 16-bit
1383		 * granularities, we must prevent the card from DMA'ing
1384		 * up the status while we update the command field.
1385		 * This could cause us to overwrite the completion status.
1386		 */
1387		atomic_clear_short(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1388		    FXP_CB_COMMAND_S);
1389#else
1390		sc->fxp_desc.tx_last->tx_cb->cb_command &= ~FXP_CB_COMMAND_S;
1391#endif /*__alpha__*/
1392		sc->fxp_desc.tx_last = txp;
1393
1394		/*
1395		 * Advance the beginning of the list forward if there are
1396		 * no other packets queued (when nothing is queued, tx_first
1397		 * sits on the last TxCB that was sent out).
1398		 */
1399		if (sc->tx_queued == 0)
1400			sc->fxp_desc.tx_first = txp;
1401
1402		sc->tx_queued++;
1403
1404		/*
1405		 * Pass packet to bpf if there is a listener.
1406		 */
1407		BPF_MTAP(ifp, mb_head);
1408	}
1409	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1410
1411	/*
1412	 * We're finished. If we added to the list, issue a RESUME to get DMA
1413	 * going again if suspended.
1414	 */
1415	if (txp != NULL) {
1416		fxp_scb_wait(sc);
1417		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1418	}
1419}
1420
1421static void fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count);
1422
1423#ifdef DEVICE_POLLING
1424static poll_handler_t fxp_poll;
1425
1426static void
1427fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1428{
1429	struct fxp_softc *sc = ifp->if_softc;
1430	u_int8_t statack;
1431
1432	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1433		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1434		return;
1435	}
1436	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1437	    FXP_SCB_STATACK_FR;
1438	if (cmd == POLL_AND_CHECK_STATUS) {
1439		u_int8_t tmp;
1440
1441		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1442		if (tmp == 0xff || tmp == 0)
1443			return; /* nothing to do */
1444		tmp &= ~statack;
1445		/* ack what we can */
1446		if (tmp != 0)
1447			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1448		statack |= tmp;
1449	}
1450	fxp_intr_body(sc, statack, count);
1451}
1452#endif /* DEVICE_POLLING */
1453
1454/*
1455 * Process interface interrupts.
1456 */
1457static void
1458fxp_intr(void *xsc)
1459{
1460	struct fxp_softc *sc = xsc;
1461	u_int8_t statack;
1462
1463#ifdef DEVICE_POLLING
1464	struct ifnet *ifp = &sc->sc_if;
1465
1466	if (ifp->if_flags & IFF_POLLING)
1467		return;
1468	if (ether_poll_register(fxp_poll, ifp)) {
1469		/* disable interrupts */
1470		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
1471		fxp_poll(ifp, 0, 1);
1472		return;
1473	}
1474#endif
1475
1476	if (sc->suspended) {
1477		return;
1478	}
1479
1480	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1481		/*
1482		 * It should not be possible to have all bits set; the
1483		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
1484		 * all bits are set, this may indicate that the card has
1485		 * been physically ejected, so ignore it.
1486		 */
1487		if (statack == 0xff)
1488			return;
1489
1490		/*
1491		 * First ACK all the interrupts in this pass.
1492		 */
1493		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1494		fxp_intr_body(sc, statack, -1);
1495	}
1496}
1497
1498static void
1499fxp_txeof(struct fxp_softc *sc)
1500{
1501	struct fxp_tx *txp;
1502
1503	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1504	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
1505	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1506	    txp = txp->tx_next) {
1507		if (txp->tx_mbuf != NULL) {
1508			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1509			    BUS_DMASYNC_POSTWRITE);
1510			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1511			m_freem(txp->tx_mbuf);
1512			txp->tx_mbuf = NULL;
1513			/* clear this to reset csum offload bits */
1514			txp->tx_cb->tbd[0].tb_addr = 0;
1515		}
1516		sc->tx_queued--;
1517	}
1518	sc->fxp_desc.tx_first = txp;
1519	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1520}
1521
1522static void
1523fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count)
1524{
1525	struct ifnet *ifp = &sc->sc_if;
1526	struct mbuf *m;
1527	struct fxp_rx *rxp;
1528	struct fxp_rfa *rfa;
1529	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
1530
1531	if (rnr)
1532		fxp_rnr++;
1533#ifdef DEVICE_POLLING
1534	/* Pick up a deferred RNR condition if `count' ran out last time. */
1535	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1536		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1537		rnr = 1;
1538	}
1539#endif
1540
1541	/*
1542	 * Free any finished transmit mbuf chains.
1543	 *
1544	 * Handle the CNA event likt a CXTNO event. It used to
1545	 * be that this event (control unit not ready) was not
1546	 * encountered, but it is now with the SMPng modifications.
1547	 * The exact sequence of events that occur when the interface
1548	 * is brought up are different now, and if this event
1549	 * goes unhandled, the configuration/rxfilter setup sequence
1550	 * can stall for several seconds. The result is that no
1551	 * packets go out onto the wire for about 5 to 10 seconds
1552	 * after the interface is ifconfig'ed for the first time.
1553	 */
1554	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
1555		fxp_txeof(sc);
1556
1557		ifp->if_timer = 0;
1558		if (sc->tx_queued == 0) {
1559			if (sc->need_mcsetup)
1560				fxp_mc_setup(sc);
1561		}
1562		/*
1563		 * Try to start more packets transmitting.
1564		 */
1565		if (ifp->if_snd.ifq_head != NULL)
1566			fxp_start(ifp);
1567	}
1568
1569	/*
1570	 * Just return if nothing happened on the receive side.
1571	 */
1572	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
1573		return;
1574
1575	/*
1576	 * Process receiver interrupts. If a no-resource (RNR)
1577	 * condition exists, get whatever packets we can and
1578	 * re-start the receiver.
1579	 *
1580	 * When using polling, we do not process the list to completion,
1581	 * so when we get an RNR interrupt we must defer the restart
1582	 * until we hit the last buffer with the C bit set.
1583	 * If we run out of cycles and rfa_headm has the C bit set,
1584	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1585	 * that the info will be used in the subsequent polling cycle.
1586	 */
1587	for (;;) {
1588		rxp = sc->fxp_desc.rx_head;
1589		m = rxp->rx_mbuf;
1590		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1591		    RFA_ALIGNMENT_FUDGE);
1592		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1593		    BUS_DMASYNC_POSTREAD);
1594
1595#ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1596		if (count >= 0 && count-- == 0) {
1597			if (rnr) {
1598				/* Defer RNR processing until the next time. */
1599				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1600				rnr = 0;
1601			}
1602			break;
1603		}
1604#endif /* DEVICE_POLLING */
1605
1606		if ((le16toh(rfa->rfa_status) & FXP_RFA_STATUS_C) == 0)
1607			break;
1608
1609		/*
1610		 * Advance head forward.
1611		 */
1612		sc->fxp_desc.rx_head = rxp->rx_next;
1613
1614		/*
1615		 * Add a new buffer to the receive chain.
1616		 * If this fails, the old buffer is recycled
1617		 * instead.
1618		 */
1619		if (fxp_add_rfabuf(sc, rxp) == 0) {
1620			int total_len;
1621
1622			/*
1623			 * Fetch packet length (the top 2 bits of
1624			 * actual_size are flags set by the controller
1625			 * upon completion), and drop the packet in case
1626			 * of bogus length or CRC errors.
1627			 */
1628			total_len = le16toh(rfa->actual_size & 0x3fff);
1629			if (total_len < sizeof(struct ether_header) ||
1630			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
1631				sc->rfa_size ||
1632			    le16toh(rfa->rfa_status) & FXP_RFA_STATUS_CRC) {
1633				m_freem(m);
1634				continue;
1635			}
1636
1637                        /* Do IP checksum checking. */
1638			if (le16toh(rfa->rfa_status) & FXP_RFA_STATUS_PARSE) {
1639				if (rfa->rfax_csum_sts &
1640				    FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1641					m->m_pkthdr.csum_flags |=
1642					    CSUM_IP_CHECKED;
1643				if (rfa->rfax_csum_sts &
1644				    FXP_RFDX_CS_IP_CSUM_VALID)
1645					m->m_pkthdr.csum_flags |=
1646					    CSUM_IP_VALID;
1647				if ((rfa->rfax_csum_sts &
1648				    FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1649				    (rfa->rfax_csum_sts &
1650				    FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1651					m->m_pkthdr.csum_flags |=
1652					    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1653					m->m_pkthdr.csum_data = 0xffff;
1654				}
1655			}
1656
1657			m->m_pkthdr.len = m->m_len = total_len;
1658			m->m_pkthdr.rcvif = ifp;
1659
1660			(*ifp->if_input)(ifp, m);
1661		}
1662	}
1663	if (rnr) {
1664		fxp_scb_wait(sc);
1665		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1666		    sc->fxp_desc.rx_head->rx_addr);
1667		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1668	}
1669}
1670
1671/*
1672 * Update packet in/out/collision statistics. The i82557 doesn't
1673 * allow you to access these counters without doing a fairly
1674 * expensive DMA to get _all_ of the statistics it maintains, so
1675 * we do this operation here only once per second. The statistics
1676 * counters in the kernel are updated from the previous dump-stats
1677 * DMA and then a new dump-stats DMA is started. The on-chip
1678 * counters are zeroed when the DMA completes. If we can't start
1679 * the DMA immediately, we don't wait - we just prepare to read
1680 * them again next time.
1681 */
1682static void
1683fxp_tick(void *xsc)
1684{
1685	struct fxp_softc *sc = xsc;
1686	struct ifnet *ifp = &sc->sc_if;
1687	struct fxp_stats *sp = sc->fxp_stats;
1688	int s;
1689
1690	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
1691	ifp->if_opackets += le32toh(sp->tx_good);
1692	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1693	if (sp->rx_good) {
1694		ifp->if_ipackets += le32toh(sp->rx_good);
1695		sc->rx_idle_secs = 0;
1696	} else {
1697		/*
1698		 * Receiver's been idle for another second.
1699		 */
1700		sc->rx_idle_secs++;
1701	}
1702	ifp->if_ierrors +=
1703	    le32toh(sp->rx_crc_errors) +
1704	    le32toh(sp->rx_alignment_errors) +
1705	    le32toh(sp->rx_rnr_errors) +
1706	    le32toh(sp->rx_overrun_errors);
1707	/*
1708	 * If any transmit underruns occured, bump up the transmit
1709	 * threshold by another 512 bytes (64 * 8).
1710	 */
1711	if (sp->tx_underruns) {
1712		ifp->if_oerrors += le32toh(sp->tx_underruns);
1713		if (tx_threshold < 192)
1714			tx_threshold += 64;
1715	}
1716	s = splimp();
1717	/*
1718	 * Release any xmit buffers that have completed DMA. This isn't
1719	 * strictly necessary to do here, but it's advantagous for mbufs
1720	 * with external storage to be released in a timely manner rather
1721	 * than being defered for a potentially long time. This limits
1722	 * the delay to a maximum of one second.
1723	 */
1724	fxp_txeof(sc);
1725
1726	/*
1727	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1728	 * then assume the receiver has locked up and attempt to clear
1729	 * the condition by reprogramming the multicast filter. This is
1730	 * a work-around for a bug in the 82557 where the receiver locks
1731	 * up if it gets certain types of garbage in the syncronization
1732	 * bits prior to the packet header. This bug is supposed to only
1733	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1734	 * mode as well (perhaps due to a 10/100 speed transition).
1735	 */
1736	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1737		sc->rx_idle_secs = 0;
1738		fxp_mc_setup(sc);
1739	}
1740	/*
1741	 * If there is no pending command, start another stats
1742	 * dump. Otherwise punt for now.
1743	 */
1744	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1745		/*
1746		 * Start another stats dump.
1747		 */
1748		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
1749		    BUS_DMASYNC_PREREAD);
1750		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1751	} else {
1752		/*
1753		 * A previous command is still waiting to be accepted.
1754		 * Just zero our copy of the stats and wait for the
1755		 * next timer event to update them.
1756		 */
1757		sp->tx_good = 0;
1758		sp->tx_underruns = 0;
1759		sp->tx_total_collisions = 0;
1760
1761		sp->rx_good = 0;
1762		sp->rx_crc_errors = 0;
1763		sp->rx_alignment_errors = 0;
1764		sp->rx_rnr_errors = 0;
1765		sp->rx_overrun_errors = 0;
1766	}
1767	if (sc->miibus != NULL)
1768		mii_tick(device_get_softc(sc->miibus));
1769	splx(s);
1770	/*
1771	 * Schedule another timeout one second from now.
1772	 */
1773	sc->stat_ch = timeout(fxp_tick, sc, hz);
1774}
1775
1776/*
1777 * Stop the interface. Cancels the statistics updater and resets
1778 * the interface.
1779 */
1780static void
1781fxp_stop(struct fxp_softc *sc)
1782{
1783	struct ifnet *ifp = &sc->sc_if;
1784	struct fxp_tx *txp;
1785	int i;
1786
1787	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1788	ifp->if_timer = 0;
1789
1790#ifdef DEVICE_POLLING
1791	ether_poll_deregister(ifp);
1792#endif
1793	/*
1794	 * Cancel stats updater.
1795	 */
1796	untimeout(fxp_tick, sc, sc->stat_ch);
1797
1798	/*
1799	 * Issue software reset, which also unloads the microcode.
1800	 */
1801	sc->flags &= ~FXP_FLAG_UCODE;
1802	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
1803	DELAY(50);
1804
1805	/*
1806	 * Release any xmit buffers.
1807	 */
1808	txp = sc->fxp_desc.tx_list;
1809	if (txp != NULL) {
1810		for (i = 0; i < FXP_NTXCB; i++) {
1811			if (txp[i].tx_mbuf != NULL) {
1812				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
1813				    BUS_DMASYNC_POSTWRITE);
1814				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
1815				m_freem(txp[i].tx_mbuf);
1816				txp[i].tx_mbuf = NULL;
1817				/* clear this to reset csum offload bits */
1818				txp[i].tx_cb->tbd[0].tb_addr = 0;
1819			}
1820		}
1821	}
1822	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1823	sc->tx_queued = 0;
1824}
1825
1826/*
1827 * Watchdog/transmission transmit timeout handler. Called when a
1828 * transmission is started on the interface, but no interrupt is
1829 * received before the timeout. This usually indicates that the
1830 * card has wedged for some reason.
1831 */
1832static void
1833fxp_watchdog(struct ifnet *ifp)
1834{
1835	struct fxp_softc *sc = ifp->if_softc;
1836
1837	device_printf(sc->dev, "device timeout\n");
1838	ifp->if_oerrors++;
1839
1840	fxp_init(sc);
1841}
1842
1843static void
1844fxp_init(void *xsc)
1845{
1846	struct fxp_softc *sc = xsc;
1847	struct ifnet *ifp = &sc->sc_if;
1848	struct fxp_cb_config *cbp;
1849	struct fxp_cb_ias *cb_ias;
1850	struct fxp_cb_tx *tcbp;
1851	struct fxp_tx *txp;
1852	struct fxp_cb_mcs *mcsp;
1853	int i, prm, s;
1854
1855	s = splimp();
1856	/*
1857	 * Cancel any pending I/O
1858	 */
1859	fxp_stop(sc);
1860
1861	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1862
1863	/*
1864	 * Initialize base of CBL and RFA memory. Loading with zero
1865	 * sets it up for regular linear addressing.
1866	 */
1867	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1868	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1869
1870	fxp_scb_wait(sc);
1871	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1872
1873	/*
1874	 * Initialize base of dump-stats buffer.
1875	 */
1876	fxp_scb_wait(sc);
1877	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
1878	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
1879	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1880
1881	/*
1882	 * Attempt to load microcode if requested.
1883	 */
1884	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
1885		fxp_load_ucode(sc);
1886
1887	/*
1888	 * Initialize the multicast address list.
1889	 */
1890	if (fxp_mc_addrs(sc)) {
1891		mcsp = sc->mcsp;
1892		mcsp->cb_status = 0;
1893		mcsp->cb_command =
1894		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
1895		mcsp->link_addr = 0xffffffff;
1896		/*
1897	 	 * Start the multicast setup command.
1898		 */
1899		fxp_scb_wait(sc);
1900		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
1901		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
1902		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1903		/* ...and wait for it to complete. */
1904		fxp_dma_wait(&mcsp->cb_status, sc);
1905		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
1906		    BUS_DMASYNC_POSTWRITE);
1907	}
1908
1909	/*
1910	 * We temporarily use memory that contains the TxCB list to
1911	 * construct the config CB. The TxCB list memory is rebuilt
1912	 * later.
1913	 */
1914	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
1915
1916	/*
1917	 * This bcopy is kind of disgusting, but there are a bunch of must be
1918	 * zero and must be one bits in this structure and this is the easiest
1919	 * way to initialize them all to proper values.
1920	 */
1921	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
1922
1923	cbp->cb_status =	0;
1924	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
1925	    FXP_CB_COMMAND_EL);
1926	cbp->link_addr =	0xffffffff;	/* (no) next command */
1927	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
1928	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1929	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1930	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1931	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1932	cbp->type_enable =	0;	/* actually reserved */
1933	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1934	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1935	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1936	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1937	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1938	cbp->late_scb =		0;	/* (don't) defer SCB update */
1939	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1940	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
1941	cbp->ci_int =		1;	/* interrupt on CU idle */
1942	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1943	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1944	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
1945	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
1946	cbp->disc_short_rx =	!prm;	/* discard short packets */
1947	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1948	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1949	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1950	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
1951	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1952	cbp->csma_dis =		0;	/* (don't) disable link */
1953	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1954	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1955	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1956	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1957	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1958	cbp->nsai =		1;	/* (don't) disable source addr insert */
1959	cbp->preamble_length =	2;	/* (7 byte) preamble */
1960	cbp->loopback =		0;	/* (don't) loopback */
1961	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1962	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1963	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1964	cbp->promiscuous =	prm;	/* promiscuous mode */
1965	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1966	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1967	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1968	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1969	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1970
1971	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1972	cbp->padding =		1;	/* (do) pad short tx packets */
1973	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1974	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1975	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1976	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1977					/* must set wake_en in PMCSR also */
1978	cbp->force_fdx =	0;	/* (don't) force full duplex */
1979	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1980	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1981	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1982	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
1983
1984	if (sc->revision == FXP_REV_82557) {
1985		/*
1986		 * The 82557 has no hardware flow control, the values
1987		 * below are the defaults for the chip.
1988		 */
1989		cbp->fc_delay_lsb =	0;
1990		cbp->fc_delay_msb =	0x40;
1991		cbp->pri_fc_thresh =	3;
1992		cbp->tx_fc_dis =	0;
1993		cbp->rx_fc_restop =	0;
1994		cbp->rx_fc_restart =	0;
1995		cbp->fc_filter =	0;
1996		cbp->pri_fc_loc =	1;
1997	} else {
1998		cbp->fc_delay_lsb =	0x1f;
1999		cbp->fc_delay_msb =	0x01;
2000		cbp->pri_fc_thresh =	3;
2001		cbp->tx_fc_dis =	0;	/* enable transmit FC */
2002		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
2003		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
2004		cbp->fc_filter =	!prm;	/* drop FC frames to host */
2005		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
2006	}
2007
2008	/*
2009	 * Start the config command/DMA.
2010	 */
2011	fxp_scb_wait(sc);
2012	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2013	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
2014	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2015	/* ...and wait for it to complete. */
2016	fxp_dma_wait(&cbp->cb_status, sc);
2017	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2018
2019	/*
2020	 * Now initialize the station address. Temporarily use the TxCB
2021	 * memory area like we did above for the config CB.
2022	 */
2023	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2024	cb_ias->cb_status = 0;
2025	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
2026	cb_ias->link_addr = 0xffffffff;
2027	bcopy(sc->arpcom.ac_enaddr, cb_ias->macaddr,
2028	    sizeof(sc->arpcom.ac_enaddr));
2029
2030	/*
2031	 * Start the IAS (Individual Address Setup) command/DMA.
2032	 */
2033	fxp_scb_wait(sc);
2034	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2035	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2036	/* ...and wait for it to complete. */
2037	fxp_dma_wait(&cb_ias->cb_status, sc);
2038	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2039
2040	/*
2041	 * Initialize transmit control block (TxCB) list.
2042	 */
2043	txp = sc->fxp_desc.tx_list;
2044	tcbp = sc->fxp_desc.cbl_list;
2045	bzero(tcbp, FXP_TXCB_SZ);
2046	for (i = 0; i < FXP_NTXCB; i++) {
2047		txp[i].tx_cb = tcbp + i;
2048		txp[i].tx_mbuf = NULL;
2049		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
2050		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
2051		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
2052		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
2053		if (sc->flags & FXP_FLAG_EXT_TXCB)
2054			tcbp[i].tbd_array_addr =
2055			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
2056		else
2057			tcbp[i].tbd_array_addr =
2058			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2059		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2060	}
2061	/*
2062	 * Set the suspend flag on the first TxCB and start the control
2063	 * unit. It will execute the NOP and then suspend.
2064	 */
2065	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2066	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2067	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2068	sc->tx_queued = 1;
2069
2070	fxp_scb_wait(sc);
2071	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2072
2073	/*
2074	 * Initialize receiver buffer area - RFA.
2075	 */
2076	fxp_scb_wait(sc);
2077	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
2078	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2079
2080	/*
2081	 * Set current media.
2082	 */
2083	if (sc->miibus != NULL)
2084		mii_mediachg(device_get_softc(sc->miibus));
2085
2086	ifp->if_flags |= IFF_RUNNING;
2087	ifp->if_flags &= ~IFF_OACTIVE;
2088
2089	/*
2090	 * Enable interrupts.
2091	 */
2092#ifdef DEVICE_POLLING
2093	/*
2094	 * ... but only do that if we are not polling. And because (presumably)
2095	 * the default is interrupts on, we need to disable them explicitly!
2096	 */
2097	if ( ifp->if_flags & IFF_POLLING )
2098		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2099	else
2100#endif /* DEVICE_POLLING */
2101	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2102	splx(s);
2103
2104	/*
2105	 * Start stats updater.
2106	 */
2107	sc->stat_ch = timeout(fxp_tick, sc, hz);
2108}
2109
2110static int
2111fxp_serial_ifmedia_upd(struct ifnet *ifp)
2112{
2113
2114	return (0);
2115}
2116
2117static void
2118fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2119{
2120
2121	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2122}
2123
2124/*
2125 * Change media according to request.
2126 */
2127static int
2128fxp_ifmedia_upd(struct ifnet *ifp)
2129{
2130	struct fxp_softc *sc = ifp->if_softc;
2131	struct mii_data *mii;
2132
2133	mii = device_get_softc(sc->miibus);
2134	mii_mediachg(mii);
2135	return (0);
2136}
2137
2138/*
2139 * Notify the world which media we're using.
2140 */
2141static void
2142fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2143{
2144	struct fxp_softc *sc = ifp->if_softc;
2145	struct mii_data *mii;
2146
2147	mii = device_get_softc(sc->miibus);
2148	mii_pollstat(mii);
2149	ifmr->ifm_active = mii->mii_media_active;
2150	ifmr->ifm_status = mii->mii_media_status;
2151
2152	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
2153		sc->cu_resume_bug = 1;
2154	else
2155		sc->cu_resume_bug = 0;
2156}
2157
2158/*
2159 * Add a buffer to the end of the RFA buffer list.
2160 * Return 0 if successful, 1 for failure. A failure results in
2161 * adding the 'oldm' (if non-NULL) on to the end of the list -
2162 * tossing out its old contents and recycling it.
2163 * The RFA struct is stuck at the beginning of mbuf cluster and the
2164 * data pointer is fixed up to point just past it.
2165 */
2166static int
2167fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2168{
2169	struct mbuf *m;
2170	struct fxp_rfa *rfa, *p_rfa;
2171	struct fxp_rx *p_rx;
2172	bus_dmamap_t tmp_map;
2173	int error;
2174
2175	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2176	if (m == NULL)
2177		return (ENOBUFS);
2178
2179	/*
2180	 * Move the data pointer up so that the incoming data packet
2181	 * will be 32-bit aligned.
2182	 */
2183	m->m_data += RFA_ALIGNMENT_FUDGE;
2184
2185	/*
2186	 * Get a pointer to the base of the mbuf cluster and move
2187	 * data start past it.
2188	 */
2189	rfa = mtod(m, struct fxp_rfa *);
2190	m->m_data += sc->rfa_size;
2191	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2192
2193	/*
2194	 * Initialize the rest of the RFA.  Note that since the RFA
2195	 * is misaligned, we cannot store values directly.  Instead,
2196	 * we use an optimized, inline copy.
2197	 */
2198
2199	rfa->rfa_status = 0;
2200	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2201	rfa->actual_size = 0;
2202
2203	le32enc(&rfa->link_addr, 0xffffffff);
2204	le32enc(&rfa->rbd_addr, 0xffffffff);
2205
2206	/* Map the RFA into DMA memory. */
2207	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2208	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2209	    &rxp->rx_addr, 0);
2210	if (error) {
2211		m_freem(m);
2212		return (error);
2213	}
2214
2215	bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2216	tmp_map = sc->spare_map;
2217	sc->spare_map = rxp->rx_map;
2218	rxp->rx_map = tmp_map;
2219	rxp->rx_mbuf = m;
2220
2221	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, BUS_DMASYNC_PREWRITE);
2222
2223	/*
2224	 * If there are other buffers already on the list, attach this
2225	 * one to the end by fixing up the tail to point to this one.
2226	 */
2227	if (sc->fxp_desc.rx_head != NULL) {
2228		p_rx = sc->fxp_desc.rx_tail;
2229		p_rfa = (struct fxp_rfa *)
2230		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2231		p_rx->rx_next = rxp;
2232		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2233		p_rfa->rfa_control = 0;
2234		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
2235		    BUS_DMASYNC_PREWRITE);
2236	} else {
2237		rxp->rx_next = NULL;
2238		sc->fxp_desc.rx_head = rxp;
2239	}
2240	sc->fxp_desc.rx_tail = rxp;
2241	return (0);
2242}
2243
2244static volatile int
2245fxp_miibus_readreg(device_t dev, int phy, int reg)
2246{
2247	struct fxp_softc *sc = device_get_softc(dev);
2248	int count = 10000;
2249	int value;
2250
2251	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2252	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2253
2254	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2255	    && count--)
2256		DELAY(10);
2257
2258	if (count <= 0)
2259		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2260
2261	return (value & 0xffff);
2262}
2263
2264static void
2265fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2266{
2267	struct fxp_softc *sc = device_get_softc(dev);
2268	int count = 10000;
2269
2270	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2271	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2272	    (value & 0xffff));
2273
2274	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2275	    count--)
2276		DELAY(10);
2277
2278	if (count <= 0)
2279		device_printf(dev, "fxp_miibus_writereg: timed out\n");
2280}
2281
2282static int
2283fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2284{
2285	struct fxp_softc *sc = ifp->if_softc;
2286	struct ifreq *ifr = (struct ifreq *)data;
2287	struct mii_data *mii;
2288	int s, error = 0;
2289
2290	s = splimp();
2291
2292	switch (command) {
2293	case SIOCSIFFLAGS:
2294		if (ifp->if_flags & IFF_ALLMULTI)
2295			sc->flags |= FXP_FLAG_ALL_MCAST;
2296		else
2297			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2298
2299		/*
2300		 * If interface is marked up and not running, then start it.
2301		 * If it is marked down and running, stop it.
2302		 * XXX If it's up then re-initialize it. This is so flags
2303		 * such as IFF_PROMISC are handled.
2304		 */
2305		if (ifp->if_flags & IFF_UP) {
2306			fxp_init(sc);
2307		} else {
2308			if (ifp->if_flags & IFF_RUNNING)
2309				fxp_stop(sc);
2310		}
2311		break;
2312
2313	case SIOCADDMULTI:
2314	case SIOCDELMULTI:
2315		if (ifp->if_flags & IFF_ALLMULTI)
2316			sc->flags |= FXP_FLAG_ALL_MCAST;
2317		else
2318			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2319		/*
2320		 * Multicast list has changed; set the hardware filter
2321		 * accordingly.
2322		 */
2323		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2324			fxp_mc_setup(sc);
2325		/*
2326		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2327		 * again rather than else {}.
2328		 */
2329		if (sc->flags & FXP_FLAG_ALL_MCAST)
2330			fxp_init(sc);
2331		error = 0;
2332		break;
2333
2334	case SIOCSIFMEDIA:
2335	case SIOCGIFMEDIA:
2336		if (sc->miibus != NULL) {
2337			mii = device_get_softc(sc->miibus);
2338                        error = ifmedia_ioctl(ifp, ifr,
2339                            &mii->mii_media, command);
2340		} else {
2341                        error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2342		}
2343		break;
2344
2345	default:
2346		error = ether_ioctl(ifp, command, data);
2347	}
2348	splx(s);
2349	return (error);
2350}
2351
2352/*
2353 * Fill in the multicast address list and return number of entries.
2354 */
2355static int
2356fxp_mc_addrs(struct fxp_softc *sc)
2357{
2358	struct fxp_cb_mcs *mcsp = sc->mcsp;
2359	struct ifnet *ifp = &sc->sc_if;
2360	struct ifmultiaddr *ifma;
2361	int nmcasts;
2362
2363	nmcasts = 0;
2364	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
2365#if __FreeBSD_version < 500000
2366		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2367#else
2368		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2369#endif
2370			if (ifma->ifma_addr->sa_family != AF_LINK)
2371				continue;
2372			if (nmcasts >= MAXMCADDR) {
2373				sc->flags |= FXP_FLAG_ALL_MCAST;
2374				nmcasts = 0;
2375				break;
2376			}
2377			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2378			    &sc->mcsp->mc_addr[nmcasts][0], 6);
2379			nmcasts++;
2380		}
2381	}
2382	mcsp->mc_cnt = htole16(nmcasts * 6);
2383	return (nmcasts);
2384}
2385
2386/*
2387 * Program the multicast filter.
2388 *
2389 * We have an artificial restriction that the multicast setup command
2390 * must be the first command in the chain, so we take steps to ensure
2391 * this. By requiring this, it allows us to keep up the performance of
2392 * the pre-initialized command ring (esp. link pointers) by not actually
2393 * inserting the mcsetup command in the ring - i.e. its link pointer
2394 * points to the TxCB ring, but the mcsetup descriptor itself is not part
2395 * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2396 * lead into the regular TxCB ring when it completes.
2397 *
2398 * This function must be called at splimp.
2399 */
2400static void
2401fxp_mc_setup(struct fxp_softc *sc)
2402{
2403	struct fxp_cb_mcs *mcsp = sc->mcsp;
2404	struct ifnet *ifp = &sc->sc_if;
2405	struct fxp_tx *txp;
2406	int count;
2407
2408	/*
2409	 * If there are queued commands, we must wait until they are all
2410	 * completed. If we are already waiting, then add a NOP command
2411	 * with interrupt option so that we're notified when all commands
2412	 * have been completed - fxp_start() ensures that no additional
2413	 * TX commands will be added when need_mcsetup is true.
2414	 */
2415	if (sc->tx_queued) {
2416		/*
2417		 * need_mcsetup will be true if we are already waiting for the
2418		 * NOP command to be completed (see below). In this case, bail.
2419		 */
2420		if (sc->need_mcsetup)
2421			return;
2422		sc->need_mcsetup = 1;
2423
2424		/*
2425		 * Add a NOP command with interrupt so that we are notified
2426		 * when all TX commands have been processed.
2427		 */
2428		txp = sc->fxp_desc.tx_last->tx_next;
2429		txp->tx_mbuf = NULL;
2430		txp->tx_cb->cb_status = 0;
2431		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
2432		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
2433		/*
2434		 * Advance the end of list forward.
2435		 */
2436		sc->fxp_desc.tx_last->tx_cb->cb_command &=
2437		    htole16(~FXP_CB_COMMAND_S);
2438		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2439		sc->fxp_desc.tx_last = txp;
2440		sc->tx_queued++;
2441		/*
2442		 * Issue a resume in case the CU has just suspended.
2443		 */
2444		fxp_scb_wait(sc);
2445		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
2446		/*
2447		 * Set a 5 second timer just in case we don't hear from the
2448		 * card again.
2449		 */
2450		ifp->if_timer = 5;
2451
2452		return;
2453	}
2454	sc->need_mcsetup = 0;
2455
2456	/*
2457	 * Initialize multicast setup descriptor.
2458	 */
2459	mcsp->cb_status = 0;
2460	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
2461	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
2462	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2463	txp = &sc->fxp_desc.mcs_tx;
2464	txp->tx_mbuf = NULL;
2465	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2466	txp->tx_next = sc->fxp_desc.tx_list;
2467	(void) fxp_mc_addrs(sc);
2468	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2469	sc->tx_queued = 1;
2470
2471	/*
2472	 * Wait until command unit is not active. This should never
2473	 * be the case when nothing is queued, but make sure anyway.
2474	 */
2475	count = 100;
2476	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
2477	    FXP_SCB_CUS_ACTIVE && --count)
2478		DELAY(10);
2479	if (count == 0) {
2480		device_printf(sc->dev, "command queue timeout\n");
2481		return;
2482	}
2483
2484	/*
2485	 * Start the multicast setup command.
2486	 */
2487	fxp_scb_wait(sc);
2488	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2489	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
2490	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2491
2492	ifp->if_timer = 2;
2493	return;
2494}
2495
2496static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
2497static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
2498static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
2499static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
2500static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
2501static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2502
2503#define UCODE(x)	x, sizeof(x)
2504
2505struct ucode {
2506	u_int32_t	revision;
2507	u_int32_t	*ucode;
2508	int		length;
2509	u_short		int_delay_offset;
2510	u_short		bundle_max_offset;
2511} ucode_table[] = {
2512	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
2513	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
2514	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
2515	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
2516	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
2517	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
2518	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
2519	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
2520	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
2521	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2522	{ 0, NULL, 0, 0, 0 }
2523};
2524
2525static void
2526fxp_load_ucode(struct fxp_softc *sc)
2527{
2528	struct ucode *uc;
2529	struct fxp_cb_ucode *cbp;
2530
2531	for (uc = ucode_table; uc->ucode != NULL; uc++)
2532		if (sc->revision == uc->revision)
2533			break;
2534	if (uc->ucode == NULL)
2535		return;
2536	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
2537	cbp->cb_status = 0;
2538	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
2539	cbp->link_addr = 0xffffffff;    	/* (no) next command */
2540	memcpy(cbp->ucode, uc->ucode, uc->length);
2541	if (uc->int_delay_offset)
2542		*(u_int16_t *)&cbp->ucode[uc->int_delay_offset] =
2543		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
2544	if (uc->bundle_max_offset)
2545		*(u_int16_t *)&cbp->ucode[uc->bundle_max_offset] =
2546		    htole16(sc->tunable_bundle_max);
2547	/*
2548	 * Download the ucode to the chip.
2549	 */
2550	fxp_scb_wait(sc);
2551	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2552	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
2553	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2554	/* ...and wait for it to complete. */
2555	fxp_dma_wait(&cbp->cb_status, sc);
2556	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2557	device_printf(sc->dev,
2558	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
2559	    sc->tunable_int_delay,
2560	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
2561	sc->flags |= FXP_FLAG_UCODE;
2562}
2563
2564static int
2565sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2566{
2567	int error, value;
2568
2569	value = *(int *)arg1;
2570	error = sysctl_handle_int(oidp, &value, 0, req);
2571	if (error || !req->newptr)
2572		return (error);
2573	if (value < low || value > high)
2574		return (EINVAL);
2575	*(int *)arg1 = value;
2576	return (0);
2577}
2578
2579/*
2580 * Interrupt delay is expressed in microseconds, a multiplier is used
2581 * to convert this to the appropriate clock ticks before using.
2582 */
2583static int
2584sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
2585{
2586	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
2587}
2588
2589static int
2590sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
2591{
2592	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
2593}
2594