xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 0685c824ca36f69f76f1ba7e66478e80138c06b7)
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>
476d7e1582SPyun YongHyeon #include <sys/lock.h>
48fe12f24bSPoul-Henning Kamp #include <sys/module.h>
496d7e1582SPyun YongHyeon #include <sys/mutex.h>
508fae3bd4SPyun YongHyeon #include <sys/rman.h>
514458ac71SBruce Evans #include <sys/socket.h>
528fae3bd4SPyun YongHyeon #include <sys/sockio.h>
5372a32a26SJonathan Lemon #include <sys/sysctl.h>
54a17c678eSDavid Greenman 
558fae3bd4SPyun YongHyeon #include <net/bpf.h>
568fae3bd4SPyun YongHyeon #include <net/ethernet.h>
57a17c678eSDavid Greenman #include <net/if.h>
588fae3bd4SPyun YongHyeon #include <net/if_arp.h>
59397f9dfeSDavid Greenman #include <net/if_dl.h>
60ba8c6fd5SDavid Greenman #include <net/if_media.h>
61e8c8b728SJonathan Lemon #include <net/if_types.h>
62e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
63e8c8b728SJonathan Lemon 
64c8bca6dcSBill Paul #include <netinet/in.h>
65c8bca6dcSBill Paul #include <netinet/in_systm.h>
66c8bca6dcSBill Paul #include <netinet/ip.h>
67f13075afSPyun YongHyeon #include <netinet/tcp.h>
68f13075afSPyun YongHyeon #include <netinet/udp.h>
69f13075afSPyun YongHyeon 
70f13075afSPyun YongHyeon #include <machine/bus.h>
71c8bca6dcSBill Paul #include <machine/in_cksum.h>
72f13075afSPyun YongHyeon #include <machine/resource.h>
73c8bca6dcSBill Paul 
744fbd232cSWarner Losh #include <dev/pci/pcivar.h>
754fbd232cSWarner Losh #include <dev/pci/pcireg.h>		/* for PCIM_CMD_xxx */
76a17c678eSDavid Greenman 
77f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
78f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
79f7788e8eSJonathan Lemon 
80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
81f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8272a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
83f7788e8eSJonathan Lemon 
84f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
85f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
86f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
87f7788e8eSJonathan Lemon #include "miibus_if.h"
884fc1dda9SAndrew Gallatin 
89ba8c6fd5SDavid Greenman /*
90ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
91ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
92ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
93ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
94ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
95ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
96ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
97ba8c6fd5SDavid Greenman  */
98ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
99ba8c6fd5SDavid Greenman 
100ba8c6fd5SDavid Greenman /*
101f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
102f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
103f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
104f7788e8eSJonathan Lemon  */
105f7788e8eSJonathan Lemon static int tx_threshold = 64;
106f7788e8eSJonathan Lemon 
107f7788e8eSJonathan Lemon /*
108f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
109f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
110f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
111f7788e8eSJonathan Lemon  * to fxp_init.
112f7788e8eSJonathan Lemon  *
113f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
114f7788e8eSJonathan Lemon  */
115f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
116f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
117f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
118f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
119f7788e8eSJonathan Lemon 	0x0,	/*  0 */
120f7788e8eSJonathan Lemon 	0x0,	/*  1 */
121f7788e8eSJonathan Lemon 	0x0,	/*  2 */
122f7788e8eSJonathan Lemon 	0x0,	/*  3 */
123f7788e8eSJonathan Lemon 	0x0,	/*  4 */
124f7788e8eSJonathan Lemon 	0x0,	/*  5 */
125f7788e8eSJonathan Lemon 	0x32,	/*  6 */
126f7788e8eSJonathan Lemon 	0x0,	/*  7 */
127f7788e8eSJonathan Lemon 	0x0,	/*  8 */
128f7788e8eSJonathan Lemon 	0x0,	/*  9 */
129f7788e8eSJonathan Lemon 	0x6,	/* 10 */
130f7788e8eSJonathan Lemon 	0x0,	/* 11 */
131f7788e8eSJonathan Lemon 	0x0,	/* 12 */
132f7788e8eSJonathan Lemon 	0x0,	/* 13 */
133f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
134f7788e8eSJonathan Lemon 	0x48,	/* 15 */
135f7788e8eSJonathan Lemon 	0x0,	/* 16 */
136f7788e8eSJonathan Lemon 	0x40,	/* 17 */
137f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
138f7788e8eSJonathan Lemon 	0x0,	/* 19 */
139f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
140f7788e8eSJonathan Lemon 	0x5	/* 21 */
141f7788e8eSJonathan Lemon };
142f7788e8eSJonathan Lemon 
143f7788e8eSJonathan Lemon struct fxp_ident {
14474d1ed23SMaxime Henrion 	uint16_t	devid;
145f19fc5d8SJohn Polstra 	int16_t		revid;		/* -1 matches anything */
146f7788e8eSJonathan Lemon 	char 		*name;
147f7788e8eSJonathan Lemon };
148f7788e8eSJonathan Lemon 
149f7788e8eSJonathan Lemon /*
150f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
151f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
152f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
153f7788e8eSJonathan Lemon  * them.
154f7788e8eSJonathan Lemon  */
155f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
156f19fc5d8SJohn Polstra     { 0x1029,	-1,	"Intel 82559 PCI/CardBus Pro/100" },
157f19fc5d8SJohn Polstra     { 0x1030,	-1,	"Intel 82559 Pro/100 Ethernet" },
158f19fc5d8SJohn Polstra     { 0x1031,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
159f19fc5d8SJohn Polstra     { 0x1032,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
160f19fc5d8SJohn Polstra     { 0x1033,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
161f19fc5d8SJohn Polstra     { 0x1034,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
162f19fc5d8SJohn Polstra     { 0x1035,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
163f19fc5d8SJohn Polstra     { 0x1036,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
164f19fc5d8SJohn Polstra     { 0x1037,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
165f19fc5d8SJohn Polstra     { 0x1038,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
166f19fc5d8SJohn Polstra     { 0x1039,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
167f19fc5d8SJohn Polstra     { 0x103A,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
168f19fc5d8SJohn Polstra     { 0x103B,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
169f19fc5d8SJohn Polstra     { 0x103C,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
170f19fc5d8SJohn Polstra     { 0x103D,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
171f19fc5d8SJohn Polstra     { 0x103E,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
172f19fc5d8SJohn Polstra     { 0x1050,	-1,	"Intel 82801BA (D865) Pro/100 VE Ethernet" },
173c2b37819SWarner Losh     { 0x1051,	-1,	"Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
174f19fc5d8SJohn Polstra     { 0x1059,	-1,	"Intel 82551QM Pro/100 M Mobile Connection" },
175048ca166SMaxime Henrion     { 0x1064,	-1,	"Intel 82562EZ (ICH6)" },
17642a4336aSRink Springer     { 0x1065,	-1,	"Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
17729a8929dSMaxime Henrion     { 0x1068,	-1,	"Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
17852dfd9cdSMaxime Henrion     { 0x1069,	-1,	"Intel 82562EM/EX/GX Pro/100 Ethernet" },
179847f5310SRemko Lodder     { 0x1091,	-1,	"Intel 82562GX Pro/100 Ethernet" },
180c943ffccSMatteo Riondato     { 0x1092,	-1,	"Intel Pro/100 VE Network Connection" },
181597d4fe4SRink Springer     { 0x1093,	-1,	"Intel Pro/100 VM Network Connection" },
18242a4336aSRink Springer     { 0x1094,	-1,	"Intel Pro/100 946GZ (ICH7) Network Connection" },
183f19fc5d8SJohn Polstra     { 0x1209,	-1,	"Intel 82559ER Embedded 10/100 Ethernet" },
184f19fc5d8SJohn Polstra     { 0x1229,	0x01,	"Intel 82557 Pro/100 Ethernet" },
185f19fc5d8SJohn Polstra     { 0x1229,	0x02,	"Intel 82557 Pro/100 Ethernet" },
186f19fc5d8SJohn Polstra     { 0x1229,	0x03,	"Intel 82557 Pro/100 Ethernet" },
187f19fc5d8SJohn Polstra     { 0x1229,	0x04,	"Intel 82558 Pro/100 Ethernet" },
188f19fc5d8SJohn Polstra     { 0x1229,	0x05,	"Intel 82558 Pro/100 Ethernet" },
189f19fc5d8SJohn Polstra     { 0x1229,	0x06,	"Intel 82559 Pro/100 Ethernet" },
190f19fc5d8SJohn Polstra     { 0x1229,	0x07,	"Intel 82559 Pro/100 Ethernet" },
191f19fc5d8SJohn Polstra     { 0x1229,	0x08,	"Intel 82559 Pro/100 Ethernet" },
192f19fc5d8SJohn Polstra     { 0x1229,	0x09,	"Intel 82559ER Pro/100 Ethernet" },
193f19fc5d8SJohn Polstra     { 0x1229,	0x0c,	"Intel 82550 Pro/100 Ethernet" },
194f19fc5d8SJohn Polstra     { 0x1229,	0x0d,	"Intel 82550 Pro/100 Ethernet" },
195f19fc5d8SJohn Polstra     { 0x1229,	0x0e,	"Intel 82550 Pro/100 Ethernet" },
196f19fc5d8SJohn Polstra     { 0x1229,	0x0f,	"Intel 82551 Pro/100 Ethernet" },
197f19fc5d8SJohn Polstra     { 0x1229,	0x10,	"Intel 82551 Pro/100 Ethernet" },
198f19fc5d8SJohn Polstra     { 0x1229,	-1,	"Intel 82557/8/9 Pro/100 Ethernet" },
199f19fc5d8SJohn Polstra     { 0x2449,	-1,	"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
20086c8aacbSMaxime Henrion     { 0x27dc,	-1,	"Intel 82801GB (ICH7) 10/100 Ethernet" },
201f19fc5d8SJohn Polstra     { 0,	-1,	NULL },
202f7788e8eSJonathan Lemon };
203f7788e8eSJonathan Lemon 
204c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
205c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
206c8bca6dcSBill Paul #else
207c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
208c8bca6dcSBill Paul #endif
209c8bca6dcSBill Paul 
210f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
211f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
212f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
213f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
214f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
215f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
216f7788e8eSJonathan Lemon 
217f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
218f13075afSPyun YongHyeon static void		fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp,
219f13075afSPyun YongHyeon 			    struct mbuf *m, uint16_t status, int pos);
2204953bccaSNate Lawson static void		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
22174d1ed23SMaxime Henrion 			    uint8_t statack, int count);
222f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2234953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
224f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
225f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
2264953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
2274e53f837SPyun YongHyeon static int		fxp_encap(struct fxp_softc *sc, struct mbuf **m_head);
2284e53f837SPyun YongHyeon static void		fxp_txeof(struct fxp_softc *sc);
229f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
230f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
231f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
232f7788e8eSJonathan Lemon 			    caddr_t data);
233df79d527SGleb Smirnoff static void 		fxp_watchdog(struct fxp_softc *sc);
23485050421SPyun YongHyeon static void		fxp_add_rfabuf(struct fxp_softc *sc,
23585050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23685050421SPyun YongHyeon static void		fxp_discard_rfabuf(struct fxp_softc *sc,
23785050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23885050421SPyun YongHyeon static int		fxp_new_rfabuf(struct fxp_softc *sc,
23985050421SPyun YongHyeon     			    struct fxp_rx *rxp);
24009882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
241f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
24274d1ed23SMaxime Henrion static uint16_t		fxp_eeprom_getword(struct fxp_softc *sc, int offset,
243f7788e8eSJonathan Lemon 			    int autosize);
24400c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
24574d1ed23SMaxime Henrion 			    uint16_t data);
246f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
247f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
248f7788e8eSJonathan Lemon 			    int offset, int words);
24900c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
25000c4116bSJonathan Lemon 			    int offset, int words);
251f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
252f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
253f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
254f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
255f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
256f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
257f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
25816ec4b00SWarner Losh static int		fxp_miibus_writereg(device_t dev, int phy, int reg,
259f7788e8eSJonathan Lemon 			    int value);
26072a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
26172a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
26272a32a26SJonathan Lemon 			    int low, int high);
26372a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
26472a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
26528935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
26628935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
26728935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
26874d1ed23SMaxime Henrion     			    volatile uint16_t *status, bus_dma_tag_t dmat,
269209b07bcSMaxime Henrion 			    bus_dmamap_t map);
270f7788e8eSJonathan Lemon 
271f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
272f7788e8eSJonathan Lemon 	/* Device interface */
273f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
274f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
275f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
276f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
277f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
278f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
279f7788e8eSJonathan Lemon 
280f7788e8eSJonathan Lemon 	/* MII interface */
281f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
282f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
283f7788e8eSJonathan Lemon 
284f7788e8eSJonathan Lemon 	{ 0, 0 }
285f7788e8eSJonathan Lemon };
286f7788e8eSJonathan Lemon 
287f7788e8eSJonathan Lemon static driver_t fxp_driver = {
288f7788e8eSJonathan Lemon 	"fxp",
289f7788e8eSJonathan Lemon 	fxp_methods,
290f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
291f7788e8eSJonathan Lemon };
292f7788e8eSJonathan Lemon 
293f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
294f7788e8eSJonathan Lemon 
295f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
296f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
297f7788e8eSJonathan Lemon 
29805bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
29905bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
30005bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30105bd8c22SMaxime Henrion 	{ -1, 0 }
30205bd8c22SMaxime Henrion };
30305bd8c22SMaxime Henrion 
30405bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
30505bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
30605bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30705bd8c22SMaxime Henrion 	{ -1, 0 }
30805bd8c22SMaxime Henrion };
30905bd8c22SMaxime Henrion 
310f7788e8eSJonathan Lemon /*
311dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
312dfe61cf1SDavid Greenman  * completed).
313dfe61cf1SDavid Greenman  */
31428935f27SMaxime Henrion static void
315f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
316a17c678eSDavid Greenman {
3173cf09dd1SMarcel Moolenaar 	union {
3183cf09dd1SMarcel Moolenaar 		uint16_t w;
3193cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3203cf09dd1SMarcel Moolenaar 	} flowctl;
321a17c678eSDavid Greenman 	int i = 10000;
322a17c678eSDavid Greenman 
3237dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3247dced78aSDavid Greenman 		DELAY(2);
3253cf09dd1SMarcel Moolenaar 	if (i == 0) {
3263cf09dd1SMarcel Moolenaar 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL);
3273cf09dd1SMarcel Moolenaar 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL + 1);
32800c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
329e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
330e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3313cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3323cf09dd1SMarcel Moolenaar 	}
3337dced78aSDavid Greenman }
3347dced78aSDavid Greenman 
33528935f27SMaxime Henrion static void
3362e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3372e2b8238SJonathan Lemon {
3382e2b8238SJonathan Lemon 
3392e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3402e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3412e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3422e2b8238SJonathan Lemon 	}
3432e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3442e2b8238SJonathan Lemon }
3452e2b8238SJonathan Lemon 
34628935f27SMaxime Henrion static void
34774d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
348209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3497dced78aSDavid Greenman {
3507dced78aSDavid Greenman 	int i = 10000;
3517dced78aSDavid Greenman 
352209b07bcSMaxime Henrion 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
353209b07bcSMaxime Henrion 	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) {
3547dced78aSDavid Greenman 		DELAY(2);
355209b07bcSMaxime Henrion 		bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
356209b07bcSMaxime Henrion 	}
3577dced78aSDavid Greenman 	if (i == 0)
358f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
359a17c678eSDavid Greenman }
360a17c678eSDavid Greenman 
361dfe61cf1SDavid Greenman /*
36228935f27SMaxime Henrion  * Return identification string if this device is ours.
363dfe61cf1SDavid Greenman  */
3646182fdbdSPeter Wemm static int
3656182fdbdSPeter Wemm fxp_probe(device_t dev)
366a17c678eSDavid Greenman {
36774d1ed23SMaxime Henrion 	uint16_t devid;
36874d1ed23SMaxime Henrion 	uint8_t revid;
369f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
370f7788e8eSJonathan Lemon 
37155ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
372f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
373f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
374f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
375f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
376f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
377f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
378538565c4SWarner Losh 				return (BUS_PROBE_DEFAULT);
37955ce7b51SDavid Greenman 			}
380dd68ef16SPeter Wemm 		}
381f7788e8eSJonathan Lemon 	}
382f7788e8eSJonathan Lemon 	return (ENXIO);
3836182fdbdSPeter Wemm }
3846182fdbdSPeter Wemm 
385b2badf02SMaxime Henrion static void
386b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
387b2badf02SMaxime Henrion {
38874d1ed23SMaxime Henrion 	uint32_t *addr;
389b2badf02SMaxime Henrion 
390b2badf02SMaxime Henrion 	if (error)
391b2badf02SMaxime Henrion 		return;
392b2badf02SMaxime Henrion 
393b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
394b2badf02SMaxime Henrion 	addr = arg;
395b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
396b2badf02SMaxime Henrion }
397b2badf02SMaxime Henrion 
3986182fdbdSPeter Wemm static int
3996182fdbdSPeter Wemm fxp_attach(device_t dev)
400a17c678eSDavid Greenman {
4016720ebccSMaxime Henrion 	struct fxp_softc *sc;
4026720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4036720ebccSMaxime Henrion 	struct fxp_tx *txp;
404b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4056720ebccSMaxime Henrion 	struct ifnet *ifp;
40674d1ed23SMaxime Henrion 	uint32_t val;
40774d1ed23SMaxime Henrion 	uint16_t data, myea[ETHER_ADDR_LEN / 2];
408fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4097137cea0SPyun YongHyeon 	int i, pmc, prefer_iomap;
4103212724cSJohn Baldwin 	int error;
411a17c678eSDavid Greenman 
4126720ebccSMaxime Henrion 	error = 0;
4136720ebccSMaxime Henrion 	sc = device_get_softc(dev);
414f7788e8eSJonathan Lemon 	sc->dev = dev;
4156008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4164953bccaSNate Lawson 	    MTX_DEF);
4173212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4184953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4194953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
420a17c678eSDavid Greenman 
4217ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4227ba33d82SBrooks Davis 	if (ifp == NULL) {
4237ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4247ba33d82SBrooks Davis 		error = ENOSPC;
4257ba33d82SBrooks Davis 		goto fail;
4267ba33d82SBrooks Davis 	}
4277ba33d82SBrooks Davis 
428dfe61cf1SDavid Greenman 	/*
4292bce79a2SMaxim Sobolev 	 * Enable bus mastering.
430df373873SWes Peters 	 */
431cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4329fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
43379495006SWarner Losh 
434df373873SWes Peters 	/*
4359fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4369fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4379fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
438dfe61cf1SDavid Greenman 	 */
4392a05a4ebSMatt Jacob 	prefer_iomap = 0;
44005bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
44105bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
44205bd8c22SMaxime Henrion 	if (prefer_iomap)
44305bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
44405bd8c22SMaxime Henrion 	else
44505bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4469fa6ccfbSMatt Jacob 
44705bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
44805bd8c22SMaxime Henrion 	if (error) {
44905bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
45005bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
45105bd8c22SMaxime Henrion 		else
45205bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
45305bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4549fa6ccfbSMatt Jacob 	}
45505bd8c22SMaxime Henrion 	if (error) {
45605bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4576182fdbdSPeter Wemm 		error = ENXIO;
458a17c678eSDavid Greenman 		goto fail;
459a17c678eSDavid Greenman 	}
46005bd8c22SMaxime Henrion 
4619fa6ccfbSMatt Jacob 	if (bootverbose) {
4629fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
46305bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4646182fdbdSPeter Wemm 	}
4656182fdbdSPeter Wemm 
466f7788e8eSJonathan Lemon 	/*
467f7788e8eSJonathan Lemon 	 * Reset to a stable state.
468f7788e8eSJonathan Lemon 	 */
469f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
470f7788e8eSJonathan Lemon 	DELAY(10);
471f7788e8eSJonathan Lemon 
472f7788e8eSJonathan Lemon 	/*
473f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
474f7788e8eSJonathan Lemon 	 */
475f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
476f7788e8eSJonathan Lemon 
477f7788e8eSJonathan Lemon 	/*
47893b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
47993b6e2e6SMaxime Henrion 	 */
48093b6e2e6SMaxime Henrion 	fxp_read_eeprom(sc, &data, 5, 1);
48193b6e2e6SMaxime Henrion 	if ((data >> 8) == 1)
48293b6e2e6SMaxime Henrion 		sc->revision = FXP_REV_82557;
48393b6e2e6SMaxime Henrion 	else
48493b6e2e6SMaxime Henrion 		sc->revision = pci_get_revid(dev);
48593b6e2e6SMaxime Henrion 
48693b6e2e6SMaxime Henrion 	/*
4877137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
4887137cea0SPyun YongHyeon 	 */
4897137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
4907137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
4917137cea0SPyun YongHyeon 		fxp_read_eeprom(sc, &data, 10, 1);
4927137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
4937137cea0SPyun YongHyeon 		    pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0)
4947137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
4957137cea0SPyun YongHyeon 	}
4967137cea0SPyun YongHyeon 
4977137cea0SPyun YongHyeon 	/*
4983bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
499f7788e8eSJonathan Lemon 	 */
500f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
50193b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5024ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
503dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
504f7788e8eSJonathan Lemon 
5050f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5060f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
50750a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
508858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
50972a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
5100f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5110f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
51250a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
513858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
51472a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
5150f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5160f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5170f1db1d6SMaxime Henrion 	    OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
5180f1db1d6SMaxime Henrion 	    "FXP RNR events");
5190f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5200f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5210f1db1d6SMaxime Henrion 	    OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0,
5220f1db1d6SMaxime Henrion 	    "FXP flow control disabled");
52372a32a26SJonathan Lemon 
52472a32a26SJonathan Lemon 	/*
52572a32a26SJonathan Lemon 	 * Pull in device tunables.
52672a32a26SJonathan Lemon 	 */
52772a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
52872a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
52903edfff3SRobert Watson 	sc->tunable_noflow = 1;
53072a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
53172a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
53272a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
53372a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
5340f1db1d6SMaxime Henrion 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
5350f1db1d6SMaxime Henrion 	    "noflow", &sc->tunable_noflow);
5360f1db1d6SMaxime Henrion 	sc->rnr = 0;
53772a32a26SJonathan Lemon 
53872a32a26SJonathan Lemon 	/*
5392e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
54000c4116bSJonathan Lemon 	 *
54172a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
54272a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
54372a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
54400c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
54500c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
54600c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
54700c4116bSJonathan Lemon 	 *
54800c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5492e2b8238SJonathan Lemon 	 */
5502e2b8238SJonathan Lemon 	i = pci_get_device(dev);
55172a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
55272a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
55300c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
55400c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
55574d1ed23SMaxime Henrion 			uint16_t cksum;
55600c4116bSJonathan Lemon 			int i;
55700c4116bSJonathan Lemon 
55800c4116bSJonathan Lemon 			device_printf(dev,
559001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
56000c4116bSJonathan Lemon 			data &= ~0x02;
56100c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
56200c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
56300c4116bSJonathan Lemon 			cksum = 0;
56400c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
56500c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
56600c4116bSJonathan Lemon 				cksum += data;
56700c4116bSJonathan Lemon 			}
56800c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
56900c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
57000c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
57100c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
57200c4116bSJonathan Lemon 			device_printf(dev,
57300c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
57400c4116bSJonathan Lemon 			    i, data, cksum);
57500c4116bSJonathan Lemon #if 1
57600c4116bSJonathan Lemon 			/*
57700c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
57800c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
57900c4116bSJonathan Lemon 			 */
5802e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
58100c4116bSJonathan Lemon #endif
58200c4116bSJonathan Lemon 		}
58300c4116bSJonathan Lemon 	}
5842e2b8238SJonathan Lemon 
5852e2b8238SJonathan Lemon 	/*
5863bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5873bd07cfdSJonathan Lemon 	 */
58872a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5893bd07cfdSJonathan Lemon 		/*
59074396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
59174396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
59274396a0aSJonathan Lemon 		 * the board to turn on MWI.
5933bd07cfdSJonathan Lemon 		 */
59474396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
59574396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5963bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5973bd07cfdSJonathan Lemon 
5983bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5993bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
60044e0bc11SYaroslav Tykhiy 
60144e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
60244e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
60344e0bc11SYaroslav Tykhiy 	} else {
60444e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
60544e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6063bd07cfdSJonathan Lemon 	}
6073bd07cfdSJonathan Lemon 
608f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
609f13075afSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
610f13075afSPyun YongHyeon 		sc->flags |= FXP_FLAG_82559_RXCSUM;
6113bd07cfdSJonathan Lemon 	/*
612c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
613c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
614c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
615c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
616c8bca6dcSBill Paul 	 */
617507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
618507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
619507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
620c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
621c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
622c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
623f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
624f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
625c8bca6dcSBill Paul 	} else {
626c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
627c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
628c8bca6dcSBill Paul 	}
629c8bca6dcSBill Paul 
630c8bca6dcSBill Paul 	/*
631b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
632b2badf02SMaxime Henrion 	 */
63340c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
634c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
635c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
63640c20505SMaxime Henrion 		sc->maxtxseg--;
637c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
638c21e84e4SPyun YongHyeon 	}
639c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
640c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
641c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
642c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
643c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_mtag);
644b2badf02SMaxime Henrion 	if (error) {
645b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
646b2badf02SMaxime Henrion 		goto fail;
647b2badf02SMaxime Henrion 	}
648b2badf02SMaxime Henrion 
649c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
650c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
651c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
652c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
653b2badf02SMaxime Henrion 	if (error) {
654b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
655b2badf02SMaxime Henrion 		goto fail;
656b2badf02SMaxime Henrion 	}
657b2badf02SMaxime Henrion 
658b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
659aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap);
660b2badf02SMaxime Henrion 	if (error)
6614953bccaSNate Lawson 		goto fail;
662b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
663b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
664b2badf02SMaxime Henrion 	if (error) {
665b2badf02SMaxime Henrion 		device_printf(dev, "could not map the stats buffer\n");
666b2badf02SMaxime Henrion 		goto fail;
667b2badf02SMaxime Henrion 	}
668b2badf02SMaxime Henrion 
669c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
670c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
671c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
672c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
673b2badf02SMaxime Henrion 	if (error) {
674b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
675b2badf02SMaxime Henrion 		goto fail;
676b2badf02SMaxime Henrion 	}
677b2badf02SMaxime Henrion 
678b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
679aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map);
680b2badf02SMaxime Henrion 	if (error)
6814953bccaSNate Lawson 		goto fail;
682b2badf02SMaxime Henrion 
683b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
684b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
685b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
686b2badf02SMaxime Henrion 	if (error) {
687b2badf02SMaxime Henrion 		device_printf(dev, "could not map DMA memory\n");
688b2badf02SMaxime Henrion 		goto fail;
689b2badf02SMaxime Henrion 	}
690b2badf02SMaxime Henrion 
691c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
692c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
693c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
694c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
695b2badf02SMaxime Henrion 	if (error) {
696b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
697b2badf02SMaxime Henrion 		goto fail;
698b2badf02SMaxime Henrion 	}
699b2badf02SMaxime Henrion 
700b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
701b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->mcs_map);
702b2badf02SMaxime Henrion 	if (error)
7034953bccaSNate Lawson 		goto fail;
704b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
705b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
706b2badf02SMaxime Henrion 	if (error) {
707b2badf02SMaxime Henrion 		device_printf(dev, "can't map the multicast setup command\n");
708b2badf02SMaxime Henrion 		goto fail;
709b2badf02SMaxime Henrion 	}
710b2badf02SMaxime Henrion 
711b2badf02SMaxime Henrion 	/*
7126720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7136720ebccSMaxime Henrion 	 * the TX command blocks.
714b2badf02SMaxime Henrion 	 */
7156720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7166720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7174cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7186720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
7196720ebccSMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &txp[i].tx_map);
720b2badf02SMaxime Henrion 		if (error) {
721b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
722b2badf02SMaxime Henrion 			goto fail;
723b2badf02SMaxime Henrion 		}
724b2badf02SMaxime Henrion 	}
725b2badf02SMaxime Henrion 	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
726b2badf02SMaxime Henrion 	if (error) {
727b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
728b2badf02SMaxime Henrion 		goto fail;
729b2badf02SMaxime Henrion 	}
730b2badf02SMaxime Henrion 
731b2badf02SMaxime Henrion 	/*
732b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
733b2badf02SMaxime Henrion 	 */
734b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
735b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
736b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
737b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
738b2badf02SMaxime Henrion 		if (error) {
739b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
740b2badf02SMaxime Henrion 			goto fail;
741b2badf02SMaxime Henrion 		}
74285050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7434953bccaSNate Lawson 			error = ENOMEM;
7444953bccaSNate Lawson 			goto fail;
7454953bccaSNate Lawson 		}
74685050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
747b2badf02SMaxime Henrion 	}
748b2badf02SMaxime Henrion 
749b2badf02SMaxime Henrion 	/*
750f7788e8eSJonathan Lemon 	 * Read MAC address.
751f7788e8eSJonathan Lemon 	 */
75283e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
753fc74a9f9SBrooks Davis 	eaddr[0] = myea[0] & 0xff;
754fc74a9f9SBrooks Davis 	eaddr[1] = myea[0] >> 8;
755fc74a9f9SBrooks Davis 	eaddr[2] = myea[1] & 0xff;
756fc74a9f9SBrooks Davis 	eaddr[3] = myea[1] >> 8;
757fc74a9f9SBrooks Davis 	eaddr[4] = myea[2] & 0xff;
758fc74a9f9SBrooks Davis 	eaddr[5] = myea[2] >> 8;
759f7788e8eSJonathan Lemon 	if (bootverbose) {
7602e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
761f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7622e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7632e2b8238SJonathan Lemon 		    pci_get_revid(dev));
76472a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
76572a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
76672a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
767f7788e8eSJonathan Lemon 	}
768f7788e8eSJonathan Lemon 
769f7788e8eSJonathan Lemon 	/*
770f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
771f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
772f7788e8eSJonathan Lemon 	 *
773f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
774f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
775f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
776f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
777f7788e8eSJonathan Lemon 	 */
778f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
779f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
780f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
781f7788e8eSJonathan Lemon 	} else {
782f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
783f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
784f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
7856182fdbdSPeter Wemm 			error = ENXIO;
786ba8c6fd5SDavid Greenman 			goto fail;
787a17c678eSDavid Greenman 		}
788f7788e8eSJonathan Lemon 	}
789dccee1a1SDavid Greenman 
7909bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
791fb583156SDavid Greenman 	ifp->if_init = fxp_init;
792ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
793ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
794ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
795ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
796a17c678eSDavid Greenman 
7975fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
7985fe9116bSYaroslav Tykhiy 
799c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
800c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
801c21e84e4SPyun YongHyeon 		ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO;
802c21e84e4SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4;
803c21e84e4SPyun YongHyeon 		ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4;
804c8bca6dcSBill Paul 	}
805c8bca6dcSBill Paul 
806f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
807f13075afSPyun YongHyeon 		ifp->if_capabilities |= IFCAP_RXCSUM;
808f13075afSPyun YongHyeon 		ifp->if_capenable |= IFCAP_RXCSUM;
809f13075afSPyun YongHyeon 	}
810f13075afSPyun YongHyeon 
8117137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
8127137cea0SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL_MAGIC;
8137137cea0SPyun YongHyeon 		ifp->if_capenable |= IFCAP_WOL_MAGIC;
8147137cea0SPyun YongHyeon 	}
8157137cea0SPyun YongHyeon 
816fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
817fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
818fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
819fb917226SRuslan Ermilov #endif
820fb917226SRuslan Ermilov 
821dfe61cf1SDavid Greenman 	/*
8224953bccaSNate Lawson 	 * Attach the interface.
8234953bccaSNate Lawson 	 */
824fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
8254953bccaSNate Lawson 
8264953bccaSNate Lawson 	/*
827e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
8285fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
8295fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
830e8c8b728SJonathan Lemon 	 */
831e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
832673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
83344e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
834bd4fa9d9SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) {
835bd4fa9d9SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING |
836bd4fa9d9SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM;
837bd4fa9d9SPyun YongHyeon 		ifp->if_capenable |= IFCAP_VLAN_HWTAGGING |
838bd4fa9d9SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM;
839bd4fa9d9SPyun YongHyeon 	}
840e8c8b728SJonathan Lemon 
841483b9871SDavid Greenman 	/*
8423114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8433114fdb4SDavid Greenman 	 * TX descriptors.
844483b9871SDavid Greenman 	 */
8457929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8467929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8477929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8484a684684SDavid Greenman 
849201afb0eSMaxime Henrion 	/*
8504953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
851201afb0eSMaxime Henrion 	 */
85205bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
853ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
854201afb0eSMaxime Henrion 	if (error) {
855201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
856fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
857201afb0eSMaxime Henrion 		goto fail;
858201afb0eSMaxime Henrion 	}
859201afb0eSMaxime Henrion 
8607137cea0SPyun YongHyeon 	/*
8617137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
8627137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
8637137cea0SPyun YongHyeon 	 */
8647137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
8657137cea0SPyun YongHyeon 		FXP_LOCK(sc);
8667137cea0SPyun YongHyeon 		/* Clear wakeup events. */
867af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
8687137cea0SPyun YongHyeon 		fxp_init_body(sc);
8697137cea0SPyun YongHyeon 		fxp_stop(sc);
8707137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
8717137cea0SPyun YongHyeon 	}
8727137cea0SPyun YongHyeon 
873a17c678eSDavid Greenman fail:
8741b5a39d3SBrooks Davis 	if (error)
875f7788e8eSJonathan Lemon 		fxp_release(sc);
876f7788e8eSJonathan Lemon 	return (error);
877f7788e8eSJonathan Lemon }
878f7788e8eSJonathan Lemon 
879f7788e8eSJonathan Lemon /*
8804953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
8814953bccaSNate Lawson  * interrupt should already be torn down.
882f7788e8eSJonathan Lemon  */
883f7788e8eSJonathan Lemon static void
884f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
885f7788e8eSJonathan Lemon {
886b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
887b2badf02SMaxime Henrion 	struct fxp_tx *txp;
888b2badf02SMaxime Henrion 	int i;
889b2badf02SMaxime Henrion 
89067fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
891670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
892670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
8934953bccaSNate Lawson 	if (sc->miibus)
8944953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
8954953bccaSNate Lawson 	bus_generic_detach(sc->dev);
8964953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
897b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
898b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
899b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
900b2badf02SMaxime Henrion 		    sc->cbl_map);
901b2badf02SMaxime Henrion 	}
902b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
903b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
904b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
905b2badf02SMaxime Henrion 	}
906b2badf02SMaxime Henrion 	if (sc->mcsp) {
907b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
908b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
909b2badf02SMaxime Henrion 	}
91005bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
911b983c7b3SMaxime Henrion 	if (sc->fxp_mtag) {
912b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
913b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
914b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
915b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
916b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
917b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
918b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
919b983c7b3SMaxime Henrion 			}
920b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
921b983c7b3SMaxime Henrion 		}
922b983c7b3SMaxime Henrion 		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
923b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
924b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
925b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
926b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
927b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
928b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
929b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
930b983c7b3SMaxime Henrion 			}
931b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
932b983c7b3SMaxime Henrion 		}
933c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_mtag);
934b983c7b3SMaxime Henrion 	}
935c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
936c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
937b2badf02SMaxime Henrion 	if (sc->cbl_tag)
938b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
939b2badf02SMaxime Henrion 	if (sc->mcs_tag)
940b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
941fc74a9f9SBrooks Davis 	if (sc->ifp)
942fc74a9f9SBrooks Davis 		if_free(sc->ifp);
94372a32a26SJonathan Lemon 
9440f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
9456182fdbdSPeter Wemm }
9466182fdbdSPeter Wemm 
9476182fdbdSPeter Wemm /*
9486182fdbdSPeter Wemm  * Detach interface.
9496182fdbdSPeter Wemm  */
9506182fdbdSPeter Wemm static int
9516182fdbdSPeter Wemm fxp_detach(device_t dev)
9526182fdbdSPeter Wemm {
9536182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
9546182fdbdSPeter Wemm 
95540929967SGleb Smirnoff #ifdef DEVICE_POLLING
95640929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
95740929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
95840929967SGleb Smirnoff #endif
95940929967SGleb Smirnoff 
9604953bccaSNate Lawson 	FXP_LOCK(sc);
9611d2945d5SWarner Losh 	sc->suspended = 1;	/* Do same thing as we do for suspend */
9626182fdbdSPeter Wemm 	/*
96332cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
96420f0c80fSMaxime Henrion 	 */
96520f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
96620f0c80fSMaxime Henrion 	fxp_stop(sc);
96732cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
9689eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
96920f0c80fSMaxime Henrion 
9706182fdbdSPeter Wemm 	/*
9713212724cSJohn Baldwin 	 * Close down routes etc.
9723212724cSJohn Baldwin 	 */
9733212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
9743212724cSJohn Baldwin 
9753212724cSJohn Baldwin 	/*
9764953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
9774953bccaSNate Lawson 	 * races with fxp_intr().
9786182fdbdSPeter Wemm 	 */
97905bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
9804953bccaSNate Lawson 	sc->ih = NULL;
9816182fdbdSPeter Wemm 
982f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
983f7788e8eSJonathan Lemon 	fxp_release(sc);
984f7788e8eSJonathan Lemon 	return (0);
985a17c678eSDavid Greenman }
986a17c678eSDavid Greenman 
987a17c678eSDavid Greenman /*
9884a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
989a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
990a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
991a17c678eSDavid Greenman  */
9926182fdbdSPeter Wemm static int
9936182fdbdSPeter Wemm fxp_shutdown(device_t dev)
994a17c678eSDavid Greenman {
9953212724cSJohn Baldwin 
9966182fdbdSPeter Wemm 	/*
9976182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
9986182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
9996182fdbdSPeter Wemm 	 * reboot before the driver initializes.
10006182fdbdSPeter Wemm 	 */
10017137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
1002a17c678eSDavid Greenman }
1003a17c678eSDavid Greenman 
10047dced78aSDavid Greenman /*
10057dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
10067dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10077dced78aSDavid Greenman  * resume.
10087dced78aSDavid Greenman  */
10097dced78aSDavid Greenman static int
10107dced78aSDavid Greenman fxp_suspend(device_t dev)
10117dced78aSDavid Greenman {
10127dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
10137137cea0SPyun YongHyeon 	struct ifnet *ifp;
10147137cea0SPyun YongHyeon 	int pmc;
10157137cea0SPyun YongHyeon 	uint16_t pmstat;
10167dced78aSDavid Greenman 
10174953bccaSNate Lawson 	FXP_LOCK(sc);
10187dced78aSDavid Greenman 
10197137cea0SPyun YongHyeon 	ifp = sc->ifp;
10207137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
10217137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10227137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
10237137cea0SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
10247137cea0SPyun YongHyeon 			/* Request PME. */
10257137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10267137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10277137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
10287137cea0SPyun YongHyeon 			fxp_init_body(sc);
10297137cea0SPyun YongHyeon 		}
10307137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
10317137cea0SPyun YongHyeon 	}
10327dced78aSDavid Greenman 	fxp_stop(sc);
10337dced78aSDavid Greenman 
10347dced78aSDavid Greenman 	sc->suspended = 1;
10357dced78aSDavid Greenman 
10364953bccaSNate Lawson 	FXP_UNLOCK(sc);
1037f7788e8eSJonathan Lemon 	return (0);
10387dced78aSDavid Greenman }
10397dced78aSDavid Greenman 
10407dced78aSDavid Greenman /*
104167ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
10427dced78aSDavid Greenman  * appropriate.
10437dced78aSDavid Greenman  */
10447dced78aSDavid Greenman static int
10457dced78aSDavid Greenman fxp_resume(device_t dev)
10467dced78aSDavid Greenman {
10477dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
1048fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
10497137cea0SPyun YongHyeon 	int pmc;
10507137cea0SPyun YongHyeon 	uint16_t pmstat;
10517dced78aSDavid Greenman 
10524953bccaSNate Lawson 	FXP_LOCK(sc);
10537dced78aSDavid Greenman 
10547137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
10557137cea0SPyun YongHyeon 		sc->flags &= ~FXP_FLAG_WOL;
10567137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10577137cea0SPyun YongHyeon 		/* Disable PME and clear PME status. */
10587137cea0SPyun YongHyeon 		pmstat &= ~PCIM_PSTAT_PMEENABLE;
10597137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1060af75b654SPyun YongHyeon 		if ((sc->flags & FXP_FLAG_WOLCAP) != 0)
1061af75b654SPyun YongHyeon 			CSR_WRITE_1(sc, FXP_CSR_PMDR,
1062af75b654SPyun YongHyeon 			    CSR_READ_1(sc, FXP_CSR_PMDR));
10637137cea0SPyun YongHyeon 	}
10647137cea0SPyun YongHyeon 
10657dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
10667dced78aSDavid Greenman 	DELAY(10);
10677dced78aSDavid Greenman 
10687dced78aSDavid Greenman 	/* reinitialize interface if necessary */
10697dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
10704953bccaSNate Lawson 		fxp_init_body(sc);
10717dced78aSDavid Greenman 
10727dced78aSDavid Greenman 	sc->suspended = 0;
10737dced78aSDavid Greenman 
10744953bccaSNate Lawson 	FXP_UNLOCK(sc);
1075ba8c6fd5SDavid Greenman 	return (0);
1076f7788e8eSJonathan Lemon }
1077ba8c6fd5SDavid Greenman 
107800c4116bSJonathan Lemon static void
107900c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
108000c4116bSJonathan Lemon {
108174d1ed23SMaxime Henrion 	uint16_t reg;
108200c4116bSJonathan Lemon 	int x;
108300c4116bSJonathan Lemon 
108400c4116bSJonathan Lemon 	/*
108500c4116bSJonathan Lemon 	 * Shift in data.
108600c4116bSJonathan Lemon 	 */
108700c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
108800c4116bSJonathan Lemon 		if (data & x)
108900c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
109000c4116bSJonathan Lemon 		else
109100c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
109200c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
109300c4116bSJonathan Lemon 		DELAY(1);
109400c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
109500c4116bSJonathan Lemon 		DELAY(1);
109600c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
109700c4116bSJonathan Lemon 		DELAY(1);
109800c4116bSJonathan Lemon 	}
109900c4116bSJonathan Lemon }
110000c4116bSJonathan Lemon 
1101f7788e8eSJonathan Lemon /*
1102f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1103f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1104f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1105f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1106f7788e8eSJonathan Lemon  * every 16 bits of data.
1107f7788e8eSJonathan Lemon  */
110874d1ed23SMaxime Henrion static uint16_t
1109f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1110f7788e8eSJonathan Lemon {
111174d1ed23SMaxime Henrion 	uint16_t reg, data;
1112f7788e8eSJonathan Lemon 	int x;
1113ba8c6fd5SDavid Greenman 
1114f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1115f7788e8eSJonathan Lemon 	/*
1116f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1117f7788e8eSJonathan Lemon 	 */
111800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1119f7788e8eSJonathan Lemon 	/*
1120f7788e8eSJonathan Lemon 	 * Shift in address.
1121f7788e8eSJonathan Lemon 	 */
1122f7788e8eSJonathan Lemon 	data = 0;
1123f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1124f7788e8eSJonathan Lemon 		if (offset & x)
1125f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1126f7788e8eSJonathan Lemon 		else
1127f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1128f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1129f7788e8eSJonathan Lemon 		DELAY(1);
1130f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1131f7788e8eSJonathan Lemon 		DELAY(1);
1132f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1133f7788e8eSJonathan Lemon 		DELAY(1);
1134f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1135f7788e8eSJonathan Lemon 		data++;
1136f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1137f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1138f7788e8eSJonathan Lemon 			break;
1139f7788e8eSJonathan Lemon 		}
1140f7788e8eSJonathan Lemon 	}
1141f7788e8eSJonathan Lemon 	/*
1142f7788e8eSJonathan Lemon 	 * Shift out data.
1143f7788e8eSJonathan Lemon 	 */
1144f7788e8eSJonathan Lemon 	data = 0;
1145f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1146f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1147f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1148f7788e8eSJonathan Lemon 		DELAY(1);
1149f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1150f7788e8eSJonathan Lemon 			data |= x;
1151f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1152f7788e8eSJonathan Lemon 		DELAY(1);
1153f7788e8eSJonathan Lemon 	}
1154f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1155f7788e8eSJonathan Lemon 	DELAY(1);
1156f7788e8eSJonathan Lemon 
1157f7788e8eSJonathan Lemon 	return (data);
1158ba8c6fd5SDavid Greenman }
1159ba8c6fd5SDavid Greenman 
116000c4116bSJonathan Lemon static void
116174d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
116200c4116bSJonathan Lemon {
116300c4116bSJonathan Lemon 	int i;
116400c4116bSJonathan Lemon 
116500c4116bSJonathan Lemon 	/*
116600c4116bSJonathan Lemon 	 * Erase/write enable.
116700c4116bSJonathan Lemon 	 */
116800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
116900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
117000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
117100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
117200c4116bSJonathan Lemon 	DELAY(1);
117300c4116bSJonathan Lemon 	/*
117400c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
117500c4116bSJonathan Lemon 	 */
117600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
117700c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
117800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
117900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
118000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
118100c4116bSJonathan Lemon 	DELAY(1);
118200c4116bSJonathan Lemon 	/*
118300c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
118400c4116bSJonathan Lemon 	 */
118500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
118600c4116bSJonathan Lemon 	DELAY(1);
118700c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
118800c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
118900c4116bSJonathan Lemon 			break;
119000c4116bSJonathan Lemon 		DELAY(50);
119100c4116bSJonathan Lemon 	}
119200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
119300c4116bSJonathan Lemon 	DELAY(1);
119400c4116bSJonathan Lemon 	/*
119500c4116bSJonathan Lemon 	 * Erase/write disable.
119600c4116bSJonathan Lemon 	 */
119700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
119800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
119900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
120000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
120100c4116bSJonathan Lemon 	DELAY(1);
120200c4116bSJonathan Lemon }
120300c4116bSJonathan Lemon 
1204ba8c6fd5SDavid Greenman /*
1205e9bf2fa7SDavid Greenman  * From NetBSD:
1206e9bf2fa7SDavid Greenman  *
1207e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1208e9bf2fa7SDavid Greenman  *
1209e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1210e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1211e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1212e9bf2fa7SDavid Greenman  *
1213e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1214e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1215e9bf2fa7SDavid Greenman  *
1216e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1217e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1218e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1219e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1220e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1221e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1222e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1223e9bf2fa7SDavid Greenman  */
1224e9bf2fa7SDavid Greenman static void
1225f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1226e9bf2fa7SDavid Greenman {
1227e9bf2fa7SDavid Greenman 
1228f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1229f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1230f7788e8eSJonathan Lemon 
1231f7788e8eSJonathan Lemon 	/* autosize */
1232f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1233e9bf2fa7SDavid Greenman }
1234f7788e8eSJonathan Lemon 
1235ba8c6fd5SDavid Greenman static void
1236f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1237ba8c6fd5SDavid Greenman {
1238f7788e8eSJonathan Lemon 	int i;
1239ba8c6fd5SDavid Greenman 
1240f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1241f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1242ba8c6fd5SDavid Greenman }
1243ba8c6fd5SDavid Greenman 
124400c4116bSJonathan Lemon static void
124500c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
124600c4116bSJonathan Lemon {
124700c4116bSJonathan Lemon 	int i;
124800c4116bSJonathan Lemon 
124900c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
125000c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
125100c4116bSJonathan Lemon }
125200c4116bSJonathan Lemon 
1253a17c678eSDavid Greenman /*
12544953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1255a17c678eSDavid Greenman  */
1256a17c678eSDavid Greenman static void
1257f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1258a17c678eSDavid Greenman {
12599b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
12604953bccaSNate Lawson 
12614953bccaSNate Lawson 	FXP_LOCK(sc);
12624953bccaSNate Lawson 	fxp_start_body(ifp);
12634953bccaSNate Lawson 	FXP_UNLOCK(sc);
12644953bccaSNate Lawson }
12654953bccaSNate Lawson 
12664953bccaSNate Lawson /*
12674953bccaSNate Lawson  * Start packet transmission on the interface.
12684953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
12694953bccaSNate Lawson  * internal entry point only.
12704953bccaSNate Lawson  */
12714953bccaSNate Lawson static void
12724953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
12734953bccaSNate Lawson {
12744953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1275b2badf02SMaxime Henrion 	struct mbuf *mb_head;
12764e53f837SPyun YongHyeon 	int txqueued;
1277a17c678eSDavid Greenman 
127867fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
127940c20505SMaxime Henrion 
1280a17c678eSDavid Greenman 	/*
1281483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1282483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1283483b9871SDavid Greenman 	 * of the command chain).
1284a17c678eSDavid Greenman 	 */
128540c20505SMaxime Henrion 	if (sc->need_mcsetup)
1286a17c678eSDavid Greenman 		return;
1287483b9871SDavid Greenman 
12884e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
12894e53f837SPyun YongHyeon 		fxp_txeof(sc);
1290483b9871SDavid Greenman 	/*
1291483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1292483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
12933114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
12943114fdb4SDavid Greenman 	 *       a NOP command when needed.
1295483b9871SDavid Greenman 	 */
129640c20505SMaxime Henrion 	txqueued = 0;
12977929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
12987929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1299483b9871SDavid Greenman 
1300dfe61cf1SDavid Greenman 		/*
1301dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1302dfe61cf1SDavid Greenman 		 */
13037929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
13047929aa03SMax Laier 		if (mb_head == NULL)
13057929aa03SMax Laier 			break;
1306a17c678eSDavid Greenman 
13074e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13084e53f837SPyun YongHyeon 			if (mb_head == NULL)
130940c20505SMaxime Henrion 				break;
13104e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
13114e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
131240c20505SMaxime Henrion 		}
13134e53f837SPyun YongHyeon 		txqueued++;
13144e53f837SPyun YongHyeon 		/*
13154e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13164e53f837SPyun YongHyeon 		 */
13174e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
13184e53f837SPyun YongHyeon 	}
131940c20505SMaxime Henrion 
132040c20505SMaxime Henrion 	/*
132140c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
132240c20505SMaxime Henrion 	 * going again if suspended.
132340c20505SMaxime Henrion 	 */
13244e53f837SPyun YongHyeon 	if (txqueued > 0) {
13254e53f837SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
132640c20505SMaxime Henrion 		fxp_scb_wait(sc);
132740c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13284e53f837SPyun YongHyeon 		/*
13294e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
13304e53f837SPyun YongHyeon 		 * from the card again.
13314e53f837SPyun YongHyeon 		 */
13324e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
133340c20505SMaxime Henrion 	}
133440c20505SMaxime Henrion }
133540c20505SMaxime Henrion 
133640c20505SMaxime Henrion static int
13374e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
133840c20505SMaxime Henrion {
133940c20505SMaxime Henrion 	struct ifnet *ifp;
134040c20505SMaxime Henrion 	struct mbuf *m;
134140c20505SMaxime Henrion 	struct fxp_tx *txp;
134240c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1343c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
134440c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1345c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
134640c20505SMaxime Henrion 
134740c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1348fc74a9f9SBrooks Davis 	ifp = sc->ifp;
134940c20505SMaxime Henrion 
1350c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1351c21e84e4SPyun YongHyeon 	tcp = NULL;
1352dfe61cf1SDavid Greenman 	/*
1353483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1354dfe61cf1SDavid Greenman 	 */
1355b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1356c8bca6dcSBill Paul 
1357c8bca6dcSBill Paul 	/*
1358a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1359a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1360a35e7eaaSDon Lewis 	 * Developer Manual says:
1361a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1362a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1363a35e7eaaSDon Lewis 	 *   ...
1364a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1365a35e7eaaSDon Lewis 	 *   be used.
1366a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1367a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1368a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1369a35e7eaaSDon Lewis 	 */
1370a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1371a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1372a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1373a35e7eaaSDon Lewis 
13744e53f837SPyun YongHyeon 	m = *m_head;
1375a35e7eaaSDon Lewis 	/*
1376c8bca6dcSBill Paul 	 * Deal with TCP/IP checksum offload. Note that
1377c8bca6dcSBill Paul 	 * in order for TCP checksum offload to work,
1378c8bca6dcSBill Paul 	 * the pseudo header checksum must have already
1379c8bca6dcSBill Paul 	 * been computed and stored in the checksum field
1380c8bca6dcSBill Paul 	 * in the TCP header. The stack should have
1381c8bca6dcSBill Paul 	 * already done this for us.
1382c8bca6dcSBill Paul 	 */
1383c583cc04SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
1384c583cc04SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
13854e53f837SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
1386c583cc04SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
138740c20505SMaxime Henrion 
1388c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
1389c8bca6dcSBill Paul 		/*
1390c8bca6dcSBill Paul 		 * XXX The 82550 chip appears to have trouble
1391c8bca6dcSBill Paul 		 * dealing with IP header checksums in very small
1392c8bca6dcSBill Paul 		 * datagrams, namely fragments from 1 to 3 bytes
1393c8bca6dcSBill Paul 		 * in size. For example, say you want to transmit
1394c8bca6dcSBill Paul 		 * a UDP packet of 1473 bytes. The packet will be
1395c8bca6dcSBill Paul 		 * fragmented over two IP datagrams, the latter
1396c8bca6dcSBill Paul 		 * containing only one byte of data. The 82550 will
1397c8bca6dcSBill Paul 		 * botch the header checksum on the 1-byte fragment.
1398c8bca6dcSBill Paul 		 * As long as the datagram contains 4 or more bytes
1399c8bca6dcSBill Paul 		 * of data, you're ok.
1400c8bca6dcSBill Paul 		 *
1401c8bca6dcSBill Paul                  * The following code attempts to work around this
1402c8bca6dcSBill Paul 		 * problem: if the datagram is less than 38 bytes
1403c8bca6dcSBill Paul 		 * in size (14 bytes ether header, 20 bytes IP header,
1404c8bca6dcSBill Paul 		 * plus 4 bytes of data), we punt and compute the IP
1405c8bca6dcSBill Paul 		 * header checksum by hand. This workaround doesn't
1406c8bca6dcSBill Paul 		 * work very well, however, since it can be fooled
1407c8bca6dcSBill Paul 		 * by things like VLAN tags and IP options that make
1408c8bca6dcSBill Paul 		 * the header sizes/offsets vary.
1409c8bca6dcSBill Paul 		 */
1410c8bca6dcSBill Paul 
14114e53f837SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
14124e53f837SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
1413c8bca6dcSBill Paul 				struct ip *ip;
14144e53f837SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
14154e53f837SPyun YongHyeon 				ip = mtod(m, struct ip *);
14164e53f837SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
14174e53f837SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
1418c583cc04SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
1419c8bca6dcSBill Paul 			} else {
1420b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_activation_high =
1421c8bca6dcSBill Paul 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
1422b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_schedule |=
1423c8bca6dcSBill Paul 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
1424c8bca6dcSBill Paul 			}
1425c8bca6dcSBill Paul 		}
1426c8bca6dcSBill Paul #endif
1427c8bca6dcSBill Paul 	}
1428c8bca6dcSBill Paul 
1429c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1430c21e84e4SPyun YongHyeon 		/*
1431c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1432c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1433c21e84e4SPyun YongHyeon 		 */
1434c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1435c21e84e4SPyun YongHyeon 		struct ip *ip;
1436c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1437c21e84e4SPyun YongHyeon 
1438c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1439c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1440c21e84e4SPyun YongHyeon 			m = m_dup(*m_head, M_DONTWAIT);
1441c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1442c21e84e4SPyun YongHyeon 			if (m == NULL) {
1443c21e84e4SPyun YongHyeon 				*m_head = NULL;
1444c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1445c21e84e4SPyun YongHyeon 			}
1446c21e84e4SPyun YongHyeon 			*m_head = m;
1447c21e84e4SPyun YongHyeon 		}
1448c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1449c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1450c21e84e4SPyun YongHyeon 		if (m == NULL) {
1451c21e84e4SPyun YongHyeon 			*m_head = NULL;
1452c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1453c21e84e4SPyun YongHyeon 		}
1454c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1455c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1456c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1457c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1458c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1459c21e84e4SPyun YongHyeon 			if (m == NULL) {
1460c21e84e4SPyun YongHyeon 				*m_head = NULL;
1461c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1462c21e84e4SPyun YongHyeon 			}
1463c21e84e4SPyun YongHyeon 		}
1464c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1465c21e84e4SPyun YongHyeon 		if (m == NULL) {
1466c21e84e4SPyun YongHyeon 			*m_head = NULL;
1467c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1468c21e84e4SPyun YongHyeon 		}
1469c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1470c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1471c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1472c21e84e4SPyun YongHyeon 		if (m == NULL) {
1473c21e84e4SPyun YongHyeon 			*m_head = NULL;
1474c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1475c21e84e4SPyun YongHyeon 		}
1476c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1477c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr) + tcp->th_off);
1478c21e84e4SPyun YongHyeon 		if (m == NULL) {
1479c21e84e4SPyun YongHyeon 			*m_head = NULL;
1480c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1481c21e84e4SPyun YongHyeon 		}
1482c21e84e4SPyun YongHyeon 
1483c21e84e4SPyun YongHyeon 		/*
1484c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1485c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1486c21e84e4SPyun YongHyeon 		 */
1487c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
14880685c824SPyun YongHyeon 		ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) +
14890685c824SPyun YongHyeon 		    (tcp->th_off << 2));
1490c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1491c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1492c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1493c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1494c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1495c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1496c21e84e4SPyun YongHyeon 		*m_head = m;
1497c21e84e4SPyun YongHyeon 	}
1498c21e84e4SPyun YongHyeon 
14994e53f837SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_mtag, txp->tx_map, *m_head,
15004e53f837SPyun YongHyeon 	    segs, &nseg, 0);
15014e53f837SPyun YongHyeon 	if (error == EFBIG) {
15024e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
15034e53f837SPyun YongHyeon 		if (m == NULL) {
15044e53f837SPyun YongHyeon 			m_freem(*m_head);
15054e53f837SPyun YongHyeon 			*m_head = NULL;
15064e53f837SPyun YongHyeon 			return (ENOMEM);
15071104779bSMike Silbersack 		}
15084e53f837SPyun YongHyeon 		*m_head = m;
150940c20505SMaxime Henrion 		error = bus_dmamap_load_mbuf_sg(sc->fxp_mtag, txp->tx_map,
15104e53f837SPyun YongHyeon 	    	    *m_head, segs, &nseg, 0);
15114e53f837SPyun YongHyeon 		if (error != 0) {
15124e53f837SPyun YongHyeon 			m_freem(*m_head);
15134e53f837SPyun YongHyeon 			*m_head = NULL;
15144e53f837SPyun YongHyeon 			return (ENOMEM);
15154e53f837SPyun YongHyeon 		}
15164e53f837SPyun YongHyeon 	} else if (error != 0)
15174e53f837SPyun YongHyeon 		return (error);
15184e53f837SPyun YongHyeon 	if (nseg == 0) {
15194e53f837SPyun YongHyeon 		m_freem(*m_head);
15204e53f837SPyun YongHyeon 		*m_head = NULL;
15214e53f837SPyun YongHyeon 		return (EIO);
152223a0ed7cSDavid Greenman 	}
152323a0ed7cSDavid Greenman 
152440c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
15254e53f837SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1526b2badf02SMaxime Henrion 
152740c20505SMaxime Henrion 	cbp = txp->tx_cb;
152840c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
152940c20505SMaxime Henrion 		/*
153040c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
153140c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
153240c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
153340c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
153440c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
153540c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
153640c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
153740c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
153840c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
153940c20505SMaxime Henrion 		 * starting from a different place depending on whether
154040c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
154140c20505SMaxime Henrion 		 */
154240c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
154368f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
154468f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
154540c20505SMaxime Henrion 		} else {
154640c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
154740c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
154840c20505SMaxime Henrion 		}
154940c20505SMaxime Henrion 	}
1550c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1551c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1552c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
155368f4ab9aSPyun YongHyeon 		cbp->tbd[nseg].tb_size |= htole32(0x8000);
1554c21e84e4SPyun YongHyeon 	} else
155540c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1556c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1557c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1558c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
155968f4ab9aSPyun YongHyeon 		cbp->tbd[1].tb_size |= htole32(tcp_payload << 16);
1560c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1561c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1562c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1563c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1564c21e84e4SPyun YongHyeon 	}
1565bd4fa9d9SPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
1566bd4fa9d9SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
1567bd4fa9d9SPyun YongHyeon 		cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag);
1568bd4fa9d9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_activation_high |=
1569bd4fa9d9SPyun YongHyeon 		    FXP_IPCB_INSERTVLAN_ENABLE;
1570bd4fa9d9SPyun YongHyeon 	}
157140c20505SMaxime Henrion 
15724e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1573b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1574b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
15754e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1576b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
157783e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
157883e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
15794e53f837SPyun YongHyeon 	else
1580b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
158183e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
158283e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1583c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1584b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1585a17c678eSDavid Greenman 
1586a17c678eSDavid Greenman 	/*
1587483b9871SDavid Greenman 	 * Advance the end of list forward.
1588a17c678eSDavid Greenman 	 */
158906175228SAndrew Gallatin 
159050d81222SMaxime Henrion #ifdef __alpha__
159106175228SAndrew Gallatin 	/*
159206175228SAndrew Gallatin 	 * On platforms which can't access memory in 16-bit
159306175228SAndrew Gallatin 	 * granularities, we must prevent the card from DMA'ing
159406175228SAndrew Gallatin 	 * up the status while we update the command field.
159506175228SAndrew Gallatin 	 * This could cause us to overwrite the completion status.
159614fd1071SMaxime Henrion 	 * XXX This is probably bogus and we're _not_ looking
159714fd1071SMaxime Henrion 	 * for atomicity here.
159806175228SAndrew Gallatin 	 */
159914fd1071SMaxime Henrion 	atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1600bafb64afSMaxime Henrion 	    htole16(FXP_CB_COMMAND_S));
160150d81222SMaxime Henrion #else
160240c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
160350d81222SMaxime Henrion #endif /*__alpha__*/
1604b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1605a17c678eSDavid Greenman 
1606a17c678eSDavid Greenman 	/*
16071cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1608b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1609483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1610a17c678eSDavid Greenman 	 */
16111cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1612b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1613a17c678eSDavid Greenman 
16141cd443acSDavid Greenman 	sc->tx_queued++;
16151cd443acSDavid Greenman 
161640c20505SMaxime Henrion 	return (0);
1617a17c678eSDavid Greenman }
1618a17c678eSDavid Greenman 
1619e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1620e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1621e4fc250cSLuigi Rizzo 
1622e4fc250cSLuigi Rizzo static void
1623e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1624e4fc250cSLuigi Rizzo {
1625e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
162674d1ed23SMaxime Henrion 	uint8_t statack;
1627e4fc250cSLuigi Rizzo 
16284953bccaSNate Lawson 	FXP_LOCK(sc);
162940929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
16304953bccaSNate Lawson 		FXP_UNLOCK(sc);
1631e4fc250cSLuigi Rizzo 		return;
1632e4fc250cSLuigi Rizzo 	}
163340929967SGleb Smirnoff 
1634e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1635e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1636e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
163774d1ed23SMaxime Henrion 		uint8_t tmp;
16386481f301SPeter Wemm 
1639e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16404953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16414953bccaSNate Lawson 			FXP_UNLOCK(sc);
1642e4fc250cSLuigi Rizzo 			return; /* nothing to do */
16434953bccaSNate Lawson 		}
1644e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1645e4fc250cSLuigi Rizzo 		/* ack what we can */
1646e4fc250cSLuigi Rizzo 		if (tmp != 0)
1647e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1648e4fc250cSLuigi Rizzo 		statack |= tmp;
1649e4fc250cSLuigi Rizzo 	}
16504953bccaSNate Lawson 	fxp_intr_body(sc, ifp, statack, count);
16514953bccaSNate Lawson 	FXP_UNLOCK(sc);
1652e4fc250cSLuigi Rizzo }
1653e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1654e4fc250cSLuigi Rizzo 
1655a17c678eSDavid Greenman /*
16569c7d2607SDavid Greenman  * Process interface interrupts.
1657a17c678eSDavid Greenman  */
165894927790SDavid Greenman static void
1659f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1660a17c678eSDavid Greenman {
1661f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1662fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
166374d1ed23SMaxime Henrion 	uint8_t statack;
16640f4dc94cSChuck Paterson 
16654953bccaSNate Lawson 	FXP_LOCK(sc);
1666704d1965SWarner Losh 	if (sc->suspended) {
1667704d1965SWarner Losh 		FXP_UNLOCK(sc);
1668704d1965SWarner Losh 		return;
1669704d1965SWarner Losh 	}
1670704d1965SWarner Losh 
1671e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
167240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
16734953bccaSNate Lawson 		FXP_UNLOCK(sc);
1674e4fc250cSLuigi Rizzo 		return;
16754953bccaSNate Lawson 	}
1676e4fc250cSLuigi Rizzo #endif
1677b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1678a17c678eSDavid Greenman 		/*
167911457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
168011457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
168111457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
168211457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
168311457bbfSJonathan Lemon 		 */
16844953bccaSNate Lawson 		if (statack == 0xff) {
16854953bccaSNate Lawson 			FXP_UNLOCK(sc);
168611457bbfSJonathan Lemon 			return;
16874953bccaSNate Lawson 		}
168811457bbfSJonathan Lemon 
168911457bbfSJonathan Lemon 		/*
1690a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1691a17c678eSDavid Greenman 		 */
1692ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
16934953bccaSNate Lawson 		fxp_intr_body(sc, ifp, statack, -1);
1694e4fc250cSLuigi Rizzo 	}
16954953bccaSNate Lawson 	FXP_UNLOCK(sc);
1696e4fc250cSLuigi Rizzo }
1697e4fc250cSLuigi Rizzo 
1698e4fc250cSLuigi Rizzo static void
1699b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1700b2badf02SMaxime Henrion {
17014e53f837SPyun YongHyeon 	struct ifnet *ifp;
1702b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1703b2badf02SMaxime Henrion 
17044e53f837SPyun YongHyeon 	ifp = sc->ifp;
1705b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1706b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
170783e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1708b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1709b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1710b2badf02SMaxime Henrion 			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1711b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1712b2badf02SMaxime Henrion 			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1713b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1714b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1715b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1716b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1717b2badf02SMaxime Henrion 		}
1718b2badf02SMaxime Henrion 		sc->tx_queued--;
17194e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1720b2badf02SMaxime Henrion 	}
1721b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1722b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
172325935344SPyun YongHyeon 	if (sc->tx_queued == 0) {
172425935344SPyun YongHyeon 		sc->watchdog_timer = 0;
172525935344SPyun YongHyeon 		if (sc->need_mcsetup)
172625935344SPyun YongHyeon 			fxp_mc_setup(sc);
172725935344SPyun YongHyeon 	}
1728b2badf02SMaxime Henrion }
1729b2badf02SMaxime Henrion 
1730b2badf02SMaxime Henrion static void
1731f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m,
1732f13075afSPyun YongHyeon     uint16_t status, int pos)
1733f13075afSPyun YongHyeon {
1734f13075afSPyun YongHyeon 	struct ether_header *eh;
1735f13075afSPyun YongHyeon 	struct ip *ip;
1736f13075afSPyun YongHyeon 	struct udphdr *uh;
1737f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1738f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1739f13075afSPyun YongHyeon 
1740f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1741f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1742f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1743f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1744f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1745f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1746f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1747f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1748f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1749f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1750f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1751f13075afSPyun YongHyeon 			}
1752f13075afSPyun YongHyeon 		}
1753f13075afSPyun YongHyeon 		return;
1754f13075afSPyun YongHyeon 	}
1755f13075afSPyun YongHyeon 
1756f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1757f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1758f13075afSPyun YongHyeon 		return;
1759f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1760f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1761f13075afSPyun YongHyeon 		return;
1762f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1763f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1764f13075afSPyun YongHyeon 		return;
1765f13075afSPyun YongHyeon 
1766f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1767f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1768f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1769f13075afSPyun YongHyeon 		return;
1770f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1771f13075afSPyun YongHyeon 		return;
1772f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1773f13075afSPyun YongHyeon 		return;
1774f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1775f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1776f13075afSPyun YongHyeon 
1777f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1778f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1779f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1780f13075afSPyun YongHyeon 			return;
1781f13075afSPyun YongHyeon 		break;
1782f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1783f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1784f13075afSPyun YongHyeon 			return;
1785f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1786f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1787f13075afSPyun YongHyeon 			return; /* no checksum */
1788f13075afSPyun YongHyeon 		break;
1789f13075afSPyun YongHyeon 	default:
1790f13075afSPyun YongHyeon 		return;
1791f13075afSPyun YongHyeon 	}
1792f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1793f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1794f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1795f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1796f13075afSPyun YongHyeon 	if (len > 0) {
1797f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1798f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1799f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1800f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1801f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1802f13075afSPyun YongHyeon 		}
1803f13075afSPyun YongHyeon 	}
1804f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1805f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1806f13075afSPyun YongHyeon }
1807f13075afSPyun YongHyeon 
1808f13075afSPyun YongHyeon static void
180974d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
18104953bccaSNate Lawson     int count)
1811e4fc250cSLuigi Rizzo {
18122b5989e9SLuigi Rizzo 	struct mbuf *m;
1813b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18142b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18152b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
181660bb79ebSPyun YongHyeon 	uint16_t status;
18172b5989e9SLuigi Rizzo 
181867fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18192b5989e9SLuigi Rizzo 	if (rnr)
18200f1db1d6SMaxime Henrion 		sc->rnr++;
1821947e3815SIan Dowse #ifdef DEVICE_POLLING
1822947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1823947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1824947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1825947e3815SIan Dowse 		rnr = 1;
1826947e3815SIan Dowse 	}
1827947e3815SIan Dowse #endif
1828a17c678eSDavid Greenman 
1829a17c678eSDavid Greenman 	/*
18303114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
183106936301SBill Paul 	 *
183206936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
183306936301SBill Paul 	 * be that this event (control unit not ready) was not
183406936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
183506936301SBill Paul 	 * The exact sequence of events that occur when the interface
183606936301SBill Paul 	 * is brought up are different now, and if this event
183706936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
183806936301SBill Paul 	 * can stall for several seconds. The result is that no
183906936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
184006936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
18413114fdb4SDavid Greenman 	 */
18424e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1843b2badf02SMaxime Henrion 		fxp_txeof(sc);
18443114fdb4SDavid Greenman 
18453114fdb4SDavid Greenman 	/*
18463114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
18473114fdb4SDavid Greenman 	 */
18487929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
18494953bccaSNate Lawson 		fxp_start_body(ifp);
18502b5989e9SLuigi Rizzo 
18512b5989e9SLuigi Rizzo 	/*
18522b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
18532b5989e9SLuigi Rizzo 	 */
1854947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
18552b5989e9SLuigi Rizzo 		return;
18562b5989e9SLuigi Rizzo 
18573114fdb4SDavid Greenman 	/*
1858a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1859a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1860a17c678eSDavid Greenman 	 * re-start the receiver.
1861947e3815SIan Dowse 	 *
18622b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
18632b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
18642b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
18652b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1866947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1867947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1868a17c678eSDavid Greenman 	 */
18692b5989e9SLuigi Rizzo 	for (;;) {
1870b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1871b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1872ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1873ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1874b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1875b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1876a17c678eSDavid Greenman 
1877e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1878947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1879947e3815SIan Dowse 			if (rnr) {
1880947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1881947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1882947e3815SIan Dowse 				rnr = 0;
1883947e3815SIan Dowse 			}
18842b5989e9SLuigi Rizzo 			break;
1885947e3815SIan Dowse 		}
18862b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
18872b5989e9SLuigi Rizzo 
188860bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
188960bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
18902b5989e9SLuigi Rizzo 			break;
18912b5989e9SLuigi Rizzo 
1892dfe61cf1SDavid Greenman 		/*
1893b2badf02SMaxime Henrion 		 * Advance head forward.
1894dfe61cf1SDavid Greenman 		 */
1895b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1896a17c678eSDavid Greenman 
1897dfe61cf1SDavid Greenman 		/*
1898ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1899ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1900ba8c6fd5SDavid Greenman 		 * instead.
1901dfe61cf1SDavid Greenman 		 */
190285050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1903aed53495SDavid Greenman 			int total_len;
1904a17c678eSDavid Greenman 
1905e8c8b728SJonathan Lemon 			/*
19062b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
19072b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
19082b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
19092b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1910e8c8b728SJonathan Lemon 			 */
1911bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1912f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
1913f13075afSPyun YongHyeon 			    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1914f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1915f13075afSPyun YongHyeon 				total_len -= 2;
1916f13075afSPyun YongHyeon 			}
19172b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
19182b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
191960bb79ebSPyun YongHyeon 				sc->rfa_size || status & FXP_RFA_STATUS_CRC) {
1920e8c8b728SJonathan Lemon 				m_freem(m);
19212b5989e9SLuigi Rizzo 				continue;
1922e8c8b728SJonathan Lemon 			}
1923920b58e8SBrooks Davis 
19242e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1925673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1926673d9191SSam Leffler 
1927f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
1928f13075afSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1929f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
1930bd4fa9d9SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
1931bd4fa9d9SPyun YongHyeon 			    (status & FXP_RFA_STATUS_VLAN) != 0) {
1932bd4fa9d9SPyun YongHyeon 				m->m_pkthdr.ether_vtag =
1933bd4fa9d9SPyun YongHyeon 				    ntohs(rfa->rfax_vlan_id);
1934bd4fa9d9SPyun YongHyeon 				m->m_flags |= M_VLANTAG;
1935bd4fa9d9SPyun YongHyeon 			}
193605fb8c3fSNate Lawson 			/*
193705fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
193805fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
193905fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
194005fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
194105fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
194205fb8c3fSNate Lawson 			 * calling if_input() on each one.
194305fb8c3fSNate Lawson 			 */
194405fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
1945673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
194605fb8c3fSNate Lawson 			FXP_LOCK(sc);
194785050421SPyun YongHyeon 		} else {
194885050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
194985050421SPyun YongHyeon 			ifp->if_iqdrops++;
195085050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
1951a17c678eSDavid Greenman 		}
195285050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
1953a17c678eSDavid Greenman 	}
19542b5989e9SLuigi Rizzo 	if (rnr) {
1955ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1956ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1957b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
19582e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1959a17c678eSDavid Greenman 	}
1960a17c678eSDavid Greenman }
1961a17c678eSDavid Greenman 
1962dfe61cf1SDavid Greenman /*
1963dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1964dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1965dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1966dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1967dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1968dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1969dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1970dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1971dfe61cf1SDavid Greenman  * them again next time.
1972dfe61cf1SDavid Greenman  */
1973303b270bSEivind Eklund static void
1974f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1975a17c678eSDavid Greenman {
1976f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1977fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
1978a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1979a17c678eSDavid Greenman 
19803212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1981b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
198283e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
198383e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1984397f9dfeSDavid Greenman 	if (sp->rx_good) {
198583e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
1986397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1987397f9dfeSDavid Greenman 	} else {
1988c8cc6fcaSDavid Greenman 		/*
1989c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1990c8cc6fcaSDavid Greenman 		 */
1991397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1992397f9dfeSDavid Greenman 	}
19933ba65732SDavid Greenman 	ifp->if_ierrors +=
199483e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
199583e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
199683e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
199783e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
1998a17c678eSDavid Greenman 	/*
1999f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
2000f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
2001f9be9005SDavid Greenman 	 */
2002f9be9005SDavid Greenman 	if (sp->tx_underruns) {
200383e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
2004f9be9005SDavid Greenman 		if (tx_threshold < 192)
2005f9be9005SDavid Greenman 			tx_threshold += 64;
2006f9be9005SDavid Greenman 	}
20074953bccaSNate Lawson 
2008397f9dfeSDavid Greenman 	/*
2009c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
2010c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
2011c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
2012c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
2013c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
2014c8cc6fcaSDavid Greenman 	 */
2015b2badf02SMaxime Henrion 	fxp_txeof(sc);
2016b2badf02SMaxime Henrion 
2017c8cc6fcaSDavid Greenman 	/*
2018397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
2019397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2020397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2021397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2022397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
2023397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2024397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2025397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2026397f9dfeSDavid Greenman 	 */
2027397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2028397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
2029397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
2030397f9dfeSDavid Greenman 	}
2031f9be9005SDavid Greenman 	/*
20323ba65732SDavid Greenman 	 * If there is no pending command, start another stats
20333ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2034a17c678eSDavid Greenman 	 */
2035397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2036a17c678eSDavid Greenman 		/*
2037397f9dfeSDavid Greenman 		 * Start another stats dump.
2038a17c678eSDavid Greenman 		 */
2039b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
2040b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
20412e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2042dfe61cf1SDavid Greenman 	} else {
2043dfe61cf1SDavid Greenman 		/*
2044dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
2045dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
20463ba65732SDavid Greenman 		 * next timer event to update them.
2047dfe61cf1SDavid Greenman 		 */
2048dfe61cf1SDavid Greenman 		sp->tx_good = 0;
2049f9be9005SDavid Greenman 		sp->tx_underruns = 0;
2050dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
20513ba65732SDavid Greenman 
2052dfe61cf1SDavid Greenman 		sp->rx_good = 0;
20533ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
20543ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
20553ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
20563ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
2057dfe61cf1SDavid Greenman 	}
2058f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2059f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
20604953bccaSNate Lawson 
2061a17c678eSDavid Greenman 	/*
206216f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2063df79d527SGleb Smirnoff 	 */
2064df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2065df79d527SGleb Smirnoff 
2066df79d527SGleb Smirnoff 	/*
2067a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2068a17c678eSDavid Greenman 	 */
206945276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2070a17c678eSDavid Greenman }
2071a17c678eSDavid Greenman 
2072a17c678eSDavid Greenman /*
2073a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2074a17c678eSDavid Greenman  * the interface.
2075a17c678eSDavid Greenman  */
2076a17c678eSDavid Greenman static void
2077f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2078a17c678eSDavid Greenman {
2079fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2080b2badf02SMaxime Henrion 	struct fxp_tx *txp;
20813ba65732SDavid Greenman 	int i;
2082a17c678eSDavid Greenman 
208313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2084df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
20857dced78aSDavid Greenman 
2086a17c678eSDavid Greenman 	/*
2087a17c678eSDavid Greenman 	 * Cancel stats updater.
2088a17c678eSDavid Greenman 	 */
208945276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
20903ba65732SDavid Greenman 
20913ba65732SDavid Greenman 	/*
20927137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
20937137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
20943ba65732SDavid Greenman 	 */
20957137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
209672a32a26SJonathan Lemon 	DELAY(50);
20977137cea0SPyun YongHyeon 	/* Disable interrupts. */
20987137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2099a17c678eSDavid Greenman 
21003ba65732SDavid Greenman 	/*
21013ba65732SDavid Greenman 	 * Release any xmit buffers.
21023ba65732SDavid Greenman 	 */
2103b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2104da91462dSDavid Greenman 	if (txp != NULL) {
2105da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
2106b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
2107b2badf02SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
2108b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
2109b2badf02SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
2110b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
2111b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
2112c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
2113b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
2114da91462dSDavid Greenman 			}
2115da91462dSDavid Greenman 		}
21163ba65732SDavid Greenman 	}
2117b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
21183ba65732SDavid Greenman 	sc->tx_queued = 0;
2119a17c678eSDavid Greenman }
2120a17c678eSDavid Greenman 
2121a17c678eSDavid Greenman /*
2122a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2123a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2124a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2125a17c678eSDavid Greenman  * card has wedged for some reason.
2126a17c678eSDavid Greenman  */
2127a17c678eSDavid Greenman static void
2128df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2129a17c678eSDavid Greenman {
2130ba8c6fd5SDavid Greenman 
2131df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2132df79d527SGleb Smirnoff 
2133df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2134df79d527SGleb Smirnoff 		return;
2135df79d527SGleb Smirnoff 
2136f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2137df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
2138a17c678eSDavid Greenman 
21394953bccaSNate Lawson 	fxp_init_body(sc);
2140a17c678eSDavid Greenman }
2141a17c678eSDavid Greenman 
21424953bccaSNate Lawson /*
21434953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
21444953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
21454953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
21464953bccaSNate Lawson  */
2147a17c678eSDavid Greenman static void
2148f7788e8eSJonathan Lemon fxp_init(void *xsc)
2149a17c678eSDavid Greenman {
2150fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
21514953bccaSNate Lawson 
21524953bccaSNate Lawson 	FXP_LOCK(sc);
21534953bccaSNate Lawson 	fxp_init_body(sc);
21544953bccaSNate Lawson 	FXP_UNLOCK(sc);
21554953bccaSNate Lawson }
21564953bccaSNate Lawson 
21574953bccaSNate Lawson /*
21584953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
21594953bccaSNate Lawson  * softc lock held.
21604953bccaSNate Lawson  */
21614953bccaSNate Lawson static void
21624953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
21634953bccaSNate Lawson {
2164fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2165a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2166a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2167b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2168b2badf02SMaxime Henrion 	struct fxp_tx *txp;
216909882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
21703212724cSJohn Baldwin 	int i, prm;
2171a17c678eSDavid Greenman 
217267fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2173a17c678eSDavid Greenman 	/*
21743ba65732SDavid Greenman 	 * Cancel any pending I/O
2175a17c678eSDavid Greenman 	 */
21763ba65732SDavid Greenman 	fxp_stop(sc);
2177a17c678eSDavid Greenman 
21787137cea0SPyun YongHyeon 	/*
21797137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
21807137cea0SPyun YongHyeon 	 */
21817137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
21827137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
21837137cea0SPyun YongHyeon 	DELAY(50);
21847137cea0SPyun YongHyeon 
2185a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
2186a17c678eSDavid Greenman 
2187a17c678eSDavid Greenman 	/*
2188a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2189a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2190a17c678eSDavid Greenman 	 */
2191ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
21922e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2193a17c678eSDavid Greenman 
2194ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
21952e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2196a17c678eSDavid Greenman 
2197a17c678eSDavid Greenman 	/*
2198a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2199a17c678eSDavid Greenman 	 */
2200ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2201b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
2202b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
22032e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2204a17c678eSDavid Greenman 
2205a17c678eSDavid Greenman 	/*
220672a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
220772a32a26SJonathan Lemon 	 */
220872a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
220972a32a26SJonathan Lemon 		fxp_load_ucode(sc);
221072a32a26SJonathan Lemon 
221172a32a26SJonathan Lemon 	/*
221209882363SJonathan Lemon 	 * Initialize the multicast address list.
221309882363SJonathan Lemon 	 */
221409882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
221509882363SJonathan Lemon 		mcsp = sc->mcsp;
221609882363SJonathan Lemon 		mcsp->cb_status = 0;
221783e6547dSMaxime Henrion 		mcsp->cb_command =
221883e6547dSMaxime Henrion 		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
221983e6547dSMaxime Henrion 		mcsp->link_addr = 0xffffffff;
222009882363SJonathan Lemon 		/*
222109882363SJonathan Lemon 	 	 * Start the multicast setup command.
222209882363SJonathan Lemon 		 */
222309882363SJonathan Lemon 		fxp_scb_wait(sc);
2224b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2225b2badf02SMaxime Henrion 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
222609882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
222709882363SJonathan Lemon 		/* ...and wait for it to complete. */
2228209b07bcSMaxime Henrion 		fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
2229b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
2230b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
223109882363SJonathan Lemon 	}
223209882363SJonathan Lemon 
223309882363SJonathan Lemon 	/*
2234a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2235a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2236a17c678eSDavid Greenman 	 * later.
2237a17c678eSDavid Greenman 	 */
2238b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2239a17c678eSDavid Greenman 
2240a17c678eSDavid Greenman 	/*
2241a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2242a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2243a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2244a17c678eSDavid Greenman 	 */
2245b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2246a17c678eSDavid Greenman 
2247a17c678eSDavid Greenman 	cbp->cb_status =	0;
224883e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
224983e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
225083e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
22512c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2252001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2253001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2254a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2255f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2256f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2257f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2258f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2259001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2260001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2261f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2262a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2263f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2264f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
22653114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2266f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2267f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2268f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
22698ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2270a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2271f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2272f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2273c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2274c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2275f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2276f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2277f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
2278f13075afSPyun YongHyeon 	    (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0;
2279f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2280f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2281f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2282f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2283a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2284a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2285a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2286a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2287a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2288a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2289a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2290a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2291f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2292f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2293f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2294f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2295f7788e8eSJonathan Lemon 
2296a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2297a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2298a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2299f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2300f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
23017137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2302a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
23033ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2304a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2305f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
2306c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2307bd4fa9d9SPyun YongHyeon 	cbp->vlan_strip_en =	((sc->flags & FXP_FLAG_EXT_RFA) != 0 &&
2308bd4fa9d9SPyun YongHyeon 	    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0;
2309a17c678eSDavid Greenman 
23100f1db1d6SMaxime Henrion 	if (sc->tunable_noflow || sc->revision == FXP_REV_82557) {
23113bd07cfdSJonathan Lemon 		/*
23123bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
23133bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
23143bd07cfdSJonathan Lemon 		 */
23153bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
23163bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
23173bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23183bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
23193bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
23203bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
23213bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
23223bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
23233bd07cfdSJonathan Lemon 	} else {
23243bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
23253bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
23263bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23273bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
23283bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
23293bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
23303bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
23313bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
23323bd07cfdSJonathan Lemon 	}
23333bd07cfdSJonathan Lemon 
2334a17c678eSDavid Greenman 	/*
2335a17c678eSDavid Greenman 	 * Start the config command/DMA.
2336a17c678eSDavid Greenman 	 */
2337ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2338b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2339b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
23402e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2341a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2342209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2343b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2344a17c678eSDavid Greenman 
2345a17c678eSDavid Greenman 	/*
2346a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2347a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2348a17c678eSDavid Greenman 	 */
2349b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2350a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
235183e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
235283e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
23534a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2354a17c678eSDavid Greenman 
2355a17c678eSDavid Greenman 	/*
2356a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2357a17c678eSDavid Greenman 	 */
2358ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2359b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
23602e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2361a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2362209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2363b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2364a17c678eSDavid Greenman 
2365a17c678eSDavid Greenman 	/*
2366a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2367a17c678eSDavid Greenman 	 */
2368b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2369b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2370b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2371a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2372b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
237383e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
237483e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
237583e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
237683e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
23773bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2378b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
237983e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
23803bd07cfdSJonathan Lemon 		else
2381b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
238283e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2383b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2384a17c678eSDavid Greenman 	}
2385a17c678eSDavid Greenman 	/*
2386397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2387a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2388a17c678eSDavid Greenman 	 */
238983e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2390b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2391b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2392397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2393a17c678eSDavid Greenman 
2394ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23952e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2396a17c678eSDavid Greenman 
2397a17c678eSDavid Greenman 	/*
2398a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2399a17c678eSDavid Greenman 	 */
2400ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2401b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
24022e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2403a17c678eSDavid Greenman 
2404dccee1a1SDavid Greenman 	/*
2405ba8c6fd5SDavid Greenman 	 * Set current media.
2406dccee1a1SDavid Greenman 	 */
2407f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2408f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2409dccee1a1SDavid Greenman 
241013f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
241113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2412e8c8b728SJonathan Lemon 
2413e8c8b728SJonathan Lemon 	/*
2414e8c8b728SJonathan Lemon 	 * Enable interrupts.
2415e8c8b728SJonathan Lemon 	 */
24162b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
24172b5989e9SLuigi Rizzo 	/*
24182b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
24192b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
24202b5989e9SLuigi Rizzo 	 */
242140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
24222b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
24232b5989e9SLuigi Rizzo 	else
24242b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2425e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2426a17c678eSDavid Greenman 
2427a17c678eSDavid Greenman 	/*
2428a17c678eSDavid Greenman 	 * Start stats updater.
2429a17c678eSDavid Greenman 	 */
243045276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2431f7788e8eSJonathan Lemon }
2432f7788e8eSJonathan Lemon 
2433f7788e8eSJonathan Lemon static int
2434f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2435f7788e8eSJonathan Lemon {
2436f7788e8eSJonathan Lemon 
2437f7788e8eSJonathan Lemon 	return (0);
2438a17c678eSDavid Greenman }
2439a17c678eSDavid Greenman 
2440303b270bSEivind Eklund static void
2441f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2442ba8c6fd5SDavid Greenman {
2443ba8c6fd5SDavid Greenman 
2444f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2445ba8c6fd5SDavid Greenman }
2446ba8c6fd5SDavid Greenman 
2447ba8c6fd5SDavid Greenman /*
2448ba8c6fd5SDavid Greenman  * Change media according to request.
2449ba8c6fd5SDavid Greenman  */
2450f7788e8eSJonathan Lemon static int
2451f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2452ba8c6fd5SDavid Greenman {
2453ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2454f7788e8eSJonathan Lemon 	struct mii_data *mii;
2455ba8c6fd5SDavid Greenman 
2456f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
24573212724cSJohn Baldwin 	FXP_LOCK(sc);
24585aa0cdf4SJohn-Mark Gurney 	if (mii->mii_instance) {
24595aa0cdf4SJohn-Mark Gurney 		struct mii_softc	*miisc;
24605aa0cdf4SJohn-Mark Gurney 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
24615aa0cdf4SJohn-Mark Gurney 			mii_phy_reset(miisc);
24625aa0cdf4SJohn-Mark Gurney 	}
2463f7788e8eSJonathan Lemon 	mii_mediachg(mii);
24643212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2465ba8c6fd5SDavid Greenman 	return (0);
2466ba8c6fd5SDavid Greenman }
2467ba8c6fd5SDavid Greenman 
2468ba8c6fd5SDavid Greenman /*
2469ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2470ba8c6fd5SDavid Greenman  */
2471f7788e8eSJonathan Lemon static void
2472f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2473ba8c6fd5SDavid Greenman {
2474ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2475f7788e8eSJonathan Lemon 	struct mii_data *mii;
2476ba8c6fd5SDavid Greenman 
2477f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
24783212724cSJohn Baldwin 	FXP_LOCK(sc);
2479f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2480f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2481f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
24822e2b8238SJonathan Lemon 
24832b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
24842b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
24852e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
24862e2b8238SJonathan Lemon 	else
24872e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
24883212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2489ba8c6fd5SDavid Greenman }
2490ba8c6fd5SDavid Greenman 
2491a17c678eSDavid Greenman /*
2492a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2493a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
249485050421SPyun YongHyeon  * reusing the RFA buffer.
2495a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2496a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2497a17c678eSDavid Greenman  */
2498a17c678eSDavid Greenman static int
249985050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2500a17c678eSDavid Greenman {
2501a17c678eSDavid Greenman 	struct mbuf *m;
250285050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2503b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
250485050421SPyun YongHyeon 	int error;
2505a17c678eSDavid Greenman 
2506a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
250785050421SPyun YongHyeon 	if (m == NULL)
250885050421SPyun YongHyeon 		return (ENOBUFS);
2509ba8c6fd5SDavid Greenman 
2510ba8c6fd5SDavid Greenman 	/*
2511ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2512ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2513ba8c6fd5SDavid Greenman 	 */
2514ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2515ba8c6fd5SDavid Greenman 
2516eadd5e3aSDavid Greenman 	/*
2517eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2518eadd5e3aSDavid Greenman 	 * data start past it.
2519eadd5e3aSDavid Greenman 	 */
2520a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2521c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
252283e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2523eadd5e3aSDavid Greenman 
2524a17c678eSDavid Greenman 	rfa->rfa_status = 0;
252583e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2526a17c678eSDavid Greenman 	rfa->actual_size = 0;
252785050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
252885050421SPyun YongHyeon 	    sc->rfa_size;
2529ba8c6fd5SDavid Greenman 
253028935f27SMaxime Henrion 	/*
253128935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
253228935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
253328935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
253428935f27SMaxime Henrion 	 * is also alignment-safe.
253528935f27SMaxime Henrion 	 */
253683e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
253783e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2538ba8c6fd5SDavid Greenman 
2539b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2540b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2541b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2542b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2543b2badf02SMaxime Henrion 	if (error) {
2544b2badf02SMaxime Henrion 		m_freem(m);
2545b2badf02SMaxime Henrion 		return (error);
2546b2badf02SMaxime Henrion 	}
2547b2badf02SMaxime Henrion 
2548e2157cf7SPyun YongHyeon 	if (rxp->rx_mbuf != NULL)
2549b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2550b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2551b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2552b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2553b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2554b2badf02SMaxime Henrion 
2555b983c7b3SMaxime Henrion 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
2556b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
255785050421SPyun YongHyeon 	return (0);
255885050421SPyun YongHyeon }
255985050421SPyun YongHyeon 
256085050421SPyun YongHyeon static void
256185050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
256285050421SPyun YongHyeon {
256385050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
256485050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2565b2badf02SMaxime Henrion 
2566dfe61cf1SDavid Greenman 	/*
2567dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2568dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2569dfe61cf1SDavid Greenman 	 */
2570b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2571b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2572b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2573b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2574b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
257583e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2576aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2577b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
25784cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2579a17c678eSDavid Greenman 	} else {
2580b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2581b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2582a17c678eSDavid Greenman 	}
2583b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
258485050421SPyun YongHyeon }
258585050421SPyun YongHyeon 
258685050421SPyun YongHyeon static void
258785050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
258885050421SPyun YongHyeon {
258985050421SPyun YongHyeon 	struct mbuf *m;
259085050421SPyun YongHyeon 	struct fxp_rfa *rfa;
259185050421SPyun YongHyeon 
259285050421SPyun YongHyeon 	m = rxp->rx_mbuf;
259385050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
259485050421SPyun YongHyeon 	/*
259585050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
259685050421SPyun YongHyeon 	 * will be 32-bit aligned.
259785050421SPyun YongHyeon 	 */
259885050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
259985050421SPyun YongHyeon 
260085050421SPyun YongHyeon 	/*
260185050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
260285050421SPyun YongHyeon 	 * data start past it.
260385050421SPyun YongHyeon 	 */
260485050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
260585050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
260685050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
260785050421SPyun YongHyeon 
260885050421SPyun YongHyeon 	rfa->rfa_status = 0;
260985050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
261085050421SPyun YongHyeon 	rfa->actual_size = 0;
261185050421SPyun YongHyeon 
261285050421SPyun YongHyeon 	/*
261385050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
261485050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
261585050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
261685050421SPyun YongHyeon 	 * is also alignment-safe.
261785050421SPyun YongHyeon 	 */
261885050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
261985050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
262085050421SPyun YongHyeon 
262185050421SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
262285050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2623a17c678eSDavid Greenman }
2624a17c678eSDavid Greenman 
2625f1928b0cSKevin Lo static int
2626f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2627dccee1a1SDavid Greenman {
2628f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2629dccee1a1SDavid Greenman 	int count = 10000;
26306ebc3153SDavid Greenman 	int value;
2631dccee1a1SDavid Greenman 
2632ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2633ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2634dccee1a1SDavid Greenman 
2635ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2636ba8c6fd5SDavid Greenman 	    && count--)
26376ebc3153SDavid Greenman 		DELAY(10);
2638dccee1a1SDavid Greenman 
2639dccee1a1SDavid Greenman 	if (count <= 0)
2640f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2641dccee1a1SDavid Greenman 
26426ebc3153SDavid Greenman 	return (value & 0xffff);
2643dccee1a1SDavid Greenman }
2644dccee1a1SDavid Greenman 
264516ec4b00SWarner Losh static int
2646f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2647dccee1a1SDavid Greenman {
2648f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2649dccee1a1SDavid Greenman 	int count = 10000;
2650dccee1a1SDavid Greenman 
2651ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2652ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2653ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2654dccee1a1SDavid Greenman 
2655ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2656ba8c6fd5SDavid Greenman 	    count--)
26576ebc3153SDavid Greenman 		DELAY(10);
2658dccee1a1SDavid Greenman 
2659dccee1a1SDavid Greenman 	if (count <= 0)
2660f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
266116ec4b00SWarner Losh 	return (0);
2662dccee1a1SDavid Greenman }
2663dccee1a1SDavid Greenman 
2664dccee1a1SDavid Greenman static int
2665f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2666a17c678eSDavid Greenman {
26679b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2668a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2669f7788e8eSJonathan Lemon 	struct mii_data *mii;
267060bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2671a17c678eSDavid Greenman 
2672a17c678eSDavid Greenman 	switch (command) {
2673a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
26743212724cSJohn Baldwin 		FXP_LOCK(sc);
2675f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2676f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2677f7788e8eSJonathan Lemon 		else
2678f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2679a17c678eSDavid Greenman 
2680a17c678eSDavid Greenman 		/*
2681a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2682a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2683a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2684a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2685a17c678eSDavid Greenman 		 */
2686a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
26874953bccaSNate Lawson 			fxp_init_body(sc);
2688a17c678eSDavid Greenman 		} else {
268913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
26904a5f1499SDavid Greenman 				fxp_stop(sc);
2691a17c678eSDavid Greenman 		}
26923212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2693a17c678eSDavid Greenman 		break;
2694a17c678eSDavid Greenman 
2695a17c678eSDavid Greenman 	case SIOCADDMULTI:
2696a17c678eSDavid Greenman 	case SIOCDELMULTI:
26973212724cSJohn Baldwin 		FXP_LOCK(sc);
2698f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2699f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2700f7788e8eSJonathan Lemon 		else
2701f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2702a17c678eSDavid Greenman 		/*
2703a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2704a17c678eSDavid Greenman 		 * accordingly.
2705a17c678eSDavid Greenman 		 */
2706f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2707397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2708397f9dfeSDavid Greenman 		/*
2709f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2710397f9dfeSDavid Greenman 		 * again rather than else {}.
2711397f9dfeSDavid Greenman 		 */
2712f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
27134953bccaSNate Lawson 			fxp_init_body(sc);
27143212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2715a17c678eSDavid Greenman 		error = 0;
2716ba8c6fd5SDavid Greenman 		break;
2717ba8c6fd5SDavid Greenman 
2718ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2719ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2720f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2721f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2722f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2723f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2724f7788e8eSJonathan Lemon 		} else {
2725ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2726f7788e8eSJonathan Lemon 		}
2727a17c678eSDavid Greenman 		break;
2728a17c678eSDavid Greenman 
2729fb917226SRuslan Ermilov 	case SIOCSIFCAP:
273060bb79ebSPyun YongHyeon 		reinit = 0;
27318ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
273240929967SGleb Smirnoff #ifdef DEVICE_POLLING
273340929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
273440929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
273540929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
273640929967SGleb Smirnoff 				if (error)
273740929967SGleb Smirnoff 					return(error);
273840929967SGleb Smirnoff 				FXP_LOCK(sc);
273940929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
274040929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
274140929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
274240929967SGleb Smirnoff 				FXP_UNLOCK(sc);
274340929967SGleb Smirnoff 			} else {
274440929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
274540929967SGleb Smirnoff 				/* Enable interrupts in any case */
274640929967SGleb Smirnoff 				FXP_LOCK(sc);
274740929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
274840929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
274940929967SGleb Smirnoff 				FXP_UNLOCK(sc);
275040929967SGleb Smirnoff 			}
275140929967SGleb Smirnoff 		}
275240929967SGleb Smirnoff #endif
275340929967SGleb Smirnoff 		FXP_LOCK(sc);
275460bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
275560bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
275660bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
275760bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
275860bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
275960bb79ebSPyun YongHyeon 			else
276060bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
276160bb79ebSPyun YongHyeon 		}
276260bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2763f13075afSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
276460bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2765f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2766f13075afSPyun YongHyeon 				reinit++;
2767f13075afSPyun YongHyeon 		}
2768c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
2769c21e84e4SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2770c21e84e4SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2771c21e84e4SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
2772c21e84e4SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2773c21e84e4SPyun YongHyeon 			else
2774c21e84e4SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2775c21e84e4SPyun YongHyeon 		}
27767137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
27777137cea0SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
27787137cea0SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
277960bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
278060bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
27818ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
27828ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
27838ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
27848ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
27858ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
27868ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
27878ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
278860bb79ebSPyun YongHyeon 				reinit++;
278960bb79ebSPyun YongHyeon 		}
2790bd4fa9d9SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2791bd4fa9d9SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2792bd4fa9d9SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2793bd4fa9d9SPyun YongHyeon 				reinit++;
2794bd4fa9d9SPyun YongHyeon 		}
2795bd4fa9d9SPyun YongHyeon 		if (reinit > 0 && ifp->if_flags & IFF_UP)
27968ef1f631SYaroslav Tykhiy 			fxp_init_body(sc);
27973212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2798bd4fa9d9SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
2799fb917226SRuslan Ermilov 		break;
2800fb917226SRuslan Ermilov 
2801a17c678eSDavid Greenman 	default:
2802673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2803a17c678eSDavid Greenman 	}
2804a17c678eSDavid Greenman 	return (error);
2805a17c678eSDavid Greenman }
2806397f9dfeSDavid Greenman 
2807397f9dfeSDavid Greenman /*
280809882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
280909882363SJonathan Lemon  */
281009882363SJonathan Lemon static int
281109882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
281209882363SJonathan Lemon {
281309882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2814fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
281509882363SJonathan Lemon 	struct ifmultiaddr *ifma;
281609882363SJonathan Lemon 	int nmcasts;
281709882363SJonathan Lemon 
281809882363SJonathan Lemon 	nmcasts = 0;
281909882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
282013b203d0SRobert Watson 		IF_ADDR_LOCK(ifp);
282109882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
282209882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
282309882363SJonathan Lemon 				continue;
282409882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
282509882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
282609882363SJonathan Lemon 				nmcasts = 0;
282709882363SJonathan Lemon 				break;
282809882363SJonathan Lemon 			}
282909882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2830bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
283109882363SJonathan Lemon 			nmcasts++;
283209882363SJonathan Lemon 		}
283313b203d0SRobert Watson 		IF_ADDR_UNLOCK(ifp);
283409882363SJonathan Lemon 	}
2835bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
283609882363SJonathan Lemon 	return (nmcasts);
283709882363SJonathan Lemon }
283809882363SJonathan Lemon 
283909882363SJonathan Lemon /*
2840397f9dfeSDavid Greenman  * Program the multicast filter.
2841397f9dfeSDavid Greenman  *
2842397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2843397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
28443114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2845397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2846dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2847397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2848397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2849397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2850397f9dfeSDavid Greenman  *
2851397f9dfeSDavid Greenman  * This function must be called at splimp.
2852397f9dfeSDavid Greenman  */
2853397f9dfeSDavid Greenman static void
2854f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2855397f9dfeSDavid Greenman {
2856397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2857b2badf02SMaxime Henrion 	struct fxp_tx *txp;
28587dced78aSDavid Greenman 	int count;
2859397f9dfeSDavid Greenman 
286067fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
28613114fdb4SDavid Greenman 	/*
28623114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
28633114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
28643114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
28653114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
28663114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
28673114fdb4SDavid Greenman 	 */
2868397f9dfeSDavid Greenman 	if (sc->tx_queued) {
28693114fdb4SDavid Greenman 		/*
28703114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
28713114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
28723114fdb4SDavid Greenman 		 */
28733114fdb4SDavid Greenman 		if (sc->need_mcsetup)
28743114fdb4SDavid Greenman 			return;
2875397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
28763114fdb4SDavid Greenman 
28773114fdb4SDavid Greenman 		/*
287872a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
287972a32a26SJonathan Lemon 		 * when all TX commands have been processed.
28803114fdb4SDavid Greenman 		 */
2881b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
2882b2badf02SMaxime Henrion 		txp->tx_mbuf = NULL;
2883b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
288483e6547dSMaxime Henrion 		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
288583e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
28863114fdb4SDavid Greenman 		/*
28873114fdb4SDavid Greenman 		 * Advance the end of list forward.
28883114fdb4SDavid Greenman 		 */
288983e6547dSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
289083e6547dSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
28915f361cbeSMaxime Henrion 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2892b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
28933114fdb4SDavid Greenman 		sc->tx_queued++;
28943114fdb4SDavid Greenman 		/*
28953114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
28963114fdb4SDavid Greenman 		 */
28973114fdb4SDavid Greenman 		fxp_scb_wait(sc);
28982e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
28993114fdb4SDavid Greenman 		/*
29003114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
29013114fdb4SDavid Greenman 		 * card again.
29023114fdb4SDavid Greenman 		 */
2903df79d527SGleb Smirnoff 		sc->watchdog_timer = 5;
29043114fdb4SDavid Greenman 
2905397f9dfeSDavid Greenman 		return;
2906397f9dfeSDavid Greenman 	}
2907397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2908397f9dfeSDavid Greenman 
2909397f9dfeSDavid Greenman 	/*
2910397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2911397f9dfeSDavid Greenman 	 */
2912397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
291383e6547dSMaxime Henrion 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
291483e6547dSMaxime Henrion 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
291583e6547dSMaxime Henrion 	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2916b2badf02SMaxime Henrion 	txp = &sc->fxp_desc.mcs_tx;
2917b2badf02SMaxime Henrion 	txp->tx_mbuf = NULL;
2918b2badf02SMaxime Henrion 	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2919b2badf02SMaxime Henrion 	txp->tx_next = sc->fxp_desc.tx_list;
292009882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2921b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2922397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2923397f9dfeSDavid Greenman 
2924397f9dfeSDavid Greenman 	/*
2925397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2926397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2927397f9dfeSDavid Greenman 	 */
29287dced78aSDavid Greenman 	count = 100;
2929397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
29307dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
29317dced78aSDavid Greenman 		DELAY(10);
29327dced78aSDavid Greenman 	if (count == 0) {
2933f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
29347dced78aSDavid Greenman 		return;
29357dced78aSDavid Greenman 	}
2936397f9dfeSDavid Greenman 
2937397f9dfeSDavid Greenman 	/*
2938397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2939397f9dfeSDavid Greenman 	 */
2940397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2941b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2942b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
29432e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2944397f9dfeSDavid Greenman 
2945df79d527SGleb Smirnoff 	sc->watchdog_timer = 2;
2946397f9dfeSDavid Greenman 	return;
2947397f9dfeSDavid Greenman }
294872a32a26SJonathan Lemon 
294974d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
295074d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
295174d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
295274d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
295374d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
295474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2955de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
295672a32a26SJonathan Lemon 
295774d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
295872a32a26SJonathan Lemon 
295972a32a26SJonathan Lemon struct ucode {
296074d1ed23SMaxime Henrion 	uint32_t	revision;
296174d1ed23SMaxime Henrion 	uint32_t	*ucode;
296272a32a26SJonathan Lemon 	int		length;
296372a32a26SJonathan Lemon 	u_short		int_delay_offset;
296472a32a26SJonathan Lemon 	u_short		bundle_max_offset;
296572a32a26SJonathan Lemon } ucode_table[] = {
296672a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
296772a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
296872a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
296972a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
297072a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
297172a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
297272a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
297372a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
297472a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
297572a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2976507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
2977de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
297872a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
297972a32a26SJonathan Lemon };
298072a32a26SJonathan Lemon 
298172a32a26SJonathan Lemon static void
298272a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
298372a32a26SJonathan Lemon {
298472a32a26SJonathan Lemon 	struct ucode *uc;
298572a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
298694a4f968SPyun YongHyeon 	int i;
298772a32a26SJonathan Lemon 
298872a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
298972a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
299072a32a26SJonathan Lemon 			break;
299172a32a26SJonathan Lemon 	if (uc->ucode == NULL)
299272a32a26SJonathan Lemon 		return;
2993b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
299472a32a26SJonathan Lemon 	cbp->cb_status = 0;
299583e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
299683e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
299794a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
299894a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
299972a32a26SJonathan Lemon 	if (uc->int_delay_offset)
300074d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
300183e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
300272a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
300374d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
300483e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
300572a32a26SJonathan Lemon 	/*
300672a32a26SJonathan Lemon 	 * Download the ucode to the chip.
300772a32a26SJonathan Lemon 	 */
300872a32a26SJonathan Lemon 	fxp_scb_wait(sc);
3009b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
3010b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
301172a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
301272a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
3013209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
3014b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
301572a32a26SJonathan Lemon 	device_printf(sc->dev,
301672a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
301772a32a26SJonathan Lemon 	    sc->tunable_int_delay,
301872a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
301972a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
302072a32a26SJonathan Lemon }
302172a32a26SJonathan Lemon 
302272a32a26SJonathan Lemon static int
302372a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
302472a32a26SJonathan Lemon {
302572a32a26SJonathan Lemon 	int error, value;
302672a32a26SJonathan Lemon 
302772a32a26SJonathan Lemon 	value = *(int *)arg1;
302872a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
302972a32a26SJonathan Lemon 	if (error || !req->newptr)
303072a32a26SJonathan Lemon 		return (error);
303172a32a26SJonathan Lemon 	if (value < low || value > high)
303272a32a26SJonathan Lemon 		return (EINVAL);
303372a32a26SJonathan Lemon 	*(int *)arg1 = value;
303472a32a26SJonathan Lemon 	return (0);
303572a32a26SJonathan Lemon }
303672a32a26SJonathan Lemon 
303772a32a26SJonathan Lemon /*
303872a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
303972a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
304072a32a26SJonathan Lemon  */
304172a32a26SJonathan Lemon static int
304272a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
304372a32a26SJonathan Lemon {
304472a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
304572a32a26SJonathan Lemon }
304672a32a26SJonathan Lemon 
304772a32a26SJonathan Lemon static int
304872a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
304972a32a26SJonathan Lemon {
305072a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
305172a32a26SJonathan Lemon }
3052