xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 7929aa036c85b3ff4badebf421426e81b3e1e652)
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 
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
32aad970f1SDavid E. O'Brien 
33a17c678eSDavid Greenman /*
34ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
35a17c678eSDavid Greenman  */
36a17c678eSDavid Greenman 
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>
43fe12f24bSPoul-Henning Kamp #include <sys/module.h>
444458ac71SBruce Evans #include <sys/socket.h>
4572a32a26SJonathan Lemon #include <sys/sysctl.h>
46a17c678eSDavid Greenman 
47a17c678eSDavid Greenman #include <net/if.h>
48397f9dfeSDavid Greenman #include <net/if_dl.h>
49ba8c6fd5SDavid Greenman #include <net/if_media.h>
50a17c678eSDavid Greenman 
51a17c678eSDavid Greenman #include <net/bpf.h>
52ba8c6fd5SDavid Greenman #include <sys/sockio.h>
536182fdbdSPeter Wemm #include <sys/bus.h>
546182fdbdSPeter Wemm #include <machine/bus.h>
556182fdbdSPeter Wemm #include <sys/rman.h>
566182fdbdSPeter Wemm #include <machine/resource.h>
57ba8c6fd5SDavid Greenman 
581d5e9e22SEivind Eklund #include <net/ethernet.h>
591d5e9e22SEivind Eklund #include <net/if_arp.h>
60ba8c6fd5SDavid Greenman 
61f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
62a17c678eSDavid Greenman 
63e8c8b728SJonathan Lemon #include <net/if_types.h>
64e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
65e8c8b728SJonathan Lemon 
66c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
67c8bca6dcSBill Paul #include <netinet/in.h>
68c8bca6dcSBill Paul #include <netinet/in_systm.h>
69c8bca6dcSBill Paul #include <netinet/ip.h>
70c8bca6dcSBill Paul #include <machine/in_cksum.h>
71c8bca6dcSBill Paul #endif
72c8bca6dcSBill Paul 
734fbd232cSWarner Losh #include <dev/pci/pcivar.h>
744fbd232cSWarner Losh #include <dev/pci/pcireg.h>		/* for PCIM_CMD_xxx */
75a17c678eSDavid Greenman 
76f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
77f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
78f7788e8eSJonathan Lemon 
79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8172a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
82f7788e8eSJonathan Lemon 
83f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
84f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
85f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
86f7788e8eSJonathan Lemon #include "miibus_if.h"
874fc1dda9SAndrew Gallatin 
88ba8c6fd5SDavid Greenman /*
89ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
90ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
91ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
92ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
93ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
94ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
95ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
96ba8c6fd5SDavid Greenman  */
97ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
98ba8c6fd5SDavid Greenman 
99ba8c6fd5SDavid Greenman /*
100f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
101f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
102f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
103f7788e8eSJonathan Lemon  */
104f7788e8eSJonathan Lemon static int tx_threshold = 64;
105f7788e8eSJonathan Lemon 
106f7788e8eSJonathan Lemon /*
107f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
108f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
109f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
110f7788e8eSJonathan Lemon  * to fxp_init.
111f7788e8eSJonathan Lemon  *
112f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
113f7788e8eSJonathan Lemon  */
114f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
115f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
116f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
117f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
118f7788e8eSJonathan Lemon 	0x0,	/*  0 */
119f7788e8eSJonathan Lemon 	0x0,	/*  1 */
120f7788e8eSJonathan Lemon 	0x0,	/*  2 */
121f7788e8eSJonathan Lemon 	0x0,	/*  3 */
122f7788e8eSJonathan Lemon 	0x0,	/*  4 */
123f7788e8eSJonathan Lemon 	0x0,	/*  5 */
124f7788e8eSJonathan Lemon 	0x32,	/*  6 */
125f7788e8eSJonathan Lemon 	0x0,	/*  7 */
126f7788e8eSJonathan Lemon 	0x0,	/*  8 */
127f7788e8eSJonathan Lemon 	0x0,	/*  9 */
128f7788e8eSJonathan Lemon 	0x6,	/* 10 */
129f7788e8eSJonathan Lemon 	0x0,	/* 11 */
130f7788e8eSJonathan Lemon 	0x0,	/* 12 */
131f7788e8eSJonathan Lemon 	0x0,	/* 13 */
132f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
133f7788e8eSJonathan Lemon 	0x48,	/* 15 */
134f7788e8eSJonathan Lemon 	0x0,	/* 16 */
135f7788e8eSJonathan Lemon 	0x40,	/* 17 */
136f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
137f7788e8eSJonathan Lemon 	0x0,	/* 19 */
138f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
139f7788e8eSJonathan Lemon 	0x5	/* 21 */
140f7788e8eSJonathan Lemon };
141f7788e8eSJonathan Lemon 
142f7788e8eSJonathan Lemon struct fxp_ident {
143f7788e8eSJonathan Lemon 	u_int16_t	devid;
144f19fc5d8SJohn Polstra 	int16_t		revid;		/* -1 matches anything */
145f7788e8eSJonathan Lemon 	char 		*name;
146f7788e8eSJonathan Lemon };
147f7788e8eSJonathan Lemon 
148f7788e8eSJonathan Lemon /*
149f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
150f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
151f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
152f7788e8eSJonathan Lemon  * them.
153f7788e8eSJonathan Lemon  */
154f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
155f19fc5d8SJohn Polstra     { 0x1029,	-1,	"Intel 82559 PCI/CardBus Pro/100" },
156f19fc5d8SJohn Polstra     { 0x1030,	-1,	"Intel 82559 Pro/100 Ethernet" },
157f19fc5d8SJohn Polstra     { 0x1031,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
158f19fc5d8SJohn Polstra     { 0x1032,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
159f19fc5d8SJohn Polstra     { 0x1033,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
160f19fc5d8SJohn Polstra     { 0x1034,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
161f19fc5d8SJohn Polstra     { 0x1035,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
162f19fc5d8SJohn Polstra     { 0x1036,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
163f19fc5d8SJohn Polstra     { 0x1037,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
164f19fc5d8SJohn Polstra     { 0x1038,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
165f19fc5d8SJohn Polstra     { 0x1039,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
166f19fc5d8SJohn Polstra     { 0x103A,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
167f19fc5d8SJohn Polstra     { 0x103B,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
168f19fc5d8SJohn Polstra     { 0x103C,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
169f19fc5d8SJohn Polstra     { 0x103D,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
170f19fc5d8SJohn Polstra     { 0x103E,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
171f19fc5d8SJohn Polstra     { 0x1050,	-1,	"Intel 82801BA (D865) Pro/100 VE Ethernet" },
172c2b37819SWarner Losh     { 0x1051,	-1,	"Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
173f19fc5d8SJohn Polstra     { 0x1059,	-1,	"Intel 82551QM Pro/100 M Mobile Connection" },
174f19fc5d8SJohn Polstra     { 0x1209,	-1,	"Intel 82559ER Embedded 10/100 Ethernet" },
175f19fc5d8SJohn Polstra     { 0x1229,	0x01,	"Intel 82557 Pro/100 Ethernet" },
176f19fc5d8SJohn Polstra     { 0x1229,	0x02,	"Intel 82557 Pro/100 Ethernet" },
177f19fc5d8SJohn Polstra     { 0x1229,	0x03,	"Intel 82557 Pro/100 Ethernet" },
178f19fc5d8SJohn Polstra     { 0x1229,	0x04,	"Intel 82558 Pro/100 Ethernet" },
179f19fc5d8SJohn Polstra     { 0x1229,	0x05,	"Intel 82558 Pro/100 Ethernet" },
180f19fc5d8SJohn Polstra     { 0x1229,	0x06,	"Intel 82559 Pro/100 Ethernet" },
181f19fc5d8SJohn Polstra     { 0x1229,	0x07,	"Intel 82559 Pro/100 Ethernet" },
182f19fc5d8SJohn Polstra     { 0x1229,	0x08,	"Intel 82559 Pro/100 Ethernet" },
183f19fc5d8SJohn Polstra     { 0x1229,	0x09,	"Intel 82559ER Pro/100 Ethernet" },
184f19fc5d8SJohn Polstra     { 0x1229,	0x0c,	"Intel 82550 Pro/100 Ethernet" },
185f19fc5d8SJohn Polstra     { 0x1229,	0x0d,	"Intel 82550 Pro/100 Ethernet" },
186f19fc5d8SJohn Polstra     { 0x1229,	0x0e,	"Intel 82550 Pro/100 Ethernet" },
187f19fc5d8SJohn Polstra     { 0x1229,	0x0f,	"Intel 82551 Pro/100 Ethernet" },
188f19fc5d8SJohn Polstra     { 0x1229,	0x10,	"Intel 82551 Pro/100 Ethernet" },
189f19fc5d8SJohn Polstra     { 0x1229,	-1,	"Intel 82557/8/9 Pro/100 Ethernet" },
190f19fc5d8SJohn Polstra     { 0x2449,	-1,	"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
191f19fc5d8SJohn Polstra     { 0,	-1,	NULL },
192f7788e8eSJonathan Lemon };
193f7788e8eSJonathan Lemon 
194c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
195c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
196c8bca6dcSBill Paul #else
197c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
198c8bca6dcSBill Paul #endif
199c8bca6dcSBill Paul 
200f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
201f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
202f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
203f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
204f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
205f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
206f7788e8eSJonathan Lemon 
207f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
2084953bccaSNate Lawson static void		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
2094953bccaSNate Lawson 			    u_int8_t statack, int count);
210f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2114953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
212f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
213f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
2144953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
215f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
216f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
217f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
218f7788e8eSJonathan Lemon 			    caddr_t data);
219f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
220b2badf02SMaxime Henrion static int		fxp_add_rfabuf(struct fxp_softc *sc,
221b2badf02SMaxime Henrion     			    struct fxp_rx *rxp);
22209882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
223f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
224f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
225f7788e8eSJonathan Lemon 			    int autosize);
22600c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
22700c4116bSJonathan Lemon 			    u_int16_t data);
228f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
229f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
230f7788e8eSJonathan Lemon 			    int offset, int words);
23100c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
23200c4116bSJonathan Lemon 			    int offset, int words);
233f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
234f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
235f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
236f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
237f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
238f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
239f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
240f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
241f7788e8eSJonathan Lemon 			    int value);
24272a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
24372a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
24472a32a26SJonathan Lemon 			    int low, int high);
24572a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
24672a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
24728935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
24828935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
24928935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
250209b07bcSMaxime Henrion     			    volatile u_int16_t *status, bus_dma_tag_t dmat,
251209b07bcSMaxime Henrion 			    bus_dmamap_t map);
252f7788e8eSJonathan Lemon 
253f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
254f7788e8eSJonathan Lemon 	/* Device interface */
255f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
256f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
257f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
258f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
259f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
260f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
261f7788e8eSJonathan Lemon 
262f7788e8eSJonathan Lemon 	/* MII interface */
263f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
264f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
265f7788e8eSJonathan Lemon 
266f7788e8eSJonathan Lemon 	{ 0, 0 }
267f7788e8eSJonathan Lemon };
268f7788e8eSJonathan Lemon 
269f7788e8eSJonathan Lemon static driver_t fxp_driver = {
270f7788e8eSJonathan Lemon 	"fxp",
271f7788e8eSJonathan Lemon 	fxp_methods,
272f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
273f7788e8eSJonathan Lemon };
274f7788e8eSJonathan Lemon 
275f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
276f7788e8eSJonathan Lemon 
277f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
278347934faSWarner Losh DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
279f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
280f7788e8eSJonathan Lemon 
281f7788e8eSJonathan Lemon /*
282dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
283dfe61cf1SDavid Greenman  * completed).
284dfe61cf1SDavid Greenman  */
28528935f27SMaxime Henrion static void
286f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
287a17c678eSDavid Greenman {
288a17c678eSDavid Greenman 	int i = 10000;
289a17c678eSDavid Greenman 
2907dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2917dced78aSDavid Greenman 		DELAY(2);
2927dced78aSDavid Greenman 	if (i == 0)
29300c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
294e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
295e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
296e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
297e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2987dced78aSDavid Greenman }
2997dced78aSDavid Greenman 
30028935f27SMaxime Henrion static void
3012e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3022e2b8238SJonathan Lemon {
3032e2b8238SJonathan Lemon 
3042e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3052e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3062e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3072e2b8238SJonathan Lemon 	}
3082e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3092e2b8238SJonathan Lemon }
3102e2b8238SJonathan Lemon 
31128935f27SMaxime Henrion static void
312209b07bcSMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile u_int16_t *status,
313209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3147dced78aSDavid Greenman {
3157dced78aSDavid Greenman 	int i = 10000;
3167dced78aSDavid Greenman 
317209b07bcSMaxime Henrion 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
318209b07bcSMaxime Henrion 	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) {
3197dced78aSDavid Greenman 		DELAY(2);
320209b07bcSMaxime Henrion 		bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
321209b07bcSMaxime Henrion 	}
3227dced78aSDavid Greenman 	if (i == 0)
323f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
324a17c678eSDavid Greenman }
325a17c678eSDavid Greenman 
326dfe61cf1SDavid Greenman /*
32728935f27SMaxime Henrion  * Return identification string if this device is ours.
328dfe61cf1SDavid Greenman  */
3296182fdbdSPeter Wemm static int
3306182fdbdSPeter Wemm fxp_probe(device_t dev)
331a17c678eSDavid Greenman {
332f7788e8eSJonathan Lemon 	u_int16_t devid;
333f19fc5d8SJohn Polstra 	u_int8_t revid;
334f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
335f7788e8eSJonathan Lemon 
33655ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
337f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
338f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
339f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
340f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
341f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
342f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
343f7788e8eSJonathan Lemon 				return (0);
34455ce7b51SDavid Greenman 			}
345dd68ef16SPeter Wemm 		}
346f7788e8eSJonathan Lemon 	}
347f7788e8eSJonathan Lemon 	return (ENXIO);
3486182fdbdSPeter Wemm }
3496182fdbdSPeter Wemm 
350b2badf02SMaxime Henrion static void
351b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
352b2badf02SMaxime Henrion {
353b2badf02SMaxime Henrion 	u_int32_t *addr;
354b2badf02SMaxime Henrion 
355b2badf02SMaxime Henrion 	if (error)
356b2badf02SMaxime Henrion 		return;
357b2badf02SMaxime Henrion 
358b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
359b2badf02SMaxime Henrion 	addr = arg;
360b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
361b2badf02SMaxime Henrion }
362b2badf02SMaxime Henrion 
3636182fdbdSPeter Wemm static int
3646182fdbdSPeter Wemm fxp_attach(device_t dev)
365a17c678eSDavid Greenman {
3666182fdbdSPeter Wemm 	int error = 0;
3676182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
368ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
369b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
3709fa6ccfbSMatt Jacob 	u_int32_t val;
37183e6547dSMaxime Henrion 	u_int16_t data, myea[ETHER_ADDR_LEN / 2];
372d73e2e55SMaxime Henrion 	int i, rid, m1, m2, prefer_iomap, maxtxseg;
373a35e7eaaSDon Lewis 	int s, ipcbxmit_disable;
374a17c678eSDavid Greenman 
375f7788e8eSJonathan Lemon 	sc->dev = dev;
37645276e4aSSam Leffler 	callout_init(&sc->stat_ch, CALLOUT_MPSAFE);
3776008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
3784953bccaSNate Lawson 	    MTX_DEF);
3794953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
3804953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
381a17c678eSDavid Greenman 
382f7788e8eSJonathan Lemon 	s = splimp();
383a17c678eSDavid Greenman 
384dfe61cf1SDavid Greenman 	/*
3852bce79a2SMaxim Sobolev 	 * Enable bus mastering.
386df373873SWes Peters 	 */
387cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
3889fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
38979495006SWarner Losh 
390df373873SWes Peters 	/*
3919fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3929fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
3939fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
394dfe61cf1SDavid Greenman 	 */
3959fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
3969fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
3972a05a4ebSMatt Jacob 	prefer_iomap = 0;
3982a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3992a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
4009fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
4019fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
4029fa6ccfbSMatt Jacob 	}
4039fa6ccfbSMatt Jacob 
404533294b9SMatthew N. Dodd 	sc->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4059fa6ccfbSMatt Jacob 	sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4065f96beb9SNate Lawson 	sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd, RF_ACTIVE);
407533294b9SMatthew N. Dodd 	if (sc->mem == NULL) {
4089fa6ccfbSMatt Jacob 		sc->rtp =
4099fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4109fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4115f96beb9SNate Lawson 		sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd,
4125f96beb9SNate Lawson                                             RF_ACTIVE);
4139fa6ccfbSMatt Jacob 	}
4149fa6ccfbSMatt Jacob 
4156182fdbdSPeter Wemm 	if (!sc->mem) {
4166182fdbdSPeter Wemm 		error = ENXIO;
417a17c678eSDavid Greenman 		goto fail;
418a17c678eSDavid Greenman         }
4199fa6ccfbSMatt Jacob 	if (bootverbose) {
4209fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4219fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4229fa6ccfbSMatt Jacob 	}
4234fc1dda9SAndrew Gallatin 
4244fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4254fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
426a17c678eSDavid Greenman 
427a17c678eSDavid Greenman 	/*
428dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
429dfe61cf1SDavid Greenman 	 */
4306182fdbdSPeter Wemm 	rid = 0;
4315f96beb9SNate Lawson 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4326182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4336182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4346182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4356182fdbdSPeter Wemm 		error = ENXIO;
4366182fdbdSPeter Wemm 		goto fail;
4376182fdbdSPeter Wemm 	}
4386182fdbdSPeter Wemm 
439f7788e8eSJonathan Lemon 	/*
440f7788e8eSJonathan Lemon 	 * Reset to a stable state.
441f7788e8eSJonathan Lemon 	 */
442f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
443f7788e8eSJonathan Lemon 	DELAY(10);
444f7788e8eSJonathan Lemon 
445f7788e8eSJonathan Lemon 	/*
446f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
447f7788e8eSJonathan Lemon 	 */
448f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
449f7788e8eSJonathan Lemon 
450f7788e8eSJonathan Lemon 	/*
4513bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
452f7788e8eSJonathan Lemon 	 */
453f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
454f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
455f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
456dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
457f7788e8eSJonathan Lemon 
4580f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
4590f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
46050a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
461858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
46272a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
4630f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
4640f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
46550a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
466858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
46772a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
4680f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
4690f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
4700f1db1d6SMaxime Henrion 	    OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
4710f1db1d6SMaxime Henrion 	    "FXP RNR events");
4720f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
4730f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
4740f1db1d6SMaxime Henrion 	    OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0,
4750f1db1d6SMaxime Henrion 	    "FXP flow control disabled");
47672a32a26SJonathan Lemon 
47772a32a26SJonathan Lemon 	/*
47872a32a26SJonathan Lemon 	 * Pull in device tunables.
47972a32a26SJonathan Lemon 	 */
48072a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
48172a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
4820f1db1d6SMaxime Henrion 	sc->tunable_noflow = 0;
48372a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
48472a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
48572a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
48672a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
4870f1db1d6SMaxime Henrion 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
4880f1db1d6SMaxime Henrion 	    "noflow", &sc->tunable_noflow);
4890f1db1d6SMaxime Henrion 	sc->rnr = 0;
49072a32a26SJonathan Lemon 
49172a32a26SJonathan Lemon 	/*
49272a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
4933bd07cfdSJonathan Lemon 	 */
4943bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
4953bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
49672a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
49772a32a26SJonathan Lemon 	else
49872a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
4993bd07cfdSJonathan Lemon 
5003bd07cfdSJonathan Lemon 	/*
5012e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
50200c4116bSJonathan Lemon 	 *
50372a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
50472a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
50572a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
50600c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
50700c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
50800c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
50900c4116bSJonathan Lemon 	 *
51000c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5112e2b8238SJonathan Lemon 	 */
5122e2b8238SJonathan Lemon 	i = pci_get_device(dev);
51372a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
51472a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
51500c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
51600c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
51700c4116bSJonathan Lemon 			u_int16_t cksum;
51800c4116bSJonathan Lemon 			int i;
51900c4116bSJonathan Lemon 
52000c4116bSJonathan Lemon 			device_printf(dev,
521001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
52200c4116bSJonathan Lemon 			data &= ~0x02;
52300c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
52400c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
52500c4116bSJonathan Lemon 			cksum = 0;
52600c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
52700c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
52800c4116bSJonathan Lemon 				cksum += data;
52900c4116bSJonathan Lemon 			}
53000c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
53100c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
53200c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
53300c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
53400c4116bSJonathan Lemon 			device_printf(dev,
53500c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
53600c4116bSJonathan Lemon 			    i, data, cksum);
53700c4116bSJonathan Lemon #if 1
53800c4116bSJonathan Lemon 			/*
53900c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
54000c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
54100c4116bSJonathan Lemon 			 */
5422e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
54300c4116bSJonathan Lemon #endif
54400c4116bSJonathan Lemon 		}
54500c4116bSJonathan Lemon 	}
5462e2b8238SJonathan Lemon 
5472e2b8238SJonathan Lemon 	/*
5483bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5493bd07cfdSJonathan Lemon 	 */
55072a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5513bd07cfdSJonathan Lemon 		/*
55274396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
55374396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
55474396a0aSJonathan Lemon 		 * the board to turn on MWI.
5553bd07cfdSJonathan Lemon 		 */
55674396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
55774396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5583bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5593bd07cfdSJonathan Lemon 
5603bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5613bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
56244e0bc11SYaroslav Tykhiy 
56344e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
56444e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
56544e0bc11SYaroslav Tykhiy 	} else {
56644e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
56744e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
5683bd07cfdSJonathan Lemon 	}
5693bd07cfdSJonathan Lemon 
5703bd07cfdSJonathan Lemon 	/*
571c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
572c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
573c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
574c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
575a35e7eaaSDon Lewis 	 *
576a35e7eaaSDon Lewis 	 * At least some 82550 cards probed as "chip=0x12298086 rev=0x0d"
577a35e7eaaSDon Lewis 	 * truncate packets that end with an mbuf containing 1 to 3 bytes
578a35e7eaaSDon Lewis 	 * when used with this feature enabled in the previous version of the
579a35e7eaaSDon Lewis 	 * driver.  This problem appears to be fixed now that the driver
580a35e7eaaSDon Lewis 	 * always sets the hardware parse bit in the IPCB structure, which
581a35e7eaaSDon Lewis 	 * the "Intel 8255x 10/100 Mbps Ethernet Controller Family Open
582a35e7eaaSDon Lewis 	 * Source Software Developer Manual" says is necessary in the
583a35e7eaaSDon Lewis 	 * cases where packet truncation was observed.
584a35e7eaaSDon Lewis 	 *
585a35e7eaaSDon Lewis 	 * The device hint "hint.fxp.UNIT_NUMBER.ipcbxmit_disable"
586a35e7eaaSDon Lewis 	 * allows this feature to be disabled at boot time.
587a35e7eaaSDon Lewis 	 *
588a35e7eaaSDon Lewis 	 * If fxp is not compiled into the kernel, this feature may also
589a35e7eaaSDon Lewis 	 * be disabled at run time:
590a35e7eaaSDon Lewis 	 *    # kldunload fxp
591a35e7eaaSDon Lewis 	 *    # kenv hint.fxp.0.ipcbxmit_disable=1
592a35e7eaaSDon Lewis 	 *    # kldload fxp
593c8bca6dcSBill Paul 	 */
594c8bca6dcSBill Paul 
595a35e7eaaSDon Lewis 	if (resource_int_value("fxp", device_get_unit(dev), "ipcbxmit_disable",
596a35e7eaaSDon Lewis 	    &ipcbxmit_disable) != 0)
597a35e7eaaSDon Lewis 		ipcbxmit_disable = 0;
598a35e7eaaSDon Lewis 	if (ipcbxmit_disable == 0 && (sc->revision == FXP_REV_82550 ||
599a35e7eaaSDon Lewis 	    sc->revision == FXP_REV_82550_C)) {
600c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
601c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
602c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
603c8bca6dcSBill Paul 	} else {
604c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
605c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
606c8bca6dcSBill Paul 	}
607c8bca6dcSBill Paul 
608c8bca6dcSBill Paul 	/*
609b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
610b2badf02SMaxime Henrion 	 */
611d73e2e55SMaxime Henrion 	maxtxseg = sc->flags & FXP_FLAG_EXT_RFA ? FXP_NTXSEG - 1 : FXP_NTXSEG;
612b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 2, 0, BUS_SPACE_MAXADDR_32BIT,
613d73e2e55SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * maxtxseg,
614f6b1c44dSScott Long 	    maxtxseg, MCLBYTES, 0, busdma_lock_mutex, &Giant, &sc->fxp_mtag);
615b2badf02SMaxime Henrion 	if (error) {
616b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
617b2badf02SMaxime Henrion 		goto fail;
618b2badf02SMaxime Henrion 	}
619b2badf02SMaxime Henrion 
620b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
621b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_stats), 1,
622f6b1c44dSScott Long 	    sizeof(struct fxp_stats), 0, busdma_lock_mutex, &Giant,
623f6b1c44dSScott Long 	    &sc->fxp_stag);
624b2badf02SMaxime Henrion 	if (error) {
625b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
626b2badf02SMaxime Henrion 		goto fail;
627b2badf02SMaxime Henrion 	}
628b2badf02SMaxime Henrion 
629b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
630aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap);
631b2badf02SMaxime Henrion 	if (error)
6324953bccaSNate Lawson 		goto fail;
633b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
634b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
635b2badf02SMaxime Henrion 	if (error) {
636b2badf02SMaxime Henrion 		device_printf(dev, "could not map the stats buffer\n");
637b2badf02SMaxime Henrion 		goto fail;
638b2badf02SMaxime Henrion 	}
639b2badf02SMaxime Henrion 
640b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
641b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, FXP_TXCB_SZ, 1,
642f6b1c44dSScott Long 	    FXP_TXCB_SZ, 0, busdma_lock_mutex, &Giant, &sc->cbl_tag);
643b2badf02SMaxime Henrion 	if (error) {
644b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
645b2badf02SMaxime Henrion 		goto fail;
646b2badf02SMaxime Henrion 	}
647b2badf02SMaxime Henrion 
648b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
649aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map);
650b2badf02SMaxime Henrion 	if (error)
6514953bccaSNate Lawson 		goto fail;
652b2badf02SMaxime Henrion 
653b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
654b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
655b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
656b2badf02SMaxime Henrion 	if (error) {
657b2badf02SMaxime Henrion 		device_printf(dev, "could not map DMA memory\n");
658b2badf02SMaxime Henrion 		goto fail;
659b2badf02SMaxime Henrion 	}
660b2badf02SMaxime Henrion 
661b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
662b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_cb_mcs), 1,
663f6b1c44dSScott Long 	    sizeof(struct fxp_cb_mcs), 0, busdma_lock_mutex, &Giant,
664f6b1c44dSScott Long 	    &sc->mcs_tag);
665b2badf02SMaxime Henrion 	if (error) {
666b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
667b2badf02SMaxime Henrion 		goto fail;
668b2badf02SMaxime Henrion 	}
669b2badf02SMaxime Henrion 
670b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
671b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->mcs_map);
672b2badf02SMaxime Henrion 	if (error)
6734953bccaSNate Lawson 		goto fail;
674b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
675b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
676b2badf02SMaxime Henrion 	if (error) {
677b2badf02SMaxime Henrion 		device_printf(dev, "can't map the multicast setup command\n");
678b2badf02SMaxime Henrion 		goto fail;
679b2badf02SMaxime Henrion 	}
680b2badf02SMaxime Henrion 
681b2badf02SMaxime Henrion 	/*
682b2badf02SMaxime Henrion 	 * Pre-allocate the TX DMA maps.
683b2badf02SMaxime Henrion 	 */
6844cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
685b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0,
686b2badf02SMaxime Henrion 		    &sc->fxp_desc.tx_list[i].tx_map);
687b2badf02SMaxime Henrion 		if (error) {
688b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
689b2badf02SMaxime Henrion 			goto fail;
690b2badf02SMaxime Henrion 		}
691b2badf02SMaxime Henrion 	}
692b2badf02SMaxime Henrion 	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
693b2badf02SMaxime Henrion 	if (error) {
694b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
695b2badf02SMaxime Henrion 		goto fail;
696b2badf02SMaxime Henrion 	}
697b2badf02SMaxime Henrion 
698b2badf02SMaxime Henrion 	/*
699b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
700b2badf02SMaxime Henrion 	 */
701b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
702b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
703b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
704b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
705b2badf02SMaxime Henrion 		if (error) {
706b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
707b2badf02SMaxime Henrion 			goto fail;
708b2badf02SMaxime Henrion 		}
7094953bccaSNate Lawson 		if (fxp_add_rfabuf(sc, rxp) != 0) {
7104953bccaSNate Lawson 			error = ENOMEM;
7114953bccaSNate Lawson 			goto fail;
7124953bccaSNate Lawson 		}
713b2badf02SMaxime Henrion 	}
714b2badf02SMaxime Henrion 
715b2badf02SMaxime Henrion 	/*
716f7788e8eSJonathan Lemon 	 * Read MAC address.
717f7788e8eSJonathan Lemon 	 */
71883e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
71983e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[0] = myea[0] & 0xff;
72083e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[1] = myea[0] >> 8;
72183e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[2] = myea[1] & 0xff;
72283e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[3] = myea[1] >> 8;
72383e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[4] = myea[2] & 0xff;
72483e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[5] = myea[2] >> 8;
725f7788e8eSJonathan Lemon 	if (bootverbose) {
7262e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
727f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7282e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7292e2b8238SJonathan Lemon 		    pci_get_revid(dev));
73072a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
73172a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
73272a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
733f7788e8eSJonathan Lemon 	}
734f7788e8eSJonathan Lemon 
735f7788e8eSJonathan Lemon 	/*
736f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
737f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
738f7788e8eSJonathan Lemon 	 *
739f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
740f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
741f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
742f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
743f7788e8eSJonathan Lemon 	 */
744f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
745f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
746f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
747f7788e8eSJonathan Lemon 	} else {
748f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
749f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
750f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
7516182fdbdSPeter Wemm 			error = ENXIO;
752ba8c6fd5SDavid Greenman 			goto fail;
753a17c678eSDavid Greenman 		}
754f7788e8eSJonathan Lemon 	}
755dccee1a1SDavid Greenman 
756a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
7579bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
758a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
759fb583156SDavid Greenman 	ifp->if_init = fxp_init;
760ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
761ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
762ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
763ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
764ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
765a17c678eSDavid Greenman 
7665fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
7675fe9116bSYaroslav Tykhiy 
768c8bca6dcSBill Paul 	/* Enable checksum offload for 82550 or better chips */
769c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
770c8bca6dcSBill Paul 		ifp->if_hwassist = FXP_CSUM_FEATURES;
7715fe9116bSYaroslav Tykhiy 		ifp->if_capabilities |= IFCAP_HWCSUM;
7725fe9116bSYaroslav Tykhiy 		ifp->if_capenable |= IFCAP_HWCSUM;
773c8bca6dcSBill Paul 	}
774c8bca6dcSBill Paul 
775fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
776fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
777fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
778fb917226SRuslan Ermilov 	ifp->if_capenable |= IFCAP_POLLING;
779fb917226SRuslan Ermilov #endif
780fb917226SRuslan Ermilov 
781dfe61cf1SDavid Greenman 	/*
7824953bccaSNate Lawson 	 * Attach the interface.
7834953bccaSNate Lawson 	 */
7844953bccaSNate Lawson 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
7854953bccaSNate Lawson 
7864953bccaSNate Lawson 	/*
787e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
7885fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
7895fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
790e8c8b728SJonathan Lemon 	 */
791e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
792673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
79344e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
794e8c8b728SJonathan Lemon 
795483b9871SDavid Greenman 	/*
7963114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
7973114fdb4SDavid Greenman 	 * TX descriptors.
798483b9871SDavid Greenman 	 */
7997929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8007929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8017929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8024a684684SDavid Greenman 
803201afb0eSMaxime Henrion 	/*
8044953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
8054953bccaSNate Lawson 	 * XXX This driver has been tested with the INTR_MPSAFFE flag set
8064953bccaSNate Lawson 	 * however, ifp and its functions are not fully locked so MPSAFE
8074953bccaSNate Lawson 	 * should not be used unless you can handle potential data loss.
808201afb0eSMaxime Henrion 	 */
809b237430cSSam Leffler 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
810201afb0eSMaxime Henrion 			       fxp_intr, sc, &sc->ih);
811201afb0eSMaxime Henrion 	if (error) {
812201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
8134953bccaSNate Lawson 		ether_ifdetach(&sc->arpcom.ac_if);
814201afb0eSMaxime Henrion 		goto fail;
815201afb0eSMaxime Henrion 	}
816201afb0eSMaxime Henrion 
817a17c678eSDavid Greenman fail:
818f7788e8eSJonathan Lemon 	splx(s);
8194953bccaSNate Lawson 	if (error)
820f7788e8eSJonathan Lemon 		fxp_release(sc);
821f7788e8eSJonathan Lemon 	return (error);
822f7788e8eSJonathan Lemon }
823f7788e8eSJonathan Lemon 
824f7788e8eSJonathan Lemon /*
8254953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
8264953bccaSNate Lawson  * interrupt should already be torn down.
827f7788e8eSJonathan Lemon  */
828f7788e8eSJonathan Lemon static void
829f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
830f7788e8eSJonathan Lemon {
831b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
832b2badf02SMaxime Henrion 	struct fxp_tx *txp;
833b2badf02SMaxime Henrion 	int i;
834b2badf02SMaxime Henrion 
83567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
836670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
837670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
8384953bccaSNate Lawson 	if (sc->miibus)
8394953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
8404953bccaSNate Lawson 	bus_generic_detach(sc->dev);
8414953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
842b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
843b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
844b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
845b2badf02SMaxime Henrion 		    sc->cbl_map);
846b2badf02SMaxime Henrion 	}
847b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
848b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
849b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
850b2badf02SMaxime Henrion 	}
851b2badf02SMaxime Henrion 	if (sc->mcsp) {
852b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
853b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
854b2badf02SMaxime Henrion 	}
855f7788e8eSJonathan Lemon 	if (sc->irq)
856f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
857f7788e8eSJonathan Lemon 	if (sc->mem)
858f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
859b983c7b3SMaxime Henrion 	if (sc->fxp_mtag) {
860b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
861b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
862b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
863b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
864b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
865b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
866b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
867b983c7b3SMaxime Henrion 			}
868b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
869b983c7b3SMaxime Henrion 		}
870b983c7b3SMaxime Henrion 		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
871b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_mtag);
872b983c7b3SMaxime Henrion 	}
873b983c7b3SMaxime Henrion 	if (sc->fxp_stag) {
874b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
875b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
876b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
877b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
878b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
879b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
880b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
881b983c7b3SMaxime Henrion 			}
882b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
883b983c7b3SMaxime Henrion 		}
884b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
885b983c7b3SMaxime Henrion 	}
886b2badf02SMaxime Henrion 	if (sc->cbl_tag)
887b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
888b2badf02SMaxime Henrion 	if (sc->mcs_tag)
889b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
89072a32a26SJonathan Lemon 
8910f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
8926182fdbdSPeter Wemm }
8936182fdbdSPeter Wemm 
8946182fdbdSPeter Wemm /*
8956182fdbdSPeter Wemm  * Detach interface.
8966182fdbdSPeter Wemm  */
8976182fdbdSPeter Wemm static int
8986182fdbdSPeter Wemm fxp_detach(device_t dev)
8996182fdbdSPeter Wemm {
9006182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
901f7788e8eSJonathan Lemon 	int s;
9026182fdbdSPeter Wemm 
9034953bccaSNate Lawson 	FXP_LOCK(sc);
904f7788e8eSJonathan Lemon 	s = splimp();
90532cd7a9cSWarner Losh 
9061d2945d5SWarner Losh 	sc->suspended = 1;	/* Do same thing as we do for suspend */
9076182fdbdSPeter Wemm 	/*
908f7788e8eSJonathan Lemon 	 * Close down routes etc.
9096182fdbdSPeter Wemm 	 */
910673d9191SSam Leffler 	ether_ifdetach(&sc->arpcom.ac_if);
91120f0c80fSMaxime Henrion 
91220f0c80fSMaxime Henrion 	/*
91332cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
91420f0c80fSMaxime Henrion 	 */
91520f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
91620f0c80fSMaxime Henrion 	fxp_stop(sc);
91732cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
91820f0c80fSMaxime Henrion 
9196182fdbdSPeter Wemm 	/*
9204953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
9214953bccaSNate Lawson 	 * races with fxp_intr().
9226182fdbdSPeter Wemm 	 */
9234953bccaSNate Lawson 	bus_teardown_intr(sc->dev, sc->irq, sc->ih);
9244953bccaSNate Lawson 	sc->ih = NULL;
9256182fdbdSPeter Wemm 
926f7788e8eSJonathan Lemon 	splx(s);
9276182fdbdSPeter Wemm 
928f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
929f7788e8eSJonathan Lemon 	fxp_release(sc);
930f7788e8eSJonathan Lemon 	return (0);
931a17c678eSDavid Greenman }
932a17c678eSDavid Greenman 
933a17c678eSDavid Greenman /*
9344a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
935a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
936a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
937a17c678eSDavid Greenman  */
9386182fdbdSPeter Wemm static int
9396182fdbdSPeter Wemm fxp_shutdown(device_t dev)
940a17c678eSDavid Greenman {
9416182fdbdSPeter Wemm 	/*
9426182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
9436182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
9446182fdbdSPeter Wemm 	 * reboot before the driver initializes.
9456182fdbdSPeter Wemm 	 */
9466182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
947f7788e8eSJonathan Lemon 	return (0);
948a17c678eSDavid Greenman }
949a17c678eSDavid Greenman 
9507dced78aSDavid Greenman /*
9517dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
9527dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
9537dced78aSDavid Greenman  * resume.
9547dced78aSDavid Greenman  */
9557dced78aSDavid Greenman static int
9567dced78aSDavid Greenman fxp_suspend(device_t dev)
9577dced78aSDavid Greenman {
9587dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
959f7788e8eSJonathan Lemon 	int i, s;
9607dced78aSDavid Greenman 
9614953bccaSNate Lawson 	FXP_LOCK(sc);
962f7788e8eSJonathan Lemon 	s = splimp();
9637dced78aSDavid Greenman 
9647dced78aSDavid Greenman 	fxp_stop(sc);
9657dced78aSDavid Greenman 
9667dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
967e27951b2SJohn Baldwin 		sc->saved_maps[i] = pci_read_config(dev, PCIR_BAR(i), 4);
9687dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
9697dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
9707dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
9717dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
9727dced78aSDavid Greenman 
9737dced78aSDavid Greenman 	sc->suspended = 1;
9747dced78aSDavid Greenman 
9754953bccaSNate Lawson 	FXP_UNLOCK(sc);
976f7788e8eSJonathan Lemon 	splx(s);
977f7788e8eSJonathan Lemon 	return (0);
9787dced78aSDavid Greenman }
9797dced78aSDavid Greenman 
9807dced78aSDavid Greenman /*
9817dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
9827dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
9837dced78aSDavid Greenman  * appropriate.
9847dced78aSDavid Greenman  */
9857dced78aSDavid Greenman static int
9867dced78aSDavid Greenman fxp_resume(device_t dev)
9877dced78aSDavid Greenman {
9887dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
9897dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
9907dced78aSDavid Greenman 	u_int16_t pci_command;
991f7788e8eSJonathan Lemon 	int i, s;
9927dced78aSDavid Greenman 
9934953bccaSNate Lawson 	FXP_LOCK(sc);
994f7788e8eSJonathan Lemon 	s = splimp();
99579495006SWarner Losh 
9967dced78aSDavid Greenman 	/* better way to do this? */
9977dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
998e27951b2SJohn Baldwin 		pci_write_config(dev, PCIR_BAR(i), sc->saved_maps[i], 4);
9997dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
10007dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
10017dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
10027dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
10037dced78aSDavid Greenman 
10047dced78aSDavid Greenman 	/* reenable busmastering */
10057dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
10067dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
10077dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
10087dced78aSDavid Greenman 
10097dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
10107dced78aSDavid Greenman 	DELAY(10);
10117dced78aSDavid Greenman 
10127dced78aSDavid Greenman 	/* reinitialize interface if necessary */
10137dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
10144953bccaSNate Lawson 		fxp_init_body(sc);
10157dced78aSDavid Greenman 
10167dced78aSDavid Greenman 	sc->suspended = 0;
10177dced78aSDavid Greenman 
10184953bccaSNate Lawson 	FXP_UNLOCK(sc);
1019f7788e8eSJonathan Lemon 	splx(s);
1020ba8c6fd5SDavid Greenman 	return (0);
1021f7788e8eSJonathan Lemon }
1022ba8c6fd5SDavid Greenman 
102300c4116bSJonathan Lemon static void
102400c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
102500c4116bSJonathan Lemon {
102600c4116bSJonathan Lemon 	u_int16_t reg;
102700c4116bSJonathan Lemon 	int x;
102800c4116bSJonathan Lemon 
102900c4116bSJonathan Lemon 	/*
103000c4116bSJonathan Lemon 	 * Shift in data.
103100c4116bSJonathan Lemon 	 */
103200c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
103300c4116bSJonathan Lemon 		if (data & x)
103400c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
103500c4116bSJonathan Lemon 		else
103600c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
103700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
103800c4116bSJonathan Lemon 		DELAY(1);
103900c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
104000c4116bSJonathan Lemon 		DELAY(1);
104100c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
104200c4116bSJonathan Lemon 		DELAY(1);
104300c4116bSJonathan Lemon 	}
104400c4116bSJonathan Lemon }
104500c4116bSJonathan Lemon 
1046f7788e8eSJonathan Lemon /*
1047f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1048f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1049f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1050f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1051f7788e8eSJonathan Lemon  * every 16 bits of data.
1052f7788e8eSJonathan Lemon  */
1053f7788e8eSJonathan Lemon static u_int16_t
1054f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1055f7788e8eSJonathan Lemon {
1056f7788e8eSJonathan Lemon 	u_int16_t reg, data;
1057f7788e8eSJonathan Lemon 	int x;
1058ba8c6fd5SDavid Greenman 
1059f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1060f7788e8eSJonathan Lemon 	/*
1061f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1062f7788e8eSJonathan Lemon 	 */
106300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1064f7788e8eSJonathan Lemon 	/*
1065f7788e8eSJonathan Lemon 	 * Shift in address.
1066f7788e8eSJonathan Lemon 	 */
1067f7788e8eSJonathan Lemon 	data = 0;
1068f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1069f7788e8eSJonathan Lemon 		if (offset & x)
1070f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1071f7788e8eSJonathan Lemon 		else
1072f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1073f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1074f7788e8eSJonathan Lemon 		DELAY(1);
1075f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1076f7788e8eSJonathan Lemon 		DELAY(1);
1077f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1078f7788e8eSJonathan Lemon 		DELAY(1);
1079f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1080f7788e8eSJonathan Lemon 		data++;
1081f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1082f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1083f7788e8eSJonathan Lemon 			break;
1084f7788e8eSJonathan Lemon 		}
1085f7788e8eSJonathan Lemon 	}
1086f7788e8eSJonathan Lemon 	/*
1087f7788e8eSJonathan Lemon 	 * Shift out data.
1088f7788e8eSJonathan Lemon 	 */
1089f7788e8eSJonathan Lemon 	data = 0;
1090f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1091f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1092f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1093f7788e8eSJonathan Lemon 		DELAY(1);
1094f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1095f7788e8eSJonathan Lemon 			data |= x;
1096f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1097f7788e8eSJonathan Lemon 		DELAY(1);
1098f7788e8eSJonathan Lemon 	}
1099f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1100f7788e8eSJonathan Lemon 	DELAY(1);
1101f7788e8eSJonathan Lemon 
1102f7788e8eSJonathan Lemon 	return (data);
1103ba8c6fd5SDavid Greenman }
1104ba8c6fd5SDavid Greenman 
110500c4116bSJonathan Lemon static void
110600c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
110700c4116bSJonathan Lemon {
110800c4116bSJonathan Lemon 	int i;
110900c4116bSJonathan Lemon 
111000c4116bSJonathan Lemon 	/*
111100c4116bSJonathan Lemon 	 * Erase/write enable.
111200c4116bSJonathan Lemon 	 */
111300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
111400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
111500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
111600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
111700c4116bSJonathan Lemon 	DELAY(1);
111800c4116bSJonathan Lemon 	/*
111900c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
112000c4116bSJonathan Lemon 	 */
112100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
112200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
112300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
112400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
112500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
112600c4116bSJonathan Lemon 	DELAY(1);
112700c4116bSJonathan Lemon 	/*
112800c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
112900c4116bSJonathan Lemon 	 */
113000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
113100c4116bSJonathan Lemon 	DELAY(1);
113200c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
113300c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
113400c4116bSJonathan Lemon 			break;
113500c4116bSJonathan Lemon 		DELAY(50);
113600c4116bSJonathan Lemon 	}
113700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
113800c4116bSJonathan Lemon 	DELAY(1);
113900c4116bSJonathan Lemon 	/*
114000c4116bSJonathan Lemon 	 * Erase/write disable.
114100c4116bSJonathan Lemon 	 */
114200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
114300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
114400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
114500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
114600c4116bSJonathan Lemon 	DELAY(1);
114700c4116bSJonathan Lemon }
114800c4116bSJonathan Lemon 
1149ba8c6fd5SDavid Greenman /*
1150e9bf2fa7SDavid Greenman  * From NetBSD:
1151e9bf2fa7SDavid Greenman  *
1152e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1153e9bf2fa7SDavid Greenman  *
1154e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1155e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1156e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1157e9bf2fa7SDavid Greenman  *
1158e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1159e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1160e9bf2fa7SDavid Greenman  *
1161e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1162e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1163e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1164e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1165e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1166e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1167e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1168e9bf2fa7SDavid Greenman  */
1169e9bf2fa7SDavid Greenman static void
1170f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1171e9bf2fa7SDavid Greenman {
1172e9bf2fa7SDavid Greenman 
1173f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1174f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1175f7788e8eSJonathan Lemon 
1176f7788e8eSJonathan Lemon 	/* autosize */
1177f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1178e9bf2fa7SDavid Greenman }
1179f7788e8eSJonathan Lemon 
1180ba8c6fd5SDavid Greenman static void
1181f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1182ba8c6fd5SDavid Greenman {
1183f7788e8eSJonathan Lemon 	int i;
1184ba8c6fd5SDavid Greenman 
1185f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1186f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1187ba8c6fd5SDavid Greenman }
1188ba8c6fd5SDavid Greenman 
118900c4116bSJonathan Lemon static void
119000c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
119100c4116bSJonathan Lemon {
119200c4116bSJonathan Lemon 	int i;
119300c4116bSJonathan Lemon 
119400c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
119500c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
119600c4116bSJonathan Lemon }
119700c4116bSJonathan Lemon 
1198b2badf02SMaxime Henrion static void
1199b2badf02SMaxime Henrion fxp_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1200b2badf02SMaxime Henrion     bus_size_t mapsize, int error)
1201b2badf02SMaxime Henrion {
1202b2badf02SMaxime Henrion 	struct fxp_softc *sc;
1203b2badf02SMaxime Henrion 	struct fxp_cb_tx *txp;
1204b2badf02SMaxime Henrion 	int i;
1205b2badf02SMaxime Henrion 
1206b2badf02SMaxime Henrion 	if (error)
1207b2badf02SMaxime Henrion 		return;
1208b2badf02SMaxime Henrion 
1209b2badf02SMaxime Henrion 	KASSERT(nseg <= FXP_NTXSEG, ("too many DMA segments"));
1210b2badf02SMaxime Henrion 
1211b2badf02SMaxime Henrion 	sc = arg;
1212b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next->tx_cb;
1213b2badf02SMaxime Henrion 	for (i = 0; i < nseg; i++) {
1214b2badf02SMaxime Henrion 		KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large"));
1215b2badf02SMaxime Henrion 		/*
1216b2badf02SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
1217b2badf02SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
1218b2badf02SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
1219b2badf02SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
1220b2badf02SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
1221b2badf02SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
1222b2badf02SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
1223b2badf02SMaxime Henrion 		 * checksum offload control bits. So to make things work
1224b2badf02SMaxime Henrion 		 * right, we have to start filling in the TBD array
1225b2badf02SMaxime Henrion 		 * starting from a different place depending on whether
1226b2badf02SMaxime Henrion 		 * the chip is an 82550/82551 or not.
1227b2badf02SMaxime Henrion 		 */
1228b2badf02SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
122983e6547dSMaxime Henrion 			txp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
123083e6547dSMaxime Henrion 			txp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
1231b2badf02SMaxime Henrion 		} else {
123283e6547dSMaxime Henrion 			txp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
123383e6547dSMaxime Henrion 			txp->tbd[i].tb_size = htole32(segs[i].ds_len);
1234b2badf02SMaxime Henrion 		}
1235b2badf02SMaxime Henrion 	}
1236b2badf02SMaxime Henrion 	txp->tbd_number = nseg;
1237b2badf02SMaxime Henrion }
1238b2badf02SMaxime Henrion 
1239a17c678eSDavid Greenman /*
12404953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1241a17c678eSDavid Greenman  */
1242a17c678eSDavid Greenman static void
1243f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1244a17c678eSDavid Greenman {
12459b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
12464953bccaSNate Lawson 
12474953bccaSNate Lawson 	FXP_LOCK(sc);
12484953bccaSNate Lawson 	fxp_start_body(ifp);
12494953bccaSNate Lawson 	FXP_UNLOCK(sc);
12504953bccaSNate Lawson }
12514953bccaSNate Lawson 
12524953bccaSNate Lawson /*
12534953bccaSNate Lawson  * Start packet transmission on the interface.
12544953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
12554953bccaSNate Lawson  * internal entry point only.
12564953bccaSNate Lawson  */
12574953bccaSNate Lawson static void
12584953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
12594953bccaSNate Lawson {
12604953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
126150d81222SMaxime Henrion 	struct fxp_tx *txp;
1262b2badf02SMaxime Henrion 	struct mbuf *mb_head;
1263b2badf02SMaxime Henrion 	int error;
1264a17c678eSDavid Greenman 
126567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1266a17c678eSDavid Greenman 	/*
1267483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1268483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1269483b9871SDavid Greenman 	 * of the command chain).
1270a17c678eSDavid Greenman 	 */
12710f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1272a17c678eSDavid Greenman 		return;
12730f4dc94cSChuck Paterson 	}
12741cd443acSDavid Greenman 
1275483b9871SDavid Greenman 	txp = NULL;
1276483b9871SDavid Greenman 
1277483b9871SDavid Greenman 	/*
1278483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1279483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
12803114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
12813114fdb4SDavid Greenman 	 *       a NOP command when needed.
1282483b9871SDavid Greenman 	 */
12837929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
12847929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1285483b9871SDavid Greenman 
1286dfe61cf1SDavid Greenman 		/*
1287dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1288dfe61cf1SDavid Greenman 		 */
12897929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
12907929aa03SMax Laier 		if (mb_head == NULL)
12917929aa03SMax Laier 			break;
1292a17c678eSDavid Greenman 
1293dfe61cf1SDavid Greenman 		/*
1294483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1295dfe61cf1SDavid Greenman 		 */
1296b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
1297c8bca6dcSBill Paul 
1298c8bca6dcSBill Paul 		/*
1299a35e7eaaSDon Lewis 		 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1300a35e7eaaSDon Lewis 		 * Ethernet Controller Family Open Source Software
1301a35e7eaaSDon Lewis 		 * Developer Manual says:
1302a35e7eaaSDon Lewis 		 *   Using software parsing is only allowed with legal
1303a35e7eaaSDon Lewis 		 *   TCP/IP or UDP/IP packets.
1304a35e7eaaSDon Lewis 		 *   ...
1305a35e7eaaSDon Lewis 		 *   For all other datagrams, hardware parsing must
1306a35e7eaaSDon Lewis 		 *   be used.
1307a35e7eaaSDon Lewis 		 * Software parsing appears to truncate ICMP and
1308a35e7eaaSDon Lewis 		 * fragmented UDP packets that contain one to three
1309a35e7eaaSDon Lewis 		 * bytes in the second (and final) mbuf of the packet.
1310a35e7eaaSDon Lewis 		 */
1311a35e7eaaSDon Lewis 		if (sc->flags & FXP_FLAG_EXT_RFA)
1312a35e7eaaSDon Lewis 			txp->tx_cb->ipcb_ip_activation_high =
1313a35e7eaaSDon Lewis 			    FXP_IPCB_HARDWAREPARSING_ENABLE;
1314a35e7eaaSDon Lewis 
1315a35e7eaaSDon Lewis 		/*
1316c8bca6dcSBill Paul 		 * Deal with TCP/IP checksum offload. Note that
1317c8bca6dcSBill Paul 		 * in order for TCP checksum offload to work,
1318c8bca6dcSBill Paul 		 * the pseudo header checksum must have already
1319c8bca6dcSBill Paul 		 * been computed and stored in the checksum field
1320c8bca6dcSBill Paul 		 * in the TCP header. The stack should have
1321c8bca6dcSBill Paul 		 * already done this for us.
1322c8bca6dcSBill Paul 		 */
1323c8bca6dcSBill Paul 
1324c8bca6dcSBill Paul 		if (mb_head->m_pkthdr.csum_flags) {
1325c8bca6dcSBill Paul 			if (mb_head->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1326b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_schedule =
1327c8bca6dcSBill Paul 				    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1328c8bca6dcSBill Paul 				if (mb_head->m_pkthdr.csum_flags & CSUM_TCP)
1329b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_schedule |=
1330c8bca6dcSBill Paul 					    FXP_IPCB_TCP_PACKET;
1331c8bca6dcSBill Paul 			}
1332c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
1333c8bca6dcSBill Paul 		/*
1334c8bca6dcSBill Paul 		 * XXX The 82550 chip appears to have trouble
1335c8bca6dcSBill Paul 		 * dealing with IP header checksums in very small
1336c8bca6dcSBill Paul 		 * datagrams, namely fragments from 1 to 3 bytes
1337c8bca6dcSBill Paul 		 * in size. For example, say you want to transmit
1338c8bca6dcSBill Paul 		 * a UDP packet of 1473 bytes. The packet will be
1339c8bca6dcSBill Paul 		 * fragmented over two IP datagrams, the latter
1340c8bca6dcSBill Paul 		 * containing only one byte of data. The 82550 will
1341c8bca6dcSBill Paul 		 * botch the header checksum on the 1-byte fragment.
1342c8bca6dcSBill Paul 		 * As long as the datagram contains 4 or more bytes
1343c8bca6dcSBill Paul 		 * of data, you're ok.
1344c8bca6dcSBill Paul 		 *
1345c8bca6dcSBill Paul                  * The following code attempts to work around this
1346c8bca6dcSBill Paul 		 * problem: if the datagram is less than 38 bytes
1347c8bca6dcSBill Paul 		 * in size (14 bytes ether header, 20 bytes IP header,
1348c8bca6dcSBill Paul 		 * plus 4 bytes of data), we punt and compute the IP
1349c8bca6dcSBill Paul 		 * header checksum by hand. This workaround doesn't
1350c8bca6dcSBill Paul 		 * work very well, however, since it can be fooled
1351c8bca6dcSBill Paul 		 * by things like VLAN tags and IP options that make
1352c8bca6dcSBill Paul 		 * the header sizes/offsets vary.
1353c8bca6dcSBill Paul 		 */
1354c8bca6dcSBill Paul 
1355c8bca6dcSBill Paul 			if (mb_head->m_pkthdr.csum_flags & CSUM_IP) {
1356c8bca6dcSBill Paul 				if (mb_head->m_pkthdr.len < 38) {
1357c8bca6dcSBill Paul 					struct ip *ip;
1358c8bca6dcSBill Paul 					mb_head->m_data += ETHER_HDR_LEN;
1359c8bca6dcSBill Paul 					ip = mtod(mb_head, struct ip *);
1360c8bca6dcSBill Paul 					ip->ip_sum = in_cksum(mb_head,
1361c8bca6dcSBill Paul 					    ip->ip_hl << 2);
1362c8bca6dcSBill Paul 					mb_head->m_data -= ETHER_HDR_LEN;
1363c8bca6dcSBill Paul 				} else {
1364b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_activation_high =
1365c8bca6dcSBill Paul 					    FXP_IPCB_HARDWAREPARSING_ENABLE;
1366b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_schedule |=
1367c8bca6dcSBill Paul 					    FXP_IPCB_IP_CHECKSUM_ENABLE;
1368c8bca6dcSBill Paul 				}
1369c8bca6dcSBill Paul 			}
1370c8bca6dcSBill Paul #endif
1371c8bca6dcSBill Paul 		}
1372c8bca6dcSBill Paul 
1373c8bca6dcSBill Paul 		/*
1374a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1375483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1376a17c678eSDavid Greenman 		 * and size of the mbuf.
1377a17c678eSDavid Greenman 		 */
1378b2badf02SMaxime Henrion 		error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1379b2badf02SMaxime Henrion 		    mb_head, fxp_dma_map_txbuf, sc, 0);
1380b2badf02SMaxime Henrion 
1381b2badf02SMaxime Henrion 		if (error && error != EFBIG) {
1382b2badf02SMaxime Henrion 			device_printf(sc->dev, "can't map mbuf (error %d)\n",
1383b2badf02SMaxime Henrion 			    error);
1384b2badf02SMaxime Henrion 			m_freem(mb_head);
1385a17c678eSDavid Greenman 			break;
1386a17c678eSDavid Greenman 		}
1387b2badf02SMaxime Henrion 
1388b2badf02SMaxime Henrion 		if (error) {
138923a0ed7cSDavid Greenman 			struct mbuf *mn;
139023a0ed7cSDavid Greenman 
1391a17c678eSDavid Greenman 			/*
13923bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
13933bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
13943bd07cfdSJonathan Lemon 			 * new buffers.
1395a17c678eSDavid Greenman 			 */
13961104779bSMike Silbersack 			mn = m_defrag(mb_head, M_DONTWAIT);
139723a0ed7cSDavid Greenman 			if (mn == NULL) {
139823a0ed7cSDavid Greenman 				m_freem(mb_head);
1399483b9871SDavid Greenman 				break;
14001104779bSMike Silbersack 			} else {
140123a0ed7cSDavid Greenman 				mb_head = mn;
14021104779bSMike Silbersack 			}
1403b2badf02SMaxime Henrion 			error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1404b2badf02SMaxime Henrion 			    mb_head, fxp_dma_map_txbuf, sc, 0);
1405b2badf02SMaxime Henrion 			if (error) {
1406b2badf02SMaxime Henrion 				device_printf(sc->dev,
1407b2badf02SMaxime Henrion 				    "can't map mbuf (error %d)\n", error);
1408b2badf02SMaxime Henrion 				m_freem(mb_head);
1409b2badf02SMaxime Henrion 				break;
1410b2badf02SMaxime Henrion 			}
141123a0ed7cSDavid Greenman 		}
141223a0ed7cSDavid Greenman 
1413b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1414b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
1415b2badf02SMaxime Henrion 
1416b2badf02SMaxime Henrion 		txp->tx_mbuf = mb_head;
1417b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
1418b2badf02SMaxime Henrion 		txp->tx_cb->byte_count = 0;
14193114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1420b2badf02SMaxime Henrion 			txp->tx_cb->cb_command =
142183e6547dSMaxime Henrion 			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
142283e6547dSMaxime Henrion 			    FXP_CB_COMMAND_S);
14233114fdb4SDavid Greenman 		} else {
1424b2badf02SMaxime Henrion 			txp->tx_cb->cb_command =
142583e6547dSMaxime Henrion 			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
142683e6547dSMaxime Henrion 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
14273114fdb4SDavid Greenman 			/*
14283bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
14293bd07cfdSJonathan Lemon 			 * from the card again.
14303114fdb4SDavid Greenman 			 */
14313114fdb4SDavid Greenman 			ifp->if_timer = 5;
14323114fdb4SDavid Greenman 		}
1433b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1434a17c678eSDavid Greenman 
1435a17c678eSDavid Greenman 		/*
1436483b9871SDavid Greenman 		 * Advance the end of list forward.
1437a17c678eSDavid Greenman 		 */
143806175228SAndrew Gallatin 
143950d81222SMaxime Henrion #ifdef __alpha__
144006175228SAndrew Gallatin 		/*
144106175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
144206175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
144306175228SAndrew Gallatin 		 * up the status while we update the command field.
144406175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
144514fd1071SMaxime Henrion 		 * XXX This is probably bogus and we're _not_ looking
144614fd1071SMaxime Henrion 		 * for atomicity here.
144706175228SAndrew Gallatin 		 */
144814fd1071SMaxime Henrion 		atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1449bafb64afSMaxime Henrion 		    htole16(FXP_CB_COMMAND_S));
145050d81222SMaxime Henrion #else
1451bafb64afSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
1452bafb64afSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
145350d81222SMaxime Henrion #endif /*__alpha__*/
1454b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
1455a17c678eSDavid Greenman 
1456a17c678eSDavid Greenman 		/*
14571cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
1458b2badf02SMaxime Henrion 		 * no other packets queued (when nothing is queued, tx_first
1459483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1460a17c678eSDavid Greenman 		 */
14611cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1462b2badf02SMaxime Henrion 			sc->fxp_desc.tx_first = txp;
1463a17c678eSDavid Greenman 
14641cd443acSDavid Greenman 		sc->tx_queued++;
14651cd443acSDavid Greenman 
1466a17c678eSDavid Greenman 		/*
1467a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1468a17c678eSDavid Greenman 		 */
1469673d9191SSam Leffler 		BPF_MTAP(ifp, mb_head);
1470483b9871SDavid Greenman 	}
1471b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1472483b9871SDavid Greenman 
1473483b9871SDavid Greenman 	/*
1474483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1475483b9871SDavid Greenman 	 * going again if suspended.
1476483b9871SDavid Greenman 	 */
1477483b9871SDavid Greenman 	if (txp != NULL) {
1478483b9871SDavid Greenman 		fxp_scb_wait(sc);
14792e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1480483b9871SDavid Greenman 	}
1481a17c678eSDavid Greenman }
1482a17c678eSDavid Greenman 
1483e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1484e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1485e4fc250cSLuigi Rizzo 
1486e4fc250cSLuigi Rizzo static void
1487e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1488e4fc250cSLuigi Rizzo {
1489e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
1490e4fc250cSLuigi Rizzo 	u_int8_t statack;
1491e4fc250cSLuigi Rizzo 
14924953bccaSNate Lawson 	FXP_LOCK(sc);
1493fb917226SRuslan Ermilov 	if (!(ifp->if_capenable & IFCAP_POLLING)) {
1494fb917226SRuslan Ermilov 		ether_poll_deregister(ifp);
1495fb917226SRuslan Ermilov 		cmd = POLL_DEREGISTER;
1496fb917226SRuslan Ermilov 	}
1497e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1498e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
14994953bccaSNate Lawson 		FXP_UNLOCK(sc);
1500e4fc250cSLuigi Rizzo 		return;
1501e4fc250cSLuigi Rizzo 	}
1502e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1503e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1504e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
1505e4fc250cSLuigi Rizzo 		u_int8_t tmp;
15066481f301SPeter Wemm 
1507e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
15084953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
15094953bccaSNate Lawson 			FXP_UNLOCK(sc);
1510e4fc250cSLuigi Rizzo 			return; /* nothing to do */
15114953bccaSNate Lawson 		}
1512e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1513e4fc250cSLuigi Rizzo 		/* ack what we can */
1514e4fc250cSLuigi Rizzo 		if (tmp != 0)
1515e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1516e4fc250cSLuigi Rizzo 		statack |= tmp;
1517e4fc250cSLuigi Rizzo 	}
15184953bccaSNate Lawson 	fxp_intr_body(sc, ifp, statack, count);
15194953bccaSNate Lawson 	FXP_UNLOCK(sc);
1520e4fc250cSLuigi Rizzo }
1521e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1522e4fc250cSLuigi Rizzo 
1523a17c678eSDavid Greenman /*
15249c7d2607SDavid Greenman  * Process interface interrupts.
1525a17c678eSDavid Greenman  */
152694927790SDavid Greenman static void
1527f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1528a17c678eSDavid Greenman {
1529f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
15304953bccaSNate Lawson 	struct ifnet *ifp = &sc->sc_if;
15311cd443acSDavid Greenman 	u_int8_t statack;
15320f4dc94cSChuck Paterson 
15334953bccaSNate Lawson 	FXP_LOCK(sc);
1534704d1965SWarner Losh 	if (sc->suspended) {
1535704d1965SWarner Losh 		FXP_UNLOCK(sc);
1536704d1965SWarner Losh 		return;
1537704d1965SWarner Losh 	}
1538704d1965SWarner Losh 
1539e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
15404953bccaSNate Lawson 	if (ifp->if_flags & IFF_POLLING) {
15414953bccaSNate Lawson 		FXP_UNLOCK(sc);
1542e4fc250cSLuigi Rizzo 		return;
15434953bccaSNate Lawson 	}
1544fb917226SRuslan Ermilov 	if ((ifp->if_capenable & IFCAP_POLLING) &&
1545fb917226SRuslan Ermilov 	    ether_poll_register(fxp_poll, ifp)) {
1546e4fc250cSLuigi Rizzo 		/* disable interrupts */
1547e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
15484953bccaSNate Lawson 		FXP_UNLOCK(sc);
1549c660bdfaSJohn Baldwin 		fxp_poll(ifp, 0, 1);
1550e4fc250cSLuigi Rizzo 		return;
1551e4fc250cSLuigi Rizzo 	}
1552e4fc250cSLuigi Rizzo #endif
1553b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1554a17c678eSDavid Greenman 		/*
155511457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
155611457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
155711457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
155811457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
155911457bbfSJonathan Lemon 		 */
15604953bccaSNate Lawson 		if (statack == 0xff) {
15614953bccaSNate Lawson 			FXP_UNLOCK(sc);
156211457bbfSJonathan Lemon 			return;
15634953bccaSNate Lawson 		}
156411457bbfSJonathan Lemon 
156511457bbfSJonathan Lemon 		/*
1566a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1567a17c678eSDavid Greenman 		 */
1568ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
15694953bccaSNate Lawson 		fxp_intr_body(sc, ifp, statack, -1);
1570e4fc250cSLuigi Rizzo 	}
15714953bccaSNate Lawson 	FXP_UNLOCK(sc);
1572e4fc250cSLuigi Rizzo }
1573e4fc250cSLuigi Rizzo 
1574e4fc250cSLuigi Rizzo static void
1575b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1576b2badf02SMaxime Henrion {
1577b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1578b2badf02SMaxime Henrion 
1579b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1580b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
158183e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1582b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1583b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1584b2badf02SMaxime Henrion 			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1585b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1586b2badf02SMaxime Henrion 			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1587b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1588b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1589b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1590b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1591b2badf02SMaxime Henrion 		}
1592b2badf02SMaxime Henrion 		sc->tx_queued--;
1593b2badf02SMaxime Henrion 	}
1594b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1595b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1596b2badf02SMaxime Henrion }
1597b2badf02SMaxime Henrion 
1598b2badf02SMaxime Henrion static void
15994953bccaSNate Lawson fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, u_int8_t statack,
16004953bccaSNate Lawson     int count)
1601e4fc250cSLuigi Rizzo {
16022b5989e9SLuigi Rizzo 	struct mbuf *m;
1603b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
16042b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
16052b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
16062b5989e9SLuigi Rizzo 
160767fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
16082b5989e9SLuigi Rizzo 	if (rnr)
16090f1db1d6SMaxime Henrion 		sc->rnr++;
1610947e3815SIan Dowse #ifdef DEVICE_POLLING
1611947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1612947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1613947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1614947e3815SIan Dowse 		rnr = 1;
1615947e3815SIan Dowse 	}
1616947e3815SIan Dowse #endif
1617a17c678eSDavid Greenman 
1618a17c678eSDavid Greenman 	/*
16193114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
162006936301SBill Paul 	 *
162106936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
162206936301SBill Paul 	 * be that this event (control unit not ready) was not
162306936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
162406936301SBill Paul 	 * The exact sequence of events that occur when the interface
162506936301SBill Paul 	 * is brought up are different now, and if this event
162606936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
162706936301SBill Paul 	 * can stall for several seconds. The result is that no
162806936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
162906936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
16303114fdb4SDavid Greenman 	 */
163106936301SBill Paul 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
1632b2badf02SMaxime Henrion 		fxp_txeof(sc);
16333114fdb4SDavid Greenman 
163441aa0ba2SLuigi Rizzo 		ifp->if_timer = 0;
1635e2102ae4SMike Silbersack 		if (sc->tx_queued == 0) {
16363114fdb4SDavid Greenman 			if (sc->need_mcsetup)
16373114fdb4SDavid Greenman 				fxp_mc_setup(sc);
1638e2102ae4SMike Silbersack 		}
16393114fdb4SDavid Greenman 		/*
16403114fdb4SDavid Greenman 		 * Try to start more packets transmitting.
16413114fdb4SDavid Greenman 		 */
16427929aa03SMax Laier 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
16434953bccaSNate Lawson 			fxp_start_body(ifp);
16443114fdb4SDavid Greenman 	}
16452b5989e9SLuigi Rizzo 
16462b5989e9SLuigi Rizzo 	/*
16472b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
16482b5989e9SLuigi Rizzo 	 */
1649947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
16502b5989e9SLuigi Rizzo 		return;
16512b5989e9SLuigi Rizzo 
16523114fdb4SDavid Greenman 	/*
1653a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1654a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1655a17c678eSDavid Greenman 	 * re-start the receiver.
1656947e3815SIan Dowse 	 *
16572b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
16582b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
16592b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
16602b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1661947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1662947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1663a17c678eSDavid Greenman 	 */
16642b5989e9SLuigi Rizzo 	for (;;) {
1665b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1666b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1667ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1668ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1669b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1670b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1671a17c678eSDavid Greenman 
1672e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1673947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1674947e3815SIan Dowse 			if (rnr) {
1675947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1676947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1677947e3815SIan Dowse 				rnr = 0;
1678947e3815SIan Dowse 			}
16792b5989e9SLuigi Rizzo 			break;
1680947e3815SIan Dowse 		}
16812b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
16822b5989e9SLuigi Rizzo 
168383e6547dSMaxime Henrion 		if ((le16toh(rfa->rfa_status) & FXP_RFA_STATUS_C) == 0)
16842b5989e9SLuigi Rizzo 			break;
16852b5989e9SLuigi Rizzo 
1686dfe61cf1SDavid Greenman 		/*
1687b2badf02SMaxime Henrion 		 * Advance head forward.
1688dfe61cf1SDavid Greenman 		 */
1689b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1690a17c678eSDavid Greenman 
1691dfe61cf1SDavid Greenman 		/*
1692ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1693ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1694ba8c6fd5SDavid Greenman 		 * instead.
1695dfe61cf1SDavid Greenman 		 */
1696b2badf02SMaxime Henrion 		if (fxp_add_rfabuf(sc, rxp) == 0) {
1697aed53495SDavid Greenman 			int total_len;
1698a17c678eSDavid Greenman 
1699e8c8b728SJonathan Lemon 			/*
17002b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
17012b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
17022b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
17032b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1704e8c8b728SJonathan Lemon 			 */
1705bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
17062b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
17072b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
1708b2badf02SMaxime Henrion 				sc->rfa_size ||
170983e6547dSMaxime Henrion 			    le16toh(rfa->rfa_status) & FXP_RFA_STATUS_CRC) {
1710e8c8b728SJonathan Lemon 				m_freem(m);
17112b5989e9SLuigi Rizzo 				continue;
1712e8c8b728SJonathan Lemon 			}
1713920b58e8SBrooks Davis 
1714c8bca6dcSBill Paul                         /* Do IP checksum checking. */
171583e6547dSMaxime Henrion 			if (le16toh(rfa->rfa_status) & FXP_RFA_STATUS_PARSE) {
1716c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1717c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1718c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1719c8bca6dcSBill Paul 					    CSUM_IP_CHECKED;
1720c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1721c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_VALID)
1722c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1723c8bca6dcSBill Paul 					    CSUM_IP_VALID;
1724c8bca6dcSBill Paul 				if ((rfa->rfax_csum_sts &
1725c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1726c8bca6dcSBill Paul 				    (rfa->rfax_csum_sts &
1727c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1728c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1729c8bca6dcSBill Paul 					    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1730c8bca6dcSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1731c8bca6dcSBill Paul 				}
1732c8bca6dcSBill Paul 			}
1733c8bca6dcSBill Paul 
17342e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1735673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1736673d9191SSam Leffler 
173705fb8c3fSNate Lawson 			/*
173805fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
173905fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
174005fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
174105fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
174205fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
174305fb8c3fSNate Lawson 			 * calling if_input() on each one.
174405fb8c3fSNate Lawson 			 */
174505fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
1746673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
174705fb8c3fSNate Lawson 			FXP_LOCK(sc);
1748a17c678eSDavid Greenman 		}
1749a17c678eSDavid Greenman 	}
17502b5989e9SLuigi Rizzo 	if (rnr) {
1751ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1752ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1753b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
17542e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1755a17c678eSDavid Greenman 	}
1756a17c678eSDavid Greenman }
1757a17c678eSDavid Greenman 
1758dfe61cf1SDavid Greenman /*
1759dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1760dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1761dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1762dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1763dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1764dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1765dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1766dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1767dfe61cf1SDavid Greenman  * them again next time.
1768dfe61cf1SDavid Greenman  */
1769303b270bSEivind Eklund static void
1770f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1771a17c678eSDavid Greenman {
1772f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1773ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1774a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1775f7788e8eSJonathan Lemon 	int s;
1776a17c678eSDavid Greenman 
17774953bccaSNate Lawson 	FXP_LOCK(sc);
17784953bccaSNate Lawson 	s = splimp();
1779b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
178083e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
178183e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1782397f9dfeSDavid Greenman 	if (sp->rx_good) {
178383e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
1784397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1785397f9dfeSDavid Greenman 	} else {
1786c8cc6fcaSDavid Greenman 		/*
1787c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1788c8cc6fcaSDavid Greenman 		 */
1789397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1790397f9dfeSDavid Greenman 	}
17913ba65732SDavid Greenman 	ifp->if_ierrors +=
179283e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
179383e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
179483e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
179583e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
1796a17c678eSDavid Greenman 	/*
1797f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1798f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1799f9be9005SDavid Greenman 	 */
1800f9be9005SDavid Greenman 	if (sp->tx_underruns) {
180183e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
1802f9be9005SDavid Greenman 		if (tx_threshold < 192)
1803f9be9005SDavid Greenman 			tx_threshold += 64;
1804f9be9005SDavid Greenman 	}
18054953bccaSNate Lawson 
1806397f9dfeSDavid Greenman 	/*
1807c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1808c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1809c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1810c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1811c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1812c8cc6fcaSDavid Greenman 	 */
1813b2badf02SMaxime Henrion 	fxp_txeof(sc);
1814b2badf02SMaxime Henrion 
1815c8cc6fcaSDavid Greenman 	/*
1816397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1817397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1818397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1819397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1820397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1821397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1822397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1823397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1824397f9dfeSDavid Greenman 	 */
1825397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1826397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1827397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1828397f9dfeSDavid Greenman 	}
1829f9be9005SDavid Greenman 	/*
18303ba65732SDavid Greenman 	 * If there is no pending command, start another stats
18313ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1832a17c678eSDavid Greenman 	 */
1833397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1834a17c678eSDavid Greenman 		/*
1835397f9dfeSDavid Greenman 		 * Start another stats dump.
1836a17c678eSDavid Greenman 		 */
1837b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
1838b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
18392e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1840dfe61cf1SDavid Greenman 	} else {
1841dfe61cf1SDavid Greenman 		/*
1842dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1843dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
18443ba65732SDavid Greenman 		 * next timer event to update them.
1845dfe61cf1SDavid Greenman 		 */
1846dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1847f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1848dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
18493ba65732SDavid Greenman 
1850dfe61cf1SDavid Greenman 		sp->rx_good = 0;
18513ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
18523ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
18533ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
18543ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1855dfe61cf1SDavid Greenman 	}
1856f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1857f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
18584953bccaSNate Lawson 
1859a17c678eSDavid Greenman 	/*
1860a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1861a17c678eSDavid Greenman 	 */
186245276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
18634953bccaSNate Lawson 	FXP_UNLOCK(sc);
18644953bccaSNate Lawson 	splx(s);
1865a17c678eSDavid Greenman }
1866a17c678eSDavid Greenman 
1867a17c678eSDavid Greenman /*
1868a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1869a17c678eSDavid Greenman  * the interface.
1870a17c678eSDavid Greenman  */
1871a17c678eSDavid Greenman static void
1872f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1873a17c678eSDavid Greenman {
1874ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1875b2badf02SMaxime Henrion 	struct fxp_tx *txp;
18763ba65732SDavid Greenman 	int i;
1877a17c678eSDavid Greenman 
18787dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
18797dced78aSDavid Greenman 	ifp->if_timer = 0;
18807dced78aSDavid Greenman 
1881e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1882e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
1883e4fc250cSLuigi Rizzo #endif
1884a17c678eSDavid Greenman 	/*
1885a17c678eSDavid Greenman 	 * Cancel stats updater.
1886a17c678eSDavid Greenman 	 */
188745276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
18883ba65732SDavid Greenman 
18893ba65732SDavid Greenman 	/*
189072a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
18913ba65732SDavid Greenman 	 */
189272a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
189309882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
189472a32a26SJonathan Lemon 	DELAY(50);
1895a17c678eSDavid Greenman 
18963ba65732SDavid Greenman 	/*
18973ba65732SDavid Greenman 	 * Release any xmit buffers.
18983ba65732SDavid Greenman 	 */
1899b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
1900da91462dSDavid Greenman 	if (txp != NULL) {
1901da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1902b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
1903b2badf02SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
1904b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
1905b2badf02SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
1906b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
1907b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
1908c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
1909b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
1910da91462dSDavid Greenman 			}
1911da91462dSDavid Greenman 		}
19123ba65732SDavid Greenman 	}
1913b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
19143ba65732SDavid Greenman 	sc->tx_queued = 0;
1915a17c678eSDavid Greenman }
1916a17c678eSDavid Greenman 
1917a17c678eSDavid Greenman /*
1918a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1919a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1920a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1921a17c678eSDavid Greenman  * card has wedged for some reason.
1922a17c678eSDavid Greenman  */
1923a17c678eSDavid Greenman static void
1924f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1925a17c678eSDavid Greenman {
1926ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1927ba8c6fd5SDavid Greenman 
19284953bccaSNate Lawson 	FXP_LOCK(sc);
1929f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
19304a5f1499SDavid Greenman 	ifp->if_oerrors++;
1931a17c678eSDavid Greenman 
19324953bccaSNate Lawson 	fxp_init_body(sc);
19334953bccaSNate Lawson 	FXP_UNLOCK(sc);
1934a17c678eSDavid Greenman }
1935a17c678eSDavid Greenman 
19364953bccaSNate Lawson /*
19374953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
19384953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
19394953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
19404953bccaSNate Lawson  */
1941a17c678eSDavid Greenman static void
1942f7788e8eSJonathan Lemon fxp_init(void *xsc)
1943a17c678eSDavid Greenman {
1944fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
19454953bccaSNate Lawson 
19464953bccaSNate Lawson 	FXP_LOCK(sc);
19474953bccaSNate Lawson 	fxp_init_body(sc);
19484953bccaSNate Lawson 	FXP_UNLOCK(sc);
19494953bccaSNate Lawson }
19504953bccaSNate Lawson 
19514953bccaSNate Lawson /*
19524953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
19534953bccaSNate Lawson  * softc lock held.
19544953bccaSNate Lawson  */
19554953bccaSNate Lawson static void
19564953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
19574953bccaSNate Lawson {
1958ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1959a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1960a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1961b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
1962b2badf02SMaxime Henrion 	struct fxp_tx *txp;
196309882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
1964f7788e8eSJonathan Lemon 	int i, prm, s;
1965a17c678eSDavid Greenman 
196667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1967f7788e8eSJonathan Lemon 	s = splimp();
1968a17c678eSDavid Greenman 	/*
19693ba65732SDavid Greenman 	 * Cancel any pending I/O
1970a17c678eSDavid Greenman 	 */
19713ba65732SDavid Greenman 	fxp_stop(sc);
1972a17c678eSDavid Greenman 
1973a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1974a17c678eSDavid Greenman 
1975a17c678eSDavid Greenman 	/*
1976a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1977a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1978a17c678eSDavid Greenman 	 */
1979ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
19802e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1981a17c678eSDavid Greenman 
1982ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
19832e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1984a17c678eSDavid Greenman 
1985a17c678eSDavid Greenman 	/*
1986a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1987a17c678eSDavid Greenman 	 */
1988ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1989b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
1990b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
19912e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1992a17c678eSDavid Greenman 
1993a17c678eSDavid Greenman 	/*
199472a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
199572a32a26SJonathan Lemon 	 */
199672a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
199772a32a26SJonathan Lemon 		fxp_load_ucode(sc);
199872a32a26SJonathan Lemon 
199972a32a26SJonathan Lemon 	/*
200009882363SJonathan Lemon 	 * Initialize the multicast address list.
200109882363SJonathan Lemon 	 */
200209882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
200309882363SJonathan Lemon 		mcsp = sc->mcsp;
200409882363SJonathan Lemon 		mcsp->cb_status = 0;
200583e6547dSMaxime Henrion 		mcsp->cb_command =
200683e6547dSMaxime Henrion 		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
200783e6547dSMaxime Henrion 		mcsp->link_addr = 0xffffffff;
200809882363SJonathan Lemon 		/*
200909882363SJonathan Lemon 	 	 * Start the multicast setup command.
201009882363SJonathan Lemon 		 */
201109882363SJonathan Lemon 		fxp_scb_wait(sc);
2012b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2013b2badf02SMaxime Henrion 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
201409882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
201509882363SJonathan Lemon 		/* ...and wait for it to complete. */
2016209b07bcSMaxime Henrion 		fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
2017b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
2018b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
201909882363SJonathan Lemon 	}
202009882363SJonathan Lemon 
202109882363SJonathan Lemon 	/*
2022a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2023a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2024a17c678eSDavid Greenman 	 * later.
2025a17c678eSDavid Greenman 	 */
2026b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2027a17c678eSDavid Greenman 
2028a17c678eSDavid Greenman 	/*
2029a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2030a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2031a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2032a17c678eSDavid Greenman 	 */
2033b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2034a17c678eSDavid Greenman 
2035a17c678eSDavid Greenman 	cbp->cb_status =	0;
203683e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
203783e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
203883e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
20392c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2040001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2041001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2042a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2043f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2044f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2045f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2046f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2047001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2048001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2049f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2050a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2051f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2052f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
20533114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2054f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2055f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2056f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
20578ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2058a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2059f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2060f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2061f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
2062c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2063f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2064f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2065f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
2066f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2067f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2068f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2069f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2070a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2071a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2072a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2073a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2074a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2075a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2076a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2077a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2078f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2079f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2080f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2081f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2082f7788e8eSJonathan Lemon 
2083a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2084a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2085a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2086f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2087f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
2088f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
2089f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
2090a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
20913ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2092a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2093f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
2094c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2095a17c678eSDavid Greenman 
20960f1db1d6SMaxime Henrion 	if (sc->tunable_noflow || sc->revision == FXP_REV_82557) {
20973bd07cfdSJonathan Lemon 		/*
20983bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
20993bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
21003bd07cfdSJonathan Lemon 		 */
21013bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
21023bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
21033bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
21043bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
21053bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
21063bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
21073bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
21083bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
21093bd07cfdSJonathan Lemon 	} else {
21103bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
21113bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
21123bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
21133bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
21143bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
21153bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
21163bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
21173bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
21183bd07cfdSJonathan Lemon 	}
21193bd07cfdSJonathan Lemon 
2120a17c678eSDavid Greenman 	/*
2121a17c678eSDavid Greenman 	 * Start the config command/DMA.
2122a17c678eSDavid Greenman 	 */
2123ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2124b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2125b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
21262e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2127a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2128209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2129b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2130a17c678eSDavid Greenman 
2131a17c678eSDavid Greenman 	/*
2132a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2133a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2134a17c678eSDavid Greenman 	 */
2135b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2136a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
213783e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
213883e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
2139e609b4d7SMaxime Henrion 	bcopy(sc->arpcom.ac_enaddr, cb_ias->macaddr,
2140a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
2141a17c678eSDavid Greenman 
2142a17c678eSDavid Greenman 	/*
2143a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2144a17c678eSDavid Greenman 	 */
2145ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2146b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
21472e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2148a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2149209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2150b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2151a17c678eSDavid Greenman 
2152a17c678eSDavid Greenman 	/*
2153a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2154a17c678eSDavid Greenman 	 */
2155b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2156b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2157b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2158a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2159b2badf02SMaxime Henrion 		txp[i].tx_cb = tcbp + i;
2160b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
216183e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
216283e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
216383e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
216483e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
21653bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2166b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
216783e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
21683bd07cfdSJonathan Lemon 		else
2169b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
217083e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2171b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2172a17c678eSDavid Greenman 	}
2173a17c678eSDavid Greenman 	/*
2174397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2175a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2176a17c678eSDavid Greenman 	 */
217783e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2178b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2179b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2180397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2181a17c678eSDavid Greenman 
2182ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
21832e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2184a17c678eSDavid Greenman 
2185a17c678eSDavid Greenman 	/*
2186a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2187a17c678eSDavid Greenman 	 */
2188ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2189b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
21902e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2191a17c678eSDavid Greenman 
2192dccee1a1SDavid Greenman 	/*
2193ba8c6fd5SDavid Greenman 	 * Set current media.
2194dccee1a1SDavid Greenman 	 */
2195f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2196f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2197dccee1a1SDavid Greenman 
2198a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
2199a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
2200e8c8b728SJonathan Lemon 
2201e8c8b728SJonathan Lemon 	/*
2202e8c8b728SJonathan Lemon 	 * Enable interrupts.
2203e8c8b728SJonathan Lemon 	 */
22042b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
22052b5989e9SLuigi Rizzo 	/*
22062b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
22072b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
22082b5989e9SLuigi Rizzo 	 */
220962f76486SMaxim Sobolev 	if ( ifp->if_flags & IFF_POLLING )
22102b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
22112b5989e9SLuigi Rizzo 	else
22122b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2213e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2214a17c678eSDavid Greenman 
2215a17c678eSDavid Greenman 	/*
2216a17c678eSDavid Greenman 	 * Start stats updater.
2217a17c678eSDavid Greenman 	 */
221845276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
22194953bccaSNate Lawson 	splx(s);
2220f7788e8eSJonathan Lemon }
2221f7788e8eSJonathan Lemon 
2222f7788e8eSJonathan Lemon static int
2223f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2224f7788e8eSJonathan Lemon {
2225f7788e8eSJonathan Lemon 
2226f7788e8eSJonathan Lemon 	return (0);
2227a17c678eSDavid Greenman }
2228a17c678eSDavid Greenman 
2229303b270bSEivind Eklund static void
2230f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2231ba8c6fd5SDavid Greenman {
2232ba8c6fd5SDavid Greenman 
2233f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2234ba8c6fd5SDavid Greenman }
2235ba8c6fd5SDavid Greenman 
2236ba8c6fd5SDavid Greenman /*
2237ba8c6fd5SDavid Greenman  * Change media according to request.
2238ba8c6fd5SDavid Greenman  */
2239f7788e8eSJonathan Lemon static int
2240f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2241ba8c6fd5SDavid Greenman {
2242ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2243f7788e8eSJonathan Lemon 	struct mii_data *mii;
2244ba8c6fd5SDavid Greenman 
2245f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
2246f7788e8eSJonathan Lemon 	mii_mediachg(mii);
2247ba8c6fd5SDavid Greenman 	return (0);
2248ba8c6fd5SDavid Greenman }
2249ba8c6fd5SDavid Greenman 
2250ba8c6fd5SDavid Greenman /*
2251ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2252ba8c6fd5SDavid Greenman  */
2253f7788e8eSJonathan Lemon static void
2254f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2255ba8c6fd5SDavid Greenman {
2256ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2257f7788e8eSJonathan Lemon 	struct mii_data *mii;
2258ba8c6fd5SDavid Greenman 
2259f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
2260f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2261f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2262f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
22632e2b8238SJonathan Lemon 
22642e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
22652e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
22662e2b8238SJonathan Lemon 	else
22672e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
2268ba8c6fd5SDavid Greenman }
2269ba8c6fd5SDavid Greenman 
2270a17c678eSDavid Greenman /*
2271a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2272a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
2273a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
2274dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
2275a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2276a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2277a17c678eSDavid Greenman  */
2278a17c678eSDavid Greenman static int
2279b2badf02SMaxime Henrion fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2280a17c678eSDavid Greenman {
2281a17c678eSDavid Greenman 	struct mbuf *m;
2282a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
2283b2badf02SMaxime Henrion 	struct fxp_rx *p_rx;
2284b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
2285b2badf02SMaxime Henrion 	int error;
2286a17c678eSDavid Greenman 
2287a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2288b2badf02SMaxime Henrion 	if (m == NULL)
2289b2badf02SMaxime Henrion 		return (ENOBUFS);
2290ba8c6fd5SDavid Greenman 
2291ba8c6fd5SDavid Greenman 	/*
2292ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2293ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2294ba8c6fd5SDavid Greenman 	 */
2295ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2296ba8c6fd5SDavid Greenman 
2297eadd5e3aSDavid Greenman 	/*
2298eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2299eadd5e3aSDavid Greenman 	 * data start past it.
2300eadd5e3aSDavid Greenman 	 */
2301a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2302c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
230383e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2304eadd5e3aSDavid Greenman 
2305a17c678eSDavid Greenman 	rfa->rfa_status = 0;
230683e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2307a17c678eSDavid Greenman 	rfa->actual_size = 0;
2308ba8c6fd5SDavid Greenman 
230928935f27SMaxime Henrion 	/*
231028935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
231128935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
231228935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
231328935f27SMaxime Henrion 	 * is also alignment-safe.
231428935f27SMaxime Henrion 	 */
231583e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
231683e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2317ba8c6fd5SDavid Greenman 
2318b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2319b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2320b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2321b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2322b2badf02SMaxime Henrion 	if (error) {
2323b2badf02SMaxime Henrion 		m_freem(m);
2324b2badf02SMaxime Henrion 		return (error);
2325b2badf02SMaxime Henrion 	}
2326b2badf02SMaxime Henrion 
2327b2badf02SMaxime Henrion 	bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2328b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2329b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2330b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2331b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2332b2badf02SMaxime Henrion 
2333b983c7b3SMaxime Henrion 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
2334b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2335b2badf02SMaxime Henrion 
2336dfe61cf1SDavid Greenman 	/*
2337dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2338dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2339dfe61cf1SDavid Greenman 	 */
2340b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2341b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2342b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2343b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2344b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
234583e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2346aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2347b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
23484cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2349a17c678eSDavid Greenman 	} else {
2350b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2351b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2352a17c678eSDavid Greenman 	}
2353b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
2354b2badf02SMaxime Henrion 	return (0);
2355a17c678eSDavid Greenman }
2356a17c678eSDavid Greenman 
23576ebc3153SDavid Greenman static volatile int
2358f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2359dccee1a1SDavid Greenman {
2360f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2361dccee1a1SDavid Greenman 	int count = 10000;
23626ebc3153SDavid Greenman 	int value;
2363dccee1a1SDavid Greenman 
2364ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2365ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2366dccee1a1SDavid Greenman 
2367ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2368ba8c6fd5SDavid Greenman 	    && count--)
23696ebc3153SDavid Greenman 		DELAY(10);
2370dccee1a1SDavid Greenman 
2371dccee1a1SDavid Greenman 	if (count <= 0)
2372f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2373dccee1a1SDavid Greenman 
23746ebc3153SDavid Greenman 	return (value & 0xffff);
2375dccee1a1SDavid Greenman }
2376dccee1a1SDavid Greenman 
2377dccee1a1SDavid Greenman static void
2378f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2379dccee1a1SDavid Greenman {
2380f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2381dccee1a1SDavid Greenman 	int count = 10000;
2382dccee1a1SDavid Greenman 
2383ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2384ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2385ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2386dccee1a1SDavid Greenman 
2387ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2388ba8c6fd5SDavid Greenman 	    count--)
23896ebc3153SDavid Greenman 		DELAY(10);
2390dccee1a1SDavid Greenman 
2391dccee1a1SDavid Greenman 	if (count <= 0)
2392f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
2393dccee1a1SDavid Greenman }
2394dccee1a1SDavid Greenman 
2395dccee1a1SDavid Greenman static int
2396f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2397a17c678eSDavid Greenman {
23989b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2399a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2400f7788e8eSJonathan Lemon 	struct mii_data *mii;
24018ef1f631SYaroslav Tykhiy 	int flag, mask, s, error = 0;
2402a17c678eSDavid Greenman 
2403704d1965SWarner Losh 	/*
2404704d1965SWarner Losh 	 * Detaching causes us to call ioctl with the mutex owned.  Preclude
2405704d1965SWarner Losh 	 * that by saying we're busy if the lock is already held.
2406704d1965SWarner Losh 	 */
240767fc050fSMaxime Henrion 	if (FXP_LOCKED(sc))
2408704d1965SWarner Losh 		return (EBUSY);
240932cd7a9cSWarner Losh 
24104953bccaSNate Lawson 	FXP_LOCK(sc);
2411f7788e8eSJonathan Lemon 	s = splimp();
2412a17c678eSDavid Greenman 
2413a17c678eSDavid Greenman 	switch (command) {
2414a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
2415f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2416f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2417f7788e8eSJonathan Lemon 		else
2418f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2419a17c678eSDavid Greenman 
2420a17c678eSDavid Greenman 		/*
2421a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2422a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2423a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2424a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2425a17c678eSDavid Greenman 		 */
2426a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
24274953bccaSNate Lawson 			fxp_init_body(sc);
2428a17c678eSDavid Greenman 		} else {
2429a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
24304a5f1499SDavid Greenman 				fxp_stop(sc);
2431a17c678eSDavid Greenman 		}
2432a17c678eSDavid Greenman 		break;
2433a17c678eSDavid Greenman 
2434a17c678eSDavid Greenman 	case SIOCADDMULTI:
2435a17c678eSDavid Greenman 	case SIOCDELMULTI:
2436f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2437f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2438f7788e8eSJonathan Lemon 		else
2439f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2440a17c678eSDavid Greenman 		/*
2441a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2442a17c678eSDavid Greenman 		 * accordingly.
2443a17c678eSDavid Greenman 		 */
2444f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2445397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2446397f9dfeSDavid Greenman 		/*
2447f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2448397f9dfeSDavid Greenman 		 * again rather than else {}.
2449397f9dfeSDavid Greenman 		 */
2450f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
24514953bccaSNate Lawson 			fxp_init_body(sc);
2452a17c678eSDavid Greenman 		error = 0;
2453ba8c6fd5SDavid Greenman 		break;
2454ba8c6fd5SDavid Greenman 
2455ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2456ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2457f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2458f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2459f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2460f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2461f7788e8eSJonathan Lemon 		} else {
2462ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2463f7788e8eSJonathan Lemon 		}
2464a17c678eSDavid Greenman 		break;
2465a17c678eSDavid Greenman 
2466fb917226SRuslan Ermilov 	case SIOCSIFCAP:
24678ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
24688ef1f631SYaroslav Tykhiy 		if (mask & IFCAP_POLLING)
24698ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_POLLING;
24708ef1f631SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
24718ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
24728ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
24738ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
24748ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
24758ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
24768ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
24778ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
24788ef1f631SYaroslav Tykhiy 				fxp_init_body(sc);
24798ef1f631SYaroslav Tykhiy 		}
2480fb917226SRuslan Ermilov 		break;
2481fb917226SRuslan Ermilov 
2482a17c678eSDavid Greenman 	default:
24834953bccaSNate Lawson 		/*
24844953bccaSNate Lawson 		 * ether_ioctl() will eventually call fxp_start() which
24854953bccaSNate Lawson 		 * will result in mutex recursion so drop it first.
24864953bccaSNate Lawson 		 */
24874953bccaSNate Lawson 		FXP_UNLOCK(sc);
2488673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2489a17c678eSDavid Greenman 	}
249067fc050fSMaxime Henrion 	if (FXP_LOCKED(sc))
24914953bccaSNate Lawson 		FXP_UNLOCK(sc);
2492f7788e8eSJonathan Lemon 	splx(s);
2493a17c678eSDavid Greenman 	return (error);
2494a17c678eSDavid Greenman }
2495397f9dfeSDavid Greenman 
2496397f9dfeSDavid Greenman /*
249709882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
249809882363SJonathan Lemon  */
249909882363SJonathan Lemon static int
250009882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
250109882363SJonathan Lemon {
250209882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
250309882363SJonathan Lemon 	struct ifnet *ifp = &sc->sc_if;
250409882363SJonathan Lemon 	struct ifmultiaddr *ifma;
250509882363SJonathan Lemon 	int nmcasts;
250609882363SJonathan Lemon 
250709882363SJonathan Lemon 	nmcasts = 0;
250809882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
250909882363SJonathan Lemon #if __FreeBSD_version < 500000
251009882363SJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
251109882363SJonathan Lemon #else
251209882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
251309882363SJonathan Lemon #endif
251409882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
251509882363SJonathan Lemon 				continue;
251609882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
251709882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
251809882363SJonathan Lemon 				nmcasts = 0;
251909882363SJonathan Lemon 				break;
252009882363SJonathan Lemon 			}
252109882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2522bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
252309882363SJonathan Lemon 			nmcasts++;
252409882363SJonathan Lemon 		}
252509882363SJonathan Lemon 	}
2526bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
252709882363SJonathan Lemon 	return (nmcasts);
252809882363SJonathan Lemon }
252909882363SJonathan Lemon 
253009882363SJonathan Lemon /*
2531397f9dfeSDavid Greenman  * Program the multicast filter.
2532397f9dfeSDavid Greenman  *
2533397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2534397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
25353114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2536397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2537dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2538397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2539397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2540397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2541397f9dfeSDavid Greenman  *
2542397f9dfeSDavid Greenman  * This function must be called at splimp.
2543397f9dfeSDavid Greenman  */
2544397f9dfeSDavid Greenman static void
2545f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2546397f9dfeSDavid Greenman {
2547397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2548397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
2549b2badf02SMaxime Henrion 	struct fxp_tx *txp;
25507dced78aSDavid Greenman 	int count;
2551397f9dfeSDavid Greenman 
255267fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
25533114fdb4SDavid Greenman 	/*
25543114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
25553114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
25563114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
25573114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
25583114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
25593114fdb4SDavid Greenman 	 */
2560397f9dfeSDavid Greenman 	if (sc->tx_queued) {
25613114fdb4SDavid Greenman 		/*
25623114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
25633114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
25643114fdb4SDavid Greenman 		 */
25653114fdb4SDavid Greenman 		if (sc->need_mcsetup)
25663114fdb4SDavid Greenman 			return;
2567397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
25683114fdb4SDavid Greenman 
25693114fdb4SDavid Greenman 		/*
257072a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
257172a32a26SJonathan Lemon 		 * when all TX commands have been processed.
25723114fdb4SDavid Greenman 		 */
2573b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
2574b2badf02SMaxime Henrion 		txp->tx_mbuf = NULL;
2575b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
257683e6547dSMaxime Henrion 		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
257783e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
25783114fdb4SDavid Greenman 		/*
25793114fdb4SDavid Greenman 		 * Advance the end of list forward.
25803114fdb4SDavid Greenman 		 */
258183e6547dSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
258283e6547dSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
25835f361cbeSMaxime Henrion 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2584b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
25853114fdb4SDavid Greenman 		sc->tx_queued++;
25863114fdb4SDavid Greenman 		/*
25873114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
25883114fdb4SDavid Greenman 		 */
25893114fdb4SDavid Greenman 		fxp_scb_wait(sc);
25902e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
25913114fdb4SDavid Greenman 		/*
25923114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
25933114fdb4SDavid Greenman 		 * card again.
25943114fdb4SDavid Greenman 		 */
25953114fdb4SDavid Greenman 		ifp->if_timer = 5;
25963114fdb4SDavid Greenman 
2597397f9dfeSDavid Greenman 		return;
2598397f9dfeSDavid Greenman 	}
2599397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2600397f9dfeSDavid Greenman 
2601397f9dfeSDavid Greenman 	/*
2602397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2603397f9dfeSDavid Greenman 	 */
2604397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
260583e6547dSMaxime Henrion 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
260683e6547dSMaxime Henrion 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
260783e6547dSMaxime Henrion 	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2608b2badf02SMaxime Henrion 	txp = &sc->fxp_desc.mcs_tx;
2609b2badf02SMaxime Henrion 	txp->tx_mbuf = NULL;
2610b2badf02SMaxime Henrion 	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2611b2badf02SMaxime Henrion 	txp->tx_next = sc->fxp_desc.tx_list;
261209882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2613b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2614397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2615397f9dfeSDavid Greenman 
2616397f9dfeSDavid Greenman 	/*
2617397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2618397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2619397f9dfeSDavid Greenman 	 */
26207dced78aSDavid Greenman 	count = 100;
2621397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
26227dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
26237dced78aSDavid Greenman 		DELAY(10);
26247dced78aSDavid Greenman 	if (count == 0) {
2625f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
26267dced78aSDavid Greenman 		return;
26277dced78aSDavid Greenman 	}
2628397f9dfeSDavid Greenman 
2629397f9dfeSDavid Greenman 	/*
2630397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2631397f9dfeSDavid Greenman 	 */
2632397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2633b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2634b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
26352e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2636397f9dfeSDavid Greenman 
26373114fdb4SDavid Greenman 	ifp->if_timer = 2;
2638397f9dfeSDavid Greenman 	return;
2639397f9dfeSDavid Greenman }
264072a32a26SJonathan Lemon 
264172a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
264272a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
264372a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
264472a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
264572a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
264672a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
264772a32a26SJonathan Lemon 
264872a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
264972a32a26SJonathan Lemon 
265072a32a26SJonathan Lemon struct ucode {
265172a32a26SJonathan Lemon 	u_int32_t	revision;
265272a32a26SJonathan Lemon 	u_int32_t	*ucode;
265372a32a26SJonathan Lemon 	int		length;
265472a32a26SJonathan Lemon 	u_short		int_delay_offset;
265572a32a26SJonathan Lemon 	u_short		bundle_max_offset;
265672a32a26SJonathan Lemon } ucode_table[] = {
265772a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
265872a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
265972a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
266072a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
266172a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
266272a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
266372a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
266472a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
266572a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
266672a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
266772a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
266872a32a26SJonathan Lemon };
266972a32a26SJonathan Lemon 
267072a32a26SJonathan Lemon static void
267172a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
267272a32a26SJonathan Lemon {
267372a32a26SJonathan Lemon 	struct ucode *uc;
267472a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
267572a32a26SJonathan Lemon 
267672a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
267772a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
267872a32a26SJonathan Lemon 			break;
267972a32a26SJonathan Lemon 	if (uc->ucode == NULL)
268072a32a26SJonathan Lemon 		return;
2681b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
268272a32a26SJonathan Lemon 	cbp->cb_status = 0;
268383e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
268483e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
268572a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
268672a32a26SJonathan Lemon 	if (uc->int_delay_offset)
268783e6547dSMaxime Henrion 		*(u_int16_t *)&cbp->ucode[uc->int_delay_offset] =
268883e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
268972a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
269083e6547dSMaxime Henrion 		*(u_int16_t *)&cbp->ucode[uc->bundle_max_offset] =
269183e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
269272a32a26SJonathan Lemon 	/*
269372a32a26SJonathan Lemon 	 * Download the ucode to the chip.
269472a32a26SJonathan Lemon 	 */
269572a32a26SJonathan Lemon 	fxp_scb_wait(sc);
2696b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2697b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
269872a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
269972a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2700209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2701b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
270272a32a26SJonathan Lemon 	device_printf(sc->dev,
270372a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
270472a32a26SJonathan Lemon 	    sc->tunable_int_delay,
270572a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
270672a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
270772a32a26SJonathan Lemon }
270872a32a26SJonathan Lemon 
270972a32a26SJonathan Lemon static int
271072a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
271172a32a26SJonathan Lemon {
271272a32a26SJonathan Lemon 	int error, value;
271372a32a26SJonathan Lemon 
271472a32a26SJonathan Lemon 	value = *(int *)arg1;
271572a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
271672a32a26SJonathan Lemon 	if (error || !req->newptr)
271772a32a26SJonathan Lemon 		return (error);
271872a32a26SJonathan Lemon 	if (value < low || value > high)
271972a32a26SJonathan Lemon 		return (EINVAL);
272072a32a26SJonathan Lemon 	*(int *)arg1 = value;
272172a32a26SJonathan Lemon 	return (0);
272272a32a26SJonathan Lemon }
272372a32a26SJonathan Lemon 
272472a32a26SJonathan Lemon /*
272572a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
272672a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
272772a32a26SJonathan Lemon  */
272872a32a26SJonathan Lemon static int
272972a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
273072a32a26SJonathan Lemon {
273172a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
273272a32a26SJonathan Lemon }
273372a32a26SJonathan Lemon 
273472a32a26SJonathan Lemon static int
273572a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
273672a32a26SJonathan Lemon {
273772a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
273872a32a26SJonathan Lemon }
2739