xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 72a32a26aa76fde45219333af91aa8bafae70dc0)
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);
183f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
184f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
185f7788e8eSJonathan Lemon 			    int autosize);
18600c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
18700c4116bSJonathan Lemon 			    u_int16_t data);
188f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
189f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
190f7788e8eSJonathan Lemon 			    int offset, int words);
19100c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
19200c4116bSJonathan Lemon 			    int offset, int words);
193f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
194f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
195f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
196f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
197f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
198f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
199f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
200f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
201f7788e8eSJonathan Lemon 			    int value);
20272a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
20372a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
20472a32a26SJonathan Lemon 			    int low, int high);
20572a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
20672a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
207f7788e8eSJonathan Lemon static __inline void	fxp_lwcopy(volatile u_int32_t *src,
208f7788e8eSJonathan Lemon 			    volatile u_int32_t *dst);
209f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2102e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
211f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
212f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
213f7788e8eSJonathan Lemon 
214f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
215f7788e8eSJonathan Lemon 	/* Device interface */
216f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
217f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
218f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
219f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
220f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
221f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
222f7788e8eSJonathan Lemon 
223f7788e8eSJonathan Lemon 	/* MII interface */
224f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
225f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
226f7788e8eSJonathan Lemon 
227f7788e8eSJonathan Lemon 	{ 0, 0 }
228f7788e8eSJonathan Lemon };
229f7788e8eSJonathan Lemon 
230f7788e8eSJonathan Lemon static driver_t fxp_driver = {
231f7788e8eSJonathan Lemon 	"fxp",
232f7788e8eSJonathan Lemon 	fxp_methods,
233f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
234f7788e8eSJonathan Lemon };
235f7788e8eSJonathan Lemon 
236f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
237f7788e8eSJonathan Lemon 
238f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
239f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
240f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
241f7788e8eSJonathan Lemon 
242f7788e8eSJonathan Lemon /*
243ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
244ba8c6fd5SDavid Greenman  */
245ba8c6fd5SDavid Greenman static __inline void
246f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
247ba8c6fd5SDavid Greenman {
248aed53495SDavid Greenman #ifdef __i386__
249aed53495SDavid Greenman 	*dst = *src;
250aed53495SDavid Greenman #else
251fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
252fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
253ba8c6fd5SDavid Greenman 
254ba8c6fd5SDavid Greenman 	b[0] = a[0];
255ba8c6fd5SDavid Greenman 	b[1] = a[1];
256aed53495SDavid Greenman #endif
257ba8c6fd5SDavid Greenman }
258a17c678eSDavid Greenman 
259a17c678eSDavid Greenman /*
260dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
261dfe61cf1SDavid Greenman  * completed).
262dfe61cf1SDavid Greenman  */
263c1087c13SBruce Evans static __inline void
264f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
265a17c678eSDavid Greenman {
266a17c678eSDavid Greenman 	int i = 10000;
267a17c678eSDavid Greenman 
2687dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2697dced78aSDavid Greenman 		DELAY(2);
2707dced78aSDavid Greenman 	if (i == 0)
27100c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
272e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
273e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
274e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
275e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2767dced78aSDavid Greenman }
2777dced78aSDavid Greenman 
2787dced78aSDavid Greenman static __inline void
2792e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2802e2b8238SJonathan Lemon {
2812e2b8238SJonathan Lemon 
2822e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2832e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2842e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2852e2b8238SJonathan Lemon 	}
2862e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2872e2b8238SJonathan Lemon }
2882e2b8238SJonathan Lemon 
2892e2b8238SJonathan Lemon static __inline void
290f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
2917dced78aSDavid Greenman {
2927dced78aSDavid Greenman 	int i = 10000;
2937dced78aSDavid Greenman 
2947dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
2957dced78aSDavid Greenman 		DELAY(2);
2967dced78aSDavid Greenman 	if (i == 0)
297f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
298a17c678eSDavid Greenman }
299a17c678eSDavid Greenman 
300dfe61cf1SDavid Greenman /*
301dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
302dfe61cf1SDavid Greenman  */
3036182fdbdSPeter Wemm static int
3046182fdbdSPeter Wemm fxp_probe(device_t dev)
305a17c678eSDavid Greenman {
306f7788e8eSJonathan Lemon 	u_int16_t devid;
307f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
308f7788e8eSJonathan Lemon 
30955ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
310f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
311f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
312f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
313f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
314f7788e8eSJonathan Lemon 				return (0);
31555ce7b51SDavid Greenman 			}
316dd68ef16SPeter Wemm 		}
317f7788e8eSJonathan Lemon 	}
318f7788e8eSJonathan Lemon 	return (ENXIO);
3196182fdbdSPeter Wemm }
3206182fdbdSPeter Wemm 
32148e417ebSJonathan Lemon static void
32248e417ebSJonathan Lemon fxp_powerstate_d0(device_t dev)
32348e417ebSJonathan Lemon {
32448e417ebSJonathan Lemon #if __FreeBSD_version >= 430002
32548e417ebSJonathan Lemon 	u_int32_t iobase, membase, irq;
32648e417ebSJonathan Lemon 
32748e417ebSJonathan Lemon 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
32848e417ebSJonathan Lemon 		/* Save important PCI config data. */
32948e417ebSJonathan Lemon 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
33048e417ebSJonathan Lemon 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
33148e417ebSJonathan Lemon 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
33248e417ebSJonathan Lemon 
33348e417ebSJonathan Lemon 		/* Reset the power state. */
33448e417ebSJonathan Lemon 		device_printf(dev, "chip is in D%d power mode "
33548e417ebSJonathan Lemon 		    "-- setting to D0\n", pci_get_powerstate(dev));
33648e417ebSJonathan Lemon 
33748e417ebSJonathan Lemon 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
33848e417ebSJonathan Lemon 
33948e417ebSJonathan Lemon 		/* Restore PCI config data. */
34048e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
34148e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
34248e417ebSJonathan Lemon 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
34348e417ebSJonathan Lemon 	}
34448e417ebSJonathan Lemon #endif
34548e417ebSJonathan Lemon }
34648e417ebSJonathan Lemon 
3476182fdbdSPeter Wemm static int
3486182fdbdSPeter Wemm fxp_attach(device_t dev)
349a17c678eSDavid Greenman {
3506182fdbdSPeter Wemm 	int error = 0;
3516182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
352ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
3539fa6ccfbSMatt Jacob 	u_int32_t val;
354f7788e8eSJonathan Lemon 	u_int16_t data;
355f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
356f7788e8eSJonathan Lemon 	int s;
357a17c678eSDavid Greenman 
358f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
359f7788e8eSJonathan Lemon 	sc->dev = dev;
3606c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
361f7788e8eSJonathan Lemon 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
362a17c678eSDavid Greenman 
363f7788e8eSJonathan Lemon 	s = splimp();
364a17c678eSDavid Greenman 
365dfe61cf1SDavid Greenman 	/*
3669fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3679fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
368df373873SWes Peters 	 */
3696182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
370df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3716182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3729fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
373df373873SWes Peters 
37448e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
3758d799694SBill Paul 
376df373873SWes Peters 	/*
3779fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3789fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3799fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
380dfe61cf1SDavid Greenman 	 */
3819fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3829fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3832a05a4ebSMatt Jacob 	prefer_iomap = 0;
3842a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3852a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3869fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3879fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
3889fa6ccfbSMatt Jacob 	}
3899fa6ccfbSMatt Jacob 
3909fa6ccfbSMatt Jacob 	if (val & m1) {
3919fa6ccfbSMatt Jacob 		sc->rtp =
3929fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3939fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3949fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3956182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
3969fa6ccfbSMatt Jacob 	}
3979fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
3989fa6ccfbSMatt Jacob 		sc->rtp =
3999fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4009fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4019fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4029fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
4039fa6ccfbSMatt Jacob 	}
4049fa6ccfbSMatt Jacob 
4056182fdbdSPeter Wemm 	if (!sc->mem) {
4069fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
4076182fdbdSPeter Wemm 		error = ENXIO;
408a17c678eSDavid Greenman 		goto fail;
409a17c678eSDavid Greenman         }
4109fa6ccfbSMatt Jacob 	if (bootverbose) {
4119fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4129fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4139fa6ccfbSMatt Jacob 	}
4144fc1dda9SAndrew Gallatin 
4154fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4164fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
417a17c678eSDavid Greenman 
418a17c678eSDavid Greenman 	/*
419dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
420dfe61cf1SDavid Greenman 	 */
4216182fdbdSPeter Wemm 	rid = 0;
4226182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4236182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4246182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4256182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4266182fdbdSPeter Wemm 		error = ENXIO;
4276182fdbdSPeter Wemm 		goto fail;
4286182fdbdSPeter Wemm 	}
4296182fdbdSPeter Wemm 
430566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
431566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
4326182fdbdSPeter Wemm 	if (error) {
4336182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
434a17c678eSDavid Greenman 		goto fail;
435a17c678eSDavid Greenman 	}
436a17c678eSDavid Greenman 
437f7788e8eSJonathan Lemon 	/*
438f7788e8eSJonathan Lemon 	 * Reset to a stable state.
439f7788e8eSJonathan Lemon 	 */
440f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
441f7788e8eSJonathan Lemon 	DELAY(10);
442f7788e8eSJonathan Lemon 
443f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
444f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
445f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
446f7788e8eSJonathan Lemon 		goto failmem;
447f7788e8eSJonathan Lemon 
448f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
449f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
450f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
451f7788e8eSJonathan Lemon 		goto failmem;
452f7788e8eSJonathan Lemon 
453f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
454f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
455f7788e8eSJonathan Lemon 		goto failmem;
456f7788e8eSJonathan Lemon 
457f7788e8eSJonathan Lemon 	/*
458f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
459f7788e8eSJonathan Lemon 	 */
460f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
461f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
462f7788e8eSJonathan Lemon 			goto failmem;
463f7788e8eSJonathan Lemon 		}
464f7788e8eSJonathan Lemon 	}
465f7788e8eSJonathan Lemon 
466f7788e8eSJonathan Lemon 	/*
467f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
468f7788e8eSJonathan Lemon 	 */
469f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
470f7788e8eSJonathan Lemon 
471f7788e8eSJonathan Lemon 	/*
4723bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
473f7788e8eSJonathan Lemon 	 */
474f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
475f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
476f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
477dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
478f7788e8eSJonathan Lemon 
479f7788e8eSJonathan Lemon 	/*
48072a32a26SJonathan Lemon 	 * Create the sysctl tree
48172a32a26SJonathan Lemon 	 */
48272a32a26SJonathan Lemon 	sysctl_ctx_init(&sc->sysctl_ctx);
48372a32a26SJonathan Lemon 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
48472a32a26SJonathan Lemon 	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
48572a32a26SJonathan Lemon 	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
48672a32a26SJonathan Lemon 	if (sc->sysctl_tree == NULL)
48772a32a26SJonathan Lemon 		goto fail;
48872a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
48972a32a26SJonathan Lemon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
49072a32a26SJonathan Lemon 	    &sc->tunable_int_delay, 0, &sysctl_hw_fxp_int_delay, "I",
49172a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
49272a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
49372a32a26SJonathan Lemon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
49472a32a26SJonathan Lemon 	    &sc->tunable_bundle_max, 0, &sysctl_hw_fxp_bundle_max, "I",
49572a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
49672a32a26SJonathan Lemon 
49772a32a26SJonathan Lemon 	/*
49872a32a26SJonathan Lemon 	 * Pull in device tunables.
49972a32a26SJonathan Lemon 	 */
50072a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
50172a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
50272a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50372a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
50472a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50572a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
50672a32a26SJonathan Lemon 
50772a32a26SJonathan Lemon 	/*
50872a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
5093bd07cfdSJonathan Lemon 	 */
5103bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
5113bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
51272a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
51372a32a26SJonathan Lemon 	else
51472a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
5153bd07cfdSJonathan Lemon 
5163bd07cfdSJonathan Lemon 	/*
5172e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
51800c4116bSJonathan Lemon 	 *
51972a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
52072a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
52172a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
52200c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
52300c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
52400c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
52500c4116bSJonathan Lemon 	 *
52600c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5272e2b8238SJonathan Lemon 	 */
5282e2b8238SJonathan Lemon 	i = pci_get_device(dev);
52972a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
53072a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
53100c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
53200c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
53300c4116bSJonathan Lemon 			u_int16_t cksum;
53400c4116bSJonathan Lemon 			int i;
53500c4116bSJonathan Lemon 
53600c4116bSJonathan Lemon 			device_printf(dev,
53700c4116bSJonathan Lemon 		    "*** DISABLING DYNAMIC STANDBY MODE IN EEPROM ***\n");
53800c4116bSJonathan Lemon 			data &= ~0x02;
53900c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
54000c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
54100c4116bSJonathan Lemon 			cksum = 0;
54200c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
54300c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
54400c4116bSJonathan Lemon 				cksum += data;
54500c4116bSJonathan Lemon 			}
54600c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
54700c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
54800c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
54900c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
55000c4116bSJonathan Lemon 			device_printf(dev,
55100c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
55200c4116bSJonathan Lemon 			    i, data, cksum);
55300c4116bSJonathan Lemon 			/*
55400c4116bSJonathan Lemon 			 * We need to do a full PCI reset here.  A software
55500c4116bSJonathan Lemon 			 * reset to the port doesn't cut it, but let's try
55600c4116bSJonathan Lemon 			 * anyway.
55700c4116bSJonathan Lemon 			 */
55800c4116bSJonathan Lemon 			CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
55900c4116bSJonathan Lemon 			DELAY(50);
56000c4116bSJonathan Lemon 			device_printf(dev,
56100c4116bSJonathan Lemon 	    "*** PLEASE REBOOT THE SYSTEM NOW FOR CORRECT OPERATION ***\n");
56200c4116bSJonathan Lemon #if 1
56300c4116bSJonathan Lemon 			/*
56400c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
56500c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
56600c4116bSJonathan Lemon 			 */
5672e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
56800c4116bSJonathan Lemon #endif
56900c4116bSJonathan Lemon 		}
57000c4116bSJonathan Lemon 	}
5712e2b8238SJonathan Lemon 
5722e2b8238SJonathan Lemon 	/*
5733bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5743bd07cfdSJonathan Lemon 	 */
57572a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5763bd07cfdSJonathan Lemon 		/*
57774396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
57874396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
57974396a0aSJonathan Lemon 		 * the board to turn on MWI.
5803bd07cfdSJonathan Lemon 		 */
58174396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
58274396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5833bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5843bd07cfdSJonathan Lemon 
5853bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5863bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
587920b58e8SBrooks Davis 
588e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
589e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
5903bd07cfdSJonathan Lemon 	}
5913bd07cfdSJonathan Lemon 
5923bd07cfdSJonathan Lemon 	/*
593f7788e8eSJonathan Lemon 	 * Read MAC address.
594f7788e8eSJonathan Lemon 	 */
595f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
596f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
597f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
598f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
599f7788e8eSJonathan Lemon 	if (bootverbose) {
6002e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
601f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
6022e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
6032e2b8238SJonathan Lemon 		    pci_get_revid(dev));
60472a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
60572a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
60672a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
607f7788e8eSJonathan Lemon 	}
608f7788e8eSJonathan Lemon 
609f7788e8eSJonathan Lemon 	/*
610f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
611f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
612f7788e8eSJonathan Lemon 	 *
613f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
614f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
615f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
616f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
617f7788e8eSJonathan Lemon 	 */
618f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
619f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
620f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
621f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
622f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
623f7788e8eSJonathan Lemon 	} else {
624f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
625f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
626f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
6276182fdbdSPeter Wemm 			error = ENXIO;
628ba8c6fd5SDavid Greenman 			goto fail;
629a17c678eSDavid Greenman 		}
630f7788e8eSJonathan Lemon 	}
631dccee1a1SDavid Greenman 
632a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
6336182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
634a17c678eSDavid Greenman 	ifp->if_name = "fxp";
635a17c678eSDavid Greenman 	ifp->if_output = ether_output;
636a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
637fb583156SDavid Greenman 	ifp->if_init = fxp_init;
638ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
639ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
640ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
641ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
642ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
643a17c678eSDavid Greenman 
644dfe61cf1SDavid Greenman 	/*
645dfe61cf1SDavid Greenman 	 * Attach the interface.
646dfe61cf1SDavid Greenman 	 */
64721b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
648f7788e8eSJonathan Lemon 
649e8c8b728SJonathan Lemon 	/*
650e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
651e8c8b728SJonathan Lemon 	 */
652e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
653e8c8b728SJonathan Lemon 
654483b9871SDavid Greenman 	/*
6553114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
6563114fdb4SDavid Greenman 	 * TX descriptors.
657483b9871SDavid Greenman 	 */
6583114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
6594a684684SDavid Greenman 
660f7788e8eSJonathan Lemon 	splx(s);
661f7788e8eSJonathan Lemon 	return (0);
662a17c678eSDavid Greenman 
663f7788e8eSJonathan Lemon failmem:
664f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
665f7788e8eSJonathan Lemon 	error = ENOMEM;
666a17c678eSDavid Greenman fail:
667f7788e8eSJonathan Lemon 	splx(s);
668f7788e8eSJonathan Lemon 	fxp_release(sc);
669f7788e8eSJonathan Lemon 	return (error);
670f7788e8eSJonathan Lemon }
671f7788e8eSJonathan Lemon 
672f7788e8eSJonathan Lemon /*
673f7788e8eSJonathan Lemon  * release all resources
674f7788e8eSJonathan Lemon  */
675f7788e8eSJonathan Lemon static void
676f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
677f7788e8eSJonathan Lemon {
678f7788e8eSJonathan Lemon 
679f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
6803bd07cfdSJonathan Lemon 	if (sc->miibus)
681f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
682f7788e8eSJonathan Lemon 
683f7788e8eSJonathan Lemon 	if (sc->cbl_base)
684f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
685f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
686f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
687f7788e8eSJonathan Lemon 	if (sc->mcsp)
688f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
689f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
690f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
691f7788e8eSJonathan Lemon 
692f7788e8eSJonathan Lemon 	if (sc->ih)
693f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
694f7788e8eSJonathan Lemon 	if (sc->irq)
695f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
696f7788e8eSJonathan Lemon 	if (sc->mem)
697f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
69872a32a26SJonathan Lemon 
69972a32a26SJonathan Lemon         sysctl_ctx_free(&sc->sysctl_ctx);
70072a32a26SJonathan Lemon 
7010f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
7026182fdbdSPeter Wemm }
7036182fdbdSPeter Wemm 
7046182fdbdSPeter Wemm /*
7056182fdbdSPeter Wemm  * Detach interface.
7066182fdbdSPeter Wemm  */
7076182fdbdSPeter Wemm static int
7086182fdbdSPeter Wemm fxp_detach(device_t dev)
7096182fdbdSPeter Wemm {
7106182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
711f7788e8eSJonathan Lemon 	int s;
7126182fdbdSPeter Wemm 
7132e2b8238SJonathan Lemon 	/* disable interrupts */
7142e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
7152e2b8238SJonathan Lemon 
716f7788e8eSJonathan Lemon 	s = splimp();
7176182fdbdSPeter Wemm 
7186182fdbdSPeter Wemm 	/*
7196182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
7206182fdbdSPeter Wemm 	 */
7216182fdbdSPeter Wemm 	fxp_stop(sc);
7226182fdbdSPeter Wemm 
7236182fdbdSPeter Wemm 	/*
724f7788e8eSJonathan Lemon 	 * Close down routes etc.
7256182fdbdSPeter Wemm 	 */
726f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
7276182fdbdSPeter Wemm 
7286182fdbdSPeter Wemm 	/*
7296182fdbdSPeter Wemm 	 * Free all media structures.
7306182fdbdSPeter Wemm 	 */
7316182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
7326182fdbdSPeter Wemm 
733f7788e8eSJonathan Lemon 	splx(s);
7346182fdbdSPeter Wemm 
735f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
736f7788e8eSJonathan Lemon 	fxp_release(sc);
7376182fdbdSPeter Wemm 
738f7788e8eSJonathan Lemon 	return (0);
739a17c678eSDavid Greenman }
740a17c678eSDavid Greenman 
741a17c678eSDavid Greenman /*
7424a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
743a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
744a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
745a17c678eSDavid Greenman  */
7466182fdbdSPeter Wemm static int
7476182fdbdSPeter Wemm fxp_shutdown(device_t dev)
748a17c678eSDavid Greenman {
7496182fdbdSPeter Wemm 	/*
7506182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
7516182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
7526182fdbdSPeter Wemm 	 * reboot before the driver initializes.
7536182fdbdSPeter Wemm 	 */
7546182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
755f7788e8eSJonathan Lemon 	return (0);
756a17c678eSDavid Greenman }
757a17c678eSDavid Greenman 
7587dced78aSDavid Greenman /*
7597dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
7607dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
7617dced78aSDavid Greenman  * resume.
7627dced78aSDavid Greenman  */
7637dced78aSDavid Greenman static int
7647dced78aSDavid Greenman fxp_suspend(device_t dev)
7657dced78aSDavid Greenman {
7667dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
767f7788e8eSJonathan Lemon 	int i, s;
7687dced78aSDavid Greenman 
769f7788e8eSJonathan Lemon 	s = splimp();
7707dced78aSDavid Greenman 
7717dced78aSDavid Greenman 	fxp_stop(sc);
7727dced78aSDavid Greenman 
7737dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
7747dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
7757dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
7767dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
7777dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
7787dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
7797dced78aSDavid Greenman 
7807dced78aSDavid Greenman 	sc->suspended = 1;
7817dced78aSDavid Greenman 
782f7788e8eSJonathan Lemon 	splx(s);
783f7788e8eSJonathan Lemon 	return (0);
7847dced78aSDavid Greenman }
7857dced78aSDavid Greenman 
7867dced78aSDavid Greenman /*
7877dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
7887dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
7897dced78aSDavid Greenman  * appropriate.
7907dced78aSDavid Greenman  */
7917dced78aSDavid Greenman static int
7927dced78aSDavid Greenman fxp_resume(device_t dev)
7937dced78aSDavid Greenman {
7947dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
7957dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
7967dced78aSDavid Greenman 	u_int16_t pci_command;
797f7788e8eSJonathan Lemon 	int i, s;
7987dced78aSDavid Greenman 
799f7788e8eSJonathan Lemon 	s = splimp();
8007dced78aSDavid Greenman 
80148e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
80248e417ebSJonathan Lemon 
8037dced78aSDavid Greenman 	/* better way to do this? */
8047dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
8057dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
8067dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
8077dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
8087dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
8097dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
8107dced78aSDavid Greenman 
8117dced78aSDavid Greenman 	/* reenable busmastering */
8127dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
8137dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
8147dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
8157dced78aSDavid Greenman 
8167dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
8177dced78aSDavid Greenman 	DELAY(10);
8187dced78aSDavid Greenman 
8197dced78aSDavid Greenman 	/* reinitialize interface if necessary */
8207dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
8217dced78aSDavid Greenman 		fxp_init(sc);
8227dced78aSDavid Greenman 
8237dced78aSDavid Greenman 	sc->suspended = 0;
8247dced78aSDavid Greenman 
825f7788e8eSJonathan Lemon 	splx(s);
826ba8c6fd5SDavid Greenman 	return (0);
827f7788e8eSJonathan Lemon }
828ba8c6fd5SDavid Greenman 
82900c4116bSJonathan Lemon static void
83000c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
83100c4116bSJonathan Lemon {
83200c4116bSJonathan Lemon 	u_int16_t reg;
83300c4116bSJonathan Lemon 	int x;
83400c4116bSJonathan Lemon 
83500c4116bSJonathan Lemon 	/*
83600c4116bSJonathan Lemon 	 * Shift in data.
83700c4116bSJonathan Lemon 	 */
83800c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
83900c4116bSJonathan Lemon 		if (data & x)
84000c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
84100c4116bSJonathan Lemon 		else
84200c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
84300c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
84400c4116bSJonathan Lemon 		DELAY(1);
84500c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
84600c4116bSJonathan Lemon 		DELAY(1);
84700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
84800c4116bSJonathan Lemon 		DELAY(1);
84900c4116bSJonathan Lemon 	}
85000c4116bSJonathan Lemon }
85100c4116bSJonathan Lemon 
852f7788e8eSJonathan Lemon /*
853f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
854f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
855f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
856f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
857f7788e8eSJonathan Lemon  * every 16 bits of data.
858f7788e8eSJonathan Lemon  */
859f7788e8eSJonathan Lemon static u_int16_t
860f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
861f7788e8eSJonathan Lemon {
862f7788e8eSJonathan Lemon 	u_int16_t reg, data;
863f7788e8eSJonathan Lemon 	int x;
864ba8c6fd5SDavid Greenman 
865f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
866f7788e8eSJonathan Lemon 	/*
867f7788e8eSJonathan Lemon 	 * Shift in read opcode.
868f7788e8eSJonathan Lemon 	 */
86900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
870f7788e8eSJonathan Lemon 	/*
871f7788e8eSJonathan Lemon 	 * Shift in address.
872f7788e8eSJonathan Lemon 	 */
873f7788e8eSJonathan Lemon 	data = 0;
874f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
875f7788e8eSJonathan Lemon 		if (offset & x)
876f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
877f7788e8eSJonathan Lemon 		else
878f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
879f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
880f7788e8eSJonathan Lemon 		DELAY(1);
881f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
882f7788e8eSJonathan Lemon 		DELAY(1);
883f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
884f7788e8eSJonathan Lemon 		DELAY(1);
885f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
886f7788e8eSJonathan Lemon 		data++;
887f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
888f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
889f7788e8eSJonathan Lemon 			break;
890f7788e8eSJonathan Lemon 		}
891f7788e8eSJonathan Lemon 	}
892f7788e8eSJonathan Lemon 	/*
893f7788e8eSJonathan Lemon 	 * Shift out data.
894f7788e8eSJonathan Lemon 	 */
895f7788e8eSJonathan Lemon 	data = 0;
896f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
897f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
898f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
899f7788e8eSJonathan Lemon 		DELAY(1);
900f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
901f7788e8eSJonathan Lemon 			data |= x;
902f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
903f7788e8eSJonathan Lemon 		DELAY(1);
904f7788e8eSJonathan Lemon 	}
905f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
906f7788e8eSJonathan Lemon 	DELAY(1);
907f7788e8eSJonathan Lemon 
908f7788e8eSJonathan Lemon 	return (data);
909ba8c6fd5SDavid Greenman }
910ba8c6fd5SDavid Greenman 
91100c4116bSJonathan Lemon static void
91200c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
91300c4116bSJonathan Lemon {
91400c4116bSJonathan Lemon 	int i;
91500c4116bSJonathan Lemon 
91600c4116bSJonathan Lemon 	/*
91700c4116bSJonathan Lemon 	 * Erase/write enable.
91800c4116bSJonathan Lemon 	 */
91900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
92000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
92100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
92200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
92300c4116bSJonathan Lemon 	DELAY(1);
92400c4116bSJonathan Lemon 	/*
92500c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
92600c4116bSJonathan Lemon 	 */
92700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
92800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
92900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
93000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
93100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
93200c4116bSJonathan Lemon 	DELAY(1);
93300c4116bSJonathan Lemon 	/*
93400c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
93500c4116bSJonathan Lemon 	 */
93600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
93700c4116bSJonathan Lemon 	DELAY(1);
93800c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
93900c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
94000c4116bSJonathan Lemon 			break;
94100c4116bSJonathan Lemon 		DELAY(50);
94200c4116bSJonathan Lemon 	}
94300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
94400c4116bSJonathan Lemon 	DELAY(1);
94500c4116bSJonathan Lemon 	/*
94600c4116bSJonathan Lemon 	 * Erase/write disable.
94700c4116bSJonathan Lemon 	 */
94800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
94900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
95000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
95100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
95200c4116bSJonathan Lemon 	DELAY(1);
95300c4116bSJonathan Lemon }
95400c4116bSJonathan Lemon 
955ba8c6fd5SDavid Greenman /*
956e9bf2fa7SDavid Greenman  * From NetBSD:
957e9bf2fa7SDavid Greenman  *
958e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
959e9bf2fa7SDavid Greenman  *
960e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
961e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
962e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
963e9bf2fa7SDavid Greenman  *
964e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
965e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
966e9bf2fa7SDavid Greenman  *
967e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
968e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
969e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
970e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
971e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
972e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
973e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
974e9bf2fa7SDavid Greenman  */
975e9bf2fa7SDavid Greenman static void
976f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
977e9bf2fa7SDavid Greenman {
978e9bf2fa7SDavid Greenman 
979f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
980f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
981f7788e8eSJonathan Lemon 
982f7788e8eSJonathan Lemon 	/* autosize */
983f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
984e9bf2fa7SDavid Greenman }
985f7788e8eSJonathan Lemon 
986ba8c6fd5SDavid Greenman static void
987f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
988ba8c6fd5SDavid Greenman {
989f7788e8eSJonathan Lemon 	int i;
990ba8c6fd5SDavid Greenman 
991f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
992f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
993ba8c6fd5SDavid Greenman }
994ba8c6fd5SDavid Greenman 
99500c4116bSJonathan Lemon static void
99600c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
99700c4116bSJonathan Lemon {
99800c4116bSJonathan Lemon 	int i;
99900c4116bSJonathan Lemon 
100000c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
100100c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
100200c4116bSJonathan Lemon }
100300c4116bSJonathan Lemon 
1004a17c678eSDavid Greenman /*
1005a17c678eSDavid Greenman  * Start packet transmission on the interface.
1006a17c678eSDavid Greenman  */
1007a17c678eSDavid Greenman static void
1008f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1009a17c678eSDavid Greenman {
10109b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1011a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1012a17c678eSDavid Greenman 
1013a17c678eSDavid Greenman 	/*
1014483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1015483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1016483b9871SDavid Greenman 	 * of the command chain).
1017a17c678eSDavid Greenman 	 */
10180f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1019a17c678eSDavid Greenman 		return;
10200f4dc94cSChuck Paterson 	}
10211cd443acSDavid Greenman 
1022483b9871SDavid Greenman 	txp = NULL;
1023483b9871SDavid Greenman 
1024483b9871SDavid Greenman 	/*
1025483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1026483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
10273114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
10283114fdb4SDavid Greenman 	 *       a NOP command when needed.
1029483b9871SDavid Greenman 	 */
10303114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1031483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
1032483b9871SDavid Greenman 		int segment;
1033483b9871SDavid Greenman 
1034dfe61cf1SDavid Greenman 		/*
1035dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1036dfe61cf1SDavid Greenman 		 */
10376318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
1038a17c678eSDavid Greenman 
1039dfe61cf1SDavid Greenman 		/*
1040483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1041dfe61cf1SDavid Greenman 		 */
1042a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
1043a17c678eSDavid Greenman 
1044a17c678eSDavid Greenman 		/*
1045a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1046483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1047a17c678eSDavid Greenman 		 * and size of the mbuf.
1048a17c678eSDavid Greenman 		 */
104923a0ed7cSDavid Greenman tbdinit:
1050a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
1051a17c678eSDavid Greenman 			if (m->m_len != 0) {
1052a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1053a17c678eSDavid Greenman 					break;
1054a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1055a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1056a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1057a17c678eSDavid Greenman 				segment++;
1058a17c678eSDavid Greenman 			}
1059a17c678eSDavid Greenman 		}
1060fb583156SDavid Greenman 		if (m != NULL) {
106123a0ed7cSDavid Greenman 			struct mbuf *mn;
106223a0ed7cSDavid Greenman 
1063a17c678eSDavid Greenman 			/*
10643bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
10653bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
10663bd07cfdSJonathan Lemon 			 * new buffers.
1067a17c678eSDavid Greenman 			 */
106823a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
106923a0ed7cSDavid Greenman 			if (mn == NULL) {
107023a0ed7cSDavid Greenman 				m_freem(mb_head);
1071483b9871SDavid Greenman 				break;
1072a17c678eSDavid Greenman 			}
107323a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
107423a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
107523a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
107623a0ed7cSDavid Greenman 					m_freem(mn);
107723a0ed7cSDavid Greenman 					m_freem(mb_head);
1078483b9871SDavid Greenman 					break;
107923a0ed7cSDavid Greenman 				}
108023a0ed7cSDavid Greenman 			}
1081ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1082ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
108323a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
108423a0ed7cSDavid Greenman 			m_freem(mb_head);
108523a0ed7cSDavid Greenman 			mb_head = mn;
108623a0ed7cSDavid Greenman 			goto tbdinit;
108723a0ed7cSDavid Greenman 		}
108823a0ed7cSDavid Greenman 
108923a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10901cd443acSDavid Greenman 		txp->mb_head = mb_head;
1091a17c678eSDavid Greenman 		txp->cb_status = 0;
10923114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1093a17c678eSDavid Greenman 			txp->cb_command =
10943bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10953bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
10963114fdb4SDavid Greenman 		} else {
10973114fdb4SDavid Greenman 			txp->cb_command =
10983bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10993bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
11003114fdb4SDavid Greenman 			/*
11013bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
11023bd07cfdSJonathan Lemon 			 * from the card again.
11033114fdb4SDavid Greenman 			 */
11043114fdb4SDavid Greenman 			ifp->if_timer = 5;
11053114fdb4SDavid Greenman 		}
1106f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1107a17c678eSDavid Greenman 
1108a17c678eSDavid Greenman 		/*
1109483b9871SDavid Greenman 		 * Advance the end of list forward.
1110a17c678eSDavid Greenman 		 */
111106175228SAndrew Gallatin 
111206175228SAndrew Gallatin #ifdef __alpha__
111306175228SAndrew Gallatin 		/*
111406175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
111506175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
111606175228SAndrew Gallatin 		 * up the status while we update the command field.
111706175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
111806175228SAndrew Gallatin 		 */
111906175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
112006175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
112106175228SAndrew Gallatin #else
1122a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
112306175228SAndrew Gallatin #endif /*__alpha__*/
1124a17c678eSDavid Greenman 		sc->cbl_last = txp;
1125a17c678eSDavid Greenman 
1126a17c678eSDavid Greenman 		/*
11271cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
11281cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1129483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1130a17c678eSDavid Greenman 		 */
11311cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1132a17c678eSDavid Greenman 			sc->cbl_first = txp;
1133a17c678eSDavid Greenman 
11341cd443acSDavid Greenman 		sc->tx_queued++;
11351cd443acSDavid Greenman 
1136a17c678eSDavid Greenman 		/*
1137a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1138a17c678eSDavid Greenman 		 */
1139fb583156SDavid Greenman 		if (ifp->if_bpf)
114094927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
1141483b9871SDavid Greenman 	}
1142483b9871SDavid Greenman 
1143483b9871SDavid Greenman 	/*
1144483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1145483b9871SDavid Greenman 	 * going again if suspended.
1146483b9871SDavid Greenman 	 */
1147483b9871SDavid Greenman 	if (txp != NULL) {
1148483b9871SDavid Greenman 		fxp_scb_wait(sc);
11492e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1150483b9871SDavid Greenman 	}
1151a17c678eSDavid Greenman }
1152a17c678eSDavid Greenman 
1153a17c678eSDavid Greenman /*
11549c7d2607SDavid Greenman  * Process interface interrupts.
1155a17c678eSDavid Greenman  */
115694927790SDavid Greenman static void
1157f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1158a17c678eSDavid Greenman {
1159f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1160ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
11611cd443acSDavid Greenman 	u_int8_t statack;
11620f4dc94cSChuck Paterson 
1163b184b38eSDavid Greenman 	if (sc->suspended) {
1164b184b38eSDavid Greenman 		return;
1165b184b38eSDavid Greenman 	}
1166b184b38eSDavid Greenman 
1167b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1168a17c678eSDavid Greenman 		/*
116911457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
117011457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
117111457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
117211457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
117311457bbfSJonathan Lemon 		 */
117411457bbfSJonathan Lemon 		if (statack == 0xff)
117511457bbfSJonathan Lemon 			return;
117611457bbfSJonathan Lemon 
117711457bbfSJonathan Lemon 		/*
1178a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1179a17c678eSDavid Greenman 		 */
1180ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1181a17c678eSDavid Greenman 
1182a17c678eSDavid Greenman 		/*
11833114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
118406936301SBill Paul 		 *
118506936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
118606936301SBill Paul 		 * be that this event (control unit not ready) was not
118706936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
118806936301SBill Paul 		 * The exact sequence of events that occur when the interface
118906936301SBill Paul 		 * is brought up are different now, and if this event
119006936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
119106936301SBill Paul 		 * can stall for several seconds. The result is that no
119206936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
119306936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
11943114fdb4SDavid Greenman 		 */
119506936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
11963114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
11973114fdb4SDavid Greenman 
11983114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
11993114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
12003114fdb4SDavid Greenman 			    txp = txp->next) {
12013114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
12023114fdb4SDavid Greenman 					m_freem(txp->mb_head);
12033114fdb4SDavid Greenman 					txp->mb_head = NULL;
12043114fdb4SDavid Greenman 				}
12053114fdb4SDavid Greenman 				sc->tx_queued--;
12063114fdb4SDavid Greenman 			}
12073114fdb4SDavid Greenman 			sc->cbl_first = txp;
12083114fdb4SDavid Greenman 			ifp->if_timer = 0;
12093114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
12103114fdb4SDavid Greenman 				if (sc->need_mcsetup)
12113114fdb4SDavid Greenman 					fxp_mc_setup(sc);
12123114fdb4SDavid Greenman 			}
12133114fdb4SDavid Greenman 			/*
12143114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
12153114fdb4SDavid Greenman 			 */
12163114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
12173114fdb4SDavid Greenman 				fxp_start(ifp);
12183114fdb4SDavid Greenman 		}
12193114fdb4SDavid Greenman 		/*
1220a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1221a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1222a17c678eSDavid Greenman 		 * re-start the receiver.
1223a17c678eSDavid Greenman 		 */
1224a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1225a17c678eSDavid Greenman 			struct mbuf *m;
1226a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1227a17c678eSDavid Greenman rcvloop:
1228a17c678eSDavid Greenman 			m = sc->rfa_headm;
1229ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1230ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1231a17c678eSDavid Greenman 
1232a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1233dfe61cf1SDavid Greenman 				/*
1234dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1235dfe61cf1SDavid Greenman 				 */
1236a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1237a17c678eSDavid Greenman 				m->m_next = NULL;
1238a17c678eSDavid Greenman 
1239dfe61cf1SDavid Greenman 				/*
1240ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1241ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1242ba8c6fd5SDavid Greenman 				 * instead.
1243dfe61cf1SDavid Greenman 				 */
1244a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1245a17c678eSDavid Greenman 					struct ether_header *eh;
1246aed53495SDavid Greenman 					int total_len;
1247a17c678eSDavid Greenman 
1248ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1249ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1250ba8c6fd5SDavid Greenman 					if (total_len <
1251ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
125206339180SDavid Greenman 						m_freem(m);
125306339180SDavid Greenman 						goto rcvloop;
125406339180SDavid Greenman 					}
1255920b58e8SBrooks Davis 
1256e8c8b728SJonathan Lemon 					/*
1257e8c8b728SJonathan Lemon 					 * Drop the packet if it has CRC
1258e8c8b728SJonathan Lemon 					 * errors.  This test is only needed
1259e8c8b728SJonathan Lemon 					 * when doing 802.1q VLAN on the 82557
1260e8c8b728SJonathan Lemon 					 * chip.
1261e8c8b728SJonathan Lemon 					 */
1262e8c8b728SJonathan Lemon 					if (rfa->rfa_status &
1263e8c8b728SJonathan Lemon 					    FXP_RFA_STATUS_CRC) {
1264e8c8b728SJonathan Lemon 						m_freem(m);
1265e8c8b728SJonathan Lemon 						goto rcvloop;
1266e8c8b728SJonathan Lemon 					}
1267920b58e8SBrooks Davis 
1268a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
12692e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1270a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1271ba8c6fd5SDavid Greenman 					m->m_data +=
1272ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1273ab090e5bSLuigi Rizzo 					m->m_len -=
1274ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1275ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1276a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1277a17c678eSDavid Greenman 				}
1278a17c678eSDavid Greenman 				goto rcvloop;
1279a17c678eSDavid Greenman 			}
1280a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1281ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1282ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1283ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1284ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
12852e2b8238SJonathan Lemon 				fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1286a17c678eSDavid Greenman 			}
1287a17c678eSDavid Greenman 		}
1288a17c678eSDavid Greenman 	}
1289a17c678eSDavid Greenman }
1290a17c678eSDavid Greenman 
1291dfe61cf1SDavid Greenman /*
1292dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1293dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1294dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1295dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1296dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1297dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1298dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1299dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1300dfe61cf1SDavid Greenman  * them again next time.
1301dfe61cf1SDavid Greenman  */
1302303b270bSEivind Eklund static void
1303f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1304a17c678eSDavid Greenman {
1305f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1306ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1307a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1308c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1309f7788e8eSJonathan Lemon 	int s;
1310a17c678eSDavid Greenman 
1311a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1312a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1313397f9dfeSDavid Greenman 	if (sp->rx_good) {
1314397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1315397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1316397f9dfeSDavid Greenman 	} else {
1317c8cc6fcaSDavid Greenman 		/*
1318c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1319c8cc6fcaSDavid Greenman 		 */
1320397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1321397f9dfeSDavid Greenman 	}
13223ba65732SDavid Greenman 	ifp->if_ierrors +=
13233ba65732SDavid Greenman 	    sp->rx_crc_errors +
13243ba65732SDavid Greenman 	    sp->rx_alignment_errors +
13253ba65732SDavid Greenman 	    sp->rx_rnr_errors +
13266e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1327a17c678eSDavid Greenman 	/*
1328f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1329f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1330f9be9005SDavid Greenman 	 */
1331f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1332f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1333f9be9005SDavid Greenman 		if (tx_threshold < 192)
1334f9be9005SDavid Greenman 			tx_threshold += 64;
1335f9be9005SDavid Greenman 	}
1336f7788e8eSJonathan Lemon 	s = splimp();
1337397f9dfeSDavid Greenman 	/*
1338c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1339c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1340c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1341c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1342c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1343c8cc6fcaSDavid Greenman 	 */
1344c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1345c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1346c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1347c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1348c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1349c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1350c8cc6fcaSDavid Greenman 		}
1351c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1352c8cc6fcaSDavid Greenman 	}
1353c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1354c8cc6fcaSDavid Greenman 	/*
1355397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1356397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1357397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1358397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1359397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1360397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1361397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1362397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1363397f9dfeSDavid Greenman 	 */
1364397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1365397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1366397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1367397f9dfeSDavid Greenman 	}
1368f9be9005SDavid Greenman 	/*
13693ba65732SDavid Greenman 	 * If there is no pending command, start another stats
13703ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1371a17c678eSDavid Greenman 	 */
1372397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1373a17c678eSDavid Greenman 		/*
1374397f9dfeSDavid Greenman 		 * Start another stats dump.
1375a17c678eSDavid Greenman 		 */
13762e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1377dfe61cf1SDavid Greenman 	} else {
1378dfe61cf1SDavid Greenman 		/*
1379dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1380dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
13813ba65732SDavid Greenman 		 * next timer event to update them.
1382dfe61cf1SDavid Greenman 		 */
1383dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1384f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1385dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
13863ba65732SDavid Greenman 
1387dfe61cf1SDavid Greenman 		sp->rx_good = 0;
13883ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
13893ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
13903ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
13913ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1392dfe61cf1SDavid Greenman 	}
1393f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1394f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
139574396a0aSJonathan Lemon 	splx(s);
1396a17c678eSDavid Greenman 	/*
1397a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1398a17c678eSDavid Greenman 	 */
1399f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1400a17c678eSDavid Greenman }
1401a17c678eSDavid Greenman 
1402a17c678eSDavid Greenman /*
1403a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1404a17c678eSDavid Greenman  * the interface.
1405a17c678eSDavid Greenman  */
1406a17c678eSDavid Greenman static void
1407f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1408a17c678eSDavid Greenman {
1409ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
14103ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
14113ba65732SDavid Greenman 	int i;
1412a17c678eSDavid Greenman 
14137dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
14147dced78aSDavid Greenman 	ifp->if_timer = 0;
14157dced78aSDavid Greenman 
1416a17c678eSDavid Greenman 	/*
1417a17c678eSDavid Greenman 	 * Cancel stats updater.
1418a17c678eSDavid Greenman 	 */
1419f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
14203ba65732SDavid Greenman 
14213ba65732SDavid Greenman 	/*
142272a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
14233ba65732SDavid Greenman 	 */
142472a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
142572a32a26SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
142672a32a26SJonathan Lemon 	DELAY(50);
1427a17c678eSDavid Greenman 
14283ba65732SDavid Greenman 	/*
14293ba65732SDavid Greenman 	 * Release any xmit buffers.
14303ba65732SDavid Greenman 	 */
1431da91462dSDavid Greenman 	txp = sc->cbl_base;
1432da91462dSDavid Greenman 	if (txp != NULL) {
1433da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1434da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1435da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1436da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1437da91462dSDavid Greenman 			}
1438da91462dSDavid Greenman 		}
14393ba65732SDavid Greenman 	}
14403ba65732SDavid Greenman 	sc->tx_queued = 0;
14413ba65732SDavid Greenman 
14423ba65732SDavid Greenman 	/*
14433ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
14443ba65732SDavid Greenman 	 */
14453ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
14463ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
14473ba65732SDavid Greenman 	sc->rfa_headm = NULL;
14483ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
14493ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
14503ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
14513ba65732SDavid Greenman 			/*
14523ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
14533ba65732SDavid Greenman 			 * and we just freed all the buffers we need
14543ba65732SDavid Greenman 			 * above.
14553ba65732SDavid Greenman 			 */
14563ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
14573ba65732SDavid Greenman 		}
14583ba65732SDavid Greenman 	}
1459a17c678eSDavid Greenman }
1460a17c678eSDavid Greenman 
1461a17c678eSDavid Greenman /*
1462a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1463a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1464a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1465a17c678eSDavid Greenman  * card has wedged for some reason.
1466a17c678eSDavid Greenman  */
1467a17c678eSDavid Greenman static void
1468f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1469a17c678eSDavid Greenman {
1470ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1471ba8c6fd5SDavid Greenman 
1472f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
14734a5f1499SDavid Greenman 	ifp->if_oerrors++;
1474a17c678eSDavid Greenman 
1475ba8c6fd5SDavid Greenman 	fxp_init(sc);
1476a17c678eSDavid Greenman }
1477a17c678eSDavid Greenman 
1478a17c678eSDavid Greenman static void
1479f7788e8eSJonathan Lemon fxp_init(void *xsc)
1480a17c678eSDavid Greenman {
1481fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1482ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1483a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1484a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1485a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1486f7788e8eSJonathan Lemon 	int i, prm, s;
1487a17c678eSDavid Greenman 
1488f7788e8eSJonathan Lemon 	s = splimp();
1489a17c678eSDavid Greenman 	/*
14903ba65732SDavid Greenman 	 * Cancel any pending I/O
1491a17c678eSDavid Greenman 	 */
14923ba65732SDavid Greenman 	fxp_stop(sc);
1493a17c678eSDavid Greenman 
1494a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1495a17c678eSDavid Greenman 
1496a17c678eSDavid Greenman 	/*
1497a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1498a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1499a17c678eSDavid Greenman 	 */
1500ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
15012e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1502a17c678eSDavid Greenman 
1503ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
15042e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1505a17c678eSDavid Greenman 
1506a17c678eSDavid Greenman 	/*
1507a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1508a17c678eSDavid Greenman 	 */
1509ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1510ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
15112e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1512a17c678eSDavid Greenman 
1513a17c678eSDavid Greenman 	/*
151472a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
151572a32a26SJonathan Lemon 	 */
151672a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
151772a32a26SJonathan Lemon 		fxp_load_ucode(sc);
151872a32a26SJonathan Lemon 
151972a32a26SJonathan Lemon 	/*
1520a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1521a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1522a17c678eSDavid Greenman 	 * later.
1523a17c678eSDavid Greenman 	 */
1524a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1525a17c678eSDavid Greenman 
1526a17c678eSDavid Greenman 	/*
1527a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1528a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1529a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1530a17c678eSDavid Greenman 	 */
1531d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1532d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1533397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1534a17c678eSDavid Greenman 
1535a17c678eSDavid Greenman 	cbp->cb_status =	0;
1536a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1537a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1538a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1539001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1540001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1541a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1542f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1543f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1544f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1545f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1546001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1547001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1548f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1549a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1550f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1551f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
15523114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1553f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1554f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1555f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
155672a32a26SJonathan Lemon 	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
1557a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1558f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1559f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1560f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1561f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1562f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1563f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1564f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1565f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1566f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1567f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1568a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1569a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1570a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1571a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1572a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1573a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1574a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1575a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1576f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1577f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1578f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1579f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1580f7788e8eSJonathan Lemon 
1581a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1582a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1583a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1584f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1585f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1586f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1587f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1588a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
15893ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1590a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1591f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1592a17c678eSDavid Greenman 
159372a32a26SJonathan Lemon 	if (sc->revision == FXP_REV_82557) {
15943bd07cfdSJonathan Lemon 		/*
15953bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
15963bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
15973bd07cfdSJonathan Lemon 		 */
15983bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
15993bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
16003bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
16013bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
16023bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
16033bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
16043bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
16053bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
16063bd07cfdSJonathan Lemon 	} else {
16073bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
16083bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
16093bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
16103bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
16113bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
16123bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
16133bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
16143bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
16153bd07cfdSJonathan Lemon 	}
16163bd07cfdSJonathan Lemon 
1617a17c678eSDavid Greenman 	/*
1618a17c678eSDavid Greenman 	 * Start the config command/DMA.
1619a17c678eSDavid Greenman 	 */
1620ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1621397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
16222e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1623a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
16247dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1625a17c678eSDavid Greenman 
1626a17c678eSDavid Greenman 	/*
1627a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1628a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1629a17c678eSDavid Greenman 	 */
1630a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1631a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1632a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1633a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1634d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1635d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1636a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1637a17c678eSDavid Greenman 
1638a17c678eSDavid Greenman 	/*
1639a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1640a17c678eSDavid Greenman 	 */
1641ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
16422e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1643a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
16447dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1645a17c678eSDavid Greenman 
1646a17c678eSDavid Greenman 	/*
1647a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1648a17c678eSDavid Greenman 	 */
1649a17c678eSDavid Greenman 
1650a17c678eSDavid Greenman 	txp = sc->cbl_base;
1651a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1652a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1653a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1654a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
16553bd07cfdSJonathan Lemon 		txp[i].link_addr =
16563bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
16573bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
16583bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
16593bd07cfdSJonathan Lemon 		else
1660a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1661a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1662a17c678eSDavid Greenman 	}
1663a17c678eSDavid Greenman 	/*
1664397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1665a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1666a17c678eSDavid Greenman 	 */
1667a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1668a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1669397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1670a17c678eSDavid Greenman 
1671ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
16722e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1673a17c678eSDavid Greenman 
1674a17c678eSDavid Greenman 	/*
1675a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1676a17c678eSDavid Greenman 	 */
1677ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1678ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1679ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
16802e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1681a17c678eSDavid Greenman 
1682dccee1a1SDavid Greenman 	/*
1683ba8c6fd5SDavid Greenman 	 * Set current media.
1684dccee1a1SDavid Greenman 	 */
1685f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1686f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1687dccee1a1SDavid Greenman 
1688a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1689a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1690e8c8b728SJonathan Lemon 
1691e8c8b728SJonathan Lemon 	/*
1692e8c8b728SJonathan Lemon 	 * Enable interrupts.
1693e8c8b728SJonathan Lemon 	 */
1694e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1695f7788e8eSJonathan Lemon 	splx(s);
1696a17c678eSDavid Greenman 
1697a17c678eSDavid Greenman 	/*
1698a17c678eSDavid Greenman 	 * Start stats updater.
1699a17c678eSDavid Greenman 	 */
1700f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1701f7788e8eSJonathan Lemon }
1702f7788e8eSJonathan Lemon 
1703f7788e8eSJonathan Lemon static int
1704f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1705f7788e8eSJonathan Lemon {
1706f7788e8eSJonathan Lemon 
1707f7788e8eSJonathan Lemon 	return (0);
1708a17c678eSDavid Greenman }
1709a17c678eSDavid Greenman 
1710303b270bSEivind Eklund static void
1711f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1712ba8c6fd5SDavid Greenman {
1713ba8c6fd5SDavid Greenman 
1714f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1715ba8c6fd5SDavid Greenman }
1716ba8c6fd5SDavid Greenman 
1717ba8c6fd5SDavid Greenman /*
1718ba8c6fd5SDavid Greenman  * Change media according to request.
1719ba8c6fd5SDavid Greenman  */
1720f7788e8eSJonathan Lemon static int
1721f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1722ba8c6fd5SDavid Greenman {
1723ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1724f7788e8eSJonathan Lemon 	struct mii_data *mii;
1725ba8c6fd5SDavid Greenman 
1726f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1727f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1728ba8c6fd5SDavid Greenman 	return (0);
1729ba8c6fd5SDavid Greenman }
1730ba8c6fd5SDavid Greenman 
1731ba8c6fd5SDavid Greenman /*
1732ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1733ba8c6fd5SDavid Greenman  */
1734f7788e8eSJonathan Lemon static void
1735f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1736ba8c6fd5SDavid Greenman {
1737ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1738f7788e8eSJonathan Lemon 	struct mii_data *mii;
1739ba8c6fd5SDavid Greenman 
1740f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1741f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1742f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1743f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
17442e2b8238SJonathan Lemon 
17452e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
17462e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
17472e2b8238SJonathan Lemon 	else
17482e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
1749ba8c6fd5SDavid Greenman }
1750ba8c6fd5SDavid Greenman 
1751a17c678eSDavid Greenman /*
1752a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1753a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1754a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1755dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1756a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1757a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1758a17c678eSDavid Greenman  */
1759a17c678eSDavid Greenman static int
1760f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1761a17c678eSDavid Greenman {
1762ba8c6fd5SDavid Greenman 	u_int32_t v;
1763a17c678eSDavid Greenman 	struct mbuf *m;
1764a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1765a17c678eSDavid Greenman 
1766a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1767a17c678eSDavid Greenman 	if (m != NULL) {
1768a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1769a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1770a17c678eSDavid Greenman 			m_freem(m);
1771eadd5e3aSDavid Greenman 			if (oldm == NULL)
1772eadd5e3aSDavid Greenman 				return 1;
1773a17c678eSDavid Greenman 			m = oldm;
1774eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1775a17c678eSDavid Greenman 		}
1776a17c678eSDavid Greenman 	} else {
1777eadd5e3aSDavid Greenman 		if (oldm == NULL)
1778a17c678eSDavid Greenman 			return 1;
1779eadd5e3aSDavid Greenman 		m = oldm;
1780eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1781eadd5e3aSDavid Greenman 	}
1782ba8c6fd5SDavid Greenman 
1783ba8c6fd5SDavid Greenman 	/*
1784ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1785ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1786ba8c6fd5SDavid Greenman 	 */
1787ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1788ba8c6fd5SDavid Greenman 
1789eadd5e3aSDavid Greenman 	/*
1790eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1791eadd5e3aSDavid Greenman 	 * data start past it.
1792eadd5e3aSDavid Greenman 	 */
1793a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1794eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
17954fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1796eadd5e3aSDavid Greenman 
1797ba8c6fd5SDavid Greenman 	/*
1798ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1799ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1800ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1801ba8c6fd5SDavid Greenman 	 */
18024fc1dda9SAndrew Gallatin 
1803a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1804a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1805a17c678eSDavid Greenman 	rfa->actual_size = 0;
1806ba8c6fd5SDavid Greenman 
1807ba8c6fd5SDavid Greenman 	v = -1;
18084fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
18094fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1810ba8c6fd5SDavid Greenman 
1811dfe61cf1SDavid Greenman 	/*
1812dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1813dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1814dfe61cf1SDavid Greenman 	 */
1815a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1816ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1817ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1818a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1819ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
18204fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1821aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1822a17c678eSDavid Greenman 	} else {
1823a17c678eSDavid Greenman 		sc->rfa_headm = m;
1824a17c678eSDavid Greenman 	}
1825a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1826a17c678eSDavid Greenman 
1827dfe61cf1SDavid Greenman 	return (m == oldm);
1828a17c678eSDavid Greenman }
1829a17c678eSDavid Greenman 
18306ebc3153SDavid Greenman static volatile int
1831f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1832dccee1a1SDavid Greenman {
1833f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1834dccee1a1SDavid Greenman 	int count = 10000;
18356ebc3153SDavid Greenman 	int value;
1836dccee1a1SDavid Greenman 
1837ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1838ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1839dccee1a1SDavid Greenman 
1840ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1841ba8c6fd5SDavid Greenman 	    && count--)
18426ebc3153SDavid Greenman 		DELAY(10);
1843dccee1a1SDavid Greenman 
1844dccee1a1SDavid Greenman 	if (count <= 0)
1845f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1846dccee1a1SDavid Greenman 
18476ebc3153SDavid Greenman 	return (value & 0xffff);
1848dccee1a1SDavid Greenman }
1849dccee1a1SDavid Greenman 
1850dccee1a1SDavid Greenman static void
1851f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1852dccee1a1SDavid Greenman {
1853f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1854dccee1a1SDavid Greenman 	int count = 10000;
1855dccee1a1SDavid Greenman 
1856ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1857ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1858ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1859dccee1a1SDavid Greenman 
1860ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1861ba8c6fd5SDavid Greenman 	    count--)
18626ebc3153SDavid Greenman 		DELAY(10);
1863dccee1a1SDavid Greenman 
1864dccee1a1SDavid Greenman 	if (count <= 0)
1865f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1866dccee1a1SDavid Greenman }
1867dccee1a1SDavid Greenman 
1868dccee1a1SDavid Greenman static int
1869f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1870a17c678eSDavid Greenman {
18719b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1872a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1873f7788e8eSJonathan Lemon 	struct mii_data *mii;
1874f7788e8eSJonathan Lemon 	int s, error = 0;
1875a17c678eSDavid Greenman 
1876f7788e8eSJonathan Lemon 	s = splimp();
1877a17c678eSDavid Greenman 
1878a17c678eSDavid Greenman 	switch (command) {
1879a17c678eSDavid Greenman 	case SIOCSIFADDR:
1880a17c678eSDavid Greenman 	case SIOCGIFADDR:
1881fb583156SDavid Greenman 	case SIOCSIFMTU:
1882fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1883a17c678eSDavid Greenman 		break;
1884a17c678eSDavid Greenman 
1885a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1886f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1887f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1888f7788e8eSJonathan Lemon 		else
1889f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1890a17c678eSDavid Greenman 
1891a17c678eSDavid Greenman 		/*
1892a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1893a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1894a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1895a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1896a17c678eSDavid Greenman 		 */
1897a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1898fb583156SDavid Greenman 			fxp_init(sc);
1899a17c678eSDavid Greenman 		} else {
1900a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
19014a5f1499SDavid Greenman 				fxp_stop(sc);
1902a17c678eSDavid Greenman 		}
1903a17c678eSDavid Greenman 		break;
1904a17c678eSDavid Greenman 
1905a17c678eSDavid Greenman 	case SIOCADDMULTI:
1906a17c678eSDavid Greenman 	case SIOCDELMULTI:
1907f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1908f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1909f7788e8eSJonathan Lemon 		else
1910f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1911a17c678eSDavid Greenman 		/*
1912a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1913a17c678eSDavid Greenman 		 * accordingly.
1914a17c678eSDavid Greenman 		 */
1915f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1916397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1917397f9dfeSDavid Greenman 		/*
1918f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1919397f9dfeSDavid Greenman 		 * again rather than else {}.
1920397f9dfeSDavid Greenman 		 */
1921f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1922fb583156SDavid Greenman 			fxp_init(sc);
1923a17c678eSDavid Greenman 		error = 0;
1924ba8c6fd5SDavid Greenman 		break;
1925ba8c6fd5SDavid Greenman 
1926ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1927ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1928f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
1929f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
1930f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
1931f7788e8eSJonathan Lemon                             &mii->mii_media, command);
1932f7788e8eSJonathan Lemon 		} else {
1933ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1934f7788e8eSJonathan Lemon 		}
1935a17c678eSDavid Greenman 		break;
1936a17c678eSDavid Greenman 
1937a17c678eSDavid Greenman 	default:
1938a17c678eSDavid Greenman 		error = EINVAL;
1939a17c678eSDavid Greenman 	}
1940f7788e8eSJonathan Lemon 	splx(s);
1941a17c678eSDavid Greenman 	return (error);
1942a17c678eSDavid Greenman }
1943397f9dfeSDavid Greenman 
1944397f9dfeSDavid Greenman /*
1945397f9dfeSDavid Greenman  * Program the multicast filter.
1946397f9dfeSDavid Greenman  *
1947397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1948397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
19493114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1950397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1951dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1952397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1953397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1954397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1955397f9dfeSDavid Greenman  *
1956397f9dfeSDavid Greenman  * This function must be called at splimp.
1957397f9dfeSDavid Greenman  */
1958397f9dfeSDavid Greenman static void
1959f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
1960397f9dfeSDavid Greenman {
1961397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1962397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1963397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1964397f9dfeSDavid Greenman 	int nmcasts;
19657dced78aSDavid Greenman 	int count;
1966397f9dfeSDavid Greenman 
19673114fdb4SDavid Greenman 	/*
19683114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
19693114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
19703114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
19713114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
19723114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
19733114fdb4SDavid Greenman 	 */
1974397f9dfeSDavid Greenman 	if (sc->tx_queued) {
19753114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
19763114fdb4SDavid Greenman 
19773114fdb4SDavid Greenman 		/*
19783114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
19793114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
19803114fdb4SDavid Greenman 		 */
19813114fdb4SDavid Greenman 		if (sc->need_mcsetup)
19823114fdb4SDavid Greenman 			return;
1983397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
19843114fdb4SDavid Greenman 
19853114fdb4SDavid Greenman 		/*
198672a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
198772a32a26SJonathan Lemon 		 * when all TX commands have been processed.
19883114fdb4SDavid Greenman 		 */
19893114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
19903114fdb4SDavid Greenman 		txp->mb_head = NULL;
19913114fdb4SDavid Greenman 		txp->cb_status = 0;
1992e8c8b728SJonathan Lemon 		txp->cb_command = FXP_CB_COMMAND_NOP |
1993e8c8b728SJonathan Lemon 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
19943114fdb4SDavid Greenman 		/*
19953114fdb4SDavid Greenman 		 * Advance the end of list forward.
19963114fdb4SDavid Greenman 		 */
19973114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
19983114fdb4SDavid Greenman 		sc->cbl_last = txp;
19993114fdb4SDavid Greenman 		sc->tx_queued++;
20003114fdb4SDavid Greenman 		/*
20013114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
20023114fdb4SDavid Greenman 		 */
20033114fdb4SDavid Greenman 		fxp_scb_wait(sc);
20042e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
20053114fdb4SDavid Greenman 		/*
20063114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
20073114fdb4SDavid Greenman 		 * card again.
20083114fdb4SDavid Greenman 		 */
20093114fdb4SDavid Greenman 		ifp->if_timer = 5;
20103114fdb4SDavid Greenman 
2011397f9dfeSDavid Greenman 		return;
2012397f9dfeSDavid Greenman 	}
2013397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2014397f9dfeSDavid Greenman 
2015397f9dfeSDavid Greenman 	/*
2016397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2017397f9dfeSDavid Greenman 	 */
2018397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
2019397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
2020397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
2021e8c8b728SJonathan Lemon 	mcsp->cb_command = FXP_CB_COMMAND_MCAS |
2022e8c8b728SJonathan Lemon 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
2023397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
2024397f9dfeSDavid Greenman 
2025397f9dfeSDavid Greenman 	nmcasts = 0;
2026f7788e8eSJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
2027f7788e8eSJonathan Lemon #if __FreeBSD_version < 500000
2028f7788e8eSJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2029f7788e8eSJonathan Lemon #else
20306817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2031f7788e8eSJonathan Lemon #endif
2032397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
2033397f9dfeSDavid Greenman 				continue;
2034397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
2035f7788e8eSJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
2036397f9dfeSDavid Greenman 				nmcasts = 0;
2037397f9dfeSDavid Greenman 				break;
2038397f9dfeSDavid Greenman 			}
2039397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2040d244b0e9SPeter Wemm 			    (void *)(uintptr_t)(volatile void *)
2041d244b0e9SPeter Wemm 				&sc->mcsp->mc_addr[nmcasts][0], 6);
2042397f9dfeSDavid Greenman 			nmcasts++;
2043397f9dfeSDavid Greenman 		}
2044397f9dfeSDavid Greenman 	}
2045397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
2046397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2047397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2048397f9dfeSDavid Greenman 
2049397f9dfeSDavid Greenman 	/*
2050397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2051397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2052397f9dfeSDavid Greenman 	 */
20537dced78aSDavid Greenman 	count = 100;
2054397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
20557dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
20567dced78aSDavid Greenman 		DELAY(10);
20577dced78aSDavid Greenman 	if (count == 0) {
2058f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
20597dced78aSDavid Greenman 		return;
20607dced78aSDavid Greenman 	}
2061397f9dfeSDavid Greenman 
2062397f9dfeSDavid Greenman 	/*
2063397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2064397f9dfeSDavid Greenman 	 */
2065397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2066397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
20672e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2068397f9dfeSDavid Greenman 
20693114fdb4SDavid Greenman 	ifp->if_timer = 2;
2070397f9dfeSDavid Greenman 	return;
2071397f9dfeSDavid Greenman }
207272a32a26SJonathan Lemon 
207372a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
207472a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
207572a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
207672a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
207772a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
207872a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
207972a32a26SJonathan Lemon 
208072a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
208172a32a26SJonathan Lemon 
208272a32a26SJonathan Lemon struct ucode {
208372a32a26SJonathan Lemon 	u_int32_t	revision;
208472a32a26SJonathan Lemon 	u_int32_t	*ucode;
208572a32a26SJonathan Lemon 	int		length;
208672a32a26SJonathan Lemon 	u_short		int_delay_offset;
208772a32a26SJonathan Lemon 	u_short		bundle_max_offset;
208872a32a26SJonathan Lemon } ucode_table[] = {
208972a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
209072a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
209172a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
209272a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
209372a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
209472a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
209572a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
209672a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
209772a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
209872a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
209972a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
210072a32a26SJonathan Lemon };
210172a32a26SJonathan Lemon 
210272a32a26SJonathan Lemon static void
210372a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
210472a32a26SJonathan Lemon {
210572a32a26SJonathan Lemon 	struct ucode *uc;
210672a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
210772a32a26SJonathan Lemon 
210872a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
210972a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
211072a32a26SJonathan Lemon 			break;
211172a32a26SJonathan Lemon 	if (uc->ucode == NULL)
211272a32a26SJonathan Lemon 		return;
211372a32a26SJonathan Lemon 	cbp = (struct fxp_cb_ucode *)sc->cbl_base;
211472a32a26SJonathan Lemon 	cbp->cb_status = 0;
211572a32a26SJonathan Lemon 	cbp->cb_command = FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL;
211672a32a26SJonathan Lemon 	cbp->link_addr = -1;    	/* (no) next command */
211772a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
211872a32a26SJonathan Lemon 	if (uc->int_delay_offset)
211972a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->int_delay_offset] =
212072a32a26SJonathan Lemon 		    sc->tunable_int_delay * 1.4881;
212172a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
212272a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->bundle_max_offset] =
212372a32a26SJonathan Lemon 		    sc->tunable_bundle_max;
212472a32a26SJonathan Lemon 	/*
212572a32a26SJonathan Lemon 	 * Download the ucode to the chip.
212672a32a26SJonathan Lemon 	 */
212772a32a26SJonathan Lemon 	fxp_scb_wait(sc);
212872a32a26SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
212972a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
213072a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
213172a32a26SJonathan Lemon 	fxp_dma_wait(&cbp->cb_status, sc);
213272a32a26SJonathan Lemon 	device_printf(sc->dev,
213372a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
213472a32a26SJonathan Lemon 	    sc->tunable_int_delay,
213572a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
213672a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
213772a32a26SJonathan Lemon }
213872a32a26SJonathan Lemon 
213972a32a26SJonathan Lemon static int
214072a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
214172a32a26SJonathan Lemon {
214272a32a26SJonathan Lemon 	int error, value;
214372a32a26SJonathan Lemon 
214472a32a26SJonathan Lemon 	value = *(int *)arg1;
214572a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
214672a32a26SJonathan Lemon 	if (error || !req->newptr)
214772a32a26SJonathan Lemon 		return (error);
214872a32a26SJonathan Lemon 	if (value < low || value > high)
214972a32a26SJonathan Lemon 		return (EINVAL);
215072a32a26SJonathan Lemon 	*(int *)arg1 = value;
215172a32a26SJonathan Lemon 	return (0);
215272a32a26SJonathan Lemon }
215372a32a26SJonathan Lemon 
215472a32a26SJonathan Lemon /*
215572a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
215672a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
215772a32a26SJonathan Lemon  */
215872a32a26SJonathan Lemon static int
215972a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
216072a32a26SJonathan Lemon {
216172a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
216272a32a26SJonathan Lemon }
216372a32a26SJonathan Lemon 
216472a32a26SJonathan Lemon static int
216572a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
216672a32a26SJonathan Lemon {
216772a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
216872a32a26SJonathan Lemon }
2169