xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 6da6d0a9e3f608feb5ca67cf633670359157549b)
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 /*
144f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
145f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
146f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
147f7788e8eSJonathan Lemon  * them.
148f7788e8eSJonathan Lemon  */
149f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
150b96ad4b2SPyun YongHyeon     { 0x1029,	-1,	0, "Intel 82559 PCI/CardBus Pro/100" },
151b96ad4b2SPyun YongHyeon     { 0x1030,	-1,	0, "Intel 82559 Pro/100 Ethernet" },
152b96ad4b2SPyun YongHyeon     { 0x1031,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
153b96ad4b2SPyun YongHyeon     { 0x1032,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
154b96ad4b2SPyun YongHyeon     { 0x1033,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
155b96ad4b2SPyun YongHyeon     { 0x1034,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
156b96ad4b2SPyun YongHyeon     { 0x1035,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
157b96ad4b2SPyun YongHyeon     { 0x1036,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
158b96ad4b2SPyun YongHyeon     { 0x1037,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
159b96ad4b2SPyun YongHyeon     { 0x1038,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
160b96ad4b2SPyun YongHyeon     { 0x1039,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
161b96ad4b2SPyun YongHyeon     { 0x103A,	-1,	4, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
162b96ad4b2SPyun YongHyeon     { 0x103B,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
163b96ad4b2SPyun YongHyeon     { 0x103C,	-1,	4, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
164b96ad4b2SPyun YongHyeon     { 0x103D,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
165b96ad4b2SPyun YongHyeon     { 0x103E,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
166b96ad4b2SPyun YongHyeon     { 0x1050,	-1,	5, "Intel 82801BA (D865) Pro/100 VE Ethernet" },
167b96ad4b2SPyun YongHyeon     { 0x1051,	-1,	5, "Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
168b96ad4b2SPyun YongHyeon     { 0x1059,	-1,	0, "Intel 82551QM Pro/100 M Mobile Connection" },
169b96ad4b2SPyun YongHyeon     { 0x1064,	-1,	6, "Intel 82562EZ (ICH6)" },
170b96ad4b2SPyun YongHyeon     { 0x1065,	-1,	6, "Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
171b96ad4b2SPyun YongHyeon     { 0x1068,	-1,	6, "Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
172b96ad4b2SPyun YongHyeon     { 0x1069,	-1,	6, "Intel 82562EM/EX/GX Pro/100 Ethernet" },
173b96ad4b2SPyun YongHyeon     { 0x1091,	-1,	7, "Intel 82562GX Pro/100 Ethernet" },
174b96ad4b2SPyun YongHyeon     { 0x1092,	-1,	7, "Intel Pro/100 VE Network Connection" },
175b96ad4b2SPyun YongHyeon     { 0x1093,	-1,	7, "Intel Pro/100 VM Network Connection" },
176b96ad4b2SPyun YongHyeon     { 0x1094,	-1,	7, "Intel Pro/100 946GZ (ICH7) Network Connection" },
177b96ad4b2SPyun YongHyeon     { 0x1209,	-1,	0, "Intel 82559ER Embedded 10/100 Ethernet" },
178b96ad4b2SPyun YongHyeon     { 0x1229,	0x01,	0, "Intel 82557 Pro/100 Ethernet" },
179b96ad4b2SPyun YongHyeon     { 0x1229,	0x02,	0, "Intel 82557 Pro/100 Ethernet" },
180b96ad4b2SPyun YongHyeon     { 0x1229,	0x03,	0, "Intel 82557 Pro/100 Ethernet" },
181b96ad4b2SPyun YongHyeon     { 0x1229,	0x04,	0, "Intel 82558 Pro/100 Ethernet" },
182b96ad4b2SPyun YongHyeon     { 0x1229,	0x05,	0, "Intel 82558 Pro/100 Ethernet" },
183b96ad4b2SPyun YongHyeon     { 0x1229,	0x06,	0, "Intel 82559 Pro/100 Ethernet" },
184b96ad4b2SPyun YongHyeon     { 0x1229,	0x07,	0, "Intel 82559 Pro/100 Ethernet" },
185b96ad4b2SPyun YongHyeon     { 0x1229,	0x08,	0, "Intel 82559 Pro/100 Ethernet" },
186b96ad4b2SPyun YongHyeon     { 0x1229,	0x09,	0, "Intel 82559ER Pro/100 Ethernet" },
187b96ad4b2SPyun YongHyeon     { 0x1229,	0x0c,	0, "Intel 82550 Pro/100 Ethernet" },
188b96ad4b2SPyun YongHyeon     { 0x1229,	0x0d,	0, "Intel 82550 Pro/100 Ethernet" },
189b96ad4b2SPyun YongHyeon     { 0x1229,	0x0e,	0, "Intel 82550 Pro/100 Ethernet" },
190b96ad4b2SPyun YongHyeon     { 0x1229,	0x0f,	0, "Intel 82551 Pro/100 Ethernet" },
191b96ad4b2SPyun YongHyeon     { 0x1229,	0x10,	0, "Intel 82551 Pro/100 Ethernet" },
192b96ad4b2SPyun YongHyeon     { 0x1229,	-1,	0, "Intel 82557/8/9 Pro/100 Ethernet" },
193b96ad4b2SPyun YongHyeon     { 0x2449,	-1,	2, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
194b96ad4b2SPyun YongHyeon     { 0x27dc,	-1,	7, "Intel 82801GB (ICH7) 10/100 Ethernet" },
195b96ad4b2SPyun YongHyeon     { 0,	-1,	0, NULL },
196f7788e8eSJonathan Lemon };
197f7788e8eSJonathan Lemon 
198c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
199c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
200c8bca6dcSBill Paul #else
201c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
202c8bca6dcSBill Paul #endif
203c8bca6dcSBill Paul 
204f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
205f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
206f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
207f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
208f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
209f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
210f7788e8eSJonathan Lemon 
211b96ad4b2SPyun YongHyeon static struct fxp_ident	*fxp_find_ident(device_t dev);
212f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
213f13075afSPyun YongHyeon static void		fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp,
214f13075afSPyun YongHyeon 			    struct mbuf *m, uint16_t status, int pos);
2151abcdbd1SAttilio Rao static int		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
21674d1ed23SMaxime Henrion 			    uint8_t statack, int count);
217f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2184953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
219f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
220f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
2214953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
2224e53f837SPyun YongHyeon static int		fxp_encap(struct fxp_softc *sc, struct mbuf **m_head);
2234e53f837SPyun YongHyeon static void		fxp_txeof(struct fxp_softc *sc);
224f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
225f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
226f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
227f7788e8eSJonathan Lemon 			    caddr_t data);
228df79d527SGleb Smirnoff static void 		fxp_watchdog(struct fxp_softc *sc);
22985050421SPyun YongHyeon static void		fxp_add_rfabuf(struct fxp_softc *sc,
23085050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23185050421SPyun YongHyeon static void		fxp_discard_rfabuf(struct fxp_softc *sc,
23285050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23385050421SPyun YongHyeon static int		fxp_new_rfabuf(struct fxp_softc *sc,
23485050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23509882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
236f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
23774d1ed23SMaxime Henrion static uint16_t		fxp_eeprom_getword(struct fxp_softc *sc, int offset,
238f7788e8eSJonathan Lemon 			    int autosize);
23900c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
24074d1ed23SMaxime Henrion 			    uint16_t data);
241f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
242f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
243f7788e8eSJonathan Lemon 			    int offset, int words);
24400c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
24500c4116bSJonathan Lemon 			    int offset, int words);
246f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
247f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
248f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
249f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
250f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
251f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
252f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
25316ec4b00SWarner Losh static int		fxp_miibus_writereg(device_t dev, int phy, int reg,
254f7788e8eSJonathan Lemon 			    int value);
25572a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
25672a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
25772a32a26SJonathan Lemon 			    int low, int high);
25872a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
25972a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
26028935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
26128935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
26228935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
26374d1ed23SMaxime Henrion     			    volatile uint16_t *status, bus_dma_tag_t dmat,
264209b07bcSMaxime Henrion 			    bus_dmamap_t map);
265f7788e8eSJonathan Lemon 
266f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
267f7788e8eSJonathan Lemon 	/* Device interface */
268f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
269f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
270f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
271f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
272f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
273f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
274f7788e8eSJonathan Lemon 
275f7788e8eSJonathan Lemon 	/* MII interface */
276f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
277f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
278f7788e8eSJonathan Lemon 
279f7788e8eSJonathan Lemon 	{ 0, 0 }
280f7788e8eSJonathan Lemon };
281f7788e8eSJonathan Lemon 
282f7788e8eSJonathan Lemon static driver_t fxp_driver = {
283f7788e8eSJonathan Lemon 	"fxp",
284f7788e8eSJonathan Lemon 	fxp_methods,
285f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
286f7788e8eSJonathan Lemon };
287f7788e8eSJonathan Lemon 
288f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
289f7788e8eSJonathan Lemon 
290f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
291f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
292f7788e8eSJonathan Lemon 
29305bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
29405bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
29505bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
29605bd8c22SMaxime Henrion 	{ -1, 0 }
29705bd8c22SMaxime Henrion };
29805bd8c22SMaxime Henrion 
29905bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
30005bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
30105bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30205bd8c22SMaxime Henrion 	{ -1, 0 }
30305bd8c22SMaxime Henrion };
30405bd8c22SMaxime Henrion 
305f7788e8eSJonathan Lemon /*
306dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
307dfe61cf1SDavid Greenman  * completed).
308dfe61cf1SDavid Greenman  */
30928935f27SMaxime Henrion static void
310f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
311a17c678eSDavid Greenman {
3123cf09dd1SMarcel Moolenaar 	union {
3133cf09dd1SMarcel Moolenaar 		uint16_t w;
3143cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3153cf09dd1SMarcel Moolenaar 	} flowctl;
316a17c678eSDavid Greenman 	int i = 10000;
317a17c678eSDavid Greenman 
3187dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3197dced78aSDavid Greenman 		DELAY(2);
3203cf09dd1SMarcel Moolenaar 	if (i == 0) {
3213cf09dd1SMarcel Moolenaar 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL);
3223cf09dd1SMarcel Moolenaar 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL + 1);
32300c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
324e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
325e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3263cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3273cf09dd1SMarcel Moolenaar 	}
3287dced78aSDavid Greenman }
3297dced78aSDavid Greenman 
33028935f27SMaxime Henrion static void
3312e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3322e2b8238SJonathan Lemon {
3332e2b8238SJonathan Lemon 
3342e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3352e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3362e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3372e2b8238SJonathan Lemon 	}
3382e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3392e2b8238SJonathan Lemon }
3402e2b8238SJonathan Lemon 
34128935f27SMaxime Henrion static void
34274d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
343209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3447dced78aSDavid Greenman {
3455986d0d2SPyun YongHyeon 	int i;
3467dced78aSDavid Greenman 
3475986d0d2SPyun YongHyeon 	for (i = 10000; i > 0; i--) {
3487dced78aSDavid Greenman 		DELAY(2);
3495986d0d2SPyun YongHyeon 		bus_dmamap_sync(dmat, map,
3505986d0d2SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3515986d0d2SPyun YongHyeon 		if ((le16toh(*status) & FXP_CB_STATUS_C) != 0)
3525986d0d2SPyun YongHyeon 			break;
353209b07bcSMaxime Henrion 	}
3547dced78aSDavid Greenman 	if (i == 0)
355f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
356a17c678eSDavid Greenman }
357a17c678eSDavid Greenman 
358b96ad4b2SPyun YongHyeon static struct fxp_ident *
359b96ad4b2SPyun YongHyeon fxp_find_ident(device_t dev)
360a17c678eSDavid Greenman {
36174d1ed23SMaxime Henrion 	uint16_t devid;
36274d1ed23SMaxime Henrion 	uint8_t revid;
363f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
364f7788e8eSJonathan Lemon 
36555ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
366f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
367f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
368f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
369f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
370f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
371b96ad4b2SPyun YongHyeon 				return (ident);
372b96ad4b2SPyun YongHyeon 			}
373b96ad4b2SPyun YongHyeon 		}
374b96ad4b2SPyun YongHyeon 	}
375b96ad4b2SPyun YongHyeon 	return (NULL);
376b96ad4b2SPyun YongHyeon }
377b96ad4b2SPyun YongHyeon 
378b96ad4b2SPyun YongHyeon /*
379b96ad4b2SPyun YongHyeon  * Return identification string if this device is ours.
380b96ad4b2SPyun YongHyeon  */
381b96ad4b2SPyun YongHyeon static int
382b96ad4b2SPyun YongHyeon fxp_probe(device_t dev)
383b96ad4b2SPyun YongHyeon {
384b96ad4b2SPyun YongHyeon 	struct fxp_ident *ident;
385b96ad4b2SPyun YongHyeon 
386b96ad4b2SPyun YongHyeon 	ident = fxp_find_ident(dev);
387b96ad4b2SPyun YongHyeon 	if (ident != NULL) {
388f7788e8eSJonathan Lemon 		device_set_desc(dev, ident->name);
389538565c4SWarner Losh 		return (BUS_PROBE_DEFAULT);
39055ce7b51SDavid Greenman 	}
391f7788e8eSJonathan Lemon 	return (ENXIO);
3926182fdbdSPeter Wemm }
3936182fdbdSPeter Wemm 
394b2badf02SMaxime Henrion static void
395b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
396b2badf02SMaxime Henrion {
39774d1ed23SMaxime Henrion 	uint32_t *addr;
398b2badf02SMaxime Henrion 
399b2badf02SMaxime Henrion 	if (error)
400b2badf02SMaxime Henrion 		return;
401b2badf02SMaxime Henrion 
402b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
403b2badf02SMaxime Henrion 	addr = arg;
404b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
405b2badf02SMaxime Henrion }
406b2badf02SMaxime Henrion 
4076182fdbdSPeter Wemm static int
4086182fdbdSPeter Wemm fxp_attach(device_t dev)
409a17c678eSDavid Greenman {
4106720ebccSMaxime Henrion 	struct fxp_softc *sc;
4116720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4126720ebccSMaxime Henrion 	struct fxp_tx *txp;
413b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4146720ebccSMaxime Henrion 	struct ifnet *ifp;
41574d1ed23SMaxime Henrion 	uint32_t val;
41674d1ed23SMaxime Henrion 	uint16_t data, myea[ETHER_ADDR_LEN / 2];
417fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4187137cea0SPyun YongHyeon 	int i, pmc, prefer_iomap;
4193212724cSJohn Baldwin 	int error;
420a17c678eSDavid Greenman 
4216720ebccSMaxime Henrion 	error = 0;
4226720ebccSMaxime Henrion 	sc = device_get_softc(dev);
423f7788e8eSJonathan Lemon 	sc->dev = dev;
4246008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4254953bccaSNate Lawson 	    MTX_DEF);
4263212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4274953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4284953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
429a17c678eSDavid Greenman 
4307ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4317ba33d82SBrooks Davis 	if (ifp == NULL) {
4327ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4337ba33d82SBrooks Davis 		error = ENOSPC;
4347ba33d82SBrooks Davis 		goto fail;
4357ba33d82SBrooks Davis 	}
4367ba33d82SBrooks Davis 
437dfe61cf1SDavid Greenman 	/*
4382bce79a2SMaxim Sobolev 	 * Enable bus mastering.
439df373873SWes Peters 	 */
440cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4419fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
44279495006SWarner Losh 
443df373873SWes Peters 	/*
4449fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4459fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4469fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
447dfe61cf1SDavid Greenman 	 */
4482a05a4ebSMatt Jacob 	prefer_iomap = 0;
44905bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
45005bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
45105bd8c22SMaxime Henrion 	if (prefer_iomap)
45205bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
45305bd8c22SMaxime Henrion 	else
45405bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4559fa6ccfbSMatt Jacob 
45605bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
45705bd8c22SMaxime Henrion 	if (error) {
45805bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
45905bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
46005bd8c22SMaxime Henrion 		else
46105bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
46205bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4639fa6ccfbSMatt Jacob 	}
46405bd8c22SMaxime Henrion 	if (error) {
46505bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4666182fdbdSPeter Wemm 		error = ENXIO;
467a17c678eSDavid Greenman 		goto fail;
468a17c678eSDavid Greenman 	}
46905bd8c22SMaxime Henrion 
4709fa6ccfbSMatt Jacob 	if (bootverbose) {
4719fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
47205bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4736182fdbdSPeter Wemm 	}
4746182fdbdSPeter Wemm 
475f7788e8eSJonathan Lemon 	/*
476a996f023SPyun YongHyeon 	 * Put CU/RU idle state and prepare full reset.
477f7788e8eSJonathan Lemon 	 */
478f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
479f7788e8eSJonathan Lemon 	DELAY(10);
480a996f023SPyun YongHyeon 	/* Full reset and disable interrupts. */
481a996f023SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
482a996f023SPyun YongHyeon 	DELAY(10);
483a996f023SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
484f7788e8eSJonathan Lemon 
485f7788e8eSJonathan Lemon 	/*
486f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
487f7788e8eSJonathan Lemon 	 */
488f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
489f7788e8eSJonathan Lemon 
490f7788e8eSJonathan Lemon 	/*
49193b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
49293b6e2e6SMaxime Henrion 	 */
493b96ad4b2SPyun YongHyeon 	sc->ident = fxp_find_ident(dev);
494b96ad4b2SPyun YongHyeon 	if (sc->ident->ich > 0) {
495b96ad4b2SPyun YongHyeon 		/* Assume ICH controllers are 82559. */
496b96ad4b2SPyun YongHyeon 		sc->revision = FXP_REV_82559_A0;
497b96ad4b2SPyun YongHyeon 	} else {
49893b6e2e6SMaxime Henrion 		fxp_read_eeprom(sc, &data, 5, 1);
49993b6e2e6SMaxime Henrion 		if ((data >> 8) == 1)
50093b6e2e6SMaxime Henrion 			sc->revision = FXP_REV_82557;
50193b6e2e6SMaxime Henrion 		else
50293b6e2e6SMaxime Henrion 			sc->revision = pci_get_revid(dev);
503b96ad4b2SPyun YongHyeon 	}
50493b6e2e6SMaxime Henrion 
50593b6e2e6SMaxime Henrion 	/*
5067137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
5077137cea0SPyun YongHyeon 	 */
5087137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
5097137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
5107137cea0SPyun YongHyeon 		fxp_read_eeprom(sc, &data, 10, 1);
5117137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
5127137cea0SPyun YongHyeon 		    pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0)
5137137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
5147137cea0SPyun YongHyeon 	}
5157137cea0SPyun YongHyeon 
51643d8b117SPyun YongHyeon 	/* Receiver lock-up workaround detection. */
51743d8b117SPyun YongHyeon 	fxp_read_eeprom(sc, &data, 3, 1);
51843d8b117SPyun YongHyeon 	if ((data & 0x03) != 0x03) {
51943d8b117SPyun YongHyeon 		sc->flags |= FXP_FLAG_RXBUG;
52043d8b117SPyun YongHyeon 		device_printf(dev, "Enabling Rx lock-up workaround\n");
52143d8b117SPyun YongHyeon 	}
52243d8b117SPyun YongHyeon 
5237137cea0SPyun YongHyeon 	/*
5243bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
525f7788e8eSJonathan Lemon 	 */
526f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
52793b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5284ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
529dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
530f7788e8eSJonathan Lemon 
5310f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5320f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
53350a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
534858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
53572a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
5360f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5370f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
53850a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
539858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
54072a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
5410f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5420f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5430f1db1d6SMaxime Henrion 	    OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
5440f1db1d6SMaxime Henrion 	    "FXP RNR events");
5450f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5460f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5470f1db1d6SMaxime Henrion 	    OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0,
5480f1db1d6SMaxime Henrion 	    "FXP flow control disabled");
54972a32a26SJonathan Lemon 
55072a32a26SJonathan Lemon 	/*
55172a32a26SJonathan Lemon 	 * Pull in device tunables.
55272a32a26SJonathan Lemon 	 */
55372a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
55472a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
55503edfff3SRobert Watson 	sc->tunable_noflow = 1;
55672a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
55772a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
55872a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
55972a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
5600f1db1d6SMaxime Henrion 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
5610f1db1d6SMaxime Henrion 	    "noflow", &sc->tunable_noflow);
5620f1db1d6SMaxime Henrion 	sc->rnr = 0;
56372a32a26SJonathan Lemon 
56472a32a26SJonathan Lemon 	/*
5652e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
56600c4116bSJonathan Lemon 	 *
56772a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
56872a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
56972a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
57000c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
57100c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
57200c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
57300c4116bSJonathan Lemon 	 *
57400c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5752e2b8238SJonathan Lemon 	 */
576b96ad4b2SPyun YongHyeon 	if ((sc->ident->ich >= 2 && sc->ident->ich <= 3) ||
577b96ad4b2SPyun YongHyeon 	    (sc->ident->ich == 0 && sc->revision >= FXP_REV_82559_A0)) {
57800c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
57900c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
58074d1ed23SMaxime Henrion 			uint16_t cksum;
58100c4116bSJonathan Lemon 			int i;
58200c4116bSJonathan Lemon 
58300c4116bSJonathan Lemon 			device_printf(dev,
584001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
58500c4116bSJonathan Lemon 			data &= ~0x02;
58600c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
58700c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
58800c4116bSJonathan Lemon 			cksum = 0;
58900c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
59000c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
59100c4116bSJonathan Lemon 				cksum += data;
59200c4116bSJonathan Lemon 			}
59300c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
59400c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
59500c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
59600c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
59700c4116bSJonathan Lemon 			device_printf(dev,
59800c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
59900c4116bSJonathan Lemon 			    i, data, cksum);
60000c4116bSJonathan Lemon #if 1
60100c4116bSJonathan Lemon 			/*
60200c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
60300c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
60400c4116bSJonathan Lemon 			 */
6052e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
60600c4116bSJonathan Lemon #endif
60700c4116bSJonathan Lemon 		}
60800c4116bSJonathan Lemon 	}
6092e2b8238SJonathan Lemon 
6102e2b8238SJonathan Lemon 	/*
6113bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
6123bd07cfdSJonathan Lemon 	 */
61372a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
6143bd07cfdSJonathan Lemon 		/*
61574396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
61674396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
61774396a0aSJonathan Lemon 		 * the board to turn on MWI.
6183bd07cfdSJonathan Lemon 		 */
61974396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
62074396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
6213bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
6223bd07cfdSJonathan Lemon 
6233bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
6243bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
62544e0bc11SYaroslav Tykhiy 
62644e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
62744e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
62844e0bc11SYaroslav Tykhiy 	} else {
62944e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
63044e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6313bd07cfdSJonathan Lemon 	}
6323bd07cfdSJonathan Lemon 
633f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
634829b278eSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0) {
635829b278eSPyun YongHyeon 		/* 82559ER does not support Rx checksum offloading. */
636829b278eSPyun YongHyeon 		if (sc->ident->devid != 0x1209)
637f13075afSPyun YongHyeon 			sc->flags |= FXP_FLAG_82559_RXCSUM;
638829b278eSPyun YongHyeon 	}
6393bd07cfdSJonathan Lemon 	/*
640c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
641c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
642c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
643c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
644c8bca6dcSBill Paul 	 */
645507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
646507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
647507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
648c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
649c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
650c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
651f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
652f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
653c8bca6dcSBill Paul 	} else {
654c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
655c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
656c8bca6dcSBill Paul 	}
657c8bca6dcSBill Paul 
658c8bca6dcSBill Paul 	/*
659b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
660b2badf02SMaxime Henrion 	 */
66140c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
662c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
663c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
66440c20505SMaxime Henrion 		sc->maxtxseg--;
665c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
666c21e84e4SPyun YongHyeon 	}
667c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
668c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
669c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
670c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
671a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_txmtag);
672b2badf02SMaxime Henrion 	if (error) {
673a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TX DMA tag\n");
674a2057a72SPyun YongHyeon 		goto fail;
675a2057a72SPyun YongHyeon 	}
676a2057a72SPyun YongHyeon 
677a2057a72SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
678a2057a72SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
679a2057a72SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0,
680a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_rxmtag);
681a2057a72SPyun YongHyeon 	if (error) {
682a2057a72SPyun YongHyeon 		device_printf(dev, "could not create RX DMA tag\n");
683b2badf02SMaxime Henrion 		goto fail;
684b2badf02SMaxime Henrion 	}
685b2badf02SMaxime Henrion 
686c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
687c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
688c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
689c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
690b2badf02SMaxime Henrion 	if (error) {
691a2057a72SPyun YongHyeon 		device_printf(dev, "could not create stats DMA tag\n");
692b2badf02SMaxime Henrion 		goto fail;
693b2badf02SMaxime Henrion 	}
694b2badf02SMaxime Henrion 
695b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
696aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap);
697a2057a72SPyun YongHyeon 	if (error) {
698a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate stats DMA memory\n");
6994953bccaSNate Lawson 		goto fail;
700a2057a72SPyun YongHyeon 	}
701b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
702b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
703b2badf02SMaxime Henrion 	if (error) {
704a2057a72SPyun YongHyeon 		device_printf(dev, "could not load the stats DMA buffer\n");
705b2badf02SMaxime Henrion 		goto fail;
706b2badf02SMaxime Henrion 	}
707b2badf02SMaxime Henrion 
708c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
709c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
710c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
711c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
712b2badf02SMaxime Henrion 	if (error) {
713a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TxCB DMA tag\n");
714b2badf02SMaxime Henrion 		goto fail;
715b2badf02SMaxime Henrion 	}
716b2badf02SMaxime Henrion 
717b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
718aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map);
719a2057a72SPyun YongHyeon 	if (error) {
720a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate TxCB DMA memory\n");
7214953bccaSNate Lawson 		goto fail;
722a2057a72SPyun YongHyeon 	}
723b2badf02SMaxime Henrion 
724b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
725b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
726b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
727b2badf02SMaxime Henrion 	if (error) {
728a2057a72SPyun YongHyeon 		device_printf(dev, "could not load TxCB DMA buffer\n");
729b2badf02SMaxime Henrion 		goto fail;
730b2badf02SMaxime Henrion 	}
731b2badf02SMaxime Henrion 
732c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
733c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
734c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
735c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
736b2badf02SMaxime Henrion 	if (error) {
737a2057a72SPyun YongHyeon 		device_printf(dev,
738a2057a72SPyun YongHyeon 		    "could not create multicast setup DMA tag\n");
739b2badf02SMaxime Henrion 		goto fail;
740b2badf02SMaxime Henrion 	}
741b2badf02SMaxime Henrion 
742b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
743a2057a72SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->mcs_map);
744a2057a72SPyun YongHyeon 	if (error) {
745a2057a72SPyun YongHyeon 		device_printf(dev,
746a2057a72SPyun YongHyeon 		    "could not allocate multicast setup DMA memory\n");
7474953bccaSNate Lawson 		goto fail;
748a2057a72SPyun YongHyeon 	}
749b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
750b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
751b2badf02SMaxime Henrion 	if (error) {
752a2057a72SPyun YongHyeon 		device_printf(dev,
753a2057a72SPyun YongHyeon 		    "can't load the multicast setup DMA buffer\n");
754b2badf02SMaxime Henrion 		goto fail;
755b2badf02SMaxime Henrion 	}
756b2badf02SMaxime Henrion 
757b2badf02SMaxime Henrion 	/*
7586720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7596720ebccSMaxime Henrion 	 * the TX command blocks.
760b2badf02SMaxime Henrion 	 */
7616720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7626720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7634cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7646720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
765a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_txmtag, 0, &txp[i].tx_map);
766b2badf02SMaxime Henrion 		if (error) {
767b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
768b2badf02SMaxime Henrion 			goto fail;
769b2badf02SMaxime Henrion 		}
770b2badf02SMaxime Henrion 	}
771a2057a72SPyun YongHyeon 	error = bus_dmamap_create(sc->fxp_rxmtag, 0, &sc->spare_map);
772b2badf02SMaxime Henrion 	if (error) {
773b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
774b2badf02SMaxime Henrion 		goto fail;
775b2badf02SMaxime Henrion 	}
776b2badf02SMaxime Henrion 
777b2badf02SMaxime Henrion 	/*
778b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
779b2badf02SMaxime Henrion 	 */
780b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
781b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
782b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
783a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_rxmtag, 0, &rxp->rx_map);
784b2badf02SMaxime Henrion 		if (error) {
785b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
786b2badf02SMaxime Henrion 			goto fail;
787b2badf02SMaxime Henrion 		}
78885050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7894953bccaSNate Lawson 			error = ENOMEM;
7904953bccaSNate Lawson 			goto fail;
7914953bccaSNate Lawson 		}
79285050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
793b2badf02SMaxime Henrion 	}
794b2badf02SMaxime Henrion 
795b2badf02SMaxime Henrion 	/*
796f7788e8eSJonathan Lemon 	 * Read MAC address.
797f7788e8eSJonathan Lemon 	 */
79883e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
799fc74a9f9SBrooks Davis 	eaddr[0] = myea[0] & 0xff;
800fc74a9f9SBrooks Davis 	eaddr[1] = myea[0] >> 8;
801fc74a9f9SBrooks Davis 	eaddr[2] = myea[1] & 0xff;
802fc74a9f9SBrooks Davis 	eaddr[3] = myea[1] >> 8;
803fc74a9f9SBrooks Davis 	eaddr[4] = myea[2] & 0xff;
804fc74a9f9SBrooks Davis 	eaddr[5] = myea[2] >> 8;
805f7788e8eSJonathan Lemon 	if (bootverbose) {
8062e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
807f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
8082e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
8092e2b8238SJonathan Lemon 		    pci_get_revid(dev));
81072a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
81172a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
81272a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
813f7788e8eSJonathan Lemon 	}
814f7788e8eSJonathan Lemon 
815f7788e8eSJonathan Lemon 	/*
816f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
817f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
818f7788e8eSJonathan Lemon 	 *
819f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
820f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
821f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
822f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
823f7788e8eSJonathan Lemon 	 */
824f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
825f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
826f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
827f7788e8eSJonathan Lemon 	} else {
828f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
829f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
830f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
8316182fdbdSPeter Wemm 			error = ENXIO;
832ba8c6fd5SDavid Greenman 			goto fail;
833a17c678eSDavid Greenman 		}
834f7788e8eSJonathan Lemon 	}
835dccee1a1SDavid Greenman 
8369bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
837fb583156SDavid Greenman 	ifp->if_init = fxp_init;
838ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
839ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
840ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
841ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
842a17c678eSDavid Greenman 
8435fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
8445fe9116bSYaroslav Tykhiy 
845c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
846c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
847c21e84e4SPyun YongHyeon 		ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO;
848c21e84e4SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4;
849c21e84e4SPyun YongHyeon 		ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4;
850c8bca6dcSBill Paul 	}
851c8bca6dcSBill Paul 
852f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
853f13075afSPyun YongHyeon 		ifp->if_capabilities |= IFCAP_RXCSUM;
854f13075afSPyun YongHyeon 		ifp->if_capenable |= IFCAP_RXCSUM;
855f13075afSPyun YongHyeon 	}
856f13075afSPyun YongHyeon 
8577137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
8587137cea0SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL_MAGIC;
8597137cea0SPyun YongHyeon 		ifp->if_capenable |= IFCAP_WOL_MAGIC;
8607137cea0SPyun YongHyeon 	}
8617137cea0SPyun YongHyeon 
862fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
863fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
864fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
865fb917226SRuslan Ermilov #endif
866fb917226SRuslan Ermilov 
867dfe61cf1SDavid Greenman 	/*
8684953bccaSNate Lawson 	 * Attach the interface.
8694953bccaSNate Lawson 	 */
870fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
8714953bccaSNate Lawson 
8724953bccaSNate Lawson 	/*
873e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
8745fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
8755fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
876e8c8b728SJonathan Lemon 	 */
877e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
878673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
87944e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
880bd4fa9d9SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) {
881bd4fa9d9SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING |
882bd4fa9d9SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM;
883bd4fa9d9SPyun YongHyeon 		ifp->if_capenable |= IFCAP_VLAN_HWTAGGING |
884bd4fa9d9SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM;
885bd4fa9d9SPyun YongHyeon 	}
886e8c8b728SJonathan Lemon 
887483b9871SDavid Greenman 	/*
8883114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8893114fdb4SDavid Greenman 	 * TX descriptors.
890483b9871SDavid Greenman 	 */
8917929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8927929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8937929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8944a684684SDavid Greenman 
895201afb0eSMaxime Henrion 	/*
8964953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
897201afb0eSMaxime Henrion 	 */
89805bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
899ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
900201afb0eSMaxime Henrion 	if (error) {
901201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
902fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
903201afb0eSMaxime Henrion 		goto fail;
904201afb0eSMaxime Henrion 	}
905201afb0eSMaxime Henrion 
9067137cea0SPyun YongHyeon 	/*
9077137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
9087137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
9097137cea0SPyun YongHyeon 	 */
9107137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
9117137cea0SPyun YongHyeon 		FXP_LOCK(sc);
9127137cea0SPyun YongHyeon 		/* Clear wakeup events. */
913af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
9147137cea0SPyun YongHyeon 		fxp_init_body(sc);
9157137cea0SPyun YongHyeon 		fxp_stop(sc);
9167137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
9177137cea0SPyun YongHyeon 	}
9187137cea0SPyun YongHyeon 
919a17c678eSDavid Greenman fail:
9201b5a39d3SBrooks Davis 	if (error)
921f7788e8eSJonathan Lemon 		fxp_release(sc);
922f7788e8eSJonathan Lemon 	return (error);
923f7788e8eSJonathan Lemon }
924f7788e8eSJonathan Lemon 
925f7788e8eSJonathan Lemon /*
9264953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
9274953bccaSNate Lawson  * interrupt should already be torn down.
928f7788e8eSJonathan Lemon  */
929f7788e8eSJonathan Lemon static void
930f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
931f7788e8eSJonathan Lemon {
932b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
933b2badf02SMaxime Henrion 	struct fxp_tx *txp;
934b2badf02SMaxime Henrion 	int i;
935b2badf02SMaxime Henrion 
93667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
937670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
938670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
9394953bccaSNate Lawson 	if (sc->miibus)
9404953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
9414953bccaSNate Lawson 	bus_generic_detach(sc->dev);
9424953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
943b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
944b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
945b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
946b2badf02SMaxime Henrion 		    sc->cbl_map);
947b2badf02SMaxime Henrion 	}
948b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
949b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
950b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
951b2badf02SMaxime Henrion 	}
952b2badf02SMaxime Henrion 	if (sc->mcsp) {
953b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
954b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
955b2badf02SMaxime Henrion 	}
95605bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
957a2057a72SPyun YongHyeon 	if (sc->fxp_rxmtag) {
958b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
959b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
960b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
961a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
962b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
963a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
964b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
965b983c7b3SMaxime Henrion 			}
966a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_rxmtag, rxp->rx_map);
967b983c7b3SMaxime Henrion 		}
968a2057a72SPyun YongHyeon 		bus_dmamap_destroy(sc->fxp_rxmtag, sc->spare_map);
969a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_rxmtag);
970a2057a72SPyun YongHyeon 	}
971a2057a72SPyun YongHyeon 	if (sc->fxp_txmtag) {
972b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
973b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
974b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
975a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
976b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
977a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
978b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
979b983c7b3SMaxime Henrion 			}
980a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_txmtag, txp->tx_map);
981b983c7b3SMaxime Henrion 		}
982a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_txmtag);
983b983c7b3SMaxime Henrion 	}
984c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
985c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
986b2badf02SMaxime Henrion 	if (sc->cbl_tag)
987b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
988b2badf02SMaxime Henrion 	if (sc->mcs_tag)
989b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
990fc74a9f9SBrooks Davis 	if (sc->ifp)
991fc74a9f9SBrooks Davis 		if_free(sc->ifp);
99272a32a26SJonathan Lemon 
9930f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
9946182fdbdSPeter Wemm }
9956182fdbdSPeter Wemm 
9966182fdbdSPeter Wemm /*
9976182fdbdSPeter Wemm  * Detach interface.
9986182fdbdSPeter Wemm  */
9996182fdbdSPeter Wemm static int
10006182fdbdSPeter Wemm fxp_detach(device_t dev)
10016182fdbdSPeter Wemm {
10026182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
10036182fdbdSPeter Wemm 
100440929967SGleb Smirnoff #ifdef DEVICE_POLLING
100540929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
100640929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
100740929967SGleb Smirnoff #endif
100840929967SGleb Smirnoff 
10094953bccaSNate Lawson 	FXP_LOCK(sc);
10106182fdbdSPeter Wemm 	/*
101132cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
101220f0c80fSMaxime Henrion 	 */
101320f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
101420f0c80fSMaxime Henrion 	fxp_stop(sc);
101532cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
10169eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
101720f0c80fSMaxime Henrion 
10186182fdbdSPeter Wemm 	/*
10193212724cSJohn Baldwin 	 * Close down routes etc.
10203212724cSJohn Baldwin 	 */
10213212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
10223212724cSJohn Baldwin 
10233212724cSJohn Baldwin 	/*
10244953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
10254953bccaSNate Lawson 	 * races with fxp_intr().
10266182fdbdSPeter Wemm 	 */
102705bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
10284953bccaSNate Lawson 	sc->ih = NULL;
10296182fdbdSPeter Wemm 
1030f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
1031f7788e8eSJonathan Lemon 	fxp_release(sc);
1032f7788e8eSJonathan Lemon 	return (0);
1033a17c678eSDavid Greenman }
1034a17c678eSDavid Greenman 
1035a17c678eSDavid Greenman /*
10364a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
1037a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
1038a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
1039a17c678eSDavid Greenman  */
10406182fdbdSPeter Wemm static int
10416182fdbdSPeter Wemm fxp_shutdown(device_t dev)
1042a17c678eSDavid Greenman {
10433212724cSJohn Baldwin 
10446182fdbdSPeter Wemm 	/*
10456182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
10466182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
10476182fdbdSPeter Wemm 	 * reboot before the driver initializes.
10486182fdbdSPeter Wemm 	 */
10497137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
1050a17c678eSDavid Greenman }
1051a17c678eSDavid Greenman 
10527dced78aSDavid Greenman /*
10537dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
10547dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10557dced78aSDavid Greenman  * resume.
10567dced78aSDavid Greenman  */
10577dced78aSDavid Greenman static int
10587dced78aSDavid Greenman fxp_suspend(device_t dev)
10597dced78aSDavid Greenman {
10607dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
10617137cea0SPyun YongHyeon 	struct ifnet *ifp;
10627137cea0SPyun YongHyeon 	int pmc;
10637137cea0SPyun YongHyeon 	uint16_t pmstat;
10647dced78aSDavid Greenman 
10654953bccaSNate Lawson 	FXP_LOCK(sc);
10667dced78aSDavid Greenman 
10677137cea0SPyun YongHyeon 	ifp = sc->ifp;
10687137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
10697137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10707137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
10717137cea0SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
10727137cea0SPyun YongHyeon 			/* Request PME. */
10737137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10747137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10757137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
10767137cea0SPyun YongHyeon 			fxp_init_body(sc);
10777137cea0SPyun YongHyeon 		}
10787137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
10797137cea0SPyun YongHyeon 	}
10807dced78aSDavid Greenman 	fxp_stop(sc);
10817dced78aSDavid Greenman 
10827dced78aSDavid Greenman 	sc->suspended = 1;
10837dced78aSDavid Greenman 
10844953bccaSNate Lawson 	FXP_UNLOCK(sc);
1085f7788e8eSJonathan Lemon 	return (0);
10867dced78aSDavid Greenman }
10877dced78aSDavid Greenman 
10887dced78aSDavid Greenman /*
108967ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
10907dced78aSDavid Greenman  * appropriate.
10917dced78aSDavid Greenman  */
10927dced78aSDavid Greenman static int
10937dced78aSDavid Greenman fxp_resume(device_t dev)
10947dced78aSDavid Greenman {
10957dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
1096fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
10977137cea0SPyun YongHyeon 	int pmc;
10987137cea0SPyun YongHyeon 	uint16_t pmstat;
10997dced78aSDavid Greenman 
11004953bccaSNate Lawson 	FXP_LOCK(sc);
11017dced78aSDavid Greenman 
11027137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
11037137cea0SPyun YongHyeon 		sc->flags &= ~FXP_FLAG_WOL;
11047137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
11057137cea0SPyun YongHyeon 		/* Disable PME and clear PME status. */
11067137cea0SPyun YongHyeon 		pmstat &= ~PCIM_PSTAT_PMEENABLE;
11077137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1108af75b654SPyun YongHyeon 		if ((sc->flags & FXP_FLAG_WOLCAP) != 0)
1109af75b654SPyun YongHyeon 			CSR_WRITE_1(sc, FXP_CSR_PMDR,
1110af75b654SPyun YongHyeon 			    CSR_READ_1(sc, FXP_CSR_PMDR));
11117137cea0SPyun YongHyeon 	}
11127137cea0SPyun YongHyeon 
11137dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
11147dced78aSDavid Greenman 	DELAY(10);
11157dced78aSDavid Greenman 
11167dced78aSDavid Greenman 	/* reinitialize interface if necessary */
11177dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
11184953bccaSNate Lawson 		fxp_init_body(sc);
11197dced78aSDavid Greenman 
11207dced78aSDavid Greenman 	sc->suspended = 0;
11217dced78aSDavid Greenman 
11224953bccaSNate Lawson 	FXP_UNLOCK(sc);
1123ba8c6fd5SDavid Greenman 	return (0);
1124f7788e8eSJonathan Lemon }
1125ba8c6fd5SDavid Greenman 
112600c4116bSJonathan Lemon static void
112700c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
112800c4116bSJonathan Lemon {
112974d1ed23SMaxime Henrion 	uint16_t reg;
113000c4116bSJonathan Lemon 	int x;
113100c4116bSJonathan Lemon 
113200c4116bSJonathan Lemon 	/*
113300c4116bSJonathan Lemon 	 * Shift in data.
113400c4116bSJonathan Lemon 	 */
113500c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
113600c4116bSJonathan Lemon 		if (data & x)
113700c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
113800c4116bSJonathan Lemon 		else
113900c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
114000c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
114100c4116bSJonathan Lemon 		DELAY(1);
114200c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
114300c4116bSJonathan Lemon 		DELAY(1);
114400c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
114500c4116bSJonathan Lemon 		DELAY(1);
114600c4116bSJonathan Lemon 	}
114700c4116bSJonathan Lemon }
114800c4116bSJonathan Lemon 
1149f7788e8eSJonathan Lemon /*
1150f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1151f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1152f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1153f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1154f7788e8eSJonathan Lemon  * every 16 bits of data.
1155f7788e8eSJonathan Lemon  */
115674d1ed23SMaxime Henrion static uint16_t
1157f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1158f7788e8eSJonathan Lemon {
115974d1ed23SMaxime Henrion 	uint16_t reg, data;
1160f7788e8eSJonathan Lemon 	int x;
1161ba8c6fd5SDavid Greenman 
1162f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1163f7788e8eSJonathan Lemon 	/*
1164f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1165f7788e8eSJonathan Lemon 	 */
116600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1167f7788e8eSJonathan Lemon 	/*
1168f7788e8eSJonathan Lemon 	 * Shift in address.
1169f7788e8eSJonathan Lemon 	 */
1170f7788e8eSJonathan Lemon 	data = 0;
1171f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1172f7788e8eSJonathan Lemon 		if (offset & x)
1173f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1174f7788e8eSJonathan Lemon 		else
1175f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1176f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1177f7788e8eSJonathan Lemon 		DELAY(1);
1178f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1179f7788e8eSJonathan Lemon 		DELAY(1);
1180f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1181f7788e8eSJonathan Lemon 		DELAY(1);
1182f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1183f7788e8eSJonathan Lemon 		data++;
1184f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1185f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1186f7788e8eSJonathan Lemon 			break;
1187f7788e8eSJonathan Lemon 		}
1188f7788e8eSJonathan Lemon 	}
1189f7788e8eSJonathan Lemon 	/*
1190f7788e8eSJonathan Lemon 	 * Shift out data.
1191f7788e8eSJonathan Lemon 	 */
1192f7788e8eSJonathan Lemon 	data = 0;
1193f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1194f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1195f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1196f7788e8eSJonathan Lemon 		DELAY(1);
1197f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1198f7788e8eSJonathan Lemon 			data |= x;
1199f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1200f7788e8eSJonathan Lemon 		DELAY(1);
1201f7788e8eSJonathan Lemon 	}
1202f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1203f7788e8eSJonathan Lemon 	DELAY(1);
1204f7788e8eSJonathan Lemon 
1205f7788e8eSJonathan Lemon 	return (data);
1206ba8c6fd5SDavid Greenman }
1207ba8c6fd5SDavid Greenman 
120800c4116bSJonathan Lemon static void
120974d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
121000c4116bSJonathan Lemon {
121100c4116bSJonathan Lemon 	int i;
121200c4116bSJonathan Lemon 
121300c4116bSJonathan Lemon 	/*
121400c4116bSJonathan Lemon 	 * Erase/write enable.
121500c4116bSJonathan Lemon 	 */
121600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
121700c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
121800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
121900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
122000c4116bSJonathan Lemon 	DELAY(1);
122100c4116bSJonathan Lemon 	/*
122200c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
122300c4116bSJonathan Lemon 	 */
122400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
122500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
122600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
122700c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
122800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
122900c4116bSJonathan Lemon 	DELAY(1);
123000c4116bSJonathan Lemon 	/*
123100c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
123200c4116bSJonathan Lemon 	 */
123300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
123400c4116bSJonathan Lemon 	DELAY(1);
123500c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
123600c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
123700c4116bSJonathan Lemon 			break;
123800c4116bSJonathan Lemon 		DELAY(50);
123900c4116bSJonathan Lemon 	}
124000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
124100c4116bSJonathan Lemon 	DELAY(1);
124200c4116bSJonathan Lemon 	/*
124300c4116bSJonathan Lemon 	 * Erase/write disable.
124400c4116bSJonathan Lemon 	 */
124500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
124600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
124700c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
124800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
124900c4116bSJonathan Lemon 	DELAY(1);
125000c4116bSJonathan Lemon }
125100c4116bSJonathan Lemon 
1252ba8c6fd5SDavid Greenman /*
1253e9bf2fa7SDavid Greenman  * From NetBSD:
1254e9bf2fa7SDavid Greenman  *
1255e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1256e9bf2fa7SDavid Greenman  *
1257e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1258e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1259e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1260e9bf2fa7SDavid Greenman  *
1261e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1262e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1263e9bf2fa7SDavid Greenman  *
1264e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1265e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1266e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1267e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1268e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1269e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1270e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1271e9bf2fa7SDavid Greenman  */
1272e9bf2fa7SDavid Greenman static void
1273f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1274e9bf2fa7SDavid Greenman {
1275e9bf2fa7SDavid Greenman 
1276f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1277f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1278f7788e8eSJonathan Lemon 
1279f7788e8eSJonathan Lemon 	/* autosize */
1280f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1281e9bf2fa7SDavid Greenman }
1282f7788e8eSJonathan Lemon 
1283ba8c6fd5SDavid Greenman static void
1284f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1285ba8c6fd5SDavid Greenman {
1286f7788e8eSJonathan Lemon 	int i;
1287ba8c6fd5SDavid Greenman 
1288f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1289f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1290ba8c6fd5SDavid Greenman }
1291ba8c6fd5SDavid Greenman 
129200c4116bSJonathan Lemon static void
129300c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
129400c4116bSJonathan Lemon {
129500c4116bSJonathan Lemon 	int i;
129600c4116bSJonathan Lemon 
129700c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
129800c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
129900c4116bSJonathan Lemon }
130000c4116bSJonathan Lemon 
1301a17c678eSDavid Greenman /*
13024953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1303a17c678eSDavid Greenman  */
1304a17c678eSDavid Greenman static void
1305f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1306a17c678eSDavid Greenman {
13079b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
13084953bccaSNate Lawson 
13094953bccaSNate Lawson 	FXP_LOCK(sc);
13104953bccaSNate Lawson 	fxp_start_body(ifp);
13114953bccaSNate Lawson 	FXP_UNLOCK(sc);
13124953bccaSNate Lawson }
13134953bccaSNate Lawson 
13144953bccaSNate Lawson /*
13154953bccaSNate Lawson  * Start packet transmission on the interface.
13164953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
13174953bccaSNate Lawson  * internal entry point only.
13184953bccaSNate Lawson  */
13194953bccaSNate Lawson static void
13204953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
13214953bccaSNate Lawson {
13224953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1323b2badf02SMaxime Henrion 	struct mbuf *mb_head;
13244e53f837SPyun YongHyeon 	int txqueued;
1325a17c678eSDavid Greenman 
132667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
132740c20505SMaxime Henrion 
1328c109e385SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1329c109e385SPyun YongHyeon 	    IFF_DRV_RUNNING)
1330c109e385SPyun YongHyeon 		return;
1331c109e385SPyun YongHyeon 
13324e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
13334e53f837SPyun YongHyeon 		fxp_txeof(sc);
1334483b9871SDavid Greenman 	/*
1335483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1336483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
13373114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
13383114fdb4SDavid Greenman 	 *       a NOP command when needed.
1339483b9871SDavid Greenman 	 */
134040c20505SMaxime Henrion 	txqueued = 0;
13417929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
13427929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1343483b9871SDavid Greenman 
1344dfe61cf1SDavid Greenman 		/*
1345dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1346dfe61cf1SDavid Greenman 		 */
13477929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
13487929aa03SMax Laier 		if (mb_head == NULL)
13497929aa03SMax Laier 			break;
1350a17c678eSDavid Greenman 
13514e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13524e53f837SPyun YongHyeon 			if (mb_head == NULL)
135340c20505SMaxime Henrion 				break;
13544e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
13554e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
135640c20505SMaxime Henrion 		}
13574e53f837SPyun YongHyeon 		txqueued++;
13584e53f837SPyun YongHyeon 		/*
13594e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13604e53f837SPyun YongHyeon 		 */
13614e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
13624e53f837SPyun YongHyeon 	}
136340c20505SMaxime Henrion 
136440c20505SMaxime Henrion 	/*
136540c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
136640c20505SMaxime Henrion 	 * going again if suspended.
136740c20505SMaxime Henrion 	 */
13684e53f837SPyun YongHyeon 	if (txqueued > 0) {
1369a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1370a2057a72SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
137140c20505SMaxime Henrion 		fxp_scb_wait(sc);
137240c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13734e53f837SPyun YongHyeon 		/*
13744e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
13754e53f837SPyun YongHyeon 		 * from the card again.
13764e53f837SPyun YongHyeon 		 */
13774e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
137840c20505SMaxime Henrion 	}
137940c20505SMaxime Henrion }
138040c20505SMaxime Henrion 
138140c20505SMaxime Henrion static int
13824e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
138340c20505SMaxime Henrion {
138440c20505SMaxime Henrion 	struct ifnet *ifp;
138540c20505SMaxime Henrion 	struct mbuf *m;
138640c20505SMaxime Henrion 	struct fxp_tx *txp;
138740c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1388c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
138940c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1390c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
139140c20505SMaxime Henrion 
139240c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1393fc74a9f9SBrooks Davis 	ifp = sc->ifp;
139440c20505SMaxime Henrion 
1395c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1396c21e84e4SPyun YongHyeon 	tcp = NULL;
1397dfe61cf1SDavid Greenman 	/*
1398483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1399dfe61cf1SDavid Greenman 	 */
1400b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1401c8bca6dcSBill Paul 
1402c8bca6dcSBill Paul 	/*
1403a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1404a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1405a35e7eaaSDon Lewis 	 * Developer Manual says:
1406a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1407a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1408a35e7eaaSDon Lewis 	 *   ...
1409a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1410a35e7eaaSDon Lewis 	 *   be used.
1411a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1412a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1413a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1414a35e7eaaSDon Lewis 	 */
1415a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1416a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1417a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1418a35e7eaaSDon Lewis 
14194e53f837SPyun YongHyeon 	m = *m_head;
1420c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1421c21e84e4SPyun YongHyeon 		/*
1422c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1423c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1424c21e84e4SPyun YongHyeon 		 */
1425c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1426c21e84e4SPyun YongHyeon 		struct ip *ip;
1427c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1428c21e84e4SPyun YongHyeon 
1429c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1430c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1431c21e84e4SPyun YongHyeon 			m = m_dup(*m_head, M_DONTWAIT);
1432c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1433c21e84e4SPyun YongHyeon 			if (m == NULL) {
1434c21e84e4SPyun YongHyeon 				*m_head = NULL;
1435c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1436c21e84e4SPyun YongHyeon 			}
1437c21e84e4SPyun YongHyeon 			*m_head = m;
1438c21e84e4SPyun YongHyeon 		}
1439c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1440c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1441c21e84e4SPyun YongHyeon 		if (m == NULL) {
1442c21e84e4SPyun YongHyeon 			*m_head = NULL;
1443c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1444c21e84e4SPyun YongHyeon 		}
1445c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1446c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1447c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1448c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1449c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1450c21e84e4SPyun YongHyeon 			if (m == NULL) {
1451c21e84e4SPyun YongHyeon 				*m_head = NULL;
1452c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1453c21e84e4SPyun YongHyeon 			}
1454c21e84e4SPyun YongHyeon 		}
1455c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1456c21e84e4SPyun YongHyeon 		if (m == NULL) {
1457c21e84e4SPyun YongHyeon 			*m_head = NULL;
1458c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1459c21e84e4SPyun YongHyeon 		}
1460c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1461c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1462c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1463c21e84e4SPyun YongHyeon 		if (m == NULL) {
1464c21e84e4SPyun YongHyeon 			*m_head = NULL;
1465c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1466c21e84e4SPyun YongHyeon 		}
1467c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1468c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr) + tcp->th_off);
1469c21e84e4SPyun YongHyeon 		if (m == NULL) {
1470c21e84e4SPyun YongHyeon 			*m_head = NULL;
1471c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1472c21e84e4SPyun YongHyeon 		}
1473c21e84e4SPyun YongHyeon 
1474c21e84e4SPyun YongHyeon 		/*
1475c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1476c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1477c21e84e4SPyun YongHyeon 		 */
1478c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
14790685c824SPyun YongHyeon 		ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) +
14800685c824SPyun YongHyeon 		    (tcp->th_off << 2));
1481c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1482c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1483c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1484c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1485c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1486c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1487c21e84e4SPyun YongHyeon 		*m_head = m;
1488*6da6d0a9SPyun YongHyeon 	} else if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
1489*6da6d0a9SPyun YongHyeon 		/*
1490*6da6d0a9SPyun YongHyeon 		 * Deal with TCP/IP checksum offload. Note that
1491*6da6d0a9SPyun YongHyeon 		 * in order for TCP checksum offload to work,
1492*6da6d0a9SPyun YongHyeon 		 * the pseudo header checksum must have already
1493*6da6d0a9SPyun YongHyeon 		 * been computed and stored in the checksum field
1494*6da6d0a9SPyun YongHyeon 		 * in the TCP header. The stack should have
1495*6da6d0a9SPyun YongHyeon 		 * already done this for us.
1496*6da6d0a9SPyun YongHyeon 		 */
1497*6da6d0a9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1498*6da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
1499*6da6d0a9SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
1500*6da6d0a9SPyun YongHyeon 
1501*6da6d0a9SPyun YongHyeon #ifdef FXP_IP_CSUM_WAR
1502*6da6d0a9SPyun YongHyeon 		/*
1503*6da6d0a9SPyun YongHyeon 		 * XXX The 82550 chip appears to have trouble
1504*6da6d0a9SPyun YongHyeon 		 * dealing with IP header checksums in very small
1505*6da6d0a9SPyun YongHyeon 		 * datagrams, namely fragments from 1 to 3 bytes
1506*6da6d0a9SPyun YongHyeon 		 * in size. For example, say you want to transmit
1507*6da6d0a9SPyun YongHyeon 		 * a UDP packet of 1473 bytes. The packet will be
1508*6da6d0a9SPyun YongHyeon 		 * fragmented over two IP datagrams, the latter
1509*6da6d0a9SPyun YongHyeon 		 * containing only one byte of data. The 82550 will
1510*6da6d0a9SPyun YongHyeon 		 * botch the header checksum on the 1-byte fragment.
1511*6da6d0a9SPyun YongHyeon 		 * As long as the datagram contains 4 or more bytes
1512*6da6d0a9SPyun YongHyeon 		 * of data, you're ok.
1513*6da6d0a9SPyun YongHyeon 		 *
1514*6da6d0a9SPyun YongHyeon                  * The following code attempts to work around this
1515*6da6d0a9SPyun YongHyeon 		 * problem: if the datagram is less than 38 bytes
1516*6da6d0a9SPyun YongHyeon 		 * in size (14 bytes ether header, 20 bytes IP header,
1517*6da6d0a9SPyun YongHyeon 		 * plus 4 bytes of data), we punt and compute the IP
1518*6da6d0a9SPyun YongHyeon 		 * header checksum by hand. This workaround doesn't
1519*6da6d0a9SPyun YongHyeon 		 * work very well, however, since it can be fooled
1520*6da6d0a9SPyun YongHyeon 		 * by things like VLAN tags and IP options that make
1521*6da6d0a9SPyun YongHyeon 		 * the header sizes/offsets vary.
1522*6da6d0a9SPyun YongHyeon 		 */
1523*6da6d0a9SPyun YongHyeon 
1524*6da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
1525*6da6d0a9SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
1526*6da6d0a9SPyun YongHyeon 				struct ip *ip;
1527*6da6d0a9SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
1528*6da6d0a9SPyun YongHyeon 				ip = mtod(m, struct ip *);
1529*6da6d0a9SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
1530*6da6d0a9SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
1531*6da6d0a9SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
1532*6da6d0a9SPyun YongHyeon 			} else {
1533*6da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_activation_high =
1534*6da6d0a9SPyun YongHyeon 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
1535*6da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_schedule |=
1536*6da6d0a9SPyun YongHyeon 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
1537*6da6d0a9SPyun YongHyeon 			}
1538*6da6d0a9SPyun YongHyeon 		}
1539*6da6d0a9SPyun YongHyeon #endif
1540c21e84e4SPyun YongHyeon 	}
1541c21e84e4SPyun YongHyeon 
1542a2057a72SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, *m_head,
15434e53f837SPyun YongHyeon 	    segs, &nseg, 0);
15444e53f837SPyun YongHyeon 	if (error == EFBIG) {
15454e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
15464e53f837SPyun YongHyeon 		if (m == NULL) {
15474e53f837SPyun YongHyeon 			m_freem(*m_head);
15484e53f837SPyun YongHyeon 			*m_head = NULL;
15494e53f837SPyun YongHyeon 			return (ENOMEM);
15501104779bSMike Silbersack 		}
15514e53f837SPyun YongHyeon 		*m_head = m;
1552a2057a72SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map,
15534e53f837SPyun YongHyeon 	    	    *m_head, segs, &nseg, 0);
15544e53f837SPyun YongHyeon 		if (error != 0) {
15554e53f837SPyun YongHyeon 			m_freem(*m_head);
15564e53f837SPyun YongHyeon 			*m_head = NULL;
15574e53f837SPyun YongHyeon 			return (ENOMEM);
15584e53f837SPyun YongHyeon 		}
15594e53f837SPyun YongHyeon 	} else if (error != 0)
15604e53f837SPyun YongHyeon 		return (error);
15614e53f837SPyun YongHyeon 	if (nseg == 0) {
15624e53f837SPyun YongHyeon 		m_freem(*m_head);
15634e53f837SPyun YongHyeon 		*m_head = NULL;
15644e53f837SPyun YongHyeon 		return (EIO);
156523a0ed7cSDavid Greenman 	}
156623a0ed7cSDavid Greenman 
156740c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
1568a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1569b2badf02SMaxime Henrion 
157040c20505SMaxime Henrion 	cbp = txp->tx_cb;
157140c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
157240c20505SMaxime Henrion 		/*
157340c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
157440c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
157540c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
157640c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
157740c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
157840c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
157940c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
158040c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
158140c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
158240c20505SMaxime Henrion 		 * starting from a different place depending on whether
158340c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
158440c20505SMaxime Henrion 		 */
158540c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
158668f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
158768f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
158840c20505SMaxime Henrion 		} else {
158940c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
159040c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
159140c20505SMaxime Henrion 		}
159240c20505SMaxime Henrion 	}
1593c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1594c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1595c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
159668f4ab9aSPyun YongHyeon 		cbp->tbd[nseg].tb_size |= htole32(0x8000);
1597c21e84e4SPyun YongHyeon 	} else
159840c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1599c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1600c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1601c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
160268f4ab9aSPyun YongHyeon 		cbp->tbd[1].tb_size |= htole32(tcp_payload << 16);
1603c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1604c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1605c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1606c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1607c21e84e4SPyun YongHyeon 	}
1608bd4fa9d9SPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
1609bd4fa9d9SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
1610bd4fa9d9SPyun YongHyeon 		cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag);
1611bd4fa9d9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_activation_high |=
1612bd4fa9d9SPyun YongHyeon 		    FXP_IPCB_INSERTVLAN_ENABLE;
1613bd4fa9d9SPyun YongHyeon 	}
161440c20505SMaxime Henrion 
16154e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1616b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1617b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
16184e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1619b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
162083e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
162183e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
16224e53f837SPyun YongHyeon 	else
1623b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
162483e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
162583e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1626c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1627b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1628a17c678eSDavid Greenman 
1629a17c678eSDavid Greenman 	/*
1630483b9871SDavid Greenman 	 * Advance the end of list forward.
1631a17c678eSDavid Greenman 	 */
163206175228SAndrew Gallatin 
163350d81222SMaxime Henrion #ifdef __alpha__
163406175228SAndrew Gallatin 	/*
163506175228SAndrew Gallatin 	 * On platforms which can't access memory in 16-bit
163606175228SAndrew Gallatin 	 * granularities, we must prevent the card from DMA'ing
163706175228SAndrew Gallatin 	 * up the status while we update the command field.
163806175228SAndrew Gallatin 	 * This could cause us to overwrite the completion status.
163914fd1071SMaxime Henrion 	 * XXX This is probably bogus and we're _not_ looking
164014fd1071SMaxime Henrion 	 * for atomicity here.
164106175228SAndrew Gallatin 	 */
164214fd1071SMaxime Henrion 	atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1643bafb64afSMaxime Henrion 	    htole16(FXP_CB_COMMAND_S));
164450d81222SMaxime Henrion #else
164540c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
164650d81222SMaxime Henrion #endif /*__alpha__*/
1647b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1648a17c678eSDavid Greenman 
1649a17c678eSDavid Greenman 	/*
16501cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1651b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1652483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1653a17c678eSDavid Greenman 	 */
16541cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1655b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1656a17c678eSDavid Greenman 
16571cd443acSDavid Greenman 	sc->tx_queued++;
16581cd443acSDavid Greenman 
165940c20505SMaxime Henrion 	return (0);
1660a17c678eSDavid Greenman }
1661a17c678eSDavid Greenman 
1662e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1663e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1664e4fc250cSLuigi Rizzo 
16651abcdbd1SAttilio Rao static int
1666e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1667e4fc250cSLuigi Rizzo {
1668e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
166974d1ed23SMaxime Henrion 	uint8_t statack;
16701abcdbd1SAttilio Rao 	int rx_npkts = 0;
1671e4fc250cSLuigi Rizzo 
16724953bccaSNate Lawson 	FXP_LOCK(sc);
167340929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
16744953bccaSNate Lawson 		FXP_UNLOCK(sc);
16751abcdbd1SAttilio Rao 		return (rx_npkts);
1676e4fc250cSLuigi Rizzo 	}
167740929967SGleb Smirnoff 
1678e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1679e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1680e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
168174d1ed23SMaxime Henrion 		uint8_t tmp;
16826481f301SPeter Wemm 
1683e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16844953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16854953bccaSNate Lawson 			FXP_UNLOCK(sc);
16861abcdbd1SAttilio Rao 			return (rx_npkts); /* nothing to do */
16874953bccaSNate Lawson 		}
1688e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1689e4fc250cSLuigi Rizzo 		/* ack what we can */
1690e4fc250cSLuigi Rizzo 		if (tmp != 0)
1691e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1692e4fc250cSLuigi Rizzo 		statack |= tmp;
1693e4fc250cSLuigi Rizzo 	}
16941abcdbd1SAttilio Rao 	rx_npkts = fxp_intr_body(sc, ifp, statack, count);
16954953bccaSNate Lawson 	FXP_UNLOCK(sc);
16961abcdbd1SAttilio Rao 	return (rx_npkts);
1697e4fc250cSLuigi Rizzo }
1698e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1699e4fc250cSLuigi Rizzo 
1700a17c678eSDavid Greenman /*
17019c7d2607SDavid Greenman  * Process interface interrupts.
1702a17c678eSDavid Greenman  */
170394927790SDavid Greenman static void
1704f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1705a17c678eSDavid Greenman {
1706f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1707fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
170874d1ed23SMaxime Henrion 	uint8_t statack;
17090f4dc94cSChuck Paterson 
17104953bccaSNate Lawson 	FXP_LOCK(sc);
1711704d1965SWarner Losh 	if (sc->suspended) {
1712704d1965SWarner Losh 		FXP_UNLOCK(sc);
1713704d1965SWarner Losh 		return;
1714704d1965SWarner Losh 	}
1715704d1965SWarner Losh 
1716e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
171740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
17184953bccaSNate Lawson 		FXP_UNLOCK(sc);
1719e4fc250cSLuigi Rizzo 		return;
17204953bccaSNate Lawson 	}
1721e4fc250cSLuigi Rizzo #endif
1722b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1723a17c678eSDavid Greenman 		/*
172411457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
172511457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
172611457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
172711457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
172811457bbfSJonathan Lemon 		 */
17294953bccaSNate Lawson 		if (statack == 0xff) {
17304953bccaSNate Lawson 			FXP_UNLOCK(sc);
173111457bbfSJonathan Lemon 			return;
17324953bccaSNate Lawson 		}
173311457bbfSJonathan Lemon 
173411457bbfSJonathan Lemon 		/*
1735a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1736a17c678eSDavid Greenman 		 */
1737ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1738c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
17394953bccaSNate Lawson 			fxp_intr_body(sc, ifp, statack, -1);
1740e4fc250cSLuigi Rizzo 	}
17414953bccaSNate Lawson 	FXP_UNLOCK(sc);
1742e4fc250cSLuigi Rizzo }
1743e4fc250cSLuigi Rizzo 
1744e4fc250cSLuigi Rizzo static void
1745b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1746b2badf02SMaxime Henrion {
17474e53f837SPyun YongHyeon 	struct ifnet *ifp;
1748b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1749b2badf02SMaxime Henrion 
17504e53f837SPyun YongHyeon 	ifp = sc->ifp;
1751a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1752a2057a72SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1753b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
175483e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1755b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1756b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1757a2057a72SPyun YongHyeon 			bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
1758b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1759a2057a72SPyun YongHyeon 			bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
1760b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1761b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1762b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1763b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1764b2badf02SMaxime Henrion 		}
1765b2badf02SMaxime Henrion 		sc->tx_queued--;
17664e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1767b2badf02SMaxime Henrion 	}
1768b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1769a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1770a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17716b24912cSPyun YongHyeon 	if (sc->tx_queued == 0)
177225935344SPyun YongHyeon 		sc->watchdog_timer = 0;
1773b2badf02SMaxime Henrion }
1774b2badf02SMaxime Henrion 
1775b2badf02SMaxime Henrion static void
1776f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m,
1777f13075afSPyun YongHyeon     uint16_t status, int pos)
1778f13075afSPyun YongHyeon {
1779f13075afSPyun YongHyeon 	struct ether_header *eh;
1780f13075afSPyun YongHyeon 	struct ip *ip;
1781f13075afSPyun YongHyeon 	struct udphdr *uh;
1782f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1783f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1784f13075afSPyun YongHyeon 
1785f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1786f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1787f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1788f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1789f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1790f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1791f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1792f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1793f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1794f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1795f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1796f13075afSPyun YongHyeon 			}
1797f13075afSPyun YongHyeon 		}
1798f13075afSPyun YongHyeon 		return;
1799f13075afSPyun YongHyeon 	}
1800f13075afSPyun YongHyeon 
1801f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1802f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1803f13075afSPyun YongHyeon 		return;
1804f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1805f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1806f13075afSPyun YongHyeon 		return;
1807f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1808f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1809f13075afSPyun YongHyeon 		return;
1810f13075afSPyun YongHyeon 
1811f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1812f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1813f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1814f13075afSPyun YongHyeon 		return;
1815f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1816f13075afSPyun YongHyeon 		return;
1817f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1818f13075afSPyun YongHyeon 		return;
1819f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1820f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1821f13075afSPyun YongHyeon 
1822f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1823f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1824f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1825f13075afSPyun YongHyeon 			return;
1826f13075afSPyun YongHyeon 		break;
1827f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1828f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1829f13075afSPyun YongHyeon 			return;
1830f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1831f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1832f13075afSPyun YongHyeon 			return; /* no checksum */
1833f13075afSPyun YongHyeon 		break;
1834f13075afSPyun YongHyeon 	default:
1835f13075afSPyun YongHyeon 		return;
1836f13075afSPyun YongHyeon 	}
1837f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1838f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1839f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1840f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1841f13075afSPyun YongHyeon 	if (len > 0) {
1842f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1843f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1844f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1845f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1846f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1847f13075afSPyun YongHyeon 		}
1848f13075afSPyun YongHyeon 	}
1849f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1850f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1851f13075afSPyun YongHyeon }
1852f13075afSPyun YongHyeon 
18531abcdbd1SAttilio Rao static int
185474d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
18554953bccaSNate Lawson     int count)
1856e4fc250cSLuigi Rizzo {
18572b5989e9SLuigi Rizzo 	struct mbuf *m;
1858b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18592b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18602b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
18611abcdbd1SAttilio Rao 	int rx_npkts;
186260bb79ebSPyun YongHyeon 	uint16_t status;
18632b5989e9SLuigi Rizzo 
18641abcdbd1SAttilio Rao 	rx_npkts = 0;
186567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18661abcdbd1SAttilio Rao 
18672b5989e9SLuigi Rizzo 	if (rnr)
18680f1db1d6SMaxime Henrion 		sc->rnr++;
1869947e3815SIan Dowse #ifdef DEVICE_POLLING
1870947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1871947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1872947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1873947e3815SIan Dowse 		rnr = 1;
1874947e3815SIan Dowse 	}
1875947e3815SIan Dowse #endif
1876a17c678eSDavid Greenman 
1877a17c678eSDavid Greenman 	/*
18783114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
187906936301SBill Paul 	 *
188006936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
188106936301SBill Paul 	 * be that this event (control unit not ready) was not
188206936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
188306936301SBill Paul 	 * The exact sequence of events that occur when the interface
188406936301SBill Paul 	 * is brought up are different now, and if this event
188506936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
188606936301SBill Paul 	 * can stall for several seconds. The result is that no
188706936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
188806936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
18893114fdb4SDavid Greenman 	 */
18904e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1891b2badf02SMaxime Henrion 		fxp_txeof(sc);
18923114fdb4SDavid Greenman 
18933114fdb4SDavid Greenman 	/*
18943114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
18953114fdb4SDavid Greenman 	 */
18967929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
18974953bccaSNate Lawson 		fxp_start_body(ifp);
18982b5989e9SLuigi Rizzo 
18992b5989e9SLuigi Rizzo 	/*
19002b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
19012b5989e9SLuigi Rizzo 	 */
1902947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
19031abcdbd1SAttilio Rao 		return (rx_npkts);
19042b5989e9SLuigi Rizzo 
19053114fdb4SDavid Greenman 	/*
1906a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1907a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1908a17c678eSDavid Greenman 	 * re-start the receiver.
1909947e3815SIan Dowse 	 *
19102b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
19112b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
19122b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
19132b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1914947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1915947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1916a17c678eSDavid Greenman 	 */
19172b5989e9SLuigi Rizzo 	for (;;) {
1918b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1919b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1920ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1921ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1922a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
1923b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1924a17c678eSDavid Greenman 
1925e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1926947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1927947e3815SIan Dowse 			if (rnr) {
1928947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1929947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1930947e3815SIan Dowse 				rnr = 0;
1931947e3815SIan Dowse 			}
19322b5989e9SLuigi Rizzo 			break;
1933947e3815SIan Dowse 		}
19342b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
19352b5989e9SLuigi Rizzo 
193660bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
193760bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
19382b5989e9SLuigi Rizzo 			break;
19392b5989e9SLuigi Rizzo 
1940dfe61cf1SDavid Greenman 		/*
1941b2badf02SMaxime Henrion 		 * Advance head forward.
1942dfe61cf1SDavid Greenman 		 */
1943b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1944a17c678eSDavid Greenman 
1945dfe61cf1SDavid Greenman 		/*
1946ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1947ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1948ba8c6fd5SDavid Greenman 		 * instead.
1949dfe61cf1SDavid Greenman 		 */
195085050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1951aed53495SDavid Greenman 			int total_len;
1952a17c678eSDavid Greenman 
1953e8c8b728SJonathan Lemon 			/*
19542b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
19552b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
19562b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
19572b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1958e8c8b728SJonathan Lemon 			 */
1959bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1960f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
1961f13075afSPyun YongHyeon 			    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1962f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1963f13075afSPyun YongHyeon 				total_len -= 2;
1964f13075afSPyun YongHyeon 			}
19652b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
19662b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
196760bb79ebSPyun YongHyeon 				sc->rfa_size || status & FXP_RFA_STATUS_CRC) {
1968e8c8b728SJonathan Lemon 				m_freem(m);
19692b5989e9SLuigi Rizzo 				continue;
1970e8c8b728SJonathan Lemon 			}
1971920b58e8SBrooks Davis 
19722e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1973673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1974673d9191SSam Leffler 
1975f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
1976f13075afSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1977f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
1978bd4fa9d9SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
1979bd4fa9d9SPyun YongHyeon 			    (status & FXP_RFA_STATUS_VLAN) != 0) {
1980bd4fa9d9SPyun YongHyeon 				m->m_pkthdr.ether_vtag =
1981bd4fa9d9SPyun YongHyeon 				    ntohs(rfa->rfax_vlan_id);
1982bd4fa9d9SPyun YongHyeon 				m->m_flags |= M_VLANTAG;
1983bd4fa9d9SPyun YongHyeon 			}
198405fb8c3fSNate Lawson 			/*
198505fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
198605fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
198705fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
198805fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
198905fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
199005fb8c3fSNate Lawson 			 * calling if_input() on each one.
199105fb8c3fSNate Lawson 			 */
199205fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
1993673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
199405fb8c3fSNate Lawson 			FXP_LOCK(sc);
19951abcdbd1SAttilio Rao 			rx_npkts++;
1996c109e385SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1997c109e385SPyun YongHyeon 				return (rx_npkts);
199885050421SPyun YongHyeon 		} else {
199985050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
200085050421SPyun YongHyeon 			ifp->if_iqdrops++;
200185050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
2002a17c678eSDavid Greenman 		}
200385050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
2004a17c678eSDavid Greenman 	}
20052b5989e9SLuigi Rizzo 	if (rnr) {
2006ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
2007ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
2008b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
20092e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2010a17c678eSDavid Greenman 	}
20111abcdbd1SAttilio Rao 	return (rx_npkts);
2012a17c678eSDavid Greenman }
2013a17c678eSDavid Greenman 
2014dfe61cf1SDavid Greenman /*
2015dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
2016dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
2017dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
2018dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
2019dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
2020dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
2021dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
2022dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
2023dfe61cf1SDavid Greenman  * them again next time.
2024dfe61cf1SDavid Greenman  */
2025303b270bSEivind Eklund static void
2026f7788e8eSJonathan Lemon fxp_tick(void *xsc)
2027a17c678eSDavid Greenman {
2028f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
2029fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2030a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
2031a17c678eSDavid Greenman 
20323212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2033b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
203483e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
203583e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
2036397f9dfeSDavid Greenman 	if (sp->rx_good) {
203783e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
2038397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
203943d8b117SPyun YongHyeon 	} else if (sc->flags & FXP_FLAG_RXBUG) {
2040c8cc6fcaSDavid Greenman 		/*
2041c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
2042c8cc6fcaSDavid Greenman 		 */
2043397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
2044397f9dfeSDavid Greenman 	}
20453ba65732SDavid Greenman 	ifp->if_ierrors +=
204683e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
204783e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
204883e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
204983e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
2050a17c678eSDavid Greenman 	/*
2051f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
2052f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
2053f9be9005SDavid Greenman 	 */
2054f9be9005SDavid Greenman 	if (sp->tx_underruns) {
205583e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
2056f9be9005SDavid Greenman 		if (tx_threshold < 192)
2057f9be9005SDavid Greenman 			tx_threshold += 64;
2058f9be9005SDavid Greenman 	}
20594953bccaSNate Lawson 
2060397f9dfeSDavid Greenman 	/*
2061c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
2062c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
2063c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
2064c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
2065c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
2066c8cc6fcaSDavid Greenman 	 */
2067b2badf02SMaxime Henrion 	fxp_txeof(sc);
2068b2badf02SMaxime Henrion 
2069c8cc6fcaSDavid Greenman 	/*
2070397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
2071397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2072397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2073397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2074397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
2075397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2076397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2077397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2078397f9dfeSDavid Greenman 	 */
2079397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2080397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
2081c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
20826b24912cSPyun YongHyeon 			fxp_init_body(sc);
20836b24912cSPyun YongHyeon 		return;
2084397f9dfeSDavid Greenman 	}
2085f9be9005SDavid Greenman 	/*
20863ba65732SDavid Greenman 	 * If there is no pending command, start another stats
20873ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2088a17c678eSDavid Greenman 	 */
2089397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2090a17c678eSDavid Greenman 		/*
2091397f9dfeSDavid Greenman 		 * Start another stats dump.
2092a17c678eSDavid Greenman 		 */
2093b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
2094b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
20952e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2096dfe61cf1SDavid Greenman 	} else {
2097dfe61cf1SDavid Greenman 		/*
2098dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
2099dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
21003ba65732SDavid Greenman 		 * next timer event to update them.
2101dfe61cf1SDavid Greenman 		 */
2102dfe61cf1SDavid Greenman 		sp->tx_good = 0;
2103f9be9005SDavid Greenman 		sp->tx_underruns = 0;
2104dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
21053ba65732SDavid Greenman 
2106dfe61cf1SDavid Greenman 		sp->rx_good = 0;
21073ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
21083ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
21093ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
21103ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
2111dfe61cf1SDavid Greenman 	}
2112f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2113f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
21144953bccaSNate Lawson 
2115a17c678eSDavid Greenman 	/*
211616f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2117df79d527SGleb Smirnoff 	 */
2118df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2119df79d527SGleb Smirnoff 
2120df79d527SGleb Smirnoff 	/*
2121a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2122a17c678eSDavid Greenman 	 */
212345276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2124a17c678eSDavid Greenman }
2125a17c678eSDavid Greenman 
2126a17c678eSDavid Greenman /*
2127a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2128a17c678eSDavid Greenman  * the interface.
2129a17c678eSDavid Greenman  */
2130a17c678eSDavid Greenman static void
2131f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2132a17c678eSDavid Greenman {
2133fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2134b2badf02SMaxime Henrion 	struct fxp_tx *txp;
21353ba65732SDavid Greenman 	int i;
2136a17c678eSDavid Greenman 
213713f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2138df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
21397dced78aSDavid Greenman 
2140a17c678eSDavid Greenman 	/*
2141a17c678eSDavid Greenman 	 * Cancel stats updater.
2142a17c678eSDavid Greenman 	 */
214345276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
21443ba65732SDavid Greenman 
21453ba65732SDavid Greenman 	/*
21467137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
21477137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
21483ba65732SDavid Greenman 	 */
21497137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
215072a32a26SJonathan Lemon 	DELAY(50);
21517137cea0SPyun YongHyeon 	/* Disable interrupts. */
21527137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2153a17c678eSDavid Greenman 
21543ba65732SDavid Greenman 	/*
21553ba65732SDavid Greenman 	 * Release any xmit buffers.
21563ba65732SDavid Greenman 	 */
2157b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2158da91462dSDavid Greenman 	if (txp != NULL) {
2159da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
2160b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
2161a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp[i].tx_map,
2162b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
2163a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag,
2164a2057a72SPyun YongHyeon 				    txp[i].tx_map);
2165b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
2166b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
2167c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
2168b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
2169da91462dSDavid Greenman 			}
2170da91462dSDavid Greenman 		}
21713ba65732SDavid Greenman 	}
2172a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2173a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
21743ba65732SDavid Greenman 	sc->tx_queued = 0;
2175a17c678eSDavid Greenman }
2176a17c678eSDavid Greenman 
2177a17c678eSDavid Greenman /*
2178a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2179a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2180a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2181a17c678eSDavid Greenman  * card has wedged for some reason.
2182a17c678eSDavid Greenman  */
2183a17c678eSDavid Greenman static void
2184df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2185a17c678eSDavid Greenman {
2186ba8c6fd5SDavid Greenman 
2187df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2188df79d527SGleb Smirnoff 
2189df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2190df79d527SGleb Smirnoff 		return;
2191df79d527SGleb Smirnoff 
2192f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2193df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
2194a17c678eSDavid Greenman 
21954953bccaSNate Lawson 	fxp_init_body(sc);
2196a17c678eSDavid Greenman }
2197a17c678eSDavid Greenman 
21984953bccaSNate Lawson /*
21994953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
22004953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
22014953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
22024953bccaSNate Lawson  */
2203a17c678eSDavid Greenman static void
2204f7788e8eSJonathan Lemon fxp_init(void *xsc)
2205a17c678eSDavid Greenman {
2206fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
22074953bccaSNate Lawson 
22084953bccaSNate Lawson 	FXP_LOCK(sc);
22094953bccaSNate Lawson 	fxp_init_body(sc);
22104953bccaSNate Lawson 	FXP_UNLOCK(sc);
22114953bccaSNate Lawson }
22124953bccaSNate Lawson 
22134953bccaSNate Lawson /*
22144953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
22154953bccaSNate Lawson  * softc lock held.
22164953bccaSNate Lawson  */
22174953bccaSNate Lawson static void
22184953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
22194953bccaSNate Lawson {
2220fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2221a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2222a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2223b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2224b2badf02SMaxime Henrion 	struct fxp_tx *txp;
22253212724cSJohn Baldwin 	int i, prm;
2226a17c678eSDavid Greenman 
222767fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2228a17c678eSDavid Greenman 	/*
22293ba65732SDavid Greenman 	 * Cancel any pending I/O
2230a17c678eSDavid Greenman 	 */
22313ba65732SDavid Greenman 	fxp_stop(sc);
2232a17c678eSDavid Greenman 
22337137cea0SPyun YongHyeon 	/*
22347137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
22357137cea0SPyun YongHyeon 	 */
22367137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
22377137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
22387137cea0SPyun YongHyeon 	DELAY(50);
22397137cea0SPyun YongHyeon 
2240a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
2241a17c678eSDavid Greenman 
2242a17c678eSDavid Greenman 	/*
2243a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2244a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2245a17c678eSDavid Greenman 	 */
2246ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
22472e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2248a17c678eSDavid Greenman 
2249ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
22502e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2251a17c678eSDavid Greenman 
2252a17c678eSDavid Greenman 	/*
2253a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2254a17c678eSDavid Greenman 	 */
2255ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2256b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
2257b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
22582e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2259a17c678eSDavid Greenman 
2260a17c678eSDavid Greenman 	/*
226172a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
2262b96ad4b2SPyun YongHyeon 	 * For ICH based controllers do not load microcode.
226372a32a26SJonathan Lemon 	 */
2264b96ad4b2SPyun YongHyeon 	if (sc->ident->ich == 0) {
2265b96ad4b2SPyun YongHyeon 		if (ifp->if_flags & IFF_LINK0 &&
2266b96ad4b2SPyun YongHyeon 		    (sc->flags & FXP_FLAG_UCODE) == 0)
226772a32a26SJonathan Lemon 			fxp_load_ucode(sc);
2268b96ad4b2SPyun YongHyeon 	}
226972a32a26SJonathan Lemon 
227072a32a26SJonathan Lemon 	/*
22716b24912cSPyun YongHyeon 	 * Set IFF_ALLMULTI status. It's needed in configure action
22726b24912cSPyun YongHyeon 	 * command.
227309882363SJonathan Lemon 	 */
22746b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
227509882363SJonathan Lemon 
227609882363SJonathan Lemon 	/*
2277a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2278a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2279a17c678eSDavid Greenman 	 * later.
2280a17c678eSDavid Greenman 	 */
2281b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2282a17c678eSDavid Greenman 
2283a17c678eSDavid Greenman 	/*
2284a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2285a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2286a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2287a17c678eSDavid Greenman 	 */
2288b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2289a17c678eSDavid Greenman 
2290a17c678eSDavid Greenman 	cbp->cb_status =	0;
229183e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
229283e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
229383e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
22942c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2295001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2296001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2297a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2298f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2299f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2300f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2301f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2302001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2303001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2304f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2305a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2306f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2307f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
23083114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2309f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2310f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2311f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
23128ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2313a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2314f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2315f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2316c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2317c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2318f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2319f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2320f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
2321f13075afSPyun YongHyeon 	    (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0;
2322f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2323f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2324f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2325f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2326a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2327a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2328a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2329a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2330a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2331a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2332a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2333a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2334f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2335f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2336f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2337f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2338f7788e8eSJonathan Lemon 
2339a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2340a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2341a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2342f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2343f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
23447137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2345a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
23463ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2347a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
23486b24912cSPyun YongHyeon 	cbp->mc_all =		ifp->if_flags & IFF_ALLMULTI ? 1 : 0;
2349c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2350bd4fa9d9SPyun YongHyeon 	cbp->vlan_strip_en =	((sc->flags & FXP_FLAG_EXT_RFA) != 0 &&
2351bd4fa9d9SPyun YongHyeon 	    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0;
2352a17c678eSDavid Greenman 
23530f1db1d6SMaxime Henrion 	if (sc->tunable_noflow || sc->revision == FXP_REV_82557) {
23543bd07cfdSJonathan Lemon 		/*
23553bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
23563bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
23573bd07cfdSJonathan Lemon 		 */
23583bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
23593bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
23603bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23613bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
23623bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
23633bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
23643bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
23653bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
23663bd07cfdSJonathan Lemon 	} else {
23673bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
23683bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
23693bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23703bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
23713bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
23723bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
23733bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
23743bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
23753bd07cfdSJonathan Lemon 	}
23763bd07cfdSJonathan Lemon 
2377a17c678eSDavid Greenman 	/*
2378a17c678eSDavid Greenman 	 * Start the config command/DMA.
2379a17c678eSDavid Greenman 	 */
2380ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23815986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
23825986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2383b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
23842e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2385a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2386209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2387a17c678eSDavid Greenman 
2388a17c678eSDavid Greenman 	/*
2389a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2390a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2391a17c678eSDavid Greenman 	 */
2392b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2393a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
239483e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
239583e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
23964a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2397a17c678eSDavid Greenman 
2398a17c678eSDavid Greenman 	/*
2399a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2400a17c678eSDavid Greenman 	 */
2401ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24025986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24035986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
24046b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24052e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2406a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2407209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2408a17c678eSDavid Greenman 
2409a17c678eSDavid Greenman 	/*
24106b24912cSPyun YongHyeon 	 * Initialize the multicast address list.
24116b24912cSPyun YongHyeon 	 */
24126b24912cSPyun YongHyeon 	fxp_mc_setup(sc);
24136b24912cSPyun YongHyeon 
24146b24912cSPyun YongHyeon 	/*
2415a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2416a17c678eSDavid Greenman 	 */
2417b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2418b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2419b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2420a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2421b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
242283e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
242383e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
242483e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
242583e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
24263bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2427b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
242883e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
24293bd07cfdSJonathan Lemon 		else
2430b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
243183e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2432b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2433a17c678eSDavid Greenman 	}
2434a17c678eSDavid Greenman 	/*
2435397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2436a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2437a17c678eSDavid Greenman 	 */
243883e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2439a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2440a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2441b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2442397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2443a17c678eSDavid Greenman 
2444ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24456b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24462e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2447a17c678eSDavid Greenman 
2448a17c678eSDavid Greenman 	/*
2449a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2450a17c678eSDavid Greenman 	 */
2451ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2452b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
24532e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2454a17c678eSDavid Greenman 
2455dccee1a1SDavid Greenman 	/*
2456ba8c6fd5SDavid Greenman 	 * Set current media.
2457dccee1a1SDavid Greenman 	 */
2458f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2459f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2460dccee1a1SDavid Greenman 
246113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
246213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2463e8c8b728SJonathan Lemon 
2464e8c8b728SJonathan Lemon 	/*
2465e8c8b728SJonathan Lemon 	 * Enable interrupts.
2466e8c8b728SJonathan Lemon 	 */
24672b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
24682b5989e9SLuigi Rizzo 	/*
24692b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
24702b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
24712b5989e9SLuigi Rizzo 	 */
247240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
24732b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
24742b5989e9SLuigi Rizzo 	else
24752b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2476e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2477a17c678eSDavid Greenman 
2478a17c678eSDavid Greenman 	/*
2479a17c678eSDavid Greenman 	 * Start stats updater.
2480a17c678eSDavid Greenman 	 */
248145276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2482f7788e8eSJonathan Lemon }
2483f7788e8eSJonathan Lemon 
2484f7788e8eSJonathan Lemon static int
2485f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2486f7788e8eSJonathan Lemon {
2487f7788e8eSJonathan Lemon 
2488f7788e8eSJonathan Lemon 	return (0);
2489a17c678eSDavid Greenman }
2490a17c678eSDavid Greenman 
2491303b270bSEivind Eklund static void
2492f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2493ba8c6fd5SDavid Greenman {
2494ba8c6fd5SDavid Greenman 
2495f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2496ba8c6fd5SDavid Greenman }
2497ba8c6fd5SDavid Greenman 
2498ba8c6fd5SDavid Greenman /*
2499ba8c6fd5SDavid Greenman  * Change media according to request.
2500ba8c6fd5SDavid Greenman  */
2501f7788e8eSJonathan Lemon static int
2502f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2503ba8c6fd5SDavid Greenman {
2504ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2505f7788e8eSJonathan Lemon 	struct mii_data *mii;
2506ba8c6fd5SDavid Greenman 
2507f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25083212724cSJohn Baldwin 	FXP_LOCK(sc);
25095aa0cdf4SJohn-Mark Gurney 	if (mii->mii_instance) {
25105aa0cdf4SJohn-Mark Gurney 		struct mii_softc	*miisc;
25115aa0cdf4SJohn-Mark Gurney 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
25125aa0cdf4SJohn-Mark Gurney 			mii_phy_reset(miisc);
25135aa0cdf4SJohn-Mark Gurney 	}
2514f7788e8eSJonathan Lemon 	mii_mediachg(mii);
25153212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2516ba8c6fd5SDavid Greenman 	return (0);
2517ba8c6fd5SDavid Greenman }
2518ba8c6fd5SDavid Greenman 
2519ba8c6fd5SDavid Greenman /*
2520ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2521ba8c6fd5SDavid Greenman  */
2522f7788e8eSJonathan Lemon static void
2523f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2524ba8c6fd5SDavid Greenman {
2525ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2526f7788e8eSJonathan Lemon 	struct mii_data *mii;
2527ba8c6fd5SDavid Greenman 
2528f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25293212724cSJohn Baldwin 	FXP_LOCK(sc);
2530f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2531f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2532f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
25332e2b8238SJonathan Lemon 
25342b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
25352b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
25362e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
25372e2b8238SJonathan Lemon 	else
25382e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
25393212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2540ba8c6fd5SDavid Greenman }
2541ba8c6fd5SDavid Greenman 
2542a17c678eSDavid Greenman /*
2543a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2544a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
254585050421SPyun YongHyeon  * reusing the RFA buffer.
2546a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2547a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2548a17c678eSDavid Greenman  */
2549a17c678eSDavid Greenman static int
255085050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2551a17c678eSDavid Greenman {
2552a17c678eSDavid Greenman 	struct mbuf *m;
255385050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2554b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
255585050421SPyun YongHyeon 	int error;
2556a17c678eSDavid Greenman 
2557a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
255885050421SPyun YongHyeon 	if (m == NULL)
255985050421SPyun YongHyeon 		return (ENOBUFS);
2560ba8c6fd5SDavid Greenman 
2561ba8c6fd5SDavid Greenman 	/*
2562ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2563ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2564ba8c6fd5SDavid Greenman 	 */
2565ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2566ba8c6fd5SDavid Greenman 
2567eadd5e3aSDavid Greenman 	/*
2568eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2569eadd5e3aSDavid Greenman 	 * data start past it.
2570eadd5e3aSDavid Greenman 	 */
2571a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2572c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
257383e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2574eadd5e3aSDavid Greenman 
2575a17c678eSDavid Greenman 	rfa->rfa_status = 0;
257683e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2577a17c678eSDavid Greenman 	rfa->actual_size = 0;
257885050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
257985050421SPyun YongHyeon 	    sc->rfa_size;
2580ba8c6fd5SDavid Greenman 
258128935f27SMaxime Henrion 	/*
258228935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
258328935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
258428935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
258528935f27SMaxime Henrion 	 * is also alignment-safe.
258628935f27SMaxime Henrion 	 */
258783e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
258883e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2589ba8c6fd5SDavid Greenman 
2590b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2591a2057a72SPyun YongHyeon 	error = bus_dmamap_load(sc->fxp_rxmtag, sc->spare_map, rfa,
2592b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2593b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2594b2badf02SMaxime Henrion 	if (error) {
2595b2badf02SMaxime Henrion 		m_freem(m);
2596b2badf02SMaxime Henrion 		return (error);
2597b2badf02SMaxime Henrion 	}
2598b2badf02SMaxime Henrion 
2599e2157cf7SPyun YongHyeon 	if (rxp->rx_mbuf != NULL)
2600a2057a72SPyun YongHyeon 		bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
2601b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2602b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2603b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2604b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2605b2badf02SMaxime Henrion 
2606a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
2607b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
260885050421SPyun YongHyeon 	return (0);
260985050421SPyun YongHyeon }
261085050421SPyun YongHyeon 
261185050421SPyun YongHyeon static void
261285050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
261385050421SPyun YongHyeon {
261485050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
261585050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2616b2badf02SMaxime Henrion 
2617dfe61cf1SDavid Greenman 	/*
2618dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2619dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2620dfe61cf1SDavid Greenman 	 */
2621b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2622b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2623b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2624b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2625b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
262683e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2627aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2628a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, p_rx->rx_map,
26294cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2630a17c678eSDavid Greenman 	} else {
2631b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2632b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2633a17c678eSDavid Greenman 	}
2634b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
263585050421SPyun YongHyeon }
263685050421SPyun YongHyeon 
263785050421SPyun YongHyeon static void
263885050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
263985050421SPyun YongHyeon {
264085050421SPyun YongHyeon 	struct mbuf *m;
264185050421SPyun YongHyeon 	struct fxp_rfa *rfa;
264285050421SPyun YongHyeon 
264385050421SPyun YongHyeon 	m = rxp->rx_mbuf;
264485050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
264585050421SPyun YongHyeon 	/*
264685050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
264785050421SPyun YongHyeon 	 * will be 32-bit aligned.
264885050421SPyun YongHyeon 	 */
264985050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
265085050421SPyun YongHyeon 
265185050421SPyun YongHyeon 	/*
265285050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
265385050421SPyun YongHyeon 	 * data start past it.
265485050421SPyun YongHyeon 	 */
265585050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
265685050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
265785050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
265885050421SPyun YongHyeon 
265985050421SPyun YongHyeon 	rfa->rfa_status = 0;
266085050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
266185050421SPyun YongHyeon 	rfa->actual_size = 0;
266285050421SPyun YongHyeon 
266385050421SPyun YongHyeon 	/*
266485050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
266585050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
266685050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
266785050421SPyun YongHyeon 	 * is also alignment-safe.
266885050421SPyun YongHyeon 	 */
266985050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
267085050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
267185050421SPyun YongHyeon 
2672a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
267385050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2674a17c678eSDavid Greenman }
2675a17c678eSDavid Greenman 
2676f1928b0cSKevin Lo static int
2677f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2678dccee1a1SDavid Greenman {
2679f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2680dccee1a1SDavid Greenman 	int count = 10000;
26816ebc3153SDavid Greenman 	int value;
2682dccee1a1SDavid Greenman 
2683ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2684ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2685dccee1a1SDavid Greenman 
2686ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2687ba8c6fd5SDavid Greenman 	    && count--)
26886ebc3153SDavid Greenman 		DELAY(10);
2689dccee1a1SDavid Greenman 
2690dccee1a1SDavid Greenman 	if (count <= 0)
2691f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2692dccee1a1SDavid Greenman 
26936ebc3153SDavid Greenman 	return (value & 0xffff);
2694dccee1a1SDavid Greenman }
2695dccee1a1SDavid Greenman 
269616ec4b00SWarner Losh static int
2697f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2698dccee1a1SDavid Greenman {
2699f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2700dccee1a1SDavid Greenman 	int count = 10000;
2701dccee1a1SDavid Greenman 
2702ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2703ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2704ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2705dccee1a1SDavid Greenman 
2706ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2707ba8c6fd5SDavid Greenman 	    count--)
27086ebc3153SDavid Greenman 		DELAY(10);
2709dccee1a1SDavid Greenman 
2710dccee1a1SDavid Greenman 	if (count <= 0)
2711f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
271216ec4b00SWarner Losh 	return (0);
2713dccee1a1SDavid Greenman }
2714dccee1a1SDavid Greenman 
2715dccee1a1SDavid Greenman static int
2716f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2717a17c678eSDavid Greenman {
27189b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2719a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2720f7788e8eSJonathan Lemon 	struct mii_data *mii;
272160bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2722a17c678eSDavid Greenman 
2723a17c678eSDavid Greenman 	switch (command) {
2724a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
27253212724cSJohn Baldwin 		FXP_LOCK(sc);
2726a17c678eSDavid Greenman 		/*
2727a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2728a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2729a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2730a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2731a17c678eSDavid Greenman 		 */
2732a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
27336b24912cSPyun YongHyeon 			if (((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) &&
27346b24912cSPyun YongHyeon 			    ((ifp->if_flags ^ sc->if_flags) &
27356b24912cSPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI | IFF_LINK0)) != 0)
27366b24912cSPyun YongHyeon 				fxp_init_body(sc);
27376b24912cSPyun YongHyeon 			else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
27384953bccaSNate Lawson 				fxp_init_body(sc);
2739a17c678eSDavid Greenman 		} else {
27406b24912cSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
27414a5f1499SDavid Greenman 				fxp_stop(sc);
2742a17c678eSDavid Greenman 		}
27436b24912cSPyun YongHyeon 		sc->if_flags = ifp->if_flags;
27443212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2745a17c678eSDavid Greenman 		break;
2746a17c678eSDavid Greenman 
2747a17c678eSDavid Greenman 	case SIOCADDMULTI:
2748a17c678eSDavid Greenman 	case SIOCDELMULTI:
27496b24912cSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
27506b24912cSPyun YongHyeon 			fxp_init(sc);
2751ba8c6fd5SDavid Greenman 		break;
2752ba8c6fd5SDavid Greenman 
2753ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2754ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2755f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2756f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2757f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2758f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2759f7788e8eSJonathan Lemon 		} else {
2760ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2761f7788e8eSJonathan Lemon 		}
2762a17c678eSDavid Greenman 		break;
2763a17c678eSDavid Greenman 
2764fb917226SRuslan Ermilov 	case SIOCSIFCAP:
276560bb79ebSPyun YongHyeon 		reinit = 0;
27668ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
276740929967SGleb Smirnoff #ifdef DEVICE_POLLING
276840929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
276940929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
277040929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
277140929967SGleb Smirnoff 				if (error)
277240929967SGleb Smirnoff 					return(error);
277340929967SGleb Smirnoff 				FXP_LOCK(sc);
277440929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
277540929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
277640929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
277740929967SGleb Smirnoff 				FXP_UNLOCK(sc);
277840929967SGleb Smirnoff 			} else {
277940929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
278040929967SGleb Smirnoff 				/* Enable interrupts in any case */
278140929967SGleb Smirnoff 				FXP_LOCK(sc);
278240929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
278340929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
278440929967SGleb Smirnoff 				FXP_UNLOCK(sc);
278540929967SGleb Smirnoff 			}
278640929967SGleb Smirnoff 		}
278740929967SGleb Smirnoff #endif
278840929967SGleb Smirnoff 		FXP_LOCK(sc);
278960bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
279060bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
279160bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
279260bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
279360bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
279460bb79ebSPyun YongHyeon 			else
279560bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
279660bb79ebSPyun YongHyeon 		}
279760bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2798f13075afSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
279960bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2800f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2801f13075afSPyun YongHyeon 				reinit++;
2802f13075afSPyun YongHyeon 		}
2803c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
2804c21e84e4SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2805c21e84e4SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2806c21e84e4SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
2807c21e84e4SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2808c21e84e4SPyun YongHyeon 			else
2809c21e84e4SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2810c21e84e4SPyun YongHyeon 		}
28117137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
28127137cea0SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
28137137cea0SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
281460bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
281560bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
28168ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
28178ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
28188ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
28198ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
28208ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
28218ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
28228ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
282360bb79ebSPyun YongHyeon 				reinit++;
282460bb79ebSPyun YongHyeon 		}
2825bd4fa9d9SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2826bd4fa9d9SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2827bd4fa9d9SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2828bd4fa9d9SPyun YongHyeon 				reinit++;
2829bd4fa9d9SPyun YongHyeon 		}
2830bd4fa9d9SPyun YongHyeon 		if (reinit > 0 && ifp->if_flags & IFF_UP)
28318ef1f631SYaroslav Tykhiy 			fxp_init_body(sc);
28323212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2833bd4fa9d9SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
2834fb917226SRuslan Ermilov 		break;
2835fb917226SRuslan Ermilov 
2836a17c678eSDavid Greenman 	default:
2837673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2838a17c678eSDavid Greenman 	}
2839a17c678eSDavid Greenman 	return (error);
2840a17c678eSDavid Greenman }
2841397f9dfeSDavid Greenman 
2842397f9dfeSDavid Greenman /*
284309882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
284409882363SJonathan Lemon  */
284509882363SJonathan Lemon static int
284609882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
284709882363SJonathan Lemon {
284809882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2849fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
285009882363SJonathan Lemon 	struct ifmultiaddr *ifma;
285109882363SJonathan Lemon 	int nmcasts;
285209882363SJonathan Lemon 
285309882363SJonathan Lemon 	nmcasts = 0;
28546b24912cSPyun YongHyeon 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2855eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
285609882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
285709882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
285809882363SJonathan Lemon 				continue;
285909882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
28606b24912cSPyun YongHyeon 				ifp->if_flags |= IFF_ALLMULTI;
286109882363SJonathan Lemon 				nmcasts = 0;
286209882363SJonathan Lemon 				break;
286309882363SJonathan Lemon 			}
286409882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2865bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
286609882363SJonathan Lemon 			nmcasts++;
286709882363SJonathan Lemon 		}
2868eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
286909882363SJonathan Lemon 	}
2870bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
287109882363SJonathan Lemon 	return (nmcasts);
287209882363SJonathan Lemon }
287309882363SJonathan Lemon 
287409882363SJonathan Lemon /*
2875397f9dfeSDavid Greenman  * Program the multicast filter.
2876397f9dfeSDavid Greenman  *
2877397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2878397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
28793114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2880397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2881dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2882397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2883397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2884397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2885397f9dfeSDavid Greenman  */
2886397f9dfeSDavid Greenman static void
2887f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2888397f9dfeSDavid Greenman {
28896b24912cSPyun YongHyeon 	struct fxp_cb_mcs *mcsp;
28907dced78aSDavid Greenman 	int count;
2891397f9dfeSDavid Greenman 
289267fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
28933114fdb4SDavid Greenman 
28946b24912cSPyun YongHyeon 	mcsp = sc->mcsp;
2895397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
28966b24912cSPyun YongHyeon 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
28976b24912cSPyun YongHyeon 	mcsp->link_addr = 0xffffffff;
28986b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
2899397f9dfeSDavid Greenman 
2900397f9dfeSDavid Greenman 	/*
29016b24912cSPyun YongHyeon 	 * Wait until command unit is idle. This should never be the
29026b24912cSPyun YongHyeon 	 * case when nothing is queued, but make sure anyway.
2903397f9dfeSDavid Greenman 	 */
29047dced78aSDavid Greenman 	count = 100;
29056b24912cSPyun YongHyeon 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) !=
29066b24912cSPyun YongHyeon 	    FXP_SCB_CUS_IDLE && --count)
29077dced78aSDavid Greenman 		DELAY(10);
29087dced78aSDavid Greenman 	if (count == 0) {
2909f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
29107dced78aSDavid Greenman 		return;
29117dced78aSDavid Greenman 	}
2912397f9dfeSDavid Greenman 
2913397f9dfeSDavid Greenman 	/*
2914397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2915397f9dfeSDavid Greenman 	 */
2916397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2917a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
2918a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2919b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
29202e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
29216b24912cSPyun YongHyeon 	/* ...and wait for it to complete. */
29226b24912cSPyun YongHyeon 	fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
2923397f9dfeSDavid Greenman }
292472a32a26SJonathan Lemon 
292574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
292674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
292774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
292874d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
292974d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
293074d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2931de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
293272a32a26SJonathan Lemon 
293374d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
293472a32a26SJonathan Lemon 
293572a32a26SJonathan Lemon struct ucode {
293674d1ed23SMaxime Henrion 	uint32_t	revision;
293774d1ed23SMaxime Henrion 	uint32_t	*ucode;
293872a32a26SJonathan Lemon 	int		length;
293972a32a26SJonathan Lemon 	u_short		int_delay_offset;
294072a32a26SJonathan Lemon 	u_short		bundle_max_offset;
294172a32a26SJonathan Lemon } ucode_table[] = {
294272a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
294372a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
294472a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
294572a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
294672a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
294772a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
294872a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
294972a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
295072a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
295172a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2952507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
2953de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
295472a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
295572a32a26SJonathan Lemon };
295672a32a26SJonathan Lemon 
295772a32a26SJonathan Lemon static void
295872a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
295972a32a26SJonathan Lemon {
296072a32a26SJonathan Lemon 	struct ucode *uc;
296172a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
296294a4f968SPyun YongHyeon 	int i;
296372a32a26SJonathan Lemon 
296472a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
296572a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
296672a32a26SJonathan Lemon 			break;
296772a32a26SJonathan Lemon 	if (uc->ucode == NULL)
296872a32a26SJonathan Lemon 		return;
2969b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
297072a32a26SJonathan Lemon 	cbp->cb_status = 0;
297183e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
297283e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
297394a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
297494a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
297572a32a26SJonathan Lemon 	if (uc->int_delay_offset)
297674d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
297783e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
297872a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
297974d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
298083e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
298172a32a26SJonathan Lemon 	/*
298272a32a26SJonathan Lemon 	 * Download the ucode to the chip.
298372a32a26SJonathan Lemon 	 */
298472a32a26SJonathan Lemon 	fxp_scb_wait(sc);
29855986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
29865986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2987b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
298872a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
298972a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2990209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
299172a32a26SJonathan Lemon 	device_printf(sc->dev,
299272a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
299372a32a26SJonathan Lemon 	    sc->tunable_int_delay,
299472a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
299572a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
299672a32a26SJonathan Lemon }
299772a32a26SJonathan Lemon 
299872a32a26SJonathan Lemon static int
299972a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
300072a32a26SJonathan Lemon {
300172a32a26SJonathan Lemon 	int error, value;
300272a32a26SJonathan Lemon 
300372a32a26SJonathan Lemon 	value = *(int *)arg1;
300472a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
300572a32a26SJonathan Lemon 	if (error || !req->newptr)
300672a32a26SJonathan Lemon 		return (error);
300772a32a26SJonathan Lemon 	if (value < low || value > high)
300872a32a26SJonathan Lemon 		return (EINVAL);
300972a32a26SJonathan Lemon 	*(int *)arg1 = value;
301072a32a26SJonathan Lemon 	return (0);
301172a32a26SJonathan Lemon }
301272a32a26SJonathan Lemon 
301372a32a26SJonathan Lemon /*
301472a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
301572a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
301672a32a26SJonathan Lemon  */
301772a32a26SJonathan Lemon static int
301872a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
301972a32a26SJonathan Lemon {
302072a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
302172a32a26SJonathan Lemon }
302272a32a26SJonathan Lemon 
302372a32a26SJonathan Lemon static int
302472a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
302572a32a26SJonathan Lemon {
302672a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
302772a32a26SJonathan Lemon }
3028