xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 0988236390f03657d80ecb5e215f58076cd4ec52)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28c3aac50fSPeter Wemm  * $FreeBSD$
29a17c678eSDavid Greenman  */
30a17c678eSDavid Greenman 
31a17c678eSDavid Greenman /*
32ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
33a17c678eSDavid Greenman  */
34a17c678eSDavid Greenman 
35a17c678eSDavid Greenman #include <sys/param.h>
36a17c678eSDavid Greenman #include <sys/systm.h>
37a17c678eSDavid Greenman #include <sys/mbuf.h>
38a17c678eSDavid Greenman #include <sys/malloc.h>
39f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
40a17c678eSDavid Greenman #include <sys/kernel.h>
414458ac71SBruce Evans #include <sys/socket.h>
4272a32a26SJonathan Lemon #include <sys/sysctl.h>
43a17c678eSDavid Greenman 
44a17c678eSDavid Greenman #include <net/if.h>
45397f9dfeSDavid Greenman #include <net/if_dl.h>
46ba8c6fd5SDavid Greenman #include <net/if_media.h>
47a17c678eSDavid Greenman 
48a17c678eSDavid Greenman #ifdef NS
49a17c678eSDavid Greenman #include <netns/ns.h>
50a17c678eSDavid Greenman #include <netns/ns_if.h>
51a17c678eSDavid Greenman #endif
52a17c678eSDavid Greenman 
53a17c678eSDavid Greenman #include <net/bpf.h>
54ba8c6fd5SDavid Greenman #include <sys/sockio.h>
556182fdbdSPeter Wemm #include <sys/bus.h>
566182fdbdSPeter Wemm #include <machine/bus.h>
576182fdbdSPeter Wemm #include <sys/rman.h>
586182fdbdSPeter Wemm #include <machine/resource.h>
59ba8c6fd5SDavid Greenman 
601d5e9e22SEivind Eklund #include <net/ethernet.h>
611d5e9e22SEivind Eklund #include <net/if_arp.h>
62ba8c6fd5SDavid Greenman 
63dfe61cf1SDavid Greenman #include <vm/vm.h>		/* for vtophys */
64efeaf95aSDavid Greenman #include <vm/pmap.h>		/* for vtophys */
65f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
66a17c678eSDavid Greenman 
67e8c8b728SJonathan Lemon #include <net/if_types.h>
68e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
69e8c8b728SJonathan Lemon 
70a17c678eSDavid Greenman #include <pci/pcivar.h>
71df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
72a17c678eSDavid Greenman 
73f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
74f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
75f7788e8eSJonathan Lemon 
76f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
77f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
7872a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
79f7788e8eSJonathan Lemon 
80f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
81f7788e8eSJonathan Lemon #include "miibus_if.h"
824fc1dda9SAndrew Gallatin 
83ba8c6fd5SDavid Greenman /*
84ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
85ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
86ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
87ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
88ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
89ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
90ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
91ba8c6fd5SDavid Greenman  */
92ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
93ba8c6fd5SDavid Greenman 
94ba8c6fd5SDavid Greenman /*
95f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
96f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
97f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
98f7788e8eSJonathan Lemon  */
99f7788e8eSJonathan Lemon static int tx_threshold = 64;
100f7788e8eSJonathan Lemon 
101f7788e8eSJonathan Lemon /*
102f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
103f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
104f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
105f7788e8eSJonathan Lemon  * to fxp_init.
106f7788e8eSJonathan Lemon  *
107f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
108f7788e8eSJonathan Lemon  */
109f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
110f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
111f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
112f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
113f7788e8eSJonathan Lemon 	0x0,	/*  0 */
114f7788e8eSJonathan Lemon 	0x0,	/*  1 */
115f7788e8eSJonathan Lemon 	0x0,	/*  2 */
116f7788e8eSJonathan Lemon 	0x0,	/*  3 */
117f7788e8eSJonathan Lemon 	0x0,	/*  4 */
118f7788e8eSJonathan Lemon 	0x0,	/*  5 */
119f7788e8eSJonathan Lemon 	0x32,	/*  6 */
120f7788e8eSJonathan Lemon 	0x0,	/*  7 */
121f7788e8eSJonathan Lemon 	0x0,	/*  8 */
122f7788e8eSJonathan Lemon 	0x0,	/*  9 */
123f7788e8eSJonathan Lemon 	0x6,	/* 10 */
124f7788e8eSJonathan Lemon 	0x0,	/* 11 */
125f7788e8eSJonathan Lemon 	0x0,	/* 12 */
126f7788e8eSJonathan Lemon 	0x0,	/* 13 */
127f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
128f7788e8eSJonathan Lemon 	0x48,	/* 15 */
129f7788e8eSJonathan Lemon 	0x0,	/* 16 */
130f7788e8eSJonathan Lemon 	0x40,	/* 17 */
131f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
132f7788e8eSJonathan Lemon 	0x0,	/* 19 */
133f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
134f7788e8eSJonathan Lemon 	0x5	/* 21 */
135f7788e8eSJonathan Lemon };
136f7788e8eSJonathan Lemon 
137f7788e8eSJonathan Lemon struct fxp_ident {
138f7788e8eSJonathan Lemon 	u_int16_t	devid;
139f7788e8eSJonathan Lemon 	char 		*name;
140f7788e8eSJonathan Lemon };
141f7788e8eSJonathan Lemon 
142f7788e8eSJonathan Lemon /*
143f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
144f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
145f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
146f7788e8eSJonathan Lemon  * them.
147f7788e8eSJonathan Lemon  */
148f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
149f7788e8eSJonathan Lemon     { 0x1229,		"Intel Pro 10/100B/100+ Ethernet" },
150f7788e8eSJonathan Lemon     { 0x2449,		"Intel Pro/100 Ethernet" },
151f7788e8eSJonathan Lemon     { 0x1209,		"Intel Embedded 10/100 Ethernet" },
152f7788e8eSJonathan Lemon     { 0x1029,		"Intel Pro/100 Ethernet" },
153f7788e8eSJonathan Lemon     { 0x1030,		"Intel Pro/100 Ethernet" },
154f7788e8eSJonathan Lemon     { 0x1031,		"Intel Pro/100 Ethernet" },
155f7788e8eSJonathan Lemon     { 0x1032,		"Intel Pro/100 Ethernet" },
156f7788e8eSJonathan Lemon     { 0x1033,		"Intel Pro/100 Ethernet" },
157f7788e8eSJonathan Lemon     { 0x1034,		"Intel Pro/100 Ethernet" },
158f7788e8eSJonathan Lemon     { 0x1035,		"Intel Pro/100 Ethernet" },
159f7788e8eSJonathan Lemon     { 0x1036,		"Intel Pro/100 Ethernet" },
160f7788e8eSJonathan Lemon     { 0x1037,		"Intel Pro/100 Ethernet" },
161f7788e8eSJonathan Lemon     { 0x1038,		"Intel Pro/100 Ethernet" },
162f7788e8eSJonathan Lemon     { 0,		NULL },
163f7788e8eSJonathan Lemon };
164f7788e8eSJonathan Lemon 
165f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
166f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
167f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
168f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
169f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
170f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
171f7788e8eSJonathan Lemon 
172f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
173f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
174f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
17548e417ebSJonathan Lemon static void		fxp_powerstate_d0(device_t dev);
176f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
177f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
178f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
179f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
180f7788e8eSJonathan Lemon 			    caddr_t data);
181f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
182f7788e8eSJonathan Lemon static int		fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
18309882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
184f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
185f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
186f7788e8eSJonathan Lemon 			    int autosize);
18700c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
18800c4116bSJonathan Lemon 			    u_int16_t data);
189f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
190f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
191f7788e8eSJonathan Lemon 			    int offset, int words);
19200c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
19300c4116bSJonathan Lemon 			    int offset, int words);
194f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
195f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
196f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
197f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
198f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
199f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
200f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
201f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
202f7788e8eSJonathan Lemon 			    int value);
20372a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
20472a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
20572a32a26SJonathan Lemon 			    int low, int high);
20672a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
20772a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
208f7788e8eSJonathan Lemon static __inline void	fxp_lwcopy(volatile u_int32_t *src,
209f7788e8eSJonathan Lemon 			    volatile u_int32_t *dst);
210f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2112e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
212f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
213f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
214f7788e8eSJonathan Lemon 
215f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
216f7788e8eSJonathan Lemon 	/* Device interface */
217f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
218f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
219f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
220f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
221f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
222f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
223f7788e8eSJonathan Lemon 
224f7788e8eSJonathan Lemon 	/* MII interface */
225f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
226f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
227f7788e8eSJonathan Lemon 
228f7788e8eSJonathan Lemon 	{ 0, 0 }
229f7788e8eSJonathan Lemon };
230f7788e8eSJonathan Lemon 
231f7788e8eSJonathan Lemon static driver_t fxp_driver = {
232f7788e8eSJonathan Lemon 	"fxp",
233f7788e8eSJonathan Lemon 	fxp_methods,
234f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
235f7788e8eSJonathan Lemon };
236f7788e8eSJonathan Lemon 
237f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
238f7788e8eSJonathan Lemon 
239f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
240f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
241f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
242f7788e8eSJonathan Lemon 
243f7788e8eSJonathan Lemon /*
244ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
245ba8c6fd5SDavid Greenman  */
246ba8c6fd5SDavid Greenman static __inline void
247f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
248ba8c6fd5SDavid Greenman {
249aed53495SDavid Greenman #ifdef __i386__
250aed53495SDavid Greenman 	*dst = *src;
251aed53495SDavid Greenman #else
252fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
253fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
254ba8c6fd5SDavid Greenman 
255ba8c6fd5SDavid Greenman 	b[0] = a[0];
256ba8c6fd5SDavid Greenman 	b[1] = a[1];
257aed53495SDavid Greenman #endif
258ba8c6fd5SDavid Greenman }
259a17c678eSDavid Greenman 
260a17c678eSDavid Greenman /*
261dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
262dfe61cf1SDavid Greenman  * completed).
263dfe61cf1SDavid Greenman  */
264c1087c13SBruce Evans static __inline void
265f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
266a17c678eSDavid Greenman {
267a17c678eSDavid Greenman 	int i = 10000;
268a17c678eSDavid Greenman 
2697dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2707dced78aSDavid Greenman 		DELAY(2);
2717dced78aSDavid Greenman 	if (i == 0)
27200c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
273e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
274e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
275e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
276e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2777dced78aSDavid Greenman }
2787dced78aSDavid Greenman 
2797dced78aSDavid Greenman static __inline void
2802e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2812e2b8238SJonathan Lemon {
2822e2b8238SJonathan Lemon 
2832e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2842e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2852e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2862e2b8238SJonathan Lemon 	}
2872e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2882e2b8238SJonathan Lemon }
2892e2b8238SJonathan Lemon 
2902e2b8238SJonathan Lemon static __inline void
291f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
2927dced78aSDavid Greenman {
2937dced78aSDavid Greenman 	int i = 10000;
2947dced78aSDavid Greenman 
2957dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
2967dced78aSDavid Greenman 		DELAY(2);
2977dced78aSDavid Greenman 	if (i == 0)
298f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
299a17c678eSDavid Greenman }
300a17c678eSDavid Greenman 
301dfe61cf1SDavid Greenman /*
302dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
303dfe61cf1SDavid Greenman  */
3046182fdbdSPeter Wemm static int
3056182fdbdSPeter Wemm fxp_probe(device_t dev)
306a17c678eSDavid Greenman {
307f7788e8eSJonathan Lemon 	u_int16_t devid;
308f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
309f7788e8eSJonathan Lemon 
31055ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
311f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
312f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
313f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
314f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
315f7788e8eSJonathan Lemon 				return (0);
31655ce7b51SDavid Greenman 			}
317dd68ef16SPeter Wemm 		}
318f7788e8eSJonathan Lemon 	}
319f7788e8eSJonathan Lemon 	return (ENXIO);
3206182fdbdSPeter Wemm }
3216182fdbdSPeter Wemm 
32248e417ebSJonathan Lemon static void
32348e417ebSJonathan Lemon fxp_powerstate_d0(device_t dev)
32448e417ebSJonathan Lemon {
32548e417ebSJonathan Lemon #if __FreeBSD_version >= 430002
32648e417ebSJonathan Lemon 	u_int32_t iobase, membase, irq;
32748e417ebSJonathan Lemon 
32848e417ebSJonathan Lemon 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
32948e417ebSJonathan Lemon 		/* Save important PCI config data. */
33048e417ebSJonathan Lemon 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
33148e417ebSJonathan Lemon 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
33248e417ebSJonathan Lemon 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
33348e417ebSJonathan Lemon 
33448e417ebSJonathan Lemon 		/* Reset the power state. */
33548e417ebSJonathan Lemon 		device_printf(dev, "chip is in D%d power mode "
33648e417ebSJonathan Lemon 		    "-- setting to D0\n", pci_get_powerstate(dev));
33748e417ebSJonathan Lemon 
33848e417ebSJonathan Lemon 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
33948e417ebSJonathan Lemon 
34048e417ebSJonathan Lemon 		/* Restore PCI config data. */
34148e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
34248e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
34348e417ebSJonathan Lemon 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
34448e417ebSJonathan Lemon 	}
34548e417ebSJonathan Lemon #endif
34648e417ebSJonathan Lemon }
34748e417ebSJonathan Lemon 
3486182fdbdSPeter Wemm static int
3496182fdbdSPeter Wemm fxp_attach(device_t dev)
350a17c678eSDavid Greenman {
3516182fdbdSPeter Wemm 	int error = 0;
3526182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
353ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
3549fa6ccfbSMatt Jacob 	u_int32_t val;
355f7788e8eSJonathan Lemon 	u_int16_t data;
356f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
357f7788e8eSJonathan Lemon 	int s;
358a17c678eSDavid Greenman 
359f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
360f7788e8eSJonathan Lemon 	sc->dev = dev;
3616c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
362f7788e8eSJonathan Lemon 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
363a17c678eSDavid Greenman 
364f7788e8eSJonathan Lemon 	s = splimp();
365a17c678eSDavid Greenman 
366dfe61cf1SDavid Greenman 	/*
3679fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3689fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
369df373873SWes Peters 	 */
3706182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
371df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3726182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3739fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
374df373873SWes Peters 
37548e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
3768d799694SBill Paul 
377df373873SWes Peters 	/*
3789fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3799fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3809fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
381dfe61cf1SDavid Greenman 	 */
3829fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3839fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3842a05a4ebSMatt Jacob 	prefer_iomap = 0;
3852a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3862a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3879fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3889fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
3899fa6ccfbSMatt Jacob 	}
3909fa6ccfbSMatt Jacob 
3919fa6ccfbSMatt Jacob 	if (val & m1) {
3929fa6ccfbSMatt Jacob 		sc->rtp =
3939fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3949fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3959fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3966182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
3979fa6ccfbSMatt Jacob 	}
3989fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
3999fa6ccfbSMatt Jacob 		sc->rtp =
4009fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4019fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4029fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4039fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
4049fa6ccfbSMatt Jacob 	}
4059fa6ccfbSMatt Jacob 
4066182fdbdSPeter Wemm 	if (!sc->mem) {
4079fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
4086182fdbdSPeter Wemm 		error = ENXIO;
409a17c678eSDavid Greenman 		goto fail;
410a17c678eSDavid Greenman         }
4119fa6ccfbSMatt Jacob 	if (bootverbose) {
4129fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4139fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4149fa6ccfbSMatt Jacob 	}
4154fc1dda9SAndrew Gallatin 
4164fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4174fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
418a17c678eSDavid Greenman 
419a17c678eSDavid Greenman 	/*
420dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
421dfe61cf1SDavid Greenman 	 */
4226182fdbdSPeter Wemm 	rid = 0;
4236182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4246182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4256182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4266182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4276182fdbdSPeter Wemm 		error = ENXIO;
4286182fdbdSPeter Wemm 		goto fail;
4296182fdbdSPeter Wemm 	}
4306182fdbdSPeter Wemm 
431566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
432566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
4336182fdbdSPeter Wemm 	if (error) {
4346182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
435a17c678eSDavid Greenman 		goto fail;
436a17c678eSDavid Greenman 	}
437a17c678eSDavid Greenman 
438f7788e8eSJonathan Lemon 	/*
439f7788e8eSJonathan Lemon 	 * Reset to a stable state.
440f7788e8eSJonathan Lemon 	 */
441f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
442f7788e8eSJonathan Lemon 	DELAY(10);
443f7788e8eSJonathan Lemon 
444f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
445f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
446f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
447f7788e8eSJonathan Lemon 		goto failmem;
448f7788e8eSJonathan Lemon 
449f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
450f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
451f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
452f7788e8eSJonathan Lemon 		goto failmem;
453f7788e8eSJonathan Lemon 
454f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
455f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
456f7788e8eSJonathan Lemon 		goto failmem;
457f7788e8eSJonathan Lemon 
458f7788e8eSJonathan Lemon 	/*
459f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
460f7788e8eSJonathan Lemon 	 */
461f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
462f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
463f7788e8eSJonathan Lemon 			goto failmem;
464f7788e8eSJonathan Lemon 		}
465f7788e8eSJonathan Lemon 	}
466f7788e8eSJonathan Lemon 
467f7788e8eSJonathan Lemon 	/*
468f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
469f7788e8eSJonathan Lemon 	 */
470f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
471f7788e8eSJonathan Lemon 
472f7788e8eSJonathan Lemon 	/*
4733bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
474f7788e8eSJonathan Lemon 	 */
475f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
476f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
477f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
478dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
479f7788e8eSJonathan Lemon 
480f7788e8eSJonathan Lemon 	/*
48172a32a26SJonathan Lemon 	 * Create the sysctl tree
48272a32a26SJonathan Lemon 	 */
48372a32a26SJonathan Lemon 	sysctl_ctx_init(&sc->sysctl_ctx);
48472a32a26SJonathan Lemon 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
48572a32a26SJonathan Lemon 	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
48672a32a26SJonathan Lemon 	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
48772a32a26SJonathan Lemon 	if (sc->sysctl_tree == NULL)
48872a32a26SJonathan Lemon 		goto fail;
48972a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
49072a32a26SJonathan Lemon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
49172a32a26SJonathan Lemon 	    &sc->tunable_int_delay, 0, &sysctl_hw_fxp_int_delay, "I",
49272a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
49372a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
49472a32a26SJonathan Lemon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
49572a32a26SJonathan Lemon 	    &sc->tunable_bundle_max, 0, &sysctl_hw_fxp_bundle_max, "I",
49672a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
49772a32a26SJonathan Lemon 
49872a32a26SJonathan Lemon 	/*
49972a32a26SJonathan Lemon 	 * Pull in device tunables.
50072a32a26SJonathan Lemon 	 */
50172a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
50272a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
50372a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50472a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
50572a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50672a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
50772a32a26SJonathan Lemon 
50872a32a26SJonathan Lemon 	/*
50972a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
5103bd07cfdSJonathan Lemon 	 */
5113bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
5123bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
51372a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
51472a32a26SJonathan Lemon 	else
51572a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
5163bd07cfdSJonathan Lemon 
5173bd07cfdSJonathan Lemon 	/*
5182e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
51900c4116bSJonathan Lemon 	 *
52072a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
52172a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
52272a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
52300c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
52400c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
52500c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
52600c4116bSJonathan Lemon 	 *
52700c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5282e2b8238SJonathan Lemon 	 */
5292e2b8238SJonathan Lemon 	i = pci_get_device(dev);
53072a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
53172a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
53200c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
53300c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
53400c4116bSJonathan Lemon 			u_int16_t cksum;
53500c4116bSJonathan Lemon 			int i;
53600c4116bSJonathan Lemon 
53700c4116bSJonathan Lemon 			device_printf(dev,
538001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
53900c4116bSJonathan Lemon 			data &= ~0x02;
54000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
54100c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
54200c4116bSJonathan Lemon 			cksum = 0;
54300c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
54400c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
54500c4116bSJonathan Lemon 				cksum += data;
54600c4116bSJonathan Lemon 			}
54700c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
54800c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
54900c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
55000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
55100c4116bSJonathan Lemon 			device_printf(dev,
55200c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
55300c4116bSJonathan Lemon 			    i, data, cksum);
55400c4116bSJonathan Lemon #if 1
55500c4116bSJonathan Lemon 			/*
55600c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
55700c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
55800c4116bSJonathan Lemon 			 */
5592e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
56000c4116bSJonathan Lemon #endif
56100c4116bSJonathan Lemon 		}
56200c4116bSJonathan Lemon 	}
5632e2b8238SJonathan Lemon 
5642e2b8238SJonathan Lemon 	/*
5653bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5663bd07cfdSJonathan Lemon 	 */
56772a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5683bd07cfdSJonathan Lemon 		/*
56974396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
57074396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
57174396a0aSJonathan Lemon 		 * the board to turn on MWI.
5723bd07cfdSJonathan Lemon 		 */
57374396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
57474396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5753bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5763bd07cfdSJonathan Lemon 
5773bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5783bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
579920b58e8SBrooks Davis 
580e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
581e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
5823bd07cfdSJonathan Lemon 	}
5833bd07cfdSJonathan Lemon 
5843bd07cfdSJonathan Lemon 	/*
585f7788e8eSJonathan Lemon 	 * Read MAC address.
586f7788e8eSJonathan Lemon 	 */
587f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
588f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
589f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
590f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
591f7788e8eSJonathan Lemon 	if (bootverbose) {
5922e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
593f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
5942e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
5952e2b8238SJonathan Lemon 		    pci_get_revid(dev));
59672a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
59772a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
59872a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
599f7788e8eSJonathan Lemon 	}
600f7788e8eSJonathan Lemon 
601f7788e8eSJonathan Lemon 	/*
602f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
603f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
604f7788e8eSJonathan Lemon 	 *
605f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
606f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
607f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
608f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
609f7788e8eSJonathan Lemon 	 */
610f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
611f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
612f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
613f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
614f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
615f7788e8eSJonathan Lemon 	} else {
616f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
617f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
618f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
6196182fdbdSPeter Wemm 			error = ENXIO;
620ba8c6fd5SDavid Greenman 			goto fail;
621a17c678eSDavid Greenman 		}
622f7788e8eSJonathan Lemon 	}
623dccee1a1SDavid Greenman 
624a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
6256182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
626a17c678eSDavid Greenman 	ifp->if_name = "fxp";
627a17c678eSDavid Greenman 	ifp->if_output = ether_output;
628a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
629fb583156SDavid Greenman 	ifp->if_init = fxp_init;
630ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
631ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
632ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
633ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
634ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
635a17c678eSDavid Greenman 
636dfe61cf1SDavid Greenman 	/*
637dfe61cf1SDavid Greenman 	 * Attach the interface.
638dfe61cf1SDavid Greenman 	 */
63921b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
640f7788e8eSJonathan Lemon 
641e8c8b728SJonathan Lemon 	/*
642e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
643e8c8b728SJonathan Lemon 	 */
644e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
645e8c8b728SJonathan Lemon 
646483b9871SDavid Greenman 	/*
6473114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
6483114fdb4SDavid Greenman 	 * TX descriptors.
649483b9871SDavid Greenman 	 */
6503114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
6514a684684SDavid Greenman 
652f7788e8eSJonathan Lemon 	splx(s);
653f7788e8eSJonathan Lemon 	return (0);
654a17c678eSDavid Greenman 
655f7788e8eSJonathan Lemon failmem:
656f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
657f7788e8eSJonathan Lemon 	error = ENOMEM;
658a17c678eSDavid Greenman fail:
659f7788e8eSJonathan Lemon 	splx(s);
660f7788e8eSJonathan Lemon 	fxp_release(sc);
661f7788e8eSJonathan Lemon 	return (error);
662f7788e8eSJonathan Lemon }
663f7788e8eSJonathan Lemon 
664f7788e8eSJonathan Lemon /*
665f7788e8eSJonathan Lemon  * release all resources
666f7788e8eSJonathan Lemon  */
667f7788e8eSJonathan Lemon static void
668f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
669f7788e8eSJonathan Lemon {
670f7788e8eSJonathan Lemon 
671f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
6723bd07cfdSJonathan Lemon 	if (sc->miibus)
673f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
674f7788e8eSJonathan Lemon 
675f7788e8eSJonathan Lemon 	if (sc->cbl_base)
676f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
677f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
678f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
679f7788e8eSJonathan Lemon 	if (sc->mcsp)
680f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
681f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
682f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
683f7788e8eSJonathan Lemon 
684f7788e8eSJonathan Lemon 	if (sc->ih)
685f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
686f7788e8eSJonathan Lemon 	if (sc->irq)
687f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
688f7788e8eSJonathan Lemon 	if (sc->mem)
689f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
69072a32a26SJonathan Lemon 
69172a32a26SJonathan Lemon         sysctl_ctx_free(&sc->sysctl_ctx);
69272a32a26SJonathan Lemon 
6930f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
6946182fdbdSPeter Wemm }
6956182fdbdSPeter Wemm 
6966182fdbdSPeter Wemm /*
6976182fdbdSPeter Wemm  * Detach interface.
6986182fdbdSPeter Wemm  */
6996182fdbdSPeter Wemm static int
7006182fdbdSPeter Wemm fxp_detach(device_t dev)
7016182fdbdSPeter Wemm {
7026182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
703f7788e8eSJonathan Lemon 	int s;
7046182fdbdSPeter Wemm 
7052e2b8238SJonathan Lemon 	/* disable interrupts */
7062e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
7072e2b8238SJonathan Lemon 
708f7788e8eSJonathan Lemon 	s = splimp();
7096182fdbdSPeter Wemm 
7106182fdbdSPeter Wemm 	/*
7116182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
7126182fdbdSPeter Wemm 	 */
7136182fdbdSPeter Wemm 	fxp_stop(sc);
7146182fdbdSPeter Wemm 
7156182fdbdSPeter Wemm 	/*
716f7788e8eSJonathan Lemon 	 * Close down routes etc.
7176182fdbdSPeter Wemm 	 */
718f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
7196182fdbdSPeter Wemm 
7206182fdbdSPeter Wemm 	/*
7216182fdbdSPeter Wemm 	 * Free all media structures.
7226182fdbdSPeter Wemm 	 */
7236182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
7246182fdbdSPeter Wemm 
725f7788e8eSJonathan Lemon 	splx(s);
7266182fdbdSPeter Wemm 
727f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
728f7788e8eSJonathan Lemon 	fxp_release(sc);
7296182fdbdSPeter Wemm 
730f7788e8eSJonathan Lemon 	return (0);
731a17c678eSDavid Greenman }
732a17c678eSDavid Greenman 
733a17c678eSDavid Greenman /*
7344a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
735a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
736a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
737a17c678eSDavid Greenman  */
7386182fdbdSPeter Wemm static int
7396182fdbdSPeter Wemm fxp_shutdown(device_t dev)
740a17c678eSDavid Greenman {
7416182fdbdSPeter Wemm 	/*
7426182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
7436182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
7446182fdbdSPeter Wemm 	 * reboot before the driver initializes.
7456182fdbdSPeter Wemm 	 */
7466182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
747f7788e8eSJonathan Lemon 	return (0);
748a17c678eSDavid Greenman }
749a17c678eSDavid Greenman 
7507dced78aSDavid Greenman /*
7517dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
7527dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
7537dced78aSDavid Greenman  * resume.
7547dced78aSDavid Greenman  */
7557dced78aSDavid Greenman static int
7567dced78aSDavid Greenman fxp_suspend(device_t dev)
7577dced78aSDavid Greenman {
7587dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
759f7788e8eSJonathan Lemon 	int i, s;
7607dced78aSDavid Greenman 
761f7788e8eSJonathan Lemon 	s = splimp();
7627dced78aSDavid Greenman 
7637dced78aSDavid Greenman 	fxp_stop(sc);
7647dced78aSDavid Greenman 
7657dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
7667dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
7677dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
7687dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
7697dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
7707dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
7717dced78aSDavid Greenman 
7727dced78aSDavid Greenman 	sc->suspended = 1;
7737dced78aSDavid Greenman 
774f7788e8eSJonathan Lemon 	splx(s);
775f7788e8eSJonathan Lemon 	return (0);
7767dced78aSDavid Greenman }
7777dced78aSDavid Greenman 
7787dced78aSDavid Greenman /*
7797dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
7807dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
7817dced78aSDavid Greenman  * appropriate.
7827dced78aSDavid Greenman  */
7837dced78aSDavid Greenman static int
7847dced78aSDavid Greenman fxp_resume(device_t dev)
7857dced78aSDavid Greenman {
7867dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
7877dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
7887dced78aSDavid Greenman 	u_int16_t pci_command;
789f7788e8eSJonathan Lemon 	int i, s;
7907dced78aSDavid Greenman 
791f7788e8eSJonathan Lemon 	s = splimp();
7927dced78aSDavid Greenman 
79348e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
79448e417ebSJonathan Lemon 
7957dced78aSDavid Greenman 	/* better way to do this? */
7967dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
7977dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
7987dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
7997dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
8007dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
8017dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
8027dced78aSDavid Greenman 
8037dced78aSDavid Greenman 	/* reenable busmastering */
8047dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
8057dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
8067dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
8077dced78aSDavid Greenman 
8087dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
8097dced78aSDavid Greenman 	DELAY(10);
8107dced78aSDavid Greenman 
8117dced78aSDavid Greenman 	/* reinitialize interface if necessary */
8127dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
8137dced78aSDavid Greenman 		fxp_init(sc);
8147dced78aSDavid Greenman 
8157dced78aSDavid Greenman 	sc->suspended = 0;
8167dced78aSDavid Greenman 
817f7788e8eSJonathan Lemon 	splx(s);
818ba8c6fd5SDavid Greenman 	return (0);
819f7788e8eSJonathan Lemon }
820ba8c6fd5SDavid Greenman 
82100c4116bSJonathan Lemon static void
82200c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
82300c4116bSJonathan Lemon {
82400c4116bSJonathan Lemon 	u_int16_t reg;
82500c4116bSJonathan Lemon 	int x;
82600c4116bSJonathan Lemon 
82700c4116bSJonathan Lemon 	/*
82800c4116bSJonathan Lemon 	 * Shift in data.
82900c4116bSJonathan Lemon 	 */
83000c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
83100c4116bSJonathan Lemon 		if (data & x)
83200c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
83300c4116bSJonathan Lemon 		else
83400c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
83500c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
83600c4116bSJonathan Lemon 		DELAY(1);
83700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
83800c4116bSJonathan Lemon 		DELAY(1);
83900c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
84000c4116bSJonathan Lemon 		DELAY(1);
84100c4116bSJonathan Lemon 	}
84200c4116bSJonathan Lemon }
84300c4116bSJonathan Lemon 
844f7788e8eSJonathan Lemon /*
845f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
846f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
847f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
848f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
849f7788e8eSJonathan Lemon  * every 16 bits of data.
850f7788e8eSJonathan Lemon  */
851f7788e8eSJonathan Lemon static u_int16_t
852f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
853f7788e8eSJonathan Lemon {
854f7788e8eSJonathan Lemon 	u_int16_t reg, data;
855f7788e8eSJonathan Lemon 	int x;
856ba8c6fd5SDavid Greenman 
857f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
858f7788e8eSJonathan Lemon 	/*
859f7788e8eSJonathan Lemon 	 * Shift in read opcode.
860f7788e8eSJonathan Lemon 	 */
86100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
862f7788e8eSJonathan Lemon 	/*
863f7788e8eSJonathan Lemon 	 * Shift in address.
864f7788e8eSJonathan Lemon 	 */
865f7788e8eSJonathan Lemon 	data = 0;
866f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
867f7788e8eSJonathan Lemon 		if (offset & x)
868f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
869f7788e8eSJonathan Lemon 		else
870f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
871f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
872f7788e8eSJonathan Lemon 		DELAY(1);
873f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
874f7788e8eSJonathan Lemon 		DELAY(1);
875f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
876f7788e8eSJonathan Lemon 		DELAY(1);
877f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
878f7788e8eSJonathan Lemon 		data++;
879f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
880f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
881f7788e8eSJonathan Lemon 			break;
882f7788e8eSJonathan Lemon 		}
883f7788e8eSJonathan Lemon 	}
884f7788e8eSJonathan Lemon 	/*
885f7788e8eSJonathan Lemon 	 * Shift out data.
886f7788e8eSJonathan Lemon 	 */
887f7788e8eSJonathan Lemon 	data = 0;
888f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
889f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
890f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
891f7788e8eSJonathan Lemon 		DELAY(1);
892f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
893f7788e8eSJonathan Lemon 			data |= x;
894f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
895f7788e8eSJonathan Lemon 		DELAY(1);
896f7788e8eSJonathan Lemon 	}
897f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
898f7788e8eSJonathan Lemon 	DELAY(1);
899f7788e8eSJonathan Lemon 
900f7788e8eSJonathan Lemon 	return (data);
901ba8c6fd5SDavid Greenman }
902ba8c6fd5SDavid Greenman 
90300c4116bSJonathan Lemon static void
90400c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
90500c4116bSJonathan Lemon {
90600c4116bSJonathan Lemon 	int i;
90700c4116bSJonathan Lemon 
90800c4116bSJonathan Lemon 	/*
90900c4116bSJonathan Lemon 	 * Erase/write enable.
91000c4116bSJonathan Lemon 	 */
91100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
91200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
91300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
91400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
91500c4116bSJonathan Lemon 	DELAY(1);
91600c4116bSJonathan Lemon 	/*
91700c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
91800c4116bSJonathan Lemon 	 */
91900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
92000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
92100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
92200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
92300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
92400c4116bSJonathan Lemon 	DELAY(1);
92500c4116bSJonathan Lemon 	/*
92600c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
92700c4116bSJonathan Lemon 	 */
92800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
92900c4116bSJonathan Lemon 	DELAY(1);
93000c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
93100c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
93200c4116bSJonathan Lemon 			break;
93300c4116bSJonathan Lemon 		DELAY(50);
93400c4116bSJonathan Lemon 	}
93500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
93600c4116bSJonathan Lemon 	DELAY(1);
93700c4116bSJonathan Lemon 	/*
93800c4116bSJonathan Lemon 	 * Erase/write disable.
93900c4116bSJonathan Lemon 	 */
94000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
94100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
94200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
94300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
94400c4116bSJonathan Lemon 	DELAY(1);
94500c4116bSJonathan Lemon }
94600c4116bSJonathan Lemon 
947ba8c6fd5SDavid Greenman /*
948e9bf2fa7SDavid Greenman  * From NetBSD:
949e9bf2fa7SDavid Greenman  *
950e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
951e9bf2fa7SDavid Greenman  *
952e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
953e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
954e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
955e9bf2fa7SDavid Greenman  *
956e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
957e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
958e9bf2fa7SDavid Greenman  *
959e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
960e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
961e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
962e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
963e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
964e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
965e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
966e9bf2fa7SDavid Greenman  */
967e9bf2fa7SDavid Greenman static void
968f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
969e9bf2fa7SDavid Greenman {
970e9bf2fa7SDavid Greenman 
971f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
972f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
973f7788e8eSJonathan Lemon 
974f7788e8eSJonathan Lemon 	/* autosize */
975f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
976e9bf2fa7SDavid Greenman }
977f7788e8eSJonathan Lemon 
978ba8c6fd5SDavid Greenman static void
979f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
980ba8c6fd5SDavid Greenman {
981f7788e8eSJonathan Lemon 	int i;
982ba8c6fd5SDavid Greenman 
983f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
984f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
985ba8c6fd5SDavid Greenman }
986ba8c6fd5SDavid Greenman 
98700c4116bSJonathan Lemon static void
98800c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
98900c4116bSJonathan Lemon {
99000c4116bSJonathan Lemon 	int i;
99100c4116bSJonathan Lemon 
99200c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
99300c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
99400c4116bSJonathan Lemon }
99500c4116bSJonathan Lemon 
996a17c678eSDavid Greenman /*
997a17c678eSDavid Greenman  * Start packet transmission on the interface.
998a17c678eSDavid Greenman  */
999a17c678eSDavid Greenman static void
1000f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1001a17c678eSDavid Greenman {
10029b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1003a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1004a17c678eSDavid Greenman 
1005a17c678eSDavid Greenman 	/*
1006483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1007483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1008483b9871SDavid Greenman 	 * of the command chain).
1009a17c678eSDavid Greenman 	 */
10100f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1011a17c678eSDavid Greenman 		return;
10120f4dc94cSChuck Paterson 	}
10131cd443acSDavid Greenman 
1014483b9871SDavid Greenman 	txp = NULL;
1015483b9871SDavid Greenman 
1016483b9871SDavid Greenman 	/*
1017483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1018483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
10193114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
10203114fdb4SDavid Greenman 	 *       a NOP command when needed.
1021483b9871SDavid Greenman 	 */
10223114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1023483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
1024483b9871SDavid Greenman 		int segment;
1025483b9871SDavid Greenman 
1026dfe61cf1SDavid Greenman 		/*
1027dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1028dfe61cf1SDavid Greenman 		 */
10296318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
1030a17c678eSDavid Greenman 
1031dfe61cf1SDavid Greenman 		/*
1032483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1033dfe61cf1SDavid Greenman 		 */
1034a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
1035a17c678eSDavid Greenman 
1036a17c678eSDavid Greenman 		/*
1037a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1038483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1039a17c678eSDavid Greenman 		 * and size of the mbuf.
1040a17c678eSDavid Greenman 		 */
104123a0ed7cSDavid Greenman tbdinit:
1042a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
1043a17c678eSDavid Greenman 			if (m->m_len != 0) {
1044a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1045a17c678eSDavid Greenman 					break;
1046a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1047a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1048a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1049a17c678eSDavid Greenman 				segment++;
1050a17c678eSDavid Greenman 			}
1051a17c678eSDavid Greenman 		}
1052fb583156SDavid Greenman 		if (m != NULL) {
105323a0ed7cSDavid Greenman 			struct mbuf *mn;
105423a0ed7cSDavid Greenman 
1055a17c678eSDavid Greenman 			/*
10563bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
10573bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
10583bd07cfdSJonathan Lemon 			 * new buffers.
1059a17c678eSDavid Greenman 			 */
106023a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
106123a0ed7cSDavid Greenman 			if (mn == NULL) {
106223a0ed7cSDavid Greenman 				m_freem(mb_head);
1063483b9871SDavid Greenman 				break;
1064a17c678eSDavid Greenman 			}
106523a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
106623a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
106723a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
106823a0ed7cSDavid Greenman 					m_freem(mn);
106923a0ed7cSDavid Greenman 					m_freem(mb_head);
1070483b9871SDavid Greenman 					break;
107123a0ed7cSDavid Greenman 				}
107223a0ed7cSDavid Greenman 			}
1073ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1074ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
107523a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
107623a0ed7cSDavid Greenman 			m_freem(mb_head);
107723a0ed7cSDavid Greenman 			mb_head = mn;
107823a0ed7cSDavid Greenman 			goto tbdinit;
107923a0ed7cSDavid Greenman 		}
108023a0ed7cSDavid Greenman 
108123a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10821cd443acSDavid Greenman 		txp->mb_head = mb_head;
1083a17c678eSDavid Greenman 		txp->cb_status = 0;
10843114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1085a17c678eSDavid Greenman 			txp->cb_command =
10863bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10873bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
10883114fdb4SDavid Greenman 		} else {
10893114fdb4SDavid Greenman 			txp->cb_command =
10903bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
10913bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
10923114fdb4SDavid Greenman 			/*
10933bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
10943bd07cfdSJonathan Lemon 			 * from the card again.
10953114fdb4SDavid Greenman 			 */
10963114fdb4SDavid Greenman 			ifp->if_timer = 5;
10973114fdb4SDavid Greenman 		}
1098f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1099a17c678eSDavid Greenman 
1100a17c678eSDavid Greenman 		/*
1101483b9871SDavid Greenman 		 * Advance the end of list forward.
1102a17c678eSDavid Greenman 		 */
110306175228SAndrew Gallatin 
110406175228SAndrew Gallatin #ifdef __alpha__
110506175228SAndrew Gallatin 		/*
110606175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
110706175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
110806175228SAndrew Gallatin 		 * up the status while we update the command field.
110906175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
111006175228SAndrew Gallatin 		 */
111106175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
111206175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
111306175228SAndrew Gallatin #else
1114a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
111506175228SAndrew Gallatin #endif /*__alpha__*/
1116a17c678eSDavid Greenman 		sc->cbl_last = txp;
1117a17c678eSDavid Greenman 
1118a17c678eSDavid Greenman 		/*
11191cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
11201cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1121483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1122a17c678eSDavid Greenman 		 */
11231cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1124a17c678eSDavid Greenman 			sc->cbl_first = txp;
1125a17c678eSDavid Greenman 
11261cd443acSDavid Greenman 		sc->tx_queued++;
11271cd443acSDavid Greenman 
1128a17c678eSDavid Greenman 		/*
1129a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1130a17c678eSDavid Greenman 		 */
1131fb583156SDavid Greenman 		if (ifp->if_bpf)
113294927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
1133483b9871SDavid Greenman 	}
1134483b9871SDavid Greenman 
1135483b9871SDavid Greenman 	/*
1136483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1137483b9871SDavid Greenman 	 * going again if suspended.
1138483b9871SDavid Greenman 	 */
1139483b9871SDavid Greenman 	if (txp != NULL) {
1140483b9871SDavid Greenman 		fxp_scb_wait(sc);
11412e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1142483b9871SDavid Greenman 	}
1143a17c678eSDavid Greenman }
1144a17c678eSDavid Greenman 
1145e4fc250cSLuigi Rizzo static void fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count);
1146e4fc250cSLuigi Rizzo 
1147e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1148e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1149e4fc250cSLuigi Rizzo 
1150e4fc250cSLuigi Rizzo static void
1151e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1152e4fc250cSLuigi Rizzo {
1153e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
1154e4fc250cSLuigi Rizzo 	u_int8_t statack;
1155e4fc250cSLuigi Rizzo 
1156e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1157e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1158e4fc250cSLuigi Rizzo 		return;
1159e4fc250cSLuigi Rizzo 	}
1160e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1161e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1162e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
1163e4fc250cSLuigi Rizzo 		u_int8_t tmp;
11646481f301SPeter Wemm 
1165e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1166e4fc250cSLuigi Rizzo 		if (tmp == 0xff || tmp == 0)
1167e4fc250cSLuigi Rizzo 			return; /* nothing to do */
1168e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1169e4fc250cSLuigi Rizzo 		/* ack what we can */
1170e4fc250cSLuigi Rizzo 		if (tmp != 0)
1171e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1172e4fc250cSLuigi Rizzo 		statack |= tmp;
1173e4fc250cSLuigi Rizzo 	}
1174e4fc250cSLuigi Rizzo 	fxp_intr_body(sc, statack, count);
1175e4fc250cSLuigi Rizzo }
1176e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1177e4fc250cSLuigi Rizzo 
1178a17c678eSDavid Greenman /*
11799c7d2607SDavid Greenman  * Process interface interrupts.
1180a17c678eSDavid Greenman  */
118194927790SDavid Greenman static void
1182f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1183a17c678eSDavid Greenman {
1184f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
11851cd443acSDavid Greenman 	u_int8_t statack;
11860f4dc94cSChuck Paterson 
1187e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1188e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
1189e4fc250cSLuigi Rizzo 
1190e4fc250cSLuigi Rizzo 	if (ifp->if_ipending & IFF_POLLING)
1191e4fc250cSLuigi Rizzo 		return;
1192e4fc250cSLuigi Rizzo 	if (ether_poll_register(fxp_poll, ifp)) {
1193e4fc250cSLuigi Rizzo 		/* disable interrupts */
1194e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
1195e4fc250cSLuigi Rizzo 		fxp_poll(ifp, 0, 1);
1196e4fc250cSLuigi Rizzo 		return;
1197e4fc250cSLuigi Rizzo 	}
1198e4fc250cSLuigi Rizzo #endif
1199e4fc250cSLuigi Rizzo 
1200b184b38eSDavid Greenman 	if (sc->suspended) {
1201b184b38eSDavid Greenman 		return;
1202b184b38eSDavid Greenman 	}
1203b184b38eSDavid Greenman 
1204b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1205a17c678eSDavid Greenman 		/*
120611457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
120711457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
120811457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
120911457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
121011457bbfSJonathan Lemon 		 */
121111457bbfSJonathan Lemon 		if (statack == 0xff)
121211457bbfSJonathan Lemon 			return;
121311457bbfSJonathan Lemon 
121411457bbfSJonathan Lemon 		/*
1215a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1216a17c678eSDavid Greenman 		 */
1217ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1218e4fc250cSLuigi Rizzo 		fxp_intr_body(sc, statack, -1);
1219e4fc250cSLuigi Rizzo 	}
1220e4fc250cSLuigi Rizzo }
1221e4fc250cSLuigi Rizzo 
1222e4fc250cSLuigi Rizzo static void
1223e4fc250cSLuigi Rizzo fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count)
1224e4fc250cSLuigi Rizzo {
1225e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
1226a17c678eSDavid Greenman 
1227a17c678eSDavid Greenman 		/*
12283114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
122906936301SBill Paul 		 *
123006936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
123106936301SBill Paul 		 * be that this event (control unit not ready) was not
123206936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
123306936301SBill Paul 		 * The exact sequence of events that occur when the interface
123406936301SBill Paul 		 * is brought up are different now, and if this event
123506936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
123606936301SBill Paul 		 * can stall for several seconds. The result is that no
123706936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
123806936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
12393114fdb4SDavid Greenman 		 */
124006936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
12413114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
12423114fdb4SDavid Greenman 
12433114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
12443114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
12453114fdb4SDavid Greenman 			    txp = txp->next) {
12463114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
12473114fdb4SDavid Greenman 					m_freem(txp->mb_head);
12483114fdb4SDavid Greenman 					txp->mb_head = NULL;
12493114fdb4SDavid Greenman 				}
12503114fdb4SDavid Greenman 				sc->tx_queued--;
12513114fdb4SDavid Greenman 			}
12523114fdb4SDavid Greenman 			sc->cbl_first = txp;
12533114fdb4SDavid Greenman 			ifp->if_timer = 0;
12543114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
12553114fdb4SDavid Greenman 				if (sc->need_mcsetup)
12563114fdb4SDavid Greenman 					fxp_mc_setup(sc);
12573114fdb4SDavid Greenman 			}
12583114fdb4SDavid Greenman 			/*
12593114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
12603114fdb4SDavid Greenman 			 */
12613114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
12623114fdb4SDavid Greenman 				fxp_start(ifp);
12633114fdb4SDavid Greenman 		}
12643114fdb4SDavid Greenman 		/*
1265a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1266a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1267a17c678eSDavid Greenman 		 * re-start the receiver.
1268a17c678eSDavid Greenman 		 */
1269a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1270a17c678eSDavid Greenman 			struct mbuf *m;
1271a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1272a17c678eSDavid Greenman rcvloop:
1273a17c678eSDavid Greenman 			m = sc->rfa_headm;
1274ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1275ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1276a17c678eSDavid Greenman 
1277e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1278e4fc250cSLuigi Rizzo 			if (count < 0 || count-- > 0)
1279e4fc250cSLuigi Rizzo #endif
1280a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1281dfe61cf1SDavid Greenman 				/*
1282dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1283dfe61cf1SDavid Greenman 				 */
1284a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1285a17c678eSDavid Greenman 				m->m_next = NULL;
1286a17c678eSDavid Greenman 
1287dfe61cf1SDavid Greenman 				/*
1288ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1289ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1290ba8c6fd5SDavid Greenman 				 * instead.
1291dfe61cf1SDavid Greenman 				 */
1292a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1293a17c678eSDavid Greenman 					struct ether_header *eh;
1294aed53495SDavid Greenman 					int total_len;
1295a17c678eSDavid Greenman 
1296ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1297ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1298ba8c6fd5SDavid Greenman 					if (total_len <
1299ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
130006339180SDavid Greenman 						m_freem(m);
130106339180SDavid Greenman 						goto rcvloop;
130206339180SDavid Greenman 					}
1303920b58e8SBrooks Davis 
1304e8c8b728SJonathan Lemon 					/*
1305e8c8b728SJonathan Lemon 					 * Drop the packet if it has CRC
1306e8c8b728SJonathan Lemon 					 * errors.  This test is only needed
1307e8c8b728SJonathan Lemon 					 * when doing 802.1q VLAN on the 82557
1308e8c8b728SJonathan Lemon 					 * chip.
1309e8c8b728SJonathan Lemon 					 */
1310e8c8b728SJonathan Lemon 					if (rfa->rfa_status &
1311e8c8b728SJonathan Lemon 					    FXP_RFA_STATUS_CRC) {
1312e8c8b728SJonathan Lemon 						m_freem(m);
1313e8c8b728SJonathan Lemon 						goto rcvloop;
1314e8c8b728SJonathan Lemon 					}
1315920b58e8SBrooks Davis 
1316a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
13172e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1318a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1319ba8c6fd5SDavid Greenman 					m->m_data +=
1320ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1321ab090e5bSLuigi Rizzo 					m->m_len -=
1322ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1323ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1324a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1325a17c678eSDavid Greenman 				}
1326a17c678eSDavid Greenman 				goto rcvloop;
1327a17c678eSDavid Greenman 			}
1328a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1329ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1330ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1331ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1332ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
13332e2b8238SJonathan Lemon 				fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1334a17c678eSDavid Greenman 			}
1335a17c678eSDavid Greenman 		}
1336a17c678eSDavid Greenman }
1337a17c678eSDavid Greenman 
1338dfe61cf1SDavid Greenman /*
1339dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1340dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1341dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1342dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1343dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1344dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1345dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1346dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1347dfe61cf1SDavid Greenman  * them again next time.
1348dfe61cf1SDavid Greenman  */
1349303b270bSEivind Eklund static void
1350f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1351a17c678eSDavid Greenman {
1352f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1353ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1354a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1355c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1356f7788e8eSJonathan Lemon 	int s;
1357a17c678eSDavid Greenman 
1358a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1359a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1360397f9dfeSDavid Greenman 	if (sp->rx_good) {
1361397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1362397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1363397f9dfeSDavid Greenman 	} else {
1364c8cc6fcaSDavid Greenman 		/*
1365c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1366c8cc6fcaSDavid Greenman 		 */
1367397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1368397f9dfeSDavid Greenman 	}
13693ba65732SDavid Greenman 	ifp->if_ierrors +=
13703ba65732SDavid Greenman 	    sp->rx_crc_errors +
13713ba65732SDavid Greenman 	    sp->rx_alignment_errors +
13723ba65732SDavid Greenman 	    sp->rx_rnr_errors +
13736e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1374a17c678eSDavid Greenman 	/*
1375f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1376f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1377f9be9005SDavid Greenman 	 */
1378f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1379f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1380f9be9005SDavid Greenman 		if (tx_threshold < 192)
1381f9be9005SDavid Greenman 			tx_threshold += 64;
1382f9be9005SDavid Greenman 	}
1383f7788e8eSJonathan Lemon 	s = splimp();
1384397f9dfeSDavid Greenman 	/*
1385c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1386c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1387c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1388c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1389c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1390c8cc6fcaSDavid Greenman 	 */
1391c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1392c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1393c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1394c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1395c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1396c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1397c8cc6fcaSDavid Greenman 		}
1398c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1399c8cc6fcaSDavid Greenman 	}
1400c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1401c8cc6fcaSDavid Greenman 	/*
1402397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1403397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1404397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1405397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1406397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1407397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1408397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1409397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1410397f9dfeSDavid Greenman 	 */
1411397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1412397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1413397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1414397f9dfeSDavid Greenman 	}
1415f9be9005SDavid Greenman 	/*
14163ba65732SDavid Greenman 	 * If there is no pending command, start another stats
14173ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1418a17c678eSDavid Greenman 	 */
1419397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1420a17c678eSDavid Greenman 		/*
1421397f9dfeSDavid Greenman 		 * Start another stats dump.
1422a17c678eSDavid Greenman 		 */
14232e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1424dfe61cf1SDavid Greenman 	} else {
1425dfe61cf1SDavid Greenman 		/*
1426dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1427dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
14283ba65732SDavid Greenman 		 * next timer event to update them.
1429dfe61cf1SDavid Greenman 		 */
1430dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1431f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1432dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
14333ba65732SDavid Greenman 
1434dfe61cf1SDavid Greenman 		sp->rx_good = 0;
14353ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
14363ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
14373ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
14383ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1439dfe61cf1SDavid Greenman 	}
1440f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1441f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
144274396a0aSJonathan Lemon 	splx(s);
1443a17c678eSDavid Greenman 	/*
1444a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1445a17c678eSDavid Greenman 	 */
1446f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1447a17c678eSDavid Greenman }
1448a17c678eSDavid Greenman 
1449a17c678eSDavid Greenman /*
1450a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1451a17c678eSDavid Greenman  * the interface.
1452a17c678eSDavid Greenman  */
1453a17c678eSDavid Greenman static void
1454f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1455a17c678eSDavid Greenman {
1456ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
14573ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
14583ba65732SDavid Greenman 	int i;
1459a17c678eSDavid Greenman 
14607dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
14617dced78aSDavid Greenman 	ifp->if_timer = 0;
14627dced78aSDavid Greenman 
1463e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1464e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
1465e4fc250cSLuigi Rizzo #endif
1466a17c678eSDavid Greenman 	/*
1467a17c678eSDavid Greenman 	 * Cancel stats updater.
1468a17c678eSDavid Greenman 	 */
1469f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
14703ba65732SDavid Greenman 
14713ba65732SDavid Greenman 	/*
147272a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
14733ba65732SDavid Greenman 	 */
147472a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
147509882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
147672a32a26SJonathan Lemon 	DELAY(50);
1477a17c678eSDavid Greenman 
14783ba65732SDavid Greenman 	/*
14793ba65732SDavid Greenman 	 * Release any xmit buffers.
14803ba65732SDavid Greenman 	 */
1481da91462dSDavid Greenman 	txp = sc->cbl_base;
1482da91462dSDavid Greenman 	if (txp != NULL) {
1483da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1484da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1485da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1486da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1487da91462dSDavid Greenman 			}
1488da91462dSDavid Greenman 		}
14893ba65732SDavid Greenman 	}
14903ba65732SDavid Greenman 	sc->tx_queued = 0;
14913ba65732SDavid Greenman 
14923ba65732SDavid Greenman 	/*
14933ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
14943ba65732SDavid Greenman 	 */
14953ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
14963ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
14973ba65732SDavid Greenman 	sc->rfa_headm = NULL;
14983ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
14993ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
15003ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
15013ba65732SDavid Greenman 			/*
15023ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
15033ba65732SDavid Greenman 			 * and we just freed all the buffers we need
15043ba65732SDavid Greenman 			 * above.
15053ba65732SDavid Greenman 			 */
15063ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
15073ba65732SDavid Greenman 		}
15083ba65732SDavid Greenman 	}
1509a17c678eSDavid Greenman }
1510a17c678eSDavid Greenman 
1511a17c678eSDavid Greenman /*
1512a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1513a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1514a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1515a17c678eSDavid Greenman  * card has wedged for some reason.
1516a17c678eSDavid Greenman  */
1517a17c678eSDavid Greenman static void
1518f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1519a17c678eSDavid Greenman {
1520ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1521ba8c6fd5SDavid Greenman 
1522f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
15234a5f1499SDavid Greenman 	ifp->if_oerrors++;
1524a17c678eSDavid Greenman 
1525ba8c6fd5SDavid Greenman 	fxp_init(sc);
1526a17c678eSDavid Greenman }
1527a17c678eSDavid Greenman 
1528a17c678eSDavid Greenman static void
1529f7788e8eSJonathan Lemon fxp_init(void *xsc)
1530a17c678eSDavid Greenman {
1531fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1532ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1533a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1534a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1535a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
153609882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
1537f7788e8eSJonathan Lemon 	int i, prm, s;
1538a17c678eSDavid Greenman 
1539f7788e8eSJonathan Lemon 	s = splimp();
1540a17c678eSDavid Greenman 	/*
15413ba65732SDavid Greenman 	 * Cancel any pending I/O
1542a17c678eSDavid Greenman 	 */
15433ba65732SDavid Greenman 	fxp_stop(sc);
1544a17c678eSDavid Greenman 
1545a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1546a17c678eSDavid Greenman 
1547a17c678eSDavid Greenman 	/*
1548a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1549a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1550a17c678eSDavid Greenman 	 */
1551ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
15522e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1553a17c678eSDavid Greenman 
1554ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
15552e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1556a17c678eSDavid Greenman 
1557a17c678eSDavid Greenman 	/*
1558a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1559a17c678eSDavid Greenman 	 */
1560ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1561ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
15622e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1563a17c678eSDavid Greenman 
1564a17c678eSDavid Greenman 	/*
156572a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
156672a32a26SJonathan Lemon 	 */
156772a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
156872a32a26SJonathan Lemon 		fxp_load_ucode(sc);
156972a32a26SJonathan Lemon 
157072a32a26SJonathan Lemon 	/*
157109882363SJonathan Lemon 	 * Initialize the multicast address list.
157209882363SJonathan Lemon 	 */
157309882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
157409882363SJonathan Lemon 		mcsp = sc->mcsp;
157509882363SJonathan Lemon 		mcsp->cb_status = 0;
157609882363SJonathan Lemon 		mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL;
157709882363SJonathan Lemon 		mcsp->link_addr = -1;
157809882363SJonathan Lemon 		/*
157909882363SJonathan Lemon 	 	 * Start the multicast setup command.
158009882363SJonathan Lemon 		 */
158109882363SJonathan Lemon 		fxp_scb_wait(sc);
158209882363SJonathan Lemon 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
158309882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
158409882363SJonathan Lemon 		/* ...and wait for it to complete. */
158509882363SJonathan Lemon 		fxp_dma_wait(&mcsp->cb_status, sc);
158609882363SJonathan Lemon 	}
158709882363SJonathan Lemon 
158809882363SJonathan Lemon 	/*
1589a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1590a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1591a17c678eSDavid Greenman 	 * later.
1592a17c678eSDavid Greenman 	 */
1593a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1594a17c678eSDavid Greenman 
1595a17c678eSDavid Greenman 	/*
1596a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1597a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1598a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1599a17c678eSDavid Greenman 	 */
1600d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1601d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1602397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1603a17c678eSDavid Greenman 
1604a17c678eSDavid Greenman 	cbp->cb_status =	0;
1605a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1606a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1607a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1608001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1609001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1610a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1611f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1612f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1613f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1614f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1615001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1616001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1617f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1618a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1619f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1620f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
16213114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1622f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1623f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1624f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
162572a32a26SJonathan Lemon 	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
1626a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1627f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1628f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1629f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1630f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1631f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1632f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1633f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1634f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1635f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1636f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1637a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1638a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1639a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1640a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1641a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1642a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1643a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1644a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1645f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1646f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1647f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1648f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1649f7788e8eSJonathan Lemon 
1650a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1651a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1652a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1653f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1654f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1655f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1656f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1657a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
16583ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1659a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1660f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1661a17c678eSDavid Greenman 
166272a32a26SJonathan Lemon 	if (sc->revision == FXP_REV_82557) {
16633bd07cfdSJonathan Lemon 		/*
16643bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
16653bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
16663bd07cfdSJonathan Lemon 		 */
16673bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
16683bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
16693bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
16703bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
16713bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
16723bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
16733bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
16743bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
16753bd07cfdSJonathan Lemon 	} else {
16763bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
16773bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
16783bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
16793bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
16803bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
16813bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
16823bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
16833bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
16843bd07cfdSJonathan Lemon 	}
16853bd07cfdSJonathan Lemon 
1686a17c678eSDavid Greenman 	/*
1687a17c678eSDavid Greenman 	 * Start the config command/DMA.
1688a17c678eSDavid Greenman 	 */
1689ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1690397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
16912e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1692a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
16937dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1694a17c678eSDavid Greenman 
1695a17c678eSDavid Greenman 	/*
1696a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1697a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1698a17c678eSDavid Greenman 	 */
1699a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1700a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1701a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1702a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1703d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1704d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1705a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1706a17c678eSDavid Greenman 
1707a17c678eSDavid Greenman 	/*
1708a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1709a17c678eSDavid Greenman 	 */
1710ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
17112e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1712a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
17137dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1714a17c678eSDavid Greenman 
1715a17c678eSDavid Greenman 	/*
1716a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1717a17c678eSDavid Greenman 	 */
1718a17c678eSDavid Greenman 
1719a17c678eSDavid Greenman 	txp = sc->cbl_base;
1720a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1721a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1722a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1723a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
17243bd07cfdSJonathan Lemon 		txp[i].link_addr =
17253bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
17263bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
17273bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
17283bd07cfdSJonathan Lemon 		else
1729a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1730a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1731a17c678eSDavid Greenman 	}
1732a17c678eSDavid Greenman 	/*
1733397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1734a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1735a17c678eSDavid Greenman 	 */
1736a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1737a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1738397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1739a17c678eSDavid Greenman 
1740ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
17412e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1742a17c678eSDavid Greenman 
1743a17c678eSDavid Greenman 	/*
1744a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1745a17c678eSDavid Greenman 	 */
1746ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1747ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1748ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
17492e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1750a17c678eSDavid Greenman 
1751dccee1a1SDavid Greenman 	/*
1752ba8c6fd5SDavid Greenman 	 * Set current media.
1753dccee1a1SDavid Greenman 	 */
1754f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1755f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1756dccee1a1SDavid Greenman 
1757a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1758a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1759e8c8b728SJonathan Lemon 
1760e8c8b728SJonathan Lemon 	/*
1761e8c8b728SJonathan Lemon 	 * Enable interrupts.
1762e8c8b728SJonathan Lemon 	 */
1763e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1764f7788e8eSJonathan Lemon 	splx(s);
1765a17c678eSDavid Greenman 
1766a17c678eSDavid Greenman 	/*
1767a17c678eSDavid Greenman 	 * Start stats updater.
1768a17c678eSDavid Greenman 	 */
1769f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1770f7788e8eSJonathan Lemon }
1771f7788e8eSJonathan Lemon 
1772f7788e8eSJonathan Lemon static int
1773f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1774f7788e8eSJonathan Lemon {
1775f7788e8eSJonathan Lemon 
1776f7788e8eSJonathan Lemon 	return (0);
1777a17c678eSDavid Greenman }
1778a17c678eSDavid Greenman 
1779303b270bSEivind Eklund static void
1780f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1781ba8c6fd5SDavid Greenman {
1782ba8c6fd5SDavid Greenman 
1783f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1784ba8c6fd5SDavid Greenman }
1785ba8c6fd5SDavid Greenman 
1786ba8c6fd5SDavid Greenman /*
1787ba8c6fd5SDavid Greenman  * Change media according to request.
1788ba8c6fd5SDavid Greenman  */
1789f7788e8eSJonathan Lemon static int
1790f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1791ba8c6fd5SDavid Greenman {
1792ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1793f7788e8eSJonathan Lemon 	struct mii_data *mii;
1794ba8c6fd5SDavid Greenman 
1795f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1796f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1797ba8c6fd5SDavid Greenman 	return (0);
1798ba8c6fd5SDavid Greenman }
1799ba8c6fd5SDavid Greenman 
1800ba8c6fd5SDavid Greenman /*
1801ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1802ba8c6fd5SDavid Greenman  */
1803f7788e8eSJonathan Lemon static void
1804f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1805ba8c6fd5SDavid Greenman {
1806ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1807f7788e8eSJonathan Lemon 	struct mii_data *mii;
1808ba8c6fd5SDavid Greenman 
1809f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1810f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1811f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1812f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
18132e2b8238SJonathan Lemon 
18142e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
18152e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
18162e2b8238SJonathan Lemon 	else
18172e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
1818ba8c6fd5SDavid Greenman }
1819ba8c6fd5SDavid Greenman 
1820a17c678eSDavid Greenman /*
1821a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1822a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1823a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1824dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1825a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1826a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1827a17c678eSDavid Greenman  */
1828a17c678eSDavid Greenman static int
1829f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1830a17c678eSDavid Greenman {
1831ba8c6fd5SDavid Greenman 	u_int32_t v;
1832a17c678eSDavid Greenman 	struct mbuf *m;
1833a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1834a17c678eSDavid Greenman 
1835a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1836a17c678eSDavid Greenman 	if (m != NULL) {
1837a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1838a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1839d9cdd960SJonathan Lemon 			device_printf(sc->dev,
1840d9cdd960SJonathan Lemon 			    "cluster allocation failed, packet dropped!\n");
1841a17c678eSDavid Greenman 			m_freem(m);
1842eadd5e3aSDavid Greenman 			if (oldm == NULL)
1843eadd5e3aSDavid Greenman 				return 1;
1844a17c678eSDavid Greenman 			m = oldm;
1845eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1846a17c678eSDavid Greenman 		}
1847a17c678eSDavid Greenman 	} else {
1848d9cdd960SJonathan Lemon 		device_printf(sc->dev,
1849d9cdd960SJonathan Lemon 		    "mbuf allocation failed, packet dropped!\n");
1850eadd5e3aSDavid Greenman 		if (oldm == NULL)
1851a17c678eSDavid Greenman 			return 1;
1852eadd5e3aSDavid Greenman 		m = oldm;
1853eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1854eadd5e3aSDavid Greenman 	}
1855ba8c6fd5SDavid Greenman 
1856ba8c6fd5SDavid Greenman 	/*
1857ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1858ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1859ba8c6fd5SDavid Greenman 	 */
1860ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1861ba8c6fd5SDavid Greenman 
1862eadd5e3aSDavid Greenman 	/*
1863eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1864eadd5e3aSDavid Greenman 	 * data start past it.
1865eadd5e3aSDavid Greenman 	 */
1866a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1867eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
18684fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1869eadd5e3aSDavid Greenman 
1870ba8c6fd5SDavid Greenman 	/*
1871ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1872ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1873ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1874ba8c6fd5SDavid Greenman 	 */
18754fc1dda9SAndrew Gallatin 
1876a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1877a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1878a17c678eSDavid Greenman 	rfa->actual_size = 0;
1879ba8c6fd5SDavid Greenman 
1880ba8c6fd5SDavid Greenman 	v = -1;
18814fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
18824fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1883ba8c6fd5SDavid Greenman 
1884dfe61cf1SDavid Greenman 	/*
1885dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1886dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1887dfe61cf1SDavid Greenman 	 */
1888a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1889ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1890ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1891a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1892ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
18934fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1894aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1895a17c678eSDavid Greenman 	} else {
1896a17c678eSDavid Greenman 		sc->rfa_headm = m;
1897a17c678eSDavid Greenman 	}
1898a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1899a17c678eSDavid Greenman 
1900dfe61cf1SDavid Greenman 	return (m == oldm);
1901a17c678eSDavid Greenman }
1902a17c678eSDavid Greenman 
19036ebc3153SDavid Greenman static volatile int
1904f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1905dccee1a1SDavid Greenman {
1906f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1907dccee1a1SDavid Greenman 	int count = 10000;
19086ebc3153SDavid Greenman 	int value;
1909dccee1a1SDavid Greenman 
1910ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1911ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1912dccee1a1SDavid Greenman 
1913ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1914ba8c6fd5SDavid Greenman 	    && count--)
19156ebc3153SDavid Greenman 		DELAY(10);
1916dccee1a1SDavid Greenman 
1917dccee1a1SDavid Greenman 	if (count <= 0)
1918f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1919dccee1a1SDavid Greenman 
19206ebc3153SDavid Greenman 	return (value & 0xffff);
1921dccee1a1SDavid Greenman }
1922dccee1a1SDavid Greenman 
1923dccee1a1SDavid Greenman static void
1924f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1925dccee1a1SDavid Greenman {
1926f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1927dccee1a1SDavid Greenman 	int count = 10000;
1928dccee1a1SDavid Greenman 
1929ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1930ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1931ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1932dccee1a1SDavid Greenman 
1933ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1934ba8c6fd5SDavid Greenman 	    count--)
19356ebc3153SDavid Greenman 		DELAY(10);
1936dccee1a1SDavid Greenman 
1937dccee1a1SDavid Greenman 	if (count <= 0)
1938f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1939dccee1a1SDavid Greenman }
1940dccee1a1SDavid Greenman 
1941dccee1a1SDavid Greenman static int
1942f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1943a17c678eSDavid Greenman {
19449b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1945a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1946f7788e8eSJonathan Lemon 	struct mii_data *mii;
1947f7788e8eSJonathan Lemon 	int s, error = 0;
1948a17c678eSDavid Greenman 
1949f7788e8eSJonathan Lemon 	s = splimp();
1950a17c678eSDavid Greenman 
1951a17c678eSDavid Greenman 	switch (command) {
1952a17c678eSDavid Greenman 	case SIOCSIFADDR:
1953a17c678eSDavid Greenman 	case SIOCGIFADDR:
1954fb583156SDavid Greenman 	case SIOCSIFMTU:
1955fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1956a17c678eSDavid Greenman 		break;
1957a17c678eSDavid Greenman 
1958a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1959f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1960f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1961f7788e8eSJonathan Lemon 		else
1962f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1963a17c678eSDavid Greenman 
1964a17c678eSDavid Greenman 		/*
1965a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1966a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1967a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1968a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1969a17c678eSDavid Greenman 		 */
1970a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1971fb583156SDavid Greenman 			fxp_init(sc);
1972a17c678eSDavid Greenman 		} else {
1973a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
19744a5f1499SDavid Greenman 				fxp_stop(sc);
1975a17c678eSDavid Greenman 		}
1976a17c678eSDavid Greenman 		break;
1977a17c678eSDavid Greenman 
1978a17c678eSDavid Greenman 	case SIOCADDMULTI:
1979a17c678eSDavid Greenman 	case SIOCDELMULTI:
1980f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1981f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1982f7788e8eSJonathan Lemon 		else
1983f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1984a17c678eSDavid Greenman 		/*
1985a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1986a17c678eSDavid Greenman 		 * accordingly.
1987a17c678eSDavid Greenman 		 */
1988f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1989397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1990397f9dfeSDavid Greenman 		/*
1991f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1992397f9dfeSDavid Greenman 		 * again rather than else {}.
1993397f9dfeSDavid Greenman 		 */
1994f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1995fb583156SDavid Greenman 			fxp_init(sc);
1996a17c678eSDavid Greenman 		error = 0;
1997ba8c6fd5SDavid Greenman 		break;
1998ba8c6fd5SDavid Greenman 
1999ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2000ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2001f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2002f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2003f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2004f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2005f7788e8eSJonathan Lemon 		} else {
2006ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2007f7788e8eSJonathan Lemon 		}
2008a17c678eSDavid Greenman 		break;
2009a17c678eSDavid Greenman 
2010a17c678eSDavid Greenman 	default:
2011a17c678eSDavid Greenman 		error = EINVAL;
2012a17c678eSDavid Greenman 	}
2013f7788e8eSJonathan Lemon 	splx(s);
2014a17c678eSDavid Greenman 	return (error);
2015a17c678eSDavid Greenman }
2016397f9dfeSDavid Greenman 
2017397f9dfeSDavid Greenman /*
201809882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
201909882363SJonathan Lemon  */
202009882363SJonathan Lemon static int
202109882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
202209882363SJonathan Lemon {
202309882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
202409882363SJonathan Lemon 	struct ifnet *ifp = &sc->sc_if;
202509882363SJonathan Lemon 	struct ifmultiaddr *ifma;
202609882363SJonathan Lemon 	int nmcasts;
202709882363SJonathan Lemon 
202809882363SJonathan Lemon 	nmcasts = 0;
202909882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
203009882363SJonathan Lemon #if __FreeBSD_version < 500000
203109882363SJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
203209882363SJonathan Lemon #else
203309882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
203409882363SJonathan Lemon #endif
203509882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
203609882363SJonathan Lemon 				continue;
203709882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
203809882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
203909882363SJonathan Lemon 				nmcasts = 0;
204009882363SJonathan Lemon 				break;
204109882363SJonathan Lemon 			}
204209882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
204309882363SJonathan Lemon 			    (void *)(uintptr_t)(volatile void *)
204409882363SJonathan Lemon 				&sc->mcsp->mc_addr[nmcasts][0], 6);
204509882363SJonathan Lemon 			nmcasts++;
204609882363SJonathan Lemon 		}
204709882363SJonathan Lemon 	}
204809882363SJonathan Lemon 	mcsp->mc_cnt = nmcasts * 6;
204909882363SJonathan Lemon 	return (nmcasts);
205009882363SJonathan Lemon }
205109882363SJonathan Lemon 
205209882363SJonathan Lemon /*
2053397f9dfeSDavid Greenman  * Program the multicast filter.
2054397f9dfeSDavid Greenman  *
2055397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2056397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
20573114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2058397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2059dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2060397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2061397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2062397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2063397f9dfeSDavid Greenman  *
2064397f9dfeSDavid Greenman  * This function must be called at splimp.
2065397f9dfeSDavid Greenman  */
2066397f9dfeSDavid Greenman static void
2067f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2068397f9dfeSDavid Greenman {
2069397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2070397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
20717dced78aSDavid Greenman 	int count;
2072397f9dfeSDavid Greenman 
20733114fdb4SDavid Greenman 	/*
20743114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
20753114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
20763114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
20773114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
20783114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
20793114fdb4SDavid Greenman 	 */
2080397f9dfeSDavid Greenman 	if (sc->tx_queued) {
20813114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
20823114fdb4SDavid Greenman 
20833114fdb4SDavid Greenman 		/*
20843114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
20853114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
20863114fdb4SDavid Greenman 		 */
20873114fdb4SDavid Greenman 		if (sc->need_mcsetup)
20883114fdb4SDavid Greenman 			return;
2089397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
20903114fdb4SDavid Greenman 
20913114fdb4SDavid Greenman 		/*
209272a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
209372a32a26SJonathan Lemon 		 * when all TX commands have been processed.
20943114fdb4SDavid Greenman 		 */
20953114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
20963114fdb4SDavid Greenman 		txp->mb_head = NULL;
20973114fdb4SDavid Greenman 		txp->cb_status = 0;
2098e8c8b728SJonathan Lemon 		txp->cb_command = FXP_CB_COMMAND_NOP |
2099e8c8b728SJonathan Lemon 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
21003114fdb4SDavid Greenman 		/*
21013114fdb4SDavid Greenman 		 * Advance the end of list forward.
21023114fdb4SDavid Greenman 		 */
21033114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
21043114fdb4SDavid Greenman 		sc->cbl_last = txp;
21053114fdb4SDavid Greenman 		sc->tx_queued++;
21063114fdb4SDavid Greenman 		/*
21073114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
21083114fdb4SDavid Greenman 		 */
21093114fdb4SDavid Greenman 		fxp_scb_wait(sc);
21102e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
21113114fdb4SDavid Greenman 		/*
21123114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
21133114fdb4SDavid Greenman 		 * card again.
21143114fdb4SDavid Greenman 		 */
21153114fdb4SDavid Greenman 		ifp->if_timer = 5;
21163114fdb4SDavid Greenman 
2117397f9dfeSDavid Greenman 		return;
2118397f9dfeSDavid Greenman 	}
2119397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2120397f9dfeSDavid Greenman 
2121397f9dfeSDavid Greenman 	/*
2122397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2123397f9dfeSDavid Greenman 	 */
2124397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
2125397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
2126397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
2127e8c8b728SJonathan Lemon 	mcsp->cb_command = FXP_CB_COMMAND_MCAS |
2128e8c8b728SJonathan Lemon 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
2129397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
213009882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2131397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2132397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2133397f9dfeSDavid Greenman 
2134397f9dfeSDavid Greenman 	/*
2135397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2136397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2137397f9dfeSDavid Greenman 	 */
21387dced78aSDavid Greenman 	count = 100;
2139397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
21407dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
21417dced78aSDavid Greenman 		DELAY(10);
21427dced78aSDavid Greenman 	if (count == 0) {
2143f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
21447dced78aSDavid Greenman 		return;
21457dced78aSDavid Greenman 	}
2146397f9dfeSDavid Greenman 
2147397f9dfeSDavid Greenman 	/*
2148397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2149397f9dfeSDavid Greenman 	 */
2150397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2151397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
21522e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2153397f9dfeSDavid Greenman 
21543114fdb4SDavid Greenman 	ifp->if_timer = 2;
2155397f9dfeSDavid Greenman 	return;
2156397f9dfeSDavid Greenman }
215772a32a26SJonathan Lemon 
215872a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
215972a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
216072a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
216172a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
216272a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
216372a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
216472a32a26SJonathan Lemon 
216572a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
216672a32a26SJonathan Lemon 
216772a32a26SJonathan Lemon struct ucode {
216872a32a26SJonathan Lemon 	u_int32_t	revision;
216972a32a26SJonathan Lemon 	u_int32_t	*ucode;
217072a32a26SJonathan Lemon 	int		length;
217172a32a26SJonathan Lemon 	u_short		int_delay_offset;
217272a32a26SJonathan Lemon 	u_short		bundle_max_offset;
217372a32a26SJonathan Lemon } ucode_table[] = {
217472a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
217572a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
217672a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
217772a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
217872a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
217972a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
218072a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
218172a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
218272a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
218372a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
218472a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
218572a32a26SJonathan Lemon };
218672a32a26SJonathan Lemon 
218772a32a26SJonathan Lemon static void
218872a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
218972a32a26SJonathan Lemon {
219072a32a26SJonathan Lemon 	struct ucode *uc;
219172a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
219272a32a26SJonathan Lemon 
219372a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
219472a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
219572a32a26SJonathan Lemon 			break;
219672a32a26SJonathan Lemon 	if (uc->ucode == NULL)
219772a32a26SJonathan Lemon 		return;
219872a32a26SJonathan Lemon 	cbp = (struct fxp_cb_ucode *)sc->cbl_base;
219972a32a26SJonathan Lemon 	cbp->cb_status = 0;
220072a32a26SJonathan Lemon 	cbp->cb_command = FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL;
220172a32a26SJonathan Lemon 	cbp->link_addr = -1;    	/* (no) next command */
220272a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
220372a32a26SJonathan Lemon 	if (uc->int_delay_offset)
220472a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->int_delay_offset] =
2205208b417fSJonathan Lemon 		    sc->tunable_int_delay + sc->tunable_int_delay / 2;
220672a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
220772a32a26SJonathan Lemon 		*(u_short *)&cbp->ucode[uc->bundle_max_offset] =
220872a32a26SJonathan Lemon 		    sc->tunable_bundle_max;
220972a32a26SJonathan Lemon 	/*
221072a32a26SJonathan Lemon 	 * Download the ucode to the chip.
221172a32a26SJonathan Lemon 	 */
221272a32a26SJonathan Lemon 	fxp_scb_wait(sc);
221372a32a26SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
221472a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
221572a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
221672a32a26SJonathan Lemon 	fxp_dma_wait(&cbp->cb_status, sc);
221772a32a26SJonathan Lemon 	device_printf(sc->dev,
221872a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
221972a32a26SJonathan Lemon 	    sc->tunable_int_delay,
222072a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
222172a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
222272a32a26SJonathan Lemon }
222372a32a26SJonathan Lemon 
222472a32a26SJonathan Lemon static int
222572a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
222672a32a26SJonathan Lemon {
222772a32a26SJonathan Lemon 	int error, value;
222872a32a26SJonathan Lemon 
222972a32a26SJonathan Lemon 	value = *(int *)arg1;
223072a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
223172a32a26SJonathan Lemon 	if (error || !req->newptr)
223272a32a26SJonathan Lemon 		return (error);
223372a32a26SJonathan Lemon 	if (value < low || value > high)
223472a32a26SJonathan Lemon 		return (EINVAL);
223572a32a26SJonathan Lemon 	*(int *)arg1 = value;
223672a32a26SJonathan Lemon 	return (0);
223772a32a26SJonathan Lemon }
223872a32a26SJonathan Lemon 
223972a32a26SJonathan Lemon /*
224072a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
224172a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
224272a32a26SJonathan Lemon  */
224372a32a26SJonathan Lemon static int
224472a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
224572a32a26SJonathan Lemon {
224672a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
224772a32a26SJonathan Lemon }
224872a32a26SJonathan Lemon 
224972a32a26SJonathan Lemon static int
225072a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
225172a32a26SJonathan Lemon {
225272a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
225372a32a26SJonathan Lemon }
2254