xref: /freebsd/sys/dev/fxp/if_fxp.c (revision e8c8b728c7c33c286d7c033d5f1bac3f3c89cb0f)
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 
35e8c8b728SJonathan Lemon #include "vlan.h"
36e8c8b728SJonathan Lemon 
37a17c678eSDavid Greenman #include <sys/param.h>
38a17c678eSDavid Greenman #include <sys/systm.h>
39a17c678eSDavid Greenman #include <sys/mbuf.h>
40a17c678eSDavid Greenman #include <sys/malloc.h>
41f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
42a17c678eSDavid Greenman #include <sys/kernel.h>
434458ac71SBruce Evans #include <sys/socket.h>
44a17c678eSDavid Greenman 
45a17c678eSDavid Greenman #include <net/if.h>
46397f9dfeSDavid Greenman #include <net/if_dl.h>
47ba8c6fd5SDavid Greenman #include <net/if_media.h>
48a17c678eSDavid Greenman 
49a17c678eSDavid Greenman #ifdef NS
50a17c678eSDavid Greenman #include <netns/ns.h>
51a17c678eSDavid Greenman #include <netns/ns_if.h>
52a17c678eSDavid Greenman #endif
53a17c678eSDavid Greenman 
54a17c678eSDavid Greenman #include <net/bpf.h>
55ba8c6fd5SDavid Greenman #include <sys/sockio.h>
566182fdbdSPeter Wemm #include <sys/bus.h>
576182fdbdSPeter Wemm #include <machine/bus.h>
586182fdbdSPeter Wemm #include <sys/rman.h>
596182fdbdSPeter Wemm #include <machine/resource.h>
60ba8c6fd5SDavid Greenman 
611d5e9e22SEivind Eklund #include <net/ethernet.h>
621d5e9e22SEivind Eklund #include <net/if_arp.h>
63ba8c6fd5SDavid Greenman 
64dfe61cf1SDavid Greenman #include <vm/vm.h>		/* for vtophys */
65efeaf95aSDavid Greenman #include <vm/pmap.h>		/* for vtophys */
66f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
67a17c678eSDavid Greenman 
68e8c8b728SJonathan Lemon #if NVLAN > 0
69e8c8b728SJonathan Lemon #include <net/if_types.h>
70e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
71e8c8b728SJonathan Lemon #endif
72e8c8b728SJonathan Lemon 
73a17c678eSDavid Greenman #include <pci/pcivar.h>
74df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
75a17c678eSDavid Greenman 
76f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
77f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
78f7788e8eSJonathan Lemon 
79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
81f7788e8eSJonathan Lemon 
82f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
83f7788e8eSJonathan Lemon #include "miibus_if.h"
844fc1dda9SAndrew Gallatin 
85e8c8b728SJonathan Lemon #ifdef KLD_MODULE
86e8c8b728SJonathan Lemon #define NMIIBUS 1
87e8c8b728SJonathan Lemon #else
88e8c8b728SJonathan Lemon #include "miibus.h"
89e8c8b728SJonathan Lemon #endif
90e8c8b728SJonathan Lemon #if NMIIBUS < 1
91e8c8b728SJonathan Lemon #error "You need to add 'device miibus' to your kernel config!"
92e8c8b728SJonathan Lemon #else
93e8c8b728SJonathan Lemon 
94ba8c6fd5SDavid Greenman /*
95ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
96ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
97ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
98ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
99ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
100ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
101ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
102ba8c6fd5SDavid Greenman  */
103ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
104ba8c6fd5SDavid Greenman 
105ba8c6fd5SDavid Greenman /*
106f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
107f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
108f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
109f7788e8eSJonathan Lemon  */
110f7788e8eSJonathan Lemon static int tx_threshold = 64;
111f7788e8eSJonathan Lemon 
112f7788e8eSJonathan Lemon /*
113f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
114f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
115f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
116f7788e8eSJonathan Lemon  * to fxp_init.
117f7788e8eSJonathan Lemon  *
118f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
119f7788e8eSJonathan Lemon  */
120f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
121f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
122f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
123f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
124f7788e8eSJonathan Lemon 	0x0,	/*  0 */
125f7788e8eSJonathan Lemon 	0x0,	/*  1 */
126f7788e8eSJonathan Lemon 	0x0,	/*  2 */
127f7788e8eSJonathan Lemon 	0x0,	/*  3 */
128f7788e8eSJonathan Lemon 	0x0,	/*  4 */
129f7788e8eSJonathan Lemon 	0x0,	/*  5 */
130f7788e8eSJonathan Lemon 	0x32,	/*  6 */
131f7788e8eSJonathan Lemon 	0x0,	/*  7 */
132f7788e8eSJonathan Lemon 	0x0,	/*  8 */
133f7788e8eSJonathan Lemon 	0x0,	/*  9 */
134f7788e8eSJonathan Lemon 	0x6,	/* 10 */
135f7788e8eSJonathan Lemon 	0x0,	/* 11 */
136f7788e8eSJonathan Lemon 	0x0,	/* 12 */
137f7788e8eSJonathan Lemon 	0x0,	/* 13 */
138f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
139f7788e8eSJonathan Lemon 	0x48,	/* 15 */
140f7788e8eSJonathan Lemon 	0x0,	/* 16 */
141f7788e8eSJonathan Lemon 	0x40,	/* 17 */
142f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
143f7788e8eSJonathan Lemon 	0x0,	/* 19 */
144f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
145f7788e8eSJonathan Lemon 	0x5	/* 21 */
146f7788e8eSJonathan Lemon };
147f7788e8eSJonathan Lemon 
148f7788e8eSJonathan Lemon struct fxp_ident {
149f7788e8eSJonathan Lemon 	u_int16_t	devid;
150f7788e8eSJonathan Lemon 	char 		*name;
151f7788e8eSJonathan Lemon };
152f7788e8eSJonathan Lemon 
153f7788e8eSJonathan Lemon /*
154f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
155f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
156f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
157f7788e8eSJonathan Lemon  * them.
158f7788e8eSJonathan Lemon  */
159f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
160f7788e8eSJonathan Lemon     { 0x1229,		"Intel Pro 10/100B/100+ Ethernet" },
161f7788e8eSJonathan Lemon     { 0x2449,		"Intel Pro/100 Ethernet" },
162f7788e8eSJonathan Lemon     { 0x1209,		"Intel Embedded 10/100 Ethernet" },
163f7788e8eSJonathan Lemon     { 0x1029,		"Intel Pro/100 Ethernet" },
164f7788e8eSJonathan Lemon     { 0x1030,		"Intel Pro/100 Ethernet" },
165f7788e8eSJonathan Lemon     { 0x1031,		"Intel Pro/100 Ethernet" },
166f7788e8eSJonathan Lemon     { 0x1032,		"Intel Pro/100 Ethernet" },
167f7788e8eSJonathan Lemon     { 0x1033,		"Intel Pro/100 Ethernet" },
168f7788e8eSJonathan Lemon     { 0x1034,		"Intel Pro/100 Ethernet" },
169f7788e8eSJonathan Lemon     { 0x1035,		"Intel Pro/100 Ethernet" },
170f7788e8eSJonathan Lemon     { 0x1036,		"Intel Pro/100 Ethernet" },
171f7788e8eSJonathan Lemon     { 0x1037,		"Intel Pro/100 Ethernet" },
172f7788e8eSJonathan Lemon     { 0x1038,		"Intel Pro/100 Ethernet" },
173f7788e8eSJonathan Lemon     { 0,		NULL },
174f7788e8eSJonathan Lemon };
175f7788e8eSJonathan Lemon 
176f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
177f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
178f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
179f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
180f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
181f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
182f7788e8eSJonathan Lemon 
183f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
184f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
185f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
186f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
187f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
188f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
189f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
190f7788e8eSJonathan Lemon 			    caddr_t data);
191f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
192f7788e8eSJonathan Lemon static int		fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
193f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
194f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
195f7788e8eSJonathan Lemon 			    int autosize);
196f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
197f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
198f7788e8eSJonathan Lemon 			    int offset, int words);
199f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
200f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
201f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
202f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
203f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
204f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
205f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
206f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
207f7788e8eSJonathan Lemon 			    int value);
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);
211f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
212f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
213f7788e8eSJonathan Lemon 
214f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
215f7788e8eSJonathan Lemon 	/* Device interface */
216f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
217f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
218f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
219f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
220f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
221f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
222f7788e8eSJonathan Lemon 
223f7788e8eSJonathan Lemon 	/* MII interface */
224f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
225f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
226f7788e8eSJonathan Lemon 
227f7788e8eSJonathan Lemon 	{ 0, 0 }
228f7788e8eSJonathan Lemon };
229f7788e8eSJonathan Lemon 
230f7788e8eSJonathan Lemon static driver_t fxp_driver = {
231f7788e8eSJonathan Lemon 	"fxp",
232f7788e8eSJonathan Lemon 	fxp_methods,
233f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
234f7788e8eSJonathan Lemon };
235f7788e8eSJonathan Lemon 
236f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
237f7788e8eSJonathan Lemon 
238f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
239f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
240f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
241f7788e8eSJonathan Lemon 
242f7788e8eSJonathan Lemon /*
243ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
244ba8c6fd5SDavid Greenman  */
245ba8c6fd5SDavid Greenman static __inline void
246f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
247ba8c6fd5SDavid Greenman {
248aed53495SDavid Greenman #ifdef __i386__
249aed53495SDavid Greenman 	*dst = *src;
250aed53495SDavid Greenman #else
251fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
252fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
253ba8c6fd5SDavid Greenman 
254ba8c6fd5SDavid Greenman 	b[0] = a[0];
255ba8c6fd5SDavid Greenman 	b[1] = a[1];
256aed53495SDavid Greenman #endif
257ba8c6fd5SDavid Greenman }
258a17c678eSDavid Greenman 
259a17c678eSDavid Greenman /*
260dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
261dfe61cf1SDavid Greenman  * completed).
262dfe61cf1SDavid Greenman  */
263c1087c13SBruce Evans static __inline void
264f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
265a17c678eSDavid Greenman {
266a17c678eSDavid Greenman 	int i = 10000;
267a17c678eSDavid Greenman 
2687dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2697dced78aSDavid Greenman 		DELAY(2);
2707dced78aSDavid Greenman 	if (i == 0)
271e8c8b728SJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x, 0x%x, 0x%x 0x%x\n",
272e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
273e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
274e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
275e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2767dced78aSDavid Greenman }
2777dced78aSDavid Greenman 
2787dced78aSDavid Greenman static __inline void
279f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
2807dced78aSDavid Greenman {
2817dced78aSDavid Greenman 	int i = 10000;
2827dced78aSDavid Greenman 
2837dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
2847dced78aSDavid Greenman 		DELAY(2);
2857dced78aSDavid Greenman 	if (i == 0)
286f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
287a17c678eSDavid Greenman }
288a17c678eSDavid Greenman 
289dfe61cf1SDavid Greenman /*
290dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
291dfe61cf1SDavid Greenman  */
2926182fdbdSPeter Wemm static int
2936182fdbdSPeter Wemm fxp_probe(device_t dev)
294a17c678eSDavid Greenman {
295f7788e8eSJonathan Lemon 	u_int16_t devid;
296f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
297f7788e8eSJonathan Lemon 
29855ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
299f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
300f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
301f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
302f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
303f7788e8eSJonathan Lemon 				return (0);
30455ce7b51SDavid Greenman 			}
305dd68ef16SPeter Wemm 		}
306f7788e8eSJonathan Lemon 	}
307f7788e8eSJonathan Lemon 	return (ENXIO);
3086182fdbdSPeter Wemm }
3096182fdbdSPeter Wemm 
3106182fdbdSPeter Wemm static int
3116182fdbdSPeter Wemm fxp_attach(device_t dev)
312a17c678eSDavid Greenman {
3136182fdbdSPeter Wemm 	int error = 0;
3146182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
315ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
3169fa6ccfbSMatt Jacob 	u_int32_t val;
317f7788e8eSJonathan Lemon 	u_int16_t data;
318f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
319f7788e8eSJonathan Lemon 	int s;
320a17c678eSDavid Greenman 
321f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
322f7788e8eSJonathan Lemon 	sc->dev = dev;
3236c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
324f7788e8eSJonathan Lemon 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
325a17c678eSDavid Greenman 
326f7788e8eSJonathan Lemon 	s = splimp();
327a17c678eSDavid Greenman 
328dfe61cf1SDavid Greenman 	/*
3299fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3309fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
331df373873SWes Peters 	 */
3326182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
333df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3346182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3359fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
336df373873SWes Peters 
337f7788e8eSJonathan Lemon #if __FreeBSD_version >= 500000
3388d799694SBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
3398d799694SBill Paul 		u_int32_t		iobase, membase, irq;
3408d799694SBill Paul 
3418d799694SBill Paul 		/* Save important PCI config data. */
3428d799694SBill Paul 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
3438d799694SBill Paul 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
3448d799694SBill Paul 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
3458d799694SBill Paul 
3468d799694SBill Paul 		/* Reset the power state. */
3478d799694SBill Paul 		device_printf(dev, "chip is in D%d power mode "
3488d799694SBill Paul 		    "-- setting to D0\n", pci_get_powerstate(dev));
3498d799694SBill Paul 
3508d799694SBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
3518d799694SBill Paul 
3528d799694SBill Paul 		/* Restore PCI config data. */
3538d799694SBill Paul 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
3548d799694SBill Paul 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
3558d799694SBill Paul 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
3568d799694SBill Paul 	}
357f7788e8eSJonathan Lemon #endif
3588d799694SBill Paul 
359df373873SWes Peters 	/*
3609fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3619fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3629fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
363dfe61cf1SDavid Greenman 	 */
3649fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3659fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3662a05a4ebSMatt Jacob 	prefer_iomap = 0;
3672a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3682a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3699fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3709fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
3719fa6ccfbSMatt Jacob 	}
3729fa6ccfbSMatt Jacob 
3739fa6ccfbSMatt Jacob 	if (val & m1) {
3749fa6ccfbSMatt Jacob 		sc->rtp =
3759fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3769fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3779fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3786182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
3799fa6ccfbSMatt Jacob 	}
3809fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
3819fa6ccfbSMatt Jacob 		sc->rtp =
3829fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3839fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3849fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3859fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
3869fa6ccfbSMatt Jacob 	}
3879fa6ccfbSMatt Jacob 
3886182fdbdSPeter Wemm 	if (!sc->mem) {
3899fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
3906182fdbdSPeter Wemm 		error = ENXIO;
391a17c678eSDavid Greenman 		goto fail;
392a17c678eSDavid Greenman         }
3939fa6ccfbSMatt Jacob 	if (bootverbose) {
3949fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
3959fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
3969fa6ccfbSMatt Jacob 	}
3974fc1dda9SAndrew Gallatin 
3984fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
3994fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
400a17c678eSDavid Greenman 
401a17c678eSDavid Greenman 	/*
402dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
403dfe61cf1SDavid Greenman 	 */
4046182fdbdSPeter Wemm 	rid = 0;
4056182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4066182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4076182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4086182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4096182fdbdSPeter Wemm 		error = ENXIO;
4106182fdbdSPeter Wemm 		goto fail;
4116182fdbdSPeter Wemm 	}
4126182fdbdSPeter Wemm 
413566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
414566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
4156182fdbdSPeter Wemm 	if (error) {
4166182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
417a17c678eSDavid Greenman 		goto fail;
418a17c678eSDavid Greenman 	}
419a17c678eSDavid Greenman 
420f7788e8eSJonathan Lemon 	/*
421f7788e8eSJonathan Lemon 	 * Reset to a stable state.
422f7788e8eSJonathan Lemon 	 */
423f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
424f7788e8eSJonathan Lemon 	DELAY(10);
425f7788e8eSJonathan Lemon 
426f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
427f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
428f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
429f7788e8eSJonathan Lemon 		goto failmem;
430f7788e8eSJonathan Lemon 
431f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
432f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
433f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
434f7788e8eSJonathan Lemon 		goto failmem;
435f7788e8eSJonathan Lemon 
436f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
437f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
438f7788e8eSJonathan Lemon 		goto failmem;
439f7788e8eSJonathan Lemon 
440f7788e8eSJonathan Lemon 	/*
441f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
442f7788e8eSJonathan Lemon 	 */
443f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
444f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
445f7788e8eSJonathan Lemon 			goto failmem;
446f7788e8eSJonathan Lemon 		}
447f7788e8eSJonathan Lemon 	}
448f7788e8eSJonathan Lemon 
449f7788e8eSJonathan Lemon 	/*
450f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
451f7788e8eSJonathan Lemon 	 */
452f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
453f7788e8eSJonathan Lemon 
454f7788e8eSJonathan Lemon 	/*
4553bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
456f7788e8eSJonathan Lemon 	 */
457f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
458f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
459f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
460f7788e8eSJonathan Lemon 		sc->flags &= FXP_FLAG_SERIAL_MEDIA;
461f7788e8eSJonathan Lemon 
462f7788e8eSJonathan Lemon 	/*
4633bd07cfdSJonathan Lemon 	 * Find out the basic controller type; we currently only
4643bd07cfdSJonathan Lemon 	 * differentiate between a 82557 and greater.
4653bd07cfdSJonathan Lemon 	 */
4663bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
4673bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
4683bd07cfdSJonathan Lemon 		sc->chip = FXP_CHIP_82557;
4693bd07cfdSJonathan Lemon 
4703bd07cfdSJonathan Lemon 	/*
4713bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
4723bd07cfdSJonathan Lemon 	 */
4733bd07cfdSJonathan Lemon 	if (sc->chip != FXP_CHIP_82557) {
4743bd07cfdSJonathan Lemon 		/*
4753bd07cfdSJonathan Lemon 		 * If there is a valid cacheline size (8 or 16 dwords),
4763bd07cfdSJonathan Lemon 		 * then turn on MWI.
4773bd07cfdSJonathan Lemon 		 */
4783bd07cfdSJonathan Lemon 		if (pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
4793bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
4803bd07cfdSJonathan Lemon 
4813bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
4823bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
483e8c8b728SJonathan Lemon #if NVLAN > 0
484e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
485e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
486e8c8b728SJonathan Lemon #endif
4873bd07cfdSJonathan Lemon 	}
4883bd07cfdSJonathan Lemon 
4893bd07cfdSJonathan Lemon 	/*
490f7788e8eSJonathan Lemon 	 * Read MAC address.
491f7788e8eSJonathan Lemon 	 */
492f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
493f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
494f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
495f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
496f7788e8eSJonathan Lemon 	if (bootverbose) {
497f7788e8eSJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x\n",
498f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
499f7788e8eSJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev));
500e8c8b728SJonathan Lemon 		device_printf(dev, "Chip Type: %d\n", sc->chip);
501f7788e8eSJonathan Lemon 	}
502f7788e8eSJonathan Lemon 
503f7788e8eSJonathan Lemon 	/*
504f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
505f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
506f7788e8eSJonathan Lemon 	 *
507f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
508f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
509f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
510f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
511f7788e8eSJonathan Lemon 	 */
512f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
513f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
514f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
515f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
516f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
517f7788e8eSJonathan Lemon 	} else {
518f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
519f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
520f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
5216182fdbdSPeter Wemm 			error = ENXIO;
522ba8c6fd5SDavid Greenman 			goto fail;
523a17c678eSDavid Greenman 		}
524f7788e8eSJonathan Lemon 	}
525dccee1a1SDavid Greenman 
526a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
5276182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
528a17c678eSDavid Greenman 	ifp->if_name = "fxp";
529a17c678eSDavid Greenman 	ifp->if_output = ether_output;
530a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
531fb583156SDavid Greenman 	ifp->if_init = fxp_init;
532ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
533ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
534ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
535ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
536ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
537a17c678eSDavid Greenman 
538dfe61cf1SDavid Greenman 	/*
539dfe61cf1SDavid Greenman 	 * Attach the interface.
540dfe61cf1SDavid Greenman 	 */
54121b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
542f7788e8eSJonathan Lemon 
543e8c8b728SJonathan Lemon #if NVLAN > 0
544e8c8b728SJonathan Lemon 	/*
545e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
546e8c8b728SJonathan Lemon 	 */
547e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
548e8c8b728SJonathan Lemon #endif
549e8c8b728SJonathan Lemon 
550483b9871SDavid Greenman 	/*
5513114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
5523114fdb4SDavid Greenman 	 * TX descriptors.
553483b9871SDavid Greenman 	 */
5543114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
5554a684684SDavid Greenman 
556f7788e8eSJonathan Lemon 	splx(s);
557f7788e8eSJonathan Lemon 	return (0);
558a17c678eSDavid Greenman 
559f7788e8eSJonathan Lemon failmem:
560f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
561f7788e8eSJonathan Lemon 	error = ENOMEM;
562a17c678eSDavid Greenman fail:
563f7788e8eSJonathan Lemon 	splx(s);
564f7788e8eSJonathan Lemon 	fxp_release(sc);
565f7788e8eSJonathan Lemon 	return (error);
566f7788e8eSJonathan Lemon }
567f7788e8eSJonathan Lemon 
568f7788e8eSJonathan Lemon /*
569f7788e8eSJonathan Lemon  * release all resources
570f7788e8eSJonathan Lemon  */
571f7788e8eSJonathan Lemon static void
572f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
573f7788e8eSJonathan Lemon {
574f7788e8eSJonathan Lemon 
575f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
5763bd07cfdSJonathan Lemon 	if (sc->miibus)
577f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
578f7788e8eSJonathan Lemon 
579f7788e8eSJonathan Lemon 	if (sc->cbl_base)
580f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
581f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
582f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
583f7788e8eSJonathan Lemon 	if (sc->mcsp)
584f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
585f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
586f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
587f7788e8eSJonathan Lemon 
588f7788e8eSJonathan Lemon 	if (sc->ih)
589f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
590f7788e8eSJonathan Lemon 	if (sc->irq)
591f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
592f7788e8eSJonathan Lemon 	if (sc->mem)
593f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
5940f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
5956182fdbdSPeter Wemm }
5966182fdbdSPeter Wemm 
5976182fdbdSPeter Wemm /*
5986182fdbdSPeter Wemm  * Detach interface.
5996182fdbdSPeter Wemm  */
6006182fdbdSPeter Wemm static int
6016182fdbdSPeter Wemm fxp_detach(device_t dev)
6026182fdbdSPeter Wemm {
6036182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
604f7788e8eSJonathan Lemon 	int s;
6056182fdbdSPeter Wemm 
606f7788e8eSJonathan Lemon 	s = splimp();
6076182fdbdSPeter Wemm 
6086182fdbdSPeter Wemm 	/*
6096182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
6106182fdbdSPeter Wemm 	 */
6116182fdbdSPeter Wemm 	fxp_stop(sc);
6126182fdbdSPeter Wemm 
6136182fdbdSPeter Wemm 	/*
614f7788e8eSJonathan Lemon 	 * Close down routes etc.
6156182fdbdSPeter Wemm 	 */
616f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
6176182fdbdSPeter Wemm 
6186182fdbdSPeter Wemm 	/*
6196182fdbdSPeter Wemm 	 * Free all media structures.
6206182fdbdSPeter Wemm 	 */
6216182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
6226182fdbdSPeter Wemm 
623f7788e8eSJonathan Lemon 	splx(s);
6246182fdbdSPeter Wemm 
625f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
626f7788e8eSJonathan Lemon 	fxp_release(sc);
6276182fdbdSPeter Wemm 
628f7788e8eSJonathan Lemon 	return (0);
629a17c678eSDavid Greenman }
630a17c678eSDavid Greenman 
631a17c678eSDavid Greenman /*
6324a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
633a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
634a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
635a17c678eSDavid Greenman  */
6366182fdbdSPeter Wemm static int
6376182fdbdSPeter Wemm fxp_shutdown(device_t dev)
638a17c678eSDavid Greenman {
6396182fdbdSPeter Wemm 	/*
6406182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
6416182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
6426182fdbdSPeter Wemm 	 * reboot before the driver initializes.
6436182fdbdSPeter Wemm 	 */
6446182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
645f7788e8eSJonathan Lemon 	return (0);
646a17c678eSDavid Greenman }
647a17c678eSDavid Greenman 
6487dced78aSDavid Greenman /*
6497dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
6507dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
6517dced78aSDavid Greenman  * resume.
6527dced78aSDavid Greenman  */
6537dced78aSDavid Greenman static int
6547dced78aSDavid Greenman fxp_suspend(device_t dev)
6557dced78aSDavid Greenman {
6567dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
657f7788e8eSJonathan Lemon 	int i, s;
6587dced78aSDavid Greenman 
659f7788e8eSJonathan Lemon 	s = splimp();
6607dced78aSDavid Greenman 
6617dced78aSDavid Greenman 	fxp_stop(sc);
6627dced78aSDavid Greenman 
6637dced78aSDavid Greenman 	for (i=0; i<5; i++)
6647dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i*4, 4);
6657dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
6667dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
6677dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
6687dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
6697dced78aSDavid Greenman 
6707dced78aSDavid Greenman 	sc->suspended = 1;
6717dced78aSDavid Greenman 
672f7788e8eSJonathan Lemon 	splx(s);
673f7788e8eSJonathan Lemon 	return (0);
6747dced78aSDavid Greenman }
6757dced78aSDavid Greenman 
6767dced78aSDavid Greenman /*
6777dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
6787dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
6797dced78aSDavid Greenman  * appropriate.
6807dced78aSDavid Greenman  */
6817dced78aSDavid Greenman static int
6827dced78aSDavid Greenman fxp_resume(device_t dev)
6837dced78aSDavid Greenman {
6847dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
6857dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
6867dced78aSDavid Greenman 	u_int16_t pci_command;
687f7788e8eSJonathan Lemon 	int i, s;
6887dced78aSDavid Greenman 
689f7788e8eSJonathan Lemon 	s = splimp();
6907dced78aSDavid Greenman 
6917dced78aSDavid Greenman 	/* better way to do this? */
6927dced78aSDavid Greenman 	for (i=0; i<5; i++)
6937dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i*4, sc->saved_maps[i], 4);
6947dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
6957dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
6967dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
6977dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
6987dced78aSDavid Greenman 
6997dced78aSDavid Greenman 	/* reenable busmastering */
7007dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
7017dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
7027dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
7037dced78aSDavid Greenman 
7047dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
7057dced78aSDavid Greenman 	DELAY(10);
7067dced78aSDavid Greenman 
7077dced78aSDavid Greenman 	/* reinitialize interface if necessary */
7087dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
7097dced78aSDavid Greenman 		fxp_init(sc);
7107dced78aSDavid Greenman 
7117dced78aSDavid Greenman 	sc->suspended = 0;
7127dced78aSDavid Greenman 
713f7788e8eSJonathan Lemon 	splx(s);
714ba8c6fd5SDavid Greenman 	return (0);
715f7788e8eSJonathan Lemon }
716ba8c6fd5SDavid Greenman 
717f7788e8eSJonathan Lemon /*
718f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
719f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
720f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
721f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
722f7788e8eSJonathan Lemon  * every 16 bits of data.
723f7788e8eSJonathan Lemon  */
724f7788e8eSJonathan Lemon static u_int16_t
725f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
726f7788e8eSJonathan Lemon {
727f7788e8eSJonathan Lemon 	u_int16_t reg, data;
728f7788e8eSJonathan Lemon 	int x;
729ba8c6fd5SDavid Greenman 
730f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
731f7788e8eSJonathan Lemon 	/*
732f7788e8eSJonathan Lemon 	 * Shift in read opcode.
733f7788e8eSJonathan Lemon 	 */
734f7788e8eSJonathan Lemon 	for (x = 1 << 2; x; x >>= 1) {
735f7788e8eSJonathan Lemon 		if (FXP_EEPROM_OPC_READ & x)
736f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
737f7788e8eSJonathan Lemon 		else
738f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
739f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
740f7788e8eSJonathan Lemon 		DELAY(1);
741f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
742f7788e8eSJonathan Lemon 		DELAY(1);
743f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
744f7788e8eSJonathan Lemon 		DELAY(1);
745f7788e8eSJonathan Lemon 	}
746f7788e8eSJonathan Lemon 	/*
747f7788e8eSJonathan Lemon 	 * Shift in address.
748f7788e8eSJonathan Lemon 	 */
749f7788e8eSJonathan Lemon 	data = 0;
750f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
751f7788e8eSJonathan Lemon 		if (offset & x)
752f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
753f7788e8eSJonathan Lemon 		else
754f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
755f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
756f7788e8eSJonathan Lemon 		DELAY(1);
757f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
758f7788e8eSJonathan Lemon 		DELAY(1);
759f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
760f7788e8eSJonathan Lemon 		DELAY(1);
761f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
762f7788e8eSJonathan Lemon 		data++;
763f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
764f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
765f7788e8eSJonathan Lemon 			break;
766f7788e8eSJonathan Lemon 		}
767f7788e8eSJonathan Lemon 	}
768f7788e8eSJonathan Lemon 	/*
769f7788e8eSJonathan Lemon 	 * Shift out data.
770f7788e8eSJonathan Lemon 	 */
771f7788e8eSJonathan Lemon 	data = 0;
772f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
773f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
774f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
775f7788e8eSJonathan Lemon 		DELAY(1);
776f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
777f7788e8eSJonathan Lemon 			data |= x;
778f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
779f7788e8eSJonathan Lemon 		DELAY(1);
780f7788e8eSJonathan Lemon 	}
781f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
782f7788e8eSJonathan Lemon 	DELAY(1);
783f7788e8eSJonathan Lemon 
784f7788e8eSJonathan Lemon 	return (data);
785ba8c6fd5SDavid Greenman }
786ba8c6fd5SDavid Greenman 
787ba8c6fd5SDavid Greenman /*
788e9bf2fa7SDavid Greenman  * From NetBSD:
789e9bf2fa7SDavid Greenman  *
790e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
791e9bf2fa7SDavid Greenman  *
792e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
793e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
794e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
795e9bf2fa7SDavid Greenman  *
796e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
797e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
798e9bf2fa7SDavid Greenman  *
799e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
800e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
801e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
802e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
803e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
804e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
805e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
806e9bf2fa7SDavid Greenman  *
807e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
808e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
809e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
810e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
811e9bf2fa7SDavid Greenman  *
812e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
813e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
814e9bf2fa7SDavid Greenman  */
815e9bf2fa7SDavid Greenman static void
816f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
817e9bf2fa7SDavid Greenman {
818e9bf2fa7SDavid Greenman 
819f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
820f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
821f7788e8eSJonathan Lemon 
822f7788e8eSJonathan Lemon 	/* autosize */
823f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
824e9bf2fa7SDavid Greenman }
825f7788e8eSJonathan Lemon 
826ba8c6fd5SDavid Greenman static void
827f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
828ba8c6fd5SDavid Greenman {
829f7788e8eSJonathan Lemon 	int i;
830ba8c6fd5SDavid Greenman 
831f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
832f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
833ba8c6fd5SDavid Greenman }
834ba8c6fd5SDavid Greenman 
835a17c678eSDavid Greenman /*
836a17c678eSDavid Greenman  * Start packet transmission on the interface.
837a17c678eSDavid Greenman  */
838a17c678eSDavid Greenman static void
839f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
840a17c678eSDavid Greenman {
8419b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
842a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
843a17c678eSDavid Greenman 
844a17c678eSDavid Greenman 	/*
845483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
846483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
847483b9871SDavid Greenman 	 * of the command chain).
848a17c678eSDavid Greenman 	 */
8490f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
850a17c678eSDavid Greenman 		return;
8510f4dc94cSChuck Paterson 	}
8521cd443acSDavid Greenman 
853483b9871SDavid Greenman 	txp = NULL;
854483b9871SDavid Greenman 
855483b9871SDavid Greenman 	/*
856483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
857483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
8583114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
8593114fdb4SDavid Greenman 	 *       a NOP command when needed.
860483b9871SDavid Greenman 	 */
8613114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
862483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
863483b9871SDavid Greenman 		int segment;
864483b9871SDavid Greenman 
865dfe61cf1SDavid Greenman 		/*
866dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
867dfe61cf1SDavid Greenman 		 */
8686318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
869a17c678eSDavid Greenman 
870dfe61cf1SDavid Greenman 		/*
871483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
872dfe61cf1SDavid Greenman 		 */
873a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
874a17c678eSDavid Greenman 
875a17c678eSDavid Greenman 		/*
876a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
877483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
878a17c678eSDavid Greenman 		 * and size of the mbuf.
879a17c678eSDavid Greenman 		 */
88023a0ed7cSDavid Greenman tbdinit:
881a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
882a17c678eSDavid Greenman 			if (m->m_len != 0) {
883a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
884a17c678eSDavid Greenman 					break;
885a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
886a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
887a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
888a17c678eSDavid Greenman 				segment++;
889a17c678eSDavid Greenman 			}
890a17c678eSDavid Greenman 		}
891fb583156SDavid Greenman 		if (m != NULL) {
89223a0ed7cSDavid Greenman 			struct mbuf *mn;
89323a0ed7cSDavid Greenman 
894a17c678eSDavid Greenman 			/*
8953bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
8963bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
8973bd07cfdSJonathan Lemon 			 * new buffers.
898a17c678eSDavid Greenman 			 */
89923a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
90023a0ed7cSDavid Greenman 			if (mn == NULL) {
90123a0ed7cSDavid Greenman 				m_freem(mb_head);
902483b9871SDavid Greenman 				break;
903a17c678eSDavid Greenman 			}
90423a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
90523a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
90623a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
90723a0ed7cSDavid Greenman 					m_freem(mn);
90823a0ed7cSDavid Greenman 					m_freem(mb_head);
909483b9871SDavid Greenman 					break;
91023a0ed7cSDavid Greenman 				}
91123a0ed7cSDavid Greenman 			}
912ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
913ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
91423a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
91523a0ed7cSDavid Greenman 			m_freem(mb_head);
91623a0ed7cSDavid Greenman 			mb_head = mn;
91723a0ed7cSDavid Greenman 			goto tbdinit;
91823a0ed7cSDavid Greenman 		}
91923a0ed7cSDavid Greenman 
92023a0ed7cSDavid Greenman 		txp->tbd_number = segment;
9211cd443acSDavid Greenman 		txp->mb_head = mb_head;
922a17c678eSDavid Greenman 		txp->cb_status = 0;
9233114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
924a17c678eSDavid Greenman 			txp->cb_command =
9253bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
9263bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
9273114fdb4SDavid Greenman 		} else {
9283114fdb4SDavid Greenman 			txp->cb_command =
9293bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
9303bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
9313114fdb4SDavid Greenman 			/*
9323bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
9333bd07cfdSJonathan Lemon 			 * from the card again.
9343114fdb4SDavid Greenman 			 */
9353114fdb4SDavid Greenman 			ifp->if_timer = 5;
9363114fdb4SDavid Greenman 		}
937f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
938a17c678eSDavid Greenman 
939a17c678eSDavid Greenman 		/*
940483b9871SDavid Greenman 		 * Advance the end of list forward.
941a17c678eSDavid Greenman 		 */
94206175228SAndrew Gallatin 
94306175228SAndrew Gallatin #ifdef __alpha__
94406175228SAndrew Gallatin 		/*
94506175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
94606175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
94706175228SAndrew Gallatin 		 * up the status while we update the command field.
94806175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
94906175228SAndrew Gallatin 		 */
95006175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
95106175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
95206175228SAndrew Gallatin #else
953a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
95406175228SAndrew Gallatin #endif /*__alpha__*/
955a17c678eSDavid Greenman 		sc->cbl_last = txp;
956a17c678eSDavid Greenman 
957a17c678eSDavid Greenman 		/*
9581cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
9591cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
960483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
961a17c678eSDavid Greenman 		 */
9621cd443acSDavid Greenman 		if (sc->tx_queued == 0)
963a17c678eSDavid Greenman 			sc->cbl_first = txp;
964a17c678eSDavid Greenman 
9651cd443acSDavid Greenman 		sc->tx_queued++;
9661cd443acSDavid Greenman 
967a17c678eSDavid Greenman 		/*
968a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
969a17c678eSDavid Greenman 		 */
970fb583156SDavid Greenman 		if (ifp->if_bpf)
97194927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
972483b9871SDavid Greenman 	}
973483b9871SDavid Greenman 
974483b9871SDavid Greenman 	/*
975483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
976483b9871SDavid Greenman 	 * going again if suspended.
977483b9871SDavid Greenman 	 */
978483b9871SDavid Greenman 	if (txp != NULL) {
979483b9871SDavid Greenman 		fxp_scb_wait(sc);
980483b9871SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
981483b9871SDavid Greenman 	}
982a17c678eSDavid Greenman }
983a17c678eSDavid Greenman 
984a17c678eSDavid Greenman /*
9859c7d2607SDavid Greenman  * Process interface interrupts.
986a17c678eSDavid Greenman  */
98794927790SDavid Greenman static void
988f7788e8eSJonathan Lemon fxp_intr(void *xsc)
989a17c678eSDavid Greenman {
990f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
991ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
9921cd443acSDavid Greenman 	u_int8_t statack;
9930f4dc94cSChuck Paterson 
994a17c678eSDavid Greenman 
995b184b38eSDavid Greenman 	if (sc->suspended) {
996b184b38eSDavid Greenman 		return;
997b184b38eSDavid Greenman 	}
998b184b38eSDavid Greenman 
999b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1000a17c678eSDavid Greenman 		/*
1001a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1002a17c678eSDavid Greenman 		 */
1003ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1004a17c678eSDavid Greenman 
1005a17c678eSDavid Greenman 		/*
10063114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
100706936301SBill Paul 		 *
100806936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
100906936301SBill Paul 		 * be that this event (control unit not ready) was not
101006936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
101106936301SBill Paul 		 * The exact sequence of events that occur when the interface
101206936301SBill Paul 		 * is brought up are different now, and if this event
101306936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
101406936301SBill Paul 		 * can stall for several seconds. The result is that no
101506936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
101606936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
10173114fdb4SDavid Greenman 		 */
101806936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
10193114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
10203114fdb4SDavid Greenman 
10213114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
10223114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
10233114fdb4SDavid Greenman 			    txp = txp->next) {
10243114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
10253114fdb4SDavid Greenman 					m_freem(txp->mb_head);
10263114fdb4SDavid Greenman 					txp->mb_head = NULL;
10273114fdb4SDavid Greenman 				}
10283114fdb4SDavid Greenman 				sc->tx_queued--;
10293114fdb4SDavid Greenman 			}
10303114fdb4SDavid Greenman 			sc->cbl_first = txp;
10313114fdb4SDavid Greenman 			ifp->if_timer = 0;
10323114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
10333114fdb4SDavid Greenman 				if (sc->need_mcsetup)
10343114fdb4SDavid Greenman 					fxp_mc_setup(sc);
10353114fdb4SDavid Greenman 			}
10363114fdb4SDavid Greenman 			/*
10373114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
10383114fdb4SDavid Greenman 			 */
10393114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
10403114fdb4SDavid Greenman 				fxp_start(ifp);
10413114fdb4SDavid Greenman 		}
10423114fdb4SDavid Greenman 		/*
1043a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1044a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1045a17c678eSDavid Greenman 		 * re-start the receiver.
1046a17c678eSDavid Greenman 		 */
1047a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1048a17c678eSDavid Greenman 			struct mbuf *m;
1049a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1050a17c678eSDavid Greenman rcvloop:
1051a17c678eSDavid Greenman 			m = sc->rfa_headm;
1052ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1053ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1054a17c678eSDavid Greenman 
1055a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1056dfe61cf1SDavid Greenman 				/*
1057dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1058dfe61cf1SDavid Greenman 				 */
1059a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1060a17c678eSDavid Greenman 				m->m_next = NULL;
1061a17c678eSDavid Greenman 
1062dfe61cf1SDavid Greenman 				/*
1063ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1064ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1065ba8c6fd5SDavid Greenman 				 * instead.
1066dfe61cf1SDavid Greenman 				 */
1067a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1068a17c678eSDavid Greenman 					struct ether_header *eh;
1069aed53495SDavid Greenman 					int total_len;
1070a17c678eSDavid Greenman 
1071ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1072ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1073ba8c6fd5SDavid Greenman 					if (total_len <
1074ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
107506339180SDavid Greenman 						m_freem(m);
107606339180SDavid Greenman 						goto rcvloop;
107706339180SDavid Greenman 					}
1078e8c8b728SJonathan Lemon #if NVLAN > 0
1079e8c8b728SJonathan Lemon 					/*
1080e8c8b728SJonathan Lemon 					 * Drop the packet if it has CRC
1081e8c8b728SJonathan Lemon 					 * errors.  This test is only needed
1082e8c8b728SJonathan Lemon 					 * when doing 802.1q VLAN on the 82557
1083e8c8b728SJonathan Lemon 					 * chip.
1084e8c8b728SJonathan Lemon 					 */
1085e8c8b728SJonathan Lemon 					if (rfa->rfa_status &
1086e8c8b728SJonathan Lemon 					    FXP_RFA_STATUS_CRC) {
1087e8c8b728SJonathan Lemon 						m_freem(m);
1088e8c8b728SJonathan Lemon 						goto rcvloop;
1089e8c8b728SJonathan Lemon 					}
1090e8c8b728SJonathan Lemon #endif
1091a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
10922e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1093a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1094ba8c6fd5SDavid Greenman 					m->m_data +=
1095ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1096ab090e5bSLuigi Rizzo 					m->m_len -=
1097ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1098ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1099a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1100a17c678eSDavid Greenman 				}
1101a17c678eSDavid Greenman 				goto rcvloop;
1102a17c678eSDavid Greenman 			}
1103a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1104ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1105ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1106ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1107ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
1108ba8c6fd5SDavid Greenman 				CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1109ba8c6fd5SDavid Greenman 				    FXP_SCB_COMMAND_RU_START);
1110a17c678eSDavid Greenman 			}
1111a17c678eSDavid Greenman 		}
1112a17c678eSDavid Greenman 	}
1113a17c678eSDavid Greenman }
1114a17c678eSDavid Greenman 
1115dfe61cf1SDavid Greenman /*
1116dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1117dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1118dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1119dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1120dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1121dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1122dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1123dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1124dfe61cf1SDavid Greenman  * them again next time.
1125dfe61cf1SDavid Greenman  */
1126303b270bSEivind Eklund static void
1127f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1128a17c678eSDavid Greenman {
1129f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1130ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1131a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1132c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1133f7788e8eSJonathan Lemon 	int s;
1134a17c678eSDavid Greenman 
1135a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1136a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1137397f9dfeSDavid Greenman 	if (sp->rx_good) {
1138397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1139397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1140397f9dfeSDavid Greenman 	} else {
1141c8cc6fcaSDavid Greenman 		/*
1142c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1143c8cc6fcaSDavid Greenman 		 */
1144397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1145397f9dfeSDavid Greenman 	}
11463ba65732SDavid Greenman 	ifp->if_ierrors +=
11473ba65732SDavid Greenman 	    sp->rx_crc_errors +
11483ba65732SDavid Greenman 	    sp->rx_alignment_errors +
11493ba65732SDavid Greenman 	    sp->rx_rnr_errors +
11506e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1151a17c678eSDavid Greenman 	/*
1152f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1153f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1154f9be9005SDavid Greenman 	 */
1155f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1156f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1157f9be9005SDavid Greenman 		if (tx_threshold < 192)
1158f9be9005SDavid Greenman 			tx_threshold += 64;
1159f9be9005SDavid Greenman 	}
1160f7788e8eSJonathan Lemon 	s = splimp();
1161397f9dfeSDavid Greenman 	/*
1162c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1163c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1164c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1165c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1166c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1167c8cc6fcaSDavid Greenman 	 */
1168c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1169c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1170c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1171c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1172c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1173c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1174c8cc6fcaSDavid Greenman 		}
1175c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1176c8cc6fcaSDavid Greenman 	}
1177c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1178c8cc6fcaSDavid Greenman 	/*
1179397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1180397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1181397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1182397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1183397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1184397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1185397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1186397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1187397f9dfeSDavid Greenman 	 */
1188397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1189397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1190397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1191397f9dfeSDavid Greenman 	}
1192f9be9005SDavid Greenman 	/*
11933ba65732SDavid Greenman 	 * If there is no pending command, start another stats
11943ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1195a17c678eSDavid Greenman 	 */
1196397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1197a17c678eSDavid Greenman 		/*
1198397f9dfeSDavid Greenman 		 * Start another stats dump.
1199a17c678eSDavid Greenman 		 */
1200ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1201ba8c6fd5SDavid Greenman 		    FXP_SCB_COMMAND_CU_DUMPRESET);
1202dfe61cf1SDavid Greenman 	} else {
1203dfe61cf1SDavid Greenman 		/*
1204dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1205dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
12063ba65732SDavid Greenman 		 * next timer event to update them.
1207dfe61cf1SDavid Greenman 		 */
1208dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1209f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1210dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
12113ba65732SDavid Greenman 
1212dfe61cf1SDavid Greenman 		sp->rx_good = 0;
12133ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
12143ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
12153ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
12163ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1217dfe61cf1SDavid Greenman 	}
1218f7788e8eSJonathan Lemon 
1219f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1220f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
1221f7788e8eSJonathan Lemon 
1222a17c678eSDavid Greenman 	/*
1223a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1224a17c678eSDavid Greenman 	 */
1225f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1226a17c678eSDavid Greenman }
1227a17c678eSDavid Greenman 
1228a17c678eSDavid Greenman /*
1229a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1230a17c678eSDavid Greenman  * the interface.
1231a17c678eSDavid Greenman  */
1232a17c678eSDavid Greenman static void
1233f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1234a17c678eSDavid Greenman {
1235ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
12363ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
12373ba65732SDavid Greenman 	int i;
1238a17c678eSDavid Greenman 
12390f4dc94cSChuck Paterson 
12407dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
12417dced78aSDavid Greenman 	ifp->if_timer = 0;
12427dced78aSDavid Greenman 
1243a17c678eSDavid Greenman 	/*
1244a17c678eSDavid Greenman 	 * Cancel stats updater.
1245a17c678eSDavid Greenman 	 */
1246f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
12473ba65732SDavid Greenman 
12483ba65732SDavid Greenman 	/*
12493ba65732SDavid Greenman 	 * Issue software reset
12503ba65732SDavid Greenman 	 */
1251ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1252a17c678eSDavid Greenman 	DELAY(10);
1253a17c678eSDavid Greenman 
12543ba65732SDavid Greenman 	/*
12553ba65732SDavid Greenman 	 * Release any xmit buffers.
12563ba65732SDavid Greenman 	 */
1257da91462dSDavid Greenman 	txp = sc->cbl_base;
1258da91462dSDavid Greenman 	if (txp != NULL) {
1259da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1260da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1261da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1262da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1263da91462dSDavid Greenman 			}
1264da91462dSDavid Greenman 		}
12653ba65732SDavid Greenman 	}
12663ba65732SDavid Greenman 	sc->tx_queued = 0;
12673ba65732SDavid Greenman 
12683ba65732SDavid Greenman 	/*
12693ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
12703ba65732SDavid Greenman 	 */
12713ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
12723ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
12733ba65732SDavid Greenman 	sc->rfa_headm = NULL;
12743ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
12753ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
12763ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
12773ba65732SDavid Greenman 			/*
12783ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
12793ba65732SDavid Greenman 			 * and we just freed all the buffers we need
12803ba65732SDavid Greenman 			 * above.
12813ba65732SDavid Greenman 			 */
12823ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
12833ba65732SDavid Greenman 		}
12843ba65732SDavid Greenman 	}
1285a17c678eSDavid Greenman }
1286a17c678eSDavid Greenman 
1287a17c678eSDavid Greenman /*
1288a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1289a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1290a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1291a17c678eSDavid Greenman  * card has wedged for some reason.
1292a17c678eSDavid Greenman  */
1293a17c678eSDavid Greenman static void
1294f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1295a17c678eSDavid Greenman {
1296ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1297ba8c6fd5SDavid Greenman 
1298f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
12994a5f1499SDavid Greenman 	ifp->if_oerrors++;
1300a17c678eSDavid Greenman 
1301ba8c6fd5SDavid Greenman 	fxp_init(sc);
1302a17c678eSDavid Greenman }
1303a17c678eSDavid Greenman 
1304a17c678eSDavid Greenman static void
1305f7788e8eSJonathan Lemon fxp_init(void *xsc)
1306a17c678eSDavid Greenman {
1307fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1308ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1309a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1310a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1311a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1312f7788e8eSJonathan Lemon 	int i, prm, s;
1313a17c678eSDavid Greenman 
1314f7788e8eSJonathan Lemon 	s = splimp();
1315a17c678eSDavid Greenman 	/*
13163ba65732SDavid Greenman 	 * Cancel any pending I/O
1317a17c678eSDavid Greenman 	 */
13183ba65732SDavid Greenman 	fxp_stop(sc);
1319a17c678eSDavid Greenman 
1320a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1321a17c678eSDavid Greenman 
1322a17c678eSDavid Greenman 	/*
1323a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1324a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1325a17c678eSDavid Greenman 	 */
1326ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1327ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1328a17c678eSDavid Greenman 
1329ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1330ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1331a17c678eSDavid Greenman 
1332a17c678eSDavid Greenman 	/*
1333a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1334a17c678eSDavid Greenman 	 */
1335ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1336ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
1337ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1338a17c678eSDavid Greenman 
1339a17c678eSDavid Greenman 	/*
1340a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1341a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1342a17c678eSDavid Greenman 	 * later.
1343a17c678eSDavid Greenman 	 */
1344a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1345a17c678eSDavid Greenman 
1346a17c678eSDavid Greenman 	/*
1347a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1348a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1349a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1350a17c678eSDavid Greenman 	 */
1351d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1352d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1353397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1354a17c678eSDavid Greenman 
1355a17c678eSDavid Greenman 	cbp->cb_status =	0;
1356a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1357a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1358a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1359001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1360001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1361a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1362f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1363f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1364f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1365f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1366001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1367001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1368f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1369a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1370f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1371f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
13723114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1373f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1374f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1375f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
1376e8c8b728SJonathan Lemon #if NVLAN > 0
1377e8c8b728SJonathan Lemon 	cbp->save_bf =		sc->chip == FXP_CHIP_82557 ? 1 : prm;
1378e8c8b728SJonathan Lemon #else
1379a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1380e8c8b728SJonathan Lemon #endif
1381a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1382f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1383f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1384f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1385f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1386f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1387f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1388f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1389f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1390f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1391f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1392a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1393a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1394a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1395a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1396a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1397a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1398a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1399a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1400f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1401f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1402f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1403f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1404f7788e8eSJonathan Lemon 
1405a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1406a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1407a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1408f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1409f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1410f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1411f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1412a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
14133ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1414a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1415f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1416a17c678eSDavid Greenman 
14173bd07cfdSJonathan Lemon 	if (sc->chip == FXP_CHIP_82557) {
14183bd07cfdSJonathan Lemon 		/*
14193bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
14203bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
14213bd07cfdSJonathan Lemon 		 */
14223bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
14233bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
14243bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
14253bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
14263bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
14273bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
14283bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
14293bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
14303bd07cfdSJonathan Lemon 	} else {
14313bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
14323bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
14333bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
14343bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
14353bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
14363bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
14373bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
14383bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
14393bd07cfdSJonathan Lemon 	}
14403bd07cfdSJonathan Lemon 
1441a17c678eSDavid Greenman 	/*
1442a17c678eSDavid Greenman 	 * Start the config command/DMA.
1443a17c678eSDavid Greenman 	 */
1444ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1445397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
1446ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1447a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
14487dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1449a17c678eSDavid Greenman 
1450a17c678eSDavid Greenman 	/*
1451a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1452a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1453a17c678eSDavid Greenman 	 */
1454a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1455a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1456a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1457a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1458d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1459d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1460a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1461a17c678eSDavid Greenman 
1462a17c678eSDavid Greenman 	/*
1463a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1464a17c678eSDavid Greenman 	 */
1465ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1466ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1467a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
14687dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1469a17c678eSDavid Greenman 
1470a17c678eSDavid Greenman 	/*
1471a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1472a17c678eSDavid Greenman 	 */
1473a17c678eSDavid Greenman 
1474a17c678eSDavid Greenman 	txp = sc->cbl_base;
1475a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1476a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1477a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1478a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
14793bd07cfdSJonathan Lemon 		txp[i].link_addr =
14803bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
14813bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
14823bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
14833bd07cfdSJonathan Lemon 		else
1484a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1485a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1486a17c678eSDavid Greenman 	}
1487a17c678eSDavid Greenman 	/*
1488397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1489a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1490a17c678eSDavid Greenman 	 */
1491a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1492a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1493397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1494a17c678eSDavid Greenman 
1495ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1496ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1497a17c678eSDavid Greenman 
1498a17c678eSDavid Greenman 	/*
1499a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1500a17c678eSDavid Greenman 	 */
1501ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1502ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1503ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
1504ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1505a17c678eSDavid Greenman 
1506dccee1a1SDavid Greenman 	/*
1507ba8c6fd5SDavid Greenman 	 * Set current media.
1508dccee1a1SDavid Greenman 	 */
1509f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1510f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1511dccee1a1SDavid Greenman 
1512a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1513a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1514e8c8b728SJonathan Lemon 
1515e8c8b728SJonathan Lemon 	/*
1516e8c8b728SJonathan Lemon 	 * Enable interrupts.
1517e8c8b728SJonathan Lemon 	 */
1518e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1519f7788e8eSJonathan Lemon 	splx(s);
1520a17c678eSDavid Greenman 
1521a17c678eSDavid Greenman 	/*
1522a17c678eSDavid Greenman 	 * Start stats updater.
1523a17c678eSDavid Greenman 	 */
1524f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1525f7788e8eSJonathan Lemon }
1526f7788e8eSJonathan Lemon 
1527f7788e8eSJonathan Lemon static int
1528f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1529f7788e8eSJonathan Lemon {
1530f7788e8eSJonathan Lemon 
1531f7788e8eSJonathan Lemon 	return (0);
1532a17c678eSDavid Greenman }
1533a17c678eSDavid Greenman 
1534303b270bSEivind Eklund static void
1535f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1536ba8c6fd5SDavid Greenman {
1537ba8c6fd5SDavid Greenman 
1538f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1539ba8c6fd5SDavid Greenman }
1540ba8c6fd5SDavid Greenman 
1541ba8c6fd5SDavid Greenman /*
1542ba8c6fd5SDavid Greenman  * Change media according to request.
1543ba8c6fd5SDavid Greenman  */
1544f7788e8eSJonathan Lemon static int
1545f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1546ba8c6fd5SDavid Greenman {
1547ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1548f7788e8eSJonathan Lemon 	struct mii_data *mii;
1549ba8c6fd5SDavid Greenman 
1550f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1551f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1552ba8c6fd5SDavid Greenman 	return (0);
1553ba8c6fd5SDavid Greenman }
1554ba8c6fd5SDavid Greenman 
1555ba8c6fd5SDavid Greenman /*
1556ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1557ba8c6fd5SDavid Greenman  */
1558f7788e8eSJonathan Lemon static void
1559f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1560ba8c6fd5SDavid Greenman {
1561ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1562f7788e8eSJonathan Lemon 	struct mii_data *mii;
1563ba8c6fd5SDavid Greenman 
1564f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1565f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1566f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1567f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
1568ba8c6fd5SDavid Greenman }
1569ba8c6fd5SDavid Greenman 
1570a17c678eSDavid Greenman /*
1571a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1572a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1573a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1574dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1575a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1576a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1577a17c678eSDavid Greenman  */
1578a17c678eSDavid Greenman static int
1579f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1580a17c678eSDavid Greenman {
1581ba8c6fd5SDavid Greenman 	u_int32_t v;
1582a17c678eSDavid Greenman 	struct mbuf *m;
1583a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1584a17c678eSDavid Greenman 
1585a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1586a17c678eSDavid Greenman 	if (m != NULL) {
1587a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1588a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1589a17c678eSDavid Greenman 			m_freem(m);
1590eadd5e3aSDavid Greenman 			if (oldm == NULL)
1591eadd5e3aSDavid Greenman 				return 1;
1592a17c678eSDavid Greenman 			m = oldm;
1593eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1594a17c678eSDavid Greenman 		}
1595a17c678eSDavid Greenman 	} else {
1596eadd5e3aSDavid Greenman 		if (oldm == NULL)
1597a17c678eSDavid Greenman 			return 1;
1598eadd5e3aSDavid Greenman 		m = oldm;
1599eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1600eadd5e3aSDavid Greenman 	}
1601ba8c6fd5SDavid Greenman 
1602ba8c6fd5SDavid Greenman 	/*
1603ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1604ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1605ba8c6fd5SDavid Greenman 	 */
1606ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1607ba8c6fd5SDavid Greenman 
1608eadd5e3aSDavid Greenman 	/*
1609eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1610eadd5e3aSDavid Greenman 	 * data start past it.
1611eadd5e3aSDavid Greenman 	 */
1612a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1613eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
16144fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1615eadd5e3aSDavid Greenman 
1616ba8c6fd5SDavid Greenman 	/*
1617ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1618ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1619ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1620ba8c6fd5SDavid Greenman 	 */
16214fc1dda9SAndrew Gallatin 
1622a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1623a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1624a17c678eSDavid Greenman 	rfa->actual_size = 0;
1625ba8c6fd5SDavid Greenman 
1626ba8c6fd5SDavid Greenman 	v = -1;
16274fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
16284fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1629ba8c6fd5SDavid Greenman 
1630dfe61cf1SDavid Greenman 	/*
1631dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1632dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1633dfe61cf1SDavid Greenman 	 */
1634a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1635ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1636ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1637a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1638ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
16394fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1640aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1641a17c678eSDavid Greenman 	} else {
1642a17c678eSDavid Greenman 		sc->rfa_headm = m;
1643a17c678eSDavid Greenman 	}
1644a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1645a17c678eSDavid Greenman 
1646dfe61cf1SDavid Greenman 	return (m == oldm);
1647a17c678eSDavid Greenman }
1648a17c678eSDavid Greenman 
16496ebc3153SDavid Greenman static volatile int
1650f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1651dccee1a1SDavid Greenman {
1652f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1653dccee1a1SDavid Greenman 	int count = 10000;
16546ebc3153SDavid Greenman 	int value;
1655dccee1a1SDavid Greenman 
1656ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1657ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1658dccee1a1SDavid Greenman 
1659ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1660ba8c6fd5SDavid Greenman 	    && count--)
16616ebc3153SDavid Greenman 		DELAY(10);
1662dccee1a1SDavid Greenman 
1663dccee1a1SDavid Greenman 	if (count <= 0)
1664f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1665dccee1a1SDavid Greenman 
16666ebc3153SDavid Greenman 	return (value & 0xffff);
1667dccee1a1SDavid Greenman }
1668dccee1a1SDavid Greenman 
1669dccee1a1SDavid Greenman static void
1670f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1671dccee1a1SDavid Greenman {
1672f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1673dccee1a1SDavid Greenman 	int count = 10000;
1674dccee1a1SDavid Greenman 
1675ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1676ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1677ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1678dccee1a1SDavid Greenman 
1679ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1680ba8c6fd5SDavid Greenman 	    count--)
16816ebc3153SDavid Greenman 		DELAY(10);
1682dccee1a1SDavid Greenman 
1683dccee1a1SDavid Greenman 	if (count <= 0)
1684f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1685dccee1a1SDavid Greenman }
1686dccee1a1SDavid Greenman 
1687dccee1a1SDavid Greenman static int
1688f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1689a17c678eSDavid Greenman {
16909b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1691a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1692f7788e8eSJonathan Lemon 	struct mii_data *mii;
1693f7788e8eSJonathan Lemon 	int s, error = 0;
1694a17c678eSDavid Greenman 
1695f7788e8eSJonathan Lemon 	s = splimp();
1696a17c678eSDavid Greenman 
1697a17c678eSDavid Greenman 	switch (command) {
1698a17c678eSDavid Greenman 	case SIOCSIFADDR:
1699a17c678eSDavid Greenman 	case SIOCGIFADDR:
1700fb583156SDavid Greenman 	case SIOCSIFMTU:
1701fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1702a17c678eSDavid Greenman 		break;
1703a17c678eSDavid Greenman 
1704a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1705f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1706f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1707f7788e8eSJonathan Lemon 		else
1708f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1709a17c678eSDavid Greenman 
1710a17c678eSDavid Greenman 		/*
1711a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1712a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1713a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1714a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1715a17c678eSDavid Greenman 		 */
1716a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1717fb583156SDavid Greenman 			fxp_init(sc);
1718a17c678eSDavid Greenman 		} else {
1719a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
17204a5f1499SDavid Greenman 				fxp_stop(sc);
1721a17c678eSDavid Greenman 		}
1722a17c678eSDavid Greenman 		break;
1723a17c678eSDavid Greenman 
1724a17c678eSDavid Greenman 	case SIOCADDMULTI:
1725a17c678eSDavid Greenman 	case SIOCDELMULTI:
1726f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1727f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1728f7788e8eSJonathan Lemon 		else
1729f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1730a17c678eSDavid Greenman 		/*
1731a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1732a17c678eSDavid Greenman 		 * accordingly.
1733a17c678eSDavid Greenman 		 */
1734f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1735397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1736397f9dfeSDavid Greenman 		/*
1737f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1738397f9dfeSDavid Greenman 		 * again rather than else {}.
1739397f9dfeSDavid Greenman 		 */
1740f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1741fb583156SDavid Greenman 			fxp_init(sc);
1742a17c678eSDavid Greenman 		error = 0;
1743ba8c6fd5SDavid Greenman 		break;
1744ba8c6fd5SDavid Greenman 
1745ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1746ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1747f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
1748f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
1749f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
1750f7788e8eSJonathan Lemon                             &mii->mii_media, command);
1751f7788e8eSJonathan Lemon 		} else {
1752ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1753f7788e8eSJonathan Lemon 		}
1754a17c678eSDavid Greenman 		break;
1755a17c678eSDavid Greenman 
1756a17c678eSDavid Greenman 	default:
1757a17c678eSDavid Greenman 		error = EINVAL;
1758a17c678eSDavid Greenman 	}
1759f7788e8eSJonathan Lemon 	splx(s);
1760a17c678eSDavid Greenman 	return (error);
1761a17c678eSDavid Greenman }
1762397f9dfeSDavid Greenman 
1763397f9dfeSDavid Greenman /*
1764397f9dfeSDavid Greenman  * Program the multicast filter.
1765397f9dfeSDavid Greenman  *
1766397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1767397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
17683114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1769397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1770dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1771397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1772397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1773397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1774397f9dfeSDavid Greenman  *
1775397f9dfeSDavid Greenman  * This function must be called at splimp.
1776397f9dfeSDavid Greenman  */
1777397f9dfeSDavid Greenman static void
1778f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
1779397f9dfeSDavid Greenman {
1780397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1781397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1782397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1783397f9dfeSDavid Greenman 	int nmcasts;
17847dced78aSDavid Greenman 	int count;
1785397f9dfeSDavid Greenman 
17863114fdb4SDavid Greenman 	/*
17873114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
17883114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
17893114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
17903114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
17913114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
17923114fdb4SDavid Greenman 	 */
1793397f9dfeSDavid Greenman 	if (sc->tx_queued) {
17943114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
17953114fdb4SDavid Greenman 
17963114fdb4SDavid Greenman 		/*
17973114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
17983114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
17993114fdb4SDavid Greenman 		 */
18003114fdb4SDavid Greenman 		if (sc->need_mcsetup)
18013114fdb4SDavid Greenman 			return;
1802397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
18033114fdb4SDavid Greenman 
18043114fdb4SDavid Greenman 		/*
18053114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
18063114fdb4SDavid Greenman 		 * TX commands have been processed.
18073114fdb4SDavid Greenman 		 */
18083114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
18093114fdb4SDavid Greenman 		txp->mb_head = NULL;
18103114fdb4SDavid Greenman 		txp->cb_status = 0;
1811e8c8b728SJonathan Lemon 		txp->cb_command = FXP_CB_COMMAND_NOP |
1812e8c8b728SJonathan Lemon 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
18133114fdb4SDavid Greenman 		/*
18143114fdb4SDavid Greenman 		 * Advance the end of list forward.
18153114fdb4SDavid Greenman 		 */
18163114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
18173114fdb4SDavid Greenman 		sc->cbl_last = txp;
18183114fdb4SDavid Greenman 		sc->tx_queued++;
18193114fdb4SDavid Greenman 		/*
18203114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
18213114fdb4SDavid Greenman 		 */
18223114fdb4SDavid Greenman 		fxp_scb_wait(sc);
18233114fdb4SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
18243114fdb4SDavid Greenman 		/*
18253114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
18263114fdb4SDavid Greenman 		 * card again.
18273114fdb4SDavid Greenman 		 */
18283114fdb4SDavid Greenman 		ifp->if_timer = 5;
18293114fdb4SDavid Greenman 
1830397f9dfeSDavid Greenman 		return;
1831397f9dfeSDavid Greenman 	}
1832397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
1833397f9dfeSDavid Greenman 
1834397f9dfeSDavid Greenman 	/*
1835397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
1836397f9dfeSDavid Greenman 	 */
1837397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
1838397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
1839397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
1840e8c8b728SJonathan Lemon 	mcsp->cb_command = FXP_CB_COMMAND_MCAS |
1841e8c8b728SJonathan Lemon 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
1842397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
1843397f9dfeSDavid Greenman 
1844397f9dfeSDavid Greenman 	nmcasts = 0;
1845f7788e8eSJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
1846f7788e8eSJonathan Lemon #if __FreeBSD_version < 500000
1847f7788e8eSJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1848f7788e8eSJonathan Lemon #else
18496817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1850f7788e8eSJonathan Lemon #endif
1851397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
1852397f9dfeSDavid Greenman 				continue;
1853397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
1854f7788e8eSJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
1855397f9dfeSDavid Greenman 				nmcasts = 0;
1856397f9dfeSDavid Greenman 				break;
1857397f9dfeSDavid Greenman 			}
1858397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1859d244b0e9SPeter Wemm 			    (void *)(uintptr_t)(volatile void *)
1860d244b0e9SPeter Wemm 				&sc->mcsp->mc_addr[nmcasts][0], 6);
1861397f9dfeSDavid Greenman 			nmcasts++;
1862397f9dfeSDavid Greenman 		}
1863397f9dfeSDavid Greenman 	}
1864397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
1865397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
1866397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1867397f9dfeSDavid Greenman 
1868397f9dfeSDavid Greenman 	/*
1869397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
1870397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
1871397f9dfeSDavid Greenman 	 */
18727dced78aSDavid Greenman 	count = 100;
1873397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
18747dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
18757dced78aSDavid Greenman 		DELAY(10);
18767dced78aSDavid Greenman 	if (count == 0) {
1877f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
18787dced78aSDavid Greenman 		return;
18797dced78aSDavid Greenman 	}
1880397f9dfeSDavid Greenman 
1881397f9dfeSDavid Greenman 	/*
1882397f9dfeSDavid Greenman 	 * Start the multicast setup command.
1883397f9dfeSDavid Greenman 	 */
1884397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
1885397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
1886397f9dfeSDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1887397f9dfeSDavid Greenman 
18883114fdb4SDavid Greenman 	ifp->if_timer = 2;
1889397f9dfeSDavid Greenman 	return;
1890397f9dfeSDavid Greenman }
1891e8c8b728SJonathan Lemon 
1892e8c8b728SJonathan Lemon #endif /* NMIIBUS > 0 */
1893