xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 11457bbf08e696605e2e2d979975b3aaddcc2698)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28c3aac50fSPeter Wemm  * $FreeBSD$
29a17c678eSDavid Greenman  */
30a17c678eSDavid Greenman 
31a17c678eSDavid Greenman /*
32ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
33a17c678eSDavid Greenman  */
34a17c678eSDavid Greenman 
35e8c8b728SJonathan Lemon #include "vlan.h"
36e8c8b728SJonathan Lemon 
37a17c678eSDavid Greenman #include <sys/param.h>
38a17c678eSDavid Greenman #include <sys/systm.h>
39a17c678eSDavid Greenman #include <sys/mbuf.h>
40a17c678eSDavid Greenman #include <sys/malloc.h>
41f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
42a17c678eSDavid Greenman #include <sys/kernel.h>
434458ac71SBruce Evans #include <sys/socket.h>
44a17c678eSDavid Greenman 
45a17c678eSDavid Greenman #include <net/if.h>
46397f9dfeSDavid Greenman #include <net/if_dl.h>
47ba8c6fd5SDavid Greenman #include <net/if_media.h>
48a17c678eSDavid Greenman 
49a17c678eSDavid Greenman #ifdef NS
50a17c678eSDavid Greenman #include <netns/ns.h>
51a17c678eSDavid Greenman #include <netns/ns_if.h>
52a17c678eSDavid Greenman #endif
53a17c678eSDavid Greenman 
54a17c678eSDavid Greenman #include <net/bpf.h>
55ba8c6fd5SDavid Greenman #include <sys/sockio.h>
566182fdbdSPeter Wemm #include <sys/bus.h>
576182fdbdSPeter Wemm #include <machine/bus.h>
586182fdbdSPeter Wemm #include <sys/rman.h>
596182fdbdSPeter Wemm #include <machine/resource.h>
60ba8c6fd5SDavid Greenman 
611d5e9e22SEivind Eklund #include <net/ethernet.h>
621d5e9e22SEivind Eklund #include <net/if_arp.h>
63ba8c6fd5SDavid Greenman 
64dfe61cf1SDavid Greenman #include <vm/vm.h>		/* for vtophys */
65efeaf95aSDavid Greenman #include <vm/pmap.h>		/* for vtophys */
66f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
67a17c678eSDavid Greenman 
68e8c8b728SJonathan Lemon #if NVLAN > 0
69e8c8b728SJonathan Lemon #include <net/if_types.h>
70e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
71e8c8b728SJonathan Lemon #endif
72e8c8b728SJonathan Lemon 
73a17c678eSDavid Greenman #include <pci/pcivar.h>
74df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
75a17c678eSDavid Greenman 
76f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
77f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
78f7788e8eSJonathan Lemon 
79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
81f7788e8eSJonathan Lemon 
82f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
83f7788e8eSJonathan Lemon #include "miibus_if.h"
844fc1dda9SAndrew Gallatin 
85ba8c6fd5SDavid Greenman /*
86ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
87ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
88ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
89ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
90ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
91ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
92ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
93ba8c6fd5SDavid Greenman  */
94ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
95ba8c6fd5SDavid Greenman 
96ba8c6fd5SDavid Greenman /*
97f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
98f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
99f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
100f7788e8eSJonathan Lemon  */
101f7788e8eSJonathan Lemon static int tx_threshold = 64;
102f7788e8eSJonathan Lemon 
103f7788e8eSJonathan Lemon /*
104f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
105f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
106f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
107f7788e8eSJonathan Lemon  * to fxp_init.
108f7788e8eSJonathan Lemon  *
109f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
110f7788e8eSJonathan Lemon  */
111f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
112f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
113f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
114f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
115f7788e8eSJonathan Lemon 	0x0,	/*  0 */
116f7788e8eSJonathan Lemon 	0x0,	/*  1 */
117f7788e8eSJonathan Lemon 	0x0,	/*  2 */
118f7788e8eSJonathan Lemon 	0x0,	/*  3 */
119f7788e8eSJonathan Lemon 	0x0,	/*  4 */
120f7788e8eSJonathan Lemon 	0x0,	/*  5 */
121f7788e8eSJonathan Lemon 	0x32,	/*  6 */
122f7788e8eSJonathan Lemon 	0x0,	/*  7 */
123f7788e8eSJonathan Lemon 	0x0,	/*  8 */
124f7788e8eSJonathan Lemon 	0x0,	/*  9 */
125f7788e8eSJonathan Lemon 	0x6,	/* 10 */
126f7788e8eSJonathan Lemon 	0x0,	/* 11 */
127f7788e8eSJonathan Lemon 	0x0,	/* 12 */
128f7788e8eSJonathan Lemon 	0x0,	/* 13 */
129f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
130f7788e8eSJonathan Lemon 	0x48,	/* 15 */
131f7788e8eSJonathan Lemon 	0x0,	/* 16 */
132f7788e8eSJonathan Lemon 	0x40,	/* 17 */
133f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
134f7788e8eSJonathan Lemon 	0x0,	/* 19 */
135f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
136f7788e8eSJonathan Lemon 	0x5	/* 21 */
137f7788e8eSJonathan Lemon };
138f7788e8eSJonathan Lemon 
139f7788e8eSJonathan Lemon struct fxp_ident {
140f7788e8eSJonathan Lemon 	u_int16_t	devid;
141f7788e8eSJonathan Lemon 	char 		*name;
142f7788e8eSJonathan Lemon };
143f7788e8eSJonathan Lemon 
144f7788e8eSJonathan Lemon /*
145f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
146f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
147f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
148f7788e8eSJonathan Lemon  * them.
149f7788e8eSJonathan Lemon  */
150f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
151f7788e8eSJonathan Lemon     { 0x1229,		"Intel Pro 10/100B/100+ Ethernet" },
152f7788e8eSJonathan Lemon     { 0x2449,		"Intel Pro/100 Ethernet" },
153f7788e8eSJonathan Lemon     { 0x1209,		"Intel Embedded 10/100 Ethernet" },
154f7788e8eSJonathan Lemon     { 0x1029,		"Intel Pro/100 Ethernet" },
155f7788e8eSJonathan Lemon     { 0x1030,		"Intel Pro/100 Ethernet" },
156f7788e8eSJonathan Lemon     { 0x1031,		"Intel Pro/100 Ethernet" },
157f7788e8eSJonathan Lemon     { 0x1032,		"Intel Pro/100 Ethernet" },
158f7788e8eSJonathan Lemon     { 0x1033,		"Intel Pro/100 Ethernet" },
159f7788e8eSJonathan Lemon     { 0x1034,		"Intel Pro/100 Ethernet" },
160f7788e8eSJonathan Lemon     { 0x1035,		"Intel Pro/100 Ethernet" },
161f7788e8eSJonathan Lemon     { 0x1036,		"Intel Pro/100 Ethernet" },
162f7788e8eSJonathan Lemon     { 0x1037,		"Intel Pro/100 Ethernet" },
163f7788e8eSJonathan Lemon     { 0x1038,		"Intel Pro/100 Ethernet" },
164f7788e8eSJonathan Lemon     { 0,		NULL },
165f7788e8eSJonathan Lemon };
166f7788e8eSJonathan Lemon 
167f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
168f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
169f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
170f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
171f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
172f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
173f7788e8eSJonathan Lemon 
174f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
175f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
176f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
177f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
178f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
179f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
180f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
181f7788e8eSJonathan Lemon 			    caddr_t data);
182f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
183f7788e8eSJonathan Lemon static int		fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
184f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
185f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
186f7788e8eSJonathan Lemon 			    int autosize);
187f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
188f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
189f7788e8eSJonathan Lemon 			    int offset, int words);
190f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
191f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
192f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
193f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
194f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
195f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
196f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
197f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
198f7788e8eSJonathan Lemon 			    int value);
199f7788e8eSJonathan Lemon static __inline void	fxp_lwcopy(volatile u_int32_t *src,
200f7788e8eSJonathan Lemon 			    volatile u_int32_t *dst);
201f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2022e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
203f7788e8eSJonathan Lemon static __inline void	fxp_dma_wait(volatile u_int16_t *status,
204f7788e8eSJonathan Lemon 			    struct fxp_softc *sc);
205f7788e8eSJonathan Lemon 
206f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
207f7788e8eSJonathan Lemon 	/* Device interface */
208f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
209f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
210f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
211f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
212f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
213f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
214f7788e8eSJonathan Lemon 
215f7788e8eSJonathan Lemon 	/* MII interface */
216f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
217f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
218f7788e8eSJonathan Lemon 
219f7788e8eSJonathan Lemon 	{ 0, 0 }
220f7788e8eSJonathan Lemon };
221f7788e8eSJonathan Lemon 
222f7788e8eSJonathan Lemon static driver_t fxp_driver = {
223f7788e8eSJonathan Lemon 	"fxp",
224f7788e8eSJonathan Lemon 	fxp_methods,
225f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
226f7788e8eSJonathan Lemon };
227f7788e8eSJonathan Lemon 
228f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
229f7788e8eSJonathan Lemon 
230f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
231f7788e8eSJonathan Lemon DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
232f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
233f7788e8eSJonathan Lemon 
234f7788e8eSJonathan Lemon /*
235ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
236ba8c6fd5SDavid Greenman  */
237ba8c6fd5SDavid Greenman static __inline void
238f7788e8eSJonathan Lemon fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
239ba8c6fd5SDavid Greenman {
240aed53495SDavid Greenman #ifdef __i386__
241aed53495SDavid Greenman 	*dst = *src;
242aed53495SDavid Greenman #else
243fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
244fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
245ba8c6fd5SDavid Greenman 
246ba8c6fd5SDavid Greenman 	b[0] = a[0];
247ba8c6fd5SDavid Greenman 	b[1] = a[1];
248aed53495SDavid Greenman #endif
249ba8c6fd5SDavid Greenman }
250a17c678eSDavid Greenman 
251a17c678eSDavid Greenman /*
252dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
253dfe61cf1SDavid Greenman  * completed).
254dfe61cf1SDavid Greenman  */
255c1087c13SBruce Evans static __inline void
256f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
257a17c678eSDavid Greenman {
258a17c678eSDavid Greenman 	int i = 10000;
259a17c678eSDavid Greenman 
2607dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2617dced78aSDavid Greenman 		DELAY(2);
2627dced78aSDavid Greenman 	if (i == 0)
263e8c8b728SJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x, 0x%x, 0x%x 0x%x\n",
264e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
265e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
266e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
267e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2687dced78aSDavid Greenman }
2697dced78aSDavid Greenman 
2707dced78aSDavid Greenman static __inline void
2712e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2722e2b8238SJonathan Lemon {
2732e2b8238SJonathan Lemon 
2742e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2752e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2762e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2772e2b8238SJonathan Lemon 	}
2782e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2792e2b8238SJonathan Lemon }
2802e2b8238SJonathan Lemon 
2812e2b8238SJonathan Lemon static __inline void
282f7788e8eSJonathan Lemon fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
2837dced78aSDavid Greenman {
2847dced78aSDavid Greenman 	int i = 10000;
2857dced78aSDavid Greenman 
2867dced78aSDavid Greenman 	while (!(*status & FXP_CB_STATUS_C) && --i)
2877dced78aSDavid Greenman 		DELAY(2);
2887dced78aSDavid Greenman 	if (i == 0)
289f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
290a17c678eSDavid Greenman }
291a17c678eSDavid Greenman 
292dfe61cf1SDavid Greenman /*
293dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
294dfe61cf1SDavid Greenman  */
2956182fdbdSPeter Wemm static int
2966182fdbdSPeter Wemm fxp_probe(device_t dev)
297a17c678eSDavid Greenman {
298f7788e8eSJonathan Lemon 	u_int16_t devid;
299f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
300f7788e8eSJonathan Lemon 
30155ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
302f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
303f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
304f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
305f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
306f7788e8eSJonathan Lemon 				return (0);
30755ce7b51SDavid Greenman 			}
308dd68ef16SPeter Wemm 		}
309f7788e8eSJonathan Lemon 	}
310f7788e8eSJonathan Lemon 	return (ENXIO);
3116182fdbdSPeter Wemm }
3126182fdbdSPeter Wemm 
3136182fdbdSPeter Wemm static int
3146182fdbdSPeter Wemm fxp_attach(device_t dev)
315a17c678eSDavid Greenman {
3166182fdbdSPeter Wemm 	int error = 0;
3176182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
318ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
3199fa6ccfbSMatt Jacob 	u_int32_t val;
320f7788e8eSJonathan Lemon 	u_int16_t data;
321f7788e8eSJonathan Lemon 	int i, rid, m1, m2, prefer_iomap;
322f7788e8eSJonathan Lemon 	int s;
323a17c678eSDavid Greenman 
324f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
325f7788e8eSJonathan Lemon 	sc->dev = dev;
3266c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
327f7788e8eSJonathan Lemon 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
328a17c678eSDavid Greenman 
329f7788e8eSJonathan Lemon 	s = splimp();
330a17c678eSDavid Greenman 
331dfe61cf1SDavid Greenman 	/*
3329fa6ccfbSMatt Jacob 	 * Enable bus mastering. Enable memory space too, in case
3339fa6ccfbSMatt Jacob 	 * BIOS/Prom forgot about it.
334df373873SWes Peters 	 */
3356182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
336df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
3376182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
3389fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
339df373873SWes Peters 
340f7788e8eSJonathan Lemon #if __FreeBSD_version >= 500000
3418d799694SBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
3428d799694SBill Paul 		u_int32_t		iobase, membase, irq;
3438d799694SBill Paul 
3448d799694SBill Paul 		/* Save important PCI config data. */
3458d799694SBill Paul 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
3468d799694SBill Paul 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
3478d799694SBill Paul 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
3488d799694SBill Paul 
3498d799694SBill Paul 		/* Reset the power state. */
3508d799694SBill Paul 		device_printf(dev, "chip is in D%d power mode "
3518d799694SBill Paul 		    "-- setting to D0\n", pci_get_powerstate(dev));
3528d799694SBill Paul 
3538d799694SBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
3548d799694SBill Paul 
3558d799694SBill Paul 		/* Restore PCI config data. */
3568d799694SBill Paul 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
3578d799694SBill Paul 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
3588d799694SBill Paul 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
3598d799694SBill Paul 	}
360f7788e8eSJonathan Lemon #endif
3618d799694SBill Paul 
362df373873SWes Peters 	/*
3639fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3649fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3659fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
366dfe61cf1SDavid Greenman 	 */
3679fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3689fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3692a05a4ebSMatt Jacob 	prefer_iomap = 0;
3702a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3712a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
3729fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
3739fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
3749fa6ccfbSMatt Jacob 	}
3759fa6ccfbSMatt Jacob 
3769fa6ccfbSMatt Jacob 	if (val & m1) {
3779fa6ccfbSMatt Jacob 		sc->rtp =
3789fa6ccfbSMatt Jacob 		    (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3799fa6ccfbSMatt Jacob 		sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3809fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3816182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
3829fa6ccfbSMatt Jacob 	}
3839fa6ccfbSMatt Jacob 	if (sc->mem == NULL && (val & m2)) {
3849fa6ccfbSMatt Jacob 		sc->rtp =
3859fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
3869fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
3879fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
3889fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
3899fa6ccfbSMatt Jacob 	}
3909fa6ccfbSMatt Jacob 
3916182fdbdSPeter Wemm 	if (!sc->mem) {
3929fa6ccfbSMatt Jacob 		device_printf(dev, "could not map device registers\n");
3936182fdbdSPeter Wemm 		error = ENXIO;
394a17c678eSDavid Greenman 		goto fail;
395a17c678eSDavid Greenman         }
3969fa6ccfbSMatt Jacob 	if (bootverbose) {
3979fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
3989fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
3999fa6ccfbSMatt Jacob 	}
4004fc1dda9SAndrew Gallatin 
4014fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4024fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
403a17c678eSDavid Greenman 
404a17c678eSDavid Greenman 	/*
405dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
406dfe61cf1SDavid Greenman 	 */
4076182fdbdSPeter Wemm 	rid = 0;
4086182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4096182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4106182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4116182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4126182fdbdSPeter Wemm 		error = ENXIO;
4136182fdbdSPeter Wemm 		goto fail;
4146182fdbdSPeter Wemm 	}
4156182fdbdSPeter Wemm 
416566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
417566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
4186182fdbdSPeter Wemm 	if (error) {
4196182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
420a17c678eSDavid Greenman 		goto fail;
421a17c678eSDavid Greenman 	}
422a17c678eSDavid Greenman 
423f7788e8eSJonathan Lemon 	/*
424f7788e8eSJonathan Lemon 	 * Reset to a stable state.
425f7788e8eSJonathan Lemon 	 */
426f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
427f7788e8eSJonathan Lemon 	DELAY(10);
428f7788e8eSJonathan Lemon 
429f7788e8eSJonathan Lemon 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
430f7788e8eSJonathan Lemon 	    M_DEVBUF, M_NOWAIT | M_ZERO);
431f7788e8eSJonathan Lemon 	if (sc->cbl_base == NULL)
432f7788e8eSJonathan Lemon 		goto failmem;
433f7788e8eSJonathan Lemon 
434f7788e8eSJonathan Lemon 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF,
435f7788e8eSJonathan Lemon 	    M_NOWAIT | M_ZERO);
436f7788e8eSJonathan Lemon 	if (sc->fxp_stats == NULL)
437f7788e8eSJonathan Lemon 		goto failmem;
438f7788e8eSJonathan Lemon 
439f7788e8eSJonathan Lemon 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
440f7788e8eSJonathan Lemon 	if (sc->mcsp == NULL)
441f7788e8eSJonathan Lemon 		goto failmem;
442f7788e8eSJonathan Lemon 
443f7788e8eSJonathan Lemon 	/*
444f7788e8eSJonathan Lemon 	 * Pre-allocate our receive buffers.
445f7788e8eSJonathan Lemon 	 */
446f7788e8eSJonathan Lemon 	for (i = 0; i < FXP_NRFABUFS; i++) {
447f7788e8eSJonathan Lemon 		if (fxp_add_rfabuf(sc, NULL) != 0) {
448f7788e8eSJonathan Lemon 			goto failmem;
449f7788e8eSJonathan Lemon 		}
450f7788e8eSJonathan Lemon 	}
451f7788e8eSJonathan Lemon 
452f7788e8eSJonathan Lemon 	/*
453f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
454f7788e8eSJonathan Lemon 	 */
455f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
456f7788e8eSJonathan Lemon 
457f7788e8eSJonathan Lemon 	/*
4583bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
459f7788e8eSJonathan Lemon 	 */
460f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
461f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
462f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
463dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
464f7788e8eSJonathan Lemon 
465f7788e8eSJonathan Lemon 	/*
4663bd07cfdSJonathan Lemon 	 * Find out the basic controller type; we currently only
4673bd07cfdSJonathan Lemon 	 * differentiate between a 82557 and greater.
4683bd07cfdSJonathan Lemon 	 */
4693bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
4703bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
4713bd07cfdSJonathan Lemon 		sc->chip = FXP_CHIP_82557;
4723bd07cfdSJonathan Lemon 
4733bd07cfdSJonathan Lemon 	/*
4742e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
4752e2b8238SJonathan Lemon 	 */
4762e2b8238SJonathan Lemon 	i = pci_get_device(dev);
4772e2b8238SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039))
4782e2b8238SJonathan Lemon 		sc->flags |= FXP_FLAG_CU_RESUME_BUG;
4792e2b8238SJonathan Lemon 
4802e2b8238SJonathan Lemon 	/*
4813bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
4823bd07cfdSJonathan Lemon 	 */
4833bd07cfdSJonathan Lemon 	if (sc->chip != FXP_CHIP_82557) {
4843bd07cfdSJonathan Lemon 		/*
4853bd07cfdSJonathan Lemon 		 * If there is a valid cacheline size (8 or 16 dwords),
4863bd07cfdSJonathan Lemon 		 * then turn on MWI.
4873bd07cfdSJonathan Lemon 		 */
4883bd07cfdSJonathan Lemon 		if (pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
4893bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
4903bd07cfdSJonathan Lemon 
4913bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
4923bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
493e8c8b728SJonathan Lemon #if NVLAN > 0
494e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
495e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
496e8c8b728SJonathan Lemon #endif
4973bd07cfdSJonathan Lemon 	}
4983bd07cfdSJonathan Lemon 
4993bd07cfdSJonathan Lemon 	/*
500f7788e8eSJonathan Lemon 	 * Read MAC address.
501f7788e8eSJonathan Lemon 	 */
502f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
503f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
504f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
505f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
506f7788e8eSJonathan Lemon 	if (bootverbose) {
5072e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
508f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
5092e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
5102e2b8238SJonathan Lemon 		    pci_get_revid(dev));
511e8c8b728SJonathan Lemon 		device_printf(dev, "Chip Type: %d\n", sc->chip);
512f7788e8eSJonathan Lemon 	}
513f7788e8eSJonathan Lemon 
514f7788e8eSJonathan Lemon 	/*
515f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
516f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
517f7788e8eSJonathan Lemon 	 *
518f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
519f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
520f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
521f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
522f7788e8eSJonathan Lemon 	 */
523f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
524f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
525f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
526f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
527f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
528f7788e8eSJonathan Lemon 	} else {
529f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
530f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
531f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
5326182fdbdSPeter Wemm 			error = ENXIO;
533ba8c6fd5SDavid Greenman 			goto fail;
534a17c678eSDavid Greenman 		}
535f7788e8eSJonathan Lemon 	}
536dccee1a1SDavid Greenman 
537a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
5386182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
539a17c678eSDavid Greenman 	ifp->if_name = "fxp";
540a17c678eSDavid Greenman 	ifp->if_output = ether_output;
541a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
542fb583156SDavid Greenman 	ifp->if_init = fxp_init;
543ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
544ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
545ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
546ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
547ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
548a17c678eSDavid Greenman 
549dfe61cf1SDavid Greenman 	/*
550dfe61cf1SDavid Greenman 	 * Attach the interface.
551dfe61cf1SDavid Greenman 	 */
55221b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
553f7788e8eSJonathan Lemon 
554e8c8b728SJonathan Lemon #if NVLAN > 0
555e8c8b728SJonathan Lemon 	/*
556e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
557e8c8b728SJonathan Lemon 	 */
558e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
559e8c8b728SJonathan Lemon #endif
560e8c8b728SJonathan Lemon 
561483b9871SDavid Greenman 	/*
5623114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
5633114fdb4SDavid Greenman 	 * TX descriptors.
564483b9871SDavid Greenman 	 */
5653114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
5664a684684SDavid Greenman 
567f7788e8eSJonathan Lemon 	splx(s);
568f7788e8eSJonathan Lemon 	return (0);
569a17c678eSDavid Greenman 
570f7788e8eSJonathan Lemon failmem:
571f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
572f7788e8eSJonathan Lemon 	error = ENOMEM;
573a17c678eSDavid Greenman fail:
574f7788e8eSJonathan Lemon 	splx(s);
575f7788e8eSJonathan Lemon 	fxp_release(sc);
576f7788e8eSJonathan Lemon 	return (error);
577f7788e8eSJonathan Lemon }
578f7788e8eSJonathan Lemon 
579f7788e8eSJonathan Lemon /*
580f7788e8eSJonathan Lemon  * release all resources
581f7788e8eSJonathan Lemon  */
582f7788e8eSJonathan Lemon static void
583f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
584f7788e8eSJonathan Lemon {
585f7788e8eSJonathan Lemon 
586f7788e8eSJonathan Lemon 	bus_generic_detach(sc->dev);
5873bd07cfdSJonathan Lemon 	if (sc->miibus)
588f7788e8eSJonathan Lemon 		device_delete_child(sc->dev, sc->miibus);
589f7788e8eSJonathan Lemon 
590f7788e8eSJonathan Lemon 	if (sc->cbl_base)
591f7788e8eSJonathan Lemon 		free(sc->cbl_base, M_DEVBUF);
592f7788e8eSJonathan Lemon 	if (sc->fxp_stats)
593f7788e8eSJonathan Lemon 		free(sc->fxp_stats, M_DEVBUF);
594f7788e8eSJonathan Lemon 	if (sc->mcsp)
595f7788e8eSJonathan Lemon 		free(sc->mcsp, M_DEVBUF);
596f7788e8eSJonathan Lemon 	if (sc->rfa_headm)
597f7788e8eSJonathan Lemon 		m_freem(sc->rfa_headm);
598f7788e8eSJonathan Lemon 
599f7788e8eSJonathan Lemon 	if (sc->ih)
600f7788e8eSJonathan Lemon 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
601f7788e8eSJonathan Lemon 	if (sc->irq)
602f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
603f7788e8eSJonathan Lemon 	if (sc->mem)
604f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
6050f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
6066182fdbdSPeter Wemm }
6076182fdbdSPeter Wemm 
6086182fdbdSPeter Wemm /*
6096182fdbdSPeter Wemm  * Detach interface.
6106182fdbdSPeter Wemm  */
6116182fdbdSPeter Wemm static int
6126182fdbdSPeter Wemm fxp_detach(device_t dev)
6136182fdbdSPeter Wemm {
6146182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
615f7788e8eSJonathan Lemon 	int s;
6166182fdbdSPeter Wemm 
6172e2b8238SJonathan Lemon 	/* disable interrupts */
6182e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
6192e2b8238SJonathan Lemon 
620f7788e8eSJonathan Lemon 	s = splimp();
6216182fdbdSPeter Wemm 
6226182fdbdSPeter Wemm 	/*
6236182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
6246182fdbdSPeter Wemm 	 */
6256182fdbdSPeter Wemm 	fxp_stop(sc);
6266182fdbdSPeter Wemm 
6276182fdbdSPeter Wemm 	/*
628f7788e8eSJonathan Lemon 	 * Close down routes etc.
6296182fdbdSPeter Wemm 	 */
630f7788e8eSJonathan Lemon 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
6316182fdbdSPeter Wemm 
6326182fdbdSPeter Wemm 	/*
6336182fdbdSPeter Wemm 	 * Free all media structures.
6346182fdbdSPeter Wemm 	 */
6356182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
6366182fdbdSPeter Wemm 
637f7788e8eSJonathan Lemon 	splx(s);
6386182fdbdSPeter Wemm 
639f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
640f7788e8eSJonathan Lemon 	fxp_release(sc);
6416182fdbdSPeter Wemm 
642f7788e8eSJonathan Lemon 	return (0);
643a17c678eSDavid Greenman }
644a17c678eSDavid Greenman 
645a17c678eSDavid Greenman /*
6464a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
647a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
648a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
649a17c678eSDavid Greenman  */
6506182fdbdSPeter Wemm static int
6516182fdbdSPeter Wemm fxp_shutdown(device_t dev)
652a17c678eSDavid Greenman {
6536182fdbdSPeter Wemm 	/*
6546182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
6556182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
6566182fdbdSPeter Wemm 	 * reboot before the driver initializes.
6576182fdbdSPeter Wemm 	 */
6586182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
659f7788e8eSJonathan Lemon 	return (0);
660a17c678eSDavid Greenman }
661a17c678eSDavid Greenman 
6627dced78aSDavid Greenman /*
6637dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
6647dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
6657dced78aSDavid Greenman  * resume.
6667dced78aSDavid Greenman  */
6677dced78aSDavid Greenman static int
6687dced78aSDavid Greenman fxp_suspend(device_t dev)
6697dced78aSDavid Greenman {
6707dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
671f7788e8eSJonathan Lemon 	int i, s;
6727dced78aSDavid Greenman 
673f7788e8eSJonathan Lemon 	s = splimp();
6747dced78aSDavid Greenman 
6757dced78aSDavid Greenman 	fxp_stop(sc);
6767dced78aSDavid Greenman 
6777dced78aSDavid Greenman 	for (i=0; i<5; i++)
6787dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i*4, 4);
6797dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
6807dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
6817dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
6827dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
6837dced78aSDavid Greenman 
6847dced78aSDavid Greenman 	sc->suspended = 1;
6857dced78aSDavid Greenman 
686f7788e8eSJonathan Lemon 	splx(s);
687f7788e8eSJonathan Lemon 	return (0);
6887dced78aSDavid Greenman }
6897dced78aSDavid Greenman 
6907dced78aSDavid Greenman /*
6917dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
6927dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
6937dced78aSDavid Greenman  * appropriate.
6947dced78aSDavid Greenman  */
6957dced78aSDavid Greenman static int
6967dced78aSDavid Greenman fxp_resume(device_t dev)
6977dced78aSDavid Greenman {
6987dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
6997dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
7007dced78aSDavid Greenman 	u_int16_t pci_command;
701f7788e8eSJonathan Lemon 	int i, s;
7027dced78aSDavid Greenman 
703f7788e8eSJonathan Lemon 	s = splimp();
7047dced78aSDavid Greenman 
7057dced78aSDavid Greenman 	/* better way to do this? */
7067dced78aSDavid Greenman 	for (i=0; i<5; i++)
7077dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i*4, sc->saved_maps[i], 4);
7087dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
7097dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
7107dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
7117dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
7127dced78aSDavid Greenman 
7137dced78aSDavid Greenman 	/* reenable busmastering */
7147dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
7157dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
7167dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
7177dced78aSDavid Greenman 
7187dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
7197dced78aSDavid Greenman 	DELAY(10);
7207dced78aSDavid Greenman 
7217dced78aSDavid Greenman 	/* reinitialize interface if necessary */
7227dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
7237dced78aSDavid Greenman 		fxp_init(sc);
7247dced78aSDavid Greenman 
7257dced78aSDavid Greenman 	sc->suspended = 0;
7267dced78aSDavid Greenman 
727f7788e8eSJonathan Lemon 	splx(s);
728ba8c6fd5SDavid Greenman 	return (0);
729f7788e8eSJonathan Lemon }
730ba8c6fd5SDavid Greenman 
731f7788e8eSJonathan Lemon /*
732f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
733f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
734f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
735f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
736f7788e8eSJonathan Lemon  * every 16 bits of data.
737f7788e8eSJonathan Lemon  */
738f7788e8eSJonathan Lemon static u_int16_t
739f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
740f7788e8eSJonathan Lemon {
741f7788e8eSJonathan Lemon 	u_int16_t reg, data;
742f7788e8eSJonathan Lemon 	int x;
743ba8c6fd5SDavid Greenman 
744f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
745f7788e8eSJonathan Lemon 	/*
746f7788e8eSJonathan Lemon 	 * Shift in read opcode.
747f7788e8eSJonathan Lemon 	 */
748f7788e8eSJonathan Lemon 	for (x = 1 << 2; x; x >>= 1) {
749f7788e8eSJonathan Lemon 		if (FXP_EEPROM_OPC_READ & x)
750f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
751f7788e8eSJonathan Lemon 		else
752f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
753f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
754f7788e8eSJonathan Lemon 		DELAY(1);
755f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
756f7788e8eSJonathan Lemon 		DELAY(1);
757f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
758f7788e8eSJonathan Lemon 		DELAY(1);
759f7788e8eSJonathan Lemon 	}
760f7788e8eSJonathan Lemon 	/*
761f7788e8eSJonathan Lemon 	 * Shift in address.
762f7788e8eSJonathan Lemon 	 */
763f7788e8eSJonathan Lemon 	data = 0;
764f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
765f7788e8eSJonathan Lemon 		if (offset & x)
766f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
767f7788e8eSJonathan Lemon 		else
768f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
769f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
770f7788e8eSJonathan Lemon 		DELAY(1);
771f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
772f7788e8eSJonathan Lemon 		DELAY(1);
773f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
774f7788e8eSJonathan Lemon 		DELAY(1);
775f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
776f7788e8eSJonathan Lemon 		data++;
777f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
778f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
779f7788e8eSJonathan Lemon 			break;
780f7788e8eSJonathan Lemon 		}
781f7788e8eSJonathan Lemon 	}
782f7788e8eSJonathan Lemon 	/*
783f7788e8eSJonathan Lemon 	 * Shift out data.
784f7788e8eSJonathan Lemon 	 */
785f7788e8eSJonathan Lemon 	data = 0;
786f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
787f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
788f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
789f7788e8eSJonathan Lemon 		DELAY(1);
790f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
791f7788e8eSJonathan Lemon 			data |= x;
792f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
793f7788e8eSJonathan Lemon 		DELAY(1);
794f7788e8eSJonathan Lemon 	}
795f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
796f7788e8eSJonathan Lemon 	DELAY(1);
797f7788e8eSJonathan Lemon 
798f7788e8eSJonathan Lemon 	return (data);
799ba8c6fd5SDavid Greenman }
800ba8c6fd5SDavid Greenman 
801ba8c6fd5SDavid Greenman /*
802e9bf2fa7SDavid Greenman  * From NetBSD:
803e9bf2fa7SDavid Greenman  *
804e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
805e9bf2fa7SDavid Greenman  *
806e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
807e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
808e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
809e9bf2fa7SDavid Greenman  *
810e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
811e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
812e9bf2fa7SDavid Greenman  *
813e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
814e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
815e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
816e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
817e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
818e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
819e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
820e9bf2fa7SDavid Greenman  *
821e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
822e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
823e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
824e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
825e9bf2fa7SDavid Greenman  *
826e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
827e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
828e9bf2fa7SDavid Greenman  */
829e9bf2fa7SDavid Greenman static void
830f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
831e9bf2fa7SDavid Greenman {
832e9bf2fa7SDavid Greenman 
833f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
834f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
835f7788e8eSJonathan Lemon 
836f7788e8eSJonathan Lemon 	/* autosize */
837f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
838e9bf2fa7SDavid Greenman }
839f7788e8eSJonathan Lemon 
840ba8c6fd5SDavid Greenman static void
841f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
842ba8c6fd5SDavid Greenman {
843f7788e8eSJonathan Lemon 	int i;
844ba8c6fd5SDavid Greenman 
845f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
846f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
847ba8c6fd5SDavid Greenman }
848ba8c6fd5SDavid Greenman 
849a17c678eSDavid Greenman /*
850a17c678eSDavid Greenman  * Start packet transmission on the interface.
851a17c678eSDavid Greenman  */
852a17c678eSDavid Greenman static void
853f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
854a17c678eSDavid Greenman {
8559b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
856a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
857a17c678eSDavid Greenman 
858a17c678eSDavid Greenman 	/*
859483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
860483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
861483b9871SDavid Greenman 	 * of the command chain).
862a17c678eSDavid Greenman 	 */
8630f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
864a17c678eSDavid Greenman 		return;
8650f4dc94cSChuck Paterson 	}
8661cd443acSDavid Greenman 
867483b9871SDavid Greenman 	txp = NULL;
868483b9871SDavid Greenman 
869483b9871SDavid Greenman 	/*
870483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
871483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
8723114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
8733114fdb4SDavid Greenman 	 *       a NOP command when needed.
874483b9871SDavid Greenman 	 */
8753114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
876483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
877483b9871SDavid Greenman 		int segment;
878483b9871SDavid Greenman 
879dfe61cf1SDavid Greenman 		/*
880dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
881dfe61cf1SDavid Greenman 		 */
8826318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
883a17c678eSDavid Greenman 
884dfe61cf1SDavid Greenman 		/*
885483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
886dfe61cf1SDavid Greenman 		 */
887a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
888a17c678eSDavid Greenman 
889a17c678eSDavid Greenman 		/*
890a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
891483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
892a17c678eSDavid Greenman 		 * and size of the mbuf.
893a17c678eSDavid Greenman 		 */
89423a0ed7cSDavid Greenman tbdinit:
895a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
896a17c678eSDavid Greenman 			if (m->m_len != 0) {
897a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
898a17c678eSDavid Greenman 					break;
899a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
900a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
901a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
902a17c678eSDavid Greenman 				segment++;
903a17c678eSDavid Greenman 			}
904a17c678eSDavid Greenman 		}
905fb583156SDavid Greenman 		if (m != NULL) {
90623a0ed7cSDavid Greenman 			struct mbuf *mn;
90723a0ed7cSDavid Greenman 
908a17c678eSDavid Greenman 			/*
9093bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
9103bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
9113bd07cfdSJonathan Lemon 			 * new buffers.
912a17c678eSDavid Greenman 			 */
91323a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
91423a0ed7cSDavid Greenman 			if (mn == NULL) {
91523a0ed7cSDavid Greenman 				m_freem(mb_head);
916483b9871SDavid Greenman 				break;
917a17c678eSDavid Greenman 			}
91823a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
91923a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
92023a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
92123a0ed7cSDavid Greenman 					m_freem(mn);
92223a0ed7cSDavid Greenman 					m_freem(mb_head);
923483b9871SDavid Greenman 					break;
92423a0ed7cSDavid Greenman 				}
92523a0ed7cSDavid Greenman 			}
926ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
927ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
92823a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
92923a0ed7cSDavid Greenman 			m_freem(mb_head);
93023a0ed7cSDavid Greenman 			mb_head = mn;
93123a0ed7cSDavid Greenman 			goto tbdinit;
93223a0ed7cSDavid Greenman 		}
93323a0ed7cSDavid Greenman 
93423a0ed7cSDavid Greenman 		txp->tbd_number = segment;
9351cd443acSDavid Greenman 		txp->mb_head = mb_head;
936a17c678eSDavid Greenman 		txp->cb_status = 0;
9373114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
938a17c678eSDavid Greenman 			txp->cb_command =
9393bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
9403bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S;
9413114fdb4SDavid Greenman 		} else {
9423114fdb4SDavid Greenman 			txp->cb_command =
9433bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
9443bd07cfdSJonathan Lemon 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
9453114fdb4SDavid Greenman 			/*
9463bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
9473bd07cfdSJonathan Lemon 			 * from the card again.
9483114fdb4SDavid Greenman 			 */
9493114fdb4SDavid Greenman 			ifp->if_timer = 5;
9503114fdb4SDavid Greenman 		}
951f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
952a17c678eSDavid Greenman 
953a17c678eSDavid Greenman 		/*
954483b9871SDavid Greenman 		 * Advance the end of list forward.
955a17c678eSDavid Greenman 		 */
95606175228SAndrew Gallatin 
95706175228SAndrew Gallatin #ifdef __alpha__
95806175228SAndrew Gallatin 		/*
95906175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
96006175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
96106175228SAndrew Gallatin 		 * up the status while we update the command field.
96206175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
96306175228SAndrew Gallatin 		 */
96406175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
96506175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
96606175228SAndrew Gallatin #else
967a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
96806175228SAndrew Gallatin #endif /*__alpha__*/
969a17c678eSDavid Greenman 		sc->cbl_last = txp;
970a17c678eSDavid Greenman 
971a17c678eSDavid Greenman 		/*
9721cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
9731cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
974483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
975a17c678eSDavid Greenman 		 */
9761cd443acSDavid Greenman 		if (sc->tx_queued == 0)
977a17c678eSDavid Greenman 			sc->cbl_first = txp;
978a17c678eSDavid Greenman 
9791cd443acSDavid Greenman 		sc->tx_queued++;
9801cd443acSDavid Greenman 
981a17c678eSDavid Greenman 		/*
982a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
983a17c678eSDavid Greenman 		 */
984fb583156SDavid Greenman 		if (ifp->if_bpf)
98594927790SDavid Greenman 			bpf_mtap(ifp, mb_head);
986483b9871SDavid Greenman 	}
987483b9871SDavid Greenman 
988483b9871SDavid Greenman 	/*
989483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
990483b9871SDavid Greenman 	 * going again if suspended.
991483b9871SDavid Greenman 	 */
992483b9871SDavid Greenman 	if (txp != NULL) {
993483b9871SDavid Greenman 		fxp_scb_wait(sc);
9942e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
995483b9871SDavid Greenman 	}
996a17c678eSDavid Greenman }
997a17c678eSDavid Greenman 
998a17c678eSDavid Greenman /*
9999c7d2607SDavid Greenman  * Process interface interrupts.
1000a17c678eSDavid Greenman  */
100194927790SDavid Greenman static void
1002f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1003a17c678eSDavid Greenman {
1004f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1005ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
10061cd443acSDavid Greenman 	u_int8_t statack;
10070f4dc94cSChuck Paterson 
1008b184b38eSDavid Greenman 	if (sc->suspended) {
1009b184b38eSDavid Greenman 		return;
1010b184b38eSDavid Greenman 	}
1011b184b38eSDavid Greenman 
1012b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1013a17c678eSDavid Greenman 		/*
101411457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
101511457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
101611457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
101711457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
101811457bbfSJonathan Lemon 		 */
101911457bbfSJonathan Lemon 		if (statack == 0xff)
102011457bbfSJonathan Lemon 			return;
102111457bbfSJonathan Lemon 
102211457bbfSJonathan Lemon 		/*
1023a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1024a17c678eSDavid Greenman 		 */
1025ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1026a17c678eSDavid Greenman 
1027a17c678eSDavid Greenman 		/*
10283114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
102906936301SBill Paul 		 *
103006936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
103106936301SBill Paul 		 * be that this event (control unit not ready) was not
103206936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
103306936301SBill Paul 		 * The exact sequence of events that occur when the interface
103406936301SBill Paul 		 * is brought up are different now, and if this event
103506936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
103606936301SBill Paul 		 * can stall for several seconds. The result is that no
103706936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
103806936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
10393114fdb4SDavid Greenman 		 */
104006936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
10413114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
10423114fdb4SDavid Greenman 
10433114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
10443114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
10453114fdb4SDavid Greenman 			    txp = txp->next) {
10463114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
10473114fdb4SDavid Greenman 					m_freem(txp->mb_head);
10483114fdb4SDavid Greenman 					txp->mb_head = NULL;
10493114fdb4SDavid Greenman 				}
10503114fdb4SDavid Greenman 				sc->tx_queued--;
10513114fdb4SDavid Greenman 			}
10523114fdb4SDavid Greenman 			sc->cbl_first = txp;
10533114fdb4SDavid Greenman 			ifp->if_timer = 0;
10543114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
10553114fdb4SDavid Greenman 				if (sc->need_mcsetup)
10563114fdb4SDavid Greenman 					fxp_mc_setup(sc);
10573114fdb4SDavid Greenman 			}
10583114fdb4SDavid Greenman 			/*
10593114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
10603114fdb4SDavid Greenman 			 */
10613114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
10623114fdb4SDavid Greenman 				fxp_start(ifp);
10633114fdb4SDavid Greenman 		}
10643114fdb4SDavid Greenman 		/*
1065a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1066a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1067a17c678eSDavid Greenman 		 * re-start the receiver.
1068a17c678eSDavid Greenman 		 */
1069a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1070a17c678eSDavid Greenman 			struct mbuf *m;
1071a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1072a17c678eSDavid Greenman rcvloop:
1073a17c678eSDavid Greenman 			m = sc->rfa_headm;
1074ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1075ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1076a17c678eSDavid Greenman 
1077a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1078dfe61cf1SDavid Greenman 				/*
1079dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1080dfe61cf1SDavid Greenman 				 */
1081a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1082a17c678eSDavid Greenman 				m->m_next = NULL;
1083a17c678eSDavid Greenman 
1084dfe61cf1SDavid Greenman 				/*
1085ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1086ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1087ba8c6fd5SDavid Greenman 				 * instead.
1088dfe61cf1SDavid Greenman 				 */
1089a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1090a17c678eSDavid Greenman 					struct ether_header *eh;
1091aed53495SDavid Greenman 					int total_len;
1092a17c678eSDavid Greenman 
1093ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1094ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1095ba8c6fd5SDavid Greenman 					if (total_len <
1096ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
109706339180SDavid Greenman 						m_freem(m);
109806339180SDavid Greenman 						goto rcvloop;
109906339180SDavid Greenman 					}
1100e8c8b728SJonathan Lemon #if NVLAN > 0
1101e8c8b728SJonathan Lemon 					/*
1102e8c8b728SJonathan Lemon 					 * Drop the packet if it has CRC
1103e8c8b728SJonathan Lemon 					 * errors.  This test is only needed
1104e8c8b728SJonathan Lemon 					 * when doing 802.1q VLAN on the 82557
1105e8c8b728SJonathan Lemon 					 * chip.
1106e8c8b728SJonathan Lemon 					 */
1107e8c8b728SJonathan Lemon 					if (rfa->rfa_status &
1108e8c8b728SJonathan Lemon 					    FXP_RFA_STATUS_CRC) {
1109e8c8b728SJonathan Lemon 						m_freem(m);
1110e8c8b728SJonathan Lemon 						goto rcvloop;
1111e8c8b728SJonathan Lemon 					}
1112e8c8b728SJonathan Lemon #endif
1113a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
11142e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1115a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1116ba8c6fd5SDavid Greenman 					m->m_data +=
1117ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1118ab090e5bSLuigi Rizzo 					m->m_len -=
1119ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1120ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1121a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1122a17c678eSDavid Greenman 				}
1123a17c678eSDavid Greenman 				goto rcvloop;
1124a17c678eSDavid Greenman 			}
1125a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1126ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1127ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1128ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1129ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
11302e2b8238SJonathan Lemon 				fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1131a17c678eSDavid Greenman 			}
1132a17c678eSDavid Greenman 		}
1133a17c678eSDavid Greenman 	}
1134a17c678eSDavid Greenman }
1135a17c678eSDavid Greenman 
1136dfe61cf1SDavid Greenman /*
1137dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1138dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1139dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1140dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1141dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1142dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1143dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1144dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1145dfe61cf1SDavid Greenman  * them again next time.
1146dfe61cf1SDavid Greenman  */
1147303b270bSEivind Eklund static void
1148f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1149a17c678eSDavid Greenman {
1150f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1151ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1152a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1153c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1154f7788e8eSJonathan Lemon 	int s;
1155a17c678eSDavid Greenman 
1156a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1157a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1158397f9dfeSDavid Greenman 	if (sp->rx_good) {
1159397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1160397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1161397f9dfeSDavid Greenman 	} else {
1162c8cc6fcaSDavid Greenman 		/*
1163c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1164c8cc6fcaSDavid Greenman 		 */
1165397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1166397f9dfeSDavid Greenman 	}
11673ba65732SDavid Greenman 	ifp->if_ierrors +=
11683ba65732SDavid Greenman 	    sp->rx_crc_errors +
11693ba65732SDavid Greenman 	    sp->rx_alignment_errors +
11703ba65732SDavid Greenman 	    sp->rx_rnr_errors +
11716e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1172a17c678eSDavid Greenman 	/*
1173f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1174f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1175f9be9005SDavid Greenman 	 */
1176f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1177f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1178f9be9005SDavid Greenman 		if (tx_threshold < 192)
1179f9be9005SDavid Greenman 			tx_threshold += 64;
1180f9be9005SDavid Greenman 	}
1181f7788e8eSJonathan Lemon 	s = splimp();
1182397f9dfeSDavid Greenman 	/*
1183c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1184c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1185c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1186c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1187c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1188c8cc6fcaSDavid Greenman 	 */
1189c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1190c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1191c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1192c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1193c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1194c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1195c8cc6fcaSDavid Greenman 		}
1196c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1197c8cc6fcaSDavid Greenman 	}
1198c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1199c8cc6fcaSDavid Greenman 	/*
1200397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1201397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1202397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1203397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1204397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1205397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1206397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1207397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1208397f9dfeSDavid Greenman 	 */
1209397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1210397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1211397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1212397f9dfeSDavid Greenman 	}
1213f9be9005SDavid Greenman 	/*
12143ba65732SDavid Greenman 	 * If there is no pending command, start another stats
12153ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1216a17c678eSDavid Greenman 	 */
1217397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1218a17c678eSDavid Greenman 		/*
1219397f9dfeSDavid Greenman 		 * Start another stats dump.
1220a17c678eSDavid Greenman 		 */
12212e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1222dfe61cf1SDavid Greenman 	} else {
1223dfe61cf1SDavid Greenman 		/*
1224dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1225dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
12263ba65732SDavid Greenman 		 * next timer event to update them.
1227dfe61cf1SDavid Greenman 		 */
1228dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1229f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1230dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
12313ba65732SDavid Greenman 
1232dfe61cf1SDavid Greenman 		sp->rx_good = 0;
12333ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
12343ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
12353ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
12363ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1237dfe61cf1SDavid Greenman 	}
1238f7788e8eSJonathan Lemon 
1239f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1240f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
1241f7788e8eSJonathan Lemon 
1242a17c678eSDavid Greenman 	/*
1243a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1244a17c678eSDavid Greenman 	 */
1245f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1246a17c678eSDavid Greenman }
1247a17c678eSDavid Greenman 
1248a17c678eSDavid Greenman /*
1249a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1250a17c678eSDavid Greenman  * the interface.
1251a17c678eSDavid Greenman  */
1252a17c678eSDavid Greenman static void
1253f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1254a17c678eSDavid Greenman {
1255ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
12563ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
12573ba65732SDavid Greenman 	int i;
1258a17c678eSDavid Greenman 
12590f4dc94cSChuck Paterson 
12607dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
12617dced78aSDavid Greenman 	ifp->if_timer = 0;
12627dced78aSDavid Greenman 
1263a17c678eSDavid Greenman 	/*
1264a17c678eSDavid Greenman 	 * Cancel stats updater.
1265a17c678eSDavid Greenman 	 */
1266f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
12673ba65732SDavid Greenman 
12683ba65732SDavid Greenman 	/*
12693ba65732SDavid Greenman 	 * Issue software reset
12703ba65732SDavid Greenman 	 */
1271ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1272a17c678eSDavid Greenman 	DELAY(10);
1273a17c678eSDavid Greenman 
12743ba65732SDavid Greenman 	/*
12753ba65732SDavid Greenman 	 * Release any xmit buffers.
12763ba65732SDavid Greenman 	 */
1277da91462dSDavid Greenman 	txp = sc->cbl_base;
1278da91462dSDavid Greenman 	if (txp != NULL) {
1279da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1280da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1281da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1282da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1283da91462dSDavid Greenman 			}
1284da91462dSDavid Greenman 		}
12853ba65732SDavid Greenman 	}
12863ba65732SDavid Greenman 	sc->tx_queued = 0;
12873ba65732SDavid Greenman 
12883ba65732SDavid Greenman 	/*
12893ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
12903ba65732SDavid Greenman 	 */
12913ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
12923ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
12933ba65732SDavid Greenman 	sc->rfa_headm = NULL;
12943ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
12953ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
12963ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
12973ba65732SDavid Greenman 			/*
12983ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
12993ba65732SDavid Greenman 			 * and we just freed all the buffers we need
13003ba65732SDavid Greenman 			 * above.
13013ba65732SDavid Greenman 			 */
13023ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
13033ba65732SDavid Greenman 		}
13043ba65732SDavid Greenman 	}
1305a17c678eSDavid Greenman }
1306a17c678eSDavid Greenman 
1307a17c678eSDavid Greenman /*
1308a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1309a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1310a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1311a17c678eSDavid Greenman  * card has wedged for some reason.
1312a17c678eSDavid Greenman  */
1313a17c678eSDavid Greenman static void
1314f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1315a17c678eSDavid Greenman {
1316ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1317ba8c6fd5SDavid Greenman 
1318f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
13194a5f1499SDavid Greenman 	ifp->if_oerrors++;
1320a17c678eSDavid Greenman 
1321ba8c6fd5SDavid Greenman 	fxp_init(sc);
1322a17c678eSDavid Greenman }
1323a17c678eSDavid Greenman 
1324a17c678eSDavid Greenman static void
1325f7788e8eSJonathan Lemon fxp_init(void *xsc)
1326a17c678eSDavid Greenman {
1327fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1328ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1329a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1330a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1331a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1332f7788e8eSJonathan Lemon 	int i, prm, s;
1333a17c678eSDavid Greenman 
1334f7788e8eSJonathan Lemon 	s = splimp();
1335a17c678eSDavid Greenman 	/*
13363ba65732SDavid Greenman 	 * Cancel any pending I/O
1337a17c678eSDavid Greenman 	 */
13383ba65732SDavid Greenman 	fxp_stop(sc);
1339a17c678eSDavid Greenman 
1340a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1341a17c678eSDavid Greenman 
1342a17c678eSDavid Greenman 	/*
1343a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1344a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1345a17c678eSDavid Greenman 	 */
1346ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
13472e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1348a17c678eSDavid Greenman 
1349ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
13502e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1351a17c678eSDavid Greenman 
1352a17c678eSDavid Greenman 	/*
1353a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1354a17c678eSDavid Greenman 	 */
1355ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1356ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
13572e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1358a17c678eSDavid Greenman 
1359a17c678eSDavid Greenman 	/*
1360a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1361a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1362a17c678eSDavid Greenman 	 * later.
1363a17c678eSDavid Greenman 	 */
1364a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1365a17c678eSDavid Greenman 
1366a17c678eSDavid Greenman 	/*
1367a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1368a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1369a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1370a17c678eSDavid Greenman 	 */
1371d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1372d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1373397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1374a17c678eSDavid Greenman 
1375a17c678eSDavid Greenman 	cbp->cb_status =	0;
1376a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1377a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1378a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1379001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1380001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1381a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1382f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1383f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1384f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1385f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1386001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1387001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1388f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1389a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1390f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1391f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
13923114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1393f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1394f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1395f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
1396e8c8b728SJonathan Lemon #if NVLAN > 0
1397e8c8b728SJonathan Lemon 	cbp->save_bf =		sc->chip == FXP_CHIP_82557 ? 1 : prm;
1398e8c8b728SJonathan Lemon #else
1399a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1400e8c8b728SJonathan Lemon #endif
1401a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1402f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1403f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1404f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1405f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1406f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1407f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1408f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1409f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1410f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1411f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1412a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1413a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1414a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1415a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1416a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1417a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1418a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1419a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1420f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1421f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1422f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1423f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1424f7788e8eSJonathan Lemon 
1425a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1426a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1427a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1428f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1429f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1430f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1431f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1432a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
14333ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1434a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1435f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1436a17c678eSDavid Greenman 
14373bd07cfdSJonathan Lemon 	if (sc->chip == FXP_CHIP_82557) {
14383bd07cfdSJonathan Lemon 		/*
14393bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
14403bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
14413bd07cfdSJonathan Lemon 		 */
14423bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
14433bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
14443bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
14453bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
14463bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
14473bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
14483bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
14493bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
14503bd07cfdSJonathan Lemon 	} else {
14513bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
14523bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
14533bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
14543bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
14553bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
14563bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
14573bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
14583bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
14593bd07cfdSJonathan Lemon 	}
14603bd07cfdSJonathan Lemon 
1461a17c678eSDavid Greenman 	/*
1462a17c678eSDavid Greenman 	 * Start the config command/DMA.
1463a17c678eSDavid Greenman 	 */
1464ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1465397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
14662e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1467a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
14687dced78aSDavid Greenman 	fxp_dma_wait(&cbp->cb_status, sc);
1469a17c678eSDavid Greenman 
1470a17c678eSDavid Greenman 	/*
1471a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1472a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1473a17c678eSDavid Greenman 	 */
1474a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1475a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1476a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1477a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1478d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1479d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1480a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1481a17c678eSDavid Greenman 
1482a17c678eSDavid Greenman 	/*
1483a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1484a17c678eSDavid Greenman 	 */
1485ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
14862e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1487a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
14887dced78aSDavid Greenman 	fxp_dma_wait(&cb_ias->cb_status, sc);
1489a17c678eSDavid Greenman 
1490a17c678eSDavid Greenman 	/*
1491a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1492a17c678eSDavid Greenman 	 */
1493a17c678eSDavid Greenman 
1494a17c678eSDavid Greenman 	txp = sc->cbl_base;
1495a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1496a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1497a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1498a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
14993bd07cfdSJonathan Lemon 		txp[i].link_addr =
15003bd07cfdSJonathan Lemon 		    vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
15013bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
15023bd07cfdSJonathan Lemon 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
15033bd07cfdSJonathan Lemon 		else
1504a17c678eSDavid Greenman 			txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1505a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1506a17c678eSDavid Greenman 	}
1507a17c678eSDavid Greenman 	/*
1508397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1509a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1510a17c678eSDavid Greenman 	 */
1511a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1512a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1513397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1514a17c678eSDavid Greenman 
1515ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
15162e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1517a17c678eSDavid Greenman 
1518a17c678eSDavid Greenman 	/*
1519a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1520a17c678eSDavid Greenman 	 */
1521ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1522ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1523ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
15242e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1525a17c678eSDavid Greenman 
1526dccee1a1SDavid Greenman 	/*
1527ba8c6fd5SDavid Greenman 	 * Set current media.
1528dccee1a1SDavid Greenman 	 */
1529f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1530f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
1531dccee1a1SDavid Greenman 
1532a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1533a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1534e8c8b728SJonathan Lemon 
1535e8c8b728SJonathan Lemon 	/*
1536e8c8b728SJonathan Lemon 	 * Enable interrupts.
1537e8c8b728SJonathan Lemon 	 */
1538e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1539f7788e8eSJonathan Lemon 	splx(s);
1540a17c678eSDavid Greenman 
1541a17c678eSDavid Greenman 	/*
1542a17c678eSDavid Greenman 	 * Start stats updater.
1543a17c678eSDavid Greenman 	 */
1544f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1545f7788e8eSJonathan Lemon }
1546f7788e8eSJonathan Lemon 
1547f7788e8eSJonathan Lemon static int
1548f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
1549f7788e8eSJonathan Lemon {
1550f7788e8eSJonathan Lemon 
1551f7788e8eSJonathan Lemon 	return (0);
1552a17c678eSDavid Greenman }
1553a17c678eSDavid Greenman 
1554303b270bSEivind Eklund static void
1555f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1556ba8c6fd5SDavid Greenman {
1557ba8c6fd5SDavid Greenman 
1558f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
1559ba8c6fd5SDavid Greenman }
1560ba8c6fd5SDavid Greenman 
1561ba8c6fd5SDavid Greenman /*
1562ba8c6fd5SDavid Greenman  * Change media according to request.
1563ba8c6fd5SDavid Greenman  */
1564f7788e8eSJonathan Lemon static int
1565f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
1566ba8c6fd5SDavid Greenman {
1567ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1568f7788e8eSJonathan Lemon 	struct mii_data *mii;
1569ba8c6fd5SDavid Greenman 
1570f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1571f7788e8eSJonathan Lemon 	mii_mediachg(mii);
1572ba8c6fd5SDavid Greenman 	return (0);
1573ba8c6fd5SDavid Greenman }
1574ba8c6fd5SDavid Greenman 
1575ba8c6fd5SDavid Greenman /*
1576ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1577ba8c6fd5SDavid Greenman  */
1578f7788e8eSJonathan Lemon static void
1579f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1580ba8c6fd5SDavid Greenman {
1581ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1582f7788e8eSJonathan Lemon 	struct mii_data *mii;
1583ba8c6fd5SDavid Greenman 
1584f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
1585f7788e8eSJonathan Lemon 	mii_pollstat(mii);
1586f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
1587f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
15882e2b8238SJonathan Lemon 
15892e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
15902e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
15912e2b8238SJonathan Lemon 	else
15922e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
1593ba8c6fd5SDavid Greenman }
1594ba8c6fd5SDavid Greenman 
1595a17c678eSDavid Greenman /*
1596a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1597a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1598a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1599dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1600a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1601a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1602a17c678eSDavid Greenman  */
1603a17c678eSDavid Greenman static int
1604f7788e8eSJonathan Lemon fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
1605a17c678eSDavid Greenman {
1606ba8c6fd5SDavid Greenman 	u_int32_t v;
1607a17c678eSDavid Greenman 	struct mbuf *m;
1608a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1609a17c678eSDavid Greenman 
1610a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1611a17c678eSDavid Greenman 	if (m != NULL) {
1612a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1613a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1614a17c678eSDavid Greenman 			m_freem(m);
1615eadd5e3aSDavid Greenman 			if (oldm == NULL)
1616eadd5e3aSDavid Greenman 				return 1;
1617a17c678eSDavid Greenman 			m = oldm;
1618eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1619a17c678eSDavid Greenman 		}
1620a17c678eSDavid Greenman 	} else {
1621eadd5e3aSDavid Greenman 		if (oldm == NULL)
1622a17c678eSDavid Greenman 			return 1;
1623eadd5e3aSDavid Greenman 		m = oldm;
1624eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1625eadd5e3aSDavid Greenman 	}
1626ba8c6fd5SDavid Greenman 
1627ba8c6fd5SDavid Greenman 	/*
1628ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1629ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1630ba8c6fd5SDavid Greenman 	 */
1631ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1632ba8c6fd5SDavid Greenman 
1633eadd5e3aSDavid Greenman 	/*
1634eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1635eadd5e3aSDavid Greenman 	 * data start past it.
1636eadd5e3aSDavid Greenman 	 */
1637a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1638eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
16394fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1640eadd5e3aSDavid Greenman 
1641ba8c6fd5SDavid Greenman 	/*
1642ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1643ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1644ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1645ba8c6fd5SDavid Greenman 	 */
16464fc1dda9SAndrew Gallatin 
1647a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1648a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1649a17c678eSDavid Greenman 	rfa->actual_size = 0;
1650ba8c6fd5SDavid Greenman 
1651ba8c6fd5SDavid Greenman 	v = -1;
16524fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
16534fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1654ba8c6fd5SDavid Greenman 
1655dfe61cf1SDavid Greenman 	/*
1656dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1657dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1658dfe61cf1SDavid Greenman 	 */
1659a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1660ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1661ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1662a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1663ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
16644fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1665aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1666a17c678eSDavid Greenman 	} else {
1667a17c678eSDavid Greenman 		sc->rfa_headm = m;
1668a17c678eSDavid Greenman 	}
1669a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1670a17c678eSDavid Greenman 
1671dfe61cf1SDavid Greenman 	return (m == oldm);
1672a17c678eSDavid Greenman }
1673a17c678eSDavid Greenman 
16746ebc3153SDavid Greenman static volatile int
1675f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
1676dccee1a1SDavid Greenman {
1677f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1678dccee1a1SDavid Greenman 	int count = 10000;
16796ebc3153SDavid Greenman 	int value;
1680dccee1a1SDavid Greenman 
1681ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1682ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1683dccee1a1SDavid Greenman 
1684ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1685ba8c6fd5SDavid Greenman 	    && count--)
16866ebc3153SDavid Greenman 		DELAY(10);
1687dccee1a1SDavid Greenman 
1688dccee1a1SDavid Greenman 	if (count <= 0)
1689f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
1690dccee1a1SDavid Greenman 
16916ebc3153SDavid Greenman 	return (value & 0xffff);
1692dccee1a1SDavid Greenman }
1693dccee1a1SDavid Greenman 
1694dccee1a1SDavid Greenman static void
1695f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
1696dccee1a1SDavid Greenman {
1697f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
1698dccee1a1SDavid Greenman 	int count = 10000;
1699dccee1a1SDavid Greenman 
1700ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1701ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1702ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1703dccee1a1SDavid Greenman 
1704ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1705ba8c6fd5SDavid Greenman 	    count--)
17066ebc3153SDavid Greenman 		DELAY(10);
1707dccee1a1SDavid Greenman 
1708dccee1a1SDavid Greenman 	if (count <= 0)
1709f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
1710dccee1a1SDavid Greenman }
1711dccee1a1SDavid Greenman 
1712dccee1a1SDavid Greenman static int
1713f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1714a17c678eSDavid Greenman {
17159b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1716a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1717f7788e8eSJonathan Lemon 	struct mii_data *mii;
1718f7788e8eSJonathan Lemon 	int s, error = 0;
1719a17c678eSDavid Greenman 
1720f7788e8eSJonathan Lemon 	s = splimp();
1721a17c678eSDavid Greenman 
1722a17c678eSDavid Greenman 	switch (command) {
1723a17c678eSDavid Greenman 	case SIOCSIFADDR:
1724a17c678eSDavid Greenman 	case SIOCGIFADDR:
1725fb583156SDavid Greenman 	case SIOCSIFMTU:
1726fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1727a17c678eSDavid Greenman 		break;
1728a17c678eSDavid Greenman 
1729a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1730f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1731f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1732f7788e8eSJonathan Lemon 		else
1733f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1734a17c678eSDavid Greenman 
1735a17c678eSDavid Greenman 		/*
1736a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1737a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1738a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1739a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1740a17c678eSDavid Greenman 		 */
1741a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1742fb583156SDavid Greenman 			fxp_init(sc);
1743a17c678eSDavid Greenman 		} else {
1744a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
17454a5f1499SDavid Greenman 				fxp_stop(sc);
1746a17c678eSDavid Greenman 		}
1747a17c678eSDavid Greenman 		break;
1748a17c678eSDavid Greenman 
1749a17c678eSDavid Greenman 	case SIOCADDMULTI:
1750a17c678eSDavid Greenman 	case SIOCDELMULTI:
1751f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
1752f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
1753f7788e8eSJonathan Lemon 		else
1754f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
1755a17c678eSDavid Greenman 		/*
1756a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1757a17c678eSDavid Greenman 		 * accordingly.
1758a17c678eSDavid Greenman 		 */
1759f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
1760397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1761397f9dfeSDavid Greenman 		/*
1762f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
1763397f9dfeSDavid Greenman 		 * again rather than else {}.
1764397f9dfeSDavid Greenman 		 */
1765f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
1766fb583156SDavid Greenman 			fxp_init(sc);
1767a17c678eSDavid Greenman 		error = 0;
1768ba8c6fd5SDavid Greenman 		break;
1769ba8c6fd5SDavid Greenman 
1770ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1771ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1772f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
1773f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
1774f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
1775f7788e8eSJonathan Lemon                             &mii->mii_media, command);
1776f7788e8eSJonathan Lemon 		} else {
1777ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1778f7788e8eSJonathan Lemon 		}
1779a17c678eSDavid Greenman 		break;
1780a17c678eSDavid Greenman 
1781a17c678eSDavid Greenman 	default:
1782a17c678eSDavid Greenman 		error = EINVAL;
1783a17c678eSDavid Greenman 	}
1784f7788e8eSJonathan Lemon 	splx(s);
1785a17c678eSDavid Greenman 	return (error);
1786a17c678eSDavid Greenman }
1787397f9dfeSDavid Greenman 
1788397f9dfeSDavid Greenman /*
1789397f9dfeSDavid Greenman  * Program the multicast filter.
1790397f9dfeSDavid Greenman  *
1791397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1792397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
17933114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1794397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1795dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1796397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1797397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1798397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1799397f9dfeSDavid Greenman  *
1800397f9dfeSDavid Greenman  * This function must be called at splimp.
1801397f9dfeSDavid Greenman  */
1802397f9dfeSDavid Greenman static void
1803f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
1804397f9dfeSDavid Greenman {
1805397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1806397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1807397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1808397f9dfeSDavid Greenman 	int nmcasts;
18097dced78aSDavid Greenman 	int count;
1810397f9dfeSDavid Greenman 
18113114fdb4SDavid Greenman 	/*
18123114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
18133114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
18143114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
18153114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
18163114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
18173114fdb4SDavid Greenman 	 */
1818397f9dfeSDavid Greenman 	if (sc->tx_queued) {
18193114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
18203114fdb4SDavid Greenman 
18213114fdb4SDavid Greenman 		/*
18223114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
18233114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
18243114fdb4SDavid Greenman 		 */
18253114fdb4SDavid Greenman 		if (sc->need_mcsetup)
18263114fdb4SDavid Greenman 			return;
1827397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
18283114fdb4SDavid Greenman 
18293114fdb4SDavid Greenman 		/*
18303114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
18313114fdb4SDavid Greenman 		 * TX commands have been processed.
18323114fdb4SDavid Greenman 		 */
18333114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
18343114fdb4SDavid Greenman 		txp->mb_head = NULL;
18353114fdb4SDavid Greenman 		txp->cb_status = 0;
1836e8c8b728SJonathan Lemon 		txp->cb_command = FXP_CB_COMMAND_NOP |
1837e8c8b728SJonathan Lemon 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
18383114fdb4SDavid Greenman 		/*
18393114fdb4SDavid Greenman 		 * Advance the end of list forward.
18403114fdb4SDavid Greenman 		 */
18413114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
18423114fdb4SDavid Greenman 		sc->cbl_last = txp;
18433114fdb4SDavid Greenman 		sc->tx_queued++;
18443114fdb4SDavid Greenman 		/*
18453114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
18463114fdb4SDavid Greenman 		 */
18473114fdb4SDavid Greenman 		fxp_scb_wait(sc);
18482e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
18493114fdb4SDavid Greenman 		/*
18503114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
18513114fdb4SDavid Greenman 		 * card again.
18523114fdb4SDavid Greenman 		 */
18533114fdb4SDavid Greenman 		ifp->if_timer = 5;
18543114fdb4SDavid Greenman 
1855397f9dfeSDavid Greenman 		return;
1856397f9dfeSDavid Greenman 	}
1857397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
1858397f9dfeSDavid Greenman 
1859397f9dfeSDavid Greenman 	/*
1860397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
1861397f9dfeSDavid Greenman 	 */
1862397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
1863397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
1864397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
1865e8c8b728SJonathan Lemon 	mcsp->cb_command = FXP_CB_COMMAND_MCAS |
1866e8c8b728SJonathan Lemon 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
1867397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
1868397f9dfeSDavid Greenman 
1869397f9dfeSDavid Greenman 	nmcasts = 0;
1870f7788e8eSJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
1871f7788e8eSJonathan Lemon #if __FreeBSD_version < 500000
1872f7788e8eSJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1873f7788e8eSJonathan Lemon #else
18746817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1875f7788e8eSJonathan Lemon #endif
1876397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
1877397f9dfeSDavid Greenman 				continue;
1878397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
1879f7788e8eSJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
1880397f9dfeSDavid Greenman 				nmcasts = 0;
1881397f9dfeSDavid Greenman 				break;
1882397f9dfeSDavid Greenman 			}
1883397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1884d244b0e9SPeter Wemm 			    (void *)(uintptr_t)(volatile void *)
1885d244b0e9SPeter Wemm 				&sc->mcsp->mc_addr[nmcasts][0], 6);
1886397f9dfeSDavid Greenman 			nmcasts++;
1887397f9dfeSDavid Greenman 		}
1888397f9dfeSDavid Greenman 	}
1889397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
1890397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
1891397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1892397f9dfeSDavid Greenman 
1893397f9dfeSDavid Greenman 	/*
1894397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
1895397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
1896397f9dfeSDavid Greenman 	 */
18977dced78aSDavid Greenman 	count = 100;
1898397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
18997dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
19007dced78aSDavid Greenman 		DELAY(10);
19017dced78aSDavid Greenman 	if (count == 0) {
1902f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
19037dced78aSDavid Greenman 		return;
19047dced78aSDavid Greenman 	}
1905397f9dfeSDavid Greenman 
1906397f9dfeSDavid Greenman 	/*
1907397f9dfeSDavid Greenman 	 * Start the multicast setup command.
1908397f9dfeSDavid Greenman 	 */
1909397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
1910397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
19112e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1912397f9dfeSDavid Greenman 
19133114fdb4SDavid Greenman 	ifp->if_timer = 2;
1914397f9dfeSDavid Greenman 	return;
1915397f9dfeSDavid Greenman }
1916