xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 1104779b709f758612ed36e8ae168577e71eff3f)
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  *
28a17c678eSDavid Greenman  */
29a17c678eSDavid Greenman 
30a17c678eSDavid Greenman /*
31ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
32a17c678eSDavid Greenman  */
33a17c678eSDavid Greenman 
3401c516b0SMaxime Henrion #include <sys/cdefs.h>
3501c516b0SMaxime Henrion __FBSDID("$FreeBSD$");
3601c516b0SMaxime Henrion 
37a17c678eSDavid Greenman #include <sys/param.h>
38a17c678eSDavid Greenman #include <sys/systm.h>
3983e6547dSMaxime Henrion #include <sys/endian.h>
40a17c678eSDavid Greenman #include <sys/mbuf.h>
41f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
42a17c678eSDavid Greenman #include <sys/kernel.h>
434458ac71SBruce Evans #include <sys/socket.h>
4472a32a26SJonathan Lemon #include <sys/sysctl.h>
45a17c678eSDavid Greenman 
46a17c678eSDavid Greenman #include <net/if.h>
47397f9dfeSDavid Greenman #include <net/if_dl.h>
48ba8c6fd5SDavid Greenman #include <net/if_media.h>
49a17c678eSDavid Greenman 
50a17c678eSDavid Greenman #include <net/bpf.h>
51ba8c6fd5SDavid Greenman #include <sys/sockio.h>
526182fdbdSPeter Wemm #include <sys/bus.h>
536182fdbdSPeter Wemm #include <machine/bus.h>
546182fdbdSPeter Wemm #include <sys/rman.h>
556182fdbdSPeter Wemm #include <machine/resource.h>
56ba8c6fd5SDavid Greenman 
571d5e9e22SEivind Eklund #include <net/ethernet.h>
581d5e9e22SEivind Eklund #include <net/if_arp.h>
59ba8c6fd5SDavid Greenman 
60f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
61a17c678eSDavid Greenman 
62e8c8b728SJonathan Lemon #include <net/if_types.h>
63e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
64e8c8b728SJonathan Lemon 
65c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
66c8bca6dcSBill Paul #include <netinet/in.h>
67c8bca6dcSBill Paul #include <netinet/in_systm.h>
68c8bca6dcSBill Paul #include <netinet/ip.h>
69c8bca6dcSBill Paul #include <machine/in_cksum.h>
70c8bca6dcSBill Paul #endif
71c8bca6dcSBill Paul 
72a17c678eSDavid Greenman #include <pci/pcivar.h>
73df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
74a17c678eSDavid Greenman 
75f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
76f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
77f7788e8eSJonathan Lemon 
78f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8072a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
81f7788e8eSJonathan Lemon 
82f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
83f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
84f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
85f7788e8eSJonathan Lemon #include "miibus_if.h"
864fc1dda9SAndrew Gallatin 
87ba8c6fd5SDavid Greenman /*
88ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
89ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
90ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
91ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
92ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
93ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
94ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
95ba8c6fd5SDavid Greenman  */
96ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
97ba8c6fd5SDavid Greenman 
98ba8c6fd5SDavid Greenman /*
99f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
100f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
101f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
102f7788e8eSJonathan Lemon  */
103f7788e8eSJonathan Lemon static int tx_threshold = 64;
104f7788e8eSJonathan Lemon 
105f7788e8eSJonathan Lemon /*
106f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
107f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
108f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
109f7788e8eSJonathan Lemon  * to fxp_init.
110f7788e8eSJonathan Lemon  *
111f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
112f7788e8eSJonathan Lemon  */
113f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
114f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
115f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
116f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
117f7788e8eSJonathan Lemon 	0x0,	/*  0 */
118f7788e8eSJonathan Lemon 	0x0,	/*  1 */
119f7788e8eSJonathan Lemon 	0x0,	/*  2 */
120f7788e8eSJonathan Lemon 	0x0,	/*  3 */
121f7788e8eSJonathan Lemon 	0x0,	/*  4 */
122f7788e8eSJonathan Lemon 	0x0,	/*  5 */
123f7788e8eSJonathan Lemon 	0x32,	/*  6 */
124f7788e8eSJonathan Lemon 	0x0,	/*  7 */
125f7788e8eSJonathan Lemon 	0x0,	/*  8 */
126f7788e8eSJonathan Lemon 	0x0,	/*  9 */
127f7788e8eSJonathan Lemon 	0x6,	/* 10 */
128f7788e8eSJonathan Lemon 	0x0,	/* 11 */
129f7788e8eSJonathan Lemon 	0x0,	/* 12 */
130f7788e8eSJonathan Lemon 	0x0,	/* 13 */
131f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
132f7788e8eSJonathan Lemon 	0x48,	/* 15 */
133f7788e8eSJonathan Lemon 	0x0,	/* 16 */
134f7788e8eSJonathan Lemon 	0x40,	/* 17 */
135f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
136f7788e8eSJonathan Lemon 	0x0,	/* 19 */
137f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
138f7788e8eSJonathan Lemon 	0x5	/* 21 */
139f7788e8eSJonathan Lemon };
140f7788e8eSJonathan Lemon 
141f7788e8eSJonathan Lemon struct fxp_ident {
142f7788e8eSJonathan Lemon 	u_int16_t	devid;
143f7788e8eSJonathan Lemon 	char 		*name;
144f7788e8eSJonathan Lemon };
145f7788e8eSJonathan Lemon 
146f7788e8eSJonathan Lemon /*
147f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
148f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
149f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
150f7788e8eSJonathan Lemon  * them.
151f7788e8eSJonathan Lemon  */
152f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
153537b41d5SJohn Polstra     { 0x1029,		"Intel 82559 PCI/CardBus Pro/100" },
154537b41d5SJohn Polstra     { 0x1030,		"Intel 82559 Pro/100 Ethernet" },
155537b41d5SJohn Polstra     { 0x1031,		"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
156537b41d5SJohn Polstra     { 0x1032,		"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
157537b41d5SJohn Polstra     { 0x1033,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
158537b41d5SJohn Polstra     { 0x1034,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
159537b41d5SJohn Polstra     { 0x1035,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
160537b41d5SJohn Polstra     { 0x1036,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
161537b41d5SJohn Polstra     { 0x1037,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
162537b41d5SJohn Polstra     { 0x1038,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
163537b41d5SJohn Polstra     { 0x1039,		"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
164537b41d5SJohn Polstra     { 0x103A,		"Intel 82801DB (ICH4) Pro/100 Ethernet" },
165537b41d5SJohn Polstra     { 0x103B,		"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
166537b41d5SJohn Polstra     { 0x103C,		"Intel 82801DB (ICH4) Pro/100 Ethernet" },
167537b41d5SJohn Polstra     { 0x103D,		"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
168537b41d5SJohn Polstra     { 0x103E,		"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
169537b41d5SJohn Polstra     { 0x1059,		"Intel 82551QM Pro/100 M Mobile Connection" },
170537b41d5SJohn Polstra     { 0x1209,		"Intel 82559ER Embedded 10/100 Ethernet" },
171537b41d5SJohn Polstra     { 0x1229,		"Intel 82557/8/9 EtherExpress Pro/100(B) Ethernet" },
172537b41d5SJohn Polstra     { 0x2449,		"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
173f7788e8eSJonathan Lemon     { 0,		NULL },
174f7788e8eSJonathan Lemon };
175f7788e8eSJonathan Lemon 
176c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
177c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
178c8bca6dcSBill Paul #else
179c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
180c8bca6dcSBill Paul #endif
181c8bca6dcSBill Paul 
182f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
183f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
184f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
185f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
186f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
187f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
188f7788e8eSJonathan Lemon 
189f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
1904953bccaSNate Lawson static void		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
1914953bccaSNate Lawson 			    u_int8_t statack, int count);
192f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
1934953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
194f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
19548e417ebSJonathan Lemon static void		fxp_powerstate_d0(device_t dev);
196f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
1974953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
198f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
199f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
200f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
201f7788e8eSJonathan Lemon 			    caddr_t data);
202f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
203b2badf02SMaxime Henrion static int		fxp_add_rfabuf(struct fxp_softc *sc,
204b2badf02SMaxime Henrion     			    struct fxp_rx *rxp);
20509882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
206f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
207f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
208f7788e8eSJonathan Lemon 			    int autosize);
20900c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
21000c4116bSJonathan Lemon 			    u_int16_t data);
211f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
212f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
213f7788e8eSJonathan Lemon 			    int offset, int words);
21400c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
21500c4116bSJonathan Lemon 			    int offset, int words);
216f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
217f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
218f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
219f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
220f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
221f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
222f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
223f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
224f7788e8eSJonathan Lemon 			    int value);
22572a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
22672a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
22772a32a26SJonathan Lemon 			    int low, int high);
22872a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
22972a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
230f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2312e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
232209b07bcSMaxime Henrion static __inline void	fxp_dma_wait(struct fxp_softc *sc,
233209b07bcSMaxime Henrion     			    volatile u_int16_t *status, bus_dma_tag_t dmat,
234209b07bcSMaxime Henrion 			    bus_dmamap_t map);
235f7788e8eSJonathan Lemon 
236f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
237f7788e8eSJonathan Lemon 	/* Device interface */
238f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
239f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
240f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
241f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
242f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
243f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
244f7788e8eSJonathan Lemon 
245f7788e8eSJonathan Lemon 	/* MII interface */
246f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
247f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
248f7788e8eSJonathan Lemon 
249f7788e8eSJonathan Lemon 	{ 0, 0 }
250f7788e8eSJonathan Lemon };
251f7788e8eSJonathan Lemon 
252f7788e8eSJonathan Lemon static driver_t fxp_driver = {
253f7788e8eSJonathan Lemon 	"fxp",
254f7788e8eSJonathan Lemon 	fxp_methods,
255f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
256f7788e8eSJonathan Lemon };
257f7788e8eSJonathan Lemon 
258f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
259f7788e8eSJonathan Lemon 
260f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
261f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
262f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
263f7788e8eSJonathan Lemon 
2642b5989e9SLuigi Rizzo static int fxp_rnr;
2652b5989e9SLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, fxp_rnr, CTLFLAG_RW, &fxp_rnr, 0, "fxp rnr events");
2662b5989e9SLuigi Rizzo 
26798b27888SRobert Watson static int fxp_noflow;
26898b27888SRobert Watson SYSCTL_INT(_hw, OID_AUTO, fxp_noflow, CTLFLAG_RW, &fxp_noflow, 0, "fxp flow control disabled");
26998b27888SRobert Watson TUNABLE_INT("hw.fxp_noflow", &fxp_noflow);
27098b27888SRobert Watson 
271f7788e8eSJonathan Lemon /*
272dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
273dfe61cf1SDavid Greenman  * completed).
274dfe61cf1SDavid Greenman  */
275c1087c13SBruce Evans static __inline void
276f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
277a17c678eSDavid Greenman {
278a17c678eSDavid Greenman 	int i = 10000;
279a17c678eSDavid Greenman 
2807dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2817dced78aSDavid Greenman 		DELAY(2);
2827dced78aSDavid Greenman 	if (i == 0)
28300c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
284e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
285e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
286e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
287e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2887dced78aSDavid Greenman }
2897dced78aSDavid Greenman 
2907dced78aSDavid Greenman static __inline void
2912e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2922e2b8238SJonathan Lemon {
2932e2b8238SJonathan Lemon 
2942e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2952e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2962e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2972e2b8238SJonathan Lemon 	}
2982e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2992e2b8238SJonathan Lemon }
3002e2b8238SJonathan Lemon 
3012e2b8238SJonathan Lemon static __inline void
302209b07bcSMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile u_int16_t *status,
303209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3047dced78aSDavid Greenman {
3057dced78aSDavid Greenman 	int i = 10000;
3067dced78aSDavid Greenman 
307209b07bcSMaxime Henrion 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
308209b07bcSMaxime Henrion 	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) {
3097dced78aSDavid Greenman 		DELAY(2);
310209b07bcSMaxime Henrion 		bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
311209b07bcSMaxime Henrion 	}
3127dced78aSDavid Greenman 	if (i == 0)
313f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
314a17c678eSDavid Greenman }
315a17c678eSDavid Greenman 
316dfe61cf1SDavid Greenman /*
317dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
318dfe61cf1SDavid Greenman  */
3196182fdbdSPeter Wemm static int
3206182fdbdSPeter Wemm fxp_probe(device_t dev)
321a17c678eSDavid Greenman {
322f7788e8eSJonathan Lemon 	u_int16_t devid;
323f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
324f7788e8eSJonathan Lemon 
32555ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
326f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
327f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
328f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
329f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
330f7788e8eSJonathan Lemon 				return (0);
33155ce7b51SDavid Greenman 			}
332dd68ef16SPeter Wemm 		}
333f7788e8eSJonathan Lemon 	}
334f7788e8eSJonathan Lemon 	return (ENXIO);
3356182fdbdSPeter Wemm }
3366182fdbdSPeter Wemm 
33748e417ebSJonathan Lemon static void
33848e417ebSJonathan Lemon fxp_powerstate_d0(device_t dev)
33948e417ebSJonathan Lemon {
34048e417ebSJonathan Lemon #if __FreeBSD_version >= 430002
34148e417ebSJonathan Lemon 	u_int32_t iobase, membase, irq;
34248e417ebSJonathan Lemon 
34348e417ebSJonathan Lemon 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
34448e417ebSJonathan Lemon 		/* Save important PCI config data. */
34548e417ebSJonathan Lemon 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
34648e417ebSJonathan Lemon 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
34748e417ebSJonathan Lemon 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
34848e417ebSJonathan Lemon 
34948e417ebSJonathan Lemon 		/* Reset the power state. */
35048e417ebSJonathan Lemon 		device_printf(dev, "chip is in D%d power mode "
35148e417ebSJonathan Lemon 		    "-- setting to D0\n", pci_get_powerstate(dev));
35248e417ebSJonathan Lemon 
35348e417ebSJonathan Lemon 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
35448e417ebSJonathan Lemon 
35548e417ebSJonathan Lemon 		/* Restore PCI config data. */
35648e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
35748e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
35848e417ebSJonathan Lemon 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
35948e417ebSJonathan Lemon 	}
36048e417ebSJonathan Lemon #endif
36148e417ebSJonathan Lemon }
36248e417ebSJonathan Lemon 
363b2badf02SMaxime Henrion static void
364b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
365b2badf02SMaxime Henrion {
366b2badf02SMaxime Henrion 	u_int32_t *addr;
367b2badf02SMaxime Henrion 
368b2badf02SMaxime Henrion 	if (error)
369b2badf02SMaxime Henrion 		return;
370b2badf02SMaxime Henrion 
371b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
372b2badf02SMaxime Henrion 	addr = arg;
373b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
374b2badf02SMaxime Henrion }
375b2badf02SMaxime Henrion 
3766182fdbdSPeter Wemm static int
3776182fdbdSPeter Wemm fxp_attach(device_t dev)
378a17c678eSDavid Greenman {
3796182fdbdSPeter Wemm 	int error = 0;
3806182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
381ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
382b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
3839fa6ccfbSMatt Jacob 	u_int32_t val;
38483e6547dSMaxime Henrion 	u_int16_t data, myea[ETHER_ADDR_LEN / 2];
385d73e2e55SMaxime Henrion 	int i, rid, m1, m2, prefer_iomap, maxtxseg;
386a35e7eaaSDon Lewis 	int s, ipcbxmit_disable;
387a17c678eSDavid Greenman 
388f7788e8eSJonathan Lemon 	sc->dev = dev;
3896c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
390a1a9c8f7SJonathan Lemon 	sysctl_ctx_init(&sc->sysctl_ctx);
3916008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
3924953bccaSNate Lawson 	    MTX_DEF);
3934953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
3944953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
395a17c678eSDavid Greenman 
396f7788e8eSJonathan Lemon 	s = splimp();
397a17c678eSDavid Greenman 
398dfe61cf1SDavid Greenman 	/*
3992bce79a2SMaxim Sobolev 	 * Enable bus mastering.
400df373873SWes Peters 	 */
401cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4029fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
403df373873SWes Peters 
40448e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
4058d799694SBill Paul 
406df373873SWes Peters 	/*
4079fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4089fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4099fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
410dfe61cf1SDavid Greenman 	 */
4119fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
4129fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
4132a05a4ebSMatt Jacob 	prefer_iomap = 0;
4142a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
4152a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
4169fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
4179fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
4189fa6ccfbSMatt Jacob 	}
4199fa6ccfbSMatt Jacob 
420533294b9SMatthew N. Dodd 	sc->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4219fa6ccfbSMatt Jacob 	sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4229fa6ccfbSMatt Jacob 	sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4236182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
424533294b9SMatthew N. Dodd 	if (sc->mem == NULL) {
4259fa6ccfbSMatt Jacob 		sc->rtp =
4269fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4279fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4289fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4299fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
4309fa6ccfbSMatt Jacob 	}
4319fa6ccfbSMatt Jacob 
4326182fdbdSPeter Wemm 	if (!sc->mem) {
4336182fdbdSPeter Wemm 		error = ENXIO;
434a17c678eSDavid Greenman 		goto fail;
435a17c678eSDavid Greenman         }
4369fa6ccfbSMatt Jacob 	if (bootverbose) {
4379fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4389fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4399fa6ccfbSMatt Jacob 	}
4404fc1dda9SAndrew Gallatin 
4414fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4424fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
443a17c678eSDavid Greenman 
444a17c678eSDavid Greenman 	/*
445dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
446dfe61cf1SDavid Greenman 	 */
4476182fdbdSPeter Wemm 	rid = 0;
4486182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4496182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4506182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4516182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4526182fdbdSPeter Wemm 		error = ENXIO;
4536182fdbdSPeter Wemm 		goto fail;
4546182fdbdSPeter Wemm 	}
4556182fdbdSPeter Wemm 
456f7788e8eSJonathan Lemon 	/*
457f7788e8eSJonathan Lemon 	 * Reset to a stable state.
458f7788e8eSJonathan Lemon 	 */
459f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
460f7788e8eSJonathan Lemon 	DELAY(10);
461f7788e8eSJonathan Lemon 
462f7788e8eSJonathan Lemon 	/*
463f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
464f7788e8eSJonathan Lemon 	 */
465f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
466f7788e8eSJonathan Lemon 
467f7788e8eSJonathan Lemon 	/*
4683bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
469f7788e8eSJonathan Lemon 	 */
470f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
471f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
472f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
473dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
474f7788e8eSJonathan Lemon 
475f7788e8eSJonathan Lemon 	/*
47672a32a26SJonathan Lemon 	 * Create the sysctl tree
47772a32a26SJonathan Lemon 	 */
47872a32a26SJonathan Lemon 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
47972a32a26SJonathan Lemon 	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
48072a32a26SJonathan Lemon 	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
4814953bccaSNate Lawson 	if (sc->sysctl_tree == NULL) {
4824953bccaSNate Lawson 		error = ENXIO;
48372a32a26SJonathan Lemon 		goto fail;
4844953bccaSNate Lawson 	}
48572a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
48672a32a26SJonathan Lemon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
487858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
48872a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
48972a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
49072a32a26SJonathan Lemon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
491858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
49272a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
49372a32a26SJonathan Lemon 
49472a32a26SJonathan Lemon 	/*
49572a32a26SJonathan Lemon 	 * Pull in device tunables.
49672a32a26SJonathan Lemon 	 */
49772a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
49872a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
49972a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50072a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
50172a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
50272a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
50372a32a26SJonathan Lemon 
50472a32a26SJonathan Lemon 	/*
50572a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
5063bd07cfdSJonathan Lemon 	 */
5073bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
5083bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
50972a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
51072a32a26SJonathan Lemon 	else
51172a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
5123bd07cfdSJonathan Lemon 
5133bd07cfdSJonathan Lemon 	/*
5142e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
51500c4116bSJonathan Lemon 	 *
51672a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
51772a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
51872a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
51900c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
52000c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
52100c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
52200c4116bSJonathan Lemon 	 *
52300c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5242e2b8238SJonathan Lemon 	 */
5252e2b8238SJonathan Lemon 	i = pci_get_device(dev);
52672a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
52772a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
52800c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
52900c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
53000c4116bSJonathan Lemon 			u_int16_t cksum;
53100c4116bSJonathan Lemon 			int i;
53200c4116bSJonathan Lemon 
53300c4116bSJonathan Lemon 			device_printf(dev,
534001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
53500c4116bSJonathan Lemon 			data &= ~0x02;
53600c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
53700c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
53800c4116bSJonathan Lemon 			cksum = 0;
53900c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
54000c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
54100c4116bSJonathan Lemon 				cksum += data;
54200c4116bSJonathan Lemon 			}
54300c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
54400c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
54500c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
54600c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
54700c4116bSJonathan Lemon 			device_printf(dev,
54800c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
54900c4116bSJonathan Lemon 			    i, data, cksum);
55000c4116bSJonathan Lemon #if 1
55100c4116bSJonathan Lemon 			/*
55200c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
55300c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
55400c4116bSJonathan Lemon 			 */
5552e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
55600c4116bSJonathan Lemon #endif
55700c4116bSJonathan Lemon 		}
55800c4116bSJonathan Lemon 	}
5592e2b8238SJonathan Lemon 
5602e2b8238SJonathan Lemon 	/*
5613bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5623bd07cfdSJonathan Lemon 	 */
56372a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5643bd07cfdSJonathan Lemon 		/*
56574396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
56674396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
56774396a0aSJonathan Lemon 		 * the board to turn on MWI.
5683bd07cfdSJonathan Lemon 		 */
56974396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
57074396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5713bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5723bd07cfdSJonathan Lemon 
5733bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5743bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
575920b58e8SBrooks Davis 
576e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
577e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
5783bd07cfdSJonathan Lemon 	}
5793bd07cfdSJonathan Lemon 
5803bd07cfdSJonathan Lemon 	/*
581c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
582c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
583c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
584c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
585a35e7eaaSDon Lewis 	 *
586a35e7eaaSDon Lewis 	 * At least some 82550 cards probed as "chip=0x12298086 rev=0x0d"
587a35e7eaaSDon Lewis 	 * truncate packets that end with an mbuf containing 1 to 3 bytes
588a35e7eaaSDon Lewis 	 * when used with this feature enabled in the previous version of the
589a35e7eaaSDon Lewis 	 * driver.  This problem appears to be fixed now that the driver
590a35e7eaaSDon Lewis 	 * always sets the hardware parse bit in the IPCB structure, which
591a35e7eaaSDon Lewis 	 * the "Intel 8255x 10/100 Mbps Ethernet Controller Family Open
592a35e7eaaSDon Lewis 	 * Source Software Developer Manual" says is necessary in the
593a35e7eaaSDon Lewis 	 * cases where packet truncation was observed.
594a35e7eaaSDon Lewis 	 *
595a35e7eaaSDon Lewis 	 * The device hint "hint.fxp.UNIT_NUMBER.ipcbxmit_disable"
596a35e7eaaSDon Lewis 	 * allows this feature to be disabled at boot time.
597a35e7eaaSDon Lewis 	 *
598a35e7eaaSDon Lewis 	 * If fxp is not compiled into the kernel, this feature may also
599a35e7eaaSDon Lewis 	 * be disabled at run time:
600a35e7eaaSDon Lewis 	 *    # kldunload fxp
601a35e7eaaSDon Lewis 	 *    # kenv hint.fxp.0.ipcbxmit_disable=1
602a35e7eaaSDon Lewis 	 *    # kldload fxp
603c8bca6dcSBill Paul 	 */
604c8bca6dcSBill Paul 
605a35e7eaaSDon Lewis 	if (resource_int_value("fxp", device_get_unit(dev), "ipcbxmit_disable",
606a35e7eaaSDon Lewis 	    &ipcbxmit_disable) != 0)
607a35e7eaaSDon Lewis 		ipcbxmit_disable = 0;
608a35e7eaaSDon Lewis 	if (ipcbxmit_disable == 0 && (sc->revision == FXP_REV_82550 ||
609a35e7eaaSDon Lewis 	    sc->revision == FXP_REV_82550_C)) {
610c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
611c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
612c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
613c8bca6dcSBill Paul 	} else {
614c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
615c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
616c8bca6dcSBill Paul 	}
617c8bca6dcSBill Paul 
618c8bca6dcSBill Paul 	/*
619b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
620b2badf02SMaxime Henrion 	 */
621d73e2e55SMaxime Henrion 	maxtxseg = sc->flags & FXP_FLAG_EXT_RFA ? FXP_NTXSEG - 1 : FXP_NTXSEG;
622b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 2, 0, BUS_SPACE_MAXADDR_32BIT,
623d73e2e55SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * maxtxseg,
624d73e2e55SMaxime Henrion 	    maxtxseg, MCLBYTES, 0, &sc->fxp_mtag);
625b2badf02SMaxime Henrion 	if (error) {
626b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
627b2badf02SMaxime Henrion 		goto fail;
628b2badf02SMaxime Henrion 	}
629b2badf02SMaxime Henrion 
630b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
631b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_stats), 1,
632d73e2e55SMaxime Henrion 	    sizeof(struct fxp_stats), 0, &sc->fxp_stag);
633b2badf02SMaxime Henrion 	if (error) {
634b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
635b2badf02SMaxime Henrion 		goto fail;
636b2badf02SMaxime Henrion 	}
637b2badf02SMaxime Henrion 
638b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
639b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->fxp_smap);
640b2badf02SMaxime Henrion 	if (error)
6414953bccaSNate Lawson 		goto fail;
642b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
643b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
644b2badf02SMaxime Henrion 	if (error) {
645b2badf02SMaxime Henrion 		device_printf(dev, "could not map the stats buffer\n");
646b2badf02SMaxime Henrion 		goto fail;
647b2badf02SMaxime Henrion 	}
648b2badf02SMaxime Henrion 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
649b2badf02SMaxime Henrion 
650b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
651b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, FXP_TXCB_SZ, 1,
652d73e2e55SMaxime Henrion 	    FXP_TXCB_SZ, 0, &sc->cbl_tag);
653b2badf02SMaxime Henrion 	if (error) {
654b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
655b2badf02SMaxime Henrion 		goto fail;
656b2badf02SMaxime Henrion 	}
657b2badf02SMaxime Henrion 
658b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
659b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->cbl_map);
660b2badf02SMaxime Henrion 	if (error)
6614953bccaSNate Lawson 		goto fail;
662b2badf02SMaxime Henrion 	bzero(sc->fxp_desc.cbl_list, FXP_TXCB_SZ);
663b2badf02SMaxime Henrion 
664b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
665b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
666b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
667b2badf02SMaxime Henrion 	if (error) {
668b2badf02SMaxime Henrion 		device_printf(dev, "could not map DMA memory\n");
669b2badf02SMaxime Henrion 		goto fail;
670b2badf02SMaxime Henrion 	}
671b2badf02SMaxime Henrion 
672b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
673b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_cb_mcs), 1,
674d73e2e55SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), 0, &sc->mcs_tag);
675b2badf02SMaxime Henrion 	if (error) {
676b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
677b2badf02SMaxime Henrion 		goto fail;
678b2badf02SMaxime Henrion 	}
679b2badf02SMaxime Henrion 
680b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
681b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->mcs_map);
682b2badf02SMaxime Henrion 	if (error)
6834953bccaSNate Lawson 		goto fail;
684b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
685b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
686b2badf02SMaxime Henrion 	if (error) {
687b2badf02SMaxime Henrion 		device_printf(dev, "can't map the multicast setup command\n");
688b2badf02SMaxime Henrion 		goto fail;
689b2badf02SMaxime Henrion 	}
690b2badf02SMaxime Henrion 
691b2badf02SMaxime Henrion 	/*
692b2badf02SMaxime Henrion 	 * Pre-allocate the TX DMA maps.
693b2badf02SMaxime Henrion 	 */
6944cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
695b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0,
696b2badf02SMaxime Henrion 		    &sc->fxp_desc.tx_list[i].tx_map);
697b2badf02SMaxime Henrion 		if (error) {
698b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
699b2badf02SMaxime Henrion 			goto fail;
700b2badf02SMaxime Henrion 		}
701b2badf02SMaxime Henrion 	}
702b2badf02SMaxime Henrion 	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
703b2badf02SMaxime Henrion 	if (error) {
704b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
705b2badf02SMaxime Henrion 		goto fail;
706b2badf02SMaxime Henrion 	}
707b2badf02SMaxime Henrion 
708b2badf02SMaxime Henrion 	/*
709b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
710b2badf02SMaxime Henrion 	 */
711b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
712b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
713b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
714b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
715b2badf02SMaxime Henrion 		if (error) {
716b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
717b2badf02SMaxime Henrion 			goto fail;
718b2badf02SMaxime Henrion 		}
7194953bccaSNate Lawson 		if (fxp_add_rfabuf(sc, rxp) != 0) {
7204953bccaSNate Lawson 			error = ENOMEM;
7214953bccaSNate Lawson 			goto fail;
7224953bccaSNate Lawson 		}
723b2badf02SMaxime Henrion 	}
724b2badf02SMaxime Henrion 
725b2badf02SMaxime Henrion 	/*
726f7788e8eSJonathan Lemon 	 * Read MAC address.
727f7788e8eSJonathan Lemon 	 */
72883e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
72983e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[0] = myea[0] & 0xff;
73083e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[1] = myea[0] >> 8;
73183e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[2] = myea[1] & 0xff;
73283e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[3] = myea[1] >> 8;
73383e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[4] = myea[2] & 0xff;
73483e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[5] = myea[2] >> 8;
735f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
736f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
737f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
738f7788e8eSJonathan Lemon 	if (bootverbose) {
7392e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
740f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7412e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7422e2b8238SJonathan Lemon 		    pci_get_revid(dev));
74372a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
74472a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
74572a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
746f7788e8eSJonathan Lemon 	}
747f7788e8eSJonathan Lemon 
748f7788e8eSJonathan Lemon 	/*
749f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
750f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
751f7788e8eSJonathan Lemon 	 *
752f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
753f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
754f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
755f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
756f7788e8eSJonathan Lemon 	 */
757f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
758f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
759f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
760f7788e8eSJonathan Lemon 	} else {
761f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
762f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
763f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
7646182fdbdSPeter Wemm 			error = ENXIO;
765ba8c6fd5SDavid Greenman 			goto fail;
766a17c678eSDavid Greenman 		}
767f7788e8eSJonathan Lemon 	}
768dccee1a1SDavid Greenman 
769a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
7706182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
771a17c678eSDavid Greenman 	ifp->if_name = "fxp";
772a17c678eSDavid Greenman 	ifp->if_output = ether_output;
773a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
774fb583156SDavid Greenman 	ifp->if_init = fxp_init;
775ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
776ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
777ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
778ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
779ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
780a17c678eSDavid Greenman 
781c8bca6dcSBill Paul 	/* Enable checksum offload for 82550 or better chips */
782c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
783c8bca6dcSBill Paul 		ifp->if_hwassist = FXP_CSUM_FEATURES;
784c8bca6dcSBill Paul 		ifp->if_capabilities = IFCAP_HWCSUM;
785c6d8cd1eSBill Paul 		ifp->if_capenable = ifp->if_capabilities;
786c8bca6dcSBill Paul 	}
787c8bca6dcSBill Paul 
788dfe61cf1SDavid Greenman 	/*
7894953bccaSNate Lawson 	 * Attach the interface.
7904953bccaSNate Lawson 	 */
7914953bccaSNate Lawson 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
7924953bccaSNate Lawson 
7934953bccaSNate Lawson 	/*
794e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
795e8c8b728SJonathan Lemon 	 */
796e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
797673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
798e8c8b728SJonathan Lemon 
799483b9871SDavid Greenman 	/*
8003114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8013114fdb4SDavid Greenman 	 * TX descriptors.
802483b9871SDavid Greenman 	 */
8033114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
8044a684684SDavid Greenman 
805201afb0eSMaxime Henrion 	/*
8064953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
8074953bccaSNate Lawson 	 * XXX This driver has been tested with the INTR_MPSAFFE flag set
8084953bccaSNate Lawson 	 * however, ifp and its functions are not fully locked so MPSAFE
8094953bccaSNate Lawson 	 * should not be used unless you can handle potential data loss.
810201afb0eSMaxime Henrion 	 */
8114953bccaSNate Lawson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET /*|INTR_MPSAFE*/,
812201afb0eSMaxime Henrion 			       fxp_intr, sc, &sc->ih);
813201afb0eSMaxime Henrion 	if (error) {
814201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
8154953bccaSNate Lawson 		ether_ifdetach(&sc->arpcom.ac_if);
816201afb0eSMaxime Henrion 		goto fail;
817201afb0eSMaxime Henrion 	}
818201afb0eSMaxime Henrion 
819a17c678eSDavid Greenman fail:
820f7788e8eSJonathan Lemon 	splx(s);
8214953bccaSNate Lawson 	if (error)
822f7788e8eSJonathan Lemon 		fxp_release(sc);
823f7788e8eSJonathan Lemon 	return (error);
824f7788e8eSJonathan Lemon }
825f7788e8eSJonathan Lemon 
826f7788e8eSJonathan Lemon /*
8274953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
8284953bccaSNate Lawson  * interrupt should already be torn down.
829f7788e8eSJonathan Lemon  */
830f7788e8eSJonathan Lemon static void
831f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
832f7788e8eSJonathan Lemon {
833b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
834b2badf02SMaxime Henrion 	struct fxp_tx *txp;
835b2badf02SMaxime Henrion 	int i;
836b2badf02SMaxime Henrion 
8374953bccaSNate Lawson 	mtx_assert(&sc->sc_mtx, MA_NOTOWNED);
838b983c7b3SMaxime Henrion 	if (sc->ih)
8394953bccaSNate Lawson 		panic("fxp_release() called with intr handle still active");
8404953bccaSNate Lawson 	if (sc->miibus)
8414953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
8424953bccaSNate Lawson 	bus_generic_detach(sc->dev);
8434953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
844b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
845b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
846b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
847b2badf02SMaxime Henrion 		    sc->cbl_map);
848b2badf02SMaxime Henrion 	}
849b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
850b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
851b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
852b2badf02SMaxime Henrion 	}
853b2badf02SMaxime Henrion 	if (sc->mcsp) {
854b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
855b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
856b2badf02SMaxime Henrion 	}
857f7788e8eSJonathan Lemon 	if (sc->irq)
858f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
859f7788e8eSJonathan Lemon 	if (sc->mem)
860f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
861b983c7b3SMaxime Henrion 	if (sc->fxp_mtag) {
862b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
863b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
864b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
865b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
866b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
867b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
868b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
869b983c7b3SMaxime Henrion 			}
870b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
871b983c7b3SMaxime Henrion 		}
872b983c7b3SMaxime Henrion 		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
873b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_mtag);
874b983c7b3SMaxime Henrion 	}
875b983c7b3SMaxime Henrion 	if (sc->fxp_stag) {
876b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
877b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
878b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
879b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
880b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
881b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
882b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
883b983c7b3SMaxime Henrion 			}
884b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
885b983c7b3SMaxime Henrion 		}
886b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
887b983c7b3SMaxime Henrion 	}
888b2badf02SMaxime Henrion 	if (sc->cbl_tag)
889b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
890b2badf02SMaxime Henrion 	if (sc->mcs_tag)
891b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
89272a32a26SJonathan Lemon 
89372a32a26SJonathan Lemon         sysctl_ctx_free(&sc->sysctl_ctx);
89472a32a26SJonathan Lemon 
8950f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
8966182fdbdSPeter Wemm }
8976182fdbdSPeter Wemm 
8986182fdbdSPeter Wemm /*
8996182fdbdSPeter Wemm  * Detach interface.
9006182fdbdSPeter Wemm  */
9016182fdbdSPeter Wemm static int
9026182fdbdSPeter Wemm fxp_detach(device_t dev)
9036182fdbdSPeter Wemm {
9046182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
905f7788e8eSJonathan Lemon 	int s;
9066182fdbdSPeter Wemm 
9074953bccaSNate Lawson 	FXP_LOCK(sc);
908f7788e8eSJonathan Lemon 	s = splimp();
90932cd7a9cSWarner Losh 
9101d2945d5SWarner Losh 	sc->suspended = 1;	/* Do same thing as we do for suspend */
9116182fdbdSPeter Wemm 	/*
912f7788e8eSJonathan Lemon 	 * Close down routes etc.
9136182fdbdSPeter Wemm 	 */
914673d9191SSam Leffler 	ether_ifdetach(&sc->arpcom.ac_if);
91520f0c80fSMaxime Henrion 
91620f0c80fSMaxime Henrion 	/*
91732cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
91820f0c80fSMaxime Henrion 	 */
91920f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
92020f0c80fSMaxime Henrion 	fxp_stop(sc);
92132cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
92220f0c80fSMaxime Henrion 
9236182fdbdSPeter Wemm 	/*
9244953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
9254953bccaSNate Lawson 	 * races with fxp_intr().
9266182fdbdSPeter Wemm 	 */
9274953bccaSNate Lawson 	bus_teardown_intr(sc->dev, sc->irq, sc->ih);
9284953bccaSNate Lawson 	sc->ih = NULL;
9296182fdbdSPeter Wemm 
930f7788e8eSJonathan Lemon 	splx(s);
9316182fdbdSPeter Wemm 
932f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
933f7788e8eSJonathan Lemon 	fxp_release(sc);
934f7788e8eSJonathan Lemon 	return (0);
935a17c678eSDavid Greenman }
936a17c678eSDavid Greenman 
937a17c678eSDavid Greenman /*
9384a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
939a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
940a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
941a17c678eSDavid Greenman  */
9426182fdbdSPeter Wemm static int
9436182fdbdSPeter Wemm fxp_shutdown(device_t dev)
944a17c678eSDavid Greenman {
9456182fdbdSPeter Wemm 	/*
9466182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
9476182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
9486182fdbdSPeter Wemm 	 * reboot before the driver initializes.
9496182fdbdSPeter Wemm 	 */
9506182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
951f7788e8eSJonathan Lemon 	return (0);
952a17c678eSDavid Greenman }
953a17c678eSDavid Greenman 
9547dced78aSDavid Greenman /*
9557dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
9567dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
9577dced78aSDavid Greenman  * resume.
9587dced78aSDavid Greenman  */
9597dced78aSDavid Greenman static int
9607dced78aSDavid Greenman fxp_suspend(device_t dev)
9617dced78aSDavid Greenman {
9627dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
963f7788e8eSJonathan Lemon 	int i, s;
9647dced78aSDavid Greenman 
9654953bccaSNate Lawson 	FXP_LOCK(sc);
966f7788e8eSJonathan Lemon 	s = splimp();
9677dced78aSDavid Greenman 
9687dced78aSDavid Greenman 	fxp_stop(sc);
9697dced78aSDavid Greenman 
9707dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
9717dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
9727dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
9737dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
9747dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
9757dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
9767dced78aSDavid Greenman 
9777dced78aSDavid Greenman 	sc->suspended = 1;
9787dced78aSDavid Greenman 
9794953bccaSNate Lawson 	FXP_UNLOCK(sc);
980f7788e8eSJonathan Lemon 	splx(s);
981f7788e8eSJonathan Lemon 	return (0);
9827dced78aSDavid Greenman }
9837dced78aSDavid Greenman 
9847dced78aSDavid Greenman /*
9857dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
9867dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
9877dced78aSDavid Greenman  * appropriate.
9887dced78aSDavid Greenman  */
9897dced78aSDavid Greenman static int
9907dced78aSDavid Greenman fxp_resume(device_t dev)
9917dced78aSDavid Greenman {
9927dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
9937dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
9947dced78aSDavid Greenman 	u_int16_t pci_command;
995f7788e8eSJonathan Lemon 	int i, s;
9967dced78aSDavid Greenman 
9974953bccaSNate Lawson 	FXP_LOCK(sc);
998f7788e8eSJonathan Lemon 	s = splimp();
9997dced78aSDavid Greenman 
100048e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
100148e417ebSJonathan Lemon 
10027dced78aSDavid Greenman 	/* better way to do this? */
10037dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
10047dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
10057dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
10067dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
10077dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
10087dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
10097dced78aSDavid Greenman 
10107dced78aSDavid Greenman 	/* reenable busmastering */
10117dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
10127dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
10137dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
10147dced78aSDavid Greenman 
10157dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
10167dced78aSDavid Greenman 	DELAY(10);
10177dced78aSDavid Greenman 
10187dced78aSDavid Greenman 	/* reinitialize interface if necessary */
10197dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
10204953bccaSNate Lawson 		fxp_init_body(sc);
10217dced78aSDavid Greenman 
10227dced78aSDavid Greenman 	sc->suspended = 0;
10237dced78aSDavid Greenman 
10244953bccaSNate Lawson 	FXP_UNLOCK(sc);
1025f7788e8eSJonathan Lemon 	splx(s);
1026ba8c6fd5SDavid Greenman 	return (0);
1027f7788e8eSJonathan Lemon }
1028ba8c6fd5SDavid Greenman 
102900c4116bSJonathan Lemon static void
103000c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
103100c4116bSJonathan Lemon {
103200c4116bSJonathan Lemon 	u_int16_t reg;
103300c4116bSJonathan Lemon 	int x;
103400c4116bSJonathan Lemon 
103500c4116bSJonathan Lemon 	/*
103600c4116bSJonathan Lemon 	 * Shift in data.
103700c4116bSJonathan Lemon 	 */
103800c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
103900c4116bSJonathan Lemon 		if (data & x)
104000c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
104100c4116bSJonathan Lemon 		else
104200c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
104300c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
104400c4116bSJonathan Lemon 		DELAY(1);
104500c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
104600c4116bSJonathan Lemon 		DELAY(1);
104700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
104800c4116bSJonathan Lemon 		DELAY(1);
104900c4116bSJonathan Lemon 	}
105000c4116bSJonathan Lemon }
105100c4116bSJonathan Lemon 
1052f7788e8eSJonathan Lemon /*
1053f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1054f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1055f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1056f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1057f7788e8eSJonathan Lemon  * every 16 bits of data.
1058f7788e8eSJonathan Lemon  */
1059f7788e8eSJonathan Lemon static u_int16_t
1060f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1061f7788e8eSJonathan Lemon {
1062f7788e8eSJonathan Lemon 	u_int16_t reg, data;
1063f7788e8eSJonathan Lemon 	int x;
1064ba8c6fd5SDavid Greenman 
1065f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1066f7788e8eSJonathan Lemon 	/*
1067f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1068f7788e8eSJonathan Lemon 	 */
106900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1070f7788e8eSJonathan Lemon 	/*
1071f7788e8eSJonathan Lemon 	 * Shift in address.
1072f7788e8eSJonathan Lemon 	 */
1073f7788e8eSJonathan Lemon 	data = 0;
1074f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1075f7788e8eSJonathan Lemon 		if (offset & x)
1076f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1077f7788e8eSJonathan Lemon 		else
1078f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1079f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1080f7788e8eSJonathan Lemon 		DELAY(1);
1081f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1082f7788e8eSJonathan Lemon 		DELAY(1);
1083f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1084f7788e8eSJonathan Lemon 		DELAY(1);
1085f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1086f7788e8eSJonathan Lemon 		data++;
1087f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1088f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1089f7788e8eSJonathan Lemon 			break;
1090f7788e8eSJonathan Lemon 		}
1091f7788e8eSJonathan Lemon 	}
1092f7788e8eSJonathan Lemon 	/*
1093f7788e8eSJonathan Lemon 	 * Shift out data.
1094f7788e8eSJonathan Lemon 	 */
1095f7788e8eSJonathan Lemon 	data = 0;
1096f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1097f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1098f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1099f7788e8eSJonathan Lemon 		DELAY(1);
1100f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1101f7788e8eSJonathan Lemon 			data |= x;
1102f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1103f7788e8eSJonathan Lemon 		DELAY(1);
1104f7788e8eSJonathan Lemon 	}
1105f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1106f7788e8eSJonathan Lemon 	DELAY(1);
1107f7788e8eSJonathan Lemon 
1108f7788e8eSJonathan Lemon 	return (data);
1109ba8c6fd5SDavid Greenman }
1110ba8c6fd5SDavid Greenman 
111100c4116bSJonathan Lemon static void
111200c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
111300c4116bSJonathan Lemon {
111400c4116bSJonathan Lemon 	int i;
111500c4116bSJonathan Lemon 
111600c4116bSJonathan Lemon 	/*
111700c4116bSJonathan Lemon 	 * Erase/write enable.
111800c4116bSJonathan Lemon 	 */
111900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
112000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
112100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
112200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
112300c4116bSJonathan Lemon 	DELAY(1);
112400c4116bSJonathan Lemon 	/*
112500c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
112600c4116bSJonathan Lemon 	 */
112700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
112800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
112900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
113000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
113100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
113200c4116bSJonathan Lemon 	DELAY(1);
113300c4116bSJonathan Lemon 	/*
113400c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
113500c4116bSJonathan Lemon 	 */
113600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
113700c4116bSJonathan Lemon 	DELAY(1);
113800c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
113900c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
114000c4116bSJonathan Lemon 			break;
114100c4116bSJonathan Lemon 		DELAY(50);
114200c4116bSJonathan Lemon 	}
114300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
114400c4116bSJonathan Lemon 	DELAY(1);
114500c4116bSJonathan Lemon 	/*
114600c4116bSJonathan Lemon 	 * Erase/write disable.
114700c4116bSJonathan Lemon 	 */
114800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
114900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
115000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
115100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
115200c4116bSJonathan Lemon 	DELAY(1);
115300c4116bSJonathan Lemon }
115400c4116bSJonathan Lemon 
1155ba8c6fd5SDavid Greenman /*
1156e9bf2fa7SDavid Greenman  * From NetBSD:
1157e9bf2fa7SDavid Greenman  *
1158e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1159e9bf2fa7SDavid Greenman  *
1160e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1161e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1162e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1163e9bf2fa7SDavid Greenman  *
1164e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1165e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1166e9bf2fa7SDavid Greenman  *
1167e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1168e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1169e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1170e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1171e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1172e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1173e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1174e9bf2fa7SDavid Greenman  */
1175e9bf2fa7SDavid Greenman static void
1176f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1177e9bf2fa7SDavid Greenman {
1178e9bf2fa7SDavid Greenman 
1179f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1180f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1181f7788e8eSJonathan Lemon 
1182f7788e8eSJonathan Lemon 	/* autosize */
1183f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1184e9bf2fa7SDavid Greenman }
1185f7788e8eSJonathan Lemon 
1186ba8c6fd5SDavid Greenman static void
1187f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1188ba8c6fd5SDavid Greenman {
1189f7788e8eSJonathan Lemon 	int i;
1190ba8c6fd5SDavid Greenman 
1191f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1192f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1193ba8c6fd5SDavid Greenman }
1194ba8c6fd5SDavid Greenman 
119500c4116bSJonathan Lemon static void
119600c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
119700c4116bSJonathan Lemon {
119800c4116bSJonathan Lemon 	int i;
119900c4116bSJonathan Lemon 
120000c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
120100c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
120200c4116bSJonathan Lemon }
120300c4116bSJonathan Lemon 
1204b2badf02SMaxime Henrion static void
1205b2badf02SMaxime Henrion fxp_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1206b2badf02SMaxime Henrion     bus_size_t mapsize, int error)
1207b2badf02SMaxime Henrion {
1208b2badf02SMaxime Henrion 	struct fxp_softc *sc;
1209b2badf02SMaxime Henrion 	struct fxp_cb_tx *txp;
1210b2badf02SMaxime Henrion 	int i;
1211b2badf02SMaxime Henrion 
1212b2badf02SMaxime Henrion 	if (error)
1213b2badf02SMaxime Henrion 		return;
1214b2badf02SMaxime Henrion 
1215b2badf02SMaxime Henrion 	KASSERT(nseg <= FXP_NTXSEG, ("too many DMA segments"));
1216b2badf02SMaxime Henrion 
1217b2badf02SMaxime Henrion 	sc = arg;
1218b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next->tx_cb;
1219b2badf02SMaxime Henrion 	for (i = 0; i < nseg; i++) {
1220b2badf02SMaxime Henrion 		KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large"));
1221b2badf02SMaxime Henrion 		/*
1222b2badf02SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
1223b2badf02SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
1224b2badf02SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
1225b2badf02SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
1226b2badf02SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
1227b2badf02SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
1228b2badf02SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
1229b2badf02SMaxime Henrion 		 * checksum offload control bits. So to make things work
1230b2badf02SMaxime Henrion 		 * right, we have to start filling in the TBD array
1231b2badf02SMaxime Henrion 		 * starting from a different place depending on whether
1232b2badf02SMaxime Henrion 		 * the chip is an 82550/82551 or not.
1233b2badf02SMaxime Henrion 		 */
1234b2badf02SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
123583e6547dSMaxime Henrion 			txp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
123683e6547dSMaxime Henrion 			txp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
1237b2badf02SMaxime Henrion 		} else {
123883e6547dSMaxime Henrion 			txp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
123983e6547dSMaxime Henrion 			txp->tbd[i].tb_size = htole32(segs[i].ds_len);
1240b2badf02SMaxime Henrion 		}
1241b2badf02SMaxime Henrion 	}
1242b2badf02SMaxime Henrion 	txp->tbd_number = nseg;
1243b2badf02SMaxime Henrion }
1244b2badf02SMaxime Henrion 
1245a17c678eSDavid Greenman /*
12464953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1247a17c678eSDavid Greenman  */
1248a17c678eSDavid Greenman static void
1249f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1250a17c678eSDavid Greenman {
12519b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
12524953bccaSNate Lawson 
12534953bccaSNate Lawson 	FXP_LOCK(sc);
12544953bccaSNate Lawson 	fxp_start_body(ifp);
12554953bccaSNate Lawson 	FXP_UNLOCK(sc);
12564953bccaSNate Lawson }
12574953bccaSNate Lawson 
12584953bccaSNate Lawson /*
12594953bccaSNate Lawson  * Start packet transmission on the interface.
12604953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
12614953bccaSNate Lawson  * internal entry point only.
12624953bccaSNate Lawson  */
12634953bccaSNate Lawson static void
12644953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
12654953bccaSNate Lawson {
12664953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
126750d81222SMaxime Henrion 	struct fxp_tx *txp;
1268b2badf02SMaxime Henrion 	struct mbuf *mb_head;
1269b2badf02SMaxime Henrion 	int error;
1270a17c678eSDavid Greenman 
12714953bccaSNate Lawson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
1272a17c678eSDavid Greenman 	/*
1273483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1274483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1275483b9871SDavid Greenman 	 * of the command chain).
1276a17c678eSDavid Greenman 	 */
12770f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1278a17c678eSDavid Greenman 		return;
12790f4dc94cSChuck Paterson 	}
12801cd443acSDavid Greenman 
1281483b9871SDavid Greenman 	txp = NULL;
1282483b9871SDavid Greenman 
1283483b9871SDavid Greenman 	/*
1284483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1285483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
12863114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
12873114fdb4SDavid Greenman 	 *       a NOP command when needed.
1288483b9871SDavid Greenman 	 */
12893114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1290483b9871SDavid Greenman 
1291dfe61cf1SDavid Greenman 		/*
1292dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1293dfe61cf1SDavid Greenman 		 */
12946318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
1295a17c678eSDavid Greenman 
1296dfe61cf1SDavid Greenman 		/*
1297483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1298dfe61cf1SDavid Greenman 		 */
1299b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
1300c8bca6dcSBill Paul 
1301c8bca6dcSBill Paul 		/*
1302a35e7eaaSDon Lewis 		 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1303a35e7eaaSDon Lewis 		 * Ethernet Controller Family Open Source Software
1304a35e7eaaSDon Lewis 		 * Developer Manual says:
1305a35e7eaaSDon Lewis 		 *   Using software parsing is only allowed with legal
1306a35e7eaaSDon Lewis 		 *   TCP/IP or UDP/IP packets.
1307a35e7eaaSDon Lewis 		 *   ...
1308a35e7eaaSDon Lewis 		 *   For all other datagrams, hardware parsing must
1309a35e7eaaSDon Lewis 		 *   be used.
1310a35e7eaaSDon Lewis 		 * Software parsing appears to truncate ICMP and
1311a35e7eaaSDon Lewis 		 * fragmented UDP packets that contain one to three
1312a35e7eaaSDon Lewis 		 * bytes in the second (and final) mbuf of the packet.
1313a35e7eaaSDon Lewis 		 */
1314a35e7eaaSDon Lewis 		if (sc->flags & FXP_FLAG_EXT_RFA)
1315a35e7eaaSDon Lewis 			txp->tx_cb->ipcb_ip_activation_high =
1316a35e7eaaSDon Lewis 			    FXP_IPCB_HARDWAREPARSING_ENABLE;
1317a35e7eaaSDon Lewis 
1318a35e7eaaSDon Lewis 		/*
1319c8bca6dcSBill Paul 		 * Deal with TCP/IP checksum offload. Note that
1320c8bca6dcSBill Paul 		 * in order for TCP checksum offload to work,
1321c8bca6dcSBill Paul 		 * the pseudo header checksum must have already
1322c8bca6dcSBill Paul 		 * been computed and stored in the checksum field
1323c8bca6dcSBill Paul 		 * in the TCP header. The stack should have
1324c8bca6dcSBill Paul 		 * already done this for us.
1325c8bca6dcSBill Paul 		 */
1326c8bca6dcSBill Paul 
1327c8bca6dcSBill Paul 		if (mb_head->m_pkthdr.csum_flags) {
1328c8bca6dcSBill Paul 			if (mb_head->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1329b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_schedule =
1330c8bca6dcSBill Paul 				    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1331c8bca6dcSBill Paul 				if (mb_head->m_pkthdr.csum_flags & CSUM_TCP)
1332b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_schedule |=
1333c8bca6dcSBill Paul 					    FXP_IPCB_TCP_PACKET;
1334c8bca6dcSBill Paul 			}
1335c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
1336c8bca6dcSBill Paul 		/*
1337c8bca6dcSBill Paul 		 * XXX The 82550 chip appears to have trouble
1338c8bca6dcSBill Paul 		 * dealing with IP header checksums in very small
1339c8bca6dcSBill Paul 		 * datagrams, namely fragments from 1 to 3 bytes
1340c8bca6dcSBill Paul 		 * in size. For example, say you want to transmit
1341c8bca6dcSBill Paul 		 * a UDP packet of 1473 bytes. The packet will be
1342c8bca6dcSBill Paul 		 * fragmented over two IP datagrams, the latter
1343c8bca6dcSBill Paul 		 * containing only one byte of data. The 82550 will
1344c8bca6dcSBill Paul 		 * botch the header checksum on the 1-byte fragment.
1345c8bca6dcSBill Paul 		 * As long as the datagram contains 4 or more bytes
1346c8bca6dcSBill Paul 		 * of data, you're ok.
1347c8bca6dcSBill Paul 		 *
1348c8bca6dcSBill Paul                  * The following code attempts to work around this
1349c8bca6dcSBill Paul 		 * problem: if the datagram is less than 38 bytes
1350c8bca6dcSBill Paul 		 * in size (14 bytes ether header, 20 bytes IP header,
1351c8bca6dcSBill Paul 		 * plus 4 bytes of data), we punt and compute the IP
1352c8bca6dcSBill Paul 		 * header checksum by hand. This workaround doesn't
1353c8bca6dcSBill Paul 		 * work very well, however, since it can be fooled
1354c8bca6dcSBill Paul 		 * by things like VLAN tags and IP options that make
1355c8bca6dcSBill Paul 		 * the header sizes/offsets vary.
1356c8bca6dcSBill Paul 		 */
1357c8bca6dcSBill Paul 
1358c8bca6dcSBill Paul 			if (mb_head->m_pkthdr.csum_flags & CSUM_IP) {
1359c8bca6dcSBill Paul 				if (mb_head->m_pkthdr.len < 38) {
1360c8bca6dcSBill Paul 					struct ip *ip;
1361c8bca6dcSBill Paul 					mb_head->m_data += ETHER_HDR_LEN;
1362c8bca6dcSBill Paul 					ip = mtod(mb_head, struct ip *);
1363c8bca6dcSBill Paul 					ip->ip_sum = in_cksum(mb_head,
1364c8bca6dcSBill Paul 					    ip->ip_hl << 2);
1365c8bca6dcSBill Paul 					mb_head->m_data -= ETHER_HDR_LEN;
1366c8bca6dcSBill Paul 				} else {
1367b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_activation_high =
1368c8bca6dcSBill Paul 					    FXP_IPCB_HARDWAREPARSING_ENABLE;
1369b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_schedule |=
1370c8bca6dcSBill Paul 					    FXP_IPCB_IP_CHECKSUM_ENABLE;
1371c8bca6dcSBill Paul 				}
1372c8bca6dcSBill Paul 			}
1373c8bca6dcSBill Paul #endif
1374c8bca6dcSBill Paul 		}
1375c8bca6dcSBill Paul 
1376c8bca6dcSBill Paul 		/*
1377a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1378483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1379a17c678eSDavid Greenman 		 * and size of the mbuf.
1380a17c678eSDavid Greenman 		 */
1381b2badf02SMaxime Henrion 		error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1382b2badf02SMaxime Henrion 		    mb_head, fxp_dma_map_txbuf, sc, 0);
1383b2badf02SMaxime Henrion 
1384b2badf02SMaxime Henrion 		if (error && error != EFBIG) {
1385b2badf02SMaxime Henrion 			device_printf(sc->dev, "can't map mbuf (error %d)\n",
1386b2badf02SMaxime Henrion 			    error);
1387b2badf02SMaxime Henrion 			m_freem(mb_head);
1388a17c678eSDavid Greenman 			break;
1389a17c678eSDavid Greenman 		}
1390b2badf02SMaxime Henrion 
1391b2badf02SMaxime Henrion 		if (error) {
139223a0ed7cSDavid Greenman 			struct mbuf *mn;
139323a0ed7cSDavid Greenman 
1394a17c678eSDavid Greenman 			/*
13953bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
13963bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
13973bd07cfdSJonathan Lemon 			 * new buffers.
1398a17c678eSDavid Greenman 			 */
13991104779bSMike Silbersack 			mn = m_defrag(mb_head, M_DONTWAIT);
140023a0ed7cSDavid Greenman 			if (mn == NULL) {
140123a0ed7cSDavid Greenman 				m_freem(mb_head);
1402483b9871SDavid Greenman 				break;
14031104779bSMike Silbersack 			} else {
140423a0ed7cSDavid Greenman 				mb_head = mn;
14051104779bSMike Silbersack 			}
1406b2badf02SMaxime Henrion 			error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1407b2badf02SMaxime Henrion 			    mb_head, fxp_dma_map_txbuf, sc, 0);
1408b2badf02SMaxime Henrion 			if (error) {
1409b2badf02SMaxime Henrion 				device_printf(sc->dev,
1410b2badf02SMaxime Henrion 				    "can't map mbuf (error %d)\n", error);
1411b2badf02SMaxime Henrion 				m_freem(mb_head);
1412b2badf02SMaxime Henrion 				break;
1413b2badf02SMaxime Henrion 			}
141423a0ed7cSDavid Greenman 		}
141523a0ed7cSDavid Greenman 
1416b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1417b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
1418b2badf02SMaxime Henrion 
1419b2badf02SMaxime Henrion 		txp->tx_mbuf = mb_head;
1420b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
1421b2badf02SMaxime Henrion 		txp->tx_cb->byte_count = 0;
14223114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1423b2badf02SMaxime Henrion 			txp->tx_cb->cb_command =
142483e6547dSMaxime Henrion 			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
142583e6547dSMaxime Henrion 			    FXP_CB_COMMAND_S);
14263114fdb4SDavid Greenman 		} else {
1427b2badf02SMaxime Henrion 			txp->tx_cb->cb_command =
142883e6547dSMaxime Henrion 			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
142983e6547dSMaxime Henrion 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
14303114fdb4SDavid Greenman 			/*
14313bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
14323bd07cfdSJonathan Lemon 			 * from the card again.
14333114fdb4SDavid Greenman 			 */
14343114fdb4SDavid Greenman 			ifp->if_timer = 5;
14353114fdb4SDavid Greenman 		}
1436b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1437a17c678eSDavid Greenman 
1438a17c678eSDavid Greenman 		/*
1439483b9871SDavid Greenman 		 * Advance the end of list forward.
1440a17c678eSDavid Greenman 		 */
144106175228SAndrew Gallatin 
144250d81222SMaxime Henrion #ifdef __alpha__
144306175228SAndrew Gallatin 		/*
144406175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
144506175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
144606175228SAndrew Gallatin 		 * up the status while we update the command field.
144706175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
144814fd1071SMaxime Henrion 		 * XXX This is probably bogus and we're _not_ looking
144914fd1071SMaxime Henrion 		 * for atomicity here.
145006175228SAndrew Gallatin 		 */
145114fd1071SMaxime Henrion 		atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1452bafb64afSMaxime Henrion 		    htole16(FXP_CB_COMMAND_S));
145350d81222SMaxime Henrion #else
1454bafb64afSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
1455bafb64afSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
145650d81222SMaxime Henrion #endif /*__alpha__*/
1457b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
1458a17c678eSDavid Greenman 
1459a17c678eSDavid Greenman 		/*
14601cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
1461b2badf02SMaxime Henrion 		 * no other packets queued (when nothing is queued, tx_first
1462483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1463a17c678eSDavid Greenman 		 */
14641cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1465b2badf02SMaxime Henrion 			sc->fxp_desc.tx_first = txp;
1466a17c678eSDavid Greenman 
14671cd443acSDavid Greenman 		sc->tx_queued++;
14681cd443acSDavid Greenman 
1469a17c678eSDavid Greenman 		/*
1470a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1471a17c678eSDavid Greenman 		 */
1472673d9191SSam Leffler 		BPF_MTAP(ifp, mb_head);
1473483b9871SDavid Greenman 	}
1474b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1475483b9871SDavid Greenman 
1476483b9871SDavid Greenman 	/*
1477483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1478483b9871SDavid Greenman 	 * going again if suspended.
1479483b9871SDavid Greenman 	 */
1480483b9871SDavid Greenman 	if (txp != NULL) {
1481483b9871SDavid Greenman 		fxp_scb_wait(sc);
14822e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1483483b9871SDavid Greenman 	}
1484a17c678eSDavid Greenman }
1485a17c678eSDavid Greenman 
1486e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1487e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1488e4fc250cSLuigi Rizzo 
1489e4fc250cSLuigi Rizzo static void
1490e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1491e4fc250cSLuigi Rizzo {
1492e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
1493e4fc250cSLuigi Rizzo 	u_int8_t statack;
1494e4fc250cSLuigi Rizzo 
14954953bccaSNate Lawson 	FXP_LOCK(sc);
1496e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1497e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
14984953bccaSNate Lawson 		FXP_UNLOCK(sc);
1499e4fc250cSLuigi Rizzo 		return;
1500e4fc250cSLuigi Rizzo 	}
1501e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1502e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1503e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
1504e4fc250cSLuigi Rizzo 		u_int8_t tmp;
15056481f301SPeter Wemm 
1506e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
15074953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
15084953bccaSNate Lawson 			FXP_UNLOCK(sc);
1509e4fc250cSLuigi Rizzo 			return; /* nothing to do */
15104953bccaSNate Lawson 		}
1511e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1512e4fc250cSLuigi Rizzo 		/* ack what we can */
1513e4fc250cSLuigi Rizzo 		if (tmp != 0)
1514e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1515e4fc250cSLuigi Rizzo 		statack |= tmp;
1516e4fc250cSLuigi Rizzo 	}
15174953bccaSNate Lawson 	fxp_intr_body(sc, ifp, statack, count);
15184953bccaSNate Lawson 	FXP_UNLOCK(sc);
1519e4fc250cSLuigi Rizzo }
1520e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1521e4fc250cSLuigi Rizzo 
1522a17c678eSDavid Greenman /*
15239c7d2607SDavid Greenman  * Process interface interrupts.
1524a17c678eSDavid Greenman  */
152594927790SDavid Greenman static void
1526f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1527a17c678eSDavid Greenman {
1528f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
15294953bccaSNate Lawson 	struct ifnet *ifp = &sc->sc_if;
15301cd443acSDavid Greenman 	u_int8_t statack;
15310f4dc94cSChuck Paterson 
15324953bccaSNate Lawson 	FXP_LOCK(sc);
1533704d1965SWarner Losh 	if (sc->suspended) {
1534704d1965SWarner Losh 		FXP_UNLOCK(sc);
1535704d1965SWarner Losh 		return;
1536704d1965SWarner Losh 	}
1537704d1965SWarner Losh 
1538e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
15394953bccaSNate Lawson 	if (ifp->if_flags & IFF_POLLING) {
15404953bccaSNate Lawson 		FXP_UNLOCK(sc);
1541e4fc250cSLuigi Rizzo 		return;
15424953bccaSNate Lawson 	}
1543e4fc250cSLuigi Rizzo 	if (ether_poll_register(fxp_poll, ifp)) {
1544e4fc250cSLuigi Rizzo 		/* disable interrupts */
1545e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
1546e4fc250cSLuigi Rizzo 		fxp_poll(ifp, 0, 1);
15474953bccaSNate Lawson 		FXP_UNLOCK(sc);
1548e4fc250cSLuigi Rizzo 		return;
1549e4fc250cSLuigi Rizzo 	}
1550e4fc250cSLuigi Rizzo #endif
1551b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1552a17c678eSDavid Greenman 		/*
155311457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
155411457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
155511457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
155611457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
155711457bbfSJonathan Lemon 		 */
15584953bccaSNate Lawson 		if (statack == 0xff) {
15594953bccaSNate Lawson 			FXP_UNLOCK(sc);
156011457bbfSJonathan Lemon 			return;
15614953bccaSNate Lawson 		}
156211457bbfSJonathan Lemon 
156311457bbfSJonathan Lemon 		/*
1564a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1565a17c678eSDavid Greenman 		 */
1566ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
15674953bccaSNate Lawson 		fxp_intr_body(sc, ifp, statack, -1);
1568e4fc250cSLuigi Rizzo 	}
15694953bccaSNate Lawson 	FXP_UNLOCK(sc);
1570e4fc250cSLuigi Rizzo }
1571e4fc250cSLuigi Rizzo 
1572e4fc250cSLuigi Rizzo static void
1573b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1574b2badf02SMaxime Henrion {
1575b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1576b2badf02SMaxime Henrion 
1577b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1578b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
157983e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1580b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1581b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1582b2badf02SMaxime Henrion 			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1583b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1584b2badf02SMaxime Henrion 			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1585b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1586b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1587b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1588b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1589b2badf02SMaxime Henrion 		}
1590b2badf02SMaxime Henrion 		sc->tx_queued--;
1591b2badf02SMaxime Henrion 	}
1592b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1593b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1594b2badf02SMaxime Henrion }
1595b2badf02SMaxime Henrion 
1596b2badf02SMaxime Henrion static void
15974953bccaSNate Lawson fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, u_int8_t statack,
15984953bccaSNate Lawson     int count)
1599e4fc250cSLuigi Rizzo {
16002b5989e9SLuigi Rizzo 	struct mbuf *m;
1601b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
16022b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
16032b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
16042b5989e9SLuigi Rizzo 
16054953bccaSNate Lawson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
16062b5989e9SLuigi Rizzo 	if (rnr)
16072b5989e9SLuigi Rizzo 		fxp_rnr++;
1608947e3815SIan Dowse #ifdef DEVICE_POLLING
1609947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1610947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1611947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1612947e3815SIan Dowse 		rnr = 1;
1613947e3815SIan Dowse 	}
1614947e3815SIan Dowse #endif
1615a17c678eSDavid Greenman 
1616a17c678eSDavid Greenman 	/*
16173114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
161806936301SBill Paul 	 *
161906936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
162006936301SBill Paul 	 * be that this event (control unit not ready) was not
162106936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
162206936301SBill Paul 	 * The exact sequence of events that occur when the interface
162306936301SBill Paul 	 * is brought up are different now, and if this event
162406936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
162506936301SBill Paul 	 * can stall for several seconds. The result is that no
162606936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
162706936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
16283114fdb4SDavid Greenman 	 */
162906936301SBill Paul 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
1630b2badf02SMaxime Henrion 		fxp_txeof(sc);
16313114fdb4SDavid Greenman 
163241aa0ba2SLuigi Rizzo 		ifp->if_timer = 0;
1633e2102ae4SMike Silbersack 		if (sc->tx_queued == 0) {
16343114fdb4SDavid Greenman 			if (sc->need_mcsetup)
16353114fdb4SDavid Greenman 				fxp_mc_setup(sc);
1636e2102ae4SMike Silbersack 		}
16373114fdb4SDavid Greenman 		/*
16383114fdb4SDavid Greenman 		 * Try to start more packets transmitting.
16393114fdb4SDavid Greenman 		 */
16403114fdb4SDavid Greenman 		if (ifp->if_snd.ifq_head != NULL)
16414953bccaSNate Lawson 			fxp_start_body(ifp);
16423114fdb4SDavid Greenman 	}
16432b5989e9SLuigi Rizzo 
16442b5989e9SLuigi Rizzo 	/*
16452b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
16462b5989e9SLuigi Rizzo 	 */
1647947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
16482b5989e9SLuigi Rizzo 		return;
16492b5989e9SLuigi Rizzo 
16503114fdb4SDavid Greenman 	/*
1651a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1652a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1653a17c678eSDavid Greenman 	 * re-start the receiver.
1654947e3815SIan Dowse 	 *
16552b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
16562b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
16572b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
16582b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1659947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1660947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1661a17c678eSDavid Greenman 	 */
16622b5989e9SLuigi Rizzo 	for (;;) {
1663b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1664b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1665ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1666ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1667b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1668b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1669a17c678eSDavid Greenman 
1670e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1671947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1672947e3815SIan Dowse 			if (rnr) {
1673947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1674947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1675947e3815SIan Dowse 				rnr = 0;
1676947e3815SIan Dowse 			}
16772b5989e9SLuigi Rizzo 			break;
1678947e3815SIan Dowse 		}
16792b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
16802b5989e9SLuigi Rizzo 
168183e6547dSMaxime Henrion 		if ((le16toh(rfa->rfa_status) & FXP_RFA_STATUS_C) == 0)
16822b5989e9SLuigi Rizzo 			break;
16832b5989e9SLuigi Rizzo 
1684dfe61cf1SDavid Greenman 		/*
1685b2badf02SMaxime Henrion 		 * Advance head forward.
1686dfe61cf1SDavid Greenman 		 */
1687b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1688a17c678eSDavid Greenman 
1689dfe61cf1SDavid Greenman 		/*
1690ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1691ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1692ba8c6fd5SDavid Greenman 		 * instead.
1693dfe61cf1SDavid Greenman 		 */
1694b2badf02SMaxime Henrion 		if (fxp_add_rfabuf(sc, rxp) == 0) {
1695aed53495SDavid Greenman 			int total_len;
1696a17c678eSDavid Greenman 
1697e8c8b728SJonathan Lemon 			/*
16982b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
16992b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
17002b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
17012b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1702e8c8b728SJonathan Lemon 			 */
1703bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
17042b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
17052b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
1706b2badf02SMaxime Henrion 				sc->rfa_size ||
170783e6547dSMaxime Henrion 			    le16toh(rfa->rfa_status) & FXP_RFA_STATUS_CRC) {
1708e8c8b728SJonathan Lemon 				m_freem(m);
17092b5989e9SLuigi Rizzo 				continue;
1710e8c8b728SJonathan Lemon 			}
1711920b58e8SBrooks Davis 
1712c8bca6dcSBill Paul                         /* Do IP checksum checking. */
171383e6547dSMaxime Henrion 			if (le16toh(rfa->rfa_status) & FXP_RFA_STATUS_PARSE) {
1714c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1715c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1716c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1717c8bca6dcSBill Paul 					    CSUM_IP_CHECKED;
1718c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1719c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_VALID)
1720c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1721c8bca6dcSBill Paul 					    CSUM_IP_VALID;
1722c8bca6dcSBill Paul 				if ((rfa->rfax_csum_sts &
1723c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1724c8bca6dcSBill Paul 				    (rfa->rfax_csum_sts &
1725c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1726c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1727c8bca6dcSBill Paul 					    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1728c8bca6dcSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1729c8bca6dcSBill Paul 				}
1730c8bca6dcSBill Paul 			}
1731c8bca6dcSBill Paul 
17322e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1733673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1734673d9191SSam Leffler 
1735673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
1736a17c678eSDavid Greenman 		}
1737a17c678eSDavid Greenman 	}
17382b5989e9SLuigi Rizzo 	if (rnr) {
1739ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1740ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1741b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
17422e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1743a17c678eSDavid Greenman 	}
1744a17c678eSDavid Greenman }
1745a17c678eSDavid Greenman 
1746dfe61cf1SDavid Greenman /*
1747dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1748dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1749dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1750dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1751dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1752dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1753dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1754dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1755dfe61cf1SDavid Greenman  * them again next time.
1756dfe61cf1SDavid Greenman  */
1757303b270bSEivind Eklund static void
1758f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1759a17c678eSDavid Greenman {
1760f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1761ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1762a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1763f7788e8eSJonathan Lemon 	int s;
1764a17c678eSDavid Greenman 
17654953bccaSNate Lawson 	FXP_LOCK(sc);
17664953bccaSNate Lawson 	s = splimp();
1767b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
176883e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
176983e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1770397f9dfeSDavid Greenman 	if (sp->rx_good) {
177183e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
1772397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1773397f9dfeSDavid Greenman 	} else {
1774c8cc6fcaSDavid Greenman 		/*
1775c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1776c8cc6fcaSDavid Greenman 		 */
1777397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1778397f9dfeSDavid Greenman 	}
17793ba65732SDavid Greenman 	ifp->if_ierrors +=
178083e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
178183e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
178283e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
178383e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
1784a17c678eSDavid Greenman 	/*
1785f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1786f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1787f9be9005SDavid Greenman 	 */
1788f9be9005SDavid Greenman 	if (sp->tx_underruns) {
178983e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
1790f9be9005SDavid Greenman 		if (tx_threshold < 192)
1791f9be9005SDavid Greenman 			tx_threshold += 64;
1792f9be9005SDavid Greenman 	}
17934953bccaSNate Lawson 
1794397f9dfeSDavid Greenman 	/*
1795c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1796c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1797c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1798c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1799c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1800c8cc6fcaSDavid Greenman 	 */
1801b2badf02SMaxime Henrion 	fxp_txeof(sc);
1802b2badf02SMaxime Henrion 
1803c8cc6fcaSDavid Greenman 	/*
1804397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1805397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1806397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1807397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1808397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1809397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1810397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1811397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1812397f9dfeSDavid Greenman 	 */
1813397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1814397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1815397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1816397f9dfeSDavid Greenman 	}
1817f9be9005SDavid Greenman 	/*
18183ba65732SDavid Greenman 	 * If there is no pending command, start another stats
18193ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1820a17c678eSDavid Greenman 	 */
1821397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1822a17c678eSDavid Greenman 		/*
1823397f9dfeSDavid Greenman 		 * Start another stats dump.
1824a17c678eSDavid Greenman 		 */
1825b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
1826b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
18272e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1828dfe61cf1SDavid Greenman 	} else {
1829dfe61cf1SDavid Greenman 		/*
1830dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1831dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
18323ba65732SDavid Greenman 		 * next timer event to update them.
1833dfe61cf1SDavid Greenman 		 */
1834dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1835f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1836dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
18373ba65732SDavid Greenman 
1838dfe61cf1SDavid Greenman 		sp->rx_good = 0;
18393ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
18403ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
18413ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
18423ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1843dfe61cf1SDavid Greenman 	}
1844f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1845f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
18464953bccaSNate Lawson 
1847a17c678eSDavid Greenman 	/*
1848a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1849a17c678eSDavid Greenman 	 */
1850f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
18514953bccaSNate Lawson 	FXP_UNLOCK(sc);
18524953bccaSNate Lawson 	splx(s);
1853a17c678eSDavid Greenman }
1854a17c678eSDavid Greenman 
1855a17c678eSDavid Greenman /*
1856a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1857a17c678eSDavid Greenman  * the interface.
1858a17c678eSDavid Greenman  */
1859a17c678eSDavid Greenman static void
1860f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1861a17c678eSDavid Greenman {
1862ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1863b2badf02SMaxime Henrion 	struct fxp_tx *txp;
18643ba65732SDavid Greenman 	int i;
1865a17c678eSDavid Greenman 
18667dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
18677dced78aSDavid Greenman 	ifp->if_timer = 0;
18687dced78aSDavid Greenman 
1869e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1870e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
1871e4fc250cSLuigi Rizzo #endif
1872a17c678eSDavid Greenman 	/*
1873a17c678eSDavid Greenman 	 * Cancel stats updater.
1874a17c678eSDavid Greenman 	 */
1875f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
18763ba65732SDavid Greenman 
18773ba65732SDavid Greenman 	/*
187872a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
18793ba65732SDavid Greenman 	 */
188072a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
188109882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
188272a32a26SJonathan Lemon 	DELAY(50);
1883a17c678eSDavid Greenman 
18843ba65732SDavid Greenman 	/*
18853ba65732SDavid Greenman 	 * Release any xmit buffers.
18863ba65732SDavid Greenman 	 */
1887b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
1888da91462dSDavid Greenman 	if (txp != NULL) {
1889da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1890b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
1891b2badf02SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
1892b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
1893b2badf02SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
1894b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
1895b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
1896c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
1897b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
1898da91462dSDavid Greenman 			}
1899da91462dSDavid Greenman 		}
19003ba65732SDavid Greenman 	}
1901b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
19023ba65732SDavid Greenman 	sc->tx_queued = 0;
1903a17c678eSDavid Greenman }
1904a17c678eSDavid Greenman 
1905a17c678eSDavid Greenman /*
1906a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1907a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1908a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1909a17c678eSDavid Greenman  * card has wedged for some reason.
1910a17c678eSDavid Greenman  */
1911a17c678eSDavid Greenman static void
1912f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1913a17c678eSDavid Greenman {
1914ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1915ba8c6fd5SDavid Greenman 
19164953bccaSNate Lawson 	FXP_LOCK(sc);
1917f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
19184a5f1499SDavid Greenman 	ifp->if_oerrors++;
1919a17c678eSDavid Greenman 
19204953bccaSNate Lawson 	fxp_init_body(sc);
19214953bccaSNate Lawson 	FXP_UNLOCK(sc);
1922a17c678eSDavid Greenman }
1923a17c678eSDavid Greenman 
19244953bccaSNate Lawson /*
19254953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
19264953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
19274953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
19284953bccaSNate Lawson  */
1929a17c678eSDavid Greenman static void
1930f7788e8eSJonathan Lemon fxp_init(void *xsc)
1931a17c678eSDavid Greenman {
1932fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
19334953bccaSNate Lawson 
19344953bccaSNate Lawson 	FXP_LOCK(sc);
19354953bccaSNate Lawson 	fxp_init_body(sc);
19364953bccaSNate Lawson 	FXP_UNLOCK(sc);
19374953bccaSNate Lawson }
19384953bccaSNate Lawson 
19394953bccaSNate Lawson /*
19404953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
19414953bccaSNate Lawson  * softc lock held.
19424953bccaSNate Lawson  */
19434953bccaSNate Lawson static void
19444953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
19454953bccaSNate Lawson {
1946ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1947a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1948a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1949b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
1950b2badf02SMaxime Henrion 	struct fxp_tx *txp;
195109882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
1952f7788e8eSJonathan Lemon 	int i, prm, s;
1953a17c678eSDavid Greenman 
19544953bccaSNate Lawson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
1955f7788e8eSJonathan Lemon 	s = splimp();
1956a17c678eSDavid Greenman 	/*
19573ba65732SDavid Greenman 	 * Cancel any pending I/O
1958a17c678eSDavid Greenman 	 */
19593ba65732SDavid Greenman 	fxp_stop(sc);
1960a17c678eSDavid Greenman 
1961a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1962a17c678eSDavid Greenman 
1963a17c678eSDavid Greenman 	/*
1964a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1965a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1966a17c678eSDavid Greenman 	 */
1967ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
19682e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1969a17c678eSDavid Greenman 
1970ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
19712e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1972a17c678eSDavid Greenman 
1973a17c678eSDavid Greenman 	/*
1974a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1975a17c678eSDavid Greenman 	 */
1976ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1977b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
1978b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
19792e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1980a17c678eSDavid Greenman 
1981a17c678eSDavid Greenman 	/*
198272a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
198372a32a26SJonathan Lemon 	 */
198472a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
198572a32a26SJonathan Lemon 		fxp_load_ucode(sc);
198672a32a26SJonathan Lemon 
198772a32a26SJonathan Lemon 	/*
198809882363SJonathan Lemon 	 * Initialize the multicast address list.
198909882363SJonathan Lemon 	 */
199009882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
199109882363SJonathan Lemon 		mcsp = sc->mcsp;
199209882363SJonathan Lemon 		mcsp->cb_status = 0;
199383e6547dSMaxime Henrion 		mcsp->cb_command =
199483e6547dSMaxime Henrion 		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
199583e6547dSMaxime Henrion 		mcsp->link_addr = 0xffffffff;
199609882363SJonathan Lemon 		/*
199709882363SJonathan Lemon 	 	 * Start the multicast setup command.
199809882363SJonathan Lemon 		 */
199909882363SJonathan Lemon 		fxp_scb_wait(sc);
2000b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2001b2badf02SMaxime Henrion 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
200209882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
200309882363SJonathan Lemon 		/* ...and wait for it to complete. */
2004209b07bcSMaxime Henrion 		fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
2005b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
2006b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
200709882363SJonathan Lemon 	}
200809882363SJonathan Lemon 
200909882363SJonathan Lemon 	/*
2010a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2011a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2012a17c678eSDavid Greenman 	 * later.
2013a17c678eSDavid Greenman 	 */
2014b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2015a17c678eSDavid Greenman 
2016a17c678eSDavid Greenman 	/*
2017a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2018a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2019a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2020a17c678eSDavid Greenman 	 */
2021b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2022a17c678eSDavid Greenman 
2023a17c678eSDavid Greenman 	cbp->cb_status =	0;
202483e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
202583e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
202683e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
20272c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2028001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2029001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2030a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2031f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2032f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2033f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2034f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2035001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2036001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2037f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2038a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2039f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2040f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
20413114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2042f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2043f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2044f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
204572a32a26SJonathan Lemon 	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
2046a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2047f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2048f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2049f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
2050c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2051f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2052f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2053f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
2054f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2055f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2056f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2057f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2058a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2059a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2060a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2061a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2062a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2063a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2064a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2065a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2066f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2067f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2068f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2069f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2070f7788e8eSJonathan Lemon 
2071a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2072a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2073a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2074f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2075f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
2076f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
2077f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
2078a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
20793ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2080a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2081f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
2082c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2083a17c678eSDavid Greenman 
208498b27888SRobert Watson 	if (fxp_noflow || sc->revision == FXP_REV_82557) {
20853bd07cfdSJonathan Lemon 		/*
20863bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
20873bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
20883bd07cfdSJonathan Lemon 		 */
20893bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
20903bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
20913bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
20923bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
20933bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
20943bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
20953bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
20963bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
20973bd07cfdSJonathan Lemon 	} else {
20983bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
20993bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
21003bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
21013bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
21023bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
21033bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
21043bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
21053bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
21063bd07cfdSJonathan Lemon 	}
21073bd07cfdSJonathan Lemon 
2108a17c678eSDavid Greenman 	/*
2109a17c678eSDavid Greenman 	 * Start the config command/DMA.
2110a17c678eSDavid Greenman 	 */
2111ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2112b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2113b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
21142e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2115a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2116209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2117b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2118a17c678eSDavid Greenman 
2119a17c678eSDavid Greenman 	/*
2120a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2121a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2122a17c678eSDavid Greenman 	 */
2123b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2124a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
212583e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
212683e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
2127e609b4d7SMaxime Henrion 	bcopy(sc->arpcom.ac_enaddr, cb_ias->macaddr,
2128a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
2129a17c678eSDavid Greenman 
2130a17c678eSDavid Greenman 	/*
2131a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2132a17c678eSDavid Greenman 	 */
2133ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2134b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
21352e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2136a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2137209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2138b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2139a17c678eSDavid Greenman 
2140a17c678eSDavid Greenman 	/*
2141a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2142a17c678eSDavid Greenman 	 */
2143b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2144b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2145b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2146a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2147b2badf02SMaxime Henrion 		txp[i].tx_cb = tcbp + i;
2148b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
214983e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
215083e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
215183e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
215283e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
21533bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2154b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
215583e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
21563bd07cfdSJonathan Lemon 		else
2157b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
215883e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2159b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2160a17c678eSDavid Greenman 	}
2161a17c678eSDavid Greenman 	/*
2162397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2163a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2164a17c678eSDavid Greenman 	 */
216583e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2166b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2167b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2168397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2169a17c678eSDavid Greenman 
2170ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
21712e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2172a17c678eSDavid Greenman 
2173a17c678eSDavid Greenman 	/*
2174a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2175a17c678eSDavid Greenman 	 */
2176ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2177b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
21782e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2179a17c678eSDavid Greenman 
2180dccee1a1SDavid Greenman 	/*
2181ba8c6fd5SDavid Greenman 	 * Set current media.
2182dccee1a1SDavid Greenman 	 */
2183f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2184f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2185dccee1a1SDavid Greenman 
2186a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
2187a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
2188e8c8b728SJonathan Lemon 
2189e8c8b728SJonathan Lemon 	/*
2190e8c8b728SJonathan Lemon 	 * Enable interrupts.
2191e8c8b728SJonathan Lemon 	 */
21922b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
21932b5989e9SLuigi Rizzo 	/*
21942b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
21952b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
21962b5989e9SLuigi Rizzo 	 */
219762f76486SMaxim Sobolev 	if ( ifp->if_flags & IFF_POLLING )
21982b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
21992b5989e9SLuigi Rizzo 	else
22002b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2201e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2202a17c678eSDavid Greenman 
2203a17c678eSDavid Greenman 	/*
2204a17c678eSDavid Greenman 	 * Start stats updater.
2205a17c678eSDavid Greenman 	 */
2206f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
22074953bccaSNate Lawson 	splx(s);
2208f7788e8eSJonathan Lemon }
2209f7788e8eSJonathan Lemon 
2210f7788e8eSJonathan Lemon static int
2211f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2212f7788e8eSJonathan Lemon {
2213f7788e8eSJonathan Lemon 
2214f7788e8eSJonathan Lemon 	return (0);
2215a17c678eSDavid Greenman }
2216a17c678eSDavid Greenman 
2217303b270bSEivind Eklund static void
2218f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2219ba8c6fd5SDavid Greenman {
2220ba8c6fd5SDavid Greenman 
2221f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2222ba8c6fd5SDavid Greenman }
2223ba8c6fd5SDavid Greenman 
2224ba8c6fd5SDavid Greenman /*
2225ba8c6fd5SDavid Greenman  * Change media according to request.
2226ba8c6fd5SDavid Greenman  */
2227f7788e8eSJonathan Lemon static int
2228f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2229ba8c6fd5SDavid Greenman {
2230ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2231f7788e8eSJonathan Lemon 	struct mii_data *mii;
2232ba8c6fd5SDavid Greenman 
2233f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
2234f7788e8eSJonathan Lemon 	mii_mediachg(mii);
2235ba8c6fd5SDavid Greenman 	return (0);
2236ba8c6fd5SDavid Greenman }
2237ba8c6fd5SDavid Greenman 
2238ba8c6fd5SDavid Greenman /*
2239ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2240ba8c6fd5SDavid Greenman  */
2241f7788e8eSJonathan Lemon static void
2242f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2243ba8c6fd5SDavid Greenman {
2244ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2245f7788e8eSJonathan Lemon 	struct mii_data *mii;
2246ba8c6fd5SDavid Greenman 
2247f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
2248f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2249f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2250f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
22512e2b8238SJonathan Lemon 
22522e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
22532e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
22542e2b8238SJonathan Lemon 	else
22552e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
2256ba8c6fd5SDavid Greenman }
2257ba8c6fd5SDavid Greenman 
2258a17c678eSDavid Greenman /*
2259a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2260a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
2261a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
2262dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
2263a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2264a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2265a17c678eSDavid Greenman  */
2266a17c678eSDavid Greenman static int
2267b2badf02SMaxime Henrion fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2268a17c678eSDavid Greenman {
2269a17c678eSDavid Greenman 	struct mbuf *m;
2270a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
2271b2badf02SMaxime Henrion 	struct fxp_rx *p_rx;
2272b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
2273b2badf02SMaxime Henrion 	int error;
2274a17c678eSDavid Greenman 
2275a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2276b2badf02SMaxime Henrion 	if (m == NULL)
2277b2badf02SMaxime Henrion 		return (ENOBUFS);
2278ba8c6fd5SDavid Greenman 
2279ba8c6fd5SDavid Greenman 	/*
2280ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2281ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2282ba8c6fd5SDavid Greenman 	 */
2283ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2284ba8c6fd5SDavid Greenman 
2285eadd5e3aSDavid Greenman 	/*
2286eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2287eadd5e3aSDavid Greenman 	 * data start past it.
2288eadd5e3aSDavid Greenman 	 */
2289a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2290c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
229183e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2292eadd5e3aSDavid Greenman 
2293ba8c6fd5SDavid Greenman 	/*
2294ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
2295ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
2296ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
2297ba8c6fd5SDavid Greenman 	 */
22984fc1dda9SAndrew Gallatin 
2299a17c678eSDavid Greenman 	rfa->rfa_status = 0;
230083e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2301a17c678eSDavid Greenman 	rfa->actual_size = 0;
2302ba8c6fd5SDavid Greenman 
230383e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
230483e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2305ba8c6fd5SDavid Greenman 
2306b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2307b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2308b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2309b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2310b2badf02SMaxime Henrion 	if (error) {
2311b2badf02SMaxime Henrion 		m_freem(m);
2312b2badf02SMaxime Henrion 		return (error);
2313b2badf02SMaxime Henrion 	}
2314b2badf02SMaxime Henrion 
2315b2badf02SMaxime Henrion 	bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2316b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2317b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2318b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2319b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2320b2badf02SMaxime Henrion 
2321b983c7b3SMaxime Henrion 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
2322b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2323b2badf02SMaxime Henrion 
2324dfe61cf1SDavid Greenman 	/*
2325dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2326dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2327dfe61cf1SDavid Greenman 	 */
2328b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2329b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2330b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2331b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2332b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
233383e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2334aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2335b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
23364cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2337a17c678eSDavid Greenman 	} else {
2338b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2339b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2340a17c678eSDavid Greenman 	}
2341b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
2342b2badf02SMaxime Henrion 	return (0);
2343a17c678eSDavid Greenman }
2344a17c678eSDavid Greenman 
23456ebc3153SDavid Greenman static volatile int
2346f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2347dccee1a1SDavid Greenman {
2348f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2349dccee1a1SDavid Greenman 	int count = 10000;
23506ebc3153SDavid Greenman 	int value;
2351dccee1a1SDavid Greenman 
2352ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2353ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2354dccee1a1SDavid Greenman 
2355ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2356ba8c6fd5SDavid Greenman 	    && count--)
23576ebc3153SDavid Greenman 		DELAY(10);
2358dccee1a1SDavid Greenman 
2359dccee1a1SDavid Greenman 	if (count <= 0)
2360f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2361dccee1a1SDavid Greenman 
23626ebc3153SDavid Greenman 	return (value & 0xffff);
2363dccee1a1SDavid Greenman }
2364dccee1a1SDavid Greenman 
2365dccee1a1SDavid Greenman static void
2366f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2367dccee1a1SDavid Greenman {
2368f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2369dccee1a1SDavid Greenman 	int count = 10000;
2370dccee1a1SDavid Greenman 
2371ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2372ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2373ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2374dccee1a1SDavid Greenman 
2375ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2376ba8c6fd5SDavid Greenman 	    count--)
23776ebc3153SDavid Greenman 		DELAY(10);
2378dccee1a1SDavid Greenman 
2379dccee1a1SDavid Greenman 	if (count <= 0)
2380f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
2381dccee1a1SDavid Greenman }
2382dccee1a1SDavid Greenman 
2383dccee1a1SDavid Greenman static int
2384f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2385a17c678eSDavid Greenman {
23869b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2387a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2388f7788e8eSJonathan Lemon 	struct mii_data *mii;
2389f7788e8eSJonathan Lemon 	int s, error = 0;
2390a17c678eSDavid Greenman 
2391704d1965SWarner Losh 	/*
2392704d1965SWarner Losh 	 * Detaching causes us to call ioctl with the mutex owned.  Preclude
2393704d1965SWarner Losh 	 * that by saying we're busy if the lock is already held.
2394704d1965SWarner Losh 	 */
2395704d1965SWarner Losh 	if (mtx_owned(&sc->sc_mtx))
2396704d1965SWarner Losh 		return (EBUSY);
239732cd7a9cSWarner Losh 
23984953bccaSNate Lawson 	FXP_LOCK(sc);
2399f7788e8eSJonathan Lemon 	s = splimp();
2400a17c678eSDavid Greenman 
2401a17c678eSDavid Greenman 	switch (command) {
2402a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
2403f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2404f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2405f7788e8eSJonathan Lemon 		else
2406f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2407a17c678eSDavid Greenman 
2408a17c678eSDavid Greenman 		/*
2409a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2410a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2411a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2412a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2413a17c678eSDavid Greenman 		 */
2414a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
24154953bccaSNate Lawson 			fxp_init_body(sc);
2416a17c678eSDavid Greenman 		} else {
2417a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
24184a5f1499SDavid Greenman 				fxp_stop(sc);
2419a17c678eSDavid Greenman 		}
2420a17c678eSDavid Greenman 		break;
2421a17c678eSDavid Greenman 
2422a17c678eSDavid Greenman 	case SIOCADDMULTI:
2423a17c678eSDavid Greenman 	case SIOCDELMULTI:
2424f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2425f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2426f7788e8eSJonathan Lemon 		else
2427f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2428a17c678eSDavid Greenman 		/*
2429a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2430a17c678eSDavid Greenman 		 * accordingly.
2431a17c678eSDavid Greenman 		 */
2432f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2433397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2434397f9dfeSDavid Greenman 		/*
2435f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2436397f9dfeSDavid Greenman 		 * again rather than else {}.
2437397f9dfeSDavid Greenman 		 */
2438f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
24394953bccaSNate Lawson 			fxp_init_body(sc);
2440a17c678eSDavid Greenman 		error = 0;
2441ba8c6fd5SDavid Greenman 		break;
2442ba8c6fd5SDavid Greenman 
2443ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2444ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2445f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2446f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2447f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2448f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2449f7788e8eSJonathan Lemon 		} else {
2450ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2451f7788e8eSJonathan Lemon 		}
2452a17c678eSDavid Greenman 		break;
2453a17c678eSDavid Greenman 
2454a17c678eSDavid Greenman 	default:
24554953bccaSNate Lawson 		/*
24564953bccaSNate Lawson 		 * ether_ioctl() will eventually call fxp_start() which
24574953bccaSNate Lawson 		 * will result in mutex recursion so drop it first.
24584953bccaSNate Lawson 		 */
24594953bccaSNate Lawson 		FXP_UNLOCK(sc);
2460673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2461a17c678eSDavid Greenman 	}
24624953bccaSNate Lawson 	if (mtx_owned(&sc->sc_mtx))
24634953bccaSNate Lawson 		FXP_UNLOCK(sc);
2464f7788e8eSJonathan Lemon 	splx(s);
2465a17c678eSDavid Greenman 	return (error);
2466a17c678eSDavid Greenman }
2467397f9dfeSDavid Greenman 
2468397f9dfeSDavid Greenman /*
246909882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
247009882363SJonathan Lemon  */
247109882363SJonathan Lemon static int
247209882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
247309882363SJonathan Lemon {
247409882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
247509882363SJonathan Lemon 	struct ifnet *ifp = &sc->sc_if;
247609882363SJonathan Lemon 	struct ifmultiaddr *ifma;
247709882363SJonathan Lemon 	int nmcasts;
247809882363SJonathan Lemon 
247909882363SJonathan Lemon 	nmcasts = 0;
248009882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
248109882363SJonathan Lemon #if __FreeBSD_version < 500000
248209882363SJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
248309882363SJonathan Lemon #else
248409882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
248509882363SJonathan Lemon #endif
248609882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
248709882363SJonathan Lemon 				continue;
248809882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
248909882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
249009882363SJonathan Lemon 				nmcasts = 0;
249109882363SJonathan Lemon 				break;
249209882363SJonathan Lemon 			}
249309882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2494bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
249509882363SJonathan Lemon 			nmcasts++;
249609882363SJonathan Lemon 		}
249709882363SJonathan Lemon 	}
2498bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
249909882363SJonathan Lemon 	return (nmcasts);
250009882363SJonathan Lemon }
250109882363SJonathan Lemon 
250209882363SJonathan Lemon /*
2503397f9dfeSDavid Greenman  * Program the multicast filter.
2504397f9dfeSDavid Greenman  *
2505397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2506397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
25073114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2508397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2509dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2510397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2511397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2512397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2513397f9dfeSDavid Greenman  *
2514397f9dfeSDavid Greenman  * This function must be called at splimp.
2515397f9dfeSDavid Greenman  */
2516397f9dfeSDavid Greenman static void
2517f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2518397f9dfeSDavid Greenman {
2519397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2520397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
2521b2badf02SMaxime Henrion 	struct fxp_tx *txp;
25227dced78aSDavid Greenman 	int count;
2523397f9dfeSDavid Greenman 
25243114fdb4SDavid Greenman 	/*
25253114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
25263114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
25273114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
25283114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
25293114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
25303114fdb4SDavid Greenman 	 */
2531397f9dfeSDavid Greenman 	if (sc->tx_queued) {
25323114fdb4SDavid Greenman 		/*
25333114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
25343114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
25353114fdb4SDavid Greenman 		 */
25363114fdb4SDavid Greenman 		if (sc->need_mcsetup)
25373114fdb4SDavid Greenman 			return;
2538397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
25393114fdb4SDavid Greenman 
25403114fdb4SDavid Greenman 		/*
254172a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
254272a32a26SJonathan Lemon 		 * when all TX commands have been processed.
25433114fdb4SDavid Greenman 		 */
2544b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
2545b2badf02SMaxime Henrion 		txp->tx_mbuf = NULL;
2546b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
254783e6547dSMaxime Henrion 		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
254883e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
25493114fdb4SDavid Greenman 		/*
25503114fdb4SDavid Greenman 		 * Advance the end of list forward.
25513114fdb4SDavid Greenman 		 */
255283e6547dSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
255383e6547dSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
25545f361cbeSMaxime Henrion 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2555b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
25563114fdb4SDavid Greenman 		sc->tx_queued++;
25573114fdb4SDavid Greenman 		/*
25583114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
25593114fdb4SDavid Greenman 		 */
25603114fdb4SDavid Greenman 		fxp_scb_wait(sc);
25612e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
25623114fdb4SDavid Greenman 		/*
25633114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
25643114fdb4SDavid Greenman 		 * card again.
25653114fdb4SDavid Greenman 		 */
25663114fdb4SDavid Greenman 		ifp->if_timer = 5;
25673114fdb4SDavid Greenman 
2568397f9dfeSDavid Greenman 		return;
2569397f9dfeSDavid Greenman 	}
2570397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2571397f9dfeSDavid Greenman 
2572397f9dfeSDavid Greenman 	/*
2573397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2574397f9dfeSDavid Greenman 	 */
2575397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
257683e6547dSMaxime Henrion 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
257783e6547dSMaxime Henrion 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
257883e6547dSMaxime Henrion 	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2579b2badf02SMaxime Henrion 	txp = &sc->fxp_desc.mcs_tx;
2580b2badf02SMaxime Henrion 	txp->tx_mbuf = NULL;
2581b2badf02SMaxime Henrion 	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2582b2badf02SMaxime Henrion 	txp->tx_next = sc->fxp_desc.tx_list;
258309882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2584b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2585397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2586397f9dfeSDavid Greenman 
2587397f9dfeSDavid Greenman 	/*
2588397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2589397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2590397f9dfeSDavid Greenman 	 */
25917dced78aSDavid Greenman 	count = 100;
2592397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
25937dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
25947dced78aSDavid Greenman 		DELAY(10);
25957dced78aSDavid Greenman 	if (count == 0) {
2596f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
25977dced78aSDavid Greenman 		return;
25987dced78aSDavid Greenman 	}
2599397f9dfeSDavid Greenman 
2600397f9dfeSDavid Greenman 	/*
2601397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2602397f9dfeSDavid Greenman 	 */
2603397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2604b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2605b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
26062e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2607397f9dfeSDavid Greenman 
26083114fdb4SDavid Greenman 	ifp->if_timer = 2;
2609397f9dfeSDavid Greenman 	return;
2610397f9dfeSDavid Greenman }
261172a32a26SJonathan Lemon 
261272a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
261372a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
261472a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
261572a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
261672a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
261772a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
261872a32a26SJonathan Lemon 
261972a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
262072a32a26SJonathan Lemon 
262172a32a26SJonathan Lemon struct ucode {
262272a32a26SJonathan Lemon 	u_int32_t	revision;
262372a32a26SJonathan Lemon 	u_int32_t	*ucode;
262472a32a26SJonathan Lemon 	int		length;
262572a32a26SJonathan Lemon 	u_short		int_delay_offset;
262672a32a26SJonathan Lemon 	u_short		bundle_max_offset;
262772a32a26SJonathan Lemon } ucode_table[] = {
262872a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
262972a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
263072a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
263172a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
263272a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
263372a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
263472a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
263572a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
263672a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
263772a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
263872a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
263972a32a26SJonathan Lemon };
264072a32a26SJonathan Lemon 
264172a32a26SJonathan Lemon static void
264272a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
264372a32a26SJonathan Lemon {
264472a32a26SJonathan Lemon 	struct ucode *uc;
264572a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
264672a32a26SJonathan Lemon 
264772a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
264872a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
264972a32a26SJonathan Lemon 			break;
265072a32a26SJonathan Lemon 	if (uc->ucode == NULL)
265172a32a26SJonathan Lemon 		return;
2652b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
265372a32a26SJonathan Lemon 	cbp->cb_status = 0;
265483e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
265583e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
265672a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
265772a32a26SJonathan Lemon 	if (uc->int_delay_offset)
265883e6547dSMaxime Henrion 		*(u_int16_t *)&cbp->ucode[uc->int_delay_offset] =
265983e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
266072a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
266183e6547dSMaxime Henrion 		*(u_int16_t *)&cbp->ucode[uc->bundle_max_offset] =
266283e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
266372a32a26SJonathan Lemon 	/*
266472a32a26SJonathan Lemon 	 * Download the ucode to the chip.
266572a32a26SJonathan Lemon 	 */
266672a32a26SJonathan Lemon 	fxp_scb_wait(sc);
2667b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2668b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
266972a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
267072a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2671209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2672b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
267372a32a26SJonathan Lemon 	device_printf(sc->dev,
267472a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
267572a32a26SJonathan Lemon 	    sc->tunable_int_delay,
267672a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
267772a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
267872a32a26SJonathan Lemon }
267972a32a26SJonathan Lemon 
268072a32a26SJonathan Lemon static int
268172a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
268272a32a26SJonathan Lemon {
268372a32a26SJonathan Lemon 	int error, value;
268472a32a26SJonathan Lemon 
268572a32a26SJonathan Lemon 	value = *(int *)arg1;
268672a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
268772a32a26SJonathan Lemon 	if (error || !req->newptr)
268872a32a26SJonathan Lemon 		return (error);
268972a32a26SJonathan Lemon 	if (value < low || value > high)
269072a32a26SJonathan Lemon 		return (EINVAL);
269172a32a26SJonathan Lemon 	*(int *)arg1 = value;
269272a32a26SJonathan Lemon 	return (0);
269372a32a26SJonathan Lemon }
269472a32a26SJonathan Lemon 
269572a32a26SJonathan Lemon /*
269672a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
269772a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
269872a32a26SJonathan Lemon  */
269972a32a26SJonathan Lemon static int
270072a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
270172a32a26SJonathan Lemon {
270272a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
270372a32a26SJonathan Lemon }
270472a32a26SJonathan Lemon 
270572a32a26SJonathan Lemon static int
270672a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
270772a32a26SJonathan Lemon {
270872a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
270972a32a26SJonathan Lemon }
2710