xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 673d91916d8748ecccff3635f1b15cc602a3898e)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28c3aac50fSPeter Wemm  * $FreeBSD$
29a17c678eSDavid Greenman  */
30a17c678eSDavid Greenman 
31a17c678eSDavid Greenman /*
32ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
33a17c678eSDavid Greenman  */
34a17c678eSDavid Greenman 
35a17c678eSDavid Greenman #include <sys/param.h>
36a17c678eSDavid Greenman #include <sys/systm.h>
37a17c678eSDavid Greenman #include <sys/mbuf.h>
38a17c678eSDavid Greenman #include <sys/malloc.h>
39f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
40a17c678eSDavid Greenman #include <sys/kernel.h>
414458ac71SBruce Evans #include <sys/socket.h>
4272a32a26SJonathan Lemon #include <sys/sysctl.h>
43a17c678eSDavid Greenman 
44a17c678eSDavid Greenman #include <net/if.h>
45397f9dfeSDavid Greenman #include <net/if_dl.h>
46ba8c6fd5SDavid Greenman #include <net/if_media.h>
47a17c678eSDavid Greenman 
48a17c678eSDavid Greenman #ifdef NS
49a17c678eSDavid Greenman #include <netns/ns.h>
50a17c678eSDavid Greenman #include <netns/ns_if.h>
51a17c678eSDavid Greenman #endif
52a17c678eSDavid Greenman 
53a17c678eSDavid Greenman #include <net/bpf.h>
54ba8c6fd5SDavid Greenman #include <sys/sockio.h>
556182fdbdSPeter Wemm #include <sys/bus.h>
566182fdbdSPeter Wemm #include <machine/bus.h>
576182fdbdSPeter Wemm #include <sys/rman.h>
586182fdbdSPeter Wemm #include <machine/resource.h>
59ba8c6fd5SDavid Greenman 
601d5e9e22SEivind Eklund #include <net/ethernet.h>
611d5e9e22SEivind Eklund #include <net/if_arp.h>
62ba8c6fd5SDavid Greenman 
63dfe61cf1SDavid Greenman #include <vm/vm.h>		/* for vtophys */
64efeaf95aSDavid Greenman #include <vm/pmap.h>		/* for vtophys */
65f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
66a17c678eSDavid Greenman 
67e8c8b728SJonathan Lemon #include <net/if_types.h>
68e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
69e8c8b728SJonathan Lemon 
70a17c678eSDavid Greenman #include <pci/pcivar.h>
71df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
72a17c678eSDavid Greenman 
73f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
74f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
75f7788e8eSJonathan Lemon 
76f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
77f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
7872a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
79f7788e8eSJonathan Lemon 
80f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
81f7788e8eSJonathan Lemon #include "miibus_if.h"
824fc1dda9SAndrew Gallatin 
83ba8c6fd5SDavid Greenman /*
84ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
85ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
86ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
87ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
88ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
89ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
90ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
91ba8c6fd5SDavid Greenman  */
92ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
93ba8c6fd5SDavid Greenman 
94ba8c6fd5SDavid Greenman /*
95f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
96f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
97f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
98f7788e8eSJonathan Lemon  */
99f7788e8eSJonathan Lemon static int tx_threshold = 64;
100f7788e8eSJonathan Lemon 
101f7788e8eSJonathan Lemon /*
102f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
103f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
104f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
105f7788e8eSJonathan Lemon  * to fxp_init.
106f7788e8eSJonathan Lemon  *
107f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
108f7788e8eSJonathan Lemon  */
109f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
110f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
111f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
112f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
113f7788e8eSJonathan Lemon 	0x0,	/*  0 */
114f7788e8eSJonathan Lemon 	0x0,	/*  1 */
115f7788e8eSJonathan Lemon 	0x0,	/*  2 */
116f7788e8eSJonathan Lemon 	0x0,	/*  3 */
117f7788e8eSJonathan Lemon 	0x0,	/*  4 */
118f7788e8eSJonathan Lemon 	0x0,	/*  5 */
119f7788e8eSJonathan Lemon 	0x32,	/*  6 */
120f7788e8eSJonathan Lemon 	0x0,	/*  7 */
121f7788e8eSJonathan Lemon 	0x0,	/*  8 */
122f7788e8eSJonathan Lemon 	0x0,	/*  9 */
123f7788e8eSJonathan Lemon 	0x6,	/* 10 */
124f7788e8eSJonathan Lemon 	0x0,	/* 11 */
125f7788e8eSJonathan Lemon 	0x0,	/* 12 */
126f7788e8eSJonathan Lemon 	0x0,	/* 13 */
127f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
128f7788e8eSJonathan Lemon 	0x48,	/* 15 */
129f7788e8eSJonathan Lemon 	0x0,	/* 16 */
130f7788e8eSJonathan Lemon 	0x40,	/* 17 */
131f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
132f7788e8eSJonathan Lemon 	0x0,	/* 19 */
133f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
134f7788e8eSJonathan Lemon 	0x5	/* 21 */
135f7788e8eSJonathan Lemon };
136f7788e8eSJonathan Lemon 
137f7788e8eSJonathan Lemon struct fxp_ident {
138f7788e8eSJonathan Lemon 	u_int16_t	devid;
139f7788e8eSJonathan Lemon 	char 		*name;
140f7788e8eSJonathan Lemon };
141f7788e8eSJonathan Lemon 
142f7788e8eSJonathan Lemon /*
143f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
144f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
145f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
146f7788e8eSJonathan Lemon  * them.
147f7788e8eSJonathan Lemon  */
148f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
149f7788e8eSJonathan Lemon     { 0x1229,		"Intel Pro 10/100B/100+ Ethernet" },
150f7788e8eSJonathan Lemon     { 0x2449,		"Intel Pro/100 Ethernet" },
151f7788e8eSJonathan Lemon     { 0x1209,		"Intel Embedded 10/100 Ethernet" },
152f7788e8eSJonathan Lemon     { 0x1029,		"Intel Pro/100 Ethernet" },
153f7788e8eSJonathan Lemon     { 0x1030,		"Intel Pro/100 Ethernet" },
154f7788e8eSJonathan Lemon     { 0x1031,		"Intel Pro/100 Ethernet" },
155f7788e8eSJonathan Lemon     { 0x1032,		"Intel Pro/100 Ethernet" },
156f7788e8eSJonathan Lemon     { 0x1033,		"Intel Pro/100 Ethernet" },
157f7788e8eSJonathan Lemon     { 0x1034,		"Intel Pro/100 Ethernet" },
158f7788e8eSJonathan Lemon     { 0x1035,		"Intel Pro/100 Ethernet" },
159f7788e8eSJonathan Lemon     { 0x1036,		"Intel Pro/100 Ethernet" },
160f7788e8eSJonathan Lemon     { 0x1037,		"Intel Pro/100 Ethernet" },
161f7788e8eSJonathan Lemon     { 0x1038,		"Intel Pro/100 Ethernet" },
162e94c058aSDoug Ambrisko     { 0x1039,		"Intel Pro/100 Ethernet" },
163f32932bcSMike Silbersack     { 0x103A,		"Intel Pro/100 Ethernet" },
164a7d68192SPaul Saab     { 0x103B,		"Intel Pro/100 Ethernet" },
165a7d68192SPaul Saab     { 0x103C,		"Intel Pro/100 Ethernet" },
166a7d68192SPaul Saab     { 0x103D,		"Intel Pro/100 Ethernet" },
167a7d68192SPaul Saab     { 0x103E,		"Intel Pro/100 Ethernet" },
168f7788e8eSJonathan Lemon     { 0,		NULL },
169f7788e8eSJonathan Lemon };
170f7788e8eSJonathan Lemon 
171f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
172f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
173f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
174f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
175f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
176f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
177f7788e8eSJonathan Lemon 
178f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
179f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
180f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
18148e417ebSJonathan Lemon static void		fxp_powerstate_d0(device_t dev);
182f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
183f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
184f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
185f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
186f7788e8eSJonathan Lemon 			    caddr_t data);
187f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
188f7788e8eSJonathan Lemon static int		fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
18909882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
190f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
191f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
192f7788e8eSJonathan Lemon 			    int autosize);
19300c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
19400c4116bSJonathan Lemon 			    u_int16_t data);
195f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
196f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
197f7788e8eSJonathan Lemon 			    int offset, int words);
19800c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
19900c4116bSJonathan Lemon 			    int offset, int words);
200f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
201f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
202f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
203f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
204f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
205f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
206f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
207f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
208f7788e8eSJonathan Lemon 			    int value);
20972a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
21072a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
21172a32a26SJonathan Lemon 			    int low, int high);
21272a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
21372a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
214f7788e8eSJonathan Lemon static __inline void	fxp_lwcopy(volatile u_int32_t *src,
215f7788e8eSJonathan Lemon 			    volatile u_int32_t *dst);
216f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2172e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
218f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
219f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
220f7788e8eSJonathan Lemon 
221f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
222f7788e8eSJonathan Lemon 	/* Device interface */
223f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
224f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
225f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
226f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
227f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
228f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
229f7788e8eSJonathan Lemon 
230f7788e8eSJonathan Lemon 	/* MII interface */
231f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
232f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
233f7788e8eSJonathan Lemon 
234f7788e8eSJonathan Lemon 	{ 0, 0 }
235f7788e8eSJonathan Lemon };
236f7788e8eSJonathan Lemon 
237f7788e8eSJonathan Lemon static driver_t fxp_driver = {
238f7788e8eSJonathan Lemon 	"fxp",
239f7788e8eSJonathan Lemon 	fxp_methods,
240f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
241f7788e8eSJonathan Lemon };
242f7788e8eSJonathan Lemon 
243f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
244f7788e8eSJonathan Lemon 
245f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
246f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
247f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
248f7788e8eSJonathan Lemon 
2492b5989e9SLuigi Rizzo static int fxp_rnr;
2502b5989e9SLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, fxp_rnr, CTLFLAG_RW, &fxp_rnr, 0, "fxp rnr events");
2512b5989e9SLuigi Rizzo 
252f7788e8eSJonathan Lemon /*
253ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
254ba8c6fd5SDavid Greenman  */
255ba8c6fd5SDavid Greenman static __inline void
256f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
257ba8c6fd5SDavid Greenman {
258aed53495SDavid Greenman #ifdef __i386__
259aed53495SDavid Greenman 	*dst = *src;
260aed53495SDavid Greenman #else
261fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
262fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
263ba8c6fd5SDavid Greenman 
264ba8c6fd5SDavid Greenman 	b[0] = a[0];
265ba8c6fd5SDavid Greenman 	b[1] = a[1];
266aed53495SDavid Greenman #endif
267ba8c6fd5SDavid Greenman }
268a17c678eSDavid Greenman 
269a17c678eSDavid Greenman /*
270dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
271dfe61cf1SDavid Greenman  * completed).
272dfe61cf1SDavid Greenman  */
273c1087c13SBruce Evans static __inline void
274f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
275a17c678eSDavid Greenman {
276a17c678eSDavid Greenman 	int i = 10000;
277a17c678eSDavid Greenman 
2787dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2797dced78aSDavid Greenman 		DELAY(2);
2807dced78aSDavid Greenman 	if (i == 0)
28100c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
282e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
283e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
284e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
285e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2867dced78aSDavid Greenman }
2877dced78aSDavid Greenman 
2887dced78aSDavid Greenman static __inline void
2892e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2902e2b8238SJonathan Lemon {
2912e2b8238SJonathan Lemon 
2922e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2932e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2942e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2952e2b8238SJonathan Lemon 	}
2962e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2972e2b8238SJonathan Lemon }
2982e2b8238SJonathan Lemon 
2992e2b8238SJonathan Lemon static __inline void
300f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
3017dced78aSDavid Greenman {
3027dced78aSDavid Greenman 	int i = 10000;
3037dced78aSDavid Greenman 
3047dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
3057dced78aSDavid Greenman 		DELAY(2);
3067dced78aSDavid Greenman 	if (i == 0)
307f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
308a17c678eSDavid Greenman }
309a17c678eSDavid Greenman 
310dfe61cf1SDavid Greenman /*
311dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
312dfe61cf1SDavid Greenman  */
3136182fdbdSPeter Wemm static int
3146182fdbdSPeter Wemm fxp_probe(device_t dev)
315a17c678eSDavid Greenman {
316f7788e8eSJonathan Lemon 	u_int16_t devid;
317f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
318f7788e8eSJonathan Lemon 
31955ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
320f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
321f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
322f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
323f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
324f7788e8eSJonathan Lemon 				return (0);
32555ce7b51SDavid Greenman 			}
326dd68ef16SPeter Wemm 		}
327f7788e8eSJonathan Lemon 	}
328f7788e8eSJonathan Lemon 	return (ENXIO);
3296182fdbdSPeter Wemm }
3306182fdbdSPeter Wemm 
33148e417ebSJonathan Lemon static void
33248e417ebSJonathan Lemon fxp_powerstate_d0(device_t dev)
33348e417ebSJonathan Lemon {
33448e417ebSJonathan Lemon #if __FreeBSD_version >= 430002
33548e417ebSJonathan Lemon 	u_int32_t iobase, membase, irq;
33648e417ebSJonathan Lemon 
33748e417ebSJonathan Lemon 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
33848e417ebSJonathan Lemon 		/* Save important PCI config data. */
33948e417ebSJonathan Lemon 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
34048e417ebSJonathan Lemon 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
34148e417ebSJonathan Lemon 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
34248e417ebSJonathan Lemon 
34348e417ebSJonathan Lemon 		/* Reset the power state. */
34448e417ebSJonathan Lemon 		device_printf(dev, "chip is in D%d power mode "
34548e417ebSJonathan Lemon 		    "-- setting to D0\n", pci_get_powerstate(dev));
34648e417ebSJonathan Lemon 
34748e417ebSJonathan Lemon 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
34848e417ebSJonathan Lemon 
34948e417ebSJonathan Lemon 		/* Restore PCI config data. */
35048e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
35148e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
35248e417ebSJonathan Lemon 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
35348e417ebSJonathan Lemon 	}
35448e417ebSJonathan Lemon #endif
35548e417ebSJonathan Lemon }
35648e417ebSJonathan Lemon 
3576182fdbdSPeter Wemm static int
3586182fdbdSPeter Wemm fxp_attach(device_t dev)
359a17c678eSDavid Greenman {
3606182fdbdSPeter Wemm 	int error = 0;
3616182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
362ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
3639fa6ccfbSMatt Jacob 	u_int32_t val;
364f7788e8eSJonathan Lemon 	u_int16_t data;
365f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
366f7788e8eSJonathan Lemon 	int s;
367a17c678eSDavid Greenman 
368f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
369f7788e8eSJonathan Lemon 	sc->dev = dev;
3706c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
371a1a9c8f7SJonathan Lemon 	sysctl_ctx_init(&sc->sysctl_ctx);
3726008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
3736008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
374a17c678eSDavid Greenman 
375f7788e8eSJonathan Lemon 	s = splimp();
376a17c678eSDavid Greenman 
377dfe61cf1SDavid Greenman 	/*
3789fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3799fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
380df373873SWes Peters 	 */
3816182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
382df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3836182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3849fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
385df373873SWes Peters 
38648e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
3878d799694SBill Paul 
388df373873SWes Peters 	/*
3899fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3909fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3919fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
392dfe61cf1SDavid Greenman 	 */
3939fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3949fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3952a05a4ebSMatt Jacob 	prefer_iomap = 0;
3962a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3972a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3989fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3999fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
4009fa6ccfbSMatt Jacob 	}
4019fa6ccfbSMatt Jacob 
4029fa6ccfbSMatt Jacob 	if (val & m1) {
4039fa6ccfbSMatt Jacob 		sc->rtp =
4049fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4059fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4069fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4076182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
4089fa6ccfbSMatt Jacob 	}
4099fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
4109fa6ccfbSMatt Jacob 		sc->rtp =
4119fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4129fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4139fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4149fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
4159fa6ccfbSMatt Jacob 	}
4169fa6ccfbSMatt Jacob 
4176182fdbdSPeter Wemm 	if (!sc->mem) {
4189fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
4196182fdbdSPeter Wemm 		error = ENXIO;
420a17c678eSDavid Greenman 		goto fail;
421a17c678eSDavid Greenman         }
4229fa6ccfbSMatt Jacob 	if (bootverbose) {
4239fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4249fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4259fa6ccfbSMatt Jacob 	}
4264fc1dda9SAndrew Gallatin 
4274fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4284fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
429a17c678eSDavid Greenman 
430a17c678eSDavid Greenman 	/*
431dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
432dfe61cf1SDavid Greenman 	 */
4336182fdbdSPeter Wemm 	rid = 0;
4346182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4356182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4366182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4376182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4386182fdbdSPeter Wemm 		error = ENXIO;
4396182fdbdSPeter Wemm 		goto fail;
4406182fdbdSPeter Wemm 	}
4416182fdbdSPeter Wemm 
442566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
443566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
4446182fdbdSPeter Wemm 	if (error) {
4456182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
446a17c678eSDavid Greenman 		goto fail;
447a17c678eSDavid Greenman 	}
448a17c678eSDavid Greenman 
449f7788e8eSJonathan Lemon 	/*
450f7788e8eSJonathan Lemon 	 * Reset to a stable state.
451f7788e8eSJonathan Lemon 	 */
452f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
453f7788e8eSJonathan Lemon 	DELAY(10);
454f7788e8eSJonathan Lemon 
455f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
456f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
457f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
458f7788e8eSJonathan Lemon 		goto failmem;
459f7788e8eSJonathan Lemon 
460f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
461f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
462f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
463f7788e8eSJonathan Lemon 		goto failmem;
464f7788e8eSJonathan Lemon 
465f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
466f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
467f7788e8eSJonathan Lemon 		goto failmem;
468f7788e8eSJonathan Lemon 
469f7788e8eSJonathan Lemon 	/*
470f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
471f7788e8eSJonathan Lemon 	 */
472f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
473f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
474f7788e8eSJonathan Lemon 			goto failmem;
475f7788e8eSJonathan Lemon 		}
476f7788e8eSJonathan Lemon 	}
477f7788e8eSJonathan Lemon 
478f7788e8eSJonathan Lemon 	/*
479f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
480f7788e8eSJonathan Lemon 	 */
481f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
482f7788e8eSJonathan Lemon 
483f7788e8eSJonathan Lemon 	/*
4843bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
485f7788e8eSJonathan Lemon 	 */
486f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
487f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
488f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
489dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
490f7788e8eSJonathan Lemon 
491f7788e8eSJonathan Lemon 	/*
49272a32a26SJonathan Lemon 	 * Create the sysctl tree
49372a32a26SJonathan Lemon 	 */
49472a32a26SJonathan Lemon 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
49572a32a26SJonathan Lemon 	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
49672a32a26SJonathan Lemon 	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
49772a32a26SJonathan Lemon 	if (sc->sysctl_tree == NULL)
49872a32a26SJonathan Lemon 		goto fail;
49972a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
50072a32a26SJonathan Lemon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
501858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
50272a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
50372a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
50472a32a26SJonathan Lemon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
505858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
50672a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
50772a32a26SJonathan Lemon 
50872a32a26SJonathan Lemon 	/*
50972a32a26SJonathan Lemon 	 * Pull in device tunables.
51072a32a26SJonathan Lemon 	 */
51172a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
51272a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
51372a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
51472a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
51572a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
51672a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
51772a32a26SJonathan Lemon 
51872a32a26SJonathan Lemon 	/*
51972a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
5203bd07cfdSJonathan Lemon 	 */
5213bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
5223bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
52372a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
52472a32a26SJonathan Lemon 	else
52572a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
5263bd07cfdSJonathan Lemon 
5273bd07cfdSJonathan Lemon 	/*
5282e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
52900c4116bSJonathan Lemon 	 *
53072a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
53172a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
53272a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
53300c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
53400c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
53500c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
53600c4116bSJonathan Lemon 	 *
53700c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5382e2b8238SJonathan Lemon 	 */
5392e2b8238SJonathan Lemon 	i = pci_get_device(dev);
54072a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
54172a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
54200c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
54300c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
54400c4116bSJonathan Lemon 			u_int16_t cksum;
54500c4116bSJonathan Lemon 			int i;
54600c4116bSJonathan Lemon 
54700c4116bSJonathan Lemon 			device_printf(dev,
548001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
54900c4116bSJonathan Lemon 			data &= ~0x02;
55000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
55100c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
55200c4116bSJonathan Lemon 			cksum = 0;
55300c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
55400c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
55500c4116bSJonathan Lemon 				cksum += data;
55600c4116bSJonathan Lemon 			}
55700c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
55800c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
55900c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
56000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
56100c4116bSJonathan Lemon 			device_printf(dev,
56200c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
56300c4116bSJonathan Lemon 			    i, data, cksum);
56400c4116bSJonathan Lemon #if 1
56500c4116bSJonathan Lemon 			/*
56600c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
56700c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
56800c4116bSJonathan Lemon 			 */
5692e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
57000c4116bSJonathan Lemon #endif
57100c4116bSJonathan Lemon 		}
57200c4116bSJonathan Lemon 	}
5732e2b8238SJonathan Lemon 
5742e2b8238SJonathan Lemon 	/*
5753bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5763bd07cfdSJonathan Lemon 	 */
57772a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5783bd07cfdSJonathan Lemon 		/*
57974396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
58074396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
58174396a0aSJonathan Lemon 		 * the board to turn on MWI.
5823bd07cfdSJonathan Lemon 		 */
58374396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
58474396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5853bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5863bd07cfdSJonathan Lemon 
5873bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5883bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
589920b58e8SBrooks Davis 
590e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
591e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
5923bd07cfdSJonathan Lemon 	}
5933bd07cfdSJonathan Lemon 
5943bd07cfdSJonathan Lemon 	/*
595f7788e8eSJonathan Lemon 	 * Read MAC address.
596f7788e8eSJonathan Lemon 	 */
597f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
598f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
599f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
600f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
601f7788e8eSJonathan Lemon 	if (bootverbose) {
6022e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
603f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
6042e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
6052e2b8238SJonathan Lemon 		    pci_get_revid(dev));
60672a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
60772a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
60872a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
609f7788e8eSJonathan Lemon 	}
610f7788e8eSJonathan Lemon 
611f7788e8eSJonathan Lemon 	/*
612f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
613f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
614f7788e8eSJonathan Lemon 	 *
615f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
616f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
617f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
618f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
619f7788e8eSJonathan Lemon 	 */
620f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
621f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
622f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
623f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
624f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
625f7788e8eSJonathan Lemon 	} else {
626f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
627f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
628f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
6296182fdbdSPeter Wemm 			error = ENXIO;
630ba8c6fd5SDavid Greenman 			goto fail;
631a17c678eSDavid Greenman 		}
632f7788e8eSJonathan Lemon 	}
633dccee1a1SDavid Greenman 
634a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
6356182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
636a17c678eSDavid Greenman 	ifp->if_name = "fxp";
637a17c678eSDavid Greenman 	ifp->if_output = ether_output;
638a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
639fb583156SDavid Greenman 	ifp->if_init = fxp_init;
640ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
641ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
642ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
643ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
644ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
645a17c678eSDavid Greenman 
646dfe61cf1SDavid Greenman 	/*
647dfe61cf1SDavid Greenman 	 * Attach the interface.
648dfe61cf1SDavid Greenman 	 */
649673d9191SSam Leffler 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
650f7788e8eSJonathan Lemon 
651e8c8b728SJonathan Lemon 	/*
652e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
653e8c8b728SJonathan Lemon 	 */
654e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
655673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
656e8c8b728SJonathan Lemon 
657483b9871SDavid Greenman 	/*
6583114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
6593114fdb4SDavid Greenman 	 * TX descriptors.
660483b9871SDavid Greenman 	 */
6613114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
6624a684684SDavid Greenman 
663f7788e8eSJonathan Lemon 	splx(s);
664f7788e8eSJonathan Lemon 	return (0);
665a17c678eSDavid Greenman 
666f7788e8eSJonathan Lemon failmem:
667f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
668f7788e8eSJonathan Lemon 	error = ENOMEM;
669a17c678eSDavid Greenman fail:
670f7788e8eSJonathan Lemon 	splx(s);
671f7788e8eSJonathan Lemon 	fxp_release(sc);
672f7788e8eSJonathan Lemon 	return (error);
673f7788e8eSJonathan Lemon }
674f7788e8eSJonathan Lemon 
675f7788e8eSJonathan Lemon /*
676f7788e8eSJonathan Lemon  * release all resources
677f7788e8eSJonathan Lemon  */
678f7788e8eSJonathan Lemon static void
679f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
680f7788e8eSJonathan Lemon {
681f7788e8eSJonathan Lemon 
682f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
6833bd07cfdSJonathan Lemon 	if (sc->miibus)
684f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
685f7788e8eSJonathan Lemon 
686f7788e8eSJonathan Lemon 	if (sc->cbl_base)
687f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
688f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
689f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
690f7788e8eSJonathan Lemon 	if (sc->mcsp)
691f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
692f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
693f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
694f7788e8eSJonathan Lemon 
695f7788e8eSJonathan Lemon 	if (sc->ih)
696f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
697f7788e8eSJonathan Lemon 	if (sc->irq)
698f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
699f7788e8eSJonathan Lemon 	if (sc->mem)
700f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
70172a32a26SJonathan Lemon 
70272a32a26SJonathan Lemon         sysctl_ctx_free(&sc->sysctl_ctx);
70372a32a26SJonathan Lemon 
7040f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
7056182fdbdSPeter Wemm }
7066182fdbdSPeter Wemm 
7076182fdbdSPeter Wemm /*
7086182fdbdSPeter Wemm  * Detach interface.
7096182fdbdSPeter Wemm  */
7106182fdbdSPeter Wemm static int
7116182fdbdSPeter Wemm fxp_detach(device_t dev)
7126182fdbdSPeter Wemm {
7136182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
714f7788e8eSJonathan Lemon 	int s;
7156182fdbdSPeter Wemm 
7162e2b8238SJonathan Lemon 	/* disable interrupts */
7172e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
7182e2b8238SJonathan Lemon 
719f7788e8eSJonathan Lemon 	s = splimp();
7206182fdbdSPeter Wemm 
7216182fdbdSPeter Wemm 	/*
7226182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
7236182fdbdSPeter Wemm 	 */
7246182fdbdSPeter Wemm 	fxp_stop(sc);
7256182fdbdSPeter Wemm 
7266182fdbdSPeter Wemm 	/*
727f7788e8eSJonathan Lemon 	 * Close down routes etc.
7286182fdbdSPeter Wemm 	 */
729673d9191SSam Leffler 	ether_ifdetach(&sc->arpcom.ac_if);
7306182fdbdSPeter Wemm 
7316182fdbdSPeter Wemm 	/*
7326182fdbdSPeter Wemm 	 * Free all media structures.
7336182fdbdSPeter Wemm 	 */
7346182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
7356182fdbdSPeter Wemm 
736f7788e8eSJonathan Lemon 	splx(s);
7376182fdbdSPeter Wemm 
738f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
739f7788e8eSJonathan Lemon 	fxp_release(sc);
7406182fdbdSPeter Wemm 
741f7788e8eSJonathan Lemon 	return (0);
742a17c678eSDavid Greenman }
743a17c678eSDavid Greenman 
744a17c678eSDavid Greenman /*
7454a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
746a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
747a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
748a17c678eSDavid Greenman  */
7496182fdbdSPeter Wemm static int
7506182fdbdSPeter Wemm fxp_shutdown(device_t dev)
751a17c678eSDavid Greenman {
7526182fdbdSPeter Wemm 	/*
7536182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
7546182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
7556182fdbdSPeter Wemm 	 * reboot before the driver initializes.
7566182fdbdSPeter Wemm 	 */
7576182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
758f7788e8eSJonathan Lemon 	return (0);
759a17c678eSDavid Greenman }
760a17c678eSDavid Greenman 
7617dced78aSDavid Greenman /*
7627dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
7637dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
7647dced78aSDavid Greenman  * resume.
7657dced78aSDavid Greenman  */
7667dced78aSDavid Greenman static int
7677dced78aSDavid Greenman fxp_suspend(device_t dev)
7687dced78aSDavid Greenman {
7697dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
770f7788e8eSJonathan Lemon 	int i, s;
7717dced78aSDavid Greenman 
772f7788e8eSJonathan Lemon 	s = splimp();
7737dced78aSDavid Greenman 
7747dced78aSDavid Greenman 	fxp_stop(sc);
7757dced78aSDavid Greenman 
7767dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
7777dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
7787dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
7797dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
7807dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
7817dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
7827dced78aSDavid Greenman 
7837dced78aSDavid Greenman 	sc->suspended = 1;
7847dced78aSDavid Greenman 
785f7788e8eSJonathan Lemon 	splx(s);
786f7788e8eSJonathan Lemon 	return (0);
7877dced78aSDavid Greenman }
7887dced78aSDavid Greenman 
7897dced78aSDavid Greenman /*
7907dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
7917dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
7927dced78aSDavid Greenman  * appropriate.
7937dced78aSDavid Greenman  */
7947dced78aSDavid Greenman static int
7957dced78aSDavid Greenman fxp_resume(device_t dev)
7967dced78aSDavid Greenman {
7977dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
7987dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
7997dced78aSDavid Greenman 	u_int16_t pci_command;
800f7788e8eSJonathan Lemon 	int i, s;
8017dced78aSDavid Greenman 
802f7788e8eSJonathan Lemon 	s = splimp();
8037dced78aSDavid Greenman 
80448e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
80548e417ebSJonathan Lemon 
8067dced78aSDavid Greenman 	/* better way to do this? */
8077dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
8087dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
8097dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
8107dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
8117dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
8127dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
8137dced78aSDavid Greenman 
8147dced78aSDavid Greenman 	/* reenable busmastering */
8157dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
8167dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
8177dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
8187dced78aSDavid Greenman 
8197dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
8207dced78aSDavid Greenman 	DELAY(10);
8217dced78aSDavid Greenman 
8227dced78aSDavid Greenman 	/* reinitialize interface if necessary */
8237dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
8247dced78aSDavid Greenman 		fxp_init(sc);
8257dced78aSDavid Greenman 
8267dced78aSDavid Greenman 	sc->suspended = 0;
8277dced78aSDavid Greenman 
828f7788e8eSJonathan Lemon 	splx(s);
829ba8c6fd5SDavid Greenman 	return (0);
830f7788e8eSJonathan Lemon }
831ba8c6fd5SDavid Greenman 
83200c4116bSJonathan Lemon static void
83300c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
83400c4116bSJonathan Lemon {
83500c4116bSJonathan Lemon 	u_int16_t reg;
83600c4116bSJonathan Lemon 	int x;
83700c4116bSJonathan Lemon 
83800c4116bSJonathan Lemon 	/*
83900c4116bSJonathan Lemon 	 * Shift in data.
84000c4116bSJonathan Lemon 	 */
84100c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
84200c4116bSJonathan Lemon 		if (data & x)
84300c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
84400c4116bSJonathan Lemon 		else
84500c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
84600c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
84700c4116bSJonathan Lemon 		DELAY(1);
84800c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
84900c4116bSJonathan Lemon 		DELAY(1);
85000c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
85100c4116bSJonathan Lemon 		DELAY(1);
85200c4116bSJonathan Lemon 	}
85300c4116bSJonathan Lemon }
85400c4116bSJonathan Lemon 
855f7788e8eSJonathan Lemon /*
856f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
857f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
858f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
859f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
860f7788e8eSJonathan Lemon  * every 16 bits of data.
861f7788e8eSJonathan Lemon  */
862f7788e8eSJonathan Lemon static u_int16_t
863f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
864f7788e8eSJonathan Lemon {
865f7788e8eSJonathan Lemon 	u_int16_t reg, data;
866f7788e8eSJonathan Lemon 	int x;
867ba8c6fd5SDavid Greenman 
868f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
869f7788e8eSJonathan Lemon 	/*
870f7788e8eSJonathan Lemon 	 * Shift in read opcode.
871f7788e8eSJonathan Lemon 	 */
87200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
873f7788e8eSJonathan Lemon 	/*
874f7788e8eSJonathan Lemon 	 * Shift in address.
875f7788e8eSJonathan Lemon 	 */
876f7788e8eSJonathan Lemon 	data = 0;
877f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
878f7788e8eSJonathan Lemon 		if (offset & x)
879f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
880f7788e8eSJonathan Lemon 		else
881f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
882f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
883f7788e8eSJonathan Lemon 		DELAY(1);
884f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
885f7788e8eSJonathan Lemon 		DELAY(1);
886f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
887f7788e8eSJonathan Lemon 		DELAY(1);
888f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
889f7788e8eSJonathan Lemon 		data++;
890f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
891f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
892f7788e8eSJonathan Lemon 			break;
893f7788e8eSJonathan Lemon 		}
894f7788e8eSJonathan Lemon 	}
895f7788e8eSJonathan Lemon 	/*
896f7788e8eSJonathan Lemon 	 * Shift out data.
897f7788e8eSJonathan Lemon 	 */
898f7788e8eSJonathan Lemon 	data = 0;
899f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
900f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
901f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
902f7788e8eSJonathan Lemon 		DELAY(1);
903f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
904f7788e8eSJonathan Lemon 			data |= x;
905f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
906f7788e8eSJonathan Lemon 		DELAY(1);
907f7788e8eSJonathan Lemon 	}
908f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
909f7788e8eSJonathan Lemon 	DELAY(1);
910f7788e8eSJonathan Lemon 
911f7788e8eSJonathan Lemon 	return (data);
912ba8c6fd5SDavid Greenman }
913ba8c6fd5SDavid Greenman 
91400c4116bSJonathan Lemon static void
91500c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
91600c4116bSJonathan Lemon {
91700c4116bSJonathan Lemon 	int i;
91800c4116bSJonathan Lemon 
91900c4116bSJonathan Lemon 	/*
92000c4116bSJonathan Lemon 	 * Erase/write enable.
92100c4116bSJonathan Lemon 	 */
92200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
92300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
92400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
92500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
92600c4116bSJonathan Lemon 	DELAY(1);
92700c4116bSJonathan Lemon 	/*
92800c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
92900c4116bSJonathan Lemon 	 */
93000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
93100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
93200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
93300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
93400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
93500c4116bSJonathan Lemon 	DELAY(1);
93600c4116bSJonathan Lemon 	/*
93700c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
93800c4116bSJonathan Lemon 	 */
93900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
94000c4116bSJonathan Lemon 	DELAY(1);
94100c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
94200c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
94300c4116bSJonathan Lemon 			break;
94400c4116bSJonathan Lemon 		DELAY(50);
94500c4116bSJonathan Lemon 	}
94600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
94700c4116bSJonathan Lemon 	DELAY(1);
94800c4116bSJonathan Lemon 	/*
94900c4116bSJonathan Lemon 	 * Erase/write disable.
95000c4116bSJonathan Lemon 	 */
95100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
95200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
95300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
95400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
95500c4116bSJonathan Lemon 	DELAY(1);
95600c4116bSJonathan Lemon }
95700c4116bSJonathan Lemon 
958ba8c6fd5SDavid Greenman /*
959e9bf2fa7SDavid Greenman  * From NetBSD:
960e9bf2fa7SDavid Greenman  *
961e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
962e9bf2fa7SDavid Greenman  *
963e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
964e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
965e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
966e9bf2fa7SDavid Greenman  *
967e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
968e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
969e9bf2fa7SDavid Greenman  *
970e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
971e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
972e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
973e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
974e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
975e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
976e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
977e9bf2fa7SDavid Greenman  */
978e9bf2fa7SDavid Greenman static void
979f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
980e9bf2fa7SDavid Greenman {
981e9bf2fa7SDavid Greenman 
982f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
983f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
984f7788e8eSJonathan Lemon 
985f7788e8eSJonathan Lemon 	/* autosize */
986f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
987e9bf2fa7SDavid Greenman }
988f7788e8eSJonathan Lemon 
989ba8c6fd5SDavid Greenman static void
990f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
991ba8c6fd5SDavid Greenman {
992f7788e8eSJonathan Lemon 	int i;
993ba8c6fd5SDavid Greenman 
994f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
995f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
996ba8c6fd5SDavid Greenman }
997ba8c6fd5SDavid Greenman 
99800c4116bSJonathan Lemon static void
99900c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
100000c4116bSJonathan Lemon {
100100c4116bSJonathan Lemon 	int i;
100200c4116bSJonathan Lemon 
100300c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
100400c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
100500c4116bSJonathan Lemon }
100600c4116bSJonathan Lemon 
1007a17c678eSDavid Greenman /*
1008a17c678eSDavid Greenman  * Start packet transmission on the interface.
1009a17c678eSDavid Greenman  */
1010a17c678eSDavid Greenman static void
1011f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1012a17c678eSDavid Greenman {
10139b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1014a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1015a17c678eSDavid Greenman 
1016a17c678eSDavid Greenman 	/*
1017483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1018483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1019483b9871SDavid Greenman 	 * of the command chain).
1020a17c678eSDavid Greenman 	 */
10210f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1022a17c678eSDavid Greenman 		return;
10230f4dc94cSChuck Paterson 	}
10241cd443acSDavid Greenman 
1025483b9871SDavid Greenman 	txp = NULL;
1026483b9871SDavid Greenman 
1027483b9871SDavid Greenman 	/*
1028483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1029483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
10303114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
10313114fdb4SDavid Greenman 	 *       a NOP command when needed.
1032483b9871SDavid Greenman 	 */
10333114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1034483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
1035483b9871SDavid Greenman 		int segment;
1036483b9871SDavid Greenman 
1037dfe61cf1SDavid Greenman 		/*
1038dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1039dfe61cf1SDavid Greenman 		 */
10406318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
1041a17c678eSDavid Greenman 
1042dfe61cf1SDavid Greenman 		/*
1043483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1044dfe61cf1SDavid Greenman 		 */
1045a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
1046a17c678eSDavid Greenman 
1047a17c678eSDavid Greenman 		/*
1048a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1049483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1050a17c678eSDavid Greenman 		 * and size of the mbuf.
1051a17c678eSDavid Greenman 		 */
105223a0ed7cSDavid Greenman tbdinit:
1053a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
1054a17c678eSDavid Greenman 			if (m->m_len != 0) {
1055a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1056a17c678eSDavid Greenman 					break;
1057a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1058a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1059a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1060a17c678eSDavid Greenman 				segment++;
1061a17c678eSDavid Greenman 			}
1062a17c678eSDavid Greenman 		}
1063fb583156SDavid Greenman 		if (m != NULL) {
106423a0ed7cSDavid Greenman 			struct mbuf *mn;
106523a0ed7cSDavid Greenman 
1066a17c678eSDavid Greenman 			/*
10673bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
10683bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
10693bd07cfdSJonathan Lemon 			 * new buffers.
1070a17c678eSDavid Greenman 			 */
107123a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
107223a0ed7cSDavid Greenman 			if (mn == NULL) {
107323a0ed7cSDavid Greenman 				m_freem(mb_head);
1074483b9871SDavid Greenman 				break;
1075a17c678eSDavid Greenman 			}
107623a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
107723a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
107823a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
107923a0ed7cSDavid Greenman 					m_freem(mn);
108023a0ed7cSDavid Greenman 					m_freem(mb_head);
1081483b9871SDavid Greenman 					break;
108223a0ed7cSDavid Greenman 				}
108323a0ed7cSDavid Greenman 			}
1084ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1085ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
108623a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
108723a0ed7cSDavid Greenman 			m_freem(mb_head);
108823a0ed7cSDavid Greenman 			mb_head = mn;
108923a0ed7cSDavid Greenman 			goto tbdinit;
109023a0ed7cSDavid Greenman 		}
109123a0ed7cSDavid Greenman 
109223a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10931cd443acSDavid Greenman 		txp->mb_head = mb_head;
1094a17c678eSDavid Greenman 		txp->cb_status = 0;
10953114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1096a17c678eSDavid Greenman 			txp->cb_command =
10973bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10983bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
10993114fdb4SDavid Greenman 		} else {
11003114fdb4SDavid Greenman 			txp->cb_command =
11013bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
11023bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
11033114fdb4SDavid Greenman 			/*
11043bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
11053bd07cfdSJonathan Lemon 			 * from the card again.
11063114fdb4SDavid Greenman 			 */
11073114fdb4SDavid Greenman 			ifp->if_timer = 5;
11083114fdb4SDavid Greenman 		}
1109f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1110a17c678eSDavid Greenman 
1111a17c678eSDavid Greenman 		/*
1112483b9871SDavid Greenman 		 * Advance the end of list forward.
1113a17c678eSDavid Greenman 		 */
111406175228SAndrew Gallatin 
111506175228SAndrew Gallatin #ifdef __alpha__
111606175228SAndrew Gallatin 		/*
111706175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
111806175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
111906175228SAndrew Gallatin 		 * up the status while we update the command field.
112006175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
112106175228SAndrew Gallatin 		 */
112206175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
112306175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
112406175228SAndrew Gallatin #else
1125a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
112606175228SAndrew Gallatin #endif /*__alpha__*/
1127a17c678eSDavid Greenman 		sc->cbl_last = txp;
1128a17c678eSDavid Greenman 
1129a17c678eSDavid Greenman 		/*
11301cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
11311cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1132483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1133a17c678eSDavid Greenman 		 */
11341cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1135a17c678eSDavid Greenman 			sc->cbl_first = txp;
1136a17c678eSDavid Greenman 
11371cd443acSDavid Greenman 		sc->tx_queued++;
11381cd443acSDavid Greenman 
1139a17c678eSDavid Greenman 		/*
1140a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1141a17c678eSDavid Greenman 		 */
1142673d9191SSam Leffler 		BPF_MTAP(ifp, mb_head);
1143483b9871SDavid Greenman 	}
1144483b9871SDavid Greenman 
1145483b9871SDavid Greenman 	/*
1146483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1147483b9871SDavid Greenman 	 * going again if suspended.
1148483b9871SDavid Greenman 	 */
1149483b9871SDavid Greenman 	if (txp != NULL) {
1150483b9871SDavid Greenman 		fxp_scb_wait(sc);
11512e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1152483b9871SDavid Greenman 	}
1153a17c678eSDavid Greenman }
1154a17c678eSDavid Greenman 
1155e4fc250cSLuigi Rizzo static void fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count);
1156e4fc250cSLuigi Rizzo 
1157e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1158e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1159e4fc250cSLuigi Rizzo 
1160e4fc250cSLuigi Rizzo static void
1161e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1162e4fc250cSLuigi Rizzo {
1163e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
1164e4fc250cSLuigi Rizzo 	u_int8_t statack;
1165e4fc250cSLuigi Rizzo 
1166e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1167e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1168e4fc250cSLuigi Rizzo 		return;
1169e4fc250cSLuigi Rizzo 	}
1170e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1171e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1172e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
1173e4fc250cSLuigi Rizzo 		u_int8_t tmp;
11746481f301SPeter Wemm 
1175e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1176e4fc250cSLuigi Rizzo 		if (tmp == 0xff || tmp == 0)
1177e4fc250cSLuigi Rizzo 			return; /* nothing to do */
1178e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1179e4fc250cSLuigi Rizzo 		/* ack what we can */
1180e4fc250cSLuigi Rizzo 		if (tmp != 0)
1181e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1182e4fc250cSLuigi Rizzo 		statack |= tmp;
1183e4fc250cSLuigi Rizzo 	}
1184e4fc250cSLuigi Rizzo 	fxp_intr_body(sc, statack, count);
1185e4fc250cSLuigi Rizzo }
1186e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1187e4fc250cSLuigi Rizzo 
1188a17c678eSDavid Greenman /*
11899c7d2607SDavid Greenman  * Process interface interrupts.
1190a17c678eSDavid Greenman  */
119194927790SDavid Greenman static void
1192f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1193a17c678eSDavid Greenman {
1194f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
11951cd443acSDavid Greenman 	u_int8_t statack;
11960f4dc94cSChuck Paterson 
1197e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1198e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
1199e4fc250cSLuigi Rizzo 
120062f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
1201e4fc250cSLuigi Rizzo 		return;
1202e4fc250cSLuigi Rizzo 	if (ether_poll_register(fxp_poll, ifp)) {
1203e4fc250cSLuigi Rizzo 		/* disable interrupts */
1204e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
1205e4fc250cSLuigi Rizzo 		fxp_poll(ifp, 0, 1);
1206e4fc250cSLuigi Rizzo 		return;
1207e4fc250cSLuigi Rizzo 	}
1208e4fc250cSLuigi Rizzo #endif
1209e4fc250cSLuigi Rizzo 
1210b184b38eSDavid Greenman 	if (sc->suspended) {
1211b184b38eSDavid Greenman 		return;
1212b184b38eSDavid Greenman 	}
1213b184b38eSDavid Greenman 
1214b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1215a17c678eSDavid Greenman 		/*
121611457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
121711457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
121811457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
121911457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
122011457bbfSJonathan Lemon 		 */
122111457bbfSJonathan Lemon 		if (statack == 0xff)
122211457bbfSJonathan Lemon 			return;
122311457bbfSJonathan Lemon 
122411457bbfSJonathan Lemon 		/*
1225a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1226a17c678eSDavid Greenman 		 */
1227ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1228e4fc250cSLuigi Rizzo 		fxp_intr_body(sc, statack, -1);
1229e4fc250cSLuigi Rizzo 	}
1230e4fc250cSLuigi Rizzo }
1231e4fc250cSLuigi Rizzo 
1232e4fc250cSLuigi Rizzo static void
1233e4fc250cSLuigi Rizzo fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count)
1234e4fc250cSLuigi Rizzo {
1235e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
12362b5989e9SLuigi Rizzo 	struct mbuf *m;
12372b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
12382b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
12392b5989e9SLuigi Rizzo 
12402b5989e9SLuigi Rizzo 	if (rnr)
12412b5989e9SLuigi Rizzo 		fxp_rnr++;
1242947e3815SIan Dowse #ifdef DEVICE_POLLING
1243947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1244947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1245947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1246947e3815SIan Dowse 		rnr = 1;
1247947e3815SIan Dowse 	}
1248947e3815SIan Dowse #endif
1249a17c678eSDavid Greenman 
1250a17c678eSDavid Greenman 	/*
12513114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
125206936301SBill Paul 	 *
125306936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
125406936301SBill Paul 	 * be that this event (control unit not ready) was not
125506936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
125606936301SBill Paul 	 * The exact sequence of events that occur when the interface
125706936301SBill Paul 	 * is brought up are different now, and if this event
125806936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
125906936301SBill Paul 	 * can stall for several seconds. The result is that no
126006936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
126106936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
12623114fdb4SDavid Greenman 	 */
126306936301SBill Paul 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
12643114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
12653114fdb4SDavid Greenman 
12663114fdb4SDavid Greenman 		for (txp = sc->cbl_first; sc->tx_queued &&
12673114fdb4SDavid Greenman 		    (txp->cb_status & FXP_CB_STATUS_C) != 0;
12683114fdb4SDavid Greenman 		    txp = txp->next) {
12693114fdb4SDavid Greenman 			if (txp->mb_head != NULL) {
12703114fdb4SDavid Greenman 				m_freem(txp->mb_head);
12713114fdb4SDavid Greenman 				txp->mb_head = NULL;
12723114fdb4SDavid Greenman 			}
12733114fdb4SDavid Greenman 			sc->tx_queued--;
12743114fdb4SDavid Greenman 		}
12753114fdb4SDavid Greenman 		sc->cbl_first = txp;
127641aa0ba2SLuigi Rizzo 		ifp->if_timer = 0;
1277e2102ae4SMike Silbersack 		if (sc->tx_queued == 0) {
12783114fdb4SDavid Greenman 			if (sc->need_mcsetup)
12793114fdb4SDavid Greenman 				fxp_mc_setup(sc);
1280e2102ae4SMike Silbersack 		}
12813114fdb4SDavid Greenman 		/*
12823114fdb4SDavid Greenman 		 * Try to start more packets transmitting.
12833114fdb4SDavid Greenman 		 */
12843114fdb4SDavid Greenman 		if (ifp->if_snd.ifq_head != NULL)
12853114fdb4SDavid Greenman 			fxp_start(ifp);
12863114fdb4SDavid Greenman 	}
12872b5989e9SLuigi Rizzo 
12882b5989e9SLuigi Rizzo 	/*
12892b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
12902b5989e9SLuigi Rizzo 	 */
1291947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
12922b5989e9SLuigi Rizzo 		return;
12932b5989e9SLuigi Rizzo 
12943114fdb4SDavid Greenman 	/*
1295a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1296a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1297a17c678eSDavid Greenman 	 * re-start the receiver.
1298947e3815SIan Dowse 	 *
12992b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
13002b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
13012b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
13022b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1303947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1304947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1305a17c678eSDavid Greenman 	 */
13062b5989e9SLuigi Rizzo 	for (;;) {
1307a17c678eSDavid Greenman 		m = sc->rfa_headm;
1308ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1309ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1310a17c678eSDavid Greenman 
1311e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1312947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1313947e3815SIan Dowse 			if (rnr) {
1314947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1315947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1316947e3815SIan Dowse 				rnr = 0;
1317947e3815SIan Dowse 			}
13182b5989e9SLuigi Rizzo 			break;
1319947e3815SIan Dowse 		}
13202b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
13212b5989e9SLuigi Rizzo 
13222b5989e9SLuigi Rizzo 		if ( (rfa->rfa_status & FXP_RFA_STATUS_C) == 0)
13232b5989e9SLuigi Rizzo 			break;
13242b5989e9SLuigi Rizzo 
1325dfe61cf1SDavid Greenman 		/*
1326dfe61cf1SDavid Greenman 		 * Remove first packet from the chain.
1327dfe61cf1SDavid Greenman 		 */
1328a17c678eSDavid Greenman 		sc->rfa_headm = m->m_next;
1329a17c678eSDavid Greenman 		m->m_next = NULL;
1330a17c678eSDavid Greenman 
1331dfe61cf1SDavid Greenman 		/*
1332ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1333ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1334ba8c6fd5SDavid Greenman 		 * instead.
1335dfe61cf1SDavid Greenman 		 */
1336a17c678eSDavid Greenman 		if (fxp_add_rfabuf(sc, m) == 0) {
1337aed53495SDavid Greenman 			int total_len;
1338a17c678eSDavid Greenman 
1339e8c8b728SJonathan Lemon 			/*
13402b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
13412b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
13422b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
13432b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1344e8c8b728SJonathan Lemon 			 */
13452b5989e9SLuigi Rizzo 			total_len = rfa->actual_size & 0x3fff;
13462b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
13472b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
13482b5989e9SLuigi Rizzo 				sizeof(struct fxp_rfa) ||
13492b5989e9SLuigi Rizzo 			    rfa->rfa_status & FXP_RFA_STATUS_CRC) {
1350e8c8b728SJonathan Lemon 				m_freem(m);
13512b5989e9SLuigi Rizzo 				continue;
1352e8c8b728SJonathan Lemon 			}
1353920b58e8SBrooks Davis 
13542e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1355673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1356673d9191SSam Leffler 
1357673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
1358a17c678eSDavid Greenman 		}
1359a17c678eSDavid Greenman 	}
13602b5989e9SLuigi Rizzo 	if (rnr) {
1361ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1362ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1363ba8c6fd5SDavid Greenman 		    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1364ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
13652e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1366a17c678eSDavid Greenman 	}
1367a17c678eSDavid Greenman }
1368a17c678eSDavid Greenman 
1369dfe61cf1SDavid Greenman /*
1370dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1371dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1372dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1373dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1374dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1375dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1376dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1377dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1378dfe61cf1SDavid Greenman  * them again next time.
1379dfe61cf1SDavid Greenman  */
1380303b270bSEivind Eklund static void
1381f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1382a17c678eSDavid Greenman {
1383f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1384ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1385a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1386c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1387f7788e8eSJonathan Lemon 	int s;
1388a17c678eSDavid Greenman 
1389a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1390a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1391397f9dfeSDavid Greenman 	if (sp->rx_good) {
1392397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1393397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1394397f9dfeSDavid Greenman 	} else {
1395c8cc6fcaSDavid Greenman 		/*
1396c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1397c8cc6fcaSDavid Greenman 		 */
1398397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1399397f9dfeSDavid Greenman 	}
14003ba65732SDavid Greenman 	ifp->if_ierrors +=
14013ba65732SDavid Greenman 	    sp->rx_crc_errors +
14023ba65732SDavid Greenman 	    sp->rx_alignment_errors +
14033ba65732SDavid Greenman 	    sp->rx_rnr_errors +
14046e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1405a17c678eSDavid Greenman 	/*
1406f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1407f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1408f9be9005SDavid Greenman 	 */
1409f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1410f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1411f9be9005SDavid Greenman 		if (tx_threshold < 192)
1412f9be9005SDavid Greenman 			tx_threshold += 64;
1413f9be9005SDavid Greenman 	}
1414f7788e8eSJonathan Lemon 	s = splimp();
1415397f9dfeSDavid Greenman 	/*
1416c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1417c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1418c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1419c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1420c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1421c8cc6fcaSDavid Greenman 	 */
1422c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1423c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1424c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1425c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1426c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1427c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1428c8cc6fcaSDavid Greenman 		}
1429c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1430c8cc6fcaSDavid Greenman 	}
1431c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1432c8cc6fcaSDavid Greenman 	/*
1433397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1434397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1435397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1436397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1437397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1438397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1439397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1440397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1441397f9dfeSDavid Greenman 	 */
1442397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1443397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1444397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1445397f9dfeSDavid Greenman 	}
1446f9be9005SDavid Greenman 	/*
14473ba65732SDavid Greenman 	 * If there is no pending command, start another stats
14483ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1449a17c678eSDavid Greenman 	 */
1450397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1451a17c678eSDavid Greenman 		/*
1452397f9dfeSDavid Greenman 		 * Start another stats dump.
1453a17c678eSDavid Greenman 		 */
14542e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1455dfe61cf1SDavid Greenman 	} else {
1456dfe61cf1SDavid Greenman 		/*
1457dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1458dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
14593ba65732SDavid Greenman 		 * next timer event to update them.
1460dfe61cf1SDavid Greenman 		 */
1461dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1462f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1463dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
14643ba65732SDavid Greenman 
1465dfe61cf1SDavid Greenman 		sp->rx_good = 0;
14663ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
14673ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
14683ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
14693ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1470dfe61cf1SDavid Greenman 	}
1471f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1472f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
147374396a0aSJonathan Lemon 	splx(s);
1474a17c678eSDavid Greenman 	/*
1475a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1476a17c678eSDavid Greenman 	 */
1477f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1478a17c678eSDavid Greenman }
1479a17c678eSDavid Greenman 
1480a17c678eSDavid Greenman /*
1481a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1482a17c678eSDavid Greenman  * the interface.
1483a17c678eSDavid Greenman  */
1484a17c678eSDavid Greenman static void
1485f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1486a17c678eSDavid Greenman {
1487ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
14883ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
14893ba65732SDavid Greenman 	int i;
1490a17c678eSDavid Greenman 
14917dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
14927dced78aSDavid Greenman 	ifp->if_timer = 0;
14937dced78aSDavid Greenman 
1494e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1495e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
1496e4fc250cSLuigi Rizzo #endif
1497a17c678eSDavid Greenman 	/*
1498a17c678eSDavid Greenman 	 * Cancel stats updater.
1499a17c678eSDavid Greenman 	 */
1500f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
15013ba65732SDavid Greenman 
15023ba65732SDavid Greenman 	/*
150372a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
15043ba65732SDavid Greenman 	 */
150572a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
150609882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
150772a32a26SJonathan Lemon 	DELAY(50);
1508a17c678eSDavid Greenman 
15093ba65732SDavid Greenman 	/*
15103ba65732SDavid Greenman 	 * Release any xmit buffers.
15113ba65732SDavid Greenman 	 */
1512da91462dSDavid Greenman 	txp = sc->cbl_base;
1513da91462dSDavid Greenman 	if (txp != NULL) {
1514da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1515da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1516da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1517da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1518da91462dSDavid Greenman 			}
1519da91462dSDavid Greenman 		}
15203ba65732SDavid Greenman 	}
15213ba65732SDavid Greenman 	sc->tx_queued = 0;
15223ba65732SDavid Greenman 
15233ba65732SDavid Greenman 	/*
15243ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
15253ba65732SDavid Greenman 	 */
15263ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
15273ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
15283ba65732SDavid Greenman 	sc->rfa_headm = NULL;
15293ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
15303ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
15313ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
15323ba65732SDavid Greenman 			/*
15333ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
15343ba65732SDavid Greenman 			 * and we just freed all the buffers we need
15353ba65732SDavid Greenman 			 * above.
15363ba65732SDavid Greenman 			 */
15373ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
15383ba65732SDavid Greenman 		}
15393ba65732SDavid Greenman 	}
1540a17c678eSDavid Greenman }
1541a17c678eSDavid Greenman 
1542a17c678eSDavid Greenman /*
1543a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1544a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1545a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1546a17c678eSDavid Greenman  * card has wedged for some reason.
1547a17c678eSDavid Greenman  */
1548a17c678eSDavid Greenman static void
1549f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1550a17c678eSDavid Greenman {
1551ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1552ba8c6fd5SDavid Greenman 
1553f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
15544a5f1499SDavid Greenman 	ifp->if_oerrors++;
1555a17c678eSDavid Greenman 
1556ba8c6fd5SDavid Greenman 	fxp_init(sc);
1557a17c678eSDavid Greenman }
1558a17c678eSDavid Greenman 
1559a17c678eSDavid Greenman static void
1560f7788e8eSJonathan Lemon fxp_init(void *xsc)
1561a17c678eSDavid Greenman {
1562fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1563ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1564a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1565a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1566a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
156709882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
1568f7788e8eSJonathan Lemon 	int i, prm, s;
1569a17c678eSDavid Greenman 
1570f7788e8eSJonathan Lemon 	s = splimp();
1571a17c678eSDavid Greenman 	/*
15723ba65732SDavid Greenman 	 * Cancel any pending I/O
1573a17c678eSDavid Greenman 	 */
15743ba65732SDavid Greenman 	fxp_stop(sc);
1575a17c678eSDavid Greenman 
1576a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1577a17c678eSDavid Greenman 
1578a17c678eSDavid Greenman 	/*
1579a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1580a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1581a17c678eSDavid Greenman 	 */
1582ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
15832e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1584a17c678eSDavid Greenman 
1585ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
15862e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1587a17c678eSDavid Greenman 
1588a17c678eSDavid Greenman 	/*
1589a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1590a17c678eSDavid Greenman 	 */
1591ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1592ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
15932e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1594a17c678eSDavid Greenman 
1595a17c678eSDavid Greenman 	/*
159672a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
159772a32a26SJonathan Lemon 	 */
159872a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
159972a32a26SJonathan Lemon 		fxp_load_ucode(sc);
160072a32a26SJonathan Lemon 
160172a32a26SJonathan Lemon 	/*
160209882363SJonathan Lemon 	 * Initialize the multicast address list.
160309882363SJonathan Lemon 	 */
160409882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
160509882363SJonathan Lemon 		mcsp = sc->mcsp;
160609882363SJonathan Lemon 		mcsp->cb_status = 0;
160709882363SJonathan Lemon 		mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL;
160809882363SJonathan Lemon 		mcsp->link_addr = -1;
160909882363SJonathan Lemon 		/*
161009882363SJonathan Lemon 	 	 * Start the multicast setup command.
161109882363SJonathan Lemon 		 */
161209882363SJonathan Lemon 		fxp_scb_wait(sc);
161309882363SJonathan Lemon 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
161409882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
161509882363SJonathan Lemon 		/* ...and wait for it to complete. */
161609882363SJonathan Lemon 		fxp_dma_wait(&mcsp->cb_status, sc);
161709882363SJonathan Lemon 	}
161809882363SJonathan Lemon 
161909882363SJonathan Lemon 	/*
1620a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1621a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1622a17c678eSDavid Greenman 	 * later.
1623a17c678eSDavid Greenman 	 */
1624a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1625a17c678eSDavid Greenman 
1626a17c678eSDavid Greenman 	/*
1627a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1628a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1629a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1630a17c678eSDavid Greenman 	 */
1631d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1632d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1633397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1634a17c678eSDavid Greenman 
1635a17c678eSDavid Greenman 	cbp->cb_status =	0;
1636a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1637a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1638a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1639001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1640001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1641a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1642f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1643f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1644f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1645f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1646001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1647001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1648f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1649a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1650f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1651f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
16523114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1653f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1654f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1655f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
165672a32a26SJonathan Lemon 	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
1657a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1658f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1659f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1660f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1661f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1662f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1663f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1664f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1665f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1666f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1667f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1668a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1669a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1670a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1671a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1672a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1673a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1674a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1675a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1676f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1677f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1678f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1679f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1680f7788e8eSJonathan Lemon 
1681a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1682a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1683a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1684f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1685f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1686f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1687f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1688a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
16893ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1690a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1691f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1692a17c678eSDavid Greenman 
169372a32a26SJonathan Lemon 	if (sc->revision == FXP_REV_82557) {
16943bd07cfdSJonathan Lemon 		/*
16953bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
16963bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
16973bd07cfdSJonathan Lemon 		 */
16983bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
16993bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
17003bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
17013bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
17023bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
17033bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
17043bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
17053bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
17063bd07cfdSJonathan Lemon 	} else {
17073bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
17083bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
17093bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
17103bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
17113bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
17123bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
17133bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
17143bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
17153bd07cfdSJonathan Lemon 	}
17163bd07cfdSJonathan Lemon 
1717a17c678eSDavid Greenman 	/*
1718a17c678eSDavid Greenman 	 * Start the config command/DMA.
1719a17c678eSDavid Greenman 	 */
1720ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1721397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
17222e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1723a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
17247dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1725a17c678eSDavid Greenman 
1726a17c678eSDavid Greenman 	/*
1727a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1728a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1729a17c678eSDavid Greenman 	 */
1730a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1731a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1732a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1733a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1734d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1735d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1736a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1737a17c678eSDavid Greenman 
1738a17c678eSDavid Greenman 	/*
1739a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1740a17c678eSDavid Greenman 	 */
1741ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
17422e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1743a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
17447dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1745a17c678eSDavid Greenman 
1746a17c678eSDavid Greenman 	/*
1747a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1748a17c678eSDavid Greenman 	 */
1749a17c678eSDavid Greenman 
1750a17c678eSDavid Greenman 	txp = sc->cbl_base;
1751a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1752a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1753a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1754a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
17553bd07cfdSJonathan Lemon 		txp[i].link_addr =
17563bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
17573bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
17583bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
17593bd07cfdSJonathan Lemon 		else
1760a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1761a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1762a17c678eSDavid Greenman 	}
1763a17c678eSDavid Greenman 	/*
1764397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1765a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1766a17c678eSDavid Greenman 	 */
1767a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1768a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1769397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1770a17c678eSDavid Greenman 
1771ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
17722e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1773a17c678eSDavid Greenman 
1774a17c678eSDavid Greenman 	/*
1775a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1776a17c678eSDavid Greenman 	 */
1777ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1778ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1779ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
17802e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1781a17c678eSDavid Greenman 
1782dccee1a1SDavid Greenman 	/*
1783ba8c6fd5SDavid Greenman 	 * Set current media.
1784dccee1a1SDavid Greenman 	 */
1785f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1786f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1787dccee1a1SDavid Greenman 
1788a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1789a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1790e8c8b728SJonathan Lemon 
1791e8c8b728SJonathan Lemon 	/*
1792e8c8b728SJonathan Lemon 	 * Enable interrupts.
1793e8c8b728SJonathan Lemon 	 */
17942b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
17952b5989e9SLuigi Rizzo 	/*
17962b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
17972b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
17982b5989e9SLuigi Rizzo 	 */
179962f76486SMaxim Sobolev 	if ( ifp->if_flags & IFF_POLLING )
18002b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
18012b5989e9SLuigi Rizzo 	else
18022b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
1803e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1804f7788e8eSJonathan Lemon 	splx(s);
1805a17c678eSDavid Greenman 
1806a17c678eSDavid Greenman 	/*
1807a17c678eSDavid Greenman 	 * Start stats updater.
1808a17c678eSDavid Greenman 	 */
1809f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1810f7788e8eSJonathan Lemon }
1811f7788e8eSJonathan Lemon 
1812f7788e8eSJonathan Lemon static int
1813f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1814f7788e8eSJonathan Lemon {
1815f7788e8eSJonathan Lemon 
1816f7788e8eSJonathan Lemon 	return (0);
1817a17c678eSDavid Greenman }
1818a17c678eSDavid Greenman 
1819303b270bSEivind Eklund static void
1820f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1821ba8c6fd5SDavid Greenman {
1822ba8c6fd5SDavid Greenman 
1823f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1824ba8c6fd5SDavid Greenman }
1825ba8c6fd5SDavid Greenman 
1826ba8c6fd5SDavid Greenman /*
1827ba8c6fd5SDavid Greenman  * Change media according to request.
1828ba8c6fd5SDavid Greenman  */
1829f7788e8eSJonathan Lemon static int
1830f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1831ba8c6fd5SDavid Greenman {
1832ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1833f7788e8eSJonathan Lemon 	struct mii_data *mii;
1834ba8c6fd5SDavid Greenman 
1835f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1836f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1837ba8c6fd5SDavid Greenman 	return (0);
1838ba8c6fd5SDavid Greenman }
1839ba8c6fd5SDavid Greenman 
1840ba8c6fd5SDavid Greenman /*
1841ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1842ba8c6fd5SDavid Greenman  */
1843f7788e8eSJonathan Lemon static void
1844f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1845ba8c6fd5SDavid Greenman {
1846ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1847f7788e8eSJonathan Lemon 	struct mii_data *mii;
1848ba8c6fd5SDavid Greenman 
1849f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1850f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1851f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1852f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
18532e2b8238SJonathan Lemon 
18542e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
18552e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
18562e2b8238SJonathan Lemon 	else
18572e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
1858ba8c6fd5SDavid Greenman }
1859ba8c6fd5SDavid Greenman 
1860a17c678eSDavid Greenman /*
1861a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1862a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1863a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1864dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1865a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1866a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1867a17c678eSDavid Greenman  */
1868a17c678eSDavid Greenman static int
1869f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1870a17c678eSDavid Greenman {
1871ba8c6fd5SDavid Greenman 	u_int32_t v;
1872a17c678eSDavid Greenman 	struct mbuf *m;
1873a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1874a17c678eSDavid Greenman 
18759e251858SLuigi Rizzo 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
18769e251858SLuigi Rizzo 	if (m == NULL) { /* try to recycle the old mbuf instead */
1877eadd5e3aSDavid Greenman 		if (oldm == NULL)
1878a17c678eSDavid Greenman 			return 1;
1879eadd5e3aSDavid Greenman 		m = oldm;
1880eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1881eadd5e3aSDavid Greenman 	}
1882ba8c6fd5SDavid Greenman 
1883ba8c6fd5SDavid Greenman 	/*
1884ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1885ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1886ba8c6fd5SDavid Greenman 	 */
1887ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1888ba8c6fd5SDavid Greenman 
1889eadd5e3aSDavid Greenman 	/*
1890eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1891eadd5e3aSDavid Greenman 	 * data start past it.
1892eadd5e3aSDavid Greenman 	 */
1893a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1894eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
18954fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1896eadd5e3aSDavid Greenman 
1897ba8c6fd5SDavid Greenman 	/*
1898ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1899ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1900ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1901ba8c6fd5SDavid Greenman 	 */
19024fc1dda9SAndrew Gallatin 
1903a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1904a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1905a17c678eSDavid Greenman 	rfa->actual_size = 0;
1906ba8c6fd5SDavid Greenman 
1907ba8c6fd5SDavid Greenman 	v = -1;
19084fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
19094fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1910ba8c6fd5SDavid Greenman 
1911dfe61cf1SDavid Greenman 	/*
1912dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1913dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1914dfe61cf1SDavid Greenman 	 */
1915a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1916ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1917ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1918a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1919ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
19204fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1921aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1922a17c678eSDavid Greenman 	} else {
1923a17c678eSDavid Greenman 		sc->rfa_headm = m;
1924a17c678eSDavid Greenman 	}
1925a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1926a17c678eSDavid Greenman 
1927dfe61cf1SDavid Greenman 	return (m == oldm);
1928a17c678eSDavid Greenman }
1929a17c678eSDavid Greenman 
19306ebc3153SDavid Greenman static volatile int
1931f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1932dccee1a1SDavid Greenman {
1933f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1934dccee1a1SDavid Greenman 	int count = 10000;
19356ebc3153SDavid Greenman 	int value;
1936dccee1a1SDavid Greenman 
1937ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1938ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1939dccee1a1SDavid Greenman 
1940ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1941ba8c6fd5SDavid Greenman 	    && count--)
19426ebc3153SDavid Greenman 		DELAY(10);
1943dccee1a1SDavid Greenman 
1944dccee1a1SDavid Greenman 	if (count <= 0)
1945f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1946dccee1a1SDavid Greenman 
19476ebc3153SDavid Greenman 	return (value & 0xffff);
1948dccee1a1SDavid Greenman }
1949dccee1a1SDavid Greenman 
1950dccee1a1SDavid Greenman static void
1951f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1952dccee1a1SDavid Greenman {
1953f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1954dccee1a1SDavid Greenman 	int count = 10000;
1955dccee1a1SDavid Greenman 
1956ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1957ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1958ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1959dccee1a1SDavid Greenman 
1960ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1961ba8c6fd5SDavid Greenman 	    count--)
19626ebc3153SDavid Greenman 		DELAY(10);
1963dccee1a1SDavid Greenman 
1964dccee1a1SDavid Greenman 	if (count <= 0)
1965f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1966dccee1a1SDavid Greenman }
1967dccee1a1SDavid Greenman 
1968dccee1a1SDavid Greenman static int
1969f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1970a17c678eSDavid Greenman {
19719b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1972a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1973f7788e8eSJonathan Lemon 	struct mii_data *mii;
1974f7788e8eSJonathan Lemon 	int s, error = 0;
1975a17c678eSDavid Greenman 
1976f7788e8eSJonathan Lemon 	s = splimp();
1977a17c678eSDavid Greenman 
1978a17c678eSDavid Greenman 	switch (command) {
1979a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1980f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1981f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1982f7788e8eSJonathan Lemon 		else
1983f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1984a17c678eSDavid Greenman 
1985a17c678eSDavid Greenman 		/*
1986a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1987a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1988a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1989a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1990a17c678eSDavid Greenman 		 */
1991a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1992fb583156SDavid Greenman 			fxp_init(sc);
1993a17c678eSDavid Greenman 		} else {
1994a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
19954a5f1499SDavid Greenman 				fxp_stop(sc);
1996a17c678eSDavid Greenman 		}
1997a17c678eSDavid Greenman 		break;
1998a17c678eSDavid Greenman 
1999a17c678eSDavid Greenman 	case SIOCADDMULTI:
2000a17c678eSDavid Greenman 	case SIOCDELMULTI:
2001f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2002f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2003f7788e8eSJonathan Lemon 		else
2004f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2005a17c678eSDavid Greenman 		/*
2006a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2007a17c678eSDavid Greenman 		 * accordingly.
2008a17c678eSDavid Greenman 		 */
2009f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2010397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2011397f9dfeSDavid Greenman 		/*
2012f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2013397f9dfeSDavid Greenman 		 * again rather than else {}.
2014397f9dfeSDavid Greenman 		 */
2015f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
2016fb583156SDavid Greenman 			fxp_init(sc);
2017a17c678eSDavid Greenman 		error = 0;
2018ba8c6fd5SDavid Greenman 		break;
2019ba8c6fd5SDavid Greenman 
2020ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2021ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2022f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2023f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2024f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2025f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2026f7788e8eSJonathan Lemon 		} else {
2027ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2028f7788e8eSJonathan Lemon 		}
2029a17c678eSDavid Greenman 		break;
2030a17c678eSDavid Greenman 
2031a17c678eSDavid Greenman 	default:
2032673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2033a17c678eSDavid Greenman 	}
2034f7788e8eSJonathan Lemon 	splx(s);
2035a17c678eSDavid Greenman 	return (error);
2036a17c678eSDavid Greenman }
2037397f9dfeSDavid Greenman 
2038397f9dfeSDavid Greenman /*
203909882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
204009882363SJonathan Lemon  */
204109882363SJonathan Lemon static int
204209882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
204309882363SJonathan Lemon {
204409882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
204509882363SJonathan Lemon 	struct ifnet *ifp = &sc->sc_if;
204609882363SJonathan Lemon 	struct ifmultiaddr *ifma;
204709882363SJonathan Lemon 	int nmcasts;
204809882363SJonathan Lemon 
204909882363SJonathan Lemon 	nmcasts = 0;
205009882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
205109882363SJonathan Lemon #if __FreeBSD_version < 500000
205209882363SJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
205309882363SJonathan Lemon #else
205409882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
205509882363SJonathan Lemon #endif
205609882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
205709882363SJonathan Lemon 				continue;
205809882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
205909882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
206009882363SJonathan Lemon 				nmcasts = 0;
206109882363SJonathan Lemon 				break;
206209882363SJonathan Lemon 			}
206309882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
206409882363SJonathan Lemon 			    (void *)(uintptr_t)(volatile void *)
206509882363SJonathan Lemon 				&sc->mcsp->mc_addr[nmcasts][0], 6);
206609882363SJonathan Lemon 			nmcasts++;
206709882363SJonathan Lemon 		}
206809882363SJonathan Lemon 	}
206909882363SJonathan Lemon 	mcsp->mc_cnt = nmcasts * 6;
207009882363SJonathan Lemon 	return (nmcasts);
207109882363SJonathan Lemon }
207209882363SJonathan Lemon 
207309882363SJonathan Lemon /*
2074397f9dfeSDavid Greenman  * Program the multicast filter.
2075397f9dfeSDavid Greenman  *
2076397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2077397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
20783114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2079397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2080dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2081397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2082397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2083397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2084397f9dfeSDavid Greenman  *
2085397f9dfeSDavid Greenman  * This function must be called at splimp.
2086397f9dfeSDavid Greenman  */
2087397f9dfeSDavid Greenman static void
2088f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2089397f9dfeSDavid Greenman {
2090397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2091397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
20927dced78aSDavid Greenman 	int count;
2093397f9dfeSDavid Greenman 
20943114fdb4SDavid Greenman 	/*
20953114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
20963114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
20973114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
20983114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
20993114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
21003114fdb4SDavid Greenman 	 */
2101397f9dfeSDavid Greenman 	if (sc->tx_queued) {
21023114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
21033114fdb4SDavid Greenman 
21043114fdb4SDavid Greenman 		/*
21053114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
21063114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
21073114fdb4SDavid Greenman 		 */
21083114fdb4SDavid Greenman 		if (sc->need_mcsetup)
21093114fdb4SDavid Greenman 			return;
2110397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
21113114fdb4SDavid Greenman 
21123114fdb4SDavid Greenman 		/*
211372a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
211472a32a26SJonathan Lemon 		 * when all TX commands have been processed.
21153114fdb4SDavid Greenman 		 */
21163114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
21173114fdb4SDavid Greenman 		txp->mb_head = NULL;
21183114fdb4SDavid Greenman 		txp->cb_status = 0;
2119e8c8b728SJonathan Lemon 		txp->cb_command = FXP_CB_COMMAND_NOP |
2120e8c8b728SJonathan Lemon 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
21213114fdb4SDavid Greenman 		/*
21223114fdb4SDavid Greenman 		 * Advance the end of list forward.
21233114fdb4SDavid Greenman 		 */
21243114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
21253114fdb4SDavid Greenman 		sc->cbl_last = txp;
21263114fdb4SDavid Greenman 		sc->tx_queued++;
21273114fdb4SDavid Greenman 		/*
21283114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
21293114fdb4SDavid Greenman 		 */
21303114fdb4SDavid Greenman 		fxp_scb_wait(sc);
21312e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
21323114fdb4SDavid Greenman 		/*
21333114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
21343114fdb4SDavid Greenman 		 * card again.
21353114fdb4SDavid Greenman 		 */
21363114fdb4SDavid Greenman 		ifp->if_timer = 5;
21373114fdb4SDavid Greenman 
2138397f9dfeSDavid Greenman 		return;
2139397f9dfeSDavid Greenman 	}
2140397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2141397f9dfeSDavid Greenman 
2142397f9dfeSDavid Greenman 	/*
2143397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2144397f9dfeSDavid Greenman 	 */
2145397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
2146397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
2147397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
2148e8c8b728SJonathan Lemon 	mcsp->cb_command = FXP_CB_COMMAND_MCAS |
2149e8c8b728SJonathan Lemon 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
2150397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
215109882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2152397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2153397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2154397f9dfeSDavid Greenman 
2155397f9dfeSDavid Greenman 	/*
2156397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2157397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2158397f9dfeSDavid Greenman 	 */
21597dced78aSDavid Greenman 	count = 100;
2160397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
21617dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
21627dced78aSDavid Greenman 		DELAY(10);
21637dced78aSDavid Greenman 	if (count == 0) {
2164f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
21657dced78aSDavid Greenman 		return;
21667dced78aSDavid Greenman 	}
2167397f9dfeSDavid Greenman 
2168397f9dfeSDavid Greenman 	/*
2169397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2170397f9dfeSDavid Greenman 	 */
2171397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2172397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
21732e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2174397f9dfeSDavid Greenman 
21753114fdb4SDavid Greenman 	ifp->if_timer = 2;
2176397f9dfeSDavid Greenman 	return;
2177397f9dfeSDavid Greenman }
217872a32a26SJonathan Lemon 
217972a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
218072a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
218172a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
218272a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
218372a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
218472a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
218572a32a26SJonathan Lemon 
218672a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
218772a32a26SJonathan Lemon 
218872a32a26SJonathan Lemon struct ucode {
218972a32a26SJonathan Lemon 	u_int32_t	revision;
219072a32a26SJonathan Lemon 	u_int32_t	*ucode;
219172a32a26SJonathan Lemon 	int		length;
219272a32a26SJonathan Lemon 	u_short		int_delay_offset;
219372a32a26SJonathan Lemon 	u_short		bundle_max_offset;
219472a32a26SJonathan Lemon } ucode_table[] = {
219572a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
219672a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
219772a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
219872a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
219972a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
220072a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
220172a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
220272a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
220372a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
220472a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
220572a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
220672a32a26SJonathan Lemon };
220772a32a26SJonathan Lemon 
220872a32a26SJonathan Lemon static void
220972a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
221072a32a26SJonathan Lemon {
221172a32a26SJonathan Lemon 	struct ucode *uc;
221272a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
221372a32a26SJonathan Lemon 
221472a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
221572a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
221672a32a26SJonathan Lemon 			break;
221772a32a26SJonathan Lemon 	if (uc->ucode == NULL)
221872a32a26SJonathan Lemon 		return;
221972a32a26SJonathan Lemon 	cbp = (struct fxp_cb_ucode *)sc->cbl_base;
222072a32a26SJonathan Lemon 	cbp->cb_status = 0;
222172a32a26SJonathan Lemon 	cbp->cb_command = FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL;
222272a32a26SJonathan Lemon 	cbp->link_addr = -1;    	/* (no) next command */
222372a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
222472a32a26SJonathan Lemon 	if (uc->int_delay_offset)
222572a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->int_delay_offset] =
2226208b417fSJonathan Lemon 		    sc->tunable_int_delay + sc->tunable_int_delay / 2;
222772a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
222872a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->bundle_max_offset] =
222972a32a26SJonathan Lemon 		    sc->tunable_bundle_max;
223072a32a26SJonathan Lemon 	/*
223172a32a26SJonathan Lemon 	 * Download the ucode to the chip.
223272a32a26SJonathan Lemon 	 */
223372a32a26SJonathan Lemon 	fxp_scb_wait(sc);
223472a32a26SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
223572a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
223672a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
223772a32a26SJonathan Lemon 	fxp_dma_wait(&cbp->cb_status, sc);
223872a32a26SJonathan Lemon 	device_printf(sc->dev,
223972a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
224072a32a26SJonathan Lemon 	    sc->tunable_int_delay,
224172a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
224272a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
224372a32a26SJonathan Lemon }
224472a32a26SJonathan Lemon 
224572a32a26SJonathan Lemon static int
224672a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
224772a32a26SJonathan Lemon {
224872a32a26SJonathan Lemon 	int error, value;
224972a32a26SJonathan Lemon 
225072a32a26SJonathan Lemon 	value = *(int *)arg1;
225172a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
225272a32a26SJonathan Lemon 	if (error || !req->newptr)
225372a32a26SJonathan Lemon 		return (error);
225472a32a26SJonathan Lemon 	if (value < low || value > high)
225572a32a26SJonathan Lemon 		return (EINVAL);
225672a32a26SJonathan Lemon 	*(int *)arg1 = value;
225772a32a26SJonathan Lemon 	return (0);
225872a32a26SJonathan Lemon }
225972a32a26SJonathan Lemon 
226072a32a26SJonathan Lemon /*
226172a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
226272a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
226372a32a26SJonathan Lemon  */
226472a32a26SJonathan Lemon static int
226572a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
226672a32a26SJonathan Lemon {
226772a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
226872a32a26SJonathan Lemon }
226972a32a26SJonathan Lemon 
227072a32a26SJonathan Lemon static int
227172a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
227272a32a26SJonathan Lemon {
227372a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
227472a32a26SJonathan Lemon }
2275