xref: /freebsd/sys/dev/fxp/if_fxp.c (revision f7788e8e9f0e7e88d774306739c9691b01ac5f08)
1f7788e8eSJonathan Lemon /*-
2f7788e8eSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
3a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
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 	/*
435f7788e8eSJonathan Lemon 	 * Determine in 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 	/*
443f7788e8eSJonathan Lemon 	 * Read MAC address.
444f7788e8eSJonathan Lemon 	 */
445f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
446f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
447f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
448f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
449f7788e8eSJonathan Lemon 	if (bootverbose) {
450f7788e8eSJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x\n",
451f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
452f7788e8eSJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev));
453f7788e8eSJonathan Lemon 	}
454f7788e8eSJonathan Lemon 
455f7788e8eSJonathan Lemon 	/*
456f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
457f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
458f7788e8eSJonathan Lemon 	 *
459f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
460f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
461f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
462f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
463f7788e8eSJonathan Lemon 	 */
464f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
465f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
466f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
467f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
468f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
469f7788e8eSJonathan Lemon 	} else {
470f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
471f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
472f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
4736182fdbdSPeter Wemm 			error = ENXIO;
474ba8c6fd5SDavid Greenman 			goto fail;
475a17c678eSDavid Greenman 		}
476f7788e8eSJonathan Lemon 	}
477dccee1a1SDavid Greenman 
478a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
4796182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
480a17c678eSDavid Greenman 	ifp->if_name = "fxp";
481a17c678eSDavid Greenman 	ifp->if_output = ether_output;
482a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
483fb583156SDavid Greenman 	ifp->if_init = fxp_init;
484ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
485ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
486ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
487ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
488ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
489a17c678eSDavid Greenman 
490dfe61cf1SDavid Greenman 	/*
491dfe61cf1SDavid Greenman 	 * Attach the interface.
492dfe61cf1SDavid Greenman 	 */
49321b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
494f7788e8eSJonathan Lemon 
495483b9871SDavid Greenman 	/*
4963114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
4973114fdb4SDavid Greenman 	 * TX descriptors.
498483b9871SDavid Greenman 	 */
4993114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
5004a684684SDavid Greenman 
501f7788e8eSJonathan Lemon 	splx(s);
502f7788e8eSJonathan Lemon 	return (0);
503a17c678eSDavid Greenman 
504f7788e8eSJonathan Lemon failmem:
505f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
506f7788e8eSJonathan Lemon 	error = ENOMEM;
507a17c678eSDavid Greenman fail:
508f7788e8eSJonathan Lemon 	splx(s);
509f7788e8eSJonathan Lemon 	fxp_release(sc);
510f7788e8eSJonathan Lemon 	return (error);
511f7788e8eSJonathan Lemon }
512f7788e8eSJonathan Lemon 
513f7788e8eSJonathan Lemon /*
514f7788e8eSJonathan Lemon  * release all resources
515f7788e8eSJonathan Lemon  */
516f7788e8eSJonathan Lemon static void
517f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
518f7788e8eSJonathan Lemon {
519f7788e8eSJonathan Lemon 
520f7788e8eSJonathan Lemon 	if (sc->miibus) {
521f7788e8eSJonathan Lemon 		bus_generic_detach(sc->dev);
522f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
523f7788e8eSJonathan Lemon 	}
524f7788e8eSJonathan Lemon 
525f7788e8eSJonathan Lemon 	if (sc->cbl_base)
526f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
527f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
528f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
529f7788e8eSJonathan Lemon 	if (sc->mcsp)
530f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
531f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
532f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
533f7788e8eSJonathan Lemon 
534f7788e8eSJonathan Lemon 	if (sc->ih)
535f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
536f7788e8eSJonathan Lemon 	if (sc->irq)
537f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
538f7788e8eSJonathan Lemon 	if (sc->mem)
539f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
5400f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
5416182fdbdSPeter Wemm }
5426182fdbdSPeter Wemm 
5436182fdbdSPeter Wemm /*
5446182fdbdSPeter Wemm  * Detach interface.
5456182fdbdSPeter Wemm  */
5466182fdbdSPeter Wemm static int
5476182fdbdSPeter Wemm fxp_detach(device_t dev)
5486182fdbdSPeter Wemm {
5496182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
550f7788e8eSJonathan Lemon 	int s;
5516182fdbdSPeter Wemm 
552f7788e8eSJonathan Lemon 	s = splimp();
5536182fdbdSPeter Wemm 
5546182fdbdSPeter Wemm 	/*
5556182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
5566182fdbdSPeter Wemm 	 */
5576182fdbdSPeter Wemm 	fxp_stop(sc);
5586182fdbdSPeter Wemm 
5596182fdbdSPeter Wemm 	/*
560f7788e8eSJonathan Lemon 	 * Close down routes etc.
5616182fdbdSPeter Wemm 	 */
562f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
5636182fdbdSPeter Wemm 
5646182fdbdSPeter Wemm 	/*
5656182fdbdSPeter Wemm 	 * Free all media structures.
5666182fdbdSPeter Wemm 	 */
5676182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
5686182fdbdSPeter Wemm 
569f7788e8eSJonathan Lemon 	splx(s);
5706182fdbdSPeter Wemm 
571f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
572f7788e8eSJonathan Lemon 	fxp_release(sc);
5736182fdbdSPeter Wemm 
574f7788e8eSJonathan Lemon 	return (0);
575a17c678eSDavid Greenman }
576a17c678eSDavid Greenman 
577a17c678eSDavid Greenman /*
5784a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
579a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
580a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
581a17c678eSDavid Greenman  */
5826182fdbdSPeter Wemm static int
5836182fdbdSPeter Wemm fxp_shutdown(device_t dev)
584a17c678eSDavid Greenman {
5856182fdbdSPeter Wemm 	/*
5866182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
5876182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
5886182fdbdSPeter Wemm 	 * reboot before the driver initializes.
5896182fdbdSPeter Wemm 	 */
5906182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
591f7788e8eSJonathan Lemon 	return (0);
592a17c678eSDavid Greenman }
593a17c678eSDavid Greenman 
5947dced78aSDavid Greenman /*
5957dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
5967dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
5977dced78aSDavid Greenman  * resume.
5987dced78aSDavid Greenman  */
5997dced78aSDavid Greenman static int
6007dced78aSDavid Greenman fxp_suspend(device_t dev)
6017dced78aSDavid Greenman {
6027dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
603f7788e8eSJonathan Lemon 	int i, s;
6047dced78aSDavid Greenman 
605f7788e8eSJonathan Lemon 	s = splimp();
6067dced78aSDavid Greenman 
6077dced78aSDavid Greenman 	fxp_stop(sc);
6087dced78aSDavid Greenman 
6097dced78aSDavid Greenman 	for (i=0; i<5; i++)
6107dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i*4, 4);
6117dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
6127dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
6137dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
6147dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
6157dced78aSDavid Greenman 
6167dced78aSDavid Greenman 	sc->suspended = 1;
6177dced78aSDavid Greenman 
618f7788e8eSJonathan Lemon 	splx(s);
619f7788e8eSJonathan Lemon 	return (0);
6207dced78aSDavid Greenman }
6217dced78aSDavid Greenman 
6227dced78aSDavid Greenman /*
6237dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
6247dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
6257dced78aSDavid Greenman  * appropriate.
6267dced78aSDavid Greenman  */
6277dced78aSDavid Greenman static int
6287dced78aSDavid Greenman fxp_resume(device_t dev)
6297dced78aSDavid Greenman {
6307dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
6317dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
6327dced78aSDavid Greenman 	u_int16_t pci_command;
633f7788e8eSJonathan Lemon 	int i, s;
6347dced78aSDavid Greenman 
635f7788e8eSJonathan Lemon 	s = splimp();
6367dced78aSDavid Greenman 
6377dced78aSDavid Greenman 	/* better way to do this? */
6387dced78aSDavid Greenman 	for (i=0; i<5; i++)
6397dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i*4, sc->saved_maps[i], 4);
6407dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
6417dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
6427dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
6437dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
6447dced78aSDavid Greenman 
6457dced78aSDavid Greenman 	/* reenable busmastering */
6467dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
6477dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
6487dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
6497dced78aSDavid Greenman 
6507dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
6517dced78aSDavid Greenman 	DELAY(10);
6527dced78aSDavid Greenman 
6537dced78aSDavid Greenman 	/* reinitialize interface if necessary */
6547dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
6557dced78aSDavid Greenman 		fxp_init(sc);
6567dced78aSDavid Greenman 
6577dced78aSDavid Greenman 	sc->suspended = 0;
6587dced78aSDavid Greenman 
659f7788e8eSJonathan Lemon 	splx(s);
660ba8c6fd5SDavid Greenman 	return (0);
661f7788e8eSJonathan Lemon }
662ba8c6fd5SDavid Greenman 
663f7788e8eSJonathan Lemon /*
664f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
665f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
666f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
667f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
668f7788e8eSJonathan Lemon  * every 16 bits of data.
669f7788e8eSJonathan Lemon  */
670f7788e8eSJonathan Lemon static u_int16_t
671f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
672f7788e8eSJonathan Lemon {
673f7788e8eSJonathan Lemon 	u_int16_t reg, data;
674f7788e8eSJonathan Lemon 	int x;
675ba8c6fd5SDavid Greenman 
676f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
677f7788e8eSJonathan Lemon 	/*
678f7788e8eSJonathan Lemon 	 * Shift in read opcode.
679f7788e8eSJonathan Lemon 	 */
680f7788e8eSJonathan Lemon 	for (x = 1 << 2; x; x >>= 1) {
681f7788e8eSJonathan Lemon 		if (FXP_EEPROM_OPC_READ & x)
682f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
683f7788e8eSJonathan Lemon 		else
684f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
685f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
686f7788e8eSJonathan Lemon 		DELAY(1);
687f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
688f7788e8eSJonathan Lemon 		DELAY(1);
689f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
690f7788e8eSJonathan Lemon 		DELAY(1);
691f7788e8eSJonathan Lemon 	}
692f7788e8eSJonathan Lemon 	/*
693f7788e8eSJonathan Lemon 	 * Shift in address.
694f7788e8eSJonathan Lemon 	 */
695f7788e8eSJonathan Lemon 	data = 0;
696f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
697f7788e8eSJonathan Lemon 		if (offset & x)
698f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
699f7788e8eSJonathan Lemon 		else
700f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
701f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
702f7788e8eSJonathan Lemon 		DELAY(1);
703f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
704f7788e8eSJonathan Lemon 		DELAY(1);
705f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
706f7788e8eSJonathan Lemon 		DELAY(1);
707f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
708f7788e8eSJonathan Lemon 		data++;
709f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
710f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
711f7788e8eSJonathan Lemon 			break;
712f7788e8eSJonathan Lemon 		}
713f7788e8eSJonathan Lemon 	}
714f7788e8eSJonathan Lemon 	/*
715f7788e8eSJonathan Lemon 	 * Shift out data.
716f7788e8eSJonathan Lemon 	 */
717f7788e8eSJonathan Lemon 	data = 0;
718f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
719f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
720f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
721f7788e8eSJonathan Lemon 		DELAY(1);
722f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
723f7788e8eSJonathan Lemon 			data |= x;
724f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
725f7788e8eSJonathan Lemon 		DELAY(1);
726f7788e8eSJonathan Lemon 	}
727f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
728f7788e8eSJonathan Lemon 	DELAY(1);
729f7788e8eSJonathan Lemon 
730f7788e8eSJonathan Lemon 	return (data);
731ba8c6fd5SDavid Greenman }
732ba8c6fd5SDavid Greenman 
733ba8c6fd5SDavid Greenman /*
734e9bf2fa7SDavid Greenman  * From NetBSD:
735e9bf2fa7SDavid Greenman  *
736e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
737e9bf2fa7SDavid Greenman  *
738e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
739e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
740e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
741e9bf2fa7SDavid Greenman  *
742e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
743e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
744e9bf2fa7SDavid Greenman  *
745e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
746e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
747e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
748e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
749e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
750e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
751e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
752e9bf2fa7SDavid Greenman  *
753e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
754e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
755e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
756e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
757e9bf2fa7SDavid Greenman  *
758e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
759e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
760e9bf2fa7SDavid Greenman  */
761e9bf2fa7SDavid Greenman static void
762f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
763e9bf2fa7SDavid Greenman {
764e9bf2fa7SDavid Greenman 
765f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
766f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
767f7788e8eSJonathan Lemon 
768f7788e8eSJonathan Lemon 	/* autosize */
769f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
770e9bf2fa7SDavid Greenman }
771f7788e8eSJonathan Lemon 
772ba8c6fd5SDavid Greenman static void
773f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
774ba8c6fd5SDavid Greenman {
775f7788e8eSJonathan Lemon 	int i;
776ba8c6fd5SDavid Greenman 
777f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
778f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
779ba8c6fd5SDavid Greenman }
780ba8c6fd5SDavid Greenman 
781a17c678eSDavid Greenman /*
782a17c678eSDavid Greenman  * Start packet transmission on the interface.
783a17c678eSDavid Greenman  */
784a17c678eSDavid Greenman static void
785f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
786a17c678eSDavid Greenman {
7879b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
788a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
789a17c678eSDavid Greenman 
790a17c678eSDavid Greenman 	/*
791483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
792483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
793483b9871SDavid Greenman 	 * of the command chain).
794a17c678eSDavid Greenman 	 */
7950f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
796a17c678eSDavid Greenman 		return;
7970f4dc94cSChuck Paterson 	}
7981cd443acSDavid Greenman 
799483b9871SDavid Greenman 	txp = NULL;
800483b9871SDavid Greenman 
801483b9871SDavid Greenman 	/*
802483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
803483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
8043114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
8053114fdb4SDavid Greenman 	 *       a NOP command when needed.
806483b9871SDavid Greenman 	 */
8073114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
808483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
809483b9871SDavid Greenman 		int segment;
810483b9871SDavid Greenman 
811dfe61cf1SDavid Greenman 		/*
812dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
813dfe61cf1SDavid Greenman 		 */
8146318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
815a17c678eSDavid Greenman 
816dfe61cf1SDavid Greenman 		/*
817483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
818dfe61cf1SDavid Greenman 		 */
819a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
820a17c678eSDavid Greenman 
821a17c678eSDavid Greenman 		/*
822a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
823483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
824a17c678eSDavid Greenman 		 * and size of the mbuf.
825a17c678eSDavid Greenman 		 */
82623a0ed7cSDavid Greenman tbdinit:
827a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
828a17c678eSDavid Greenman 			if (m->m_len != 0) {
829a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
830a17c678eSDavid Greenman 					break;
831a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
832a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
833a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
834a17c678eSDavid Greenman 				segment++;
835a17c678eSDavid Greenman 			}
836a17c678eSDavid Greenman 		}
837fb583156SDavid Greenman 		if (m != NULL) {
83823a0ed7cSDavid Greenman 			struct mbuf *mn;
83923a0ed7cSDavid Greenman 
840a17c678eSDavid Greenman 			/*
841a17c678eSDavid Greenman 			 * We ran out of segments. We have to recopy this mbuf
842483b9871SDavid Greenman 			 * chain first. Bail out if we can't get the new buffers.
843a17c678eSDavid Greenman 			 */
84423a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
84523a0ed7cSDavid Greenman 			if (mn == NULL) {
84623a0ed7cSDavid Greenman 				m_freem(mb_head);
847483b9871SDavid Greenman 				break;
848a17c678eSDavid Greenman 			}
84923a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
85023a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
85123a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
85223a0ed7cSDavid Greenman 					m_freem(mn);
85323a0ed7cSDavid Greenman 					m_freem(mb_head);
854483b9871SDavid Greenman 					break;
85523a0ed7cSDavid Greenman 				}
85623a0ed7cSDavid Greenman 			}
857ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
858ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
85923a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
86023a0ed7cSDavid Greenman 			m_freem(mb_head);
86123a0ed7cSDavid Greenman 			mb_head = mn;
86223a0ed7cSDavid Greenman 			goto tbdinit;
86323a0ed7cSDavid Greenman 		}
86423a0ed7cSDavid Greenman 
86523a0ed7cSDavid Greenman 		txp->tbd_number = segment;
8661cd443acSDavid Greenman 		txp->mb_head = mb_head;
867a17c678eSDavid Greenman 		txp->cb_status = 0;
8683114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
869a17c678eSDavid Greenman 			txp->cb_command =
870a17c678eSDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S;
8713114fdb4SDavid Greenman 		} else {
8723114fdb4SDavid Greenman 			txp->cb_command =
8733114fdb4SDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
8743114fdb4SDavid Greenman 			/*
8753114fdb4SDavid Greenman 			 * Set a 5 second timer just in case we don't hear from the
8763114fdb4SDavid Greenman 			 * card again.
8773114fdb4SDavid Greenman 			 */
8783114fdb4SDavid Greenman 			ifp->if_timer = 5;
8793114fdb4SDavid Greenman 		}
880f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
881a17c678eSDavid Greenman 
882a17c678eSDavid Greenman 		/*
883483b9871SDavid Greenman 		 * Advance the end of list forward.
884a17c678eSDavid Greenman 		 */
88506175228SAndrew Gallatin 
88606175228SAndrew Gallatin #ifdef __alpha__
88706175228SAndrew Gallatin 		/*
88806175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
88906175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
89006175228SAndrew Gallatin 		 * up the status while we update the command field.
89106175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
89206175228SAndrew Gallatin 		 */
89306175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
89406175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
89506175228SAndrew Gallatin #else
896a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
89706175228SAndrew Gallatin #endif /*__alpha__*/
898a17c678eSDavid Greenman 		sc->cbl_last = txp;
899a17c678eSDavid Greenman 
900a17c678eSDavid Greenman 		/*
9011cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
9021cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
903483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
904a17c678eSDavid Greenman 		 */
9051cd443acSDavid Greenman 		if (sc->tx_queued == 0)
906a17c678eSDavid Greenman 			sc->cbl_first = txp;
907a17c678eSDavid Greenman 
9081cd443acSDavid Greenman 		sc->tx_queued++;
9091cd443acSDavid Greenman 
910a17c678eSDavid Greenman 		/*
911a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
912a17c678eSDavid Greenman 		 */
913fb583156SDavid Greenman 		if (ifp->if_bpf)
91494927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
915483b9871SDavid Greenman 	}
916483b9871SDavid Greenman 
917483b9871SDavid Greenman 	/*
918483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
919483b9871SDavid Greenman 	 * going again if suspended.
920483b9871SDavid Greenman 	 */
921483b9871SDavid Greenman 	if (txp != NULL) {
922483b9871SDavid Greenman 		fxp_scb_wait(sc);
923483b9871SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
924483b9871SDavid Greenman 	}
925a17c678eSDavid Greenman }
926a17c678eSDavid Greenman 
927a17c678eSDavid Greenman /*
9289c7d2607SDavid Greenman  * Process interface interrupts.
929a17c678eSDavid Greenman  */
93094927790SDavid Greenman static void
931f7788e8eSJonathan Lemon fxp_intr(void *xsc)
932a17c678eSDavid Greenman {
933f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
934ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
9351cd443acSDavid Greenman 	u_int8_t statack;
9360f4dc94cSChuck Paterson 
937a17c678eSDavid Greenman 
938b184b38eSDavid Greenman 	if (sc->suspended) {
939b184b38eSDavid Greenman 		return;
940b184b38eSDavid Greenman 	}
941b184b38eSDavid Greenman 
942b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
943a17c678eSDavid Greenman 		/*
944a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
945a17c678eSDavid Greenman 		 */
946ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
947a17c678eSDavid Greenman 
948a17c678eSDavid Greenman 		/*
9493114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
95006936301SBill Paul 		 *
95106936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
95206936301SBill Paul 		 * be that this event (control unit not ready) was not
95306936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
95406936301SBill Paul 		 * The exact sequence of events that occur when the interface
95506936301SBill Paul 		 * is brought up are different now, and if this event
95606936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
95706936301SBill Paul 		 * can stall for several seconds. The result is that no
95806936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
95906936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
9603114fdb4SDavid Greenman 		 */
96106936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
9623114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
9633114fdb4SDavid Greenman 
9643114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
9653114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
9663114fdb4SDavid Greenman 			    txp = txp->next) {
9673114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
9683114fdb4SDavid Greenman 					m_freem(txp->mb_head);
9693114fdb4SDavid Greenman 					txp->mb_head = NULL;
9703114fdb4SDavid Greenman 				}
9713114fdb4SDavid Greenman 				sc->tx_queued--;
9723114fdb4SDavid Greenman 			}
9733114fdb4SDavid Greenman 			sc->cbl_first = txp;
9743114fdb4SDavid Greenman 			ifp->if_timer = 0;
9753114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
9763114fdb4SDavid Greenman 				if (sc->need_mcsetup)
9773114fdb4SDavid Greenman 					fxp_mc_setup(sc);
9783114fdb4SDavid Greenman 			}
9793114fdb4SDavid Greenman 			/*
9803114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
9813114fdb4SDavid Greenman 			 */
9823114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
9833114fdb4SDavid Greenman 				fxp_start(ifp);
9843114fdb4SDavid Greenman 		}
9853114fdb4SDavid Greenman 		/*
986a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
987a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
988a17c678eSDavid Greenman 		 * re-start the receiver.
989a17c678eSDavid Greenman 		 */
990a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
991a17c678eSDavid Greenman 			struct mbuf *m;
992a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
993a17c678eSDavid Greenman rcvloop:
994a17c678eSDavid Greenman 			m = sc->rfa_headm;
995ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
996ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
997a17c678eSDavid Greenman 
998a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
999dfe61cf1SDavid Greenman 				/*
1000dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1001dfe61cf1SDavid Greenman 				 */
1002a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1003a17c678eSDavid Greenman 				m->m_next = NULL;
1004a17c678eSDavid Greenman 
1005dfe61cf1SDavid Greenman 				/*
1006ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1007ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1008ba8c6fd5SDavid Greenman 				 * instead.
1009dfe61cf1SDavid Greenman 				 */
1010a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1011a17c678eSDavid Greenman 					struct ether_header *eh;
1012aed53495SDavid Greenman 					int total_len;
1013a17c678eSDavid Greenman 
1014ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1015ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1016ba8c6fd5SDavid Greenman 					if (total_len <
1017ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
101806339180SDavid Greenman 						m_freem(m);
101906339180SDavid Greenman 						goto rcvloop;
102006339180SDavid Greenman 					}
1021a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
10222e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1023a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1024ba8c6fd5SDavid Greenman 					m->m_data +=
1025ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1026ab090e5bSLuigi Rizzo 					m->m_len -=
1027ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1028ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1029a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1030a17c678eSDavid Greenman 				}
1031a17c678eSDavid Greenman 				goto rcvloop;
1032a17c678eSDavid Greenman 			}
1033a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1034ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1035ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1036ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1037ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
1038ba8c6fd5SDavid Greenman 				CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1039ba8c6fd5SDavid Greenman 				    FXP_SCB_COMMAND_RU_START);
1040a17c678eSDavid Greenman 			}
1041a17c678eSDavid Greenman 		}
1042a17c678eSDavid Greenman 	}
1043a17c678eSDavid Greenman }
1044a17c678eSDavid Greenman 
1045dfe61cf1SDavid Greenman /*
1046dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1047dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1048dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1049dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1050dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1051dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1052dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1053dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1054dfe61cf1SDavid Greenman  * them again next time.
1055dfe61cf1SDavid Greenman  */
1056303b270bSEivind Eklund static void
1057f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1058a17c678eSDavid Greenman {
1059f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1060ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1061a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1062c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1063f7788e8eSJonathan Lemon 	int s;
1064a17c678eSDavid Greenman 
1065a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1066a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1067397f9dfeSDavid Greenman 	if (sp->rx_good) {
1068397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1069397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1070397f9dfeSDavid Greenman 	} else {
1071c8cc6fcaSDavid Greenman 		/*
1072c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1073c8cc6fcaSDavid Greenman 		 */
1074397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1075397f9dfeSDavid Greenman 	}
10763ba65732SDavid Greenman 	ifp->if_ierrors +=
10773ba65732SDavid Greenman 	    sp->rx_crc_errors +
10783ba65732SDavid Greenman 	    sp->rx_alignment_errors +
10793ba65732SDavid Greenman 	    sp->rx_rnr_errors +
10806e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1081a17c678eSDavid Greenman 	/*
1082f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1083f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1084f9be9005SDavid Greenman 	 */
1085f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1086f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1087f9be9005SDavid Greenman 		if (tx_threshold < 192)
1088f9be9005SDavid Greenman 			tx_threshold += 64;
1089f9be9005SDavid Greenman 	}
1090f7788e8eSJonathan Lemon 	s = splimp();
1091397f9dfeSDavid Greenman 	/*
1092c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1093c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1094c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1095c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1096c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1097c8cc6fcaSDavid Greenman 	 */
1098c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1099c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1100c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1101c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1102c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1103c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1104c8cc6fcaSDavid Greenman 		}
1105c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1106c8cc6fcaSDavid Greenman 	}
1107c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1108c8cc6fcaSDavid Greenman 	/*
1109397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1110397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1111397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1112397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1113397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1114397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1115397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1116397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1117397f9dfeSDavid Greenman 	 */
1118397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1119397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1120397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1121397f9dfeSDavid Greenman 	}
1122f9be9005SDavid Greenman 	/*
11233ba65732SDavid Greenman 	 * If there is no pending command, start another stats
11243ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1125a17c678eSDavid Greenman 	 */
1126397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1127a17c678eSDavid Greenman 		/*
1128397f9dfeSDavid Greenman 		 * Start another stats dump.
1129a17c678eSDavid Greenman 		 */
1130ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1131ba8c6fd5SDavid Greenman 		    FXP_SCB_COMMAND_CU_DUMPRESET);
1132dfe61cf1SDavid Greenman 	} else {
1133dfe61cf1SDavid Greenman 		/*
1134dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1135dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
11363ba65732SDavid Greenman 		 * next timer event to update them.
1137dfe61cf1SDavid Greenman 		 */
1138dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1139f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1140dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
11413ba65732SDavid Greenman 
1142dfe61cf1SDavid Greenman 		sp->rx_good = 0;
11433ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
11443ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
11453ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
11463ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1147dfe61cf1SDavid Greenman 	}
1148f7788e8eSJonathan Lemon 
1149f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1150f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
1151f7788e8eSJonathan Lemon 
1152a17c678eSDavid Greenman 	/*
1153a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1154a17c678eSDavid Greenman 	 */
1155f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1156a17c678eSDavid Greenman }
1157a17c678eSDavid Greenman 
1158a17c678eSDavid Greenman /*
1159a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1160a17c678eSDavid Greenman  * the interface.
1161a17c678eSDavid Greenman  */
1162a17c678eSDavid Greenman static void
1163f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1164a17c678eSDavid Greenman {
1165ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
11663ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
11673ba65732SDavid Greenman 	int i;
1168a17c678eSDavid Greenman 
11690f4dc94cSChuck Paterson 
11707dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
11717dced78aSDavid Greenman 	ifp->if_timer = 0;
11727dced78aSDavid Greenman 
1173a17c678eSDavid Greenman 	/*
1174a17c678eSDavid Greenman 	 * Cancel stats updater.
1175a17c678eSDavid Greenman 	 */
1176f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
11773ba65732SDavid Greenman 
11783ba65732SDavid Greenman 	/*
11793ba65732SDavid Greenman 	 * Issue software reset
11803ba65732SDavid Greenman 	 */
1181ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1182a17c678eSDavid Greenman 	DELAY(10);
1183a17c678eSDavid Greenman 
11843ba65732SDavid Greenman 	/*
11853ba65732SDavid Greenman 	 * Release any xmit buffers.
11863ba65732SDavid Greenman 	 */
1187da91462dSDavid Greenman 	txp = sc->cbl_base;
1188da91462dSDavid Greenman 	if (txp != NULL) {
1189da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1190da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1191da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1192da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1193da91462dSDavid Greenman 			}
1194da91462dSDavid Greenman 		}
11953ba65732SDavid Greenman 	}
11963ba65732SDavid Greenman 	sc->tx_queued = 0;
11973ba65732SDavid Greenman 
11983ba65732SDavid Greenman 	/*
11993ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
12003ba65732SDavid Greenman 	 */
12013ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
12023ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
12033ba65732SDavid Greenman 	sc->rfa_headm = NULL;
12043ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
12053ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
12063ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
12073ba65732SDavid Greenman 			/*
12083ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
12093ba65732SDavid Greenman 			 * and we just freed all the buffers we need
12103ba65732SDavid Greenman 			 * above.
12113ba65732SDavid Greenman 			 */
12123ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
12133ba65732SDavid Greenman 		}
12143ba65732SDavid Greenman 	}
1215a17c678eSDavid Greenman }
1216a17c678eSDavid Greenman 
1217a17c678eSDavid Greenman /*
1218a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1219a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1220a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1221a17c678eSDavid Greenman  * card has wedged for some reason.
1222a17c678eSDavid Greenman  */
1223a17c678eSDavid Greenman static void
1224f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1225a17c678eSDavid Greenman {
1226ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1227ba8c6fd5SDavid Greenman 
1228f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
12294a5f1499SDavid Greenman 	ifp->if_oerrors++;
1230a17c678eSDavid Greenman 
1231ba8c6fd5SDavid Greenman 	fxp_init(sc);
1232a17c678eSDavid Greenman }
1233a17c678eSDavid Greenman 
1234a17c678eSDavid Greenman static void
1235f7788e8eSJonathan Lemon fxp_init(void *xsc)
1236a17c678eSDavid Greenman {
1237fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1238ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1239a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1240a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1241a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1242f7788e8eSJonathan Lemon 	int i, prm, s;
1243a17c678eSDavid Greenman 
1244f7788e8eSJonathan Lemon 	s = splimp();
1245a17c678eSDavid Greenman 	/*
12463ba65732SDavid Greenman 	 * Cancel any pending I/O
1247a17c678eSDavid Greenman 	 */
12483ba65732SDavid Greenman 	fxp_stop(sc);
1249a17c678eSDavid Greenman 
1250a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1251a17c678eSDavid Greenman 
1252a17c678eSDavid Greenman 	/*
1253a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1254a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1255a17c678eSDavid Greenman 	 */
1256ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1257ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1258a17c678eSDavid Greenman 
1259ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1260ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1261a17c678eSDavid Greenman 
1262a17c678eSDavid Greenman 	/*
1263a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1264a17c678eSDavid Greenman 	 */
1265ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1266ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
1267ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1268a17c678eSDavid Greenman 
1269a17c678eSDavid Greenman 	/*
1270a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1271a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1272a17c678eSDavid Greenman 	 * later.
1273a17c678eSDavid Greenman 	 */
1274a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1275a17c678eSDavid Greenman 
1276a17c678eSDavid Greenman 	/*
1277a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1278a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1279a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1280a17c678eSDavid Greenman 	 */
1281d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1282d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1283397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1284a17c678eSDavid Greenman 
1285a17c678eSDavid Greenman 	cbp->cb_status =	0;
1286a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1287a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1288a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1289001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1290001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1291a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1292f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1293f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1294f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1295f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1296001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1297001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1298f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1299a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1300f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1301f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
13023114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1303f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1304f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1305f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
1306a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1307a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1308f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1309f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1310f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1311f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1312f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1313f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1314f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1315f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1316f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1317f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1318a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1319a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1320a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1321a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1322a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1323a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1324a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1325a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1326f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1327f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1328f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1329f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1330f7788e8eSJonathan Lemon 
1331f7788e8eSJonathan Lemon 	/*
1332f7788e8eSJonathan Lemon 	 * we may want to move all FC stuff to a separate section.
1333f7788e8eSJonathan Lemon 	 * the values here are 82557 compatible.
1334f7788e8eSJonathan Lemon 	 */
1335f7788e8eSJonathan Lemon 	cbp->fc_delay_lsb =	0;
1336f7788e8eSJonathan Lemon 	cbp->fc_delay_msb =	0x40;
1337f7788e8eSJonathan Lemon 	cbp->pri_fc_thresh =	0x03;
1338f7788e8eSJonathan Lemon 	cbp->tx_fc_dis =	0;	/* (don't) disable transmit FC */
1339f7788e8eSJonathan Lemon 	cbp->rx_fc_restop =	0;	/* (don't) enable FC stop frame */
1340f7788e8eSJonathan Lemon 	cbp->rx_fc_restart =	0;	/* (don't) enable FC start frame */
1341f7788e8eSJonathan Lemon 	cbp->fc_filter =	0;	/* (do) pass FC frames to host */
1342f7788e8eSJonathan Lemon 	cbp->pri_fc_loc =	1;	/* location of priority in FC frame */
1343f7788e8eSJonathan Lemon 
1344a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1345a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1346a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1347f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1348f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1349f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1350f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1351a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
13523ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1353a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1354f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1355a17c678eSDavid Greenman 
1356a17c678eSDavid Greenman 	/*
1357a17c678eSDavid Greenman 	 * Start the config command/DMA.
1358a17c678eSDavid Greenman 	 */
1359ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1360397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
1361ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1362a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
13637dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1364a17c678eSDavid Greenman 
1365a17c678eSDavid Greenman 	/*
1366a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1367a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1368a17c678eSDavid Greenman 	 */
1369a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1370a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1371a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1372a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1373d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1374d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1375a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1376a17c678eSDavid Greenman 
1377a17c678eSDavid Greenman 	/*
1378a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1379a17c678eSDavid Greenman 	 */
1380ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1381ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1382a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
13837dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1384a17c678eSDavid Greenman 
1385a17c678eSDavid Greenman 	/*
1386a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1387a17c678eSDavid Greenman 	 */
1388a17c678eSDavid Greenman 
1389a17c678eSDavid Greenman 	txp = sc->cbl_base;
1390a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1391a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1392a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1393a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
1394397f9dfeSDavid Greenman 		txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
1395a17c678eSDavid Greenman 		txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1396a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1397a17c678eSDavid Greenman 	}
1398a17c678eSDavid Greenman 	/*
1399397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1400a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1401a17c678eSDavid Greenman 	 */
1402a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1403a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1404397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1405a17c678eSDavid Greenman 
1406ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1407ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1408a17c678eSDavid Greenman 
1409a17c678eSDavid Greenman 	/*
1410a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1411a17c678eSDavid Greenman 	 */
1412ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1413ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1414ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
1415ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1416a17c678eSDavid Greenman 
1417dccee1a1SDavid Greenman 	/*
1418ba8c6fd5SDavid Greenman 	 * Set current media.
1419dccee1a1SDavid Greenman 	 */
1420f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1421f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1422dccee1a1SDavid Greenman 
1423a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1424a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1425f7788e8eSJonathan Lemon 	splx(s);
1426a17c678eSDavid Greenman 
1427a17c678eSDavid Greenman 	/*
1428a17c678eSDavid Greenman 	 * Start stats updater.
1429a17c678eSDavid Greenman 	 */
1430f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1431f7788e8eSJonathan Lemon }
1432f7788e8eSJonathan Lemon 
1433f7788e8eSJonathan Lemon static int
1434f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1435f7788e8eSJonathan Lemon {
1436f7788e8eSJonathan Lemon 
1437f7788e8eSJonathan Lemon 	return (0);
1438a17c678eSDavid Greenman }
1439a17c678eSDavid Greenman 
1440303b270bSEivind Eklund static void
1441f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1442ba8c6fd5SDavid Greenman {
1443ba8c6fd5SDavid Greenman 
1444f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1445ba8c6fd5SDavid Greenman }
1446ba8c6fd5SDavid Greenman 
1447ba8c6fd5SDavid Greenman /*
1448ba8c6fd5SDavid Greenman  * Change media according to request.
1449ba8c6fd5SDavid Greenman  */
1450f7788e8eSJonathan Lemon static int
1451f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1452ba8c6fd5SDavid Greenman {
1453ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1454f7788e8eSJonathan Lemon 	struct mii_data *mii;
1455ba8c6fd5SDavid Greenman 
1456f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1457f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1458ba8c6fd5SDavid Greenman 	return (0);
1459ba8c6fd5SDavid Greenman }
1460ba8c6fd5SDavid Greenman 
1461ba8c6fd5SDavid Greenman /*
1462ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1463ba8c6fd5SDavid Greenman  */
1464f7788e8eSJonathan Lemon static void
1465f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1466ba8c6fd5SDavid Greenman {
1467ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1468f7788e8eSJonathan Lemon 	struct mii_data *mii;
1469ba8c6fd5SDavid Greenman 
1470f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1471f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1472f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1473f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
1474ba8c6fd5SDavid Greenman }
1475ba8c6fd5SDavid Greenman 
1476a17c678eSDavid Greenman /*
1477a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1478a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1479a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1480dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1481a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1482a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1483a17c678eSDavid Greenman  */
1484a17c678eSDavid Greenman static int
1485f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1486a17c678eSDavid Greenman {
1487ba8c6fd5SDavid Greenman 	u_int32_t v;
1488a17c678eSDavid Greenman 	struct mbuf *m;
1489a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1490a17c678eSDavid Greenman 
1491a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1492a17c678eSDavid Greenman 	if (m != NULL) {
1493a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1494a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1495a17c678eSDavid Greenman 			m_freem(m);
1496eadd5e3aSDavid Greenman 			if (oldm == NULL)
1497eadd5e3aSDavid Greenman 				return 1;
1498a17c678eSDavid Greenman 			m = oldm;
1499eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1500a17c678eSDavid Greenman 		}
1501a17c678eSDavid Greenman 	} else {
1502eadd5e3aSDavid Greenman 		if (oldm == NULL)
1503a17c678eSDavid Greenman 			return 1;
1504eadd5e3aSDavid Greenman 		m = oldm;
1505eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1506eadd5e3aSDavid Greenman 	}
1507ba8c6fd5SDavid Greenman 
1508ba8c6fd5SDavid Greenman 	/*
1509ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1510ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1511ba8c6fd5SDavid Greenman 	 */
1512ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1513ba8c6fd5SDavid Greenman 
1514eadd5e3aSDavid Greenman 	/*
1515eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1516eadd5e3aSDavid Greenman 	 * data start past it.
1517eadd5e3aSDavid Greenman 	 */
1518a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1519eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
15204fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1521eadd5e3aSDavid Greenman 
1522ba8c6fd5SDavid Greenman 	/*
1523ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1524ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1525ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1526ba8c6fd5SDavid Greenman 	 */
15274fc1dda9SAndrew Gallatin 
1528a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1529a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1530a17c678eSDavid Greenman 	rfa->actual_size = 0;
1531ba8c6fd5SDavid Greenman 
1532ba8c6fd5SDavid Greenman 	v = -1;
15334fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
15344fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1535ba8c6fd5SDavid Greenman 
1536dfe61cf1SDavid Greenman 	/*
1537dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1538dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1539dfe61cf1SDavid Greenman 	 */
1540a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1541ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1542ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1543a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1544ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
15454fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1546aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1547a17c678eSDavid Greenman 	} else {
1548a17c678eSDavid Greenman 		sc->rfa_headm = m;
1549a17c678eSDavid Greenman 	}
1550a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1551a17c678eSDavid Greenman 
1552dfe61cf1SDavid Greenman 	return (m == oldm);
1553a17c678eSDavid Greenman }
1554a17c678eSDavid Greenman 
15556ebc3153SDavid Greenman static volatile int
1556f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1557dccee1a1SDavid Greenman {
1558f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1559dccee1a1SDavid Greenman 	int count = 10000;
15606ebc3153SDavid Greenman 	int value;
1561dccee1a1SDavid Greenman 
1562ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1563ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1564dccee1a1SDavid Greenman 
1565ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1566ba8c6fd5SDavid Greenman 	    && count--)
15676ebc3153SDavid Greenman 		DELAY(10);
1568dccee1a1SDavid Greenman 
1569dccee1a1SDavid Greenman 	if (count <= 0)
1570f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1571dccee1a1SDavid Greenman 
15726ebc3153SDavid Greenman 	return (value & 0xffff);
1573dccee1a1SDavid Greenman }
1574dccee1a1SDavid Greenman 
1575dccee1a1SDavid Greenman static void
1576f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1577dccee1a1SDavid Greenman {
1578f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1579dccee1a1SDavid Greenman 	int count = 10000;
1580dccee1a1SDavid Greenman 
1581ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1582ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1583ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1584dccee1a1SDavid Greenman 
1585ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1586ba8c6fd5SDavid Greenman 	    count--)
15876ebc3153SDavid Greenman 		DELAY(10);
1588dccee1a1SDavid Greenman 
1589dccee1a1SDavid Greenman 	if (count <= 0)
1590f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1591dccee1a1SDavid Greenman }
1592dccee1a1SDavid Greenman 
1593dccee1a1SDavid Greenman static int
1594f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1595a17c678eSDavid Greenman {
15969b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1597a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1598f7788e8eSJonathan Lemon         struct mii_data *mii;
1599f7788e8eSJonathan Lemon 	int s, error = 0;
1600a17c678eSDavid Greenman 
1601f7788e8eSJonathan Lemon 	s = splimp();
1602a17c678eSDavid Greenman 
1603a17c678eSDavid Greenman 	switch (command) {
1604a17c678eSDavid Greenman 	case SIOCSIFADDR:
1605a17c678eSDavid Greenman 	case SIOCGIFADDR:
1606fb583156SDavid Greenman 	case SIOCSIFMTU:
1607fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1608a17c678eSDavid Greenman 		break;
1609a17c678eSDavid Greenman 
1610a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1611f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1612f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1613f7788e8eSJonathan Lemon 		else
1614f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1615a17c678eSDavid Greenman 
1616a17c678eSDavid Greenman 		/*
1617a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1618a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1619a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1620a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1621a17c678eSDavid Greenman 		 */
1622a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1623fb583156SDavid Greenman 			fxp_init(sc);
1624a17c678eSDavid Greenman 		} else {
1625a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
16264a5f1499SDavid Greenman 				fxp_stop(sc);
1627a17c678eSDavid Greenman 		}
1628a17c678eSDavid Greenman 		break;
1629a17c678eSDavid Greenman 
1630a17c678eSDavid Greenman 	case SIOCADDMULTI:
1631a17c678eSDavid Greenman 	case SIOCDELMULTI:
1632f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1633f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1634f7788e8eSJonathan Lemon 		else
1635f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1636a17c678eSDavid Greenman 		/*
1637a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1638a17c678eSDavid Greenman 		 * accordingly.
1639a17c678eSDavid Greenman 		 */
1640f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1641397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1642397f9dfeSDavid Greenman 		/*
1643f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1644397f9dfeSDavid Greenman 		 * again rather than else {}.
1645397f9dfeSDavid Greenman 		 */
1646f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1647fb583156SDavid Greenman 			fxp_init(sc);
1648a17c678eSDavid Greenman 		error = 0;
1649ba8c6fd5SDavid Greenman 		break;
1650ba8c6fd5SDavid Greenman 
1651ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1652ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1653f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
1654f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
1655f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
1656f7788e8eSJonathan Lemon                             &mii->mii_media, command);
1657f7788e8eSJonathan Lemon 		} else {
1658ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1659f7788e8eSJonathan Lemon 		}
1660a17c678eSDavid Greenman 		break;
1661a17c678eSDavid Greenman 
1662a17c678eSDavid Greenman 	default:
1663a17c678eSDavid Greenman 		error = EINVAL;
1664a17c678eSDavid Greenman 	}
1665f7788e8eSJonathan Lemon 	splx(s);
1666a17c678eSDavid Greenman 	return (error);
1667a17c678eSDavid Greenman }
1668397f9dfeSDavid Greenman 
1669397f9dfeSDavid Greenman /*
1670397f9dfeSDavid Greenman  * Program the multicast filter.
1671397f9dfeSDavid Greenman  *
1672397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1673397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
16743114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1675397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1676dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1677397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1678397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1679397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1680397f9dfeSDavid Greenman  *
1681397f9dfeSDavid Greenman  * This function must be called at splimp.
1682397f9dfeSDavid Greenman  */
1683397f9dfeSDavid Greenman static void
1684f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
1685397f9dfeSDavid Greenman {
1686397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1687397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1688397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1689397f9dfeSDavid Greenman 	int nmcasts;
16907dced78aSDavid Greenman 	int count;
1691397f9dfeSDavid Greenman 
16923114fdb4SDavid Greenman 	/*
16933114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
16943114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
16953114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
16963114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
16973114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
16983114fdb4SDavid Greenman 	 */
1699397f9dfeSDavid Greenman 	if (sc->tx_queued) {
17003114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
17013114fdb4SDavid Greenman 
17023114fdb4SDavid Greenman 		/*
17033114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
17043114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
17053114fdb4SDavid Greenman 		 */
17063114fdb4SDavid Greenman 		if (sc->need_mcsetup)
17073114fdb4SDavid Greenman 			return;
1708397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
17093114fdb4SDavid Greenman 
17103114fdb4SDavid Greenman 		/*
17113114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
17123114fdb4SDavid Greenman 		 * TX commands have been processed.
17133114fdb4SDavid Greenman 		 */
17143114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
17153114fdb4SDavid Greenman 		txp->mb_head = NULL;
17163114fdb4SDavid Greenman 		txp->cb_status = 0;
17173114fdb4SDavid Greenman 		txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
17183114fdb4SDavid Greenman 		/*
17193114fdb4SDavid Greenman 		 * Advance the end of list forward.
17203114fdb4SDavid Greenman 		 */
17213114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
17223114fdb4SDavid Greenman 		sc->cbl_last = txp;
17233114fdb4SDavid Greenman 		sc->tx_queued++;
17243114fdb4SDavid Greenman 		/*
17253114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
17263114fdb4SDavid Greenman 		 */
17273114fdb4SDavid Greenman 		fxp_scb_wait(sc);
17283114fdb4SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
17293114fdb4SDavid Greenman 		/*
17303114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
17313114fdb4SDavid Greenman 		 * card again.
17323114fdb4SDavid Greenman 		 */
17333114fdb4SDavid Greenman 		ifp->if_timer = 5;
17343114fdb4SDavid Greenman 
1735397f9dfeSDavid Greenman 		return;
1736397f9dfeSDavid Greenman 	}
1737397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
1738397f9dfeSDavid Greenman 
1739397f9dfeSDavid Greenman 	/*
1740397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
1741397f9dfeSDavid Greenman 	 */
1742397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
1743397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
1744397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
17453114fdb4SDavid Greenman 	mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
1746397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
1747397f9dfeSDavid Greenman 
1748397f9dfeSDavid Greenman 	nmcasts = 0;
1749f7788e8eSJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
1750f7788e8eSJonathan Lemon #if __FreeBSD_version < 500000
1751f7788e8eSJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1752f7788e8eSJonathan Lemon #else
17536817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1754f7788e8eSJonathan Lemon #endif
1755397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
1756397f9dfeSDavid Greenman 				continue;
1757397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
1758f7788e8eSJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
1759397f9dfeSDavid Greenman 				nmcasts = 0;
1760397f9dfeSDavid Greenman 				break;
1761397f9dfeSDavid Greenman 			}
1762397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1763d244b0e9SPeter Wemm 			    (void *)(uintptr_t)(volatile void *)
1764d244b0e9SPeter Wemm 				&sc->mcsp->mc_addr[nmcasts][0], 6);
1765397f9dfeSDavid Greenman 			nmcasts++;
1766397f9dfeSDavid Greenman 		}
1767397f9dfeSDavid Greenman 	}
1768397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
1769397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
1770397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1771397f9dfeSDavid Greenman 
1772397f9dfeSDavid Greenman 	/*
1773397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
1774397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
1775397f9dfeSDavid Greenman 	 */
17767dced78aSDavid Greenman 	count = 100;
1777397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
17787dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
17797dced78aSDavid Greenman 		DELAY(10);
17807dced78aSDavid Greenman 	if (count == 0) {
1781f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
17827dced78aSDavid Greenman 		return;
17837dced78aSDavid Greenman 	}
1784397f9dfeSDavid Greenman 
1785397f9dfeSDavid Greenman 	/*
1786397f9dfeSDavid Greenman 	 * Start the multicast setup command.
1787397f9dfeSDavid Greenman 	 */
1788397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
1789397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
1790397f9dfeSDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1791397f9dfeSDavid Greenman 
17923114fdb4SDavid Greenman 	ifp->if_timer = 2;
1793397f9dfeSDavid Greenman 	return;
1794397f9dfeSDavid Greenman }
1795