xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 6008862bc2494a2094e15699f4f73fad800b6bcd)
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" },
162f7788e8eSJonathan Lemon     { 0,		NULL },
163f7788e8eSJonathan Lemon };
164f7788e8eSJonathan Lemon 
165f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
166f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
167f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
168f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
169f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
170f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
171f7788e8eSJonathan Lemon 
172f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
173f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
174f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
17548e417ebSJonathan Lemon static void		fxp_powerstate_d0(device_t dev);
176f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
177f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
178f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
179f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
180f7788e8eSJonathan Lemon 			    caddr_t data);
181f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
182f7788e8eSJonathan Lemon static int		fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
18309882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
184f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
185f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
186f7788e8eSJonathan Lemon 			    int autosize);
18700c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
18800c4116bSJonathan Lemon 			    u_int16_t data);
189f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
190f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
191f7788e8eSJonathan Lemon 			    int offset, int words);
19200c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
19300c4116bSJonathan Lemon 			    int offset, int words);
194f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
195f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
196f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
197f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
198f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
199f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
200f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
201f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
202f7788e8eSJonathan Lemon 			    int value);
20372a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
20472a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
20572a32a26SJonathan Lemon 			    int low, int high);
20672a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
20772a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
208f7788e8eSJonathan Lemon static __inline void	fxp_lwcopy(volatile u_int32_t *src,
209f7788e8eSJonathan Lemon 			    volatile u_int32_t *dst);
210f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2112e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
212f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
213f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
214f7788e8eSJonathan Lemon 
215f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
216f7788e8eSJonathan Lemon 	/* Device interface */
217f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
218f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
219f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
220f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
221f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
222f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
223f7788e8eSJonathan Lemon 
224f7788e8eSJonathan Lemon 	/* MII interface */
225f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
226f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
227f7788e8eSJonathan Lemon 
228f7788e8eSJonathan Lemon 	{ 0, 0 }
229f7788e8eSJonathan Lemon };
230f7788e8eSJonathan Lemon 
231f7788e8eSJonathan Lemon static driver_t fxp_driver = {
232f7788e8eSJonathan Lemon 	"fxp",
233f7788e8eSJonathan Lemon 	fxp_methods,
234f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
235f7788e8eSJonathan Lemon };
236f7788e8eSJonathan Lemon 
237f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
238f7788e8eSJonathan Lemon 
239f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
240f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
241f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
242f7788e8eSJonathan Lemon 
243f7788e8eSJonathan Lemon /*
244ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
245ba8c6fd5SDavid Greenman  */
246ba8c6fd5SDavid Greenman static __inline void
247f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
248ba8c6fd5SDavid Greenman {
249aed53495SDavid Greenman #ifdef __i386__
250aed53495SDavid Greenman 	*dst = *src;
251aed53495SDavid Greenman #else
252fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
253fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
254ba8c6fd5SDavid Greenman 
255ba8c6fd5SDavid Greenman 	b[0] = a[0];
256ba8c6fd5SDavid Greenman 	b[1] = a[1];
257aed53495SDavid Greenman #endif
258ba8c6fd5SDavid Greenman }
259a17c678eSDavid Greenman 
260a17c678eSDavid Greenman /*
261dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
262dfe61cf1SDavid Greenman  * completed).
263dfe61cf1SDavid Greenman  */
264c1087c13SBruce Evans static __inline void
265f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
266a17c678eSDavid Greenman {
267a17c678eSDavid Greenman 	int i = 10000;
268a17c678eSDavid Greenman 
2697dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2707dced78aSDavid Greenman 		DELAY(2);
2717dced78aSDavid Greenman 	if (i == 0)
27200c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
273e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
274e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
275e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
276e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2777dced78aSDavid Greenman }
2787dced78aSDavid Greenman 
2797dced78aSDavid Greenman static __inline void
2802e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2812e2b8238SJonathan Lemon {
2822e2b8238SJonathan Lemon 
2832e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2842e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2852e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2862e2b8238SJonathan Lemon 	}
2872e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2882e2b8238SJonathan Lemon }
2892e2b8238SJonathan Lemon 
2902e2b8238SJonathan Lemon static __inline void
291f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
2927dced78aSDavid Greenman {
2937dced78aSDavid Greenman 	int i = 10000;
2947dced78aSDavid Greenman 
2957dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
2967dced78aSDavid Greenman 		DELAY(2);
2977dced78aSDavid Greenman 	if (i == 0)
298f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
299a17c678eSDavid Greenman }
300a17c678eSDavid Greenman 
301dfe61cf1SDavid Greenman /*
302dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
303dfe61cf1SDavid Greenman  */
3046182fdbdSPeter Wemm static int
3056182fdbdSPeter Wemm fxp_probe(device_t dev)
306a17c678eSDavid Greenman {
307f7788e8eSJonathan Lemon 	u_int16_t devid;
308f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
309f7788e8eSJonathan Lemon 
31055ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
311f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
312f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
313f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
314f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
315f7788e8eSJonathan Lemon 				return (0);
31655ce7b51SDavid Greenman 			}
317dd68ef16SPeter Wemm 		}
318f7788e8eSJonathan Lemon 	}
319f7788e8eSJonathan Lemon 	return (ENXIO);
3206182fdbdSPeter Wemm }
3216182fdbdSPeter Wemm 
32248e417ebSJonathan Lemon static void
32348e417ebSJonathan Lemon fxp_powerstate_d0(device_t dev)
32448e417ebSJonathan Lemon {
32548e417ebSJonathan Lemon #if __FreeBSD_version >= 430002
32648e417ebSJonathan Lemon 	u_int32_t iobase, membase, irq;
32748e417ebSJonathan Lemon 
32848e417ebSJonathan Lemon 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
32948e417ebSJonathan Lemon 		/* Save important PCI config data. */
33048e417ebSJonathan Lemon 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
33148e417ebSJonathan Lemon 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
33248e417ebSJonathan Lemon 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
33348e417ebSJonathan Lemon 
33448e417ebSJonathan Lemon 		/* Reset the power state. */
33548e417ebSJonathan Lemon 		device_printf(dev, "chip is in D%d power mode "
33648e417ebSJonathan Lemon 		    "-- setting to D0\n", pci_get_powerstate(dev));
33748e417ebSJonathan Lemon 
33848e417ebSJonathan Lemon 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
33948e417ebSJonathan Lemon 
34048e417ebSJonathan Lemon 		/* Restore PCI config data. */
34148e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
34248e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
34348e417ebSJonathan Lemon 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
34448e417ebSJonathan Lemon 	}
34548e417ebSJonathan Lemon #endif
34648e417ebSJonathan Lemon }
34748e417ebSJonathan Lemon 
3486182fdbdSPeter Wemm static int
3496182fdbdSPeter Wemm fxp_attach(device_t dev)
350a17c678eSDavid Greenman {
3516182fdbdSPeter Wemm 	int error = 0;
3526182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
353ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
3549fa6ccfbSMatt Jacob 	u_int32_t val;
355f7788e8eSJonathan Lemon 	u_int16_t data;
356f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
357f7788e8eSJonathan Lemon 	int s;
358a17c678eSDavid Greenman 
359f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
360f7788e8eSJonathan Lemon 	sc->dev = dev;
3616c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
362a1a9c8f7SJonathan Lemon 	sysctl_ctx_init(&sc->sysctl_ctx);
3636008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
3646008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
365a17c678eSDavid Greenman 
366f7788e8eSJonathan Lemon 	s = splimp();
367a17c678eSDavid Greenman 
368dfe61cf1SDavid Greenman 	/*
3699fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3709fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
371df373873SWes Peters 	 */
3726182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
373df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3746182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3759fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
376df373873SWes Peters 
37748e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
3788d799694SBill Paul 
379df373873SWes Peters 	/*
3809fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3819fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3829fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
383dfe61cf1SDavid Greenman 	 */
3849fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3859fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3862a05a4ebSMatt Jacob 	prefer_iomap = 0;
3872a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3882a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3899fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3909fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
3919fa6ccfbSMatt Jacob 	}
3929fa6ccfbSMatt Jacob 
3939fa6ccfbSMatt Jacob 	if (val & m1) {
3949fa6ccfbSMatt Jacob 		sc->rtp =
3959fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3969fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3979fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3986182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
3999fa6ccfbSMatt Jacob 	}
4009fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
4019fa6ccfbSMatt Jacob 		sc->rtp =
4029fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4039fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4049fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4059fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
4069fa6ccfbSMatt Jacob 	}
4079fa6ccfbSMatt Jacob 
4086182fdbdSPeter Wemm 	if (!sc->mem) {
4099fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
4106182fdbdSPeter Wemm 		error = ENXIO;
411a17c678eSDavid Greenman 		goto fail;
412a17c678eSDavid Greenman         }
4139fa6ccfbSMatt Jacob 	if (bootverbose) {
4149fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4159fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4169fa6ccfbSMatt Jacob 	}
4174fc1dda9SAndrew Gallatin 
4184fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4194fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
420a17c678eSDavid Greenman 
421a17c678eSDavid Greenman 	/*
422dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
423dfe61cf1SDavid Greenman 	 */
4246182fdbdSPeter Wemm 	rid = 0;
4256182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4266182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4276182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4286182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4296182fdbdSPeter Wemm 		error = ENXIO;
4306182fdbdSPeter Wemm 		goto fail;
4316182fdbdSPeter Wemm 	}
4326182fdbdSPeter Wemm 
433566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
434566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
4356182fdbdSPeter Wemm 	if (error) {
4366182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
437a17c678eSDavid Greenman 		goto fail;
438a17c678eSDavid Greenman 	}
439a17c678eSDavid Greenman 
440f7788e8eSJonathan Lemon 	/*
441f7788e8eSJonathan Lemon 	 * Reset to a stable state.
442f7788e8eSJonathan Lemon 	 */
443f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
444f7788e8eSJonathan Lemon 	DELAY(10);
445f7788e8eSJonathan Lemon 
446f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
447f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
448f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
449f7788e8eSJonathan Lemon 		goto failmem;
450f7788e8eSJonathan Lemon 
451f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
452f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
453f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
454f7788e8eSJonathan Lemon 		goto failmem;
455f7788e8eSJonathan Lemon 
456f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
457f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
458f7788e8eSJonathan Lemon 		goto failmem;
459f7788e8eSJonathan Lemon 
460f7788e8eSJonathan Lemon 	/*
461f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
462f7788e8eSJonathan Lemon 	 */
463f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
464f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
465f7788e8eSJonathan Lemon 			goto failmem;
466f7788e8eSJonathan Lemon 		}
467f7788e8eSJonathan Lemon 	}
468f7788e8eSJonathan Lemon 
469f7788e8eSJonathan Lemon 	/*
470f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
471f7788e8eSJonathan Lemon 	 */
472f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
473f7788e8eSJonathan Lemon 
474f7788e8eSJonathan Lemon 	/*
4753bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
476f7788e8eSJonathan Lemon 	 */
477f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
478f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
479f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
480dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
481f7788e8eSJonathan Lemon 
482f7788e8eSJonathan Lemon 	/*
48372a32a26SJonathan Lemon 	 * Create the sysctl tree
48472a32a26SJonathan Lemon 	 */
48572a32a26SJonathan Lemon 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
48672a32a26SJonathan Lemon 	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
48772a32a26SJonathan Lemon 	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
48872a32a26SJonathan Lemon 	if (sc->sysctl_tree == NULL)
48972a32a26SJonathan Lemon 		goto fail;
49072a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
49172a32a26SJonathan Lemon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
49272a32a26SJonathan Lemon 	    &sc->tunable_int_delay, 0, &sysctl_hw_fxp_int_delay, "I",
49372a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
49472a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
49572a32a26SJonathan Lemon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
49672a32a26SJonathan Lemon 	    &sc->tunable_bundle_max, 0, &sysctl_hw_fxp_bundle_max, "I",
49772a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
49872a32a26SJonathan Lemon 
49972a32a26SJonathan Lemon 	/*
50072a32a26SJonathan Lemon 	 * Pull in device tunables.
50172a32a26SJonathan Lemon 	 */
50272a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
50372a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
50472a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50572a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
50672a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50772a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
50872a32a26SJonathan Lemon 
50972a32a26SJonathan Lemon 	/*
51072a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
5113bd07cfdSJonathan Lemon 	 */
5123bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
5133bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
51472a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
51572a32a26SJonathan Lemon 	else
51672a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
5173bd07cfdSJonathan Lemon 
5183bd07cfdSJonathan Lemon 	/*
5192e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
52000c4116bSJonathan Lemon 	 *
52172a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
52272a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
52372a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
52400c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
52500c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
52600c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
52700c4116bSJonathan Lemon 	 *
52800c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5292e2b8238SJonathan Lemon 	 */
5302e2b8238SJonathan Lemon 	i = pci_get_device(dev);
53172a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
53272a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
53300c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
53400c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
53500c4116bSJonathan Lemon 			u_int16_t cksum;
53600c4116bSJonathan Lemon 			int i;
53700c4116bSJonathan Lemon 
53800c4116bSJonathan Lemon 			device_printf(dev,
539001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
54000c4116bSJonathan Lemon 			data &= ~0x02;
54100c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
54200c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
54300c4116bSJonathan Lemon 			cksum = 0;
54400c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
54500c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
54600c4116bSJonathan Lemon 				cksum += data;
54700c4116bSJonathan Lemon 			}
54800c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
54900c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
55000c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
55100c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
55200c4116bSJonathan Lemon 			device_printf(dev,
55300c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
55400c4116bSJonathan Lemon 			    i, data, cksum);
55500c4116bSJonathan Lemon #if 1
55600c4116bSJonathan Lemon 			/*
55700c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
55800c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
55900c4116bSJonathan Lemon 			 */
5602e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
56100c4116bSJonathan Lemon #endif
56200c4116bSJonathan Lemon 		}
56300c4116bSJonathan Lemon 	}
5642e2b8238SJonathan Lemon 
5652e2b8238SJonathan Lemon 	/*
5663bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5673bd07cfdSJonathan Lemon 	 */
56872a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5693bd07cfdSJonathan Lemon 		/*
57074396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
57174396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
57274396a0aSJonathan Lemon 		 * the board to turn on MWI.
5733bd07cfdSJonathan Lemon 		 */
57474396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
57574396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5763bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5773bd07cfdSJonathan Lemon 
5783bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5793bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
580920b58e8SBrooks Davis 
581e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
582e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
5833bd07cfdSJonathan Lemon 	}
5843bd07cfdSJonathan Lemon 
5853bd07cfdSJonathan Lemon 	/*
586f7788e8eSJonathan Lemon 	 * Read MAC address.
587f7788e8eSJonathan Lemon 	 */
588f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
589f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
590f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
591f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
592f7788e8eSJonathan Lemon 	if (bootverbose) {
5932e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
594f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
5952e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
5962e2b8238SJonathan Lemon 		    pci_get_revid(dev));
59772a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
59872a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
59972a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
600f7788e8eSJonathan Lemon 	}
601f7788e8eSJonathan Lemon 
602f7788e8eSJonathan Lemon 	/*
603f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
604f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
605f7788e8eSJonathan Lemon 	 *
606f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
607f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
608f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
609f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
610f7788e8eSJonathan Lemon 	 */
611f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
612f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
613f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
614f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
615f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
616f7788e8eSJonathan Lemon 	} else {
617f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
618f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
619f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
6206182fdbdSPeter Wemm 			error = ENXIO;
621ba8c6fd5SDavid Greenman 			goto fail;
622a17c678eSDavid Greenman 		}
623f7788e8eSJonathan Lemon 	}
624dccee1a1SDavid Greenman 
625a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
6266182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
627a17c678eSDavid Greenman 	ifp->if_name = "fxp";
628a17c678eSDavid Greenman 	ifp->if_output = ether_output;
629a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
630fb583156SDavid Greenman 	ifp->if_init = fxp_init;
631ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
632ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
633ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
634ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
635ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
636a17c678eSDavid Greenman 
637dfe61cf1SDavid Greenman 	/*
638dfe61cf1SDavid Greenman 	 * Attach the interface.
639dfe61cf1SDavid Greenman 	 */
64021b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
641f7788e8eSJonathan Lemon 
642e8c8b728SJonathan Lemon 	/*
643e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
644e8c8b728SJonathan Lemon 	 */
645e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
646e8c8b728SJonathan Lemon 
647483b9871SDavid Greenman 	/*
6483114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
6493114fdb4SDavid Greenman 	 * TX descriptors.
650483b9871SDavid Greenman 	 */
6513114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
6524a684684SDavid Greenman 
653f7788e8eSJonathan Lemon 	splx(s);
654f7788e8eSJonathan Lemon 	return (0);
655a17c678eSDavid Greenman 
656f7788e8eSJonathan Lemon failmem:
657f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
658f7788e8eSJonathan Lemon 	error = ENOMEM;
659a17c678eSDavid Greenman fail:
660f7788e8eSJonathan Lemon 	splx(s);
661f7788e8eSJonathan Lemon 	fxp_release(sc);
662f7788e8eSJonathan Lemon 	return (error);
663f7788e8eSJonathan Lemon }
664f7788e8eSJonathan Lemon 
665f7788e8eSJonathan Lemon /*
666f7788e8eSJonathan Lemon  * release all resources
667f7788e8eSJonathan Lemon  */
668f7788e8eSJonathan Lemon static void
669f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
670f7788e8eSJonathan Lemon {
671f7788e8eSJonathan Lemon 
672f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
6733bd07cfdSJonathan Lemon 	if (sc->miibus)
674f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
675f7788e8eSJonathan Lemon 
676f7788e8eSJonathan Lemon 	if (sc->cbl_base)
677f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
678f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
679f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
680f7788e8eSJonathan Lemon 	if (sc->mcsp)
681f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
682f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
683f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
684f7788e8eSJonathan Lemon 
685f7788e8eSJonathan Lemon 	if (sc->ih)
686f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
687f7788e8eSJonathan Lemon 	if (sc->irq)
688f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
689f7788e8eSJonathan Lemon 	if (sc->mem)
690f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
69172a32a26SJonathan Lemon 
69272a32a26SJonathan Lemon         sysctl_ctx_free(&sc->sysctl_ctx);
69372a32a26SJonathan Lemon 
6940f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
6956182fdbdSPeter Wemm }
6966182fdbdSPeter Wemm 
6976182fdbdSPeter Wemm /*
6986182fdbdSPeter Wemm  * Detach interface.
6996182fdbdSPeter Wemm  */
7006182fdbdSPeter Wemm static int
7016182fdbdSPeter Wemm fxp_detach(device_t dev)
7026182fdbdSPeter Wemm {
7036182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
704f7788e8eSJonathan Lemon 	int s;
7056182fdbdSPeter Wemm 
7062e2b8238SJonathan Lemon 	/* disable interrupts */
7072e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
7082e2b8238SJonathan Lemon 
709f7788e8eSJonathan Lemon 	s = splimp();
7106182fdbdSPeter Wemm 
7116182fdbdSPeter Wemm 	/*
7126182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
7136182fdbdSPeter Wemm 	 */
7146182fdbdSPeter Wemm 	fxp_stop(sc);
7156182fdbdSPeter Wemm 
7166182fdbdSPeter Wemm 	/*
717f7788e8eSJonathan Lemon 	 * Close down routes etc.
7186182fdbdSPeter Wemm 	 */
719f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
7206182fdbdSPeter Wemm 
7216182fdbdSPeter Wemm 	/*
7226182fdbdSPeter Wemm 	 * Free all media structures.
7236182fdbdSPeter Wemm 	 */
7246182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
7256182fdbdSPeter Wemm 
726f7788e8eSJonathan Lemon 	splx(s);
7276182fdbdSPeter Wemm 
728f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
729f7788e8eSJonathan Lemon 	fxp_release(sc);
7306182fdbdSPeter Wemm 
731f7788e8eSJonathan Lemon 	return (0);
732a17c678eSDavid Greenman }
733a17c678eSDavid Greenman 
734a17c678eSDavid Greenman /*
7354a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
736a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
737a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
738a17c678eSDavid Greenman  */
7396182fdbdSPeter Wemm static int
7406182fdbdSPeter Wemm fxp_shutdown(device_t dev)
741a17c678eSDavid Greenman {
7426182fdbdSPeter Wemm 	/*
7436182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
7446182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
7456182fdbdSPeter Wemm 	 * reboot before the driver initializes.
7466182fdbdSPeter Wemm 	 */
7476182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
748f7788e8eSJonathan Lemon 	return (0);
749a17c678eSDavid Greenman }
750a17c678eSDavid Greenman 
7517dced78aSDavid Greenman /*
7527dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
7537dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
7547dced78aSDavid Greenman  * resume.
7557dced78aSDavid Greenman  */
7567dced78aSDavid Greenman static int
7577dced78aSDavid Greenman fxp_suspend(device_t dev)
7587dced78aSDavid Greenman {
7597dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
760f7788e8eSJonathan Lemon 	int i, s;
7617dced78aSDavid Greenman 
762f7788e8eSJonathan Lemon 	s = splimp();
7637dced78aSDavid Greenman 
7647dced78aSDavid Greenman 	fxp_stop(sc);
7657dced78aSDavid Greenman 
7667dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
7677dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
7687dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
7697dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
7707dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
7717dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
7727dced78aSDavid Greenman 
7737dced78aSDavid Greenman 	sc->suspended = 1;
7747dced78aSDavid Greenman 
775f7788e8eSJonathan Lemon 	splx(s);
776f7788e8eSJonathan Lemon 	return (0);
7777dced78aSDavid Greenman }
7787dced78aSDavid Greenman 
7797dced78aSDavid Greenman /*
7807dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
7817dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
7827dced78aSDavid Greenman  * appropriate.
7837dced78aSDavid Greenman  */
7847dced78aSDavid Greenman static int
7857dced78aSDavid Greenman fxp_resume(device_t dev)
7867dced78aSDavid Greenman {
7877dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
7887dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
7897dced78aSDavid Greenman 	u_int16_t pci_command;
790f7788e8eSJonathan Lemon 	int i, s;
7917dced78aSDavid Greenman 
792f7788e8eSJonathan Lemon 	s = splimp();
7937dced78aSDavid Greenman 
79448e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
79548e417ebSJonathan Lemon 
7967dced78aSDavid Greenman 	/* better way to do this? */
7977dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
7987dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
7997dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
8007dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
8017dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
8027dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
8037dced78aSDavid Greenman 
8047dced78aSDavid Greenman 	/* reenable busmastering */
8057dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
8067dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
8077dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
8087dced78aSDavid Greenman 
8097dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
8107dced78aSDavid Greenman 	DELAY(10);
8117dced78aSDavid Greenman 
8127dced78aSDavid Greenman 	/* reinitialize interface if necessary */
8137dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
8147dced78aSDavid Greenman 		fxp_init(sc);
8157dced78aSDavid Greenman 
8167dced78aSDavid Greenman 	sc->suspended = 0;
8177dced78aSDavid Greenman 
818f7788e8eSJonathan Lemon 	splx(s);
819ba8c6fd5SDavid Greenman 	return (0);
820f7788e8eSJonathan Lemon }
821ba8c6fd5SDavid Greenman 
82200c4116bSJonathan Lemon static void
82300c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
82400c4116bSJonathan Lemon {
82500c4116bSJonathan Lemon 	u_int16_t reg;
82600c4116bSJonathan Lemon 	int x;
82700c4116bSJonathan Lemon 
82800c4116bSJonathan Lemon 	/*
82900c4116bSJonathan Lemon 	 * Shift in data.
83000c4116bSJonathan Lemon 	 */
83100c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
83200c4116bSJonathan Lemon 		if (data & x)
83300c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
83400c4116bSJonathan Lemon 		else
83500c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
83600c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
83700c4116bSJonathan Lemon 		DELAY(1);
83800c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
83900c4116bSJonathan Lemon 		DELAY(1);
84000c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
84100c4116bSJonathan Lemon 		DELAY(1);
84200c4116bSJonathan Lemon 	}
84300c4116bSJonathan Lemon }
84400c4116bSJonathan Lemon 
845f7788e8eSJonathan Lemon /*
846f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
847f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
848f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
849f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
850f7788e8eSJonathan Lemon  * every 16 bits of data.
851f7788e8eSJonathan Lemon  */
852f7788e8eSJonathan Lemon static u_int16_t
853f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
854f7788e8eSJonathan Lemon {
855f7788e8eSJonathan Lemon 	u_int16_t reg, data;
856f7788e8eSJonathan Lemon 	int x;
857ba8c6fd5SDavid Greenman 
858f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
859f7788e8eSJonathan Lemon 	/*
860f7788e8eSJonathan Lemon 	 * Shift in read opcode.
861f7788e8eSJonathan Lemon 	 */
86200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
863f7788e8eSJonathan Lemon 	/*
864f7788e8eSJonathan Lemon 	 * Shift in address.
865f7788e8eSJonathan Lemon 	 */
866f7788e8eSJonathan Lemon 	data = 0;
867f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
868f7788e8eSJonathan Lemon 		if (offset & x)
869f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
870f7788e8eSJonathan Lemon 		else
871f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
872f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
873f7788e8eSJonathan Lemon 		DELAY(1);
874f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
875f7788e8eSJonathan Lemon 		DELAY(1);
876f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
877f7788e8eSJonathan Lemon 		DELAY(1);
878f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
879f7788e8eSJonathan Lemon 		data++;
880f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
881f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
882f7788e8eSJonathan Lemon 			break;
883f7788e8eSJonathan Lemon 		}
884f7788e8eSJonathan Lemon 	}
885f7788e8eSJonathan Lemon 	/*
886f7788e8eSJonathan Lemon 	 * Shift out data.
887f7788e8eSJonathan Lemon 	 */
888f7788e8eSJonathan Lemon 	data = 0;
889f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
890f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
891f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
892f7788e8eSJonathan Lemon 		DELAY(1);
893f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
894f7788e8eSJonathan Lemon 			data |= x;
895f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
896f7788e8eSJonathan Lemon 		DELAY(1);
897f7788e8eSJonathan Lemon 	}
898f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
899f7788e8eSJonathan Lemon 	DELAY(1);
900f7788e8eSJonathan Lemon 
901f7788e8eSJonathan Lemon 	return (data);
902ba8c6fd5SDavid Greenman }
903ba8c6fd5SDavid Greenman 
90400c4116bSJonathan Lemon static void
90500c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
90600c4116bSJonathan Lemon {
90700c4116bSJonathan Lemon 	int i;
90800c4116bSJonathan Lemon 
90900c4116bSJonathan Lemon 	/*
91000c4116bSJonathan Lemon 	 * Erase/write enable.
91100c4116bSJonathan Lemon 	 */
91200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
91300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
91400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
91500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
91600c4116bSJonathan Lemon 	DELAY(1);
91700c4116bSJonathan Lemon 	/*
91800c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
91900c4116bSJonathan Lemon 	 */
92000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
92100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
92200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
92300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
92400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
92500c4116bSJonathan Lemon 	DELAY(1);
92600c4116bSJonathan Lemon 	/*
92700c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
92800c4116bSJonathan Lemon 	 */
92900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
93000c4116bSJonathan Lemon 	DELAY(1);
93100c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
93200c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
93300c4116bSJonathan Lemon 			break;
93400c4116bSJonathan Lemon 		DELAY(50);
93500c4116bSJonathan Lemon 	}
93600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
93700c4116bSJonathan Lemon 	DELAY(1);
93800c4116bSJonathan Lemon 	/*
93900c4116bSJonathan Lemon 	 * Erase/write disable.
94000c4116bSJonathan Lemon 	 */
94100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
94200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
94300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
94400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
94500c4116bSJonathan Lemon 	DELAY(1);
94600c4116bSJonathan Lemon }
94700c4116bSJonathan Lemon 
948ba8c6fd5SDavid Greenman /*
949e9bf2fa7SDavid Greenman  * From NetBSD:
950e9bf2fa7SDavid Greenman  *
951e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
952e9bf2fa7SDavid Greenman  *
953e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
954e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
955e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
956e9bf2fa7SDavid Greenman  *
957e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
958e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
959e9bf2fa7SDavid Greenman  *
960e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
961e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
962e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
963e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
964e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
965e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
966e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
967e9bf2fa7SDavid Greenman  */
968e9bf2fa7SDavid Greenman static void
969f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
970e9bf2fa7SDavid Greenman {
971e9bf2fa7SDavid Greenman 
972f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
973f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
974f7788e8eSJonathan Lemon 
975f7788e8eSJonathan Lemon 	/* autosize */
976f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
977e9bf2fa7SDavid Greenman }
978f7788e8eSJonathan Lemon 
979ba8c6fd5SDavid Greenman static void
980f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
981ba8c6fd5SDavid Greenman {
982f7788e8eSJonathan Lemon 	int i;
983ba8c6fd5SDavid Greenman 
984f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
985f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
986ba8c6fd5SDavid Greenman }
987ba8c6fd5SDavid Greenman 
98800c4116bSJonathan Lemon static void
98900c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
99000c4116bSJonathan Lemon {
99100c4116bSJonathan Lemon 	int i;
99200c4116bSJonathan Lemon 
99300c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
99400c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
99500c4116bSJonathan Lemon }
99600c4116bSJonathan Lemon 
997a17c678eSDavid Greenman /*
998a17c678eSDavid Greenman  * Start packet transmission on the interface.
999a17c678eSDavid Greenman  */
1000a17c678eSDavid Greenman static void
1001f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1002a17c678eSDavid Greenman {
10039b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1004a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1005a17c678eSDavid Greenman 
1006a17c678eSDavid Greenman 	/*
1007483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1008483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1009483b9871SDavid Greenman 	 * of the command chain).
1010a17c678eSDavid Greenman 	 */
10110f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1012a17c678eSDavid Greenman 		return;
10130f4dc94cSChuck Paterson 	}
10141cd443acSDavid Greenman 
1015483b9871SDavid Greenman 	txp = NULL;
1016483b9871SDavid Greenman 
1017483b9871SDavid Greenman 	/*
1018483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1019483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
10203114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
10213114fdb4SDavid Greenman 	 *       a NOP command when needed.
1022483b9871SDavid Greenman 	 */
10233114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1024483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
1025483b9871SDavid Greenman 		int segment;
1026483b9871SDavid Greenman 
1027dfe61cf1SDavid Greenman 		/*
1028dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1029dfe61cf1SDavid Greenman 		 */
10306318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
1031a17c678eSDavid Greenman 
1032dfe61cf1SDavid Greenman 		/*
1033483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1034dfe61cf1SDavid Greenman 		 */
1035a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
1036a17c678eSDavid Greenman 
1037a17c678eSDavid Greenman 		/*
1038a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1039483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1040a17c678eSDavid Greenman 		 * and size of the mbuf.
1041a17c678eSDavid Greenman 		 */
104223a0ed7cSDavid Greenman tbdinit:
1043a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
1044a17c678eSDavid Greenman 			if (m->m_len != 0) {
1045a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1046a17c678eSDavid Greenman 					break;
1047a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1048a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1049a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1050a17c678eSDavid Greenman 				segment++;
1051a17c678eSDavid Greenman 			}
1052a17c678eSDavid Greenman 		}
1053fb583156SDavid Greenman 		if (m != NULL) {
105423a0ed7cSDavid Greenman 			struct mbuf *mn;
105523a0ed7cSDavid Greenman 
1056a17c678eSDavid Greenman 			/*
10573bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
10583bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
10593bd07cfdSJonathan Lemon 			 * new buffers.
1060a17c678eSDavid Greenman 			 */
106123a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
106223a0ed7cSDavid Greenman 			if (mn == NULL) {
106323a0ed7cSDavid Greenman 				m_freem(mb_head);
1064483b9871SDavid Greenman 				break;
1065a17c678eSDavid Greenman 			}
106623a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
106723a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
106823a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
106923a0ed7cSDavid Greenman 					m_freem(mn);
107023a0ed7cSDavid Greenman 					m_freem(mb_head);
1071483b9871SDavid Greenman 					break;
107223a0ed7cSDavid Greenman 				}
107323a0ed7cSDavid Greenman 			}
1074ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1075ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
107623a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
107723a0ed7cSDavid Greenman 			m_freem(mb_head);
107823a0ed7cSDavid Greenman 			mb_head = mn;
107923a0ed7cSDavid Greenman 			goto tbdinit;
108023a0ed7cSDavid Greenman 		}
108123a0ed7cSDavid Greenman 
108223a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10831cd443acSDavid Greenman 		txp->mb_head = mb_head;
1084a17c678eSDavid Greenman 		txp->cb_status = 0;
10853114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1086a17c678eSDavid Greenman 			txp->cb_command =
10873bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10883bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
10893114fdb4SDavid Greenman 		} else {
10903114fdb4SDavid Greenman 			txp->cb_command =
10913bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10923bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
10933114fdb4SDavid Greenman 			/*
10943bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
10953bd07cfdSJonathan Lemon 			 * from the card again.
10963114fdb4SDavid Greenman 			 */
10973114fdb4SDavid Greenman 			ifp->if_timer = 5;
10983114fdb4SDavid Greenman 		}
1099f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1100a17c678eSDavid Greenman 
1101a17c678eSDavid Greenman 		/*
1102483b9871SDavid Greenman 		 * Advance the end of list forward.
1103a17c678eSDavid Greenman 		 */
110406175228SAndrew Gallatin 
110506175228SAndrew Gallatin #ifdef __alpha__
110606175228SAndrew Gallatin 		/*
110706175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
110806175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
110906175228SAndrew Gallatin 		 * up the status while we update the command field.
111006175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
111106175228SAndrew Gallatin 		 */
111206175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
111306175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
111406175228SAndrew Gallatin #else
1115a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
111606175228SAndrew Gallatin #endif /*__alpha__*/
1117a17c678eSDavid Greenman 		sc->cbl_last = txp;
1118a17c678eSDavid Greenman 
1119a17c678eSDavid Greenman 		/*
11201cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
11211cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1122483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1123a17c678eSDavid Greenman 		 */
11241cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1125a17c678eSDavid Greenman 			sc->cbl_first = txp;
1126a17c678eSDavid Greenman 
11271cd443acSDavid Greenman 		sc->tx_queued++;
11281cd443acSDavid Greenman 
1129a17c678eSDavid Greenman 		/*
1130a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1131a17c678eSDavid Greenman 		 */
1132fb583156SDavid Greenman 		if (ifp->if_bpf)
113394927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
1134483b9871SDavid Greenman 	}
1135483b9871SDavid Greenman 
1136483b9871SDavid Greenman 	/*
1137483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1138483b9871SDavid Greenman 	 * going again if suspended.
1139483b9871SDavid Greenman 	 */
1140483b9871SDavid Greenman 	if (txp != NULL) {
1141483b9871SDavid Greenman 		fxp_scb_wait(sc);
11422e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1143483b9871SDavid Greenman 	}
1144a17c678eSDavid Greenman }
1145a17c678eSDavid Greenman 
1146e4fc250cSLuigi Rizzo static void fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count);
1147e4fc250cSLuigi Rizzo 
1148e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1149e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1150e4fc250cSLuigi Rizzo 
1151e4fc250cSLuigi Rizzo static void
1152e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1153e4fc250cSLuigi Rizzo {
1154e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
1155e4fc250cSLuigi Rizzo 	u_int8_t statack;
1156e4fc250cSLuigi Rizzo 
1157e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1158e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1159e4fc250cSLuigi Rizzo 		return;
1160e4fc250cSLuigi Rizzo 	}
1161e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1162e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1163e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
1164e4fc250cSLuigi Rizzo 		u_int8_t tmp;
11656481f301SPeter Wemm 
1166e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1167e4fc250cSLuigi Rizzo 		if (tmp == 0xff || tmp == 0)
1168e4fc250cSLuigi Rizzo 			return; /* nothing to do */
1169e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1170e4fc250cSLuigi Rizzo 		/* ack what we can */
1171e4fc250cSLuigi Rizzo 		if (tmp != 0)
1172e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1173e4fc250cSLuigi Rizzo 		statack |= tmp;
1174e4fc250cSLuigi Rizzo 	}
1175e4fc250cSLuigi Rizzo 	fxp_intr_body(sc, statack, count);
1176e4fc250cSLuigi Rizzo }
1177e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1178e4fc250cSLuigi Rizzo 
1179a17c678eSDavid Greenman /*
11809c7d2607SDavid Greenman  * Process interface interrupts.
1181a17c678eSDavid Greenman  */
118294927790SDavid Greenman static void
1183f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1184a17c678eSDavid Greenman {
1185f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
11861cd443acSDavid Greenman 	u_int8_t statack;
11870f4dc94cSChuck Paterson 
1188e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1189e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
1190e4fc250cSLuigi Rizzo 
1191e4fc250cSLuigi Rizzo 	if (ifp->if_ipending & IFF_POLLING)
1192e4fc250cSLuigi Rizzo 		return;
1193e4fc250cSLuigi Rizzo 	if (ether_poll_register(fxp_poll, ifp)) {
1194e4fc250cSLuigi Rizzo 		/* disable interrupts */
1195e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
1196e4fc250cSLuigi Rizzo 		fxp_poll(ifp, 0, 1);
1197e4fc250cSLuigi Rizzo 		return;
1198e4fc250cSLuigi Rizzo 	}
1199e4fc250cSLuigi Rizzo #endif
1200e4fc250cSLuigi Rizzo 
1201b184b38eSDavid Greenman 	if (sc->suspended) {
1202b184b38eSDavid Greenman 		return;
1203b184b38eSDavid Greenman 	}
1204b184b38eSDavid Greenman 
1205b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1206a17c678eSDavid Greenman 		/*
120711457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
120811457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
120911457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
121011457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
121111457bbfSJonathan Lemon 		 */
121211457bbfSJonathan Lemon 		if (statack == 0xff)
121311457bbfSJonathan Lemon 			return;
121411457bbfSJonathan Lemon 
121511457bbfSJonathan Lemon 		/*
1216a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1217a17c678eSDavid Greenman 		 */
1218ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1219e4fc250cSLuigi Rizzo 		fxp_intr_body(sc, statack, -1);
1220e4fc250cSLuigi Rizzo 	}
1221e4fc250cSLuigi Rizzo }
1222e4fc250cSLuigi Rizzo 
1223e4fc250cSLuigi Rizzo static void
1224e4fc250cSLuigi Rizzo fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count)
1225e4fc250cSLuigi Rizzo {
1226e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
1227a17c678eSDavid Greenman 
1228a17c678eSDavid Greenman 		/*
12293114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
123006936301SBill Paul 		 *
123106936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
123206936301SBill Paul 		 * be that this event (control unit not ready) was not
123306936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
123406936301SBill Paul 		 * The exact sequence of events that occur when the interface
123506936301SBill Paul 		 * is brought up are different now, and if this event
123606936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
123706936301SBill Paul 		 * can stall for several seconds. The result is that no
123806936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
123906936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
12403114fdb4SDavid Greenman 		 */
124106936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
12423114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
12433114fdb4SDavid Greenman 
12443114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
12453114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
12463114fdb4SDavid Greenman 			    txp = txp->next) {
12473114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
12483114fdb4SDavid Greenman 					m_freem(txp->mb_head);
12493114fdb4SDavid Greenman 					txp->mb_head = NULL;
12503114fdb4SDavid Greenman 				}
12513114fdb4SDavid Greenman 				sc->tx_queued--;
12523114fdb4SDavid Greenman 			}
12533114fdb4SDavid Greenman 			sc->cbl_first = txp;
12543114fdb4SDavid Greenman 			ifp->if_timer = 0;
12553114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
12563114fdb4SDavid Greenman 				if (sc->need_mcsetup)
12573114fdb4SDavid Greenman 					fxp_mc_setup(sc);
12583114fdb4SDavid Greenman 			}
12593114fdb4SDavid Greenman 			/*
12603114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
12613114fdb4SDavid Greenman 			 */
12623114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
12633114fdb4SDavid Greenman 				fxp_start(ifp);
12643114fdb4SDavid Greenman 		}
12653114fdb4SDavid Greenman 		/*
1266a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1267a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1268a17c678eSDavid Greenman 		 * re-start the receiver.
1269a17c678eSDavid Greenman 		 */
1270a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1271a17c678eSDavid Greenman 			struct mbuf *m;
1272a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1273a17c678eSDavid Greenman rcvloop:
1274a17c678eSDavid Greenman 			m = sc->rfa_headm;
1275ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1276ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1277a17c678eSDavid Greenman 
1278e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1279e4fc250cSLuigi Rizzo 			if (count < 0 || count-- > 0)
1280e4fc250cSLuigi Rizzo #endif
1281a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1282dfe61cf1SDavid Greenman 				/*
1283dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1284dfe61cf1SDavid Greenman 				 */
1285a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1286a17c678eSDavid Greenman 				m->m_next = NULL;
1287a17c678eSDavid Greenman 
1288dfe61cf1SDavid Greenman 				/*
1289ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1290ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1291ba8c6fd5SDavid Greenman 				 * instead.
1292dfe61cf1SDavid Greenman 				 */
1293a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1294a17c678eSDavid Greenman 					struct ether_header *eh;
1295aed53495SDavid Greenman 					int total_len;
1296a17c678eSDavid Greenman 
1297ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1298ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1299ba8c6fd5SDavid Greenman 					if (total_len <
1300ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
130106339180SDavid Greenman 						m_freem(m);
130206339180SDavid Greenman 						goto rcvloop;
130306339180SDavid Greenman 					}
1304920b58e8SBrooks Davis 
1305e8c8b728SJonathan Lemon 					/*
1306e8c8b728SJonathan Lemon 					 * Drop the packet if it has CRC
1307e8c8b728SJonathan Lemon 					 * errors.  This test is only needed
1308e8c8b728SJonathan Lemon 					 * when doing 802.1q VLAN on the 82557
1309e8c8b728SJonathan Lemon 					 * chip.
1310e8c8b728SJonathan Lemon 					 */
1311e8c8b728SJonathan Lemon 					if (rfa->rfa_status &
1312e8c8b728SJonathan Lemon 					    FXP_RFA_STATUS_CRC) {
1313e8c8b728SJonathan Lemon 						m_freem(m);
1314e8c8b728SJonathan Lemon 						goto rcvloop;
1315e8c8b728SJonathan Lemon 					}
1316920b58e8SBrooks Davis 
1317a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
13182e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1319a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1320ba8c6fd5SDavid Greenman 					m->m_data +=
1321ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1322ab090e5bSLuigi Rizzo 					m->m_len -=
1323ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1324ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1325a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1326a17c678eSDavid Greenman 				}
1327a17c678eSDavid Greenman 				goto rcvloop;
1328a17c678eSDavid Greenman 			}
1329a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1330ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1331ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1332ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1333ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
13342e2b8238SJonathan Lemon 				fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1335a17c678eSDavid Greenman 			}
1336a17c678eSDavid Greenman 		}
1337a17c678eSDavid Greenman }
1338a17c678eSDavid Greenman 
1339dfe61cf1SDavid Greenman /*
1340dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1341dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1342dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1343dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1344dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1345dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1346dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1347dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1348dfe61cf1SDavid Greenman  * them again next time.
1349dfe61cf1SDavid Greenman  */
1350303b270bSEivind Eklund static void
1351f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1352a17c678eSDavid Greenman {
1353f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1354ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1355a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1356c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1357f7788e8eSJonathan Lemon 	int s;
1358a17c678eSDavid Greenman 
1359a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1360a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1361397f9dfeSDavid Greenman 	if (sp->rx_good) {
1362397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1363397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1364397f9dfeSDavid Greenman 	} else {
1365c8cc6fcaSDavid Greenman 		/*
1366c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1367c8cc6fcaSDavid Greenman 		 */
1368397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1369397f9dfeSDavid Greenman 	}
13703ba65732SDavid Greenman 	ifp->if_ierrors +=
13713ba65732SDavid Greenman 	    sp->rx_crc_errors +
13723ba65732SDavid Greenman 	    sp->rx_alignment_errors +
13733ba65732SDavid Greenman 	    sp->rx_rnr_errors +
13746e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1375a17c678eSDavid Greenman 	/*
1376f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1377f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1378f9be9005SDavid Greenman 	 */
1379f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1380f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1381f9be9005SDavid Greenman 		if (tx_threshold < 192)
1382f9be9005SDavid Greenman 			tx_threshold += 64;
1383f9be9005SDavid Greenman 	}
1384f7788e8eSJonathan Lemon 	s = splimp();
1385397f9dfeSDavid Greenman 	/*
1386c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1387c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1388c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1389c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1390c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1391c8cc6fcaSDavid Greenman 	 */
1392c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1393c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1394c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1395c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1396c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1397c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1398c8cc6fcaSDavid Greenman 		}
1399c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1400c8cc6fcaSDavid Greenman 	}
1401c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1402c8cc6fcaSDavid Greenman 	/*
1403397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1404397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1405397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1406397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1407397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1408397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1409397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1410397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1411397f9dfeSDavid Greenman 	 */
1412397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1413397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1414397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1415397f9dfeSDavid Greenman 	}
1416f9be9005SDavid Greenman 	/*
14173ba65732SDavid Greenman 	 * If there is no pending command, start another stats
14183ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1419a17c678eSDavid Greenman 	 */
1420397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1421a17c678eSDavid Greenman 		/*
1422397f9dfeSDavid Greenman 		 * Start another stats dump.
1423a17c678eSDavid Greenman 		 */
14242e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1425dfe61cf1SDavid Greenman 	} else {
1426dfe61cf1SDavid Greenman 		/*
1427dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1428dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
14293ba65732SDavid Greenman 		 * next timer event to update them.
1430dfe61cf1SDavid Greenman 		 */
1431dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1432f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1433dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
14343ba65732SDavid Greenman 
1435dfe61cf1SDavid Greenman 		sp->rx_good = 0;
14363ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
14373ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
14383ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
14393ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1440dfe61cf1SDavid Greenman 	}
1441f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1442f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
144374396a0aSJonathan Lemon 	splx(s);
1444a17c678eSDavid Greenman 	/*
1445a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1446a17c678eSDavid Greenman 	 */
1447f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1448a17c678eSDavid Greenman }
1449a17c678eSDavid Greenman 
1450a17c678eSDavid Greenman /*
1451a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1452a17c678eSDavid Greenman  * the interface.
1453a17c678eSDavid Greenman  */
1454a17c678eSDavid Greenman static void
1455f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1456a17c678eSDavid Greenman {
1457ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
14583ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
14593ba65732SDavid Greenman 	int i;
1460a17c678eSDavid Greenman 
14617dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
14627dced78aSDavid Greenman 	ifp->if_timer = 0;
14637dced78aSDavid Greenman 
1464e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1465e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
1466e4fc250cSLuigi Rizzo #endif
1467a17c678eSDavid Greenman 	/*
1468a17c678eSDavid Greenman 	 * Cancel stats updater.
1469a17c678eSDavid Greenman 	 */
1470f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
14713ba65732SDavid Greenman 
14723ba65732SDavid Greenman 	/*
147372a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
14743ba65732SDavid Greenman 	 */
147572a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
147609882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
147772a32a26SJonathan Lemon 	DELAY(50);
1478a17c678eSDavid Greenman 
14793ba65732SDavid Greenman 	/*
14803ba65732SDavid Greenman 	 * Release any xmit buffers.
14813ba65732SDavid Greenman 	 */
1482da91462dSDavid Greenman 	txp = sc->cbl_base;
1483da91462dSDavid Greenman 	if (txp != NULL) {
1484da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1485da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1486da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1487da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1488da91462dSDavid Greenman 			}
1489da91462dSDavid Greenman 		}
14903ba65732SDavid Greenman 	}
14913ba65732SDavid Greenman 	sc->tx_queued = 0;
14923ba65732SDavid Greenman 
14933ba65732SDavid Greenman 	/*
14943ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
14953ba65732SDavid Greenman 	 */
14963ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
14973ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
14983ba65732SDavid Greenman 	sc->rfa_headm = NULL;
14993ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
15003ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
15013ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
15023ba65732SDavid Greenman 			/*
15033ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
15043ba65732SDavid Greenman 			 * and we just freed all the buffers we need
15053ba65732SDavid Greenman 			 * above.
15063ba65732SDavid Greenman 			 */
15073ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
15083ba65732SDavid Greenman 		}
15093ba65732SDavid Greenman 	}
1510a17c678eSDavid Greenman }
1511a17c678eSDavid Greenman 
1512a17c678eSDavid Greenman /*
1513a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1514a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1515a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1516a17c678eSDavid Greenman  * card has wedged for some reason.
1517a17c678eSDavid Greenman  */
1518a17c678eSDavid Greenman static void
1519f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1520a17c678eSDavid Greenman {
1521ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1522ba8c6fd5SDavid Greenman 
1523f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
15244a5f1499SDavid Greenman 	ifp->if_oerrors++;
1525a17c678eSDavid Greenman 
1526ba8c6fd5SDavid Greenman 	fxp_init(sc);
1527a17c678eSDavid Greenman }
1528a17c678eSDavid Greenman 
1529a17c678eSDavid Greenman static void
1530f7788e8eSJonathan Lemon fxp_init(void *xsc)
1531a17c678eSDavid Greenman {
1532fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1533ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1534a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1535a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1536a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
153709882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
1538f7788e8eSJonathan Lemon 	int i, prm, s;
1539a17c678eSDavid Greenman 
1540f7788e8eSJonathan Lemon 	s = splimp();
1541a17c678eSDavid Greenman 	/*
15423ba65732SDavid Greenman 	 * Cancel any pending I/O
1543a17c678eSDavid Greenman 	 */
15443ba65732SDavid Greenman 	fxp_stop(sc);
1545a17c678eSDavid Greenman 
1546a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1547a17c678eSDavid Greenman 
1548a17c678eSDavid Greenman 	/*
1549a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1550a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1551a17c678eSDavid Greenman 	 */
1552ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
15532e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1554a17c678eSDavid Greenman 
1555ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
15562e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1557a17c678eSDavid Greenman 
1558a17c678eSDavid Greenman 	/*
1559a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1560a17c678eSDavid Greenman 	 */
1561ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1562ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
15632e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1564a17c678eSDavid Greenman 
1565a17c678eSDavid Greenman 	/*
156672a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
156772a32a26SJonathan Lemon 	 */
156872a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
156972a32a26SJonathan Lemon 		fxp_load_ucode(sc);
157072a32a26SJonathan Lemon 
157172a32a26SJonathan Lemon 	/*
157209882363SJonathan Lemon 	 * Initialize the multicast address list.
157309882363SJonathan Lemon 	 */
157409882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
157509882363SJonathan Lemon 		mcsp = sc->mcsp;
157609882363SJonathan Lemon 		mcsp->cb_status = 0;
157709882363SJonathan Lemon 		mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL;
157809882363SJonathan Lemon 		mcsp->link_addr = -1;
157909882363SJonathan Lemon 		/*
158009882363SJonathan Lemon 	 	 * Start the multicast setup command.
158109882363SJonathan Lemon 		 */
158209882363SJonathan Lemon 		fxp_scb_wait(sc);
158309882363SJonathan Lemon 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
158409882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
158509882363SJonathan Lemon 		/* ...and wait for it to complete. */
158609882363SJonathan Lemon 		fxp_dma_wait(&mcsp->cb_status, sc);
158709882363SJonathan Lemon 	}
158809882363SJonathan Lemon 
158909882363SJonathan Lemon 	/*
1590a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1591a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1592a17c678eSDavid Greenman 	 * later.
1593a17c678eSDavid Greenman 	 */
1594a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1595a17c678eSDavid Greenman 
1596a17c678eSDavid Greenman 	/*
1597a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1598a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1599a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1600a17c678eSDavid Greenman 	 */
1601d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1602d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1603397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1604a17c678eSDavid Greenman 
1605a17c678eSDavid Greenman 	cbp->cb_status =	0;
1606a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1607a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1608a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1609001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1610001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1611a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1612f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1613f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1614f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1615f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1616001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1617001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1618f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1619a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1620f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1621f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
16223114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1623f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1624f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1625f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
162672a32a26SJonathan Lemon 	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
1627a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1628f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1629f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1630f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1631f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1632f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1633f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1634f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1635f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1636f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1637f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1638a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1639a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1640a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1641a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1642a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1643a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1644a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1645a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1646f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1647f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1648f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1649f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1650f7788e8eSJonathan Lemon 
1651a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1652a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1653a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1654f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1655f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1656f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1657f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1658a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
16593ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1660a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1661f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1662a17c678eSDavid Greenman 
166372a32a26SJonathan Lemon 	if (sc->revision == FXP_REV_82557) {
16643bd07cfdSJonathan Lemon 		/*
16653bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
16663bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
16673bd07cfdSJonathan Lemon 		 */
16683bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
16693bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
16703bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
16713bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
16723bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
16733bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
16743bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
16753bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
16763bd07cfdSJonathan Lemon 	} else {
16773bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
16783bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
16793bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
16803bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
16813bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
16823bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
16833bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
16843bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
16853bd07cfdSJonathan Lemon 	}
16863bd07cfdSJonathan Lemon 
1687a17c678eSDavid Greenman 	/*
1688a17c678eSDavid Greenman 	 * Start the config command/DMA.
1689a17c678eSDavid Greenman 	 */
1690ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1691397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
16922e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1693a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
16947dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1695a17c678eSDavid Greenman 
1696a17c678eSDavid Greenman 	/*
1697a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1698a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1699a17c678eSDavid Greenman 	 */
1700a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1701a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1702a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1703a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1704d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1705d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1706a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1707a17c678eSDavid Greenman 
1708a17c678eSDavid Greenman 	/*
1709a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1710a17c678eSDavid Greenman 	 */
1711ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
17122e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1713a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
17147dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1715a17c678eSDavid Greenman 
1716a17c678eSDavid Greenman 	/*
1717a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1718a17c678eSDavid Greenman 	 */
1719a17c678eSDavid Greenman 
1720a17c678eSDavid Greenman 	txp = sc->cbl_base;
1721a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1722a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1723a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1724a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
17253bd07cfdSJonathan Lemon 		txp[i].link_addr =
17263bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
17273bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
17283bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
17293bd07cfdSJonathan Lemon 		else
1730a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1731a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1732a17c678eSDavid Greenman 	}
1733a17c678eSDavid Greenman 	/*
1734397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1735a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1736a17c678eSDavid Greenman 	 */
1737a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1738a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1739397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1740a17c678eSDavid Greenman 
1741ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
17422e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1743a17c678eSDavid Greenman 
1744a17c678eSDavid Greenman 	/*
1745a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1746a17c678eSDavid Greenman 	 */
1747ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1748ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1749ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
17502e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1751a17c678eSDavid Greenman 
1752dccee1a1SDavid Greenman 	/*
1753ba8c6fd5SDavid Greenman 	 * Set current media.
1754dccee1a1SDavid Greenman 	 */
1755f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1756f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1757dccee1a1SDavid Greenman 
1758a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1759a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1760e8c8b728SJonathan Lemon 
1761e8c8b728SJonathan Lemon 	/*
1762e8c8b728SJonathan Lemon 	 * Enable interrupts.
1763e8c8b728SJonathan Lemon 	 */
1764e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1765f7788e8eSJonathan Lemon 	splx(s);
1766a17c678eSDavid Greenman 
1767a17c678eSDavid Greenman 	/*
1768a17c678eSDavid Greenman 	 * Start stats updater.
1769a17c678eSDavid Greenman 	 */
1770f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1771f7788e8eSJonathan Lemon }
1772f7788e8eSJonathan Lemon 
1773f7788e8eSJonathan Lemon static int
1774f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1775f7788e8eSJonathan Lemon {
1776f7788e8eSJonathan Lemon 
1777f7788e8eSJonathan Lemon 	return (0);
1778a17c678eSDavid Greenman }
1779a17c678eSDavid Greenman 
1780303b270bSEivind Eklund static void
1781f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1782ba8c6fd5SDavid Greenman {
1783ba8c6fd5SDavid Greenman 
1784f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1785ba8c6fd5SDavid Greenman }
1786ba8c6fd5SDavid Greenman 
1787ba8c6fd5SDavid Greenman /*
1788ba8c6fd5SDavid Greenman  * Change media according to request.
1789ba8c6fd5SDavid Greenman  */
1790f7788e8eSJonathan Lemon static int
1791f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1792ba8c6fd5SDavid Greenman {
1793ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1794f7788e8eSJonathan Lemon 	struct mii_data *mii;
1795ba8c6fd5SDavid Greenman 
1796f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1797f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1798ba8c6fd5SDavid Greenman 	return (0);
1799ba8c6fd5SDavid Greenman }
1800ba8c6fd5SDavid Greenman 
1801ba8c6fd5SDavid Greenman /*
1802ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1803ba8c6fd5SDavid Greenman  */
1804f7788e8eSJonathan Lemon static void
1805f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1806ba8c6fd5SDavid Greenman {
1807ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1808f7788e8eSJonathan Lemon 	struct mii_data *mii;
1809ba8c6fd5SDavid Greenman 
1810f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1811f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1812f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1813f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
18142e2b8238SJonathan Lemon 
18152e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
18162e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
18172e2b8238SJonathan Lemon 	else
18182e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
1819ba8c6fd5SDavid Greenman }
1820ba8c6fd5SDavid Greenman 
1821a17c678eSDavid Greenman /*
1822a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1823a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1824a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1825dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1826a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1827a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1828a17c678eSDavid Greenman  */
1829a17c678eSDavid Greenman static int
1830f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1831a17c678eSDavid Greenman {
1832ba8c6fd5SDavid Greenman 	u_int32_t v;
1833a17c678eSDavid Greenman 	struct mbuf *m;
1834a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1835a17c678eSDavid Greenman 
1836a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1837a17c678eSDavid Greenman 	if (m != NULL) {
1838a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1839a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1840a17c678eSDavid Greenman 			m_freem(m);
1841eadd5e3aSDavid Greenman 			if (oldm == NULL)
1842eadd5e3aSDavid Greenman 				return 1;
1843a17c678eSDavid Greenman 			m = oldm;
1844eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1845a17c678eSDavid Greenman 		}
1846a17c678eSDavid Greenman 	} else {
1847eadd5e3aSDavid Greenman 		if (oldm == NULL)
1848a17c678eSDavid Greenman 			return 1;
1849eadd5e3aSDavid Greenman 		m = oldm;
1850eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1851eadd5e3aSDavid Greenman 	}
1852ba8c6fd5SDavid Greenman 
1853ba8c6fd5SDavid Greenman 	/*
1854ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1855ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1856ba8c6fd5SDavid Greenman 	 */
1857ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1858ba8c6fd5SDavid Greenman 
1859eadd5e3aSDavid Greenman 	/*
1860eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1861eadd5e3aSDavid Greenman 	 * data start past it.
1862eadd5e3aSDavid Greenman 	 */
1863a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1864eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
18654fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1866eadd5e3aSDavid Greenman 
1867ba8c6fd5SDavid Greenman 	/*
1868ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1869ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1870ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1871ba8c6fd5SDavid Greenman 	 */
18724fc1dda9SAndrew Gallatin 
1873a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1874a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1875a17c678eSDavid Greenman 	rfa->actual_size = 0;
1876ba8c6fd5SDavid Greenman 
1877ba8c6fd5SDavid Greenman 	v = -1;
18784fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
18794fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1880ba8c6fd5SDavid Greenman 
1881dfe61cf1SDavid Greenman 	/*
1882dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1883dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1884dfe61cf1SDavid Greenman 	 */
1885a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1886ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1887ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1888a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1889ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
18904fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1891aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1892a17c678eSDavid Greenman 	} else {
1893a17c678eSDavid Greenman 		sc->rfa_headm = m;
1894a17c678eSDavid Greenman 	}
1895a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1896a17c678eSDavid Greenman 
1897dfe61cf1SDavid Greenman 	return (m == oldm);
1898a17c678eSDavid Greenman }
1899a17c678eSDavid Greenman 
19006ebc3153SDavid Greenman static volatile int
1901f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1902dccee1a1SDavid Greenman {
1903f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1904dccee1a1SDavid Greenman 	int count = 10000;
19056ebc3153SDavid Greenman 	int value;
1906dccee1a1SDavid Greenman 
1907ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1908ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1909dccee1a1SDavid Greenman 
1910ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1911ba8c6fd5SDavid Greenman 	    && count--)
19126ebc3153SDavid Greenman 		DELAY(10);
1913dccee1a1SDavid Greenman 
1914dccee1a1SDavid Greenman 	if (count <= 0)
1915f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1916dccee1a1SDavid Greenman 
19176ebc3153SDavid Greenman 	return (value & 0xffff);
1918dccee1a1SDavid Greenman }
1919dccee1a1SDavid Greenman 
1920dccee1a1SDavid Greenman static void
1921f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1922dccee1a1SDavid Greenman {
1923f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1924dccee1a1SDavid Greenman 	int count = 10000;
1925dccee1a1SDavid Greenman 
1926ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1927ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1928ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1929dccee1a1SDavid Greenman 
1930ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1931ba8c6fd5SDavid Greenman 	    count--)
19326ebc3153SDavid Greenman 		DELAY(10);
1933dccee1a1SDavid Greenman 
1934dccee1a1SDavid Greenman 	if (count <= 0)
1935f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1936dccee1a1SDavid Greenman }
1937dccee1a1SDavid Greenman 
1938dccee1a1SDavid Greenman static int
1939f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1940a17c678eSDavid Greenman {
19419b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1942a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1943f7788e8eSJonathan Lemon 	struct mii_data *mii;
1944f7788e8eSJonathan Lemon 	int s, error = 0;
1945a17c678eSDavid Greenman 
1946f7788e8eSJonathan Lemon 	s = splimp();
1947a17c678eSDavid Greenman 
1948a17c678eSDavid Greenman 	switch (command) {
1949a17c678eSDavid Greenman 	case SIOCSIFADDR:
1950a17c678eSDavid Greenman 	case SIOCGIFADDR:
1951fb583156SDavid Greenman 	case SIOCSIFMTU:
1952fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1953a17c678eSDavid Greenman 		break;
1954a17c678eSDavid Greenman 
1955a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1956f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1957f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1958f7788e8eSJonathan Lemon 		else
1959f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1960a17c678eSDavid Greenman 
1961a17c678eSDavid Greenman 		/*
1962a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1963a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1964a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1965a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1966a17c678eSDavid Greenman 		 */
1967a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1968fb583156SDavid Greenman 			fxp_init(sc);
1969a17c678eSDavid Greenman 		} else {
1970a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
19714a5f1499SDavid Greenman 				fxp_stop(sc);
1972a17c678eSDavid Greenman 		}
1973a17c678eSDavid Greenman 		break;
1974a17c678eSDavid Greenman 
1975a17c678eSDavid Greenman 	case SIOCADDMULTI:
1976a17c678eSDavid Greenman 	case SIOCDELMULTI:
1977f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1978f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1979f7788e8eSJonathan Lemon 		else
1980f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1981a17c678eSDavid Greenman 		/*
1982a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1983a17c678eSDavid Greenman 		 * accordingly.
1984a17c678eSDavid Greenman 		 */
1985f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1986397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1987397f9dfeSDavid Greenman 		/*
1988f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1989397f9dfeSDavid Greenman 		 * again rather than else {}.
1990397f9dfeSDavid Greenman 		 */
1991f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1992fb583156SDavid Greenman 			fxp_init(sc);
1993a17c678eSDavid Greenman 		error = 0;
1994ba8c6fd5SDavid Greenman 		break;
1995ba8c6fd5SDavid Greenman 
1996ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1997ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1998f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
1999f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2000f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2001f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2002f7788e8eSJonathan Lemon 		} else {
2003ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2004f7788e8eSJonathan Lemon 		}
2005a17c678eSDavid Greenman 		break;
2006a17c678eSDavid Greenman 
2007a17c678eSDavid Greenman 	default:
2008a17c678eSDavid Greenman 		error = EINVAL;
2009a17c678eSDavid Greenman 	}
2010f7788e8eSJonathan Lemon 	splx(s);
2011a17c678eSDavid Greenman 	return (error);
2012a17c678eSDavid Greenman }
2013397f9dfeSDavid Greenman 
2014397f9dfeSDavid Greenman /*
201509882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
201609882363SJonathan Lemon  */
201709882363SJonathan Lemon static int
201809882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
201909882363SJonathan Lemon {
202009882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
202109882363SJonathan Lemon 	struct ifnet *ifp = &sc->sc_if;
202209882363SJonathan Lemon 	struct ifmultiaddr *ifma;
202309882363SJonathan Lemon 	int nmcasts;
202409882363SJonathan Lemon 
202509882363SJonathan Lemon 	nmcasts = 0;
202609882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
202709882363SJonathan Lemon #if __FreeBSD_version < 500000
202809882363SJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
202909882363SJonathan Lemon #else
203009882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
203109882363SJonathan Lemon #endif
203209882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
203309882363SJonathan Lemon 				continue;
203409882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
203509882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
203609882363SJonathan Lemon 				nmcasts = 0;
203709882363SJonathan Lemon 				break;
203809882363SJonathan Lemon 			}
203909882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
204009882363SJonathan Lemon 			    (void *)(uintptr_t)(volatile void *)
204109882363SJonathan Lemon 				&sc->mcsp->mc_addr[nmcasts][0], 6);
204209882363SJonathan Lemon 			nmcasts++;
204309882363SJonathan Lemon 		}
204409882363SJonathan Lemon 	}
204509882363SJonathan Lemon 	mcsp->mc_cnt = nmcasts * 6;
204609882363SJonathan Lemon 	return (nmcasts);
204709882363SJonathan Lemon }
204809882363SJonathan Lemon 
204909882363SJonathan Lemon /*
2050397f9dfeSDavid Greenman  * Program the multicast filter.
2051397f9dfeSDavid Greenman  *
2052397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2053397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
20543114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2055397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2056dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2057397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2058397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2059397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2060397f9dfeSDavid Greenman  *
2061397f9dfeSDavid Greenman  * This function must be called at splimp.
2062397f9dfeSDavid Greenman  */
2063397f9dfeSDavid Greenman static void
2064f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2065397f9dfeSDavid Greenman {
2066397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2067397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
20687dced78aSDavid Greenman 	int count;
2069397f9dfeSDavid Greenman 
20703114fdb4SDavid Greenman 	/*
20713114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
20723114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
20733114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
20743114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
20753114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
20763114fdb4SDavid Greenman 	 */
2077397f9dfeSDavid Greenman 	if (sc->tx_queued) {
20783114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
20793114fdb4SDavid Greenman 
20803114fdb4SDavid Greenman 		/*
20813114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
20823114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
20833114fdb4SDavid Greenman 		 */
20843114fdb4SDavid Greenman 		if (sc->need_mcsetup)
20853114fdb4SDavid Greenman 			return;
2086397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
20873114fdb4SDavid Greenman 
20883114fdb4SDavid Greenman 		/*
208972a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
209072a32a26SJonathan Lemon 		 * when all TX commands have been processed.
20913114fdb4SDavid Greenman 		 */
20923114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
20933114fdb4SDavid Greenman 		txp->mb_head = NULL;
20943114fdb4SDavid Greenman 		txp->cb_status = 0;
2095e8c8b728SJonathan Lemon 		txp->cb_command = FXP_CB_COMMAND_NOP |
2096e8c8b728SJonathan Lemon 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
20973114fdb4SDavid Greenman 		/*
20983114fdb4SDavid Greenman 		 * Advance the end of list forward.
20993114fdb4SDavid Greenman 		 */
21003114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
21013114fdb4SDavid Greenman 		sc->cbl_last = txp;
21023114fdb4SDavid Greenman 		sc->tx_queued++;
21033114fdb4SDavid Greenman 		/*
21043114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
21053114fdb4SDavid Greenman 		 */
21063114fdb4SDavid Greenman 		fxp_scb_wait(sc);
21072e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
21083114fdb4SDavid Greenman 		/*
21093114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
21103114fdb4SDavid Greenman 		 * card again.
21113114fdb4SDavid Greenman 		 */
21123114fdb4SDavid Greenman 		ifp->if_timer = 5;
21133114fdb4SDavid Greenman 
2114397f9dfeSDavid Greenman 		return;
2115397f9dfeSDavid Greenman 	}
2116397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2117397f9dfeSDavid Greenman 
2118397f9dfeSDavid Greenman 	/*
2119397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2120397f9dfeSDavid Greenman 	 */
2121397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
2122397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
2123397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
2124e8c8b728SJonathan Lemon 	mcsp->cb_command = FXP_CB_COMMAND_MCAS |
2125e8c8b728SJonathan Lemon 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
2126397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
212709882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2128397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2129397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2130397f9dfeSDavid Greenman 
2131397f9dfeSDavid Greenman 	/*
2132397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2133397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2134397f9dfeSDavid Greenman 	 */
21357dced78aSDavid Greenman 	count = 100;
2136397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
21377dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
21387dced78aSDavid Greenman 		DELAY(10);
21397dced78aSDavid Greenman 	if (count == 0) {
2140f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
21417dced78aSDavid Greenman 		return;
21427dced78aSDavid Greenman 	}
2143397f9dfeSDavid Greenman 
2144397f9dfeSDavid Greenman 	/*
2145397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2146397f9dfeSDavid Greenman 	 */
2147397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2148397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
21492e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2150397f9dfeSDavid Greenman 
21513114fdb4SDavid Greenman 	ifp->if_timer = 2;
2152397f9dfeSDavid Greenman 	return;
2153397f9dfeSDavid Greenman }
215472a32a26SJonathan Lemon 
215572a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
215672a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
215772a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
215872a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
215972a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
216072a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
216172a32a26SJonathan Lemon 
216272a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
216372a32a26SJonathan Lemon 
216472a32a26SJonathan Lemon struct ucode {
216572a32a26SJonathan Lemon 	u_int32_t	revision;
216672a32a26SJonathan Lemon 	u_int32_t	*ucode;
216772a32a26SJonathan Lemon 	int		length;
216872a32a26SJonathan Lemon 	u_short		int_delay_offset;
216972a32a26SJonathan Lemon 	u_short		bundle_max_offset;
217072a32a26SJonathan Lemon } ucode_table[] = {
217172a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
217272a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
217372a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
217472a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
217572a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
217672a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
217772a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
217872a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
217972a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
218072a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
218172a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
218272a32a26SJonathan Lemon };
218372a32a26SJonathan Lemon 
218472a32a26SJonathan Lemon static void
218572a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
218672a32a26SJonathan Lemon {
218772a32a26SJonathan Lemon 	struct ucode *uc;
218872a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
218972a32a26SJonathan Lemon 
219072a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
219172a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
219272a32a26SJonathan Lemon 			break;
219372a32a26SJonathan Lemon 	if (uc->ucode == NULL)
219472a32a26SJonathan Lemon 		return;
219572a32a26SJonathan Lemon 	cbp = (struct fxp_cb_ucode *)sc->cbl_base;
219672a32a26SJonathan Lemon 	cbp->cb_status = 0;
219772a32a26SJonathan Lemon 	cbp->cb_command = FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL;
219872a32a26SJonathan Lemon 	cbp->link_addr = -1;    	/* (no) next command */
219972a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
220072a32a26SJonathan Lemon 	if (uc->int_delay_offset)
220172a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->int_delay_offset] =
2202208b417fSJonathan Lemon 		    sc->tunable_int_delay + sc->tunable_int_delay / 2;
220372a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
220472a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->bundle_max_offset] =
220572a32a26SJonathan Lemon 		    sc->tunable_bundle_max;
220672a32a26SJonathan Lemon 	/*
220772a32a26SJonathan Lemon 	 * Download the ucode to the chip.
220872a32a26SJonathan Lemon 	 */
220972a32a26SJonathan Lemon 	fxp_scb_wait(sc);
221072a32a26SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
221172a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
221272a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
221372a32a26SJonathan Lemon 	fxp_dma_wait(&cbp->cb_status, sc);
221472a32a26SJonathan Lemon 	device_printf(sc->dev,
221572a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
221672a32a26SJonathan Lemon 	    sc->tunable_int_delay,
221772a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
221872a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
221972a32a26SJonathan Lemon }
222072a32a26SJonathan Lemon 
222172a32a26SJonathan Lemon static int
222272a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
222372a32a26SJonathan Lemon {
222472a32a26SJonathan Lemon 	int error, value;
222572a32a26SJonathan Lemon 
222672a32a26SJonathan Lemon 	value = *(int *)arg1;
222772a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
222872a32a26SJonathan Lemon 	if (error || !req->newptr)
222972a32a26SJonathan Lemon 		return (error);
223072a32a26SJonathan Lemon 	if (value < low || value > high)
223172a32a26SJonathan Lemon 		return (EINVAL);
223272a32a26SJonathan Lemon 	*(int *)arg1 = value;
223372a32a26SJonathan Lemon 	return (0);
223472a32a26SJonathan Lemon }
223572a32a26SJonathan Lemon 
223672a32a26SJonathan Lemon /*
223772a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
223872a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
223972a32a26SJonathan Lemon  */
224072a32a26SJonathan Lemon static int
224172a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
224272a32a26SJonathan Lemon {
224372a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
224472a32a26SJonathan Lemon }
224572a32a26SJonathan Lemon 
224672a32a26SJonathan Lemon static int
224772a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
224872a32a26SJonathan Lemon {
224972a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
225072a32a26SJonathan Lemon }
2251