xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 2bce79a21dde4ea49f4f790c270e0ace2e906fa3)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28a17c678eSDavid Greenman  */
29a17c678eSDavid Greenman 
30a17c678eSDavid Greenman /*
31ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
32a17c678eSDavid Greenman  */
33a17c678eSDavid Greenman 
3401c516b0SMaxime Henrion #include <sys/cdefs.h>
3501c516b0SMaxime Henrion __FBSDID("$FreeBSD$");
3601c516b0SMaxime Henrion 
37a17c678eSDavid Greenman #include <sys/param.h>
38a17c678eSDavid Greenman #include <sys/systm.h>
3983e6547dSMaxime Henrion #include <sys/endian.h>
40a17c678eSDavid Greenman #include <sys/mbuf.h>
41f7788e8eSJonathan Lemon 		/* #include <sys/mutex.h> */
42a17c678eSDavid Greenman #include <sys/kernel.h>
434458ac71SBruce Evans #include <sys/socket.h>
4472a32a26SJonathan Lemon #include <sys/sysctl.h>
45a17c678eSDavid Greenman 
46a17c678eSDavid Greenman #include <net/if.h>
47397f9dfeSDavid Greenman #include <net/if_dl.h>
48ba8c6fd5SDavid Greenman #include <net/if_media.h>
49a17c678eSDavid Greenman 
50a17c678eSDavid Greenman #include <net/bpf.h>
51ba8c6fd5SDavid Greenman #include <sys/sockio.h>
526182fdbdSPeter Wemm #include <sys/bus.h>
536182fdbdSPeter Wemm #include <machine/bus.h>
546182fdbdSPeter Wemm #include <sys/rman.h>
556182fdbdSPeter Wemm #include <machine/resource.h>
56ba8c6fd5SDavid Greenman 
571d5e9e22SEivind Eklund #include <net/ethernet.h>
581d5e9e22SEivind Eklund #include <net/if_arp.h>
59ba8c6fd5SDavid Greenman 
60f7788e8eSJonathan Lemon #include <machine/clock.h>	/* for DELAY */
61a17c678eSDavid Greenman 
62e8c8b728SJonathan Lemon #include <net/if_types.h>
63e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
64e8c8b728SJonathan Lemon 
65c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
66c8bca6dcSBill Paul #include <netinet/in.h>
67c8bca6dcSBill Paul #include <netinet/in_systm.h>
68c8bca6dcSBill Paul #include <netinet/ip.h>
69c8bca6dcSBill Paul #include <machine/in_cksum.h>
70c8bca6dcSBill Paul #endif
71c8bca6dcSBill Paul 
72a17c678eSDavid Greenman #include <pci/pcivar.h>
73df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
74a17c678eSDavid Greenman 
75f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
76f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
77f7788e8eSJonathan Lemon 
78f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8072a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
81f7788e8eSJonathan Lemon 
82f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
83f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
84f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
85f7788e8eSJonathan Lemon #include "miibus_if.h"
864fc1dda9SAndrew Gallatin 
87ba8c6fd5SDavid Greenman /*
88ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
89ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
90ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
91ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
92ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
93ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
94ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
95ba8c6fd5SDavid Greenman  */
96ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
97ba8c6fd5SDavid Greenman 
98ba8c6fd5SDavid Greenman /*
99f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
100f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
101f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
102f7788e8eSJonathan Lemon  */
103f7788e8eSJonathan Lemon static int tx_threshold = 64;
104f7788e8eSJonathan Lemon 
105f7788e8eSJonathan Lemon /*
106f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
107f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
108f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
109f7788e8eSJonathan Lemon  * to fxp_init.
110f7788e8eSJonathan Lemon  *
111f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
112f7788e8eSJonathan Lemon  */
113f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
114f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
115f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
116f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
117f7788e8eSJonathan Lemon 	0x0,	/*  0 */
118f7788e8eSJonathan Lemon 	0x0,	/*  1 */
119f7788e8eSJonathan Lemon 	0x0,	/*  2 */
120f7788e8eSJonathan Lemon 	0x0,	/*  3 */
121f7788e8eSJonathan Lemon 	0x0,	/*  4 */
122f7788e8eSJonathan Lemon 	0x0,	/*  5 */
123f7788e8eSJonathan Lemon 	0x32,	/*  6 */
124f7788e8eSJonathan Lemon 	0x0,	/*  7 */
125f7788e8eSJonathan Lemon 	0x0,	/*  8 */
126f7788e8eSJonathan Lemon 	0x0,	/*  9 */
127f7788e8eSJonathan Lemon 	0x6,	/* 10 */
128f7788e8eSJonathan Lemon 	0x0,	/* 11 */
129f7788e8eSJonathan Lemon 	0x0,	/* 12 */
130f7788e8eSJonathan Lemon 	0x0,	/* 13 */
131f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
132f7788e8eSJonathan Lemon 	0x48,	/* 15 */
133f7788e8eSJonathan Lemon 	0x0,	/* 16 */
134f7788e8eSJonathan Lemon 	0x40,	/* 17 */
135f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
136f7788e8eSJonathan Lemon 	0x0,	/* 19 */
137f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
138f7788e8eSJonathan Lemon 	0x5	/* 21 */
139f7788e8eSJonathan Lemon };
140f7788e8eSJonathan Lemon 
141f7788e8eSJonathan Lemon struct fxp_ident {
142f7788e8eSJonathan Lemon 	u_int16_t	devid;
143f7788e8eSJonathan Lemon 	char 		*name;
144f7788e8eSJonathan Lemon };
145f7788e8eSJonathan Lemon 
146f7788e8eSJonathan Lemon /*
147f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
148f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
149f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
150f7788e8eSJonathan Lemon  * them.
151f7788e8eSJonathan Lemon  */
152f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
153537b41d5SJohn Polstra     { 0x1029,		"Intel 82559 PCI/CardBus Pro/100" },
154537b41d5SJohn Polstra     { 0x1030,		"Intel 82559 Pro/100 Ethernet" },
155537b41d5SJohn Polstra     { 0x1031,		"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
156537b41d5SJohn Polstra     { 0x1032,		"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
157537b41d5SJohn Polstra     { 0x1033,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
158537b41d5SJohn Polstra     { 0x1034,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
159537b41d5SJohn Polstra     { 0x1035,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
160537b41d5SJohn Polstra     { 0x1036,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
161537b41d5SJohn Polstra     { 0x1037,		"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
162537b41d5SJohn Polstra     { 0x1038,		"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
163537b41d5SJohn Polstra     { 0x1039,		"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
164537b41d5SJohn Polstra     { 0x103A,		"Intel 82801DB (ICH4) Pro/100 Ethernet" },
165537b41d5SJohn Polstra     { 0x103B,		"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
166537b41d5SJohn Polstra     { 0x103C,		"Intel 82801DB (ICH4) Pro/100 Ethernet" },
167537b41d5SJohn Polstra     { 0x103D,		"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
168537b41d5SJohn Polstra     { 0x103E,		"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
169537b41d5SJohn Polstra     { 0x1059,		"Intel 82551QM Pro/100 M Mobile Connection" },
170537b41d5SJohn Polstra     { 0x1209,		"Intel 82559ER Embedded 10/100 Ethernet" },
171537b41d5SJohn Polstra     { 0x1229,		"Intel 82557/8/9 EtherExpress Pro/100(B) Ethernet" },
172537b41d5SJohn Polstra     { 0x2449,		"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
173f7788e8eSJonathan Lemon     { 0,		NULL },
174f7788e8eSJonathan Lemon };
175f7788e8eSJonathan Lemon 
176c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
177c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
178c8bca6dcSBill Paul #else
179c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
180c8bca6dcSBill Paul #endif
181c8bca6dcSBill Paul 
182f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
183f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
184f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
185f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
186f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
187f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
188f7788e8eSJonathan Lemon 
189f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
190f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
191f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
19248e417ebSJonathan Lemon static void		fxp_powerstate_d0(device_t dev);
193f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
194f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
195f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
196f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
197f7788e8eSJonathan Lemon 			    caddr_t data);
198f7788e8eSJonathan Lemon static void 		fxp_watchdog(struct ifnet *ifp);
199b2badf02SMaxime Henrion static int		fxp_add_rfabuf(struct fxp_softc *sc,
200b2badf02SMaxime Henrion     			    struct fxp_rx *rxp);
20109882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
202f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
203f7788e8eSJonathan Lemon static u_int16_t	fxp_eeprom_getword(struct fxp_softc *sc, int offset,
204f7788e8eSJonathan Lemon 			    int autosize);
20500c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
20600c4116bSJonathan Lemon 			    u_int16_t data);
207f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
208f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
209f7788e8eSJonathan Lemon 			    int offset, int words);
21000c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
21100c4116bSJonathan Lemon 			    int offset, int words);
212f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
213f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
214f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
215f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
216f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
217f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
218f7788e8eSJonathan Lemon static volatile int	fxp_miibus_readreg(device_t dev, int phy, int reg);
219f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
220f7788e8eSJonathan Lemon 			    int value);
22172a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
22272a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
22372a32a26SJonathan Lemon 			    int low, int high);
22472a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
22572a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
226f7788e8eSJonathan Lemon static __inline void 	fxp_scb_wait(struct fxp_softc *sc);
2272e2b8238SJonathan Lemon static __inline void	fxp_scb_cmd(struct fxp_softc *sc, int cmd);
228209b07bcSMaxime Henrion static __inline void	fxp_dma_wait(struct fxp_softc *sc,
229209b07bcSMaxime Henrion     			    volatile u_int16_t *status, bus_dma_tag_t dmat,
230209b07bcSMaxime Henrion 			    bus_dmamap_t map);
231f7788e8eSJonathan Lemon 
232f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
233f7788e8eSJonathan Lemon 	/* Device interface */
234f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
235f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
236f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
237f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
238f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
239f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
240f7788e8eSJonathan Lemon 
241f7788e8eSJonathan Lemon 	/* MII interface */
242f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
243f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
244f7788e8eSJonathan Lemon 
245f7788e8eSJonathan Lemon 	{ 0, 0 }
246f7788e8eSJonathan Lemon };
247f7788e8eSJonathan Lemon 
248f7788e8eSJonathan Lemon static driver_t fxp_driver = {
249f7788e8eSJonathan Lemon 	"fxp",
250f7788e8eSJonathan Lemon 	fxp_methods,
251f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
252f7788e8eSJonathan Lemon };
253f7788e8eSJonathan Lemon 
254f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
255f7788e8eSJonathan Lemon 
256f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
257f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
258f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
259f7788e8eSJonathan Lemon 
2602b5989e9SLuigi Rizzo static int fxp_rnr;
2612b5989e9SLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, fxp_rnr, CTLFLAG_RW, &fxp_rnr, 0, "fxp rnr events");
2622b5989e9SLuigi Rizzo 
263f7788e8eSJonathan Lemon /*
264dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
265dfe61cf1SDavid Greenman  * completed).
266dfe61cf1SDavid Greenman  */
267c1087c13SBruce Evans static __inline void
268f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
269a17c678eSDavid Greenman {
270a17c678eSDavid Greenman 	int i = 10000;
271a17c678eSDavid Greenman 
2727dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
2737dced78aSDavid Greenman 		DELAY(2);
2747dced78aSDavid Greenman 	if (i == 0)
27500c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
276e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
277e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
278e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
279e8c8b728SJonathan Lemon 		    CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
2807dced78aSDavid Greenman }
2817dced78aSDavid Greenman 
2827dced78aSDavid Greenman static __inline void
2832e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
2842e2b8238SJonathan Lemon {
2852e2b8238SJonathan Lemon 
2862e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
2872e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
2882e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
2892e2b8238SJonathan Lemon 	}
2902e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
2912e2b8238SJonathan Lemon }
2922e2b8238SJonathan Lemon 
2932e2b8238SJonathan Lemon static __inline void
294209b07bcSMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile u_int16_t *status,
295209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
2967dced78aSDavid Greenman {
2977dced78aSDavid Greenman 	int i = 10000;
2987dced78aSDavid Greenman 
299209b07bcSMaxime Henrion 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
300209b07bcSMaxime Henrion 	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) {
3017dced78aSDavid Greenman 		DELAY(2);
302209b07bcSMaxime Henrion 		bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
303209b07bcSMaxime Henrion 	}
3047dced78aSDavid Greenman 	if (i == 0)
305f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
306a17c678eSDavid Greenman }
307a17c678eSDavid Greenman 
308dfe61cf1SDavid Greenman /*
309dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
310dfe61cf1SDavid Greenman  */
3116182fdbdSPeter Wemm static int
3126182fdbdSPeter Wemm fxp_probe(device_t dev)
313a17c678eSDavid Greenman {
314f7788e8eSJonathan Lemon 	u_int16_t devid;
315f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
316f7788e8eSJonathan Lemon 
31755ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
318f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
319f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
320f7788e8eSJonathan Lemon 			if (ident->devid == devid) {
321f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
322f7788e8eSJonathan Lemon 				return (0);
32355ce7b51SDavid Greenman 			}
324dd68ef16SPeter Wemm 		}
325f7788e8eSJonathan Lemon 	}
326f7788e8eSJonathan Lemon 	return (ENXIO);
3276182fdbdSPeter Wemm }
3286182fdbdSPeter Wemm 
32948e417ebSJonathan Lemon static void
33048e417ebSJonathan Lemon fxp_powerstate_d0(device_t dev)
33148e417ebSJonathan Lemon {
33248e417ebSJonathan Lemon #if __FreeBSD_version >= 430002
33348e417ebSJonathan Lemon 	u_int32_t iobase, membase, irq;
33448e417ebSJonathan Lemon 
33548e417ebSJonathan Lemon 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
33648e417ebSJonathan Lemon 		/* Save important PCI config data. */
33748e417ebSJonathan Lemon 		iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
33848e417ebSJonathan Lemon 		membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
33948e417ebSJonathan Lemon 		irq = pci_read_config(dev, PCIR_INTLINE, 4);
34048e417ebSJonathan Lemon 
34148e417ebSJonathan Lemon 		/* Reset the power state. */
34248e417ebSJonathan Lemon 		device_printf(dev, "chip is in D%d power mode "
34348e417ebSJonathan Lemon 		    "-- setting to D0\n", pci_get_powerstate(dev));
34448e417ebSJonathan Lemon 
34548e417ebSJonathan Lemon 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
34648e417ebSJonathan Lemon 
34748e417ebSJonathan Lemon 		/* Restore PCI config data. */
34848e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
34948e417ebSJonathan Lemon 		pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
35048e417ebSJonathan Lemon 		pci_write_config(dev, PCIR_INTLINE, irq, 4);
35148e417ebSJonathan Lemon 	}
35248e417ebSJonathan Lemon #endif
35348e417ebSJonathan Lemon }
35448e417ebSJonathan Lemon 
355b2badf02SMaxime Henrion static void
356b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
357b2badf02SMaxime Henrion {
358b2badf02SMaxime Henrion 	u_int32_t *addr;
359b2badf02SMaxime Henrion 
360b2badf02SMaxime Henrion 	if (error)
361b2badf02SMaxime Henrion 		return;
362b2badf02SMaxime Henrion 
363b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
364b2badf02SMaxime Henrion 	addr = arg;
365b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
366b2badf02SMaxime Henrion }
367b2badf02SMaxime Henrion 
3686182fdbdSPeter Wemm static int
3696182fdbdSPeter Wemm fxp_attach(device_t dev)
370a17c678eSDavid Greenman {
3716182fdbdSPeter Wemm 	int error = 0;
3726182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
373ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
374b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
3759fa6ccfbSMatt Jacob 	u_int32_t val;
37683e6547dSMaxime Henrion 	u_int16_t data, myea[ETHER_ADDR_LEN / 2];
377d73e2e55SMaxime Henrion 	int i, rid, m1, m2, prefer_iomap, maxtxseg;
378f7788e8eSJonathan Lemon 	int s;
379a17c678eSDavid Greenman 
380f7788e8eSJonathan Lemon 	bzero(sc, sizeof(*sc));
381f7788e8eSJonathan Lemon 	sc->dev = dev;
3826c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
383a1a9c8f7SJonathan Lemon 	sysctl_ctx_init(&sc->sysctl_ctx);
3846008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
3856008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
386a17c678eSDavid Greenman 
387f7788e8eSJonathan Lemon 	s = splimp();
388a17c678eSDavid Greenman 
389dfe61cf1SDavid Greenman 	/*
3902bce79a2SMaxim Sobolev 	 * Enable bus mastering.
391df373873SWes Peters 	 */
392cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
3939fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
394df373873SWes Peters 
39548e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
3968d799694SBill Paul 
397df373873SWes Peters 	/*
3989fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
3999fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4009fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
401dfe61cf1SDavid Greenman 	 */
4029fa6ccfbSMatt Jacob 	m1 = PCIM_CMD_MEMEN;
4039fa6ccfbSMatt Jacob 	m2 = PCIM_CMD_PORTEN;
4042a05a4ebSMatt Jacob 	prefer_iomap = 0;
4052a05a4ebSMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
4062a05a4ebSMatt Jacob 	    "prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
4079fa6ccfbSMatt Jacob 		m1 = PCIM_CMD_PORTEN;
4089fa6ccfbSMatt Jacob 		m2 = PCIM_CMD_MEMEN;
4099fa6ccfbSMatt Jacob 	}
4109fa6ccfbSMatt Jacob 
411533294b9SMatthew N. Dodd 	sc->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4129fa6ccfbSMatt Jacob 	sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4139fa6ccfbSMatt Jacob 	sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4146182fdbdSPeter Wemm 	                                     0, ~0, 1, RF_ACTIVE);
415533294b9SMatthew N. Dodd 	if (sc->mem == NULL) {
4169fa6ccfbSMatt Jacob 		sc->rtp =
4179fa6ccfbSMatt Jacob 		    (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
4189fa6ccfbSMatt Jacob 		sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
4199fa6ccfbSMatt Jacob 		sc->mem = bus_alloc_resource(dev, sc->rtp, &sc->rgd,
4209fa6ccfbSMatt Jacob                                             0, ~0, 1, RF_ACTIVE);
4219fa6ccfbSMatt Jacob 	}
4229fa6ccfbSMatt Jacob 
4236182fdbdSPeter Wemm 	if (!sc->mem) {
4246182fdbdSPeter Wemm 		error = ENXIO;
425a17c678eSDavid Greenman 		goto fail;
426a17c678eSDavid Greenman         }
4279fa6ccfbSMatt Jacob 	if (bootverbose) {
4289fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
4299fa6ccfbSMatt Jacob 		   sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
4309fa6ccfbSMatt Jacob 	}
4314fc1dda9SAndrew Gallatin 
4324fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
4334fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
434a17c678eSDavid Greenman 
435a17c678eSDavid Greenman 	/*
436dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
437dfe61cf1SDavid Greenman 	 */
4386182fdbdSPeter Wemm 	rid = 0;
4396182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
4406182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
4416182fdbdSPeter Wemm 	if (sc->irq == NULL) {
4426182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
4436182fdbdSPeter Wemm 		error = ENXIO;
4446182fdbdSPeter Wemm 		goto fail;
4456182fdbdSPeter Wemm 	}
4466182fdbdSPeter Wemm 
447f7788e8eSJonathan Lemon 	/*
448f7788e8eSJonathan Lemon 	 * Reset to a stable state.
449f7788e8eSJonathan Lemon 	 */
450f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
451f7788e8eSJonathan Lemon 	DELAY(10);
452f7788e8eSJonathan Lemon 
453f7788e8eSJonathan Lemon 	/*
454f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
455f7788e8eSJonathan Lemon 	 */
456f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
457f7788e8eSJonathan Lemon 
458f7788e8eSJonathan Lemon 	/*
4593bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
460f7788e8eSJonathan Lemon 	 */
461f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
462f7788e8eSJonathan Lemon 	if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
463f7788e8eSJonathan Lemon 	    (data & FXP_PHY_SERIAL_ONLY))
464dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
465f7788e8eSJonathan Lemon 
466f7788e8eSJonathan Lemon 	/*
46772a32a26SJonathan Lemon 	 * Create the sysctl tree
46872a32a26SJonathan Lemon 	 */
46972a32a26SJonathan Lemon 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
47072a32a26SJonathan Lemon 	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
47172a32a26SJonathan Lemon 	    device_get_nameunit(dev), CTLFLAG_RD, 0, "");
47272a32a26SJonathan Lemon 	if (sc->sysctl_tree == NULL)
47372a32a26SJonathan Lemon 		goto fail;
47472a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
47572a32a26SJonathan Lemon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
476858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
47772a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
47872a32a26SJonathan Lemon 	SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
47972a32a26SJonathan Lemon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
480858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
48172a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
48272a32a26SJonathan Lemon 
48372a32a26SJonathan Lemon 	/*
48472a32a26SJonathan Lemon 	 * Pull in device tunables.
48572a32a26SJonathan Lemon 	 */
48672a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
48772a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
48872a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
48972a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
49072a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
49172a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
49272a32a26SJonathan Lemon 
49372a32a26SJonathan Lemon 	/*
49472a32a26SJonathan Lemon 	 * Find out the chip revision; lump all 82557 revs together.
4953bd07cfdSJonathan Lemon 	 */
4963bd07cfdSJonathan Lemon 	fxp_read_eeprom(sc, &data, 5, 1);
4973bd07cfdSJonathan Lemon 	if ((data >> 8) == 1)
49872a32a26SJonathan Lemon 		sc->revision = FXP_REV_82557;
49972a32a26SJonathan Lemon 	else
50072a32a26SJonathan Lemon 		sc->revision = pci_get_revid(dev);
5013bd07cfdSJonathan Lemon 
5023bd07cfdSJonathan Lemon 	/*
5032e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
50400c4116bSJonathan Lemon 	 *
50572a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
50672a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
50772a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
50800c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
50900c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
51000c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
51100c4116bSJonathan Lemon 	 *
51200c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5132e2b8238SJonathan Lemon 	 */
5142e2b8238SJonathan Lemon 	i = pci_get_device(dev);
51572a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
51672a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
51700c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
51800c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
51900c4116bSJonathan Lemon 			u_int16_t cksum;
52000c4116bSJonathan Lemon 			int i;
52100c4116bSJonathan Lemon 
52200c4116bSJonathan Lemon 			device_printf(dev,
523001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
52400c4116bSJonathan Lemon 			data &= ~0x02;
52500c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
52600c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
52700c4116bSJonathan Lemon 			cksum = 0;
52800c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
52900c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
53000c4116bSJonathan Lemon 				cksum += data;
53100c4116bSJonathan Lemon 			}
53200c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
53300c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
53400c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
53500c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
53600c4116bSJonathan Lemon 			device_printf(dev,
53700c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
53800c4116bSJonathan Lemon 			    i, data, cksum);
53900c4116bSJonathan Lemon #if 1
54000c4116bSJonathan Lemon 			/*
54100c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
54200c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
54300c4116bSJonathan Lemon 			 */
5442e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
54500c4116bSJonathan Lemon #endif
54600c4116bSJonathan Lemon 		}
54700c4116bSJonathan Lemon 	}
5482e2b8238SJonathan Lemon 
5492e2b8238SJonathan Lemon 	/*
5503bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5513bd07cfdSJonathan Lemon 	 */
55272a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5533bd07cfdSJonathan Lemon 		/*
55474396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
55574396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
55674396a0aSJonathan Lemon 		 * the board to turn on MWI.
5573bd07cfdSJonathan Lemon 		 */
55874396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
55974396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5603bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5613bd07cfdSJonathan Lemon 
5623bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5633bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
564920b58e8SBrooks Davis 
565e8c8b728SJonathan Lemon 		/* enable reception of long frames for VLAN */
566e8c8b728SJonathan Lemon 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
5673bd07cfdSJonathan Lemon 	}
5683bd07cfdSJonathan Lemon 
5693bd07cfdSJonathan Lemon 	/*
570c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
571c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
572c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
573c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
574c8bca6dcSBill Paul 	 */
575c8bca6dcSBill Paul 
576c8bca6dcSBill Paul 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C) {
577c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
578c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
579c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
580c8bca6dcSBill Paul 	} else {
581c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
582c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
583c8bca6dcSBill Paul 	}
584c8bca6dcSBill Paul 
585c8bca6dcSBill Paul 	/*
586b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
587b2badf02SMaxime Henrion 	 */
588d73e2e55SMaxime Henrion 	maxtxseg = sc->flags & FXP_FLAG_EXT_RFA ? FXP_NTXSEG - 1 : FXP_NTXSEG;
589b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 2, 0, BUS_SPACE_MAXADDR_32BIT,
590d73e2e55SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * maxtxseg,
591d73e2e55SMaxime Henrion 	    maxtxseg, MCLBYTES, 0, &sc->fxp_mtag);
592b2badf02SMaxime Henrion 	if (error) {
593b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
594b2badf02SMaxime Henrion 		goto fail;
595b2badf02SMaxime Henrion 	}
596b2badf02SMaxime Henrion 
597b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
598b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_stats), 1,
599d73e2e55SMaxime Henrion 	    sizeof(struct fxp_stats), 0, &sc->fxp_stag);
600b2badf02SMaxime Henrion 	if (error) {
601b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
602b2badf02SMaxime Henrion 		goto fail;
603b2badf02SMaxime Henrion 	}
604b2badf02SMaxime Henrion 
605b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
606b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->fxp_smap);
607b2badf02SMaxime Henrion 	if (error)
608b2badf02SMaxime Henrion 		goto failmem;
609b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
610b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
611b2badf02SMaxime Henrion 	if (error) {
612b2badf02SMaxime Henrion 		device_printf(dev, "could not map the stats buffer\n");
613b2badf02SMaxime Henrion 		goto fail;
614b2badf02SMaxime Henrion 	}
615b2badf02SMaxime Henrion 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
616b2badf02SMaxime Henrion 
617b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
618b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, FXP_TXCB_SZ, 1,
619d73e2e55SMaxime Henrion 	    FXP_TXCB_SZ, 0, &sc->cbl_tag);
620b2badf02SMaxime Henrion 	if (error) {
621b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
622b2badf02SMaxime Henrion 		goto fail;
623b2badf02SMaxime Henrion 	}
624b2badf02SMaxime Henrion 
625b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
626b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->cbl_map);
627b2badf02SMaxime Henrion 	if (error)
628b2badf02SMaxime Henrion 		goto failmem;
629b2badf02SMaxime Henrion 	bzero(sc->fxp_desc.cbl_list, FXP_TXCB_SZ);
630b2badf02SMaxime Henrion 
631b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
632b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
633b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
634b2badf02SMaxime Henrion 	if (error) {
635b2badf02SMaxime Henrion 		device_printf(dev, "could not map DMA memory\n");
636b2badf02SMaxime Henrion 		goto fail;
637b2badf02SMaxime Henrion 	}
638b2badf02SMaxime Henrion 
639b2badf02SMaxime Henrion 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
640b2badf02SMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct fxp_cb_mcs), 1,
641d73e2e55SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), 0, &sc->mcs_tag);
642b2badf02SMaxime Henrion 	if (error) {
643b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
644b2badf02SMaxime Henrion 		goto fail;
645b2badf02SMaxime Henrion 	}
646b2badf02SMaxime Henrion 
647b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
648b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->mcs_map);
649b2badf02SMaxime Henrion 	if (error)
650b2badf02SMaxime Henrion 		goto failmem;
651b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
652b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
653b2badf02SMaxime Henrion 	if (error) {
654b2badf02SMaxime Henrion 		device_printf(dev, "can't map the multicast setup command\n");
655b2badf02SMaxime Henrion 		goto fail;
656b2badf02SMaxime Henrion 	}
657b2badf02SMaxime Henrion 
658b2badf02SMaxime Henrion 	/*
659b2badf02SMaxime Henrion 	 * Pre-allocate the TX DMA maps.
660b2badf02SMaxime Henrion 	 */
6614cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
662b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0,
663b2badf02SMaxime Henrion 		    &sc->fxp_desc.tx_list[i].tx_map);
664b2badf02SMaxime Henrion 		if (error) {
665b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
666b2badf02SMaxime Henrion 			goto fail;
667b2badf02SMaxime Henrion 		}
668b2badf02SMaxime Henrion 	}
669b2badf02SMaxime Henrion 	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
670b2badf02SMaxime Henrion 	if (error) {
671b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
672b2badf02SMaxime Henrion 		goto fail;
673b2badf02SMaxime Henrion 	}
674b2badf02SMaxime Henrion 
675b2badf02SMaxime Henrion 	/*
676b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
677b2badf02SMaxime Henrion 	 */
678b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
679b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
680b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
681b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
682b2badf02SMaxime Henrion 		if (error) {
683b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
684b2badf02SMaxime Henrion 			goto fail;
685b2badf02SMaxime Henrion 		}
686b2badf02SMaxime Henrion 		if (fxp_add_rfabuf(sc, rxp) != 0)
687b2badf02SMaxime Henrion 			goto failmem;
688b2badf02SMaxime Henrion 	}
689b2badf02SMaxime Henrion 
690b2badf02SMaxime Henrion 	/*
691f7788e8eSJonathan Lemon 	 * Read MAC address.
692f7788e8eSJonathan Lemon 	 */
69383e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
69483e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[0] = myea[0] & 0xff;
69583e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[1] = myea[0] >> 8;
69683e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[2] = myea[1] & 0xff;
69783e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[3] = myea[1] >> 8;
69883e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[4] = myea[2] & 0xff;
69983e6547dSMaxime Henrion 	sc->arpcom.ac_enaddr[5] = myea[2] >> 8;
700f7788e8eSJonathan Lemon 	device_printf(dev, "Ethernet address %6D%s\n",
701f7788e8eSJonathan Lemon 	    sc->arpcom.ac_enaddr, ":",
702f7788e8eSJonathan Lemon 	    sc->flags & FXP_FLAG_SERIAL_MEDIA ? ", 10Mbps" : "");
703f7788e8eSJonathan Lemon 	if (bootverbose) {
7042e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
705f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7062e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7072e2b8238SJonathan Lemon 		    pci_get_revid(dev));
70872a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
70972a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
71072a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
711f7788e8eSJonathan Lemon 	}
712f7788e8eSJonathan Lemon 
713f7788e8eSJonathan Lemon 	/*
714f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
715f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
716f7788e8eSJonathan Lemon 	 *
717f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
718f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
719f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
720f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
721f7788e8eSJonathan Lemon 	 */
722f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
723f7788e8eSJonathan Lemon 		ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
724f7788e8eSJonathan Lemon 		    fxp_serial_ifmedia_sts);
725f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
726f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
727f7788e8eSJonathan Lemon 	} else {
728f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
729f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
730f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
7316182fdbdSPeter Wemm 			error = ENXIO;
732ba8c6fd5SDavid Greenman 			goto fail;
733a17c678eSDavid Greenman 		}
734f7788e8eSJonathan Lemon 	}
735dccee1a1SDavid Greenman 
736a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
7376182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
738a17c678eSDavid Greenman 	ifp->if_name = "fxp";
739a17c678eSDavid Greenman 	ifp->if_output = ether_output;
740a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
741fb583156SDavid Greenman 	ifp->if_init = fxp_init;
742ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
743ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
744ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
745ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
746ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
747a17c678eSDavid Greenman 
748c8bca6dcSBill Paul 	/* Enable checksum offload for 82550 or better chips */
749c8bca6dcSBill Paul 
750c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
751c8bca6dcSBill Paul 		ifp->if_hwassist = FXP_CSUM_FEATURES;
752c8bca6dcSBill Paul 		ifp->if_capabilities = IFCAP_HWCSUM;
753c6d8cd1eSBill Paul 		ifp->if_capenable = ifp->if_capabilities;
754c8bca6dcSBill Paul 	}
755c8bca6dcSBill Paul 
756dfe61cf1SDavid Greenman 	/*
757e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
758e8c8b728SJonathan Lemon 	 */
759e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
760673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
761e8c8b728SJonathan Lemon 
762483b9871SDavid Greenman 	/*
7633114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
7643114fdb4SDavid Greenman 	 * TX descriptors.
765483b9871SDavid Greenman 	 */
7663114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
7674a684684SDavid Greenman 
768201afb0eSMaxime Henrion 	/*
769201afb0eSMaxime Henrion 	 * Attach the interface.
770201afb0eSMaxime Henrion 	 */
771201afb0eSMaxime Henrion 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
772201afb0eSMaxime Henrion 
773201afb0eSMaxime Henrion 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
774201afb0eSMaxime Henrion 	    fxp_intr, sc, &sc->ih);
775201afb0eSMaxime Henrion 	if (error) {
776201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
777201afb0eSMaxime Henrion 		goto fail;
778201afb0eSMaxime Henrion 	}
779201afb0eSMaxime Henrion 
780f7788e8eSJonathan Lemon 	splx(s);
781f7788e8eSJonathan Lemon 	return (0);
782a17c678eSDavid Greenman 
783f7788e8eSJonathan Lemon failmem:
784f7788e8eSJonathan Lemon 	device_printf(dev, "Failed to malloc memory\n");
785f7788e8eSJonathan Lemon 	error = ENOMEM;
786a17c678eSDavid Greenman fail:
787f7788e8eSJonathan Lemon 	splx(s);
788f7788e8eSJonathan Lemon 	fxp_release(sc);
789f7788e8eSJonathan Lemon 	return (error);
790f7788e8eSJonathan Lemon }
791f7788e8eSJonathan Lemon 
792f7788e8eSJonathan Lemon /*
793f7788e8eSJonathan Lemon  * release all resources
794f7788e8eSJonathan Lemon  */
795f7788e8eSJonathan Lemon static void
796f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
797f7788e8eSJonathan Lemon {
798b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
799b2badf02SMaxime Henrion 	struct fxp_tx *txp;
800b2badf02SMaxime Henrion 	int i;
801b2badf02SMaxime Henrion 
802b983c7b3SMaxime Henrion 	if (sc->ih)
803b983c7b3SMaxime Henrion 		bus_teardown_intr(sc->dev, sc->irq, sc->ih);
804b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
805b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
806b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
807b2badf02SMaxime Henrion 		    sc->cbl_map);
808b2badf02SMaxime Henrion 	}
809b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
810b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
811b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
812b2badf02SMaxime Henrion 	}
813b2badf02SMaxime Henrion 	if (sc->mcsp) {
814b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
815b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
816b2badf02SMaxime Henrion 	}
817f7788e8eSJonathan Lemon 	if (sc->irq)
818f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->irq);
819f7788e8eSJonathan Lemon 	if (sc->mem)
820f7788e8eSJonathan Lemon 		bus_release_resource(sc->dev, sc->rtp, sc->rgd, sc->mem);
821b983c7b3SMaxime Henrion 	if (sc->fxp_mtag) {
822b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
823b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
824b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
825b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
826b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
827b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
828b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
829b983c7b3SMaxime Henrion 			}
830b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
831b983c7b3SMaxime Henrion 		}
832b983c7b3SMaxime Henrion 		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
833b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_mtag);
834b983c7b3SMaxime Henrion 	}
835b983c7b3SMaxime Henrion 	if (sc->fxp_stag) {
836b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
837b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
838b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
839b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
840b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
841b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
842b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
843b983c7b3SMaxime Henrion 			}
844b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
845b983c7b3SMaxime Henrion 		}
846b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
847b983c7b3SMaxime Henrion 	}
848b2badf02SMaxime Henrion 	if (sc->cbl_tag)
849b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
850b2badf02SMaxime Henrion 	if (sc->mcs_tag)
851b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
85272a32a26SJonathan Lemon 
85372a32a26SJonathan Lemon         sysctl_ctx_free(&sc->sysctl_ctx);
85472a32a26SJonathan Lemon 
8550f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
8566182fdbdSPeter Wemm }
8576182fdbdSPeter Wemm 
8586182fdbdSPeter Wemm /*
8596182fdbdSPeter Wemm  * Detach interface.
8606182fdbdSPeter Wemm  */
8616182fdbdSPeter Wemm static int
8626182fdbdSPeter Wemm fxp_detach(device_t dev)
8636182fdbdSPeter Wemm {
8646182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
865f7788e8eSJonathan Lemon 	int s;
8666182fdbdSPeter Wemm 
867f7788e8eSJonathan Lemon 	s = splimp();
8686182fdbdSPeter Wemm 	/*
869f7788e8eSJonathan Lemon 	 * Close down routes etc.
8706182fdbdSPeter Wemm 	 */
871673d9191SSam Leffler 	ether_ifdetach(&sc->arpcom.ac_if);
87220f0c80fSMaxime Henrion 
87320f0c80fSMaxime Henrion 	/*
87420f0c80fSMaxime Henrion 	 * Stop DMA and drop transmit queue.
87520f0c80fSMaxime Henrion 	 */
87620f0c80fSMaxime Henrion 	if (bus_child_present(dev)) {
87720f0c80fSMaxime Henrion 		/* disable interrupts */
87820f0c80fSMaxime Henrion 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
87920f0c80fSMaxime Henrion 		fxp_stop(sc);
88020f0c80fSMaxime Henrion 	}
88120f0c80fSMaxime Henrion 
882b983c7b3SMaxime Henrion 	device_delete_child(dev, sc->miibus);
883b983c7b3SMaxime Henrion 	bus_generic_detach(dev);
8846182fdbdSPeter Wemm 	/*
8856182fdbdSPeter Wemm 	 * Free all media structures.
8866182fdbdSPeter Wemm 	 */
8876182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
8886182fdbdSPeter Wemm 
889f7788e8eSJonathan Lemon 	splx(s);
8906182fdbdSPeter Wemm 
891f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
892f7788e8eSJonathan Lemon 	fxp_release(sc);
893f7788e8eSJonathan Lemon 	return (0);
894a17c678eSDavid Greenman }
895a17c678eSDavid Greenman 
896a17c678eSDavid Greenman /*
8974a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
898a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
899a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
900a17c678eSDavid Greenman  */
9016182fdbdSPeter Wemm static int
9026182fdbdSPeter Wemm fxp_shutdown(device_t dev)
903a17c678eSDavid Greenman {
9046182fdbdSPeter Wemm 	/*
9056182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
9066182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
9076182fdbdSPeter Wemm 	 * reboot before the driver initializes.
9086182fdbdSPeter Wemm 	 */
9096182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
910f7788e8eSJonathan Lemon 	return (0);
911a17c678eSDavid Greenman }
912a17c678eSDavid Greenman 
9137dced78aSDavid Greenman /*
9147dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
9157dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
9167dced78aSDavid Greenman  * resume.
9177dced78aSDavid Greenman  */
9187dced78aSDavid Greenman static int
9197dced78aSDavid Greenman fxp_suspend(device_t dev)
9207dced78aSDavid Greenman {
9217dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
922f7788e8eSJonathan Lemon 	int i, s;
9237dced78aSDavid Greenman 
924f7788e8eSJonathan Lemon 	s = splimp();
9257dced78aSDavid Greenman 
9267dced78aSDavid Greenman 	fxp_stop(sc);
9277dced78aSDavid Greenman 
9287dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
9297dced78aSDavid Greenman 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
9307dced78aSDavid Greenman 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
9317dced78aSDavid Greenman 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
9327dced78aSDavid Greenman 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
9337dced78aSDavid Greenman 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
9347dced78aSDavid Greenman 
9357dced78aSDavid Greenman 	sc->suspended = 1;
9367dced78aSDavid Greenman 
937f7788e8eSJonathan Lemon 	splx(s);
938f7788e8eSJonathan Lemon 	return (0);
9397dced78aSDavid Greenman }
9407dced78aSDavid Greenman 
9417dced78aSDavid Greenman /*
9427dced78aSDavid Greenman  * Device resume routine.  Restore some PCI settings in case the BIOS
9437dced78aSDavid Greenman  * doesn't, re-enable busmastering, and restart the interface if
9447dced78aSDavid Greenman  * appropriate.
9457dced78aSDavid Greenman  */
9467dced78aSDavid Greenman static int
9477dced78aSDavid Greenman fxp_resume(device_t dev)
9487dced78aSDavid Greenman {
9497dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
9507dced78aSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
9517dced78aSDavid Greenman 	u_int16_t pci_command;
952f7788e8eSJonathan Lemon 	int i, s;
9537dced78aSDavid Greenman 
954f7788e8eSJonathan Lemon 	s = splimp();
9557dced78aSDavid Greenman 
95648e417ebSJonathan Lemon 	fxp_powerstate_d0(dev);
95748e417ebSJonathan Lemon 
9587dced78aSDavid Greenman 	/* better way to do this? */
9597dced78aSDavid Greenman 	for (i = 0; i < 5; i++)
9607dced78aSDavid Greenman 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
9617dced78aSDavid Greenman 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
9627dced78aSDavid Greenman 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
9637dced78aSDavid Greenman 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
9647dced78aSDavid Greenman 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
9657dced78aSDavid Greenman 
9667dced78aSDavid Greenman 	/* reenable busmastering */
9677dced78aSDavid Greenman 	pci_command = pci_read_config(dev, PCIR_COMMAND, 2);
9687dced78aSDavid Greenman 	pci_command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
9697dced78aSDavid Greenman 	pci_write_config(dev, PCIR_COMMAND, pci_command, 2);
9707dced78aSDavid Greenman 
9717dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
9727dced78aSDavid Greenman 	DELAY(10);
9737dced78aSDavid Greenman 
9747dced78aSDavid Greenman 	/* reinitialize interface if necessary */
9757dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
9767dced78aSDavid Greenman 		fxp_init(sc);
9777dced78aSDavid Greenman 
9787dced78aSDavid Greenman 	sc->suspended = 0;
9797dced78aSDavid Greenman 
980f7788e8eSJonathan Lemon 	splx(s);
981ba8c6fd5SDavid Greenman 	return (0);
982f7788e8eSJonathan Lemon }
983ba8c6fd5SDavid Greenman 
98400c4116bSJonathan Lemon static void
98500c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
98600c4116bSJonathan Lemon {
98700c4116bSJonathan Lemon 	u_int16_t reg;
98800c4116bSJonathan Lemon 	int x;
98900c4116bSJonathan Lemon 
99000c4116bSJonathan Lemon 	/*
99100c4116bSJonathan Lemon 	 * Shift in data.
99200c4116bSJonathan Lemon 	 */
99300c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
99400c4116bSJonathan Lemon 		if (data & x)
99500c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
99600c4116bSJonathan Lemon 		else
99700c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
99800c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
99900c4116bSJonathan Lemon 		DELAY(1);
100000c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
100100c4116bSJonathan Lemon 		DELAY(1);
100200c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
100300c4116bSJonathan Lemon 		DELAY(1);
100400c4116bSJonathan Lemon 	}
100500c4116bSJonathan Lemon }
100600c4116bSJonathan Lemon 
1007f7788e8eSJonathan Lemon /*
1008f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1009f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1010f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1011f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1012f7788e8eSJonathan Lemon  * every 16 bits of data.
1013f7788e8eSJonathan Lemon  */
1014f7788e8eSJonathan Lemon static u_int16_t
1015f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1016f7788e8eSJonathan Lemon {
1017f7788e8eSJonathan Lemon 	u_int16_t reg, data;
1018f7788e8eSJonathan Lemon 	int x;
1019ba8c6fd5SDavid Greenman 
1020f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1021f7788e8eSJonathan Lemon 	/*
1022f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1023f7788e8eSJonathan Lemon 	 */
102400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1025f7788e8eSJonathan Lemon 	/*
1026f7788e8eSJonathan Lemon 	 * Shift in address.
1027f7788e8eSJonathan Lemon 	 */
1028f7788e8eSJonathan Lemon 	data = 0;
1029f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1030f7788e8eSJonathan Lemon 		if (offset & x)
1031f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1032f7788e8eSJonathan Lemon 		else
1033f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1034f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1035f7788e8eSJonathan Lemon 		DELAY(1);
1036f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1037f7788e8eSJonathan Lemon 		DELAY(1);
1038f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1039f7788e8eSJonathan Lemon 		DELAY(1);
1040f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1041f7788e8eSJonathan Lemon 		data++;
1042f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1043f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1044f7788e8eSJonathan Lemon 			break;
1045f7788e8eSJonathan Lemon 		}
1046f7788e8eSJonathan Lemon 	}
1047f7788e8eSJonathan Lemon 	/*
1048f7788e8eSJonathan Lemon 	 * Shift out data.
1049f7788e8eSJonathan Lemon 	 */
1050f7788e8eSJonathan Lemon 	data = 0;
1051f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1052f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1053f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1054f7788e8eSJonathan Lemon 		DELAY(1);
1055f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1056f7788e8eSJonathan Lemon 			data |= x;
1057f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1058f7788e8eSJonathan Lemon 		DELAY(1);
1059f7788e8eSJonathan Lemon 	}
1060f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1061f7788e8eSJonathan Lemon 	DELAY(1);
1062f7788e8eSJonathan Lemon 
1063f7788e8eSJonathan Lemon 	return (data);
1064ba8c6fd5SDavid Greenman }
1065ba8c6fd5SDavid Greenman 
106600c4116bSJonathan Lemon static void
106700c4116bSJonathan Lemon fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
106800c4116bSJonathan Lemon {
106900c4116bSJonathan Lemon 	int i;
107000c4116bSJonathan Lemon 
107100c4116bSJonathan Lemon 	/*
107200c4116bSJonathan Lemon 	 * Erase/write enable.
107300c4116bSJonathan Lemon 	 */
107400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
107500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
107600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
107700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
107800c4116bSJonathan Lemon 	DELAY(1);
107900c4116bSJonathan Lemon 	/*
108000c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
108100c4116bSJonathan Lemon 	 */
108200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
108300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
108400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
108500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
108600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
108700c4116bSJonathan Lemon 	DELAY(1);
108800c4116bSJonathan Lemon 	/*
108900c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
109000c4116bSJonathan Lemon 	 */
109100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
109200c4116bSJonathan Lemon 	DELAY(1);
109300c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
109400c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
109500c4116bSJonathan Lemon 			break;
109600c4116bSJonathan Lemon 		DELAY(50);
109700c4116bSJonathan Lemon 	}
109800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
109900c4116bSJonathan Lemon 	DELAY(1);
110000c4116bSJonathan Lemon 	/*
110100c4116bSJonathan Lemon 	 * Erase/write disable.
110200c4116bSJonathan Lemon 	 */
110300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
110400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
110500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
110600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
110700c4116bSJonathan Lemon 	DELAY(1);
110800c4116bSJonathan Lemon }
110900c4116bSJonathan Lemon 
1110ba8c6fd5SDavid Greenman /*
1111e9bf2fa7SDavid Greenman  * From NetBSD:
1112e9bf2fa7SDavid Greenman  *
1113e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1114e9bf2fa7SDavid Greenman  *
1115e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1116e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1117e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1118e9bf2fa7SDavid Greenman  *
1119e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1120e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1121e9bf2fa7SDavid Greenman  *
1122e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1123e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1124e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1125e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1126e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1127e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1128e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1129e9bf2fa7SDavid Greenman  */
1130e9bf2fa7SDavid Greenman static void
1131f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1132e9bf2fa7SDavid Greenman {
1133e9bf2fa7SDavid Greenman 
1134f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1135f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1136f7788e8eSJonathan Lemon 
1137f7788e8eSJonathan Lemon 	/* autosize */
1138f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1139e9bf2fa7SDavid Greenman }
1140f7788e8eSJonathan Lemon 
1141ba8c6fd5SDavid Greenman static void
1142f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1143ba8c6fd5SDavid Greenman {
1144f7788e8eSJonathan Lemon 	int i;
1145ba8c6fd5SDavid Greenman 
1146f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1147f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1148ba8c6fd5SDavid Greenman }
1149ba8c6fd5SDavid Greenman 
115000c4116bSJonathan Lemon static void
115100c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
115200c4116bSJonathan Lemon {
115300c4116bSJonathan Lemon 	int i;
115400c4116bSJonathan Lemon 
115500c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
115600c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
115700c4116bSJonathan Lemon }
115800c4116bSJonathan Lemon 
1159b2badf02SMaxime Henrion static void
1160b2badf02SMaxime Henrion fxp_dma_map_txbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1161b2badf02SMaxime Henrion     bus_size_t mapsize, int error)
1162b2badf02SMaxime Henrion {
1163b2badf02SMaxime Henrion 	struct fxp_softc *sc;
1164b2badf02SMaxime Henrion 	struct fxp_cb_tx *txp;
1165b2badf02SMaxime Henrion 	int i;
1166b2badf02SMaxime Henrion 
1167b2badf02SMaxime Henrion 	if (error)
1168b2badf02SMaxime Henrion 		return;
1169b2badf02SMaxime Henrion 
1170b2badf02SMaxime Henrion 	KASSERT(nseg <= FXP_NTXSEG, ("too many DMA segments"));
1171b2badf02SMaxime Henrion 
1172b2badf02SMaxime Henrion 	sc = arg;
1173b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next->tx_cb;
1174b2badf02SMaxime Henrion 	for (i = 0; i < nseg; i++) {
1175b2badf02SMaxime Henrion 		KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large"));
1176b2badf02SMaxime Henrion 		/*
1177b2badf02SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
1178b2badf02SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
1179b2badf02SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
1180b2badf02SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
1181b2badf02SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
1182b2badf02SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
1183b2badf02SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
1184b2badf02SMaxime Henrion 		 * checksum offload control bits. So to make things work
1185b2badf02SMaxime Henrion 		 * right, we have to start filling in the TBD array
1186b2badf02SMaxime Henrion 		 * starting from a different place depending on whether
1187b2badf02SMaxime Henrion 		 * the chip is an 82550/82551 or not.
1188b2badf02SMaxime Henrion 		 */
1189b2badf02SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
119083e6547dSMaxime Henrion 			txp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
119183e6547dSMaxime Henrion 			txp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
1192b2badf02SMaxime Henrion 		} else {
119383e6547dSMaxime Henrion 			txp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
119483e6547dSMaxime Henrion 			txp->tbd[i].tb_size = htole32(segs[i].ds_len);
1195b2badf02SMaxime Henrion 		}
1196b2badf02SMaxime Henrion 	}
1197b2badf02SMaxime Henrion 	txp->tbd_number = nseg;
1198b2badf02SMaxime Henrion }
1199b2badf02SMaxime Henrion 
1200a17c678eSDavid Greenman /*
1201a17c678eSDavid Greenman  * Start packet transmission on the interface.
1202a17c678eSDavid Greenman  */
1203a17c678eSDavid Greenman static void
1204f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1205a17c678eSDavid Greenman {
12069b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
120750d81222SMaxime Henrion 	struct fxp_tx *txp;
1208b2badf02SMaxime Henrion 	struct mbuf *mb_head;
1209b2badf02SMaxime Henrion 	int error;
1210a17c678eSDavid Greenman 
1211a17c678eSDavid Greenman 	/*
1212483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1213483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1214483b9871SDavid Greenman 	 * of the command chain).
1215a17c678eSDavid Greenman 	 */
12160f4dc94cSChuck Paterson 	if (sc->need_mcsetup) {
1217a17c678eSDavid Greenman 		return;
12180f4dc94cSChuck Paterson 	}
12191cd443acSDavid Greenman 
1220483b9871SDavid Greenman 	txp = NULL;
1221483b9871SDavid Greenman 
1222483b9871SDavid Greenman 	/*
1223483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1224483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
12253114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
12263114fdb4SDavid Greenman 	 *       a NOP command when needed.
1227483b9871SDavid Greenman 	 */
12283114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
1229483b9871SDavid Greenman 
1230dfe61cf1SDavid Greenman 		/*
1231dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1232dfe61cf1SDavid Greenman 		 */
12336318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
1234a17c678eSDavid Greenman 
1235dfe61cf1SDavid Greenman 		/*
1236483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
1237dfe61cf1SDavid Greenman 		 */
1238b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
1239c8bca6dcSBill Paul 
1240c8bca6dcSBill Paul 		/*
1241c8bca6dcSBill Paul 		 * Deal with TCP/IP checksum offload. Note that
1242c8bca6dcSBill Paul 		 * in order for TCP checksum offload to work,
1243c8bca6dcSBill Paul 		 * the pseudo header checksum must have already
1244c8bca6dcSBill Paul 		 * been computed and stored in the checksum field
1245c8bca6dcSBill Paul 		 * in the TCP header. The stack should have
1246c8bca6dcSBill Paul 		 * already done this for us.
1247c8bca6dcSBill Paul 		 */
1248c8bca6dcSBill Paul 
1249c8bca6dcSBill Paul 		if (mb_head->m_pkthdr.csum_flags) {
1250c8bca6dcSBill Paul 			if (mb_head->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1251b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_activation_high =
1252c8bca6dcSBill Paul 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
1253b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_schedule =
1254c8bca6dcSBill Paul 				    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1255c8bca6dcSBill Paul 				if (mb_head->m_pkthdr.csum_flags & CSUM_TCP)
1256b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_schedule |=
1257c8bca6dcSBill Paul 					    FXP_IPCB_TCP_PACKET;
1258c8bca6dcSBill Paul 			}
1259c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
1260c8bca6dcSBill Paul 		/*
1261c8bca6dcSBill Paul 		 * XXX The 82550 chip appears to have trouble
1262c8bca6dcSBill Paul 		 * dealing with IP header checksums in very small
1263c8bca6dcSBill Paul 		 * datagrams, namely fragments from 1 to 3 bytes
1264c8bca6dcSBill Paul 		 * in size. For example, say you want to transmit
1265c8bca6dcSBill Paul 		 * a UDP packet of 1473 bytes. The packet will be
1266c8bca6dcSBill Paul 		 * fragmented over two IP datagrams, the latter
1267c8bca6dcSBill Paul 		 * containing only one byte of data. The 82550 will
1268c8bca6dcSBill Paul 		 * botch the header checksum on the 1-byte fragment.
1269c8bca6dcSBill Paul 		 * As long as the datagram contains 4 or more bytes
1270c8bca6dcSBill Paul 		 * of data, you're ok.
1271c8bca6dcSBill Paul 		 *
1272c8bca6dcSBill Paul                  * The following code attempts to work around this
1273c8bca6dcSBill Paul 		 * problem: if the datagram is less than 38 bytes
1274c8bca6dcSBill Paul 		 * in size (14 bytes ether header, 20 bytes IP header,
1275c8bca6dcSBill Paul 		 * plus 4 bytes of data), we punt and compute the IP
1276c8bca6dcSBill Paul 		 * header checksum by hand. This workaround doesn't
1277c8bca6dcSBill Paul 		 * work very well, however, since it can be fooled
1278c8bca6dcSBill Paul 		 * by things like VLAN tags and IP options that make
1279c8bca6dcSBill Paul 		 * the header sizes/offsets vary.
1280c8bca6dcSBill Paul 		 */
1281c8bca6dcSBill Paul 
1282c8bca6dcSBill Paul 			if (mb_head->m_pkthdr.csum_flags & CSUM_IP) {
1283c8bca6dcSBill Paul 				if (mb_head->m_pkthdr.len < 38) {
1284c8bca6dcSBill Paul 					struct ip *ip;
1285c8bca6dcSBill Paul 					mb_head->m_data += ETHER_HDR_LEN;
1286c8bca6dcSBill Paul 					ip = mtod(mb_head, struct ip *);
1287c8bca6dcSBill Paul 					ip->ip_sum = in_cksum(mb_head,
1288c8bca6dcSBill Paul 					    ip->ip_hl << 2);
1289c8bca6dcSBill Paul 					mb_head->m_data -= ETHER_HDR_LEN;
1290c8bca6dcSBill Paul 				} else {
1291b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_activation_high =
1292c8bca6dcSBill Paul 					    FXP_IPCB_HARDWAREPARSING_ENABLE;
1293b2badf02SMaxime Henrion 					txp->tx_cb->ipcb_ip_schedule |=
1294c8bca6dcSBill Paul 					    FXP_IPCB_IP_CHECKSUM_ENABLE;
1295c8bca6dcSBill Paul 				}
1296c8bca6dcSBill Paul 			}
1297c8bca6dcSBill Paul #endif
1298c8bca6dcSBill Paul 		}
1299c8bca6dcSBill Paul 
1300c8bca6dcSBill Paul 		/*
1301a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
1302483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
1303a17c678eSDavid Greenman 		 * and size of the mbuf.
1304a17c678eSDavid Greenman 		 */
1305b2badf02SMaxime Henrion 		error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1306b2badf02SMaxime Henrion 		    mb_head, fxp_dma_map_txbuf, sc, 0);
1307b2badf02SMaxime Henrion 
1308b2badf02SMaxime Henrion 		if (error && error != EFBIG) {
1309b2badf02SMaxime Henrion 			device_printf(sc->dev, "can't map mbuf (error %d)\n",
1310b2badf02SMaxime Henrion 			    error);
1311b2badf02SMaxime Henrion 			m_freem(mb_head);
1312a17c678eSDavid Greenman 			break;
1313a17c678eSDavid Greenman 		}
1314b2badf02SMaxime Henrion 
1315b2badf02SMaxime Henrion 		if (error) {
131623a0ed7cSDavid Greenman 			struct mbuf *mn;
131723a0ed7cSDavid Greenman 
1318a17c678eSDavid Greenman 			/*
13193bd07cfdSJonathan Lemon 			 * We ran out of segments. We have to recopy this
13203bd07cfdSJonathan Lemon 			 * mbuf chain first. Bail out if we can't get the
13213bd07cfdSJonathan Lemon 			 * new buffers.
1322a17c678eSDavid Greenman 			 */
1323a163d034SWarner Losh 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
132423a0ed7cSDavid Greenman 			if (mn == NULL) {
132523a0ed7cSDavid Greenman 				m_freem(mb_head);
1326483b9871SDavid Greenman 				break;
1327a17c678eSDavid Greenman 			}
132823a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
1329a163d034SWarner Losh 				MCLGET(mn, M_DONTWAIT);
133023a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
133123a0ed7cSDavid Greenman 					m_freem(mn);
133223a0ed7cSDavid Greenman 					m_freem(mb_head);
1333483b9871SDavid Greenman 					break;
133423a0ed7cSDavid Greenman 				}
133523a0ed7cSDavid Greenman 			}
1336ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1337ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
133823a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
133923a0ed7cSDavid Greenman 			m_freem(mb_head);
134023a0ed7cSDavid Greenman 			mb_head = mn;
1341b2badf02SMaxime Henrion 			error = bus_dmamap_load_mbuf(sc->fxp_mtag, txp->tx_map,
1342b2badf02SMaxime Henrion 			    mb_head, fxp_dma_map_txbuf, sc, 0);
1343b2badf02SMaxime Henrion 			if (error) {
1344b2badf02SMaxime Henrion 				device_printf(sc->dev,
1345b2badf02SMaxime Henrion 				    "can't map mbuf (error %d)\n", error);
1346b2badf02SMaxime Henrion 				m_freem(mb_head);
1347b2badf02SMaxime Henrion 				break;
1348b2badf02SMaxime Henrion 			}
134923a0ed7cSDavid Greenman 		}
135023a0ed7cSDavid Greenman 
1351b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1352b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
1353b2badf02SMaxime Henrion 
1354b2badf02SMaxime Henrion 		txp->tx_mbuf = mb_head;
1355b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
1356b2badf02SMaxime Henrion 		txp->tx_cb->byte_count = 0;
13573114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1358b2badf02SMaxime Henrion 			txp->tx_cb->cb_command =
135983e6547dSMaxime Henrion 			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
136083e6547dSMaxime Henrion 			    FXP_CB_COMMAND_S);
13613114fdb4SDavid Greenman 		} else {
1362b2badf02SMaxime Henrion 			txp->tx_cb->cb_command =
136383e6547dSMaxime Henrion 			    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
136483e6547dSMaxime Henrion 			    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
13653114fdb4SDavid Greenman 			/*
13663bd07cfdSJonathan Lemon 			 * Set a 5 second timer just in case we don't hear
13673bd07cfdSJonathan Lemon 			 * from the card again.
13683114fdb4SDavid Greenman 			 */
13693114fdb4SDavid Greenman 			ifp->if_timer = 5;
13703114fdb4SDavid Greenman 		}
1371b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1372a17c678eSDavid Greenman 
1373a17c678eSDavid Greenman 		/*
1374483b9871SDavid Greenman 		 * Advance the end of list forward.
1375a17c678eSDavid Greenman 		 */
137606175228SAndrew Gallatin 
137750d81222SMaxime Henrion #ifdef __alpha__
137806175228SAndrew Gallatin 		/*
137906175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
138006175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
138106175228SAndrew Gallatin 		 * up the status while we update the command field.
138206175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
138314fd1071SMaxime Henrion 		 * XXX This is probably bogus and we're _not_ looking
138414fd1071SMaxime Henrion 		 * for atomicity here.
138506175228SAndrew Gallatin 		 */
138614fd1071SMaxime Henrion 		atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1387bafb64afSMaxime Henrion 		    htole16(FXP_CB_COMMAND_S));
138850d81222SMaxime Henrion #else
1389bafb64afSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
1390bafb64afSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
139150d81222SMaxime Henrion #endif /*__alpha__*/
1392b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
1393a17c678eSDavid Greenman 
1394a17c678eSDavid Greenman 		/*
13951cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
1396b2badf02SMaxime Henrion 		 * no other packets queued (when nothing is queued, tx_first
1397483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1398a17c678eSDavid Greenman 		 */
13991cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1400b2badf02SMaxime Henrion 			sc->fxp_desc.tx_first = txp;
1401a17c678eSDavid Greenman 
14021cd443acSDavid Greenman 		sc->tx_queued++;
14031cd443acSDavid Greenman 
1404a17c678eSDavid Greenman 		/*
1405a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1406a17c678eSDavid Greenman 		 */
1407673d9191SSam Leffler 		BPF_MTAP(ifp, mb_head);
1408483b9871SDavid Greenman 	}
1409b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1410483b9871SDavid Greenman 
1411483b9871SDavid Greenman 	/*
1412483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1413483b9871SDavid Greenman 	 * going again if suspended.
1414483b9871SDavid Greenman 	 */
1415483b9871SDavid Greenman 	if (txp != NULL) {
1416483b9871SDavid Greenman 		fxp_scb_wait(sc);
14172e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1418483b9871SDavid Greenman 	}
1419a17c678eSDavid Greenman }
1420a17c678eSDavid Greenman 
1421e4fc250cSLuigi Rizzo static void fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count);
1422e4fc250cSLuigi Rizzo 
1423e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1424e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1425e4fc250cSLuigi Rizzo 
1426e4fc250cSLuigi Rizzo static void
1427e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1428e4fc250cSLuigi Rizzo {
1429e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
1430e4fc250cSLuigi Rizzo 	u_int8_t statack;
1431e4fc250cSLuigi Rizzo 
1432e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1433e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
1434e4fc250cSLuigi Rizzo 		return;
1435e4fc250cSLuigi Rizzo 	}
1436e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1437e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1438e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
1439e4fc250cSLuigi Rizzo 		u_int8_t tmp;
14406481f301SPeter Wemm 
1441e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1442e4fc250cSLuigi Rizzo 		if (tmp == 0xff || tmp == 0)
1443e4fc250cSLuigi Rizzo 			return; /* nothing to do */
1444e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1445e4fc250cSLuigi Rizzo 		/* ack what we can */
1446e4fc250cSLuigi Rizzo 		if (tmp != 0)
1447e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1448e4fc250cSLuigi Rizzo 		statack |= tmp;
1449e4fc250cSLuigi Rizzo 	}
1450e4fc250cSLuigi Rizzo 	fxp_intr_body(sc, statack, count);
1451e4fc250cSLuigi Rizzo }
1452e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1453e4fc250cSLuigi Rizzo 
1454a17c678eSDavid Greenman /*
14559c7d2607SDavid Greenman  * Process interface interrupts.
1456a17c678eSDavid Greenman  */
145794927790SDavid Greenman static void
1458f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1459a17c678eSDavid Greenman {
1460f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
14611cd443acSDavid Greenman 	u_int8_t statack;
14620f4dc94cSChuck Paterson 
1463e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1464e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
1465e4fc250cSLuigi Rizzo 
146662f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
1467e4fc250cSLuigi Rizzo 		return;
1468e4fc250cSLuigi Rizzo 	if (ether_poll_register(fxp_poll, ifp)) {
1469e4fc250cSLuigi Rizzo 		/* disable interrupts */
1470e4fc250cSLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
1471e4fc250cSLuigi Rizzo 		fxp_poll(ifp, 0, 1);
1472e4fc250cSLuigi Rizzo 		return;
1473e4fc250cSLuigi Rizzo 	}
1474e4fc250cSLuigi Rizzo #endif
1475e4fc250cSLuigi Rizzo 
1476b184b38eSDavid Greenman 	if (sc->suspended) {
1477b184b38eSDavid Greenman 		return;
1478b184b38eSDavid Greenman 	}
1479b184b38eSDavid Greenman 
1480b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1481a17c678eSDavid Greenman 		/*
148211457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
148311457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
148411457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
148511457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
148611457bbfSJonathan Lemon 		 */
148711457bbfSJonathan Lemon 		if (statack == 0xff)
148811457bbfSJonathan Lemon 			return;
148911457bbfSJonathan Lemon 
149011457bbfSJonathan Lemon 		/*
1491a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1492a17c678eSDavid Greenman 		 */
1493ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1494e4fc250cSLuigi Rizzo 		fxp_intr_body(sc, statack, -1);
1495e4fc250cSLuigi Rizzo 	}
1496e4fc250cSLuigi Rizzo }
1497e4fc250cSLuigi Rizzo 
1498e4fc250cSLuigi Rizzo static void
1499b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1500b2badf02SMaxime Henrion {
1501b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1502b2badf02SMaxime Henrion 
1503b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1504b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
150583e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1506b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1507b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1508b2badf02SMaxime Henrion 			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1509b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1510b2badf02SMaxime Henrion 			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1511b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1512b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1513b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1514b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1515b2badf02SMaxime Henrion 		}
1516b2badf02SMaxime Henrion 		sc->tx_queued--;
1517b2badf02SMaxime Henrion 	}
1518b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1519b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
1520b2badf02SMaxime Henrion }
1521b2badf02SMaxime Henrion 
1522b2badf02SMaxime Henrion static void
1523e4fc250cSLuigi Rizzo fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count)
1524e4fc250cSLuigi Rizzo {
1525e4fc250cSLuigi Rizzo 	struct ifnet *ifp = &sc->sc_if;
15262b5989e9SLuigi Rizzo 	struct mbuf *m;
1527b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
15282b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
15292b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
15302b5989e9SLuigi Rizzo 
15312b5989e9SLuigi Rizzo 	if (rnr)
15322b5989e9SLuigi Rizzo 		fxp_rnr++;
1533947e3815SIan Dowse #ifdef DEVICE_POLLING
1534947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1535947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1536947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1537947e3815SIan Dowse 		rnr = 1;
1538947e3815SIan Dowse 	}
1539947e3815SIan Dowse #endif
1540a17c678eSDavid Greenman 
1541a17c678eSDavid Greenman 	/*
15423114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
154306936301SBill Paul 	 *
154406936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
154506936301SBill Paul 	 * be that this event (control unit not ready) was not
154606936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
154706936301SBill Paul 	 * The exact sequence of events that occur when the interface
154806936301SBill Paul 	 * is brought up are different now, and if this event
154906936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
155006936301SBill Paul 	 * can stall for several seconds. The result is that no
155106936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
155206936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
15533114fdb4SDavid Greenman 	 */
155406936301SBill Paul 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
1555b2badf02SMaxime Henrion 		fxp_txeof(sc);
15563114fdb4SDavid Greenman 
155741aa0ba2SLuigi Rizzo 		ifp->if_timer = 0;
1558e2102ae4SMike Silbersack 		if (sc->tx_queued == 0) {
15593114fdb4SDavid Greenman 			if (sc->need_mcsetup)
15603114fdb4SDavid Greenman 				fxp_mc_setup(sc);
1561e2102ae4SMike Silbersack 		}
15623114fdb4SDavid Greenman 		/*
15633114fdb4SDavid Greenman 		 * Try to start more packets transmitting.
15643114fdb4SDavid Greenman 		 */
15653114fdb4SDavid Greenman 		if (ifp->if_snd.ifq_head != NULL)
15663114fdb4SDavid Greenman 			fxp_start(ifp);
15673114fdb4SDavid Greenman 	}
15682b5989e9SLuigi Rizzo 
15692b5989e9SLuigi Rizzo 	/*
15702b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
15712b5989e9SLuigi Rizzo 	 */
1572947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
15732b5989e9SLuigi Rizzo 		return;
15742b5989e9SLuigi Rizzo 
15753114fdb4SDavid Greenman 	/*
1576a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1577a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1578a17c678eSDavid Greenman 	 * re-start the receiver.
1579947e3815SIan Dowse 	 *
15802b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
15812b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
15822b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
15832b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1584947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1585947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1586a17c678eSDavid Greenman 	 */
15872b5989e9SLuigi Rizzo 	for (;;) {
1588b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1589b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1590ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1591ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1592b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1593b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1594a17c678eSDavid Greenman 
1595e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1596947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1597947e3815SIan Dowse 			if (rnr) {
1598947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1599947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1600947e3815SIan Dowse 				rnr = 0;
1601947e3815SIan Dowse 			}
16022b5989e9SLuigi Rizzo 			break;
1603947e3815SIan Dowse 		}
16042b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
16052b5989e9SLuigi Rizzo 
160683e6547dSMaxime Henrion 		if ((le16toh(rfa->rfa_status) & FXP_RFA_STATUS_C) == 0)
16072b5989e9SLuigi Rizzo 			break;
16082b5989e9SLuigi Rizzo 
1609dfe61cf1SDavid Greenman 		/*
1610b2badf02SMaxime Henrion 		 * Advance head forward.
1611dfe61cf1SDavid Greenman 		 */
1612b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1613a17c678eSDavid Greenman 
1614dfe61cf1SDavid Greenman 		/*
1615ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1616ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1617ba8c6fd5SDavid Greenman 		 * instead.
1618dfe61cf1SDavid Greenman 		 */
1619b2badf02SMaxime Henrion 		if (fxp_add_rfabuf(sc, rxp) == 0) {
1620aed53495SDavid Greenman 			int total_len;
1621a17c678eSDavid Greenman 
1622e8c8b728SJonathan Lemon 			/*
16232b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
16242b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
16252b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
16262b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1627e8c8b728SJonathan Lemon 			 */
1628bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
16292b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
16302b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
1631b2badf02SMaxime Henrion 				sc->rfa_size ||
163283e6547dSMaxime Henrion 			    le16toh(rfa->rfa_status) & FXP_RFA_STATUS_CRC) {
1633e8c8b728SJonathan Lemon 				m_freem(m);
16342b5989e9SLuigi Rizzo 				continue;
1635e8c8b728SJonathan Lemon 			}
1636920b58e8SBrooks Davis 
1637c8bca6dcSBill Paul                         /* Do IP checksum checking. */
163883e6547dSMaxime Henrion 			if (le16toh(rfa->rfa_status) & FXP_RFA_STATUS_PARSE) {
1639c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1640c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1641c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1642c8bca6dcSBill Paul 					    CSUM_IP_CHECKED;
1643c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1644c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_VALID)
1645c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1646c8bca6dcSBill Paul 					    CSUM_IP_VALID;
1647c8bca6dcSBill Paul 				if ((rfa->rfax_csum_sts &
1648c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1649c8bca6dcSBill Paul 				    (rfa->rfax_csum_sts &
1650c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1651c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1652c8bca6dcSBill Paul 					    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1653c8bca6dcSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1654c8bca6dcSBill Paul 				}
1655c8bca6dcSBill Paul 			}
1656c8bca6dcSBill Paul 
16572e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1658673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1659673d9191SSam Leffler 
1660673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
1661a17c678eSDavid Greenman 		}
1662a17c678eSDavid Greenman 	}
16632b5989e9SLuigi Rizzo 	if (rnr) {
1664ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1665ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1666b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
16672e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1668a17c678eSDavid Greenman 	}
1669a17c678eSDavid Greenman }
1670a17c678eSDavid Greenman 
1671dfe61cf1SDavid Greenman /*
1672dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1673dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1674dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1675dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1676dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1677dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1678dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1679dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1680dfe61cf1SDavid Greenman  * them again next time.
1681dfe61cf1SDavid Greenman  */
1682303b270bSEivind Eklund static void
1683f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1684a17c678eSDavid Greenman {
1685f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1686ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1687a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1688f7788e8eSJonathan Lemon 	int s;
1689a17c678eSDavid Greenman 
1690b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
169183e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
169283e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1693397f9dfeSDavid Greenman 	if (sp->rx_good) {
169483e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
1695397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1696397f9dfeSDavid Greenman 	} else {
1697c8cc6fcaSDavid Greenman 		/*
1698c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1699c8cc6fcaSDavid Greenman 		 */
1700397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1701397f9dfeSDavid Greenman 	}
17023ba65732SDavid Greenman 	ifp->if_ierrors +=
170383e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
170483e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
170583e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
170683e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
1707a17c678eSDavid Greenman 	/*
1708f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1709f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1710f9be9005SDavid Greenman 	 */
1711f9be9005SDavid Greenman 	if (sp->tx_underruns) {
171283e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
1713f9be9005SDavid Greenman 		if (tx_threshold < 192)
1714f9be9005SDavid Greenman 			tx_threshold += 64;
1715f9be9005SDavid Greenman 	}
1716f7788e8eSJonathan Lemon 	s = splimp();
1717397f9dfeSDavid Greenman 	/*
1718c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1719c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1720c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1721c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1722c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1723c8cc6fcaSDavid Greenman 	 */
1724b2badf02SMaxime Henrion 	fxp_txeof(sc);
1725b2badf02SMaxime Henrion 
1726c8cc6fcaSDavid Greenman 	/*
1727397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1728397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1729397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1730397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1731397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1732397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1733397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1734397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1735397f9dfeSDavid Greenman 	 */
1736397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1737397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1738397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1739397f9dfeSDavid Greenman 	}
1740f9be9005SDavid Greenman 	/*
17413ba65732SDavid Greenman 	 * If there is no pending command, start another stats
17423ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1743a17c678eSDavid Greenman 	 */
1744397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1745a17c678eSDavid Greenman 		/*
1746397f9dfeSDavid Greenman 		 * Start another stats dump.
1747a17c678eSDavid Greenman 		 */
1748b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
1749b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
17502e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1751dfe61cf1SDavid Greenman 	} else {
1752dfe61cf1SDavid Greenman 		/*
1753dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1754dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
17553ba65732SDavid Greenman 		 * next timer event to update them.
1756dfe61cf1SDavid Greenman 		 */
1757dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1758f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1759dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
17603ba65732SDavid Greenman 
1761dfe61cf1SDavid Greenman 		sp->rx_good = 0;
17623ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
17633ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
17643ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
17653ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1766dfe61cf1SDavid Greenman 	}
1767f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1768f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
176974396a0aSJonathan Lemon 	splx(s);
1770a17c678eSDavid Greenman 	/*
1771a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1772a17c678eSDavid Greenman 	 */
1773f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
1774a17c678eSDavid Greenman }
1775a17c678eSDavid Greenman 
1776a17c678eSDavid Greenman /*
1777a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1778a17c678eSDavid Greenman  * the interface.
1779a17c678eSDavid Greenman  */
1780a17c678eSDavid Greenman static void
1781f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1782a17c678eSDavid Greenman {
1783ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1784b2badf02SMaxime Henrion 	struct fxp_tx *txp;
17853ba65732SDavid Greenman 	int i;
1786a17c678eSDavid Greenman 
17877dced78aSDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
17887dced78aSDavid Greenman 	ifp->if_timer = 0;
17897dced78aSDavid Greenman 
1790e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1791e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
1792e4fc250cSLuigi Rizzo #endif
1793a17c678eSDavid Greenman 	/*
1794a17c678eSDavid Greenman 	 * Cancel stats updater.
1795a17c678eSDavid Greenman 	 */
1796f7788e8eSJonathan Lemon 	untimeout(fxp_tick, sc, sc->stat_ch);
17973ba65732SDavid Greenman 
17983ba65732SDavid Greenman 	/*
179972a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
18003ba65732SDavid Greenman 	 */
180172a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
180209882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
180372a32a26SJonathan Lemon 	DELAY(50);
1804a17c678eSDavid Greenman 
18053ba65732SDavid Greenman 	/*
18063ba65732SDavid Greenman 	 * Release any xmit buffers.
18073ba65732SDavid Greenman 	 */
1808b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
1809da91462dSDavid Greenman 	if (txp != NULL) {
1810da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1811b2badf02SMaxime Henrion 			if (txp[i].tx_mbuf != NULL) {
1812b2badf02SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
1813b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
1814b2badf02SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
1815b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
1816b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
1817c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
1818b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
1819da91462dSDavid Greenman 			}
1820da91462dSDavid Greenman 		}
18213ba65732SDavid Greenman 	}
1822b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
18233ba65732SDavid Greenman 	sc->tx_queued = 0;
1824a17c678eSDavid Greenman }
1825a17c678eSDavid Greenman 
1826a17c678eSDavid Greenman /*
1827a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1828a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1829a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1830a17c678eSDavid Greenman  * card has wedged for some reason.
1831a17c678eSDavid Greenman  */
1832a17c678eSDavid Greenman static void
1833f7788e8eSJonathan Lemon fxp_watchdog(struct ifnet *ifp)
1834a17c678eSDavid Greenman {
1835ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1836ba8c6fd5SDavid Greenman 
1837f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
18384a5f1499SDavid Greenman 	ifp->if_oerrors++;
1839a17c678eSDavid Greenman 
1840ba8c6fd5SDavid Greenman 	fxp_init(sc);
1841a17c678eSDavid Greenman }
1842a17c678eSDavid Greenman 
1843a17c678eSDavid Greenman static void
1844f7788e8eSJonathan Lemon fxp_init(void *xsc)
1845a17c678eSDavid Greenman {
1846fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1847ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1848a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1849a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1850b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
1851b2badf02SMaxime Henrion 	struct fxp_tx *txp;
185209882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
1853f7788e8eSJonathan Lemon 	int i, prm, s;
1854a17c678eSDavid Greenman 
1855f7788e8eSJonathan Lemon 	s = splimp();
1856a17c678eSDavid Greenman 	/*
18573ba65732SDavid Greenman 	 * Cancel any pending I/O
1858a17c678eSDavid Greenman 	 */
18593ba65732SDavid Greenman 	fxp_stop(sc);
1860a17c678eSDavid Greenman 
1861a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1862a17c678eSDavid Greenman 
1863a17c678eSDavid Greenman 	/*
1864a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1865a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1866a17c678eSDavid Greenman 	 */
1867ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
18682e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1869a17c678eSDavid Greenman 
1870ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
18712e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1872a17c678eSDavid Greenman 
1873a17c678eSDavid Greenman 	/*
1874a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1875a17c678eSDavid Greenman 	 */
1876ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1877b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
1878b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
18792e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1880a17c678eSDavid Greenman 
1881a17c678eSDavid Greenman 	/*
188272a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
188372a32a26SJonathan Lemon 	 */
188472a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
188572a32a26SJonathan Lemon 		fxp_load_ucode(sc);
188672a32a26SJonathan Lemon 
188772a32a26SJonathan Lemon 	/*
188809882363SJonathan Lemon 	 * Initialize the multicast address list.
188909882363SJonathan Lemon 	 */
189009882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
189109882363SJonathan Lemon 		mcsp = sc->mcsp;
189209882363SJonathan Lemon 		mcsp->cb_status = 0;
189383e6547dSMaxime Henrion 		mcsp->cb_command =
189483e6547dSMaxime Henrion 		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
189583e6547dSMaxime Henrion 		mcsp->link_addr = 0xffffffff;
189609882363SJonathan Lemon 		/*
189709882363SJonathan Lemon 	 	 * Start the multicast setup command.
189809882363SJonathan Lemon 		 */
189909882363SJonathan Lemon 		fxp_scb_wait(sc);
1900b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
1901b2badf02SMaxime Henrion 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
190209882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
190309882363SJonathan Lemon 		/* ...and wait for it to complete. */
1904209b07bcSMaxime Henrion 		fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
1905b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
1906b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
190709882363SJonathan Lemon 	}
190809882363SJonathan Lemon 
190909882363SJonathan Lemon 	/*
1910a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1911a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1912a17c678eSDavid Greenman 	 * later.
1913a17c678eSDavid Greenman 	 */
1914b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
1915a17c678eSDavid Greenman 
1916a17c678eSDavid Greenman 	/*
1917a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1918a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1919a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1920a17c678eSDavid Greenman 	 */
1921b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
1922a17c678eSDavid Greenman 
1923a17c678eSDavid Greenman 	cbp->cb_status =	0;
192483e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
192583e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
192683e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
19272c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
1928001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1929001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1930a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1931f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
1932f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
1933f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
1934f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
1935001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1936001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1937f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1938a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1939f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
1940f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
19413114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1942f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
1943f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
1944f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
194572a32a26SJonathan Lemon 	cbp->save_bf =		sc->revision == FXP_REV_82557 ? 1 : prm;
1946a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1947f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
1948f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1949f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1950c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
1951f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
1952f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
1953f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
1954f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1955f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1956f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1957f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
1958a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1959a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1960a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1961a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1962a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1963a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1964a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1965a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1966f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1967f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1968f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1969f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
1970f7788e8eSJonathan Lemon 
1971a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1972a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1973a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1974f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
1975f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1976f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1977f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
1978a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
19793ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1980a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1981f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
1982c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
1983a17c678eSDavid Greenman 
198472a32a26SJonathan Lemon 	if (sc->revision == FXP_REV_82557) {
19853bd07cfdSJonathan Lemon 		/*
19863bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
19873bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
19883bd07cfdSJonathan Lemon 		 */
19893bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
19903bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
19913bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
19923bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
19933bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
19943bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
19953bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
19963bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
19973bd07cfdSJonathan Lemon 	} else {
19983bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
19993bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
20003bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
20013bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
20023bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
20033bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
20043bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
20053bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
20063bd07cfdSJonathan Lemon 	}
20073bd07cfdSJonathan Lemon 
2008a17c678eSDavid Greenman 	/*
2009a17c678eSDavid Greenman 	 * Start the config command/DMA.
2010a17c678eSDavid Greenman 	 */
2011ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2012b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2013b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
20142e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2015a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2016209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2017b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2018a17c678eSDavid Greenman 
2019a17c678eSDavid Greenman 	/*
2020a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2021a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2022a17c678eSDavid Greenman 	 */
2023b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2024a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
202583e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
202683e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
2027e609b4d7SMaxime Henrion 	bcopy(sc->arpcom.ac_enaddr, cb_ias->macaddr,
2028a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
2029a17c678eSDavid Greenman 
2030a17c678eSDavid Greenman 	/*
2031a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2032a17c678eSDavid Greenman 	 */
2033ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2034b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
20352e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2036a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2037209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2038b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2039a17c678eSDavid Greenman 
2040a17c678eSDavid Greenman 	/*
2041a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2042a17c678eSDavid Greenman 	 */
2043b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2044b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2045b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2046a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2047b2badf02SMaxime Henrion 		txp[i].tx_cb = tcbp + i;
2048b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
204983e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
205083e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
205183e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
205283e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
20533bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2054b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
205583e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
20563bd07cfdSJonathan Lemon 		else
2057b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
205883e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2059b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2060a17c678eSDavid Greenman 	}
2061a17c678eSDavid Greenman 	/*
2062397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2063a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2064a17c678eSDavid Greenman 	 */
206583e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2066b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2067b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2068397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2069a17c678eSDavid Greenman 
2070ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
20712e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2072a17c678eSDavid Greenman 
2073a17c678eSDavid Greenman 	/*
2074a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2075a17c678eSDavid Greenman 	 */
2076ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2077b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
20782e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2079a17c678eSDavid Greenman 
2080dccee1a1SDavid Greenman 	/*
2081ba8c6fd5SDavid Greenman 	 * Set current media.
2082dccee1a1SDavid Greenman 	 */
2083f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2084f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2085dccee1a1SDavid Greenman 
2086a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
2087a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
2088e8c8b728SJonathan Lemon 
2089e8c8b728SJonathan Lemon 	/*
2090e8c8b728SJonathan Lemon 	 * Enable interrupts.
2091e8c8b728SJonathan Lemon 	 */
20922b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
20932b5989e9SLuigi Rizzo 	/*
20942b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
20952b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
20962b5989e9SLuigi Rizzo 	 */
209762f76486SMaxim Sobolev 	if ( ifp->if_flags & IFF_POLLING )
20982b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
20992b5989e9SLuigi Rizzo 	else
21002b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2101e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2102f7788e8eSJonathan Lemon 	splx(s);
2103a17c678eSDavid Greenman 
2104a17c678eSDavid Greenman 	/*
2105a17c678eSDavid Greenman 	 * Start stats updater.
2106a17c678eSDavid Greenman 	 */
2107f7788e8eSJonathan Lemon 	sc->stat_ch = timeout(fxp_tick, sc, hz);
2108f7788e8eSJonathan Lemon }
2109f7788e8eSJonathan Lemon 
2110f7788e8eSJonathan Lemon static int
2111f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2112f7788e8eSJonathan Lemon {
2113f7788e8eSJonathan Lemon 
2114f7788e8eSJonathan Lemon 	return (0);
2115a17c678eSDavid Greenman }
2116a17c678eSDavid Greenman 
2117303b270bSEivind Eklund static void
2118f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2119ba8c6fd5SDavid Greenman {
2120ba8c6fd5SDavid Greenman 
2121f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2122ba8c6fd5SDavid Greenman }
2123ba8c6fd5SDavid Greenman 
2124ba8c6fd5SDavid Greenman /*
2125ba8c6fd5SDavid Greenman  * Change media according to request.
2126ba8c6fd5SDavid Greenman  */
2127f7788e8eSJonathan Lemon static int
2128f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2129ba8c6fd5SDavid Greenman {
2130ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2131f7788e8eSJonathan Lemon 	struct mii_data *mii;
2132ba8c6fd5SDavid Greenman 
2133f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
2134f7788e8eSJonathan Lemon 	mii_mediachg(mii);
2135ba8c6fd5SDavid Greenman 	return (0);
2136ba8c6fd5SDavid Greenman }
2137ba8c6fd5SDavid Greenman 
2138ba8c6fd5SDavid Greenman /*
2139ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2140ba8c6fd5SDavid Greenman  */
2141f7788e8eSJonathan Lemon static void
2142f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2143ba8c6fd5SDavid Greenman {
2144ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2145f7788e8eSJonathan Lemon 	struct mii_data *mii;
2146ba8c6fd5SDavid Greenman 
2147f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
2148f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2149f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2150f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
21512e2b8238SJonathan Lemon 
21522e2b8238SJonathan Lemon 	if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
21532e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
21542e2b8238SJonathan Lemon 	else
21552e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
2156ba8c6fd5SDavid Greenman }
2157ba8c6fd5SDavid Greenman 
2158a17c678eSDavid Greenman /*
2159a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2160a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
2161a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
2162dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
2163a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2164a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2165a17c678eSDavid Greenman  */
2166a17c678eSDavid Greenman static int
2167b2badf02SMaxime Henrion fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2168a17c678eSDavid Greenman {
2169a17c678eSDavid Greenman 	struct mbuf *m;
2170a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
2171b2badf02SMaxime Henrion 	struct fxp_rx *p_rx;
2172b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
2173b2badf02SMaxime Henrion 	int error;
2174a17c678eSDavid Greenman 
2175a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2176b2badf02SMaxime Henrion 	if (m == NULL)
2177b2badf02SMaxime Henrion 		return (ENOBUFS);
2178ba8c6fd5SDavid Greenman 
2179ba8c6fd5SDavid Greenman 	/*
2180ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2181ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2182ba8c6fd5SDavid Greenman 	 */
2183ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2184ba8c6fd5SDavid Greenman 
2185eadd5e3aSDavid Greenman 	/*
2186eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2187eadd5e3aSDavid Greenman 	 * data start past it.
2188eadd5e3aSDavid Greenman 	 */
2189a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2190c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
219183e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2192eadd5e3aSDavid Greenman 
2193ba8c6fd5SDavid Greenman 	/*
2194ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
2195ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
2196ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
2197ba8c6fd5SDavid Greenman 	 */
21984fc1dda9SAndrew Gallatin 
2199a17c678eSDavid Greenman 	rfa->rfa_status = 0;
220083e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2201a17c678eSDavid Greenman 	rfa->actual_size = 0;
2202ba8c6fd5SDavid Greenman 
220383e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
220483e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2205ba8c6fd5SDavid Greenman 
2206b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2207b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2208b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2209b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2210b2badf02SMaxime Henrion 	if (error) {
2211b2badf02SMaxime Henrion 		m_freem(m);
2212b2badf02SMaxime Henrion 		return (error);
2213b2badf02SMaxime Henrion 	}
2214b2badf02SMaxime Henrion 
2215b2badf02SMaxime Henrion 	bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2216b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2217b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2218b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2219b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2220b2badf02SMaxime Henrion 
2221b983c7b3SMaxime Henrion 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
2222b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2223b2badf02SMaxime Henrion 
2224dfe61cf1SDavid Greenman 	/*
2225dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2226dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2227dfe61cf1SDavid Greenman 	 */
2228b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2229b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2230b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2231b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2232b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
223383e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2234aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2235b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
22364cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2237a17c678eSDavid Greenman 	} else {
2238b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2239b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2240a17c678eSDavid Greenman 	}
2241b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
2242b2badf02SMaxime Henrion 	return (0);
2243a17c678eSDavid Greenman }
2244a17c678eSDavid Greenman 
22456ebc3153SDavid Greenman static volatile int
2246f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2247dccee1a1SDavid Greenman {
2248f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2249dccee1a1SDavid Greenman 	int count = 10000;
22506ebc3153SDavid Greenman 	int value;
2251dccee1a1SDavid Greenman 
2252ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2253ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2254dccee1a1SDavid Greenman 
2255ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2256ba8c6fd5SDavid Greenman 	    && count--)
22576ebc3153SDavid Greenman 		DELAY(10);
2258dccee1a1SDavid Greenman 
2259dccee1a1SDavid Greenman 	if (count <= 0)
2260f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2261dccee1a1SDavid Greenman 
22626ebc3153SDavid Greenman 	return (value & 0xffff);
2263dccee1a1SDavid Greenman }
2264dccee1a1SDavid Greenman 
2265dccee1a1SDavid Greenman static void
2266f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2267dccee1a1SDavid Greenman {
2268f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2269dccee1a1SDavid Greenman 	int count = 10000;
2270dccee1a1SDavid Greenman 
2271ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2272ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2273ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2274dccee1a1SDavid Greenman 
2275ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2276ba8c6fd5SDavid Greenman 	    count--)
22776ebc3153SDavid Greenman 		DELAY(10);
2278dccee1a1SDavid Greenman 
2279dccee1a1SDavid Greenman 	if (count <= 0)
2280f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
2281dccee1a1SDavid Greenman }
2282dccee1a1SDavid Greenman 
2283dccee1a1SDavid Greenman static int
2284f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2285a17c678eSDavid Greenman {
22869b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2287a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2288f7788e8eSJonathan Lemon 	struct mii_data *mii;
2289f7788e8eSJonathan Lemon 	int s, error = 0;
2290a17c678eSDavid Greenman 
2291f7788e8eSJonathan Lemon 	s = splimp();
2292a17c678eSDavid Greenman 
2293a17c678eSDavid Greenman 	switch (command) {
2294a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
2295f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2296f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2297f7788e8eSJonathan Lemon 		else
2298f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2299a17c678eSDavid Greenman 
2300a17c678eSDavid Greenman 		/*
2301a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2302a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2303a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2304a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2305a17c678eSDavid Greenman 		 */
2306a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
2307fb583156SDavid Greenman 			fxp_init(sc);
2308a17c678eSDavid Greenman 		} else {
2309a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
23104a5f1499SDavid Greenman 				fxp_stop(sc);
2311a17c678eSDavid Greenman 		}
2312a17c678eSDavid Greenman 		break;
2313a17c678eSDavid Greenman 
2314a17c678eSDavid Greenman 	case SIOCADDMULTI:
2315a17c678eSDavid Greenman 	case SIOCDELMULTI:
2316f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2317f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2318f7788e8eSJonathan Lemon 		else
2319f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2320a17c678eSDavid Greenman 		/*
2321a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2322a17c678eSDavid Greenman 		 * accordingly.
2323a17c678eSDavid Greenman 		 */
2324f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2325397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2326397f9dfeSDavid Greenman 		/*
2327f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2328397f9dfeSDavid Greenman 		 * again rather than else {}.
2329397f9dfeSDavid Greenman 		 */
2330f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
2331fb583156SDavid Greenman 			fxp_init(sc);
2332a17c678eSDavid Greenman 		error = 0;
2333ba8c6fd5SDavid Greenman 		break;
2334ba8c6fd5SDavid Greenman 
2335ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2336ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2337f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2338f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2339f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2340f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2341f7788e8eSJonathan Lemon 		} else {
2342ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2343f7788e8eSJonathan Lemon 		}
2344a17c678eSDavid Greenman 		break;
2345a17c678eSDavid Greenman 
2346a17c678eSDavid Greenman 	default:
2347673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2348a17c678eSDavid Greenman 	}
2349f7788e8eSJonathan Lemon 	splx(s);
2350a17c678eSDavid Greenman 	return (error);
2351a17c678eSDavid Greenman }
2352397f9dfeSDavid Greenman 
2353397f9dfeSDavid Greenman /*
235409882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
235509882363SJonathan Lemon  */
235609882363SJonathan Lemon static int
235709882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
235809882363SJonathan Lemon {
235909882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
236009882363SJonathan Lemon 	struct ifnet *ifp = &sc->sc_if;
236109882363SJonathan Lemon 	struct ifmultiaddr *ifma;
236209882363SJonathan Lemon 	int nmcasts;
236309882363SJonathan Lemon 
236409882363SJonathan Lemon 	nmcasts = 0;
236509882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
236609882363SJonathan Lemon #if __FreeBSD_version < 500000
236709882363SJonathan Lemon 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
236809882363SJonathan Lemon #else
236909882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
237009882363SJonathan Lemon #endif
237109882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
237209882363SJonathan Lemon 				continue;
237309882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
237409882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
237509882363SJonathan Lemon 				nmcasts = 0;
237609882363SJonathan Lemon 				break;
237709882363SJonathan Lemon 			}
237809882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2379bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
238009882363SJonathan Lemon 			nmcasts++;
238109882363SJonathan Lemon 		}
238209882363SJonathan Lemon 	}
2383bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
238409882363SJonathan Lemon 	return (nmcasts);
238509882363SJonathan Lemon }
238609882363SJonathan Lemon 
238709882363SJonathan Lemon /*
2388397f9dfeSDavid Greenman  * Program the multicast filter.
2389397f9dfeSDavid Greenman  *
2390397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2391397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
23923114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2393397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2394dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2395397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2396397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2397397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2398397f9dfeSDavid Greenman  *
2399397f9dfeSDavid Greenman  * This function must be called at splimp.
2400397f9dfeSDavid Greenman  */
2401397f9dfeSDavid Greenman static void
2402f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2403397f9dfeSDavid Greenman {
2404397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2405397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
2406b2badf02SMaxime Henrion 	struct fxp_tx *txp;
24077dced78aSDavid Greenman 	int count;
2408397f9dfeSDavid Greenman 
24093114fdb4SDavid Greenman 	/*
24103114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
24113114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
24123114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
24133114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
24143114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
24153114fdb4SDavid Greenman 	 */
2416397f9dfeSDavid Greenman 	if (sc->tx_queued) {
24173114fdb4SDavid Greenman 		/*
24183114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
24193114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
24203114fdb4SDavid Greenman 		 */
24213114fdb4SDavid Greenman 		if (sc->need_mcsetup)
24223114fdb4SDavid Greenman 			return;
2423397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
24243114fdb4SDavid Greenman 
24253114fdb4SDavid Greenman 		/*
242672a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
242772a32a26SJonathan Lemon 		 * when all TX commands have been processed.
24283114fdb4SDavid Greenman 		 */
2429b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
2430b2badf02SMaxime Henrion 		txp->tx_mbuf = NULL;
2431b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
243283e6547dSMaxime Henrion 		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
243383e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
24343114fdb4SDavid Greenman 		/*
24353114fdb4SDavid Greenman 		 * Advance the end of list forward.
24363114fdb4SDavid Greenman 		 */
243783e6547dSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
243883e6547dSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
24395f361cbeSMaxime Henrion 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2440b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
24413114fdb4SDavid Greenman 		sc->tx_queued++;
24423114fdb4SDavid Greenman 		/*
24433114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
24443114fdb4SDavid Greenman 		 */
24453114fdb4SDavid Greenman 		fxp_scb_wait(sc);
24462e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
24473114fdb4SDavid Greenman 		/*
24483114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
24493114fdb4SDavid Greenman 		 * card again.
24503114fdb4SDavid Greenman 		 */
24513114fdb4SDavid Greenman 		ifp->if_timer = 5;
24523114fdb4SDavid Greenman 
2453397f9dfeSDavid Greenman 		return;
2454397f9dfeSDavid Greenman 	}
2455397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2456397f9dfeSDavid Greenman 
2457397f9dfeSDavid Greenman 	/*
2458397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2459397f9dfeSDavid Greenman 	 */
2460397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
246183e6547dSMaxime Henrion 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
246283e6547dSMaxime Henrion 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
246383e6547dSMaxime Henrion 	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2464b2badf02SMaxime Henrion 	txp = &sc->fxp_desc.mcs_tx;
2465b2badf02SMaxime Henrion 	txp->tx_mbuf = NULL;
2466b2badf02SMaxime Henrion 	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2467b2badf02SMaxime Henrion 	txp->tx_next = sc->fxp_desc.tx_list;
246809882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2469b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2470397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2471397f9dfeSDavid Greenman 
2472397f9dfeSDavid Greenman 	/*
2473397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2474397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2475397f9dfeSDavid Greenman 	 */
24767dced78aSDavid Greenman 	count = 100;
2477397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
24787dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
24797dced78aSDavid Greenman 		DELAY(10);
24807dced78aSDavid Greenman 	if (count == 0) {
2481f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
24827dced78aSDavid Greenman 		return;
24837dced78aSDavid Greenman 	}
2484397f9dfeSDavid Greenman 
2485397f9dfeSDavid Greenman 	/*
2486397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2487397f9dfeSDavid Greenman 	 */
2488397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2489b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2490b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
24912e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2492397f9dfeSDavid Greenman 
24933114fdb4SDavid Greenman 	ifp->if_timer = 2;
2494397f9dfeSDavid Greenman 	return;
2495397f9dfeSDavid Greenman }
249672a32a26SJonathan Lemon 
249772a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
249872a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
249972a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
250072a32a26SJonathan Lemon static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
250172a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
250272a32a26SJonathan Lemon static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
250372a32a26SJonathan Lemon 
250472a32a26SJonathan Lemon #define UCODE(x)	x, sizeof(x)
250572a32a26SJonathan Lemon 
250672a32a26SJonathan Lemon struct ucode {
250772a32a26SJonathan Lemon 	u_int32_t	revision;
250872a32a26SJonathan Lemon 	u_int32_t	*ucode;
250972a32a26SJonathan Lemon 	int		length;
251072a32a26SJonathan Lemon 	u_short		int_delay_offset;
251172a32a26SJonathan Lemon 	u_short		bundle_max_offset;
251272a32a26SJonathan Lemon } ucode_table[] = {
251372a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
251472a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
251572a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
251672a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
251772a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
251872a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
251972a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
252072a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
252172a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
252272a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
252372a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
252472a32a26SJonathan Lemon };
252572a32a26SJonathan Lemon 
252672a32a26SJonathan Lemon static void
252772a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
252872a32a26SJonathan Lemon {
252972a32a26SJonathan Lemon 	struct ucode *uc;
253072a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
253172a32a26SJonathan Lemon 
253272a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
253372a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
253472a32a26SJonathan Lemon 			break;
253572a32a26SJonathan Lemon 	if (uc->ucode == NULL)
253672a32a26SJonathan Lemon 		return;
2537b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
253872a32a26SJonathan Lemon 	cbp->cb_status = 0;
253983e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
254083e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
254172a32a26SJonathan Lemon 	memcpy(cbp->ucode, uc->ucode, uc->length);
254272a32a26SJonathan Lemon 	if (uc->int_delay_offset)
254383e6547dSMaxime Henrion 		*(u_int16_t *)&cbp->ucode[uc->int_delay_offset] =
254483e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
254572a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
254683e6547dSMaxime Henrion 		*(u_int16_t *)&cbp->ucode[uc->bundle_max_offset] =
254783e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
254872a32a26SJonathan Lemon 	/*
254972a32a26SJonathan Lemon 	 * Download the ucode to the chip.
255072a32a26SJonathan Lemon 	 */
255172a32a26SJonathan Lemon 	fxp_scb_wait(sc);
2552b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2553b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
255472a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
255572a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2556209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2557b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
255872a32a26SJonathan Lemon 	device_printf(sc->dev,
255972a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
256072a32a26SJonathan Lemon 	    sc->tunable_int_delay,
256172a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
256272a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
256372a32a26SJonathan Lemon }
256472a32a26SJonathan Lemon 
256572a32a26SJonathan Lemon static int
256672a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
256772a32a26SJonathan Lemon {
256872a32a26SJonathan Lemon 	int error, value;
256972a32a26SJonathan Lemon 
257072a32a26SJonathan Lemon 	value = *(int *)arg1;
257172a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
257272a32a26SJonathan Lemon 	if (error || !req->newptr)
257372a32a26SJonathan Lemon 		return (error);
257472a32a26SJonathan Lemon 	if (value < low || value > high)
257572a32a26SJonathan Lemon 		return (EINVAL);
257672a32a26SJonathan Lemon 	*(int *)arg1 = value;
257772a32a26SJonathan Lemon 	return (0);
257872a32a26SJonathan Lemon }
257972a32a26SJonathan Lemon 
258072a32a26SJonathan Lemon /*
258172a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
258272a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
258372a32a26SJonathan Lemon  */
258472a32a26SJonathan Lemon static int
258572a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
258672a32a26SJonathan Lemon {
258772a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
258872a32a26SJonathan Lemon }
258972a32a26SJonathan Lemon 
259072a32a26SJonathan Lemon static int
259172a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
259272a32a26SJonathan Lemon {
259372a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
259472a32a26SJonathan Lemon }
2595