xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 3bd07cfd43ecab81686ab801a63e3ce544b1f12d)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28c3aac50fSPeter Wemm  * $FreeBSD$
29a17c678eSDavid Greenman  */
30a17c678eSDavid Greenman 
31a17c678eSDavid Greenman /*
32ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
33a17c678eSDavid Greenman  */
34a17c678eSDavid Greenman 
35a17c678eSDavid Greenman #include <sys/param.h>
36a17c678eSDavid Greenman #include <sys/systm.h>
37a17c678eSDavid Greenman #include <sys/mbuf.h>
38a17c678eSDavid Greenman #include <sys/malloc.h>
39f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
40a17c678eSDavid Greenman #include <sys/kernel.h>
414458ac71SBruce Evans #include <sys/socket.h>
42a17c678eSDavid Greenman 
43a17c678eSDavid Greenman #include <net/if.h>
44397f9dfeSDavid Greenman #include <net/if_dl.h>
45ba8c6fd5SDavid Greenman #include <net/if_media.h>
46a17c678eSDavid Greenman 
47a17c678eSDavid Greenman #ifdef NS
48a17c678eSDavid Greenman #include <netns/ns.h>
49a17c678eSDavid Greenman #include <netns/ns_if.h>
50a17c678eSDavid Greenman #endif
51a17c678eSDavid Greenman 
52a17c678eSDavid Greenman #include <net/bpf.h>
53ba8c6fd5SDavid Greenman #include <sys/sockio.h>
546182fdbdSPeter Wemm #include <sys/bus.h>
556182fdbdSPeter Wemm #include <machine/bus.h>
566182fdbdSPeter Wemm #include <sys/rman.h>
576182fdbdSPeter Wemm #include <machine/resource.h>
58ba8c6fd5SDavid Greenman 
591d5e9e22SEivind Eklund #include <net/ethernet.h>
601d5e9e22SEivind Eklund #include <net/if_arp.h>
61ba8c6fd5SDavid Greenman 
62dfe61cf1SDavid Greenman #include <vm/vm.h>		/* for vtophys */
63efeaf95aSDavid Greenman #include <vm/pmap.h>		/* for vtophys */
64f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
65a17c678eSDavid Greenman 
66a17c678eSDavid Greenman #include <pci/pcivar.h>
67df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
68a17c678eSDavid Greenman 
69f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
70f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
71f7788e8eSJonathan Lemon 
72f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
73f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
74f7788e8eSJonathan Lemon 
75f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
76f7788e8eSJonathan Lemon #include "miibus_if.h"
774fc1dda9SAndrew Gallatin 
78ba8c6fd5SDavid Greenman /*
79ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
80ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
81ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
82ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
83ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
84ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
85ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
86ba8c6fd5SDavid Greenman  */
87ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
88ba8c6fd5SDavid Greenman 
89ba8c6fd5SDavid Greenman /*
90f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
91f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
92f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
93f7788e8eSJonathan Lemon  */
94f7788e8eSJonathan Lemon static int tx_threshold = 64;
95f7788e8eSJonathan Lemon 
96f7788e8eSJonathan Lemon /*
97f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
98f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
99f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
100f7788e8eSJonathan Lemon  * to fxp_init.
101f7788e8eSJonathan Lemon  *
102f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
103f7788e8eSJonathan Lemon  */
104f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
105f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
106f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
107f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
108f7788e8eSJonathan Lemon 	0x0,	/*  0 */
109f7788e8eSJonathan Lemon 	0x0,	/*  1 */
110f7788e8eSJonathan Lemon 	0x0,	/*  2 */
111f7788e8eSJonathan Lemon 	0x0,	/*  3 */
112f7788e8eSJonathan Lemon 	0x0,	/*  4 */
113f7788e8eSJonathan Lemon 	0x0,	/*  5 */
114f7788e8eSJonathan Lemon 	0x32,	/*  6 */
115f7788e8eSJonathan Lemon 	0x0,	/*  7 */
116f7788e8eSJonathan Lemon 	0x0,	/*  8 */
117f7788e8eSJonathan Lemon 	0x0,	/*  9 */
118f7788e8eSJonathan Lemon 	0x6,	/* 10 */
119f7788e8eSJonathan Lemon 	0x0,	/* 11 */
120f7788e8eSJonathan Lemon 	0x0,	/* 12 */
121f7788e8eSJonathan Lemon 	0x0,	/* 13 */
122f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
123f7788e8eSJonathan Lemon 	0x48,	/* 15 */
124f7788e8eSJonathan Lemon 	0x0,	/* 16 */
125f7788e8eSJonathan Lemon 	0x40,	/* 17 */
126f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
127f7788e8eSJonathan Lemon 	0x0,	/* 19 */
128f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
129f7788e8eSJonathan Lemon 	0x5	/* 21 */
130f7788e8eSJonathan Lemon };
131f7788e8eSJonathan Lemon 
132f7788e8eSJonathan Lemon struct fxp_ident {
133f7788e8eSJonathan Lemon 	u_int16_t	devid;
134f7788e8eSJonathan Lemon 	char 		*name;
135f7788e8eSJonathan Lemon };
136f7788e8eSJonathan Lemon 
137f7788e8eSJonathan Lemon /*
138f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
139f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
140f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
141f7788e8eSJonathan Lemon  * them.
142f7788e8eSJonathan Lemon  */
143f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
144f7788e8eSJonathan Lemon     { 0x1229,		"Intel Pro 10/100B/100+ Ethernet" },
145f7788e8eSJonathan Lemon     { 0x2449,		"Intel Pro/100 Ethernet" },
146f7788e8eSJonathan Lemon     { 0x1209,		"Intel Embedded 10/100 Ethernet" },
147f7788e8eSJonathan Lemon     { 0x1029,		"Intel Pro/100 Ethernet" },
148f7788e8eSJonathan Lemon     { 0x1030,		"Intel Pro/100 Ethernet" },
149f7788e8eSJonathan Lemon     { 0x1031,		"Intel Pro/100 Ethernet" },
150f7788e8eSJonathan Lemon     { 0x1032,		"Intel Pro/100 Ethernet" },
151f7788e8eSJonathan Lemon     { 0x1033,		"Intel Pro/100 Ethernet" },
152f7788e8eSJonathan Lemon     { 0x1034,		"Intel Pro/100 Ethernet" },
153f7788e8eSJonathan Lemon     { 0x1035,		"Intel Pro/100 Ethernet" },
154f7788e8eSJonathan Lemon     { 0x1036,		"Intel Pro/100 Ethernet" },
155f7788e8eSJonathan Lemon     { 0x1037,		"Intel Pro/100 Ethernet" },
156f7788e8eSJonathan Lemon     { 0x1038,		"Intel Pro/100 Ethernet" },
157f7788e8eSJonathan Lemon     { 0,		NULL },
158f7788e8eSJonathan Lemon };
159f7788e8eSJonathan Lemon 
160f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
161f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
162f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
163f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
164f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
165f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
166f7788e8eSJonathan Lemon 
167f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
168f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
169f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
170f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
171f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
172f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
173f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
174f7788e8eSJonathan Lemon 			    caddr_t data);
175f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
176f7788e8eSJonathan Lemon static int		fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
177f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
178f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
179f7788e8eSJonathan Lemon 			    int autosize);
180f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
181f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
182f7788e8eSJonathan Lemon 			    int offset, int words);
183f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
184f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
185f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
186f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
187f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
188f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
189f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
190f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
191f7788e8eSJonathan Lemon 			    int value);
192f7788e8eSJonathan Lemon static __inline void	fxp_lwcopy(volatile u_int32_t *src,
193f7788e8eSJonathan Lemon 			    volatile u_int32_t *dst);
194f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
195f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
196f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
197f7788e8eSJonathan Lemon 
198f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
199f7788e8eSJonathan Lemon 	/* Device interface */
200f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
201f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
202f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
203f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
204f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
205f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
206f7788e8eSJonathan Lemon 
207f7788e8eSJonathan Lemon 	/* MII interface */
208f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
209f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
210f7788e8eSJonathan Lemon 
211f7788e8eSJonathan Lemon 	{ 0, 0 }
212f7788e8eSJonathan Lemon };
213f7788e8eSJonathan Lemon 
214f7788e8eSJonathan Lemon static driver_t fxp_driver = {
215f7788e8eSJonathan Lemon 	"fxp",
216f7788e8eSJonathan Lemon 	fxp_methods,
217f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
218f7788e8eSJonathan Lemon };
219f7788e8eSJonathan Lemon 
220f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
221f7788e8eSJonathan Lemon 
222f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
223f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
224f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
225f7788e8eSJonathan Lemon 
226f7788e8eSJonathan Lemon /*
227ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
228ba8c6fd5SDavid Greenman  */
229ba8c6fd5SDavid Greenman static __inline void
230f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
231ba8c6fd5SDavid Greenman {
232aed53495SDavid Greenman #ifdef __i386__
233aed53495SDavid Greenman 	*dst = *src;
234aed53495SDavid Greenman #else
235fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
236fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
237ba8c6fd5SDavid Greenman 
238ba8c6fd5SDavid Greenman 	b[0] = a[0];
239ba8c6fd5SDavid Greenman 	b[1] = a[1];
240aed53495SDavid Greenman #endif
241ba8c6fd5SDavid Greenman }
242a17c678eSDavid Greenman 
243a17c678eSDavid Greenman /*
244dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
245dfe61cf1SDavid Greenman  * completed).
246dfe61cf1SDavid Greenman  */
247c1087c13SBruce Evans static __inline void
248f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
249a17c678eSDavid Greenman {
250a17c678eSDavid Greenman 	int i = 10000;
251a17c678eSDavid Greenman 
2527dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2537dced78aSDavid Greenman 		DELAY(2);
2547dced78aSDavid Greenman 	if (i == 0)
255f7788e8eSJonathan Lemon 		device_printf(sc->dev, "SCB timeout\n");
2567dced78aSDavid Greenman }
2577dced78aSDavid Greenman 
2587dced78aSDavid Greenman static __inline void
259f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
2607dced78aSDavid Greenman {
2617dced78aSDavid Greenman 	int i = 10000;
2627dced78aSDavid Greenman 
2637dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
2647dced78aSDavid Greenman 		DELAY(2);
2657dced78aSDavid Greenman 	if (i == 0)
266f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
267a17c678eSDavid Greenman }
268a17c678eSDavid Greenman 
269dfe61cf1SDavid Greenman /*
270dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
271dfe61cf1SDavid Greenman  */
2726182fdbdSPeter Wemm static int
2736182fdbdSPeter Wemm fxp_probe(device_t dev)
274a17c678eSDavid Greenman {
275f7788e8eSJonathan Lemon 	u_int16_t devid;
276f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
277f7788e8eSJonathan Lemon 
27855ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
279f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
280f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
281f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
282f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
283f7788e8eSJonathan Lemon 				return (0);
28455ce7b51SDavid Greenman 			}
285dd68ef16SPeter Wemm 		}
286f7788e8eSJonathan Lemon 	}
287f7788e8eSJonathan Lemon 	return (ENXIO);
2886182fdbdSPeter Wemm }
2896182fdbdSPeter Wemm 
2906182fdbdSPeter Wemm static int
2916182fdbdSPeter Wemm fxp_attach(device_t dev)
292a17c678eSDavid Greenman {
2936182fdbdSPeter Wemm 	int error = 0;
2946182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
295ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
2969fa6ccfbSMatt Jacob 	u_int32_t val;
297f7788e8eSJonathan Lemon 	u_int16_t data;
298f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
299f7788e8eSJonathan Lemon 	int s;
300a17c678eSDavid Greenman 
301f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
302f7788e8eSJonathan Lemon 	sc->dev = dev;
3036c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
304f7788e8eSJonathan Lemon 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
305a17c678eSDavid Greenman 
306f7788e8eSJonathan Lemon 	s = splimp();
307a17c678eSDavid Greenman 
308dfe61cf1SDavid Greenman 	/*
3099fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3109fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
311df373873SWes Peters 	 */
3126182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
313df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3146182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3159fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
316df373873SWes Peters 
317f7788e8eSJonathan Lemon #if __FreeBSD_version >= 500000
3188d799694SBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
3198d799694SBill Paul 		u_int32_t		iobase, membase, irq;
3208d799694SBill Paul 
3218d799694SBill Paul 		/* Save important PCI config data. */
3228d799694SBill Paul 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
3238d799694SBill Paul 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
3248d799694SBill Paul 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
3258d799694SBill Paul 
3268d799694SBill Paul 		/* Reset the power state. */
3278d799694SBill Paul 		device_printf(dev, "chip is in D%d power mode "
3288d799694SBill Paul 		    "-- setting to D0\n", pci_get_powerstate(dev));
3298d799694SBill Paul 
3308d799694SBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
3318d799694SBill Paul 
3328d799694SBill Paul 		/* Restore PCI config data. */
3338d799694SBill Paul 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
3348d799694SBill Paul 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
3358d799694SBill Paul 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
3368d799694SBill Paul 	}
337f7788e8eSJonathan Lemon #endif
3388d799694SBill Paul 
339df373873SWes Peters 	/*
3409fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3419fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3429fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
343dfe61cf1SDavid Greenman 	 */
3449fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3459fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3462a05a4ebSMatt Jacob 	prefer_iomap = 0;
3472a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3482a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3499fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3509fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
3519fa6ccfbSMatt Jacob 	}
3529fa6ccfbSMatt Jacob 
3539fa6ccfbSMatt Jacob 	if (val & m1) {
3549fa6ccfbSMatt Jacob 		sc->rtp =
3559fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3569fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3579fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3586182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
3599fa6ccfbSMatt Jacob 	}
3609fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
3619fa6ccfbSMatt Jacob 		sc->rtp =
3629fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3639fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3649fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3659fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
3669fa6ccfbSMatt Jacob 	}
3679fa6ccfbSMatt Jacob 
3686182fdbdSPeter Wemm 	if (!sc->mem) {
3699fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
3706182fdbdSPeter Wemm 		error = ENXIO;
371a17c678eSDavid Greenman 		goto fail;
372a17c678eSDavid Greenman         }
3739fa6ccfbSMatt Jacob 	if (bootverbose) {
3749fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
3759fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
3769fa6ccfbSMatt Jacob 	}
3774fc1dda9SAndrew Gallatin 
3784fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
3794fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
380a17c678eSDavid Greenman 
381a17c678eSDavid Greenman 	/*
382dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
383dfe61cf1SDavid Greenman 	 */
3846182fdbdSPeter Wemm 	rid = 0;
3856182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
3866182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
3876182fdbdSPeter Wemm 	if (sc->irq == NULL) {
3886182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
3896182fdbdSPeter Wemm 		error = ENXIO;
3906182fdbdSPeter Wemm 		goto fail;
3916182fdbdSPeter Wemm 	}
3926182fdbdSPeter Wemm 
393566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
394566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
3956182fdbdSPeter Wemm 	if (error) {
3966182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
397a17c678eSDavid Greenman 		goto fail;
398a17c678eSDavid Greenman 	}
399a17c678eSDavid Greenman 
400f7788e8eSJonathan Lemon 	/*
401f7788e8eSJonathan Lemon 	 * Reset to a stable state.
402f7788e8eSJonathan Lemon 	 */
403f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
404f7788e8eSJonathan Lemon 	DELAY(10);
405f7788e8eSJonathan Lemon 
406f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
407f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
408f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
409f7788e8eSJonathan Lemon 		goto failmem;
410f7788e8eSJonathan Lemon 
411f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
412f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
413f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
414f7788e8eSJonathan Lemon 		goto failmem;
415f7788e8eSJonathan Lemon 
416f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
417f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
418f7788e8eSJonathan Lemon 		goto failmem;
419f7788e8eSJonathan Lemon 
420f7788e8eSJonathan Lemon 	/*
421f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
422f7788e8eSJonathan Lemon 	 */
423f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
424f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
425f7788e8eSJonathan Lemon 			goto failmem;
426f7788e8eSJonathan Lemon 		}
427f7788e8eSJonathan Lemon 	}
428f7788e8eSJonathan Lemon 
429f7788e8eSJonathan Lemon 	/*
430f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
431f7788e8eSJonathan Lemon 	 */
432f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
433f7788e8eSJonathan Lemon 
434f7788e8eSJonathan Lemon 	/*
4353bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
436f7788e8eSJonathan Lemon 	 */
437f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
438f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
439f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
440f7788e8eSJonathan Lemon 		sc->flags &= FXP_FLAG_SERIAL_MEDIA;
441f7788e8eSJonathan Lemon 
442f7788e8eSJonathan Lemon 	/*
4433bd07cfdSJonathan Lemon 	 * Find out the basic controller type; we currently only
4443bd07cfdSJonathan Lemon 	 * differentiate between a 82557 and greater.
4453bd07cfdSJonathan Lemon 	 */
4463bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
4473bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
4483bd07cfdSJonathan Lemon 	    sc->chip = FXP_CHIP_82557;
4493bd07cfdSJonathan Lemon 
4503bd07cfdSJonathan Lemon 	/*
4513bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
4523bd07cfdSJonathan Lemon 	 */
4533bd07cfdSJonathan Lemon 	if (sc->chip != FXP_CHIP_82557) {
4543bd07cfdSJonathan Lemon 		/*
4553bd07cfdSJonathan Lemon 		 * If there is a valid cacheline size (8 or 16 dwords),
4563bd07cfdSJonathan Lemon 		 * then turn on MWI.
4573bd07cfdSJonathan Lemon 		 */
4583bd07cfdSJonathan Lemon 		if (pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
4593bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
4603bd07cfdSJonathan Lemon 
4613bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
4623bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
4633bd07cfdSJonathan Lemon 	}
4643bd07cfdSJonathan Lemon 
4653bd07cfdSJonathan Lemon 	/*
466f7788e8eSJonathan Lemon 	 * Read MAC address.
467f7788e8eSJonathan Lemon 	 */
468f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
469f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
470f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
471f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
472f7788e8eSJonathan Lemon 	if (bootverbose) {
473f7788e8eSJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x\n",
474f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
475f7788e8eSJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev));
476f7788e8eSJonathan Lemon 	}
477f7788e8eSJonathan Lemon 
478f7788e8eSJonathan Lemon 	/*
479f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
480f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
481f7788e8eSJonathan Lemon 	 *
482f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
483f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
484f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
485f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
486f7788e8eSJonathan Lemon 	 */
487f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
488f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
489f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
490f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
491f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
492f7788e8eSJonathan Lemon 	} else {
493f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
494f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
495f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
4966182fdbdSPeter Wemm 			error = ENXIO;
497ba8c6fd5SDavid Greenman 			goto fail;
498a17c678eSDavid Greenman 		}
499f7788e8eSJonathan Lemon 	}
500dccee1a1SDavid Greenman 
501a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
5026182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
503a17c678eSDavid Greenman 	ifp->if_name = "fxp";
504a17c678eSDavid Greenman 	ifp->if_output = ether_output;
505a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
506fb583156SDavid Greenman 	ifp->if_init = fxp_init;
507ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
508ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
509ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
510ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
511ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
512a17c678eSDavid Greenman 
513dfe61cf1SDavid Greenman 	/*
514dfe61cf1SDavid Greenman 	 * Attach the interface.
515dfe61cf1SDavid Greenman 	 */
51621b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
517f7788e8eSJonathan Lemon 
518483b9871SDavid Greenman 	/*
5193114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
5203114fdb4SDavid Greenman 	 * TX descriptors.
521483b9871SDavid Greenman 	 */
5223114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
5234a684684SDavid Greenman 
524f7788e8eSJonathan Lemon 	splx(s);
525f7788e8eSJonathan Lemon 	return (0);
526a17c678eSDavid Greenman 
527f7788e8eSJonathan Lemon failmem:
528f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
529f7788e8eSJonathan Lemon 	error = ENOMEM;
530a17c678eSDavid Greenman fail:
531f7788e8eSJonathan Lemon 	splx(s);
532f7788e8eSJonathan Lemon 	fxp_release(sc);
533f7788e8eSJonathan Lemon 	return (error);
534f7788e8eSJonathan Lemon }
535f7788e8eSJonathan Lemon 
536f7788e8eSJonathan Lemon /*
537f7788e8eSJonathan Lemon  * release all resources
538f7788e8eSJonathan Lemon  */
539f7788e8eSJonathan Lemon static void
540f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
541f7788e8eSJonathan Lemon {
542f7788e8eSJonathan Lemon 
543f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
5443bd07cfdSJonathan Lemon 	if (sc->miibus)
545f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
546f7788e8eSJonathan Lemon 
547f7788e8eSJonathan Lemon 	if (sc->cbl_base)
548f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
549f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
550f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
551f7788e8eSJonathan Lemon 	if (sc->mcsp)
552f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
553f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
554f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
555f7788e8eSJonathan Lemon 
556f7788e8eSJonathan Lemon 	if (sc->ih)
557f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
558f7788e8eSJonathan Lemon 	if (sc->irq)
559f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
560f7788e8eSJonathan Lemon 	if (sc->mem)
561f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
5620f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
5636182fdbdSPeter Wemm }
5646182fdbdSPeter Wemm 
5656182fdbdSPeter Wemm /*
5666182fdbdSPeter Wemm  * Detach interface.
5676182fdbdSPeter Wemm  */
5686182fdbdSPeter Wemm static int
5696182fdbdSPeter Wemm fxp_detach(device_t dev)
5706182fdbdSPeter Wemm {
5716182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
572f7788e8eSJonathan Lemon 	int s;
5736182fdbdSPeter Wemm 
574f7788e8eSJonathan Lemon 	s = splimp();
5756182fdbdSPeter Wemm 
5766182fdbdSPeter Wemm 	/*
5776182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
5786182fdbdSPeter Wemm 	 */
5796182fdbdSPeter Wemm 	fxp_stop(sc);
5806182fdbdSPeter Wemm 
5816182fdbdSPeter Wemm 	/*
582f7788e8eSJonathan Lemon 	 * Close down routes etc.
5836182fdbdSPeter Wemm 	 */
584f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
5856182fdbdSPeter Wemm 
5866182fdbdSPeter Wemm 	/*
5876182fdbdSPeter Wemm 	 * Free all media structures.
5886182fdbdSPeter Wemm 	 */
5896182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
5906182fdbdSPeter Wemm 
591f7788e8eSJonathan Lemon 	splx(s);
5926182fdbdSPeter Wemm 
593f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
594f7788e8eSJonathan Lemon 	fxp_release(sc);
5956182fdbdSPeter Wemm 
596f7788e8eSJonathan Lemon 	return (0);
597a17c678eSDavid Greenman }
598a17c678eSDavid Greenman 
599a17c678eSDavid Greenman /*
6004a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
601a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
602a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
603a17c678eSDavid Greenman  */
6046182fdbdSPeter Wemm static int
6056182fdbdSPeter Wemm fxp_shutdown(device_t dev)
606a17c678eSDavid Greenman {
6076182fdbdSPeter Wemm 	/*
6086182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
6096182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
6106182fdbdSPeter Wemm 	 * reboot before the driver initializes.
6116182fdbdSPeter Wemm 	 */
6126182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
613f7788e8eSJonathan Lemon 	return (0);
614a17c678eSDavid Greenman }
615a17c678eSDavid Greenman 
6167dced78aSDavid Greenman /*
6177dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
6187dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
6197dced78aSDavid Greenman  * resume.
6207dced78aSDavid Greenman  */
6217dced78aSDavid Greenman static int
6227dced78aSDavid Greenman fxp_suspend(device_t dev)
6237dced78aSDavid Greenman {
6247dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
625f7788e8eSJonathan Lemon 	int i, s;
6267dced78aSDavid Greenman 
627f7788e8eSJonathan Lemon 	s = splimp();
6287dced78aSDavid Greenman 
6297dced78aSDavid Greenman 	fxp_stop(sc);
6307dced78aSDavid Greenman 
6317dced78aSDavid Greenman 	for (i=0; i<5; i++)
6327dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i*4, 4);
6337dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
6347dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
6357dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
6367dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
6377dced78aSDavid Greenman 
6387dced78aSDavid Greenman 	sc->suspended = 1;
6397dced78aSDavid Greenman 
640f7788e8eSJonathan Lemon 	splx(s);
641f7788e8eSJonathan Lemon 	return (0);
6427dced78aSDavid Greenman }
6437dced78aSDavid Greenman 
6447dced78aSDavid Greenman /*
6457dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
6467dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
6477dced78aSDavid Greenman  * appropriate.
6487dced78aSDavid Greenman  */
6497dced78aSDavid Greenman static int
6507dced78aSDavid Greenman fxp_resume(device_t dev)
6517dced78aSDavid Greenman {
6527dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
6537dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
6547dced78aSDavid Greenman 	u_int16_t pci_command;
655f7788e8eSJonathan Lemon 	int i, s;
6567dced78aSDavid Greenman 
657f7788e8eSJonathan Lemon 	s = splimp();
6587dced78aSDavid Greenman 
6597dced78aSDavid Greenman 	/* better way to do this? */
6607dced78aSDavid Greenman 	for (i=0; i<5; i++)
6617dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i*4, sc->saved_maps[i], 4);
6627dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
6637dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
6647dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
6657dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
6667dced78aSDavid Greenman 
6677dced78aSDavid Greenman 	/* reenable busmastering */
6687dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
6697dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
6707dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
6717dced78aSDavid Greenman 
6727dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
6737dced78aSDavid Greenman 	DELAY(10);
6747dced78aSDavid Greenman 
6757dced78aSDavid Greenman 	/* reinitialize interface if necessary */
6767dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
6777dced78aSDavid Greenman 		fxp_init(sc);
6787dced78aSDavid Greenman 
6797dced78aSDavid Greenman 	sc->suspended = 0;
6807dced78aSDavid Greenman 
681f7788e8eSJonathan Lemon 	splx(s);
682ba8c6fd5SDavid Greenman 	return (0);
683f7788e8eSJonathan Lemon }
684ba8c6fd5SDavid Greenman 
685f7788e8eSJonathan Lemon /*
686f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
687f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
688f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
689f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
690f7788e8eSJonathan Lemon  * every 16 bits of data.
691f7788e8eSJonathan Lemon  */
692f7788e8eSJonathan Lemon static u_int16_t
693f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
694f7788e8eSJonathan Lemon {
695f7788e8eSJonathan Lemon 	u_int16_t reg, data;
696f7788e8eSJonathan Lemon 	int x;
697ba8c6fd5SDavid Greenman 
698f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
699f7788e8eSJonathan Lemon 	/*
700f7788e8eSJonathan Lemon 	 * Shift in read opcode.
701f7788e8eSJonathan Lemon 	 */
702f7788e8eSJonathan Lemon 	for (x = 1 << 2; x; x >>= 1) {
703f7788e8eSJonathan Lemon 		if (FXP_EEPROM_OPC_READ & x)
704f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
705f7788e8eSJonathan Lemon 		else
706f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
707f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
708f7788e8eSJonathan Lemon 		DELAY(1);
709f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
710f7788e8eSJonathan Lemon 		DELAY(1);
711f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
712f7788e8eSJonathan Lemon 		DELAY(1);
713f7788e8eSJonathan Lemon 	}
714f7788e8eSJonathan Lemon 	/*
715f7788e8eSJonathan Lemon 	 * Shift in address.
716f7788e8eSJonathan Lemon 	 */
717f7788e8eSJonathan Lemon 	data = 0;
718f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
719f7788e8eSJonathan Lemon 		if (offset & x)
720f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
721f7788e8eSJonathan Lemon 		else
722f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
723f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
724f7788e8eSJonathan Lemon 		DELAY(1);
725f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
726f7788e8eSJonathan Lemon 		DELAY(1);
727f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
728f7788e8eSJonathan Lemon 		DELAY(1);
729f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
730f7788e8eSJonathan Lemon 		data++;
731f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
732f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
733f7788e8eSJonathan Lemon 			break;
734f7788e8eSJonathan Lemon 		}
735f7788e8eSJonathan Lemon 	}
736f7788e8eSJonathan Lemon 	/*
737f7788e8eSJonathan Lemon 	 * Shift out data.
738f7788e8eSJonathan Lemon 	 */
739f7788e8eSJonathan Lemon 	data = 0;
740f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
741f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
742f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
743f7788e8eSJonathan Lemon 		DELAY(1);
744f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
745f7788e8eSJonathan Lemon 			data |= x;
746f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
747f7788e8eSJonathan Lemon 		DELAY(1);
748f7788e8eSJonathan Lemon 	}
749f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
750f7788e8eSJonathan Lemon 	DELAY(1);
751f7788e8eSJonathan Lemon 
752f7788e8eSJonathan Lemon 	return (data);
753ba8c6fd5SDavid Greenman }
754ba8c6fd5SDavid Greenman 
755ba8c6fd5SDavid Greenman /*
756e9bf2fa7SDavid Greenman  * From NetBSD:
757e9bf2fa7SDavid Greenman  *
758e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
759e9bf2fa7SDavid Greenman  *
760e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
761e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
762e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
763e9bf2fa7SDavid Greenman  *
764e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
765e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
766e9bf2fa7SDavid Greenman  *
767e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
768e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
769e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
770e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
771e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
772e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
773e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
774e9bf2fa7SDavid Greenman  *
775e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
776e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
777e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
778e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
779e9bf2fa7SDavid Greenman  *
780e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
781e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
782e9bf2fa7SDavid Greenman  */
783e9bf2fa7SDavid Greenman static void
784f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
785e9bf2fa7SDavid Greenman {
786e9bf2fa7SDavid Greenman 
787f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
788f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
789f7788e8eSJonathan Lemon 
790f7788e8eSJonathan Lemon 	/* autosize */
791f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
792e9bf2fa7SDavid Greenman }
793f7788e8eSJonathan Lemon 
794ba8c6fd5SDavid Greenman static void
795f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
796ba8c6fd5SDavid Greenman {
797f7788e8eSJonathan Lemon 	int i;
798ba8c6fd5SDavid Greenman 
799f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
800f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
801ba8c6fd5SDavid Greenman }
802ba8c6fd5SDavid Greenman 
803a17c678eSDavid Greenman /*
804a17c678eSDavid Greenman  * Start packet transmission on the interface.
805a17c678eSDavid Greenman  */
806a17c678eSDavid Greenman static void
807f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
808a17c678eSDavid Greenman {
8099b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
810a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
811a17c678eSDavid Greenman 
812a17c678eSDavid Greenman 	/*
813483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
814483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
815483b9871SDavid Greenman 	 * of the command chain).
816a17c678eSDavid Greenman 	 */
8170f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
818a17c678eSDavid Greenman 		return;
8190f4dc94cSChuck Paterson 	}
8201cd443acSDavid Greenman 
821483b9871SDavid Greenman 	txp = NULL;
822483b9871SDavid Greenman 
823483b9871SDavid Greenman 	/*
824483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
825483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
8263114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
8273114fdb4SDavid Greenman 	 *       a NOP command when needed.
828483b9871SDavid Greenman 	 */
8293114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
830483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
831483b9871SDavid Greenman 		int segment;
832483b9871SDavid Greenman 
833dfe61cf1SDavid Greenman 		/*
834dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
835dfe61cf1SDavid Greenman 		 */
8366318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
837a17c678eSDavid Greenman 
838dfe61cf1SDavid Greenman 		/*
839483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
840dfe61cf1SDavid Greenman 		 */
841a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
842a17c678eSDavid Greenman 
843a17c678eSDavid Greenman 		/*
844a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
845483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
846a17c678eSDavid Greenman 		 * and size of the mbuf.
847a17c678eSDavid Greenman 		 */
84823a0ed7cSDavid Greenman tbdinit:
849a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
850a17c678eSDavid Greenman 			if (m->m_len != 0) {
851a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
852a17c678eSDavid Greenman 					break;
853a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
854a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
855a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
856a17c678eSDavid Greenman 				segment++;
857a17c678eSDavid Greenman 			}
858a17c678eSDavid Greenman 		}
859fb583156SDavid Greenman 		if (m != NULL) {
86023a0ed7cSDavid Greenman 			struct mbuf *mn;
86123a0ed7cSDavid Greenman 
862a17c678eSDavid Greenman 			/*
8633bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
8643bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
8653bd07cfdSJonathan Lemon 			 * new buffers.
866a17c678eSDavid Greenman 			 */
86723a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
86823a0ed7cSDavid Greenman 			if (mn == NULL) {
86923a0ed7cSDavid Greenman 				m_freem(mb_head);
870483b9871SDavid Greenman 				break;
871a17c678eSDavid Greenman 			}
87223a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
87323a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
87423a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
87523a0ed7cSDavid Greenman 					m_freem(mn);
87623a0ed7cSDavid Greenman 					m_freem(mb_head);
877483b9871SDavid Greenman 					break;
87823a0ed7cSDavid Greenman 				}
87923a0ed7cSDavid Greenman 			}
880ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
881ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
88223a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
88323a0ed7cSDavid Greenman 			m_freem(mb_head);
88423a0ed7cSDavid Greenman 			mb_head = mn;
88523a0ed7cSDavid Greenman 			goto tbdinit;
88623a0ed7cSDavid Greenman 		}
88723a0ed7cSDavid Greenman 
88823a0ed7cSDavid Greenman 		txp->tbd_number = segment;
8891cd443acSDavid Greenman 		txp->mb_head = mb_head;
890a17c678eSDavid Greenman 		txp->cb_status = 0;
8913114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
892a17c678eSDavid Greenman 			txp->cb_command =
8933bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
8943bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
8953114fdb4SDavid Greenman 		} else {
8963114fdb4SDavid Greenman 			txp->cb_command =
8973bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
8983bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
8993114fdb4SDavid Greenman 			/*
9003bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
9013bd07cfdSJonathan Lemon 			 * from the card again.
9023114fdb4SDavid Greenman 			 */
9033114fdb4SDavid Greenman 			ifp->if_timer = 5;
9043114fdb4SDavid Greenman 		}
905f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
906a17c678eSDavid Greenman 
907a17c678eSDavid Greenman 		/*
908483b9871SDavid Greenman 		 * Advance the end of list forward.
909a17c678eSDavid Greenman 		 */
91006175228SAndrew Gallatin 
91106175228SAndrew Gallatin #ifdef __alpha__
91206175228SAndrew Gallatin 		/*
91306175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
91406175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
91506175228SAndrew Gallatin 		 * up the status while we update the command field.
91606175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
91706175228SAndrew Gallatin 		 */
91806175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
91906175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
92006175228SAndrew Gallatin #else
921a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
92206175228SAndrew Gallatin #endif /*__alpha__*/
923a17c678eSDavid Greenman 		sc->cbl_last = txp;
924a17c678eSDavid Greenman 
925a17c678eSDavid Greenman 		/*
9261cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
9271cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
928483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
929a17c678eSDavid Greenman 		 */
9301cd443acSDavid Greenman 		if (sc->tx_queued == 0)
931a17c678eSDavid Greenman 			sc->cbl_first = txp;
932a17c678eSDavid Greenman 
9331cd443acSDavid Greenman 		sc->tx_queued++;
9341cd443acSDavid Greenman 
935a17c678eSDavid Greenman 		/*
936a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
937a17c678eSDavid Greenman 		 */
938fb583156SDavid Greenman 		if (ifp->if_bpf)
93994927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
940483b9871SDavid Greenman 	}
941483b9871SDavid Greenman 
942483b9871SDavid Greenman 	/*
943483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
944483b9871SDavid Greenman 	 * going again if suspended.
945483b9871SDavid Greenman 	 */
946483b9871SDavid Greenman 	if (txp != NULL) {
947483b9871SDavid Greenman 		fxp_scb_wait(sc);
948483b9871SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
949483b9871SDavid Greenman 	}
950a17c678eSDavid Greenman }
951a17c678eSDavid Greenman 
952a17c678eSDavid Greenman /*
9539c7d2607SDavid Greenman  * Process interface interrupts.
954a17c678eSDavid Greenman  */
95594927790SDavid Greenman static void
956f7788e8eSJonathan Lemon fxp_intr(void *xsc)
957a17c678eSDavid Greenman {
958f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
959ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
9601cd443acSDavid Greenman 	u_int8_t statack;
9610f4dc94cSChuck Paterson 
962a17c678eSDavid Greenman 
963b184b38eSDavid Greenman 	if (sc->suspended) {
964b184b38eSDavid Greenman 		return;
965b184b38eSDavid Greenman 	}
966b184b38eSDavid Greenman 
967b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
968a17c678eSDavid Greenman 		/*
969a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
970a17c678eSDavid Greenman 		 */
971ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
972a17c678eSDavid Greenman 
973a17c678eSDavid Greenman 		/*
9743114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
97506936301SBill Paul 		 *
97606936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
97706936301SBill Paul 		 * be that this event (control unit not ready) was not
97806936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
97906936301SBill Paul 		 * The exact sequence of events that occur when the interface
98006936301SBill Paul 		 * is brought up are different now, and if this event
98106936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
98206936301SBill Paul 		 * can stall for several seconds. The result is that no
98306936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
98406936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
9853114fdb4SDavid Greenman 		 */
98606936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
9873114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
9883114fdb4SDavid Greenman 
9893114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
9903114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
9913114fdb4SDavid Greenman 			    txp = txp->next) {
9923114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
9933114fdb4SDavid Greenman 					m_freem(txp->mb_head);
9943114fdb4SDavid Greenman 					txp->mb_head = NULL;
9953114fdb4SDavid Greenman 				}
9963114fdb4SDavid Greenman 				sc->tx_queued--;
9973114fdb4SDavid Greenman 			}
9983114fdb4SDavid Greenman 			sc->cbl_first = txp;
9993114fdb4SDavid Greenman 			ifp->if_timer = 0;
10003114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
10013114fdb4SDavid Greenman 				if (sc->need_mcsetup)
10023114fdb4SDavid Greenman 					fxp_mc_setup(sc);
10033114fdb4SDavid Greenman 			}
10043114fdb4SDavid Greenman 			/*
10053114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
10063114fdb4SDavid Greenman 			 */
10073114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
10083114fdb4SDavid Greenman 				fxp_start(ifp);
10093114fdb4SDavid Greenman 		}
10103114fdb4SDavid Greenman 		/*
1011a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1012a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1013a17c678eSDavid Greenman 		 * re-start the receiver.
1014a17c678eSDavid Greenman 		 */
1015a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1016a17c678eSDavid Greenman 			struct mbuf *m;
1017a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1018a17c678eSDavid Greenman rcvloop:
1019a17c678eSDavid Greenman 			m = sc->rfa_headm;
1020ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1021ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1022a17c678eSDavid Greenman 
1023a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1024dfe61cf1SDavid Greenman 				/*
1025dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1026dfe61cf1SDavid Greenman 				 */
1027a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1028a17c678eSDavid Greenman 				m->m_next = NULL;
1029a17c678eSDavid Greenman 
1030dfe61cf1SDavid Greenman 				/*
1031ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1032ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1033ba8c6fd5SDavid Greenman 				 * instead.
1034dfe61cf1SDavid Greenman 				 */
1035a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1036a17c678eSDavid Greenman 					struct ether_header *eh;
1037aed53495SDavid Greenman 					int total_len;
1038a17c678eSDavid Greenman 
1039ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1040ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1041ba8c6fd5SDavid Greenman 					if (total_len <
1042ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
104306339180SDavid Greenman 						m_freem(m);
104406339180SDavid Greenman 						goto rcvloop;
104506339180SDavid Greenman 					}
1046a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
10472e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1048a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1049ba8c6fd5SDavid Greenman 					m->m_data +=
1050ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1051ab090e5bSLuigi Rizzo 					m->m_len -=
1052ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1053ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1054a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1055a17c678eSDavid Greenman 				}
1056a17c678eSDavid Greenman 				goto rcvloop;
1057a17c678eSDavid Greenman 			}
1058a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1059ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1060ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1061ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1062ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
1063ba8c6fd5SDavid Greenman 				CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1064ba8c6fd5SDavid Greenman 				    FXP_SCB_COMMAND_RU_START);
1065a17c678eSDavid Greenman 			}
1066a17c678eSDavid Greenman 		}
1067a17c678eSDavid Greenman 	}
1068a17c678eSDavid Greenman }
1069a17c678eSDavid Greenman 
1070dfe61cf1SDavid Greenman /*
1071dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1072dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1073dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1074dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1075dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1076dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1077dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1078dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1079dfe61cf1SDavid Greenman  * them again next time.
1080dfe61cf1SDavid Greenman  */
1081303b270bSEivind Eklund static void
1082f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1083a17c678eSDavid Greenman {
1084f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1085ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1086a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1087c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1088f7788e8eSJonathan Lemon 	int s;
1089a17c678eSDavid Greenman 
1090a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1091a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1092397f9dfeSDavid Greenman 	if (sp->rx_good) {
1093397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1094397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1095397f9dfeSDavid Greenman 	} else {
1096c8cc6fcaSDavid Greenman 		/*
1097c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1098c8cc6fcaSDavid Greenman 		 */
1099397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1100397f9dfeSDavid Greenman 	}
11013ba65732SDavid Greenman 	ifp->if_ierrors +=
11023ba65732SDavid Greenman 	    sp->rx_crc_errors +
11033ba65732SDavid Greenman 	    sp->rx_alignment_errors +
11043ba65732SDavid Greenman 	    sp->rx_rnr_errors +
11056e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1106a17c678eSDavid Greenman 	/*
1107f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1108f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1109f9be9005SDavid Greenman 	 */
1110f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1111f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1112f9be9005SDavid Greenman 		if (tx_threshold < 192)
1113f9be9005SDavid Greenman 			tx_threshold += 64;
1114f9be9005SDavid Greenman 	}
1115f7788e8eSJonathan Lemon 	s = splimp();
1116397f9dfeSDavid Greenman 	/*
1117c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1118c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1119c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1120c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1121c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1122c8cc6fcaSDavid Greenman 	 */
1123c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1124c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1125c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1126c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1127c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1128c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1129c8cc6fcaSDavid Greenman 		}
1130c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1131c8cc6fcaSDavid Greenman 	}
1132c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1133c8cc6fcaSDavid Greenman 	/*
1134397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1135397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1136397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1137397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1138397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1139397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1140397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1141397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1142397f9dfeSDavid Greenman 	 */
1143397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1144397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1145397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1146397f9dfeSDavid Greenman 	}
1147f9be9005SDavid Greenman 	/*
11483ba65732SDavid Greenman 	 * If there is no pending command, start another stats
11493ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1150a17c678eSDavid Greenman 	 */
1151397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1152a17c678eSDavid Greenman 		/*
1153397f9dfeSDavid Greenman 		 * Start another stats dump.
1154a17c678eSDavid Greenman 		 */
1155ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1156ba8c6fd5SDavid Greenman 		    FXP_SCB_COMMAND_CU_DUMPRESET);
1157dfe61cf1SDavid Greenman 	} else {
1158dfe61cf1SDavid Greenman 		/*
1159dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1160dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
11613ba65732SDavid Greenman 		 * next timer event to update them.
1162dfe61cf1SDavid Greenman 		 */
1163dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1164f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1165dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
11663ba65732SDavid Greenman 
1167dfe61cf1SDavid Greenman 		sp->rx_good = 0;
11683ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
11693ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
11703ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
11713ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1172dfe61cf1SDavid Greenman 	}
1173f7788e8eSJonathan Lemon 
1174f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1175f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
1176f7788e8eSJonathan Lemon 
1177a17c678eSDavid Greenman 	/*
1178a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1179a17c678eSDavid Greenman 	 */
1180f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1181a17c678eSDavid Greenman }
1182a17c678eSDavid Greenman 
1183a17c678eSDavid Greenman /*
1184a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1185a17c678eSDavid Greenman  * the interface.
1186a17c678eSDavid Greenman  */
1187a17c678eSDavid Greenman static void
1188f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1189a17c678eSDavid Greenman {
1190ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
11913ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
11923ba65732SDavid Greenman 	int i;
1193a17c678eSDavid Greenman 
11940f4dc94cSChuck Paterson 
11957dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
11967dced78aSDavid Greenman 	ifp->if_timer = 0;
11977dced78aSDavid Greenman 
1198a17c678eSDavid Greenman 	/*
1199a17c678eSDavid Greenman 	 * Cancel stats updater.
1200a17c678eSDavid Greenman 	 */
1201f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
12023ba65732SDavid Greenman 
12033ba65732SDavid Greenman 	/*
12043ba65732SDavid Greenman 	 * Issue software reset
12053ba65732SDavid Greenman 	 */
1206ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1207a17c678eSDavid Greenman 	DELAY(10);
1208a17c678eSDavid Greenman 
12093ba65732SDavid Greenman 	/*
12103ba65732SDavid Greenman 	 * Release any xmit buffers.
12113ba65732SDavid Greenman 	 */
1212da91462dSDavid Greenman 	txp = sc->cbl_base;
1213da91462dSDavid Greenman 	if (txp != NULL) {
1214da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1215da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1216da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1217da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1218da91462dSDavid Greenman 			}
1219da91462dSDavid Greenman 		}
12203ba65732SDavid Greenman 	}
12213ba65732SDavid Greenman 	sc->tx_queued = 0;
12223ba65732SDavid Greenman 
12233ba65732SDavid Greenman 	/*
12243ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
12253ba65732SDavid Greenman 	 */
12263ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
12273ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
12283ba65732SDavid Greenman 	sc->rfa_headm = NULL;
12293ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
12303ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
12313ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
12323ba65732SDavid Greenman 			/*
12333ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
12343ba65732SDavid Greenman 			 * and we just freed all the buffers we need
12353ba65732SDavid Greenman 			 * above.
12363ba65732SDavid Greenman 			 */
12373ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
12383ba65732SDavid Greenman 		}
12393ba65732SDavid Greenman 	}
1240a17c678eSDavid Greenman }
1241a17c678eSDavid Greenman 
1242a17c678eSDavid Greenman /*
1243a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1244a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1245a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1246a17c678eSDavid Greenman  * card has wedged for some reason.
1247a17c678eSDavid Greenman  */
1248a17c678eSDavid Greenman static void
1249f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1250a17c678eSDavid Greenman {
1251ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1252ba8c6fd5SDavid Greenman 
1253f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
12544a5f1499SDavid Greenman 	ifp->if_oerrors++;
1255a17c678eSDavid Greenman 
1256ba8c6fd5SDavid Greenman 	fxp_init(sc);
1257a17c678eSDavid Greenman }
1258a17c678eSDavid Greenman 
1259a17c678eSDavid Greenman static void
1260f7788e8eSJonathan Lemon fxp_init(void *xsc)
1261a17c678eSDavid Greenman {
1262fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1263ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1264a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1265a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1266a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1267f7788e8eSJonathan Lemon 	int i, prm, s;
1268a17c678eSDavid Greenman 
1269f7788e8eSJonathan Lemon 	s = splimp();
1270a17c678eSDavid Greenman 	/*
12713ba65732SDavid Greenman 	 * Cancel any pending I/O
1272a17c678eSDavid Greenman 	 */
12733ba65732SDavid Greenman 	fxp_stop(sc);
1274a17c678eSDavid Greenman 
1275a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1276a17c678eSDavid Greenman 
1277a17c678eSDavid Greenman 	/*
1278a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1279a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1280a17c678eSDavid Greenman 	 */
1281ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1282ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1283a17c678eSDavid Greenman 
1284ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1285ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1286a17c678eSDavid Greenman 
1287a17c678eSDavid Greenman 	/*
1288a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1289a17c678eSDavid Greenman 	 */
1290ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1291ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
1292ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1293a17c678eSDavid Greenman 
1294a17c678eSDavid Greenman 	/*
1295a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1296a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1297a17c678eSDavid Greenman 	 * later.
1298a17c678eSDavid Greenman 	 */
1299a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1300a17c678eSDavid Greenman 
1301a17c678eSDavid Greenman 	/*
1302a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1303a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1304a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1305a17c678eSDavid Greenman 	 */
1306d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1307d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1308397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1309a17c678eSDavid Greenman 
1310a17c678eSDavid Greenman 	cbp->cb_status =	0;
1311a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1312a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1313a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1314001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1315001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1316a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1317f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1318f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1319f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1320f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1321001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1322001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1323f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1324a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1325f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1326f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
13273114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1328f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1329f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1330f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
1331a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1332a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1333f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1334f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1335f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1336f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1337f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1338f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1339f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1340f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1341f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1342f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1343a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1344a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1345a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1346a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1347a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1348a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1349a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1350a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1351f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1352f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1353f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1354f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1355f7788e8eSJonathan Lemon 
1356a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1357a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1358a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1359f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1360f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1361f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1362f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1363a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
13643ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1365a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1366f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1367a17c678eSDavid Greenman 
13683bd07cfdSJonathan Lemon 	if (sc->chip == FXP_CHIP_82557) {
13693bd07cfdSJonathan Lemon 		/*
13703bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
13713bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
13723bd07cfdSJonathan Lemon 		 */
13733bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
13743bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
13753bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
13763bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
13773bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
13783bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
13793bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
13803bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
13813bd07cfdSJonathan Lemon 	} else {
13823bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
13833bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
13843bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
13853bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
13863bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
13873bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
13883bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
13893bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
13903bd07cfdSJonathan Lemon 	}
13913bd07cfdSJonathan Lemon 
1392a17c678eSDavid Greenman 	/*
1393a17c678eSDavid Greenman 	 * Start the config command/DMA.
1394a17c678eSDavid Greenman 	 */
1395ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1396397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
1397ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1398a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
13997dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1400a17c678eSDavid Greenman 
1401a17c678eSDavid Greenman 	/*
1402a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1403a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1404a17c678eSDavid Greenman 	 */
1405a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1406a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1407a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1408a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1409d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1410d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1411a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1412a17c678eSDavid Greenman 
1413a17c678eSDavid Greenman 	/*
1414a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1415a17c678eSDavid Greenman 	 */
1416ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1417ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1418a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
14197dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1420a17c678eSDavid Greenman 
1421a17c678eSDavid Greenman 	/*
1422a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1423a17c678eSDavid Greenman 	 */
1424a17c678eSDavid Greenman 
1425a17c678eSDavid Greenman 	txp = sc->cbl_base;
1426a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1427a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1428a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1429a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
14303bd07cfdSJonathan Lemon 		txp[i].link_addr =
14313bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
14323bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
14333bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
14343bd07cfdSJonathan Lemon 		else
1435a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1436a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1437a17c678eSDavid Greenman 	}
1438a17c678eSDavid Greenman 	/*
1439397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1440a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1441a17c678eSDavid Greenman 	 */
1442a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1443a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1444397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1445a17c678eSDavid Greenman 
1446ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1447ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1448a17c678eSDavid Greenman 
1449a17c678eSDavid Greenman 	/*
1450a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1451a17c678eSDavid Greenman 	 */
1452ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1453ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1454ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
1455ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1456a17c678eSDavid Greenman 
1457dccee1a1SDavid Greenman 	/*
1458ba8c6fd5SDavid Greenman 	 * Set current media.
1459dccee1a1SDavid Greenman 	 */
1460f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1461f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1462dccee1a1SDavid Greenman 
1463a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1464a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1465f7788e8eSJonathan Lemon 	splx(s);
1466a17c678eSDavid Greenman 
1467a17c678eSDavid Greenman 	/*
1468a17c678eSDavid Greenman 	 * Start stats updater.
1469a17c678eSDavid Greenman 	 */
1470f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1471f7788e8eSJonathan Lemon }
1472f7788e8eSJonathan Lemon 
1473f7788e8eSJonathan Lemon static int
1474f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1475f7788e8eSJonathan Lemon {
1476f7788e8eSJonathan Lemon 
1477f7788e8eSJonathan Lemon 	return (0);
1478a17c678eSDavid Greenman }
1479a17c678eSDavid Greenman 
1480303b270bSEivind Eklund static void
1481f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1482ba8c6fd5SDavid Greenman {
1483ba8c6fd5SDavid Greenman 
1484f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1485ba8c6fd5SDavid Greenman }
1486ba8c6fd5SDavid Greenman 
1487ba8c6fd5SDavid Greenman /*
1488ba8c6fd5SDavid Greenman  * Change media according to request.
1489ba8c6fd5SDavid Greenman  */
1490f7788e8eSJonathan Lemon static int
1491f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1492ba8c6fd5SDavid Greenman {
1493ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1494f7788e8eSJonathan Lemon 	struct mii_data *mii;
1495ba8c6fd5SDavid Greenman 
1496f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1497f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1498ba8c6fd5SDavid Greenman 	return (0);
1499ba8c6fd5SDavid Greenman }
1500ba8c6fd5SDavid Greenman 
1501ba8c6fd5SDavid Greenman /*
1502ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1503ba8c6fd5SDavid Greenman  */
1504f7788e8eSJonathan Lemon static void
1505f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1506ba8c6fd5SDavid Greenman {
1507ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1508f7788e8eSJonathan Lemon 	struct mii_data *mii;
1509ba8c6fd5SDavid Greenman 
1510f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1511f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1512f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1513f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
1514ba8c6fd5SDavid Greenman }
1515ba8c6fd5SDavid Greenman 
1516a17c678eSDavid Greenman /*
1517a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1518a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1519a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1520dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1521a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1522a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1523a17c678eSDavid Greenman  */
1524a17c678eSDavid Greenman static int
1525f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1526a17c678eSDavid Greenman {
1527ba8c6fd5SDavid Greenman 	u_int32_t v;
1528a17c678eSDavid Greenman 	struct mbuf *m;
1529a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1530a17c678eSDavid Greenman 
1531a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1532a17c678eSDavid Greenman 	if (m != NULL) {
1533a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1534a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1535a17c678eSDavid Greenman 			m_freem(m);
1536eadd5e3aSDavid Greenman 			if (oldm == NULL)
1537eadd5e3aSDavid Greenman 				return 1;
1538a17c678eSDavid Greenman 			m = oldm;
1539eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1540a17c678eSDavid Greenman 		}
1541a17c678eSDavid Greenman 	} else {
1542eadd5e3aSDavid Greenman 		if (oldm == NULL)
1543a17c678eSDavid Greenman 			return 1;
1544eadd5e3aSDavid Greenman 		m = oldm;
1545eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1546eadd5e3aSDavid Greenman 	}
1547ba8c6fd5SDavid Greenman 
1548ba8c6fd5SDavid Greenman 	/*
1549ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1550ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1551ba8c6fd5SDavid Greenman 	 */
1552ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1553ba8c6fd5SDavid Greenman 
1554eadd5e3aSDavid Greenman 	/*
1555eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1556eadd5e3aSDavid Greenman 	 * data start past it.
1557eadd5e3aSDavid Greenman 	 */
1558a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1559eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
15604fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1561eadd5e3aSDavid Greenman 
1562ba8c6fd5SDavid Greenman 	/*
1563ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1564ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1565ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1566ba8c6fd5SDavid Greenman 	 */
15674fc1dda9SAndrew Gallatin 
1568a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1569a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1570a17c678eSDavid Greenman 	rfa->actual_size = 0;
1571ba8c6fd5SDavid Greenman 
1572ba8c6fd5SDavid Greenman 	v = -1;
15734fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
15744fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1575ba8c6fd5SDavid Greenman 
1576dfe61cf1SDavid Greenman 	/*
1577dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1578dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1579dfe61cf1SDavid Greenman 	 */
1580a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1581ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1582ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1583a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1584ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
15854fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1586aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1587a17c678eSDavid Greenman 	} else {
1588a17c678eSDavid Greenman 		sc->rfa_headm = m;
1589a17c678eSDavid Greenman 	}
1590a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1591a17c678eSDavid Greenman 
1592dfe61cf1SDavid Greenman 	return (m == oldm);
1593a17c678eSDavid Greenman }
1594a17c678eSDavid Greenman 
15956ebc3153SDavid Greenman static volatile int
1596f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1597dccee1a1SDavid Greenman {
1598f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1599dccee1a1SDavid Greenman 	int count = 10000;
16006ebc3153SDavid Greenman 	int value;
1601dccee1a1SDavid Greenman 
1602ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1603ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1604dccee1a1SDavid Greenman 
1605ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1606ba8c6fd5SDavid Greenman 	    && count--)
16076ebc3153SDavid Greenman 		DELAY(10);
1608dccee1a1SDavid Greenman 
1609dccee1a1SDavid Greenman 	if (count <= 0)
1610f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1611dccee1a1SDavid Greenman 
16126ebc3153SDavid Greenman 	return (value & 0xffff);
1613dccee1a1SDavid Greenman }
1614dccee1a1SDavid Greenman 
1615dccee1a1SDavid Greenman static void
1616f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1617dccee1a1SDavid Greenman {
1618f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1619dccee1a1SDavid Greenman 	int count = 10000;
1620dccee1a1SDavid Greenman 
1621ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1622ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1623ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1624dccee1a1SDavid Greenman 
1625ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1626ba8c6fd5SDavid Greenman 	    count--)
16276ebc3153SDavid Greenman 		DELAY(10);
1628dccee1a1SDavid Greenman 
1629dccee1a1SDavid Greenman 	if (count <= 0)
1630f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1631dccee1a1SDavid Greenman }
1632dccee1a1SDavid Greenman 
1633dccee1a1SDavid Greenman static int
1634f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1635a17c678eSDavid Greenman {
16369b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1637a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1638f7788e8eSJonathan Lemon 	struct mii_data *mii;
1639f7788e8eSJonathan Lemon 	int s, error = 0;
1640a17c678eSDavid Greenman 
1641f7788e8eSJonathan Lemon 	s = splimp();
1642a17c678eSDavid Greenman 
1643a17c678eSDavid Greenman 	switch (command) {
1644a17c678eSDavid Greenman 	case SIOCSIFADDR:
1645a17c678eSDavid Greenman 	case SIOCGIFADDR:
1646fb583156SDavid Greenman 	case SIOCSIFMTU:
1647fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1648a17c678eSDavid Greenman 		break;
1649a17c678eSDavid Greenman 
1650a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1651f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1652f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1653f7788e8eSJonathan Lemon 		else
1654f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1655a17c678eSDavid Greenman 
1656a17c678eSDavid Greenman 		/*
1657a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1658a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1659a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1660a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1661a17c678eSDavid Greenman 		 */
1662a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1663fb583156SDavid Greenman 			fxp_init(sc);
1664a17c678eSDavid Greenman 		} else {
1665a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
16664a5f1499SDavid Greenman 				fxp_stop(sc);
1667a17c678eSDavid Greenman 		}
1668a17c678eSDavid Greenman 		break;
1669a17c678eSDavid Greenman 
1670a17c678eSDavid Greenman 	case SIOCADDMULTI:
1671a17c678eSDavid Greenman 	case SIOCDELMULTI:
1672f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1673f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1674f7788e8eSJonathan Lemon 		else
1675f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1676a17c678eSDavid Greenman 		/*
1677a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1678a17c678eSDavid Greenman 		 * accordingly.
1679a17c678eSDavid Greenman 		 */
1680f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1681397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1682397f9dfeSDavid Greenman 		/*
1683f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1684397f9dfeSDavid Greenman 		 * again rather than else {}.
1685397f9dfeSDavid Greenman 		 */
1686f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1687fb583156SDavid Greenman 			fxp_init(sc);
1688a17c678eSDavid Greenman 		error = 0;
1689ba8c6fd5SDavid Greenman 		break;
1690ba8c6fd5SDavid Greenman 
1691ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1692ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1693f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
1694f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
1695f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
1696f7788e8eSJonathan Lemon                             &mii->mii_media, command);
1697f7788e8eSJonathan Lemon 		} else {
1698ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1699f7788e8eSJonathan Lemon 		}
1700a17c678eSDavid Greenman 		break;
1701a17c678eSDavid Greenman 
1702a17c678eSDavid Greenman 	default:
1703a17c678eSDavid Greenman 		error = EINVAL;
1704a17c678eSDavid Greenman 	}
1705f7788e8eSJonathan Lemon 	splx(s);
1706a17c678eSDavid Greenman 	return (error);
1707a17c678eSDavid Greenman }
1708397f9dfeSDavid Greenman 
1709397f9dfeSDavid Greenman /*
1710397f9dfeSDavid Greenman  * Program the multicast filter.
1711397f9dfeSDavid Greenman  *
1712397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1713397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
17143114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1715397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1716dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1717397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1718397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1719397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1720397f9dfeSDavid Greenman  *
1721397f9dfeSDavid Greenman  * This function must be called at splimp.
1722397f9dfeSDavid Greenman  */
1723397f9dfeSDavid Greenman static void
1724f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
1725397f9dfeSDavid Greenman {
1726397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1727397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1728397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1729397f9dfeSDavid Greenman 	int nmcasts;
17307dced78aSDavid Greenman 	int count;
1731397f9dfeSDavid Greenman 
17323114fdb4SDavid Greenman 	/*
17333114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
17343114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
17353114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
17363114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
17373114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
17383114fdb4SDavid Greenman 	 */
1739397f9dfeSDavid Greenman 	if (sc->tx_queued) {
17403114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
17413114fdb4SDavid Greenman 
17423114fdb4SDavid Greenman 		/*
17433114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
17443114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
17453114fdb4SDavid Greenman 		 */
17463114fdb4SDavid Greenman 		if (sc->need_mcsetup)
17473114fdb4SDavid Greenman 			return;
1748397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
17493114fdb4SDavid Greenman 
17503114fdb4SDavid Greenman 		/*
17513114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
17523114fdb4SDavid Greenman 		 * TX commands have been processed.
17533114fdb4SDavid Greenman 		 */
17543114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
17553114fdb4SDavid Greenman 		txp->mb_head = NULL;
17563114fdb4SDavid Greenman 		txp->cb_status = 0;
17573114fdb4SDavid Greenman 		txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
17583114fdb4SDavid Greenman 		/*
17593114fdb4SDavid Greenman 		 * Advance the end of list forward.
17603114fdb4SDavid Greenman 		 */
17613114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
17623114fdb4SDavid Greenman 		sc->cbl_last = txp;
17633114fdb4SDavid Greenman 		sc->tx_queued++;
17643114fdb4SDavid Greenman 		/*
17653114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
17663114fdb4SDavid Greenman 		 */
17673114fdb4SDavid Greenman 		fxp_scb_wait(sc);
17683114fdb4SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
17693114fdb4SDavid Greenman 		/*
17703114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
17713114fdb4SDavid Greenman 		 * card again.
17723114fdb4SDavid Greenman 		 */
17733114fdb4SDavid Greenman 		ifp->if_timer = 5;
17743114fdb4SDavid Greenman 
1775397f9dfeSDavid Greenman 		return;
1776397f9dfeSDavid Greenman 	}
1777397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
1778397f9dfeSDavid Greenman 
1779397f9dfeSDavid Greenman 	/*
1780397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
1781397f9dfeSDavid Greenman 	 */
1782397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
1783397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
1784397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
17853114fdb4SDavid Greenman 	mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
1786397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
1787397f9dfeSDavid Greenman 
1788397f9dfeSDavid Greenman 	nmcasts = 0;
1789f7788e8eSJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
1790f7788e8eSJonathan Lemon #if __FreeBSD_version < 500000
1791f7788e8eSJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1792f7788e8eSJonathan Lemon #else
17936817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1794f7788e8eSJonathan Lemon #endif
1795397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
1796397f9dfeSDavid Greenman 				continue;
1797397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
1798f7788e8eSJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
1799397f9dfeSDavid Greenman 				nmcasts = 0;
1800397f9dfeSDavid Greenman 				break;
1801397f9dfeSDavid Greenman 			}
1802397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1803d244b0e9SPeter Wemm 			    (void *)(uintptr_t)(volatile void *)
1804d244b0e9SPeter Wemm 				&sc->mcsp->mc_addr[nmcasts][0], 6);
1805397f9dfeSDavid Greenman 			nmcasts++;
1806397f9dfeSDavid Greenman 		}
1807397f9dfeSDavid Greenman 	}
1808397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
1809397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
1810397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1811397f9dfeSDavid Greenman 
1812397f9dfeSDavid Greenman 	/*
1813397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
1814397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
1815397f9dfeSDavid Greenman 	 */
18167dced78aSDavid Greenman 	count = 100;
1817397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
18187dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
18197dced78aSDavid Greenman 		DELAY(10);
18207dced78aSDavid Greenman 	if (count == 0) {
1821f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
18227dced78aSDavid Greenman 		return;
18237dced78aSDavid Greenman 	}
1824397f9dfeSDavid Greenman 
1825397f9dfeSDavid Greenman 	/*
1826397f9dfeSDavid Greenman 	 * Start the multicast setup command.
1827397f9dfeSDavid Greenman 	 */
1828397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
1829397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
1830397f9dfeSDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1831397f9dfeSDavid Greenman 
18323114fdb4SDavid Greenman 	ifp->if_timer = 2;
1833397f9dfeSDavid Greenman 	return;
1834397f9dfeSDavid Greenman }
1835