xref: /freebsd/sys/dev/fxp/if_fxp.c (revision af75b654d2a242b7b32daf961888d59cf1314058)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28a17c678eSDavid Greenman  */
29a17c678eSDavid Greenman 
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
32aad970f1SDavid E. O'Brien 
33a17c678eSDavid Greenman /*
34ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
35a17c678eSDavid Greenman  */
36a17c678eSDavid Greenman 
37f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
38f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
39f0796cd2SGleb Smirnoff #endif
40f0796cd2SGleb Smirnoff 
41a17c678eSDavid Greenman #include <sys/param.h>
42a17c678eSDavid Greenman #include <sys/systm.h>
438fae3bd4SPyun YongHyeon #include <sys/bus.h>
4483e6547dSMaxime Henrion #include <sys/endian.h>
45a17c678eSDavid Greenman #include <sys/kernel.h>
468fae3bd4SPyun YongHyeon #include <sys/mbuf.h>
47fe12f24bSPoul-Henning Kamp #include <sys/module.h>
488fae3bd4SPyun YongHyeon #include <sys/rman.h>
494458ac71SBruce Evans #include <sys/socket.h>
508fae3bd4SPyun YongHyeon #include <sys/sockio.h>
5172a32a26SJonathan Lemon #include <sys/sysctl.h>
52a17c678eSDavid Greenman 
538fae3bd4SPyun YongHyeon #include <net/bpf.h>
548fae3bd4SPyun YongHyeon #include <net/ethernet.h>
55a17c678eSDavid Greenman #include <net/if.h>
568fae3bd4SPyun YongHyeon #include <net/if_arp.h>
57397f9dfeSDavid Greenman #include <net/if_dl.h>
58ba8c6fd5SDavid Greenman #include <net/if_media.h>
59e8c8b728SJonathan Lemon #include <net/if_types.h>
60e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
61e8c8b728SJonathan Lemon 
62c8bca6dcSBill Paul #include <netinet/in.h>
63c8bca6dcSBill Paul #include <netinet/in_systm.h>
64c8bca6dcSBill Paul #include <netinet/ip.h>
65f13075afSPyun YongHyeon #include <netinet/tcp.h>
66f13075afSPyun YongHyeon #include <netinet/udp.h>
67f13075afSPyun YongHyeon 
68f13075afSPyun YongHyeon #include <machine/bus.h>
69c8bca6dcSBill Paul #include <machine/in_cksum.h>
70f13075afSPyun YongHyeon #include <machine/resource.h>
71c8bca6dcSBill Paul 
724fbd232cSWarner Losh #include <dev/pci/pcivar.h>
734fbd232cSWarner Losh #include <dev/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 {
14274d1ed23SMaxime Henrion 	uint16_t	devid;
143f19fc5d8SJohn Polstra 	int16_t		revid;		/* -1 matches anything */
144f7788e8eSJonathan Lemon 	char 		*name;
145f7788e8eSJonathan Lemon };
146f7788e8eSJonathan Lemon 
147f7788e8eSJonathan Lemon /*
148f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
149f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
150f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
151f7788e8eSJonathan Lemon  * them.
152f7788e8eSJonathan Lemon  */
153f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
154f19fc5d8SJohn Polstra     { 0x1029,	-1,	"Intel 82559 PCI/CardBus Pro/100" },
155f19fc5d8SJohn Polstra     { 0x1030,	-1,	"Intel 82559 Pro/100 Ethernet" },
156f19fc5d8SJohn Polstra     { 0x1031,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
157f19fc5d8SJohn Polstra     { 0x1032,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
158f19fc5d8SJohn Polstra     { 0x1033,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
159f19fc5d8SJohn Polstra     { 0x1034,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
160f19fc5d8SJohn Polstra     { 0x1035,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
161f19fc5d8SJohn Polstra     { 0x1036,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
162f19fc5d8SJohn Polstra     { 0x1037,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
163f19fc5d8SJohn Polstra     { 0x1038,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
164f19fc5d8SJohn Polstra     { 0x1039,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
165f19fc5d8SJohn Polstra     { 0x103A,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
166f19fc5d8SJohn Polstra     { 0x103B,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
167f19fc5d8SJohn Polstra     { 0x103C,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
168f19fc5d8SJohn Polstra     { 0x103D,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
169f19fc5d8SJohn Polstra     { 0x103E,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
170f19fc5d8SJohn Polstra     { 0x1050,	-1,	"Intel 82801BA (D865) Pro/100 VE Ethernet" },
171c2b37819SWarner Losh     { 0x1051,	-1,	"Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
172f19fc5d8SJohn Polstra     { 0x1059,	-1,	"Intel 82551QM Pro/100 M Mobile Connection" },
173048ca166SMaxime Henrion     { 0x1064,	-1,	"Intel 82562EZ (ICH6)" },
17442a4336aSRink Springer     { 0x1065,	-1,	"Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
17529a8929dSMaxime Henrion     { 0x1068,	-1,	"Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
17652dfd9cdSMaxime Henrion     { 0x1069,	-1,	"Intel 82562EM/EX/GX Pro/100 Ethernet" },
177847f5310SRemko Lodder     { 0x1091,	-1,	"Intel 82562GX Pro/100 Ethernet" },
178c943ffccSMatteo Riondato     { 0x1092,	-1,	"Intel Pro/100 VE Network Connection" },
179597d4fe4SRink Springer     { 0x1093,	-1,	"Intel Pro/100 VM Network Connection" },
18042a4336aSRink Springer     { 0x1094,	-1,	"Intel Pro/100 946GZ (ICH7) Network Connection" },
181f19fc5d8SJohn Polstra     { 0x1209,	-1,	"Intel 82559ER Embedded 10/100 Ethernet" },
182f19fc5d8SJohn Polstra     { 0x1229,	0x01,	"Intel 82557 Pro/100 Ethernet" },
183f19fc5d8SJohn Polstra     { 0x1229,	0x02,	"Intel 82557 Pro/100 Ethernet" },
184f19fc5d8SJohn Polstra     { 0x1229,	0x03,	"Intel 82557 Pro/100 Ethernet" },
185f19fc5d8SJohn Polstra     { 0x1229,	0x04,	"Intel 82558 Pro/100 Ethernet" },
186f19fc5d8SJohn Polstra     { 0x1229,	0x05,	"Intel 82558 Pro/100 Ethernet" },
187f19fc5d8SJohn Polstra     { 0x1229,	0x06,	"Intel 82559 Pro/100 Ethernet" },
188f19fc5d8SJohn Polstra     { 0x1229,	0x07,	"Intel 82559 Pro/100 Ethernet" },
189f19fc5d8SJohn Polstra     { 0x1229,	0x08,	"Intel 82559 Pro/100 Ethernet" },
190f19fc5d8SJohn Polstra     { 0x1229,	0x09,	"Intel 82559ER Pro/100 Ethernet" },
191f19fc5d8SJohn Polstra     { 0x1229,	0x0c,	"Intel 82550 Pro/100 Ethernet" },
192f19fc5d8SJohn Polstra     { 0x1229,	0x0d,	"Intel 82550 Pro/100 Ethernet" },
193f19fc5d8SJohn Polstra     { 0x1229,	0x0e,	"Intel 82550 Pro/100 Ethernet" },
194f19fc5d8SJohn Polstra     { 0x1229,	0x0f,	"Intel 82551 Pro/100 Ethernet" },
195f19fc5d8SJohn Polstra     { 0x1229,	0x10,	"Intel 82551 Pro/100 Ethernet" },
196f19fc5d8SJohn Polstra     { 0x1229,	-1,	"Intel 82557/8/9 Pro/100 Ethernet" },
197f19fc5d8SJohn Polstra     { 0x2449,	-1,	"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
19886c8aacbSMaxime Henrion     { 0x27dc,	-1,	"Intel 82801GB (ICH7) 10/100 Ethernet" },
199f19fc5d8SJohn Polstra     { 0,	-1,	NULL },
200f7788e8eSJonathan Lemon };
201f7788e8eSJonathan Lemon 
202c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
203c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
204c8bca6dcSBill Paul #else
205c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
206c8bca6dcSBill Paul #endif
207c8bca6dcSBill Paul 
208f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
209f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
210f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
211f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
212f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
213f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
214f7788e8eSJonathan Lemon 
215f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
216f13075afSPyun YongHyeon static void		fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp,
217f13075afSPyun YongHyeon 			    struct mbuf *m, uint16_t status, int pos);
2184953bccaSNate Lawson static void		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
21974d1ed23SMaxime Henrion 			    uint8_t statack, int count);
220f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2214953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
222f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
223f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
2244953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
2254e53f837SPyun YongHyeon static int		fxp_encap(struct fxp_softc *sc, struct mbuf **m_head);
2264e53f837SPyun YongHyeon static void		fxp_txeof(struct fxp_softc *sc);
227f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
228f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
229f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
230f7788e8eSJonathan Lemon 			    caddr_t data);
231df79d527SGleb Smirnoff static void 		fxp_watchdog(struct fxp_softc *sc);
23285050421SPyun YongHyeon static void		fxp_add_rfabuf(struct fxp_softc *sc,
23385050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23485050421SPyun YongHyeon static void		fxp_discard_rfabuf(struct fxp_softc *sc,
23585050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23685050421SPyun YongHyeon static int		fxp_new_rfabuf(struct fxp_softc *sc,
23785050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23809882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
239f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
24074d1ed23SMaxime Henrion static uint16_t		fxp_eeprom_getword(struct fxp_softc *sc, int offset,
241f7788e8eSJonathan Lemon 			    int autosize);
24200c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
24374d1ed23SMaxime Henrion 			    uint16_t data);
244f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
245f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
246f7788e8eSJonathan Lemon 			    int offset, int words);
24700c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
24800c4116bSJonathan Lemon 			    int offset, int words);
249f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
250f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
251f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
252f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
253f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
254f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
255f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
256f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
257f7788e8eSJonathan Lemon 			    int value);
25872a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
25972a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
26072a32a26SJonathan Lemon 			    int low, int high);
26172a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
26272a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
26328935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
26428935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
26528935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
26674d1ed23SMaxime Henrion     			    volatile uint16_t *status, bus_dma_tag_t dmat,
267209b07bcSMaxime Henrion 			    bus_dmamap_t map);
268f7788e8eSJonathan Lemon 
269f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
270f7788e8eSJonathan Lemon 	/* Device interface */
271f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
272f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
273f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
274f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
275f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
276f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
277f7788e8eSJonathan Lemon 
278f7788e8eSJonathan Lemon 	/* MII interface */
279f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
280f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
281f7788e8eSJonathan Lemon 
282f7788e8eSJonathan Lemon 	{ 0, 0 }
283f7788e8eSJonathan Lemon };
284f7788e8eSJonathan Lemon 
285f7788e8eSJonathan Lemon static driver_t fxp_driver = {
286f7788e8eSJonathan Lemon 	"fxp",
287f7788e8eSJonathan Lemon 	fxp_methods,
288f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
289f7788e8eSJonathan Lemon };
290f7788e8eSJonathan Lemon 
291f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
292f7788e8eSJonathan Lemon 
293f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
294347934faSWarner Losh DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
295f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
296f7788e8eSJonathan Lemon 
29705bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
29805bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
29905bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30005bd8c22SMaxime Henrion 	{ -1, 0 }
30105bd8c22SMaxime Henrion };
30205bd8c22SMaxime Henrion 
30305bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
30405bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
30505bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30605bd8c22SMaxime Henrion 	{ -1, 0 }
30705bd8c22SMaxime Henrion };
30805bd8c22SMaxime Henrion 
309f7788e8eSJonathan Lemon /*
310dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
311dfe61cf1SDavid Greenman  * completed).
312dfe61cf1SDavid Greenman  */
31328935f27SMaxime Henrion static void
314f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
315a17c678eSDavid Greenman {
3163cf09dd1SMarcel Moolenaar 	union {
3173cf09dd1SMarcel Moolenaar 		uint16_t w;
3183cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3193cf09dd1SMarcel Moolenaar 	} flowctl;
320a17c678eSDavid Greenman 	int i = 10000;
321a17c678eSDavid Greenman 
3227dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3237dced78aSDavid Greenman 		DELAY(2);
3243cf09dd1SMarcel Moolenaar 	if (i == 0) {
3253cf09dd1SMarcel Moolenaar 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL);
3263cf09dd1SMarcel Moolenaar 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL + 1);
32700c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
328e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
329e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3303cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3313cf09dd1SMarcel Moolenaar 	}
3327dced78aSDavid Greenman }
3337dced78aSDavid Greenman 
33428935f27SMaxime Henrion static void
3352e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3362e2b8238SJonathan Lemon {
3372e2b8238SJonathan Lemon 
3382e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3392e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3402e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3412e2b8238SJonathan Lemon 	}
3422e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3432e2b8238SJonathan Lemon }
3442e2b8238SJonathan Lemon 
34528935f27SMaxime Henrion static void
34674d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
347209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3487dced78aSDavid Greenman {
3497dced78aSDavid Greenman 	int i = 10000;
3507dced78aSDavid Greenman 
351209b07bcSMaxime Henrion 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
352209b07bcSMaxime Henrion 	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) {
3537dced78aSDavid Greenman 		DELAY(2);
354209b07bcSMaxime Henrion 		bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
355209b07bcSMaxime Henrion 	}
3567dced78aSDavid Greenman 	if (i == 0)
357f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
358a17c678eSDavid Greenman }
359a17c678eSDavid Greenman 
360dfe61cf1SDavid Greenman /*
36128935f27SMaxime Henrion  * Return identification string if this device is ours.
362dfe61cf1SDavid Greenman  */
3636182fdbdSPeter Wemm static int
3646182fdbdSPeter Wemm fxp_probe(device_t dev)
365a17c678eSDavid Greenman {
36674d1ed23SMaxime Henrion 	uint16_t devid;
36774d1ed23SMaxime Henrion 	uint8_t revid;
368f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
369f7788e8eSJonathan Lemon 
37055ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
371f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
372f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
373f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
374f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
375f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
376f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
377538565c4SWarner Losh 				return (BUS_PROBE_DEFAULT);
37855ce7b51SDavid Greenman 			}
379dd68ef16SPeter Wemm 		}
380f7788e8eSJonathan Lemon 	}
381f7788e8eSJonathan Lemon 	return (ENXIO);
3826182fdbdSPeter Wemm }
3836182fdbdSPeter Wemm 
384b2badf02SMaxime Henrion static void
385b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
386b2badf02SMaxime Henrion {
38774d1ed23SMaxime Henrion 	uint32_t *addr;
388b2badf02SMaxime Henrion 
389b2badf02SMaxime Henrion 	if (error)
390b2badf02SMaxime Henrion 		return;
391b2badf02SMaxime Henrion 
392b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
393b2badf02SMaxime Henrion 	addr = arg;
394b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
395b2badf02SMaxime Henrion }
396b2badf02SMaxime Henrion 
3976182fdbdSPeter Wemm static int
3986182fdbdSPeter Wemm fxp_attach(device_t dev)
399a17c678eSDavid Greenman {
4006720ebccSMaxime Henrion 	struct fxp_softc *sc;
4016720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4026720ebccSMaxime Henrion 	struct fxp_tx *txp;
403b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4046720ebccSMaxime Henrion 	struct ifnet *ifp;
40574d1ed23SMaxime Henrion 	uint32_t val;
40674d1ed23SMaxime Henrion 	uint16_t data, myea[ETHER_ADDR_LEN / 2];
407fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4087137cea0SPyun YongHyeon 	int i, pmc, prefer_iomap;
4093212724cSJohn Baldwin 	int error;
410a17c678eSDavid Greenman 
4116720ebccSMaxime Henrion 	error = 0;
4126720ebccSMaxime Henrion 	sc = device_get_softc(dev);
413f7788e8eSJonathan Lemon 	sc->dev = dev;
4146008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4154953bccaSNate Lawson 	    MTX_DEF);
4163212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4174953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4184953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
419a17c678eSDavid Greenman 
4207ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4217ba33d82SBrooks Davis 	if (ifp == NULL) {
4227ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4237ba33d82SBrooks Davis 		error = ENOSPC;
4247ba33d82SBrooks Davis 		goto fail;
4257ba33d82SBrooks Davis 	}
4267ba33d82SBrooks Davis 
427dfe61cf1SDavid Greenman 	/*
4282bce79a2SMaxim Sobolev 	 * Enable bus mastering.
429df373873SWes Peters 	 */
430cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4319fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
43279495006SWarner Losh 
433df373873SWes Peters 	/*
4349fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4359fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4369fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
437dfe61cf1SDavid Greenman 	 */
4382a05a4ebSMatt Jacob 	prefer_iomap = 0;
43905bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
44005bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
44105bd8c22SMaxime Henrion 	if (prefer_iomap)
44205bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
44305bd8c22SMaxime Henrion 	else
44405bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4459fa6ccfbSMatt Jacob 
44605bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
44705bd8c22SMaxime Henrion 	if (error) {
44805bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
44905bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
45005bd8c22SMaxime Henrion 		else
45105bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
45205bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4539fa6ccfbSMatt Jacob 	}
45405bd8c22SMaxime Henrion 	if (error) {
45505bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4566182fdbdSPeter Wemm 		error = ENXIO;
457a17c678eSDavid Greenman 		goto fail;
458a17c678eSDavid Greenman 	}
45905bd8c22SMaxime Henrion 
4609fa6ccfbSMatt Jacob 	if (bootverbose) {
4619fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
46205bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4636182fdbdSPeter Wemm 	}
4646182fdbdSPeter Wemm 
465f7788e8eSJonathan Lemon 	/*
466f7788e8eSJonathan Lemon 	 * Reset to a stable state.
467f7788e8eSJonathan Lemon 	 */
468f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
469f7788e8eSJonathan Lemon 	DELAY(10);
470f7788e8eSJonathan Lemon 
471f7788e8eSJonathan Lemon 	/*
472f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
473f7788e8eSJonathan Lemon 	 */
474f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
475f7788e8eSJonathan Lemon 
476f7788e8eSJonathan Lemon 	/*
47793b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
47893b6e2e6SMaxime Henrion 	 */
47993b6e2e6SMaxime Henrion 	fxp_read_eeprom(sc, &data, 5, 1);
48093b6e2e6SMaxime Henrion 	if ((data >> 8) == 1)
48193b6e2e6SMaxime Henrion 		sc->revision = FXP_REV_82557;
48293b6e2e6SMaxime Henrion 	else
48393b6e2e6SMaxime Henrion 		sc->revision = pci_get_revid(dev);
48493b6e2e6SMaxime Henrion 
48593b6e2e6SMaxime Henrion 	/*
4867137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
4877137cea0SPyun YongHyeon 	 */
4887137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
4897137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
4907137cea0SPyun YongHyeon 		fxp_read_eeprom(sc, &data, 10, 1);
4917137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
4927137cea0SPyun YongHyeon 		    pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0)
4937137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
4947137cea0SPyun YongHyeon 	}
4957137cea0SPyun YongHyeon 
4967137cea0SPyun YongHyeon 	/*
4973bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
498f7788e8eSJonathan Lemon 	 */
499f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
50093b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5014ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
502dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
503f7788e8eSJonathan Lemon 
5040f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5050f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
50650a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
507858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
50872a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
5090f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5100f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
51150a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
512858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
51372a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
5140f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5150f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5160f1db1d6SMaxime Henrion 	    OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
5170f1db1d6SMaxime Henrion 	    "FXP RNR events");
5180f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5190f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5200f1db1d6SMaxime Henrion 	    OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0,
5210f1db1d6SMaxime Henrion 	    "FXP flow control disabled");
52272a32a26SJonathan Lemon 
52372a32a26SJonathan Lemon 	/*
52472a32a26SJonathan Lemon 	 * Pull in device tunables.
52572a32a26SJonathan Lemon 	 */
52672a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
52772a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
52803edfff3SRobert Watson 	sc->tunable_noflow = 1;
52972a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
53072a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
53172a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
53272a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
5330f1db1d6SMaxime Henrion 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
5340f1db1d6SMaxime Henrion 	    "noflow", &sc->tunable_noflow);
5350f1db1d6SMaxime Henrion 	sc->rnr = 0;
53672a32a26SJonathan Lemon 
53772a32a26SJonathan Lemon 	/*
5382e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
53900c4116bSJonathan Lemon 	 *
54072a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
54172a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
54272a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
54300c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
54400c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
54500c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
54600c4116bSJonathan Lemon 	 *
54700c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5482e2b8238SJonathan Lemon 	 */
5492e2b8238SJonathan Lemon 	i = pci_get_device(dev);
55072a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
55172a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
55200c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
55300c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
55474d1ed23SMaxime Henrion 			uint16_t cksum;
55500c4116bSJonathan Lemon 			int i;
55600c4116bSJonathan Lemon 
55700c4116bSJonathan Lemon 			device_printf(dev,
558001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
55900c4116bSJonathan Lemon 			data &= ~0x02;
56000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
56100c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
56200c4116bSJonathan Lemon 			cksum = 0;
56300c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
56400c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
56500c4116bSJonathan Lemon 				cksum += data;
56600c4116bSJonathan Lemon 			}
56700c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
56800c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
56900c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
57000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
57100c4116bSJonathan Lemon 			device_printf(dev,
57200c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
57300c4116bSJonathan Lemon 			    i, data, cksum);
57400c4116bSJonathan Lemon #if 1
57500c4116bSJonathan Lemon 			/*
57600c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
57700c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
57800c4116bSJonathan Lemon 			 */
5792e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
58000c4116bSJonathan Lemon #endif
58100c4116bSJonathan Lemon 		}
58200c4116bSJonathan Lemon 	}
5832e2b8238SJonathan Lemon 
5842e2b8238SJonathan Lemon 	/*
5853bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5863bd07cfdSJonathan Lemon 	 */
58772a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5883bd07cfdSJonathan Lemon 		/*
58974396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
59074396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
59174396a0aSJonathan Lemon 		 * the board to turn on MWI.
5923bd07cfdSJonathan Lemon 		 */
59374396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
59474396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5953bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5963bd07cfdSJonathan Lemon 
5973bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5983bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
59944e0bc11SYaroslav Tykhiy 
60044e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
60144e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
60244e0bc11SYaroslav Tykhiy 	} else {
60344e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
60444e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6053bd07cfdSJonathan Lemon 	}
6063bd07cfdSJonathan Lemon 
607f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
608f13075afSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
609f13075afSPyun YongHyeon 		sc->flags |= FXP_FLAG_82559_RXCSUM;
6103bd07cfdSJonathan Lemon 	/*
611c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
612c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
613c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
614c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
615c8bca6dcSBill Paul 	 */
616507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
617507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
618507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
619c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
620c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
621c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
622f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
623f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
624c8bca6dcSBill Paul 	} else {
625c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
626c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
627c8bca6dcSBill Paul 	}
628c8bca6dcSBill Paul 
629c8bca6dcSBill Paul 	/*
630b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
631b2badf02SMaxime Henrion 	 */
63240c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
633c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
634c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
63540c20505SMaxime Henrion 		sc->maxtxseg--;
636c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
637c21e84e4SPyun YongHyeon 	}
638c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
639c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
640c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
641c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
642c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_mtag);
643b2badf02SMaxime Henrion 	if (error) {
644b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
645b2badf02SMaxime Henrion 		goto fail;
646b2badf02SMaxime Henrion 	}
647b2badf02SMaxime Henrion 
648c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
649c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
650c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
651c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
652b2badf02SMaxime Henrion 	if (error) {
653b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
654b2badf02SMaxime Henrion 		goto fail;
655b2badf02SMaxime Henrion 	}
656b2badf02SMaxime Henrion 
657b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
658aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap);
659b2badf02SMaxime Henrion 	if (error)
6604953bccaSNate Lawson 		goto fail;
661b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
662b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
663b2badf02SMaxime Henrion 	if (error) {
664b2badf02SMaxime Henrion 		device_printf(dev, "could not map the stats buffer\n");
665b2badf02SMaxime Henrion 		goto fail;
666b2badf02SMaxime Henrion 	}
667b2badf02SMaxime Henrion 
668c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
669c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
670c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
671c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
672b2badf02SMaxime Henrion 	if (error) {
673b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
674b2badf02SMaxime Henrion 		goto fail;
675b2badf02SMaxime Henrion 	}
676b2badf02SMaxime Henrion 
677b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
678aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map);
679b2badf02SMaxime Henrion 	if (error)
6804953bccaSNate Lawson 		goto fail;
681b2badf02SMaxime Henrion 
682b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
683b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
684b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
685b2badf02SMaxime Henrion 	if (error) {
686b2badf02SMaxime Henrion 		device_printf(dev, "could not map DMA memory\n");
687b2badf02SMaxime Henrion 		goto fail;
688b2badf02SMaxime Henrion 	}
689b2badf02SMaxime Henrion 
690c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
691c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
692c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
693c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
694b2badf02SMaxime Henrion 	if (error) {
695b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
696b2badf02SMaxime Henrion 		goto fail;
697b2badf02SMaxime Henrion 	}
698b2badf02SMaxime Henrion 
699b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
700b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->mcs_map);
701b2badf02SMaxime Henrion 	if (error)
7024953bccaSNate Lawson 		goto fail;
703b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
704b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
705b2badf02SMaxime Henrion 	if (error) {
706b2badf02SMaxime Henrion 		device_printf(dev, "can't map the multicast setup command\n");
707b2badf02SMaxime Henrion 		goto fail;
708b2badf02SMaxime Henrion 	}
709b2badf02SMaxime Henrion 
710b2badf02SMaxime Henrion 	/*
7116720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7126720ebccSMaxime Henrion 	 * the TX command blocks.
713b2badf02SMaxime Henrion 	 */
7146720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7156720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7164cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7176720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
7186720ebccSMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &txp[i].tx_map);
719b2badf02SMaxime Henrion 		if (error) {
720b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
721b2badf02SMaxime Henrion 			goto fail;
722b2badf02SMaxime Henrion 		}
723b2badf02SMaxime Henrion 	}
724b2badf02SMaxime Henrion 	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
725b2badf02SMaxime Henrion 	if (error) {
726b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
727b2badf02SMaxime Henrion 		goto fail;
728b2badf02SMaxime Henrion 	}
729b2badf02SMaxime Henrion 
730b2badf02SMaxime Henrion 	/*
731b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
732b2badf02SMaxime Henrion 	 */
733b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
734b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
735b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
736b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
737b2badf02SMaxime Henrion 		if (error) {
738b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
739b2badf02SMaxime Henrion 			goto fail;
740b2badf02SMaxime Henrion 		}
74185050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7424953bccaSNate Lawson 			error = ENOMEM;
7434953bccaSNate Lawson 			goto fail;
7444953bccaSNate Lawson 		}
74585050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
746b2badf02SMaxime Henrion 	}
747b2badf02SMaxime Henrion 
748b2badf02SMaxime Henrion 	/*
749f7788e8eSJonathan Lemon 	 * Read MAC address.
750f7788e8eSJonathan Lemon 	 */
75183e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
752fc74a9f9SBrooks Davis 	eaddr[0] = myea[0] & 0xff;
753fc74a9f9SBrooks Davis 	eaddr[1] = myea[0] >> 8;
754fc74a9f9SBrooks Davis 	eaddr[2] = myea[1] & 0xff;
755fc74a9f9SBrooks Davis 	eaddr[3] = myea[1] >> 8;
756fc74a9f9SBrooks Davis 	eaddr[4] = myea[2] & 0xff;
757fc74a9f9SBrooks Davis 	eaddr[5] = myea[2] >> 8;
758f7788e8eSJonathan Lemon 	if (bootverbose) {
7592e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
760f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7612e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7622e2b8238SJonathan Lemon 		    pci_get_revid(dev));
76372a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
76472a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
76572a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
766f7788e8eSJonathan Lemon 	}
767f7788e8eSJonathan Lemon 
768f7788e8eSJonathan Lemon 	/*
769f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
770f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
771f7788e8eSJonathan Lemon 	 *
772f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
773f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
774f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
775f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
776f7788e8eSJonathan Lemon 	 */
777f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
778f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
779f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
780f7788e8eSJonathan Lemon 	} else {
781f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
782f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
783f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
7846182fdbdSPeter Wemm 			error = ENXIO;
785ba8c6fd5SDavid Greenman 			goto fail;
786a17c678eSDavid Greenman 		}
787f7788e8eSJonathan Lemon 	}
788dccee1a1SDavid Greenman 
7899bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
790fb583156SDavid Greenman 	ifp->if_init = fxp_init;
791ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
792ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
793ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
794ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
795a17c678eSDavid Greenman 
7965fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
7975fe9116bSYaroslav Tykhiy 
798c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
799c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
800c21e84e4SPyun YongHyeon 		ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO;
801c21e84e4SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4;
802c21e84e4SPyun YongHyeon 		ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4;
803c8bca6dcSBill Paul 	}
804c8bca6dcSBill Paul 
805f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
806f13075afSPyun YongHyeon 		ifp->if_capabilities |= IFCAP_RXCSUM;
807f13075afSPyun YongHyeon 		ifp->if_capenable |= IFCAP_RXCSUM;
808f13075afSPyun YongHyeon 	}
809f13075afSPyun YongHyeon 
8107137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
8117137cea0SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL_MAGIC;
8127137cea0SPyun YongHyeon 		ifp->if_capenable |= IFCAP_WOL_MAGIC;
8137137cea0SPyun YongHyeon 	}
8147137cea0SPyun YongHyeon 
815fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
816fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
817fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
818fb917226SRuslan Ermilov #endif
819fb917226SRuslan Ermilov 
820dfe61cf1SDavid Greenman 	/*
8214953bccaSNate Lawson 	 * Attach the interface.
8224953bccaSNate Lawson 	 */
823fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
8244953bccaSNate Lawson 
8254953bccaSNate Lawson 	/*
826e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
8275fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
8285fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
829e8c8b728SJonathan Lemon 	 */
830e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
831673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
83244e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
833e8c8b728SJonathan Lemon 
834483b9871SDavid Greenman 	/*
8353114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8363114fdb4SDavid Greenman 	 * TX descriptors.
837483b9871SDavid Greenman 	 */
8387929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8397929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8407929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8414a684684SDavid Greenman 
842201afb0eSMaxime Henrion 	/*
8434953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
844201afb0eSMaxime Henrion 	 */
84505bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
846ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
847201afb0eSMaxime Henrion 	if (error) {
848201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
849fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
850201afb0eSMaxime Henrion 		goto fail;
851201afb0eSMaxime Henrion 	}
852201afb0eSMaxime Henrion 
8537137cea0SPyun YongHyeon 	/*
8547137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
8557137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
8567137cea0SPyun YongHyeon 	 */
8577137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
8587137cea0SPyun YongHyeon 		FXP_LOCK(sc);
8597137cea0SPyun YongHyeon 		/* Clear wakeup events. */
860af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
8617137cea0SPyun YongHyeon 		fxp_init_body(sc);
8627137cea0SPyun YongHyeon 		fxp_stop(sc);
8637137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
8647137cea0SPyun YongHyeon 	}
8657137cea0SPyun YongHyeon 
866a17c678eSDavid Greenman fail:
8671b5a39d3SBrooks Davis 	if (error)
868f7788e8eSJonathan Lemon 		fxp_release(sc);
869f7788e8eSJonathan Lemon 	return (error);
870f7788e8eSJonathan Lemon }
871f7788e8eSJonathan Lemon 
872f7788e8eSJonathan Lemon /*
8734953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
8744953bccaSNate Lawson  * interrupt should already be torn down.
875f7788e8eSJonathan Lemon  */
876f7788e8eSJonathan Lemon static void
877f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
878f7788e8eSJonathan Lemon {
879b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
880b2badf02SMaxime Henrion 	struct fxp_tx *txp;
881b2badf02SMaxime Henrion 	int i;
882b2badf02SMaxime Henrion 
88367fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
884670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
885670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
8864953bccaSNate Lawson 	if (sc->miibus)
8874953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
8884953bccaSNate Lawson 	bus_generic_detach(sc->dev);
8894953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
890b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
891b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
892b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
893b2badf02SMaxime Henrion 		    sc->cbl_map);
894b2badf02SMaxime Henrion 	}
895b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
896b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
897b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
898b2badf02SMaxime Henrion 	}
899b2badf02SMaxime Henrion 	if (sc->mcsp) {
900b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
901b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
902b2badf02SMaxime Henrion 	}
90305bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
904b983c7b3SMaxime Henrion 	if (sc->fxp_mtag) {
905b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
906b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
907b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
908b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
909b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
910b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
911b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
912b983c7b3SMaxime Henrion 			}
913b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
914b983c7b3SMaxime Henrion 		}
915b983c7b3SMaxime Henrion 		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
916b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
917b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
918b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
919b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
920b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
921b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
922b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
923b983c7b3SMaxime Henrion 			}
924b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
925b983c7b3SMaxime Henrion 		}
926c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_mtag);
927b983c7b3SMaxime Henrion 	}
928c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
929c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
930b2badf02SMaxime Henrion 	if (sc->cbl_tag)
931b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
932b2badf02SMaxime Henrion 	if (sc->mcs_tag)
933b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
934fc74a9f9SBrooks Davis 	if (sc->ifp)
935fc74a9f9SBrooks Davis 		if_free(sc->ifp);
93672a32a26SJonathan Lemon 
9370f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
9386182fdbdSPeter Wemm }
9396182fdbdSPeter Wemm 
9406182fdbdSPeter Wemm /*
9416182fdbdSPeter Wemm  * Detach interface.
9426182fdbdSPeter Wemm  */
9436182fdbdSPeter Wemm static int
9446182fdbdSPeter Wemm fxp_detach(device_t dev)
9456182fdbdSPeter Wemm {
9466182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
9476182fdbdSPeter Wemm 
94840929967SGleb Smirnoff #ifdef DEVICE_POLLING
94940929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
95040929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
95140929967SGleb Smirnoff #endif
95240929967SGleb Smirnoff 
9534953bccaSNate Lawson 	FXP_LOCK(sc);
9541d2945d5SWarner Losh 	sc->suspended = 1;	/* Do same thing as we do for suspend */
9556182fdbdSPeter Wemm 	/*
95632cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
95720f0c80fSMaxime Henrion 	 */
95820f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
95920f0c80fSMaxime Henrion 	fxp_stop(sc);
96032cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
9619eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
96220f0c80fSMaxime Henrion 
9636182fdbdSPeter Wemm 	/*
9643212724cSJohn Baldwin 	 * Close down routes etc.
9653212724cSJohn Baldwin 	 */
9663212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
9673212724cSJohn Baldwin 
9683212724cSJohn Baldwin 	/*
9694953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
9704953bccaSNate Lawson 	 * races with fxp_intr().
9716182fdbdSPeter Wemm 	 */
97205bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
9734953bccaSNate Lawson 	sc->ih = NULL;
9746182fdbdSPeter Wemm 
975f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
976f7788e8eSJonathan Lemon 	fxp_release(sc);
977f7788e8eSJonathan Lemon 	return (0);
978a17c678eSDavid Greenman }
979a17c678eSDavid Greenman 
980a17c678eSDavid Greenman /*
9814a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
982a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
983a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
984a17c678eSDavid Greenman  */
9856182fdbdSPeter Wemm static int
9866182fdbdSPeter Wemm fxp_shutdown(device_t dev)
987a17c678eSDavid Greenman {
9883212724cSJohn Baldwin 
9896182fdbdSPeter Wemm 	/*
9906182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
9916182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
9926182fdbdSPeter Wemm 	 * reboot before the driver initializes.
9936182fdbdSPeter Wemm 	 */
9947137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
995a17c678eSDavid Greenman }
996a17c678eSDavid Greenman 
9977dced78aSDavid Greenman /*
9987dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
9997dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10007dced78aSDavid Greenman  * resume.
10017dced78aSDavid Greenman  */
10027dced78aSDavid Greenman static int
10037dced78aSDavid Greenman fxp_suspend(device_t dev)
10047dced78aSDavid Greenman {
10057dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
10067137cea0SPyun YongHyeon 	struct ifnet *ifp;
10077137cea0SPyun YongHyeon 	int pmc;
10087137cea0SPyun YongHyeon 	uint16_t pmstat;
10097dced78aSDavid Greenman 
10104953bccaSNate Lawson 	FXP_LOCK(sc);
10117dced78aSDavid Greenman 
10127137cea0SPyun YongHyeon 	ifp = sc->ifp;
10137137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
10147137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10157137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
10167137cea0SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
10177137cea0SPyun YongHyeon 			/* Request PME. */
10187137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10197137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10207137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
10217137cea0SPyun YongHyeon 			fxp_init_body(sc);
10227137cea0SPyun YongHyeon 		}
10237137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
10247137cea0SPyun YongHyeon 	}
10257dced78aSDavid Greenman 	fxp_stop(sc);
10267dced78aSDavid Greenman 
10277dced78aSDavid Greenman 	sc->suspended = 1;
10287dced78aSDavid Greenman 
10294953bccaSNate Lawson 	FXP_UNLOCK(sc);
1030f7788e8eSJonathan Lemon 	return (0);
10317dced78aSDavid Greenman }
10327dced78aSDavid Greenman 
10337dced78aSDavid Greenman /*
103467ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
10357dced78aSDavid Greenman  * appropriate.
10367dced78aSDavid Greenman  */
10377dced78aSDavid Greenman static int
10387dced78aSDavid Greenman fxp_resume(device_t dev)
10397dced78aSDavid Greenman {
10407dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
1041fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
10427137cea0SPyun YongHyeon 	int pmc;
10437137cea0SPyun YongHyeon 	uint16_t pmstat;
10447dced78aSDavid Greenman 
10454953bccaSNate Lawson 	FXP_LOCK(sc);
10467dced78aSDavid Greenman 
10477137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
10487137cea0SPyun YongHyeon 		sc->flags &= ~FXP_FLAG_WOL;
10497137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10507137cea0SPyun YongHyeon 		/* Disable PME and clear PME status. */
10517137cea0SPyun YongHyeon 		pmstat &= ~PCIM_PSTAT_PMEENABLE;
10527137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1053af75b654SPyun YongHyeon 		if ((sc->flags & FXP_FLAG_WOLCAP) != 0)
1054af75b654SPyun YongHyeon 			CSR_WRITE_1(sc, FXP_CSR_PMDR,
1055af75b654SPyun YongHyeon 			    CSR_READ_1(sc, FXP_CSR_PMDR));
10567137cea0SPyun YongHyeon 	}
10577137cea0SPyun YongHyeon 
10587dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
10597dced78aSDavid Greenman 	DELAY(10);
10607dced78aSDavid Greenman 
10617dced78aSDavid Greenman 	/* reinitialize interface if necessary */
10627dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
10634953bccaSNate Lawson 		fxp_init_body(sc);
10647dced78aSDavid Greenman 
10657dced78aSDavid Greenman 	sc->suspended = 0;
10667dced78aSDavid Greenman 
10674953bccaSNate Lawson 	FXP_UNLOCK(sc);
1068ba8c6fd5SDavid Greenman 	return (0);
1069f7788e8eSJonathan Lemon }
1070ba8c6fd5SDavid Greenman 
107100c4116bSJonathan Lemon static void
107200c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
107300c4116bSJonathan Lemon {
107474d1ed23SMaxime Henrion 	uint16_t reg;
107500c4116bSJonathan Lemon 	int x;
107600c4116bSJonathan Lemon 
107700c4116bSJonathan Lemon 	/*
107800c4116bSJonathan Lemon 	 * Shift in data.
107900c4116bSJonathan Lemon 	 */
108000c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
108100c4116bSJonathan Lemon 		if (data & x)
108200c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
108300c4116bSJonathan Lemon 		else
108400c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
108500c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
108600c4116bSJonathan Lemon 		DELAY(1);
108700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
108800c4116bSJonathan Lemon 		DELAY(1);
108900c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
109000c4116bSJonathan Lemon 		DELAY(1);
109100c4116bSJonathan Lemon 	}
109200c4116bSJonathan Lemon }
109300c4116bSJonathan Lemon 
1094f7788e8eSJonathan Lemon /*
1095f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1096f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1097f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1098f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1099f7788e8eSJonathan Lemon  * every 16 bits of data.
1100f7788e8eSJonathan Lemon  */
110174d1ed23SMaxime Henrion static uint16_t
1102f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1103f7788e8eSJonathan Lemon {
110474d1ed23SMaxime Henrion 	uint16_t reg, data;
1105f7788e8eSJonathan Lemon 	int x;
1106ba8c6fd5SDavid Greenman 
1107f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1108f7788e8eSJonathan Lemon 	/*
1109f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1110f7788e8eSJonathan Lemon 	 */
111100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1112f7788e8eSJonathan Lemon 	/*
1113f7788e8eSJonathan Lemon 	 * Shift in address.
1114f7788e8eSJonathan Lemon 	 */
1115f7788e8eSJonathan Lemon 	data = 0;
1116f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1117f7788e8eSJonathan Lemon 		if (offset & x)
1118f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1119f7788e8eSJonathan Lemon 		else
1120f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1121f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1122f7788e8eSJonathan Lemon 		DELAY(1);
1123f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1124f7788e8eSJonathan Lemon 		DELAY(1);
1125f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1126f7788e8eSJonathan Lemon 		DELAY(1);
1127f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1128f7788e8eSJonathan Lemon 		data++;
1129f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1130f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1131f7788e8eSJonathan Lemon 			break;
1132f7788e8eSJonathan Lemon 		}
1133f7788e8eSJonathan Lemon 	}
1134f7788e8eSJonathan Lemon 	/*
1135f7788e8eSJonathan Lemon 	 * Shift out data.
1136f7788e8eSJonathan Lemon 	 */
1137f7788e8eSJonathan Lemon 	data = 0;
1138f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1139f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1140f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1141f7788e8eSJonathan Lemon 		DELAY(1);
1142f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1143f7788e8eSJonathan Lemon 			data |= x;
1144f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1145f7788e8eSJonathan Lemon 		DELAY(1);
1146f7788e8eSJonathan Lemon 	}
1147f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1148f7788e8eSJonathan Lemon 	DELAY(1);
1149f7788e8eSJonathan Lemon 
1150f7788e8eSJonathan Lemon 	return (data);
1151ba8c6fd5SDavid Greenman }
1152ba8c6fd5SDavid Greenman 
115300c4116bSJonathan Lemon static void
115474d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
115500c4116bSJonathan Lemon {
115600c4116bSJonathan Lemon 	int i;
115700c4116bSJonathan Lemon 
115800c4116bSJonathan Lemon 	/*
115900c4116bSJonathan Lemon 	 * Erase/write enable.
116000c4116bSJonathan Lemon 	 */
116100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
116200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
116300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
116400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
116500c4116bSJonathan Lemon 	DELAY(1);
116600c4116bSJonathan Lemon 	/*
116700c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
116800c4116bSJonathan Lemon 	 */
116900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
117000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
117100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
117200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
117300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
117400c4116bSJonathan Lemon 	DELAY(1);
117500c4116bSJonathan Lemon 	/*
117600c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
117700c4116bSJonathan Lemon 	 */
117800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
117900c4116bSJonathan Lemon 	DELAY(1);
118000c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
118100c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
118200c4116bSJonathan Lemon 			break;
118300c4116bSJonathan Lemon 		DELAY(50);
118400c4116bSJonathan Lemon 	}
118500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
118600c4116bSJonathan Lemon 	DELAY(1);
118700c4116bSJonathan Lemon 	/*
118800c4116bSJonathan Lemon 	 * Erase/write disable.
118900c4116bSJonathan Lemon 	 */
119000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
119100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
119200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
119300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
119400c4116bSJonathan Lemon 	DELAY(1);
119500c4116bSJonathan Lemon }
119600c4116bSJonathan Lemon 
1197ba8c6fd5SDavid Greenman /*
1198e9bf2fa7SDavid Greenman  * From NetBSD:
1199e9bf2fa7SDavid Greenman  *
1200e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1201e9bf2fa7SDavid Greenman  *
1202e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1203e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1204e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1205e9bf2fa7SDavid Greenman  *
1206e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1207e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1208e9bf2fa7SDavid Greenman  *
1209e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1210e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1211e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1212e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1213e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1214e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1215e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1216e9bf2fa7SDavid Greenman  */
1217e9bf2fa7SDavid Greenman static void
1218f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1219e9bf2fa7SDavid Greenman {
1220e9bf2fa7SDavid Greenman 
1221f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1222f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1223f7788e8eSJonathan Lemon 
1224f7788e8eSJonathan Lemon 	/* autosize */
1225f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1226e9bf2fa7SDavid Greenman }
1227f7788e8eSJonathan Lemon 
1228ba8c6fd5SDavid Greenman static void
1229f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1230ba8c6fd5SDavid Greenman {
1231f7788e8eSJonathan Lemon 	int i;
1232ba8c6fd5SDavid Greenman 
1233f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1234f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1235ba8c6fd5SDavid Greenman }
1236ba8c6fd5SDavid Greenman 
123700c4116bSJonathan Lemon static void
123800c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
123900c4116bSJonathan Lemon {
124000c4116bSJonathan Lemon 	int i;
124100c4116bSJonathan Lemon 
124200c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
124300c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
124400c4116bSJonathan Lemon }
124500c4116bSJonathan Lemon 
1246a17c678eSDavid Greenman /*
12474953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1248a17c678eSDavid Greenman  */
1249a17c678eSDavid Greenman static void
1250f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1251a17c678eSDavid Greenman {
12529b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
12534953bccaSNate Lawson 
12544953bccaSNate Lawson 	FXP_LOCK(sc);
12554953bccaSNate Lawson 	fxp_start_body(ifp);
12564953bccaSNate Lawson 	FXP_UNLOCK(sc);
12574953bccaSNate Lawson }
12584953bccaSNate Lawson 
12594953bccaSNate Lawson /*
12604953bccaSNate Lawson  * Start packet transmission on the interface.
12614953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
12624953bccaSNate Lawson  * internal entry point only.
12634953bccaSNate Lawson  */
12644953bccaSNate Lawson static void
12654953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
12664953bccaSNate Lawson {
12674953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1268b2badf02SMaxime Henrion 	struct mbuf *mb_head;
12694e53f837SPyun YongHyeon 	int txqueued;
1270a17c678eSDavid Greenman 
127167fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
127240c20505SMaxime Henrion 
1273a17c678eSDavid Greenman 	/*
1274483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1275483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1276483b9871SDavid Greenman 	 * of the command chain).
1277a17c678eSDavid Greenman 	 */
127840c20505SMaxime Henrion 	if (sc->need_mcsetup)
1279a17c678eSDavid Greenman 		return;
1280483b9871SDavid Greenman 
12814e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
12824e53f837SPyun YongHyeon 		fxp_txeof(sc);
1283483b9871SDavid Greenman 	/*
1284483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1285483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
12863114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
12873114fdb4SDavid Greenman 	 *       a NOP command when needed.
1288483b9871SDavid Greenman 	 */
128940c20505SMaxime Henrion 	txqueued = 0;
12907929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
12917929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1292483b9871SDavid Greenman 
1293dfe61cf1SDavid Greenman 		/*
1294dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1295dfe61cf1SDavid Greenman 		 */
12967929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
12977929aa03SMax Laier 		if (mb_head == NULL)
12987929aa03SMax Laier 			break;
1299a17c678eSDavid Greenman 
13004e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13014e53f837SPyun YongHyeon 			if (mb_head == NULL)
130240c20505SMaxime Henrion 				break;
13034e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
13044e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
130540c20505SMaxime Henrion 		}
13064e53f837SPyun YongHyeon 		txqueued++;
13074e53f837SPyun YongHyeon 		/*
13084e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13094e53f837SPyun YongHyeon 		 */
13104e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
13114e53f837SPyun YongHyeon 	}
131240c20505SMaxime Henrion 
131340c20505SMaxime Henrion 	/*
131440c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
131540c20505SMaxime Henrion 	 * going again if suspended.
131640c20505SMaxime Henrion 	 */
13174e53f837SPyun YongHyeon 	if (txqueued > 0) {
13184e53f837SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
131940c20505SMaxime Henrion 		fxp_scb_wait(sc);
132040c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13214e53f837SPyun YongHyeon 		/*
13224e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
13234e53f837SPyun YongHyeon 		 * from the card again.
13244e53f837SPyun YongHyeon 		 */
13254e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
132640c20505SMaxime Henrion 	}
132740c20505SMaxime Henrion }
132840c20505SMaxime Henrion 
132940c20505SMaxime Henrion static int
13304e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
133140c20505SMaxime Henrion {
133240c20505SMaxime Henrion 	struct ifnet *ifp;
133340c20505SMaxime Henrion 	struct mbuf *m;
133440c20505SMaxime Henrion 	struct fxp_tx *txp;
133540c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1336c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
133740c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1338c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
133940c20505SMaxime Henrion 
134040c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1341fc74a9f9SBrooks Davis 	ifp = sc->ifp;
134240c20505SMaxime Henrion 
1343c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1344c21e84e4SPyun YongHyeon 	tcp = NULL;
1345dfe61cf1SDavid Greenman 	/*
1346483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1347dfe61cf1SDavid Greenman 	 */
1348b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1349c8bca6dcSBill Paul 
1350c8bca6dcSBill Paul 	/*
1351a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1352a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1353a35e7eaaSDon Lewis 	 * Developer Manual says:
1354a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1355a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1356a35e7eaaSDon Lewis 	 *   ...
1357a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1358a35e7eaaSDon Lewis 	 *   be used.
1359a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1360a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1361a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1362a35e7eaaSDon Lewis 	 */
1363a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1364a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1365a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1366a35e7eaaSDon Lewis 
13674e53f837SPyun YongHyeon 	m = *m_head;
1368a35e7eaaSDon Lewis 	/*
1369c8bca6dcSBill Paul 	 * Deal with TCP/IP checksum offload. Note that
1370c8bca6dcSBill Paul 	 * in order for TCP checksum offload to work,
1371c8bca6dcSBill Paul 	 * the pseudo header checksum must have already
1372c8bca6dcSBill Paul 	 * been computed and stored in the checksum field
1373c8bca6dcSBill Paul 	 * in the TCP header. The stack should have
1374c8bca6dcSBill Paul 	 * already done this for us.
1375c8bca6dcSBill Paul 	 */
1376c583cc04SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
1377c583cc04SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
13784e53f837SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
1379c583cc04SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
138040c20505SMaxime Henrion 
1381c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
1382c8bca6dcSBill Paul 		/*
1383c8bca6dcSBill Paul 		 * XXX The 82550 chip appears to have trouble
1384c8bca6dcSBill Paul 		 * dealing with IP header checksums in very small
1385c8bca6dcSBill Paul 		 * datagrams, namely fragments from 1 to 3 bytes
1386c8bca6dcSBill Paul 		 * in size. For example, say you want to transmit
1387c8bca6dcSBill Paul 		 * a UDP packet of 1473 bytes. The packet will be
1388c8bca6dcSBill Paul 		 * fragmented over two IP datagrams, the latter
1389c8bca6dcSBill Paul 		 * containing only one byte of data. The 82550 will
1390c8bca6dcSBill Paul 		 * botch the header checksum on the 1-byte fragment.
1391c8bca6dcSBill Paul 		 * As long as the datagram contains 4 or more bytes
1392c8bca6dcSBill Paul 		 * of data, you're ok.
1393c8bca6dcSBill Paul 		 *
1394c8bca6dcSBill Paul                  * The following code attempts to work around this
1395c8bca6dcSBill Paul 		 * problem: if the datagram is less than 38 bytes
1396c8bca6dcSBill Paul 		 * in size (14 bytes ether header, 20 bytes IP header,
1397c8bca6dcSBill Paul 		 * plus 4 bytes of data), we punt and compute the IP
1398c8bca6dcSBill Paul 		 * header checksum by hand. This workaround doesn't
1399c8bca6dcSBill Paul 		 * work very well, however, since it can be fooled
1400c8bca6dcSBill Paul 		 * by things like VLAN tags and IP options that make
1401c8bca6dcSBill Paul 		 * the header sizes/offsets vary.
1402c8bca6dcSBill Paul 		 */
1403c8bca6dcSBill Paul 
14044e53f837SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
14054e53f837SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
1406c8bca6dcSBill Paul 				struct ip *ip;
14074e53f837SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
14084e53f837SPyun YongHyeon 				ip = mtod(m, struct ip *);
14094e53f837SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
14104e53f837SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
1411c583cc04SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
1412c8bca6dcSBill Paul 			} else {
1413b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_activation_high =
1414c8bca6dcSBill Paul 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
1415b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_schedule |=
1416c8bca6dcSBill Paul 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
1417c8bca6dcSBill Paul 			}
1418c8bca6dcSBill Paul 		}
1419c8bca6dcSBill Paul #endif
1420c8bca6dcSBill Paul 	}
1421c8bca6dcSBill Paul 
1422c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1423c21e84e4SPyun YongHyeon 		/*
1424c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1425c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1426c21e84e4SPyun YongHyeon 		 */
1427c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1428c21e84e4SPyun YongHyeon 		struct ip *ip;
1429c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1430c21e84e4SPyun YongHyeon 
1431c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1432c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1433c21e84e4SPyun YongHyeon 			m = m_dup(*m_head, M_DONTWAIT);
1434c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1435c21e84e4SPyun YongHyeon 			if (m == NULL) {
1436c21e84e4SPyun YongHyeon 				*m_head = NULL;
1437c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1438c21e84e4SPyun YongHyeon 			}
1439c21e84e4SPyun YongHyeon 			*m_head = m;
1440c21e84e4SPyun YongHyeon 		}
1441c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1442c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1443c21e84e4SPyun YongHyeon 		if (m == NULL) {
1444c21e84e4SPyun YongHyeon 			*m_head = NULL;
1445c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1446c21e84e4SPyun YongHyeon 		}
1447c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1448c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1449c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1450c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1451c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1452c21e84e4SPyun YongHyeon 			if (m == NULL) {
1453c21e84e4SPyun YongHyeon 				*m_head = NULL;
1454c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1455c21e84e4SPyun YongHyeon 			}
1456c21e84e4SPyun YongHyeon 		}
1457c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1458c21e84e4SPyun YongHyeon 		if (m == NULL) {
1459c21e84e4SPyun YongHyeon 			*m_head = NULL;
1460c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1461c21e84e4SPyun YongHyeon 		}
1462c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1463c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1464c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1465c21e84e4SPyun YongHyeon 		if (m == NULL) {
1466c21e84e4SPyun YongHyeon 			*m_head = NULL;
1467c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1468c21e84e4SPyun YongHyeon 		}
1469c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1470c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr) + tcp->th_off);
1471c21e84e4SPyun YongHyeon 		if (m == NULL) {
1472c21e84e4SPyun YongHyeon 			*m_head = NULL;
1473c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1474c21e84e4SPyun YongHyeon 		}
1475c21e84e4SPyun YongHyeon 
1476c21e84e4SPyun YongHyeon 		/*
1477c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1478c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1479c21e84e4SPyun YongHyeon 		 */
1480c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
1481c21e84e4SPyun YongHyeon 		ip->ip_len = htons(ifp->if_mtu);
1482c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1483c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1484c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1485c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1486c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1487c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1488c21e84e4SPyun YongHyeon 		*m_head = m;
1489c21e84e4SPyun YongHyeon 	}
1490c21e84e4SPyun YongHyeon 
14914e53f837SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_mtag, txp->tx_map, *m_head,
14924e53f837SPyun YongHyeon 	    segs, &nseg, 0);
14934e53f837SPyun YongHyeon 	if (error == EFBIG) {
14944e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
14954e53f837SPyun YongHyeon 		if (m == NULL) {
14964e53f837SPyun YongHyeon 			m_freem(*m_head);
14974e53f837SPyun YongHyeon 			*m_head = NULL;
14984e53f837SPyun YongHyeon 			return (ENOMEM);
14991104779bSMike Silbersack 		}
15004e53f837SPyun YongHyeon 		*m_head = m;
150140c20505SMaxime Henrion 		error = bus_dmamap_load_mbuf_sg(sc->fxp_mtag, txp->tx_map,
15024e53f837SPyun YongHyeon 	    	    *m_head, segs, &nseg, 0);
15034e53f837SPyun YongHyeon 		if (error != 0) {
15044e53f837SPyun YongHyeon 			m_freem(*m_head);
15054e53f837SPyun YongHyeon 			*m_head = NULL;
15064e53f837SPyun YongHyeon 			return (ENOMEM);
15074e53f837SPyun YongHyeon 		}
15084e53f837SPyun YongHyeon 	} else if (error != 0)
15094e53f837SPyun YongHyeon 		return (error);
15104e53f837SPyun YongHyeon 	if (nseg == 0) {
15114e53f837SPyun YongHyeon 		m_freem(*m_head);
15124e53f837SPyun YongHyeon 		*m_head = NULL;
15134e53f837SPyun YongHyeon 		return (EIO);
151423a0ed7cSDavid Greenman 	}
151523a0ed7cSDavid Greenman 
151640c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
15174e53f837SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1518b2badf02SMaxime Henrion 
151940c20505SMaxime Henrion 	cbp = txp->tx_cb;
152040c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
152140c20505SMaxime Henrion 		/*
152240c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
152340c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
152440c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
152540c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
152640c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
152740c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
152840c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
152940c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
153040c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
153140c20505SMaxime Henrion 		 * starting from a different place depending on whether
153240c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
153340c20505SMaxime Henrion 		 */
153440c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
1535c21e84e4SPyun YongHyeon 			cbp->tbd[i + 2].tb_addr = htole32(segs[i].ds_addr);
1536c21e84e4SPyun YongHyeon 			cbp->tbd[i + 2].tb_size = htole32(segs[i].ds_len);
153740c20505SMaxime Henrion 		} else {
153840c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
153940c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
154040c20505SMaxime Henrion 		}
154140c20505SMaxime Henrion 	}
1542c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1543c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1544c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
1545c21e84e4SPyun YongHyeon 		cbp->tbd[nseg + 1].tb_size |= htole32(0x8000);
1546c21e84e4SPyun YongHyeon 	} else
154740c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1548c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1549c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1550c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
1551c21e84e4SPyun YongHyeon 		cbp->tbd[1].tb_size = htole32(tcp_payload << 16);
1552c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1553c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1554c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1555c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1556c21e84e4SPyun YongHyeon 	}
155740c20505SMaxime Henrion 
15584e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1559b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1560b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
15614e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1562b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
156383e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
156483e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
15654e53f837SPyun YongHyeon 	else
1566b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
156783e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
156883e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1569c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1570b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1571a17c678eSDavid Greenman 
1572a17c678eSDavid Greenman 	/*
1573483b9871SDavid Greenman 	 * Advance the end of list forward.
1574a17c678eSDavid Greenman 	 */
157506175228SAndrew Gallatin 
157650d81222SMaxime Henrion #ifdef __alpha__
157706175228SAndrew Gallatin 	/*
157806175228SAndrew Gallatin 	 * On platforms which can't access memory in 16-bit
157906175228SAndrew Gallatin 	 * granularities, we must prevent the card from DMA'ing
158006175228SAndrew Gallatin 	 * up the status while we update the command field.
158106175228SAndrew Gallatin 	 * This could cause us to overwrite the completion status.
158214fd1071SMaxime Henrion 	 * XXX This is probably bogus and we're _not_ looking
158314fd1071SMaxime Henrion 	 * for atomicity here.
158406175228SAndrew Gallatin 	 */
158514fd1071SMaxime Henrion 	atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1586bafb64afSMaxime Henrion 	    htole16(FXP_CB_COMMAND_S));
158750d81222SMaxime Henrion #else
158840c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
158950d81222SMaxime Henrion #endif /*__alpha__*/
1590b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1591a17c678eSDavid Greenman 
1592a17c678eSDavid Greenman 	/*
15931cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1594b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1595483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1596a17c678eSDavid Greenman 	 */
15971cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1598b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1599a17c678eSDavid Greenman 
16001cd443acSDavid Greenman 	sc->tx_queued++;
16011cd443acSDavid Greenman 
160240c20505SMaxime Henrion 	return (0);
1603a17c678eSDavid Greenman }
1604a17c678eSDavid Greenman 
1605e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1606e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1607e4fc250cSLuigi Rizzo 
1608e4fc250cSLuigi Rizzo static void
1609e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1610e4fc250cSLuigi Rizzo {
1611e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
161274d1ed23SMaxime Henrion 	uint8_t statack;
1613e4fc250cSLuigi Rizzo 
16144953bccaSNate Lawson 	FXP_LOCK(sc);
161540929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
16164953bccaSNate Lawson 		FXP_UNLOCK(sc);
1617e4fc250cSLuigi Rizzo 		return;
1618e4fc250cSLuigi Rizzo 	}
161940929967SGleb Smirnoff 
1620e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1621e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1622e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
162374d1ed23SMaxime Henrion 		uint8_t tmp;
16246481f301SPeter Wemm 
1625e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16264953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16274953bccaSNate Lawson 			FXP_UNLOCK(sc);
1628e4fc250cSLuigi Rizzo 			return; /* nothing to do */
16294953bccaSNate Lawson 		}
1630e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1631e4fc250cSLuigi Rizzo 		/* ack what we can */
1632e4fc250cSLuigi Rizzo 		if (tmp != 0)
1633e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1634e4fc250cSLuigi Rizzo 		statack |= tmp;
1635e4fc250cSLuigi Rizzo 	}
16364953bccaSNate Lawson 	fxp_intr_body(sc, ifp, statack, count);
16374953bccaSNate Lawson 	FXP_UNLOCK(sc);
1638e4fc250cSLuigi Rizzo }
1639e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1640e4fc250cSLuigi Rizzo 
1641a17c678eSDavid Greenman /*
16429c7d2607SDavid Greenman  * Process interface interrupts.
1643a17c678eSDavid Greenman  */
164494927790SDavid Greenman static void
1645f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1646a17c678eSDavid Greenman {
1647f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1648fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
164974d1ed23SMaxime Henrion 	uint8_t statack;
16500f4dc94cSChuck Paterson 
16514953bccaSNate Lawson 	FXP_LOCK(sc);
1652704d1965SWarner Losh 	if (sc->suspended) {
1653704d1965SWarner Losh 		FXP_UNLOCK(sc);
1654704d1965SWarner Losh 		return;
1655704d1965SWarner Losh 	}
1656704d1965SWarner Losh 
1657e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
165840929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
16594953bccaSNate Lawson 		FXP_UNLOCK(sc);
1660e4fc250cSLuigi Rizzo 		return;
16614953bccaSNate Lawson 	}
1662e4fc250cSLuigi Rizzo #endif
1663b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1664a17c678eSDavid Greenman 		/*
166511457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
166611457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
166711457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
166811457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
166911457bbfSJonathan Lemon 		 */
16704953bccaSNate Lawson 		if (statack == 0xff) {
16714953bccaSNate Lawson 			FXP_UNLOCK(sc);
167211457bbfSJonathan Lemon 			return;
16734953bccaSNate Lawson 		}
167411457bbfSJonathan Lemon 
167511457bbfSJonathan Lemon 		/*
1676a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1677a17c678eSDavid Greenman 		 */
1678ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
16794953bccaSNate Lawson 		fxp_intr_body(sc, ifp, statack, -1);
1680e4fc250cSLuigi Rizzo 	}
16814953bccaSNate Lawson 	FXP_UNLOCK(sc);
1682e4fc250cSLuigi Rizzo }
1683e4fc250cSLuigi Rizzo 
1684e4fc250cSLuigi Rizzo static void
1685b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1686b2badf02SMaxime Henrion {
16874e53f837SPyun YongHyeon 	struct ifnet *ifp;
1688b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1689b2badf02SMaxime Henrion 
16904e53f837SPyun YongHyeon 	ifp = sc->ifp;
1691b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1692b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
169383e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1694b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1695b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1696b2badf02SMaxime Henrion 			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1697b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1698b2badf02SMaxime Henrion 			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1699b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1700b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1701b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1702b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1703b2badf02SMaxime Henrion 		}
1704b2badf02SMaxime Henrion 		sc->tx_queued--;
17054e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1706b2badf02SMaxime Henrion 	}
1707b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1708b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
170925935344SPyun YongHyeon 	if (sc->tx_queued == 0) {
171025935344SPyun YongHyeon 		sc->watchdog_timer = 0;
171125935344SPyun YongHyeon 		if (sc->need_mcsetup)
171225935344SPyun YongHyeon 			fxp_mc_setup(sc);
171325935344SPyun YongHyeon 	}
1714b2badf02SMaxime Henrion }
1715b2badf02SMaxime Henrion 
1716b2badf02SMaxime Henrion static void
1717f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m,
1718f13075afSPyun YongHyeon     uint16_t status, int pos)
1719f13075afSPyun YongHyeon {
1720f13075afSPyun YongHyeon 	struct ether_header *eh;
1721f13075afSPyun YongHyeon 	struct ip *ip;
1722f13075afSPyun YongHyeon 	struct udphdr *uh;
1723f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1724f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1725f13075afSPyun YongHyeon 
1726f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1727f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1728f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1729f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1730f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1731f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1732f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1733f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1734f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1735f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1736f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1737f13075afSPyun YongHyeon 			}
1738f13075afSPyun YongHyeon 		}
1739f13075afSPyun YongHyeon 		return;
1740f13075afSPyun YongHyeon 	}
1741f13075afSPyun YongHyeon 
1742f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1743f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1744f13075afSPyun YongHyeon 		return;
1745f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1746f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1747f13075afSPyun YongHyeon 		return;
1748f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1749f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1750f13075afSPyun YongHyeon 		return;
1751f13075afSPyun YongHyeon 
1752f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1753f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1754f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1755f13075afSPyun YongHyeon 		return;
1756f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1757f13075afSPyun YongHyeon 		return;
1758f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1759f13075afSPyun YongHyeon 		return;
1760f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1761f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1762f13075afSPyun YongHyeon 
1763f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1764f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1765f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1766f13075afSPyun YongHyeon 			return;
1767f13075afSPyun YongHyeon 		break;
1768f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1769f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1770f13075afSPyun YongHyeon 			return;
1771f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1772f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1773f13075afSPyun YongHyeon 			return; /* no checksum */
1774f13075afSPyun YongHyeon 		break;
1775f13075afSPyun YongHyeon 	default:
1776f13075afSPyun YongHyeon 		return;
1777f13075afSPyun YongHyeon 	}
1778f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1779f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1780f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1781f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1782f13075afSPyun YongHyeon 	if (len > 0) {
1783f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1784f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1785f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1786f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1787f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1788f13075afSPyun YongHyeon 		}
1789f13075afSPyun YongHyeon 	}
1790f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1791f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1792f13075afSPyun YongHyeon }
1793f13075afSPyun YongHyeon 
1794f13075afSPyun YongHyeon static void
179574d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
17964953bccaSNate Lawson     int count)
1797e4fc250cSLuigi Rizzo {
17982b5989e9SLuigi Rizzo 	struct mbuf *m;
1799b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18002b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18012b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
180260bb79ebSPyun YongHyeon 	uint16_t status;
18032b5989e9SLuigi Rizzo 
180467fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18052b5989e9SLuigi Rizzo 	if (rnr)
18060f1db1d6SMaxime Henrion 		sc->rnr++;
1807947e3815SIan Dowse #ifdef DEVICE_POLLING
1808947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1809947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1810947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1811947e3815SIan Dowse 		rnr = 1;
1812947e3815SIan Dowse 	}
1813947e3815SIan Dowse #endif
1814a17c678eSDavid Greenman 
1815a17c678eSDavid Greenman 	/*
18163114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
181706936301SBill Paul 	 *
181806936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
181906936301SBill Paul 	 * be that this event (control unit not ready) was not
182006936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
182106936301SBill Paul 	 * The exact sequence of events that occur when the interface
182206936301SBill Paul 	 * is brought up are different now, and if this event
182306936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
182406936301SBill Paul 	 * can stall for several seconds. The result is that no
182506936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
182606936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
18273114fdb4SDavid Greenman 	 */
18284e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1829b2badf02SMaxime Henrion 		fxp_txeof(sc);
18303114fdb4SDavid Greenman 
18313114fdb4SDavid Greenman 	/*
18323114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
18333114fdb4SDavid Greenman 	 */
18347929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
18354953bccaSNate Lawson 		fxp_start_body(ifp);
18362b5989e9SLuigi Rizzo 
18372b5989e9SLuigi Rizzo 	/*
18382b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
18392b5989e9SLuigi Rizzo 	 */
1840947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
18412b5989e9SLuigi Rizzo 		return;
18422b5989e9SLuigi Rizzo 
18433114fdb4SDavid Greenman 	/*
1844a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1845a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1846a17c678eSDavid Greenman 	 * re-start the receiver.
1847947e3815SIan Dowse 	 *
18482b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
18492b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
18502b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
18512b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1852947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1853947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1854a17c678eSDavid Greenman 	 */
18552b5989e9SLuigi Rizzo 	for (;;) {
1856b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1857b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1858ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1859ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1860b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1861b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1862a17c678eSDavid Greenman 
1863e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1864947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1865947e3815SIan Dowse 			if (rnr) {
1866947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1867947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1868947e3815SIan Dowse 				rnr = 0;
1869947e3815SIan Dowse 			}
18702b5989e9SLuigi Rizzo 			break;
1871947e3815SIan Dowse 		}
18722b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
18732b5989e9SLuigi Rizzo 
187460bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
187560bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
18762b5989e9SLuigi Rizzo 			break;
18772b5989e9SLuigi Rizzo 
1878dfe61cf1SDavid Greenman 		/*
1879b2badf02SMaxime Henrion 		 * Advance head forward.
1880dfe61cf1SDavid Greenman 		 */
1881b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1882a17c678eSDavid Greenman 
1883dfe61cf1SDavid Greenman 		/*
1884ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1885ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1886ba8c6fd5SDavid Greenman 		 * instead.
1887dfe61cf1SDavid Greenman 		 */
188885050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1889aed53495SDavid Greenman 			int total_len;
1890a17c678eSDavid Greenman 
1891e8c8b728SJonathan Lemon 			/*
18922b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
18932b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
18942b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
18952b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1896e8c8b728SJonathan Lemon 			 */
1897bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1898f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
1899f13075afSPyun YongHyeon 			    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1900f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1901f13075afSPyun YongHyeon 				total_len -= 2;
1902f13075afSPyun YongHyeon 			}
19032b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
19042b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
190560bb79ebSPyun YongHyeon 				sc->rfa_size || status & FXP_RFA_STATUS_CRC) {
1906e8c8b728SJonathan Lemon 				m_freem(m);
19072b5989e9SLuigi Rizzo 				continue;
1908e8c8b728SJonathan Lemon 			}
1909920b58e8SBrooks Davis 
19102e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1911673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1912673d9191SSam Leffler 
1913f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
1914f13075afSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1915f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
191605fb8c3fSNate Lawson 			/*
191705fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
191805fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
191905fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
192005fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
192105fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
192205fb8c3fSNate Lawson 			 * calling if_input() on each one.
192305fb8c3fSNate Lawson 			 */
192405fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
1925673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
192605fb8c3fSNate Lawson 			FXP_LOCK(sc);
192785050421SPyun YongHyeon 		} else {
192885050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
192985050421SPyun YongHyeon 			ifp->if_iqdrops++;
193085050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
1931a17c678eSDavid Greenman 		}
193285050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
1933a17c678eSDavid Greenman 	}
19342b5989e9SLuigi Rizzo 	if (rnr) {
1935ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1936ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1937b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
19382e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1939a17c678eSDavid Greenman 	}
1940a17c678eSDavid Greenman }
1941a17c678eSDavid Greenman 
1942dfe61cf1SDavid Greenman /*
1943dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1944dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1945dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1946dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1947dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1948dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1949dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1950dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1951dfe61cf1SDavid Greenman  * them again next time.
1952dfe61cf1SDavid Greenman  */
1953303b270bSEivind Eklund static void
1954f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1955a17c678eSDavid Greenman {
1956f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1957fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
1958a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1959a17c678eSDavid Greenman 
19603212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1961b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
196283e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
196383e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1964397f9dfeSDavid Greenman 	if (sp->rx_good) {
196583e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
1966397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1967397f9dfeSDavid Greenman 	} else {
1968c8cc6fcaSDavid Greenman 		/*
1969c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1970c8cc6fcaSDavid Greenman 		 */
1971397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1972397f9dfeSDavid Greenman 	}
19733ba65732SDavid Greenman 	ifp->if_ierrors +=
197483e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
197583e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
197683e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
197783e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
1978a17c678eSDavid Greenman 	/*
1979f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1980f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1981f9be9005SDavid Greenman 	 */
1982f9be9005SDavid Greenman 	if (sp->tx_underruns) {
198383e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
1984f9be9005SDavid Greenman 		if (tx_threshold < 192)
1985f9be9005SDavid Greenman 			tx_threshold += 64;
1986f9be9005SDavid Greenman 	}
19874953bccaSNate Lawson 
1988397f9dfeSDavid Greenman 	/*
1989c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1990c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1991c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1992c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1993c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1994c8cc6fcaSDavid Greenman 	 */
1995b2badf02SMaxime Henrion 	fxp_txeof(sc);
1996b2badf02SMaxime Henrion 
1997c8cc6fcaSDavid Greenman 	/*
1998397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1999397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2000397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2001397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2002397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
2003397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2004397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2005397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2006397f9dfeSDavid Greenman 	 */
2007397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2008397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
2009397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
2010397f9dfeSDavid Greenman 	}
2011f9be9005SDavid Greenman 	/*
20123ba65732SDavid Greenman 	 * If there is no pending command, start another stats
20133ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2014a17c678eSDavid Greenman 	 */
2015397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2016a17c678eSDavid Greenman 		/*
2017397f9dfeSDavid Greenman 		 * Start another stats dump.
2018a17c678eSDavid Greenman 		 */
2019b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
2020b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
20212e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2022dfe61cf1SDavid Greenman 	} else {
2023dfe61cf1SDavid Greenman 		/*
2024dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
2025dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
20263ba65732SDavid Greenman 		 * next timer event to update them.
2027dfe61cf1SDavid Greenman 		 */
2028dfe61cf1SDavid Greenman 		sp->tx_good = 0;
2029f9be9005SDavid Greenman 		sp->tx_underruns = 0;
2030dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
20313ba65732SDavid Greenman 
2032dfe61cf1SDavid Greenman 		sp->rx_good = 0;
20333ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
20343ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
20353ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
20363ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
2037dfe61cf1SDavid Greenman 	}
2038f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2039f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
20404953bccaSNate Lawson 
2041a17c678eSDavid Greenman 	/*
204216f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2043df79d527SGleb Smirnoff 	 */
2044df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2045df79d527SGleb Smirnoff 
2046df79d527SGleb Smirnoff 	/*
2047a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2048a17c678eSDavid Greenman 	 */
204945276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2050a17c678eSDavid Greenman }
2051a17c678eSDavid Greenman 
2052a17c678eSDavid Greenman /*
2053a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2054a17c678eSDavid Greenman  * the interface.
2055a17c678eSDavid Greenman  */
2056a17c678eSDavid Greenman static void
2057f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2058a17c678eSDavid Greenman {
2059fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2060b2badf02SMaxime Henrion 	struct fxp_tx *txp;
20613ba65732SDavid Greenman 	int i;
2062a17c678eSDavid Greenman 
206313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2064df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
20657dced78aSDavid Greenman 
2066a17c678eSDavid Greenman 	/*
2067a17c678eSDavid Greenman 	 * Cancel stats updater.
2068a17c678eSDavid Greenman 	 */
206945276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
20703ba65732SDavid Greenman 
20713ba65732SDavid Greenman 	/*
20727137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
20737137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
20743ba65732SDavid Greenman 	 */
20757137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
207672a32a26SJonathan Lemon 	DELAY(50);
20777137cea0SPyun YongHyeon 	/* Disable interrupts. */
20787137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2079a17c678eSDavid Greenman 
20803ba65732SDavid Greenman 	/*
20813ba65732SDavid Greenman 	 * Release any xmit buffers.
20823ba65732SDavid Greenman 	 */
2083b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2084da91462dSDavid Greenman 	if (txp != NULL) {
2085da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
2086b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
2087b2badf02SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
2088b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
2089b2badf02SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
2090b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
2091b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
2092c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
2093b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
2094da91462dSDavid Greenman 			}
2095da91462dSDavid Greenman 		}
20963ba65732SDavid Greenman 	}
2097b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
20983ba65732SDavid Greenman 	sc->tx_queued = 0;
2099a17c678eSDavid Greenman }
2100a17c678eSDavid Greenman 
2101a17c678eSDavid Greenman /*
2102a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2103a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2104a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2105a17c678eSDavid Greenman  * card has wedged for some reason.
2106a17c678eSDavid Greenman  */
2107a17c678eSDavid Greenman static void
2108df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2109a17c678eSDavid Greenman {
2110ba8c6fd5SDavid Greenman 
2111df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2112df79d527SGleb Smirnoff 
2113df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2114df79d527SGleb Smirnoff 		return;
2115df79d527SGleb Smirnoff 
2116f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2117df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
2118a17c678eSDavid Greenman 
21194953bccaSNate Lawson 	fxp_init_body(sc);
2120a17c678eSDavid Greenman }
2121a17c678eSDavid Greenman 
21224953bccaSNate Lawson /*
21234953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
21244953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
21254953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
21264953bccaSNate Lawson  */
2127a17c678eSDavid Greenman static void
2128f7788e8eSJonathan Lemon fxp_init(void *xsc)
2129a17c678eSDavid Greenman {
2130fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
21314953bccaSNate Lawson 
21324953bccaSNate Lawson 	FXP_LOCK(sc);
21334953bccaSNate Lawson 	fxp_init_body(sc);
21344953bccaSNate Lawson 	FXP_UNLOCK(sc);
21354953bccaSNate Lawson }
21364953bccaSNate Lawson 
21374953bccaSNate Lawson /*
21384953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
21394953bccaSNate Lawson  * softc lock held.
21404953bccaSNate Lawson  */
21414953bccaSNate Lawson static void
21424953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
21434953bccaSNate Lawson {
2144fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2145a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2146a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2147b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2148b2badf02SMaxime Henrion 	struct fxp_tx *txp;
214909882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
21503212724cSJohn Baldwin 	int i, prm;
2151a17c678eSDavid Greenman 
215267fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2153a17c678eSDavid Greenman 	/*
21543ba65732SDavid Greenman 	 * Cancel any pending I/O
2155a17c678eSDavid Greenman 	 */
21563ba65732SDavid Greenman 	fxp_stop(sc);
2157a17c678eSDavid Greenman 
21587137cea0SPyun YongHyeon 	/*
21597137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
21607137cea0SPyun YongHyeon 	 */
21617137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
21627137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
21637137cea0SPyun YongHyeon 	DELAY(50);
21647137cea0SPyun YongHyeon 
2165a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
2166a17c678eSDavid Greenman 
2167a17c678eSDavid Greenman 	/*
2168a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2169a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2170a17c678eSDavid Greenman 	 */
2171ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
21722e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2173a17c678eSDavid Greenman 
2174ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
21752e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2176a17c678eSDavid Greenman 
2177a17c678eSDavid Greenman 	/*
2178a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2179a17c678eSDavid Greenman 	 */
2180ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2181b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
2182b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
21832e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2184a17c678eSDavid Greenman 
2185a17c678eSDavid Greenman 	/*
218672a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
218772a32a26SJonathan Lemon 	 */
218872a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
218972a32a26SJonathan Lemon 		fxp_load_ucode(sc);
219072a32a26SJonathan Lemon 
219172a32a26SJonathan Lemon 	/*
219209882363SJonathan Lemon 	 * Initialize the multicast address list.
219309882363SJonathan Lemon 	 */
219409882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
219509882363SJonathan Lemon 		mcsp = sc->mcsp;
219609882363SJonathan Lemon 		mcsp->cb_status = 0;
219783e6547dSMaxime Henrion 		mcsp->cb_command =
219883e6547dSMaxime Henrion 		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
219983e6547dSMaxime Henrion 		mcsp->link_addr = 0xffffffff;
220009882363SJonathan Lemon 		/*
220109882363SJonathan Lemon 	 	 * Start the multicast setup command.
220209882363SJonathan Lemon 		 */
220309882363SJonathan Lemon 		fxp_scb_wait(sc);
2204b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2205b2badf02SMaxime Henrion 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
220609882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
220709882363SJonathan Lemon 		/* ...and wait for it to complete. */
2208209b07bcSMaxime Henrion 		fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
2209b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
2210b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
221109882363SJonathan Lemon 	}
221209882363SJonathan Lemon 
221309882363SJonathan Lemon 	/*
2214a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2215a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2216a17c678eSDavid Greenman 	 * later.
2217a17c678eSDavid Greenman 	 */
2218b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2219a17c678eSDavid Greenman 
2220a17c678eSDavid Greenman 	/*
2221a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2222a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2223a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2224a17c678eSDavid Greenman 	 */
2225b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2226a17c678eSDavid Greenman 
2227a17c678eSDavid Greenman 	cbp->cb_status =	0;
222883e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
222983e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
223083e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
22312c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2232001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2233001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2234a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2235f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2236f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2237f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2238f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2239001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2240001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2241f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2242a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2243f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2244f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
22453114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2246f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2247f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2248f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
22498ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2250a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2251f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2252f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2253c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2254c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2255f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2256f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2257f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
2258f13075afSPyun YongHyeon 	    (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0;
2259f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2260f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2261f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2262f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2263a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2264a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2265a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2266a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2267a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2268a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2269a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2270a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2271f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2272f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2273f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2274f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2275f7788e8eSJonathan Lemon 
2276a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2277a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2278a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2279f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2280f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
22817137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2282a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
22833ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2284a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2285f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
2286c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2287a17c678eSDavid Greenman 
22880f1db1d6SMaxime Henrion 	if (sc->tunable_noflow || sc->revision == FXP_REV_82557) {
22893bd07cfdSJonathan Lemon 		/*
22903bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
22913bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
22923bd07cfdSJonathan Lemon 		 */
22933bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
22943bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
22953bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
22963bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
22973bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
22983bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
22993bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
23003bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
23013bd07cfdSJonathan Lemon 	} else {
23023bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
23033bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
23043bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23053bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
23063bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
23073bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
23083bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
23093bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
23103bd07cfdSJonathan Lemon 	}
23113bd07cfdSJonathan Lemon 
2312a17c678eSDavid Greenman 	/*
2313a17c678eSDavid Greenman 	 * Start the config command/DMA.
2314a17c678eSDavid Greenman 	 */
2315ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2316b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2317b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
23182e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2319a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2320209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2321b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2322a17c678eSDavid Greenman 
2323a17c678eSDavid Greenman 	/*
2324a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2325a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2326a17c678eSDavid Greenman 	 */
2327b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2328a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
232983e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
233083e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
23314a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2332a17c678eSDavid Greenman 
2333a17c678eSDavid Greenman 	/*
2334a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2335a17c678eSDavid Greenman 	 */
2336ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2337b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
23382e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2339a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2340209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2341b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2342a17c678eSDavid Greenman 
2343a17c678eSDavid Greenman 	/*
2344a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2345a17c678eSDavid Greenman 	 */
2346b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2347b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2348b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2349a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2350b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
235183e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
235283e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
235383e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
235483e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
23553bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2356b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
235783e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
23583bd07cfdSJonathan Lemon 		else
2359b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
236083e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2361b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2362a17c678eSDavid Greenman 	}
2363a17c678eSDavid Greenman 	/*
2364397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2365a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2366a17c678eSDavid Greenman 	 */
236783e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2368b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2369b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2370397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2371a17c678eSDavid Greenman 
2372ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23732e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2374a17c678eSDavid Greenman 
2375a17c678eSDavid Greenman 	/*
2376a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2377a17c678eSDavid Greenman 	 */
2378ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2379b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
23802e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2381a17c678eSDavid Greenman 
2382dccee1a1SDavid Greenman 	/*
2383ba8c6fd5SDavid Greenman 	 * Set current media.
2384dccee1a1SDavid Greenman 	 */
2385f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2386f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2387dccee1a1SDavid Greenman 
238813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
238913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2390e8c8b728SJonathan Lemon 
2391e8c8b728SJonathan Lemon 	/*
2392e8c8b728SJonathan Lemon 	 * Enable interrupts.
2393e8c8b728SJonathan Lemon 	 */
23942b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
23952b5989e9SLuigi Rizzo 	/*
23962b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
23972b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
23982b5989e9SLuigi Rizzo 	 */
239940929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
24002b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
24012b5989e9SLuigi Rizzo 	else
24022b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2403e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2404a17c678eSDavid Greenman 
2405a17c678eSDavid Greenman 	/*
2406a17c678eSDavid Greenman 	 * Start stats updater.
2407a17c678eSDavid Greenman 	 */
240845276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2409f7788e8eSJonathan Lemon }
2410f7788e8eSJonathan Lemon 
2411f7788e8eSJonathan Lemon static int
2412f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2413f7788e8eSJonathan Lemon {
2414f7788e8eSJonathan Lemon 
2415f7788e8eSJonathan Lemon 	return (0);
2416a17c678eSDavid Greenman }
2417a17c678eSDavid Greenman 
2418303b270bSEivind Eklund static void
2419f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2420ba8c6fd5SDavid Greenman {
2421ba8c6fd5SDavid Greenman 
2422f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2423ba8c6fd5SDavid Greenman }
2424ba8c6fd5SDavid Greenman 
2425ba8c6fd5SDavid Greenman /*
2426ba8c6fd5SDavid Greenman  * Change media according to request.
2427ba8c6fd5SDavid Greenman  */
2428f7788e8eSJonathan Lemon static int
2429f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2430ba8c6fd5SDavid Greenman {
2431ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2432f7788e8eSJonathan Lemon 	struct mii_data *mii;
2433ba8c6fd5SDavid Greenman 
2434f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
24353212724cSJohn Baldwin 	FXP_LOCK(sc);
24365aa0cdf4SJohn-Mark Gurney 	if (mii->mii_instance) {
24375aa0cdf4SJohn-Mark Gurney 		struct mii_softc	*miisc;
24385aa0cdf4SJohn-Mark Gurney 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
24395aa0cdf4SJohn-Mark Gurney 			mii_phy_reset(miisc);
24405aa0cdf4SJohn-Mark Gurney 	}
2441f7788e8eSJonathan Lemon 	mii_mediachg(mii);
24423212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2443ba8c6fd5SDavid Greenman 	return (0);
2444ba8c6fd5SDavid Greenman }
2445ba8c6fd5SDavid Greenman 
2446ba8c6fd5SDavid Greenman /*
2447ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2448ba8c6fd5SDavid Greenman  */
2449f7788e8eSJonathan Lemon static void
2450f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2451ba8c6fd5SDavid Greenman {
2452ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2453f7788e8eSJonathan Lemon 	struct mii_data *mii;
2454ba8c6fd5SDavid Greenman 
2455f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
24563212724cSJohn Baldwin 	FXP_LOCK(sc);
2457f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2458f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2459f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
24602e2b8238SJonathan Lemon 
24612b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
24622b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
24632e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
24642e2b8238SJonathan Lemon 	else
24652e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
24663212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2467ba8c6fd5SDavid Greenman }
2468ba8c6fd5SDavid Greenman 
2469a17c678eSDavid Greenman /*
2470a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2471a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
247285050421SPyun YongHyeon  * reusing the RFA buffer.
2473a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2474a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2475a17c678eSDavid Greenman  */
2476a17c678eSDavid Greenman static int
247785050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2478a17c678eSDavid Greenman {
2479a17c678eSDavid Greenman 	struct mbuf *m;
248085050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2481b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
248285050421SPyun YongHyeon 	int error;
2483a17c678eSDavid Greenman 
2484a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
248585050421SPyun YongHyeon 	if (m == NULL)
248685050421SPyun YongHyeon 		return (ENOBUFS);
2487ba8c6fd5SDavid Greenman 
2488ba8c6fd5SDavid Greenman 	/*
2489ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2490ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2491ba8c6fd5SDavid Greenman 	 */
2492ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2493ba8c6fd5SDavid Greenman 
2494eadd5e3aSDavid Greenman 	/*
2495eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2496eadd5e3aSDavid Greenman 	 * data start past it.
2497eadd5e3aSDavid Greenman 	 */
2498a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2499c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
250083e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2501eadd5e3aSDavid Greenman 
2502a17c678eSDavid Greenman 	rfa->rfa_status = 0;
250383e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2504a17c678eSDavid Greenman 	rfa->actual_size = 0;
250585050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
250685050421SPyun YongHyeon 	    sc->rfa_size;
2507ba8c6fd5SDavid Greenman 
250828935f27SMaxime Henrion 	/*
250928935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
251028935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
251128935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
251228935f27SMaxime Henrion 	 * is also alignment-safe.
251328935f27SMaxime Henrion 	 */
251483e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
251583e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2516ba8c6fd5SDavid Greenman 
2517b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2518b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2519b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2520b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2521b2badf02SMaxime Henrion 	if (error) {
2522b2badf02SMaxime Henrion 		m_freem(m);
2523b2badf02SMaxime Henrion 		return (error);
2524b2badf02SMaxime Henrion 	}
2525b2badf02SMaxime Henrion 
2526b2badf02SMaxime Henrion 	bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2527b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2528b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2529b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2530b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2531b2badf02SMaxime Henrion 
2532b983c7b3SMaxime Henrion 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
2533b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
253485050421SPyun YongHyeon 	return (0);
253585050421SPyun YongHyeon }
253685050421SPyun YongHyeon 
253785050421SPyun YongHyeon static void
253885050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
253985050421SPyun YongHyeon {
254085050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
254185050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2542b2badf02SMaxime Henrion 
2543dfe61cf1SDavid Greenman 	/*
2544dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2545dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2546dfe61cf1SDavid Greenman 	 */
2547b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2548b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2549b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2550b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2551b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
255283e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2553aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2554b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
25554cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2556a17c678eSDavid Greenman 	} else {
2557b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2558b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2559a17c678eSDavid Greenman 	}
2560b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
256185050421SPyun YongHyeon }
256285050421SPyun YongHyeon 
256385050421SPyun YongHyeon static void
256485050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
256585050421SPyun YongHyeon {
256685050421SPyun YongHyeon 	struct mbuf *m;
256785050421SPyun YongHyeon 	struct fxp_rfa *rfa;
256885050421SPyun YongHyeon 
256985050421SPyun YongHyeon 	m = rxp->rx_mbuf;
257085050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
257185050421SPyun YongHyeon 	/*
257285050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
257385050421SPyun YongHyeon 	 * will be 32-bit aligned.
257485050421SPyun YongHyeon 	 */
257585050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
257685050421SPyun YongHyeon 
257785050421SPyun YongHyeon 	/*
257885050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
257985050421SPyun YongHyeon 	 * data start past it.
258085050421SPyun YongHyeon 	 */
258185050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
258285050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
258385050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
258485050421SPyun YongHyeon 
258585050421SPyun YongHyeon 	rfa->rfa_status = 0;
258685050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
258785050421SPyun YongHyeon 	rfa->actual_size = 0;
258885050421SPyun YongHyeon 
258985050421SPyun YongHyeon 	/*
259085050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
259185050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
259285050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
259385050421SPyun YongHyeon 	 * is also alignment-safe.
259485050421SPyun YongHyeon 	 */
259585050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
259685050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
259785050421SPyun YongHyeon 
259885050421SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
259985050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2600a17c678eSDavid Greenman }
2601a17c678eSDavid Greenman 
2602f1928b0cSKevin Lo static int
2603f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2604dccee1a1SDavid Greenman {
2605f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2606dccee1a1SDavid Greenman 	int count = 10000;
26076ebc3153SDavid Greenman 	int value;
2608dccee1a1SDavid Greenman 
2609ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2610ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2611dccee1a1SDavid Greenman 
2612ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2613ba8c6fd5SDavid Greenman 	    && count--)
26146ebc3153SDavid Greenman 		DELAY(10);
2615dccee1a1SDavid Greenman 
2616dccee1a1SDavid Greenman 	if (count <= 0)
2617f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2618dccee1a1SDavid Greenman 
26196ebc3153SDavid Greenman 	return (value & 0xffff);
2620dccee1a1SDavid Greenman }
2621dccee1a1SDavid Greenman 
2622dccee1a1SDavid Greenman static void
2623f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2624dccee1a1SDavid Greenman {
2625f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2626dccee1a1SDavid Greenman 	int count = 10000;
2627dccee1a1SDavid Greenman 
2628ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2629ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2630ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2631dccee1a1SDavid Greenman 
2632ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2633ba8c6fd5SDavid Greenman 	    count--)
26346ebc3153SDavid Greenman 		DELAY(10);
2635dccee1a1SDavid Greenman 
2636dccee1a1SDavid Greenman 	if (count <= 0)
2637f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
2638dccee1a1SDavid Greenman }
2639dccee1a1SDavid Greenman 
2640dccee1a1SDavid Greenman static int
2641f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2642a17c678eSDavid Greenman {
26439b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2644a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2645f7788e8eSJonathan Lemon 	struct mii_data *mii;
264660bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2647a17c678eSDavid Greenman 
2648a17c678eSDavid Greenman 	switch (command) {
2649a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
26503212724cSJohn Baldwin 		FXP_LOCK(sc);
2651f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2652f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2653f7788e8eSJonathan Lemon 		else
2654f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2655a17c678eSDavid Greenman 
2656a17c678eSDavid Greenman 		/*
2657a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2658a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2659a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2660a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2661a17c678eSDavid Greenman 		 */
2662a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
26634953bccaSNate Lawson 			fxp_init_body(sc);
2664a17c678eSDavid Greenman 		} else {
266513f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
26664a5f1499SDavid Greenman 				fxp_stop(sc);
2667a17c678eSDavid Greenman 		}
26683212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2669a17c678eSDavid Greenman 		break;
2670a17c678eSDavid Greenman 
2671a17c678eSDavid Greenman 	case SIOCADDMULTI:
2672a17c678eSDavid Greenman 	case SIOCDELMULTI:
26733212724cSJohn Baldwin 		FXP_LOCK(sc);
2674f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2675f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2676f7788e8eSJonathan Lemon 		else
2677f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2678a17c678eSDavid Greenman 		/*
2679a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2680a17c678eSDavid Greenman 		 * accordingly.
2681a17c678eSDavid Greenman 		 */
2682f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2683397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2684397f9dfeSDavid Greenman 		/*
2685f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2686397f9dfeSDavid Greenman 		 * again rather than else {}.
2687397f9dfeSDavid Greenman 		 */
2688f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
26894953bccaSNate Lawson 			fxp_init_body(sc);
26903212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2691a17c678eSDavid Greenman 		error = 0;
2692ba8c6fd5SDavid Greenman 		break;
2693ba8c6fd5SDavid Greenman 
2694ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2695ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2696f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2697f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2698f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2699f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2700f7788e8eSJonathan Lemon 		} else {
2701ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2702f7788e8eSJonathan Lemon 		}
2703a17c678eSDavid Greenman 		break;
2704a17c678eSDavid Greenman 
2705fb917226SRuslan Ermilov 	case SIOCSIFCAP:
270660bb79ebSPyun YongHyeon 		reinit = 0;
27078ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
270840929967SGleb Smirnoff #ifdef DEVICE_POLLING
270940929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
271040929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
271140929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
271240929967SGleb Smirnoff 				if (error)
271340929967SGleb Smirnoff 					return(error);
271440929967SGleb Smirnoff 				FXP_LOCK(sc);
271540929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
271640929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
271740929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
271840929967SGleb Smirnoff 				FXP_UNLOCK(sc);
271940929967SGleb Smirnoff 			} else {
272040929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
272140929967SGleb Smirnoff 				/* Enable interrupts in any case */
272240929967SGleb Smirnoff 				FXP_LOCK(sc);
272340929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
272440929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
272540929967SGleb Smirnoff 				FXP_UNLOCK(sc);
272640929967SGleb Smirnoff 			}
272740929967SGleb Smirnoff 		}
272840929967SGleb Smirnoff #endif
272940929967SGleb Smirnoff 		FXP_LOCK(sc);
273060bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
273160bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
273260bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
273360bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
273460bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
273560bb79ebSPyun YongHyeon 			else
273660bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
273760bb79ebSPyun YongHyeon 		}
273860bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2739f13075afSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
274060bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2741f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2742f13075afSPyun YongHyeon 				reinit++;
2743f13075afSPyun YongHyeon 		}
2744c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
2745c21e84e4SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2746c21e84e4SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2747c21e84e4SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
2748c21e84e4SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2749c21e84e4SPyun YongHyeon 			else
2750c21e84e4SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2751c21e84e4SPyun YongHyeon 		}
27527137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
27537137cea0SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
27547137cea0SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
275560bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
275660bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
27578ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
27588ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
27598ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
27608ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
27618ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
27628ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
27638ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
276460bb79ebSPyun YongHyeon 				reinit++;
276560bb79ebSPyun YongHyeon 		}
276660bb79ebSPyun YongHyeon 		if (reinit > 0)
27678ef1f631SYaroslav Tykhiy 			fxp_init_body(sc);
27683212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2769fb917226SRuslan Ermilov 		break;
2770fb917226SRuslan Ermilov 
2771a17c678eSDavid Greenman 	default:
2772673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2773a17c678eSDavid Greenman 	}
2774a17c678eSDavid Greenman 	return (error);
2775a17c678eSDavid Greenman }
2776397f9dfeSDavid Greenman 
2777397f9dfeSDavid Greenman /*
277809882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
277909882363SJonathan Lemon  */
278009882363SJonathan Lemon static int
278109882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
278209882363SJonathan Lemon {
278309882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2784fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
278509882363SJonathan Lemon 	struct ifmultiaddr *ifma;
278609882363SJonathan Lemon 	int nmcasts;
278709882363SJonathan Lemon 
278809882363SJonathan Lemon 	nmcasts = 0;
278909882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
279013b203d0SRobert Watson 		IF_ADDR_LOCK(ifp);
279109882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
279209882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
279309882363SJonathan Lemon 				continue;
279409882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
279509882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
279609882363SJonathan Lemon 				nmcasts = 0;
279709882363SJonathan Lemon 				break;
279809882363SJonathan Lemon 			}
279909882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2800bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
280109882363SJonathan Lemon 			nmcasts++;
280209882363SJonathan Lemon 		}
280313b203d0SRobert Watson 		IF_ADDR_UNLOCK(ifp);
280409882363SJonathan Lemon 	}
2805bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
280609882363SJonathan Lemon 	return (nmcasts);
280709882363SJonathan Lemon }
280809882363SJonathan Lemon 
280909882363SJonathan Lemon /*
2810397f9dfeSDavid Greenman  * Program the multicast filter.
2811397f9dfeSDavid Greenman  *
2812397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2813397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
28143114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2815397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2816dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2817397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2818397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2819397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2820397f9dfeSDavid Greenman  *
2821397f9dfeSDavid Greenman  * This function must be called at splimp.
2822397f9dfeSDavid Greenman  */
2823397f9dfeSDavid Greenman static void
2824f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2825397f9dfeSDavid Greenman {
2826397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2827b2badf02SMaxime Henrion 	struct fxp_tx *txp;
28287dced78aSDavid Greenman 	int count;
2829397f9dfeSDavid Greenman 
283067fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
28313114fdb4SDavid Greenman 	/*
28323114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
28333114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
28343114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
28353114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
28363114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
28373114fdb4SDavid Greenman 	 */
2838397f9dfeSDavid Greenman 	if (sc->tx_queued) {
28393114fdb4SDavid Greenman 		/*
28403114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
28413114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
28423114fdb4SDavid Greenman 		 */
28433114fdb4SDavid Greenman 		if (sc->need_mcsetup)
28443114fdb4SDavid Greenman 			return;
2845397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
28463114fdb4SDavid Greenman 
28473114fdb4SDavid Greenman 		/*
284872a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
284972a32a26SJonathan Lemon 		 * when all TX commands have been processed.
28503114fdb4SDavid Greenman 		 */
2851b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
2852b2badf02SMaxime Henrion 		txp->tx_mbuf = NULL;
2853b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
285483e6547dSMaxime Henrion 		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
285583e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
28563114fdb4SDavid Greenman 		/*
28573114fdb4SDavid Greenman 		 * Advance the end of list forward.
28583114fdb4SDavid Greenman 		 */
285983e6547dSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
286083e6547dSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
28615f361cbeSMaxime Henrion 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2862b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
28633114fdb4SDavid Greenman 		sc->tx_queued++;
28643114fdb4SDavid Greenman 		/*
28653114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
28663114fdb4SDavid Greenman 		 */
28673114fdb4SDavid Greenman 		fxp_scb_wait(sc);
28682e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
28693114fdb4SDavid Greenman 		/*
28703114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
28713114fdb4SDavid Greenman 		 * card again.
28723114fdb4SDavid Greenman 		 */
2873df79d527SGleb Smirnoff 		sc->watchdog_timer = 5;
28743114fdb4SDavid Greenman 
2875397f9dfeSDavid Greenman 		return;
2876397f9dfeSDavid Greenman 	}
2877397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2878397f9dfeSDavid Greenman 
2879397f9dfeSDavid Greenman 	/*
2880397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2881397f9dfeSDavid Greenman 	 */
2882397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
288383e6547dSMaxime Henrion 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
288483e6547dSMaxime Henrion 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
288583e6547dSMaxime Henrion 	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2886b2badf02SMaxime Henrion 	txp = &sc->fxp_desc.mcs_tx;
2887b2badf02SMaxime Henrion 	txp->tx_mbuf = NULL;
2888b2badf02SMaxime Henrion 	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2889b2badf02SMaxime Henrion 	txp->tx_next = sc->fxp_desc.tx_list;
289009882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2891b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2892397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2893397f9dfeSDavid Greenman 
2894397f9dfeSDavid Greenman 	/*
2895397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2896397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2897397f9dfeSDavid Greenman 	 */
28987dced78aSDavid Greenman 	count = 100;
2899397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
29007dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
29017dced78aSDavid Greenman 		DELAY(10);
29027dced78aSDavid Greenman 	if (count == 0) {
2903f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
29047dced78aSDavid Greenman 		return;
29057dced78aSDavid Greenman 	}
2906397f9dfeSDavid Greenman 
2907397f9dfeSDavid Greenman 	/*
2908397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2909397f9dfeSDavid Greenman 	 */
2910397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2911b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2912b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
29132e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2914397f9dfeSDavid Greenman 
2915df79d527SGleb Smirnoff 	sc->watchdog_timer = 2;
2916397f9dfeSDavid Greenman 	return;
2917397f9dfeSDavid Greenman }
291872a32a26SJonathan Lemon 
291974d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
292074d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
292174d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
292274d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
292374d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
292474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2925de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
292672a32a26SJonathan Lemon 
292774d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
292872a32a26SJonathan Lemon 
292972a32a26SJonathan Lemon struct ucode {
293074d1ed23SMaxime Henrion 	uint32_t	revision;
293174d1ed23SMaxime Henrion 	uint32_t	*ucode;
293272a32a26SJonathan Lemon 	int		length;
293372a32a26SJonathan Lemon 	u_short		int_delay_offset;
293472a32a26SJonathan Lemon 	u_short		bundle_max_offset;
293572a32a26SJonathan Lemon } ucode_table[] = {
293672a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
293772a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
293872a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
293972a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
294072a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
294172a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
294272a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
294372a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
294472a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
294572a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2946507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
2947de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
294872a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
294972a32a26SJonathan Lemon };
295072a32a26SJonathan Lemon 
295172a32a26SJonathan Lemon static void
295272a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
295372a32a26SJonathan Lemon {
295472a32a26SJonathan Lemon 	struct ucode *uc;
295572a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
295694a4f968SPyun YongHyeon 	int i;
295772a32a26SJonathan Lemon 
295872a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
295972a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
296072a32a26SJonathan Lemon 			break;
296172a32a26SJonathan Lemon 	if (uc->ucode == NULL)
296272a32a26SJonathan Lemon 		return;
2963b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
296472a32a26SJonathan Lemon 	cbp->cb_status = 0;
296583e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
296683e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
296794a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
296894a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
296972a32a26SJonathan Lemon 	if (uc->int_delay_offset)
297074d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
297183e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
297272a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
297374d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
297483e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
297572a32a26SJonathan Lemon 	/*
297672a32a26SJonathan Lemon 	 * Download the ucode to the chip.
297772a32a26SJonathan Lemon 	 */
297872a32a26SJonathan Lemon 	fxp_scb_wait(sc);
2979b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2980b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
298172a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
298272a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2983209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2984b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
298572a32a26SJonathan Lemon 	device_printf(sc->dev,
298672a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
298772a32a26SJonathan Lemon 	    sc->tunable_int_delay,
298872a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
298972a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
299072a32a26SJonathan Lemon }
299172a32a26SJonathan Lemon 
299272a32a26SJonathan Lemon static int
299372a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
299472a32a26SJonathan Lemon {
299572a32a26SJonathan Lemon 	int error, value;
299672a32a26SJonathan Lemon 
299772a32a26SJonathan Lemon 	value = *(int *)arg1;
299872a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
299972a32a26SJonathan Lemon 	if (error || !req->newptr)
300072a32a26SJonathan Lemon 		return (error);
300172a32a26SJonathan Lemon 	if (value < low || value > high)
300272a32a26SJonathan Lemon 		return (EINVAL);
300372a32a26SJonathan Lemon 	*(int *)arg1 = value;
300472a32a26SJonathan Lemon 	return (0);
300572a32a26SJonathan Lemon }
300672a32a26SJonathan Lemon 
300772a32a26SJonathan Lemon /*
300872a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
300972a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
301072a32a26SJonathan Lemon  */
301172a32a26SJonathan Lemon static int
301272a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
301372a32a26SJonathan Lemon {
301472a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
301572a32a26SJonathan Lemon }
301672a32a26SJonathan Lemon 
301772a32a26SJonathan Lemon static int
301872a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
301972a32a26SJonathan Lemon {
302072a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
302172a32a26SJonathan Lemon }
3022