xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 8262183e5bef4c91c0ed2fcf9f028349375a1b54)
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 /*
90658c8398SMarius Strobl  * NOTE!  On !x86 we typically 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
10972517829SPyun YongHyeon  * must be one or must be zero.  Set up a template for these bits.
110e0fe5c6dSMarius Strobl  * The actual configuration is performed in fxp_init_body.
111f7788e8eSJonathan Lemon  *
112f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
113f7788e8eSJonathan Lemon  */
114e0fe5c6dSMarius Strobl static const u_char const fxp_cb_config_template[] = {
115f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
116f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
117f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
118f7788e8eSJonathan Lemon 	0x0,	/*  0 */
119f7788e8eSJonathan Lemon 	0x0,	/*  1 */
120f7788e8eSJonathan Lemon 	0x0,	/*  2 */
121f7788e8eSJonathan Lemon 	0x0,	/*  3 */
122f7788e8eSJonathan Lemon 	0x0,	/*  4 */
123f7788e8eSJonathan Lemon 	0x0,	/*  5 */
124f7788e8eSJonathan Lemon 	0x32,	/*  6 */
125f7788e8eSJonathan Lemon 	0x0,	/*  7 */
126f7788e8eSJonathan Lemon 	0x0,	/*  8 */
127f7788e8eSJonathan Lemon 	0x0,	/*  9 */
128f7788e8eSJonathan Lemon 	0x6,	/* 10 */
129f7788e8eSJonathan Lemon 	0x0,	/* 11 */
130f7788e8eSJonathan Lemon 	0x0,	/* 12 */
131f7788e8eSJonathan Lemon 	0x0,	/* 13 */
132f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
133f7788e8eSJonathan Lemon 	0x48,	/* 15 */
134f7788e8eSJonathan Lemon 	0x0,	/* 16 */
135f7788e8eSJonathan Lemon 	0x40,	/* 17 */
136f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
137f7788e8eSJonathan Lemon 	0x0,	/* 19 */
138f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
13972517829SPyun YongHyeon 	0x5,	/* 21 */
14072517829SPyun YongHyeon 	0x0,	/* 22 */
14172517829SPyun YongHyeon 	0x0,	/* 23 */
14272517829SPyun YongHyeon 	0x0,	/* 24 */
14372517829SPyun YongHyeon 	0x0,	/* 25 */
14472517829SPyun YongHyeon 	0x0,	/* 26 */
14572517829SPyun YongHyeon 	0x0,	/* 27 */
14672517829SPyun YongHyeon 	0x0,	/* 28 */
14772517829SPyun YongHyeon 	0x0,	/* 29 */
14872517829SPyun YongHyeon 	0x0,	/* 30 */
14972517829SPyun YongHyeon 	0x0	/* 31 */
150f7788e8eSJonathan Lemon };
151f7788e8eSJonathan Lemon 
152f7788e8eSJonathan Lemon /*
153f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
154f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
155f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
156f7788e8eSJonathan Lemon  * them.
157f7788e8eSJonathan Lemon  */
158e0fe5c6dSMarius Strobl static const struct fxp_ident const fxp_ident_table[] = {
159b96ad4b2SPyun YongHyeon     { 0x1029,	-1,	0, "Intel 82559 PCI/CardBus Pro/100" },
160b96ad4b2SPyun YongHyeon     { 0x1030,	-1,	0, "Intel 82559 Pro/100 Ethernet" },
161b96ad4b2SPyun YongHyeon     { 0x1031,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
162b96ad4b2SPyun YongHyeon     { 0x1032,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
163b96ad4b2SPyun YongHyeon     { 0x1033,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
164b96ad4b2SPyun YongHyeon     { 0x1034,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
165b96ad4b2SPyun YongHyeon     { 0x1035,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
166b96ad4b2SPyun YongHyeon     { 0x1036,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
167b96ad4b2SPyun YongHyeon     { 0x1037,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
168b96ad4b2SPyun YongHyeon     { 0x1038,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
169b96ad4b2SPyun YongHyeon     { 0x1039,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
170b96ad4b2SPyun YongHyeon     { 0x103A,	-1,	4, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
171b96ad4b2SPyun YongHyeon     { 0x103B,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
172b96ad4b2SPyun YongHyeon     { 0x103C,	-1,	4, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
173b96ad4b2SPyun YongHyeon     { 0x103D,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
174b96ad4b2SPyun YongHyeon     { 0x103E,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
175b96ad4b2SPyun YongHyeon     { 0x1050,	-1,	5, "Intel 82801BA (D865) Pro/100 VE Ethernet" },
176b96ad4b2SPyun YongHyeon     { 0x1051,	-1,	5, "Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
177b96ad4b2SPyun YongHyeon     { 0x1059,	-1,	0, "Intel 82551QM Pro/100 M Mobile Connection" },
178b96ad4b2SPyun YongHyeon     { 0x1064,	-1,	6, "Intel 82562EZ (ICH6)" },
179b96ad4b2SPyun YongHyeon     { 0x1065,	-1,	6, "Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
180b96ad4b2SPyun YongHyeon     { 0x1068,	-1,	6, "Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
181b96ad4b2SPyun YongHyeon     { 0x1069,	-1,	6, "Intel 82562EM/EX/GX Pro/100 Ethernet" },
182b96ad4b2SPyun YongHyeon     { 0x1091,	-1,	7, "Intel 82562GX Pro/100 Ethernet" },
183b96ad4b2SPyun YongHyeon     { 0x1092,	-1,	7, "Intel Pro/100 VE Network Connection" },
184b96ad4b2SPyun YongHyeon     { 0x1093,	-1,	7, "Intel Pro/100 VM Network Connection" },
185b96ad4b2SPyun YongHyeon     { 0x1094,	-1,	7, "Intel Pro/100 946GZ (ICH7) Network Connection" },
186b96ad4b2SPyun YongHyeon     { 0x1209,	-1,	0, "Intel 82559ER Embedded 10/100 Ethernet" },
187b96ad4b2SPyun YongHyeon     { 0x1229,	0x01,	0, "Intel 82557 Pro/100 Ethernet" },
188b96ad4b2SPyun YongHyeon     { 0x1229,	0x02,	0, "Intel 82557 Pro/100 Ethernet" },
189b96ad4b2SPyun YongHyeon     { 0x1229,	0x03,	0, "Intel 82557 Pro/100 Ethernet" },
190b96ad4b2SPyun YongHyeon     { 0x1229,	0x04,	0, "Intel 82558 Pro/100 Ethernet" },
191b96ad4b2SPyun YongHyeon     { 0x1229,	0x05,	0, "Intel 82558 Pro/100 Ethernet" },
192b96ad4b2SPyun YongHyeon     { 0x1229,	0x06,	0, "Intel 82559 Pro/100 Ethernet" },
193b96ad4b2SPyun YongHyeon     { 0x1229,	0x07,	0, "Intel 82559 Pro/100 Ethernet" },
194b96ad4b2SPyun YongHyeon     { 0x1229,	0x08,	0, "Intel 82559 Pro/100 Ethernet" },
195b96ad4b2SPyun YongHyeon     { 0x1229,	0x09,	0, "Intel 82559ER Pro/100 Ethernet" },
196b96ad4b2SPyun YongHyeon     { 0x1229,	0x0c,	0, "Intel 82550 Pro/100 Ethernet" },
1971343a72fSPyun YongHyeon     { 0x1229,	0x0d,	0, "Intel 82550C Pro/100 Ethernet" },
198b96ad4b2SPyun YongHyeon     { 0x1229,	0x0e,	0, "Intel 82550 Pro/100 Ethernet" },
199b96ad4b2SPyun YongHyeon     { 0x1229,	0x0f,	0, "Intel 82551 Pro/100 Ethernet" },
200b96ad4b2SPyun YongHyeon     { 0x1229,	0x10,	0, "Intel 82551 Pro/100 Ethernet" },
201b96ad4b2SPyun YongHyeon     { 0x1229,	-1,	0, "Intel 82557/8/9 Pro/100 Ethernet" },
202b96ad4b2SPyun YongHyeon     { 0x2449,	-1,	2, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
203b96ad4b2SPyun YongHyeon     { 0x27dc,	-1,	7, "Intel 82801GB (ICH7) 10/100 Ethernet" },
204b96ad4b2SPyun YongHyeon     { 0,	-1,	0, NULL },
205f7788e8eSJonathan Lemon };
206f7788e8eSJonathan Lemon 
207c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
208c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
209c8bca6dcSBill Paul #else
210c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
211c8bca6dcSBill Paul #endif
212c8bca6dcSBill Paul 
213f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
214f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
215f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
216f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
217f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
218f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
219f7788e8eSJonathan Lemon 
220e0fe5c6dSMarius Strobl static const struct fxp_ident *fxp_find_ident(device_t dev);
221f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
222f13075afSPyun YongHyeon static void		fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp,
223f13075afSPyun YongHyeon 			    struct mbuf *m, uint16_t status, int pos);
2241abcdbd1SAttilio Rao static int		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
22574d1ed23SMaxime Henrion 			    uint8_t statack, int count);
226f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2271845b5c3SMarius Strobl static void 		fxp_init_body(struct fxp_softc *sc, int);
228f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
229f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
2304953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
2314e53f837SPyun YongHyeon static int		fxp_encap(struct fxp_softc *sc, struct mbuf **m_head);
2324e53f837SPyun YongHyeon static void		fxp_txeof(struct fxp_softc *sc);
233f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
234f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
235f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
236f7788e8eSJonathan Lemon 			    caddr_t data);
237df79d527SGleb Smirnoff static void 		fxp_watchdog(struct fxp_softc *sc);
23885050421SPyun YongHyeon static void		fxp_add_rfabuf(struct fxp_softc *sc,
23985050421SPyun YongHyeon 			    struct fxp_rx *rxp);
24085050421SPyun YongHyeon static void		fxp_discard_rfabuf(struct fxp_softc *sc,
24185050421SPyun YongHyeon 			    struct fxp_rx *rxp);
24285050421SPyun YongHyeon static int		fxp_new_rfabuf(struct fxp_softc *sc,
24385050421SPyun YongHyeon 			    struct fxp_rx *rxp);
24409882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
245f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
24674d1ed23SMaxime Henrion static uint16_t		fxp_eeprom_getword(struct fxp_softc *sc, int offset,
247f7788e8eSJonathan Lemon 			    int autosize);
24800c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
24974d1ed23SMaxime Henrion 			    uint16_t data);
250f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
251*8262183eSPyun YongHyeon static void		fxp_load_eeprom(struct fxp_softc *sc);
252f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
253f7788e8eSJonathan Lemon 			    int offset, int words);
25400c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
25500c4116bSJonathan Lemon 			    int offset, int words);
256f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
257f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
258f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
259f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
260f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
261f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
262f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
26316ec4b00SWarner Losh static int		fxp_miibus_writereg(device_t dev, int phy, int reg,
264f7788e8eSJonathan Lemon 			    int value);
2651845b5c3SMarius Strobl static void		fxp_miibus_statchg(device_t dev);
26672a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
2678da9c507SPyun YongHyeon static void		fxp_update_stats(struct fxp_softc *sc);
2688da9c507SPyun YongHyeon static void		fxp_sysctl_node(struct fxp_softc *sc);
26972a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
27072a32a26SJonathan Lemon 			    int low, int high);
27172a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
27272a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
27328935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
27428935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
27528935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
27674d1ed23SMaxime Henrion 			    volatile uint16_t *status, bus_dma_tag_t dmat,
277209b07bcSMaxime Henrion 			    bus_dmamap_t map);
278f7788e8eSJonathan Lemon 
279f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
280f7788e8eSJonathan Lemon 	/* Device interface */
281f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
282f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
283f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
284f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
285f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
286f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
287f7788e8eSJonathan Lemon 
288f7788e8eSJonathan Lemon 	/* MII interface */
289f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
290f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
2911845b5c3SMarius Strobl 	DEVMETHOD(miibus_statchg,	fxp_miibus_statchg),
292f7788e8eSJonathan Lemon 
293f7788e8eSJonathan Lemon 	{ 0, 0 }
294f7788e8eSJonathan Lemon };
295f7788e8eSJonathan Lemon 
296f7788e8eSJonathan Lemon static driver_t fxp_driver = {
297f7788e8eSJonathan Lemon 	"fxp",
298f7788e8eSJonathan Lemon 	fxp_methods,
299f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
300f7788e8eSJonathan Lemon };
301f7788e8eSJonathan Lemon 
302f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
303f7788e8eSJonathan Lemon 
304f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
305f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
306f7788e8eSJonathan Lemon 
30705bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
30805bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
30905bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
31005bd8c22SMaxime Henrion 	{ -1, 0 }
31105bd8c22SMaxime Henrion };
31205bd8c22SMaxime Henrion 
31305bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
31405bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
31505bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
31605bd8c22SMaxime Henrion 	{ -1, 0 }
31705bd8c22SMaxime Henrion };
31805bd8c22SMaxime Henrion 
319f7788e8eSJonathan Lemon /*
320dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
321dfe61cf1SDavid Greenman  * completed).
322dfe61cf1SDavid Greenman  */
32328935f27SMaxime Henrion static void
324f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
325a17c678eSDavid Greenman {
3263cf09dd1SMarcel Moolenaar 	union {
3273cf09dd1SMarcel Moolenaar 		uint16_t w;
3283cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3293cf09dd1SMarcel Moolenaar 	} flowctl;
330a17c678eSDavid Greenman 	int i = 10000;
331a17c678eSDavid Greenman 
3327dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3337dced78aSDavid Greenman 		DELAY(2);
3343cf09dd1SMarcel Moolenaar 	if (i == 0) {
3351845b5c3SMarius Strobl 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FC_THRESH);
3361845b5c3SMarius Strobl 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FC_STATUS);
33700c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
338e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
339e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3403cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3413cf09dd1SMarcel Moolenaar 	}
3427dced78aSDavid Greenman }
3437dced78aSDavid Greenman 
34428935f27SMaxime Henrion static void
3452e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3462e2b8238SJonathan Lemon {
3472e2b8238SJonathan Lemon 
3482e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3492e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3502e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3512e2b8238SJonathan Lemon 	}
3522e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3532e2b8238SJonathan Lemon }
3542e2b8238SJonathan Lemon 
35528935f27SMaxime Henrion static void
35674d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
357209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3587dced78aSDavid Greenman {
3595986d0d2SPyun YongHyeon 	int i;
3607dced78aSDavid Greenman 
3615986d0d2SPyun YongHyeon 	for (i = 10000; i > 0; i--) {
3627dced78aSDavid Greenman 		DELAY(2);
3635986d0d2SPyun YongHyeon 		bus_dmamap_sync(dmat, map,
3645986d0d2SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3655986d0d2SPyun YongHyeon 		if ((le16toh(*status) & FXP_CB_STATUS_C) != 0)
3665986d0d2SPyun YongHyeon 			break;
367209b07bcSMaxime Henrion 	}
3687dced78aSDavid Greenman 	if (i == 0)
369f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
370a17c678eSDavid Greenman }
371a17c678eSDavid Greenman 
372e0fe5c6dSMarius Strobl static const struct fxp_ident *
373b96ad4b2SPyun YongHyeon fxp_find_ident(device_t dev)
374a17c678eSDavid Greenman {
37574d1ed23SMaxime Henrion 	uint16_t devid;
37674d1ed23SMaxime Henrion 	uint8_t revid;
377e0fe5c6dSMarius Strobl 	const struct fxp_ident *ident;
378f7788e8eSJonathan Lemon 
37955ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
380f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
381f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
382f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
383f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
384f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
385b96ad4b2SPyun YongHyeon 				return (ident);
386b96ad4b2SPyun YongHyeon 			}
387b96ad4b2SPyun YongHyeon 		}
388b96ad4b2SPyun YongHyeon 	}
389b96ad4b2SPyun YongHyeon 	return (NULL);
390b96ad4b2SPyun YongHyeon }
391b96ad4b2SPyun YongHyeon 
392b96ad4b2SPyun YongHyeon /*
393b96ad4b2SPyun YongHyeon  * Return identification string if this device is ours.
394b96ad4b2SPyun YongHyeon  */
395b96ad4b2SPyun YongHyeon static int
396b96ad4b2SPyun YongHyeon fxp_probe(device_t dev)
397b96ad4b2SPyun YongHyeon {
398e0fe5c6dSMarius Strobl 	const struct fxp_ident *ident;
399b96ad4b2SPyun YongHyeon 
400b96ad4b2SPyun YongHyeon 	ident = fxp_find_ident(dev);
401b96ad4b2SPyun YongHyeon 	if (ident != NULL) {
402f7788e8eSJonathan Lemon 		device_set_desc(dev, ident->name);
403538565c4SWarner Losh 		return (BUS_PROBE_DEFAULT);
40455ce7b51SDavid Greenman 	}
405f7788e8eSJonathan Lemon 	return (ENXIO);
4066182fdbdSPeter Wemm }
4076182fdbdSPeter Wemm 
408b2badf02SMaxime Henrion static void
409b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
410b2badf02SMaxime Henrion {
41174d1ed23SMaxime Henrion 	uint32_t *addr;
412b2badf02SMaxime Henrion 
413b2badf02SMaxime Henrion 	if (error)
414b2badf02SMaxime Henrion 		return;
415b2badf02SMaxime Henrion 
416b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
417b2badf02SMaxime Henrion 	addr = arg;
418b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
419b2badf02SMaxime Henrion }
420b2badf02SMaxime Henrion 
4216182fdbdSPeter Wemm static int
4226182fdbdSPeter Wemm fxp_attach(device_t dev)
423a17c678eSDavid Greenman {
4246720ebccSMaxime Henrion 	struct fxp_softc *sc;
4256720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4266720ebccSMaxime Henrion 	struct fxp_tx *txp;
427b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4286720ebccSMaxime Henrion 	struct ifnet *ifp;
42974d1ed23SMaxime Henrion 	uint32_t val;
430*8262183eSPyun YongHyeon 	uint16_t data;
431fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4321845b5c3SMarius Strobl 	int error, flags, i, pmc, prefer_iomap;
433a17c678eSDavid Greenman 
4346720ebccSMaxime Henrion 	error = 0;
4356720ebccSMaxime Henrion 	sc = device_get_softc(dev);
436f7788e8eSJonathan Lemon 	sc->dev = dev;
4376008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4384953bccaSNate Lawson 	    MTX_DEF);
4393212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4404953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4414953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
442a17c678eSDavid Greenman 
4437ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4447ba33d82SBrooks Davis 	if (ifp == NULL) {
4457ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4467ba33d82SBrooks Davis 		error = ENOSPC;
4477ba33d82SBrooks Davis 		goto fail;
4487ba33d82SBrooks Davis 	}
4497ba33d82SBrooks Davis 
450dfe61cf1SDavid Greenman 	/*
4512bce79a2SMaxim Sobolev 	 * Enable bus mastering.
452df373873SWes Peters 	 */
453cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4549fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
45579495006SWarner Losh 
456df373873SWes Peters 	/*
4579fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4589fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4599fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
460dfe61cf1SDavid Greenman 	 */
4612a05a4ebSMatt Jacob 	prefer_iomap = 0;
46205bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
46305bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
46405bd8c22SMaxime Henrion 	if (prefer_iomap)
46505bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
46605bd8c22SMaxime Henrion 	else
46705bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4689fa6ccfbSMatt Jacob 
46905bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
47005bd8c22SMaxime Henrion 	if (error) {
47105bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
47205bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
47305bd8c22SMaxime Henrion 		else
47405bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
47505bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4769fa6ccfbSMatt Jacob 	}
47705bd8c22SMaxime Henrion 	if (error) {
47805bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4796182fdbdSPeter Wemm 		error = ENXIO;
480a17c678eSDavid Greenman 		goto fail;
481a17c678eSDavid Greenman 	}
48205bd8c22SMaxime Henrion 
4839fa6ccfbSMatt Jacob 	if (bootverbose) {
4849fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
48505bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4866182fdbdSPeter Wemm 	}
4876182fdbdSPeter Wemm 
488f7788e8eSJonathan Lemon 	/*
489a996f023SPyun YongHyeon 	 * Put CU/RU idle state and prepare full reset.
490f7788e8eSJonathan Lemon 	 */
491f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
492f7788e8eSJonathan Lemon 	DELAY(10);
493a996f023SPyun YongHyeon 	/* Full reset and disable interrupts. */
494a996f023SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
495a996f023SPyun YongHyeon 	DELAY(10);
496a996f023SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
497f7788e8eSJonathan Lemon 
498f7788e8eSJonathan Lemon 	/*
499f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
500f7788e8eSJonathan Lemon 	 */
501f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
502*8262183eSPyun YongHyeon 	fxp_load_eeprom(sc);
503f7788e8eSJonathan Lemon 
504f7788e8eSJonathan Lemon 	/*
50593b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
50693b6e2e6SMaxime Henrion 	 */
507b96ad4b2SPyun YongHyeon 	sc->ident = fxp_find_ident(dev);
508b96ad4b2SPyun YongHyeon 	if (sc->ident->ich > 0) {
509b96ad4b2SPyun YongHyeon 		/* Assume ICH controllers are 82559. */
510b96ad4b2SPyun YongHyeon 		sc->revision = FXP_REV_82559_A0;
511b96ad4b2SPyun YongHyeon 	} else {
512*8262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_CNTR];
51393b6e2e6SMaxime Henrion 		if ((data >> 8) == 1)
51493b6e2e6SMaxime Henrion 			sc->revision = FXP_REV_82557;
51593b6e2e6SMaxime Henrion 		else
51693b6e2e6SMaxime Henrion 			sc->revision = pci_get_revid(dev);
517b96ad4b2SPyun YongHyeon 	}
51893b6e2e6SMaxime Henrion 
51993b6e2e6SMaxime Henrion 	/*
5207137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
5217137cea0SPyun YongHyeon 	 */
5227137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
5237137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
524*8262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_ID];
5257137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
5263b0a4aefSJohn Baldwin 		    pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0)
5277137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
5287137cea0SPyun YongHyeon 	}
5297137cea0SPyun YongHyeon 
5301343a72fSPyun YongHyeon 	if (sc->revision == FXP_REV_82550_C) {
5311343a72fSPyun YongHyeon 		/*
5321343a72fSPyun YongHyeon 		 * 82550C with server extension requires microcode to
5331343a72fSPyun YongHyeon 		 * receive fragmented UDP datagrams.  However if the
5341343a72fSPyun YongHyeon 		 * microcode is used for client-only featured 82550C
5351343a72fSPyun YongHyeon 		 * it locks up controller.
5361343a72fSPyun YongHyeon 		 */
537*8262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_COMPAT];
5381343a72fSPyun YongHyeon 		if ((data & 0x0400) == 0)
5391343a72fSPyun YongHyeon 			sc->flags |= FXP_FLAG_NO_UCODE;
5401343a72fSPyun YongHyeon 	}
5411343a72fSPyun YongHyeon 
54243d8b117SPyun YongHyeon 	/* Receiver lock-up workaround detection. */
5436e854927SPyun YongHyeon 	if (sc->revision < FXP_REV_82558_A4) {
544*8262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_COMPAT];
54543d8b117SPyun YongHyeon 		if ((data & 0x03) != 0x03) {
54643d8b117SPyun YongHyeon 			sc->flags |= FXP_FLAG_RXBUG;
54743d8b117SPyun YongHyeon 			device_printf(dev, "Enabling Rx lock-up workaround\n");
54843d8b117SPyun YongHyeon 		}
5496e854927SPyun YongHyeon 	}
55043d8b117SPyun YongHyeon 
5517137cea0SPyun YongHyeon 	/*
5523bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
553f7788e8eSJonathan Lemon 	 */
554*8262183eSPyun YongHyeon 	data = sc->eeprom[FXP_EEPROM_MAP_PRI_PHY];
55593b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5564ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
557dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
558f7788e8eSJonathan Lemon 
5598da9c507SPyun YongHyeon 	fxp_sysctl_node(sc);
56072a32a26SJonathan Lemon 	/*
5612e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
56200c4116bSJonathan Lemon 	 *
56372a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
56472a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
56572a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
56600c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
56700c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
56800c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
56900c4116bSJonathan Lemon 	 *
57000c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5712e2b8238SJonathan Lemon 	 */
572b96ad4b2SPyun YongHyeon 	if ((sc->ident->ich >= 2 && sc->ident->ich <= 3) ||
573b96ad4b2SPyun YongHyeon 	    (sc->ident->ich == 0 && sc->revision >= FXP_REV_82559_A0)) {
574*8262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_ID];
57500c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
57674d1ed23SMaxime Henrion 			uint16_t cksum;
57700c4116bSJonathan Lemon 			int i;
57800c4116bSJonathan Lemon 
57900c4116bSJonathan Lemon 			device_printf(dev,
580001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
58100c4116bSJonathan Lemon 			data &= ~0x02;
582*8262183eSPyun YongHyeon 			sc->eeprom[FXP_EEPROM_MAP_ID] = data;
583*8262183eSPyun YongHyeon 			fxp_write_eeprom(sc, &data, FXP_EEPROM_MAP_ID, 1);
58400c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
58500c4116bSJonathan Lemon 			cksum = 0;
586*8262183eSPyun YongHyeon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++)
587*8262183eSPyun YongHyeon 				cksum += sc->eeprom[i];
58800c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
58900c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
59000c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
59100c4116bSJonathan Lemon 			device_printf(dev,
59200c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
593*8262183eSPyun YongHyeon 			    i, sc->eeprom[i], cksum);
594*8262183eSPyun YongHyeon 			sc->eeprom[i] = cksum;
59500c4116bSJonathan Lemon #if 1
59600c4116bSJonathan Lemon 			/*
59700c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
59800c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
59900c4116bSJonathan Lemon 			 */
6002e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
60100c4116bSJonathan Lemon #endif
60200c4116bSJonathan Lemon 		}
60300c4116bSJonathan Lemon 	}
6042e2b8238SJonathan Lemon 
6052e2b8238SJonathan Lemon 	/*
6063bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
6073bd07cfdSJonathan Lemon 	 */
60872a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
6093bd07cfdSJonathan Lemon 		/*
61074396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
61174396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
61274396a0aSJonathan Lemon 		 * the board to turn on MWI.
6133bd07cfdSJonathan Lemon 		 */
61474396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
61574396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
6163bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
6173bd07cfdSJonathan Lemon 
6183bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
6193bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
62044e0bc11SYaroslav Tykhiy 
62144e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
62244e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
62344e0bc11SYaroslav Tykhiy 	} else {
62444e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
62544e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6263bd07cfdSJonathan Lemon 	}
6273bd07cfdSJonathan Lemon 
628f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
629829b278eSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0) {
630829b278eSPyun YongHyeon 		/* 82559ER does not support Rx checksum offloading. */
631829b278eSPyun YongHyeon 		if (sc->ident->devid != 0x1209)
632f13075afSPyun YongHyeon 			sc->flags |= FXP_FLAG_82559_RXCSUM;
633829b278eSPyun YongHyeon 	}
6343bd07cfdSJonathan Lemon 	/*
635c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
636c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
637c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
638c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
639c8bca6dcSBill Paul 	 */
640507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
641507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
642507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
643c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
644c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
645c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
646f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
647f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
648c8bca6dcSBill Paul 	} else {
649c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
650c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
651c8bca6dcSBill Paul 	}
652c8bca6dcSBill Paul 
653c8bca6dcSBill Paul 	/*
654b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
655b2badf02SMaxime Henrion 	 */
65640c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
657c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
658c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
65940c20505SMaxime Henrion 		sc->maxtxseg--;
660c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
661c21e84e4SPyun YongHyeon 	}
662c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
663c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
664c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
665c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
666a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_txmtag);
667b2badf02SMaxime Henrion 	if (error) {
668a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TX DMA tag\n");
669a2057a72SPyun YongHyeon 		goto fail;
670a2057a72SPyun YongHyeon 	}
671a2057a72SPyun YongHyeon 
672a2057a72SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
673a2057a72SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
674a2057a72SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0,
675a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_rxmtag);
676a2057a72SPyun YongHyeon 	if (error) {
677a2057a72SPyun YongHyeon 		device_printf(dev, "could not create RX DMA tag\n");
678b2badf02SMaxime Henrion 		goto fail;
679b2badf02SMaxime Henrion 	}
680b2badf02SMaxime Henrion 
681c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
682c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
683c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
684c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
685b2badf02SMaxime Henrion 	if (error) {
686a2057a72SPyun YongHyeon 		device_printf(dev, "could not create stats DMA tag\n");
687b2badf02SMaxime Henrion 		goto fail;
688b2badf02SMaxime Henrion 	}
689b2badf02SMaxime Henrion 
690b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
691658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->fxp_smap);
692a2057a72SPyun YongHyeon 	if (error) {
693a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate stats DMA memory\n");
6944953bccaSNate Lawson 		goto fail;
695a2057a72SPyun YongHyeon 	}
696b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
697f9d050a8SPyun YongHyeon 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr,
698f9d050a8SPyun YongHyeon 	    BUS_DMA_NOWAIT);
699b2badf02SMaxime Henrion 	if (error) {
700a2057a72SPyun YongHyeon 		device_printf(dev, "could not load the stats DMA buffer\n");
701b2badf02SMaxime Henrion 		goto fail;
702b2badf02SMaxime Henrion 	}
703b2badf02SMaxime Henrion 
704c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
705c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
706c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
707c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
708b2badf02SMaxime Henrion 	if (error) {
709a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TxCB DMA tag\n");
710b2badf02SMaxime Henrion 		goto fail;
711b2badf02SMaxime Henrion 	}
712b2badf02SMaxime Henrion 
713b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
714658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->cbl_map);
715a2057a72SPyun YongHyeon 	if (error) {
716a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate TxCB DMA memory\n");
7174953bccaSNate Lawson 		goto fail;
718a2057a72SPyun YongHyeon 	}
719b2badf02SMaxime Henrion 
720b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
721b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
722f9d050a8SPyun YongHyeon 	    &sc->fxp_desc.cbl_addr, BUS_DMA_NOWAIT);
723b2badf02SMaxime Henrion 	if (error) {
724a2057a72SPyun YongHyeon 		device_printf(dev, "could not load TxCB DMA buffer\n");
725b2badf02SMaxime Henrion 		goto fail;
726b2badf02SMaxime Henrion 	}
727b2badf02SMaxime Henrion 
728c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
729c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
730c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
731c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
732b2badf02SMaxime Henrion 	if (error) {
733a2057a72SPyun YongHyeon 		device_printf(dev,
734a2057a72SPyun YongHyeon 		    "could not create multicast setup DMA tag\n");
735b2badf02SMaxime Henrion 		goto fail;
736b2badf02SMaxime Henrion 	}
737b2badf02SMaxime Henrion 
738b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
739658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->mcs_map);
740a2057a72SPyun YongHyeon 	if (error) {
741a2057a72SPyun YongHyeon 		device_printf(dev,
742a2057a72SPyun YongHyeon 		    "could not allocate multicast setup DMA memory\n");
7434953bccaSNate Lawson 		goto fail;
744a2057a72SPyun YongHyeon 	}
745b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
746f9d050a8SPyun YongHyeon 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr,
747f9d050a8SPyun YongHyeon 	    BUS_DMA_NOWAIT);
748b2badf02SMaxime Henrion 	if (error) {
749a2057a72SPyun YongHyeon 		device_printf(dev,
750a2057a72SPyun YongHyeon 		    "can't load the multicast setup DMA buffer\n");
751b2badf02SMaxime Henrion 		goto fail;
752b2badf02SMaxime Henrion 	}
753b2badf02SMaxime Henrion 
754b2badf02SMaxime Henrion 	/*
7556720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7566720ebccSMaxime Henrion 	 * the TX command blocks.
757b2badf02SMaxime Henrion 	 */
7586720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7596720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7604cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7616720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
762a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_txmtag, 0, &txp[i].tx_map);
763b2badf02SMaxime Henrion 		if (error) {
764b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
765b2badf02SMaxime Henrion 			goto fail;
766b2badf02SMaxime Henrion 		}
767b2badf02SMaxime Henrion 	}
768a2057a72SPyun YongHyeon 	error = bus_dmamap_create(sc->fxp_rxmtag, 0, &sc->spare_map);
769b2badf02SMaxime Henrion 	if (error) {
770b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
771b2badf02SMaxime Henrion 		goto fail;
772b2badf02SMaxime Henrion 	}
773b2badf02SMaxime Henrion 
774b2badf02SMaxime Henrion 	/*
775b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
776b2badf02SMaxime Henrion 	 */
777b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
778b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
779b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
780a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_rxmtag, 0, &rxp->rx_map);
781b2badf02SMaxime Henrion 		if (error) {
782b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
783b2badf02SMaxime Henrion 			goto fail;
784b2badf02SMaxime Henrion 		}
78585050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7864953bccaSNate Lawson 			error = ENOMEM;
7874953bccaSNate Lawson 			goto fail;
7884953bccaSNate Lawson 		}
78985050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
790b2badf02SMaxime Henrion 	}
791b2badf02SMaxime Henrion 
792b2badf02SMaxime Henrion 	/*
793f7788e8eSJonathan Lemon 	 * Read MAC address.
794f7788e8eSJonathan Lemon 	 */
795*8262183eSPyun YongHyeon 	eaddr[0] = sc->eeprom[FXP_EEPROM_MAP_IA0] & 0xff;
796*8262183eSPyun YongHyeon 	eaddr[1] = sc->eeprom[FXP_EEPROM_MAP_IA0] >> 8;
797*8262183eSPyun YongHyeon 	eaddr[2] = sc->eeprom[FXP_EEPROM_MAP_IA1] & 0xff;
798*8262183eSPyun YongHyeon 	eaddr[3] = sc->eeprom[FXP_EEPROM_MAP_IA1] >> 8;
799*8262183eSPyun YongHyeon 	eaddr[4] = sc->eeprom[FXP_EEPROM_MAP_IA2] & 0xff;
800*8262183eSPyun YongHyeon 	eaddr[5] = sc->eeprom[FXP_EEPROM_MAP_IA2] >> 8;
801f7788e8eSJonathan Lemon 	if (bootverbose) {
8022e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
803f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
8042e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
8052e2b8238SJonathan Lemon 		    pci_get_revid(dev));
80672a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
807*8262183eSPyun YongHyeon 		    sc->eeprom[FXP_EEPROM_MAP_ID] & 0x02 ? "enabled" :
808*8262183eSPyun YongHyeon 		    "disabled");
809f7788e8eSJonathan Lemon 	}
810f7788e8eSJonathan Lemon 
811f7788e8eSJonathan Lemon 	/*
812f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
813f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
814f7788e8eSJonathan Lemon 	 *
815f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
816f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
817f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
818f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
819f7788e8eSJonathan Lemon 	 */
820f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
821f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
822f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
823f7788e8eSJonathan Lemon 	} else {
8248e5d93dbSMarius Strobl 		/*
8258e5d93dbSMarius Strobl 		 * i82557 wedge when isolating all of their PHYs.
8268e5d93dbSMarius Strobl 		 */
8271845b5c3SMarius Strobl 		flags = MIIF_NOISOLATE;
8281845b5c3SMarius Strobl 		if (sc->revision >= FXP_REV_82558_A4)
8291845b5c3SMarius Strobl 			flags |= MIIF_DOPAUSE;
8308e5d93dbSMarius Strobl 		error = mii_attach(dev, &sc->miibus, ifp, fxp_ifmedia_upd,
8318e5d93dbSMarius Strobl 		    fxp_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
8321845b5c3SMarius Strobl 		    MII_OFFSET_ANY, flags);
8338e5d93dbSMarius Strobl 		if (error != 0) {
8348e5d93dbSMarius Strobl 			device_printf(dev, "attaching PHYs failed\n");
835ba8c6fd5SDavid Greenman 			goto fail;
836a17c678eSDavid Greenman 		}
837f7788e8eSJonathan Lemon 	}
838dccee1a1SDavid Greenman 
8399bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
840fb583156SDavid Greenman 	ifp->if_init = fxp_init;
841ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
842ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
843ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
844ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
845a17c678eSDavid Greenman 
8465fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
8475fe9116bSYaroslav Tykhiy 
848c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
849c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
850c21e84e4SPyun YongHyeon 		ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO;
851c21e84e4SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4;
852c21e84e4SPyun YongHyeon 		ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4;
853c8bca6dcSBill Paul 	}
854c8bca6dcSBill Paul 
855f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
856f13075afSPyun YongHyeon 		ifp->if_capabilities |= IFCAP_RXCSUM;
857f13075afSPyun YongHyeon 		ifp->if_capenable |= IFCAP_RXCSUM;
858f13075afSPyun YongHyeon 	}
859f13075afSPyun YongHyeon 
8607137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
8617137cea0SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL_MAGIC;
8627137cea0SPyun YongHyeon 		ifp->if_capenable |= IFCAP_WOL_MAGIC;
8637137cea0SPyun YongHyeon 	}
8647137cea0SPyun YongHyeon 
865fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
866fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
867fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
868fb917226SRuslan Ermilov #endif
869fb917226SRuslan Ermilov 
870dfe61cf1SDavid Greenman 	/*
8714953bccaSNate Lawson 	 * Attach the interface.
8724953bccaSNate Lawson 	 */
873fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
8744953bccaSNate Lawson 
8754953bccaSNate Lawson 	/*
876e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
8775fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
8785fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
879e8c8b728SJonathan Lemon 	 */
880e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
881673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
88244e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
883bd4fa9d9SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) {
884bd4fa9d9SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING |
885713ca255SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
886bd4fa9d9SPyun YongHyeon 		ifp->if_capenable |= IFCAP_VLAN_HWTAGGING |
887713ca255SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
888bd4fa9d9SPyun YongHyeon 	}
889e8c8b728SJonathan Lemon 
890483b9871SDavid Greenman 	/*
8913114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8923114fdb4SDavid Greenman 	 * TX descriptors.
893483b9871SDavid Greenman 	 */
8947929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8957929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8967929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8974a684684SDavid Greenman 
898201afb0eSMaxime Henrion 	/*
8994953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
900201afb0eSMaxime Henrion 	 */
90105bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
902ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
903201afb0eSMaxime Henrion 	if (error) {
904201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
905fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
906201afb0eSMaxime Henrion 		goto fail;
907201afb0eSMaxime Henrion 	}
908201afb0eSMaxime Henrion 
9097137cea0SPyun YongHyeon 	/*
9107137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
9117137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
9127137cea0SPyun YongHyeon 	 */
9137137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
9147137cea0SPyun YongHyeon 		FXP_LOCK(sc);
9157137cea0SPyun YongHyeon 		/* Clear wakeup events. */
916af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
917a461b201SPyun YongHyeon 		fxp_init_body(sc, 0);
9187137cea0SPyun YongHyeon 		fxp_stop(sc);
9197137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
9207137cea0SPyun YongHyeon 	}
9217137cea0SPyun YongHyeon 
922a17c678eSDavid Greenman fail:
9231b5a39d3SBrooks Davis 	if (error)
924f7788e8eSJonathan Lemon 		fxp_release(sc);
925f7788e8eSJonathan Lemon 	return (error);
926f7788e8eSJonathan Lemon }
927f7788e8eSJonathan Lemon 
928f7788e8eSJonathan Lemon /*
9294953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
9304953bccaSNate Lawson  * interrupt should already be torn down.
931f7788e8eSJonathan Lemon  */
932f7788e8eSJonathan Lemon static void
933f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
934f7788e8eSJonathan Lemon {
935b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
936b2badf02SMaxime Henrion 	struct fxp_tx *txp;
937b2badf02SMaxime Henrion 	int i;
938b2badf02SMaxime Henrion 
93967fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
940670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
941670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
9424953bccaSNate Lawson 	if (sc->miibus)
9434953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
9444953bccaSNate Lawson 	bus_generic_detach(sc->dev);
9454953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
946b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
947b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
948b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
949b2badf02SMaxime Henrion 		    sc->cbl_map);
950b2badf02SMaxime Henrion 	}
951b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
952b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
953b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
954b2badf02SMaxime Henrion 	}
955b2badf02SMaxime Henrion 	if (sc->mcsp) {
956b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
957b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
958b2badf02SMaxime Henrion 	}
95905bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
960a2057a72SPyun YongHyeon 	if (sc->fxp_rxmtag) {
961b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
962b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
963b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
964a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
965b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
966a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
967b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
968b983c7b3SMaxime Henrion 			}
969a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_rxmtag, rxp->rx_map);
970b983c7b3SMaxime Henrion 		}
971a2057a72SPyun YongHyeon 		bus_dmamap_destroy(sc->fxp_rxmtag, sc->spare_map);
972a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_rxmtag);
973a2057a72SPyun YongHyeon 	}
974a2057a72SPyun YongHyeon 	if (sc->fxp_txmtag) {
975b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
976b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
977b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
978a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
979b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
980a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
981b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
982b983c7b3SMaxime Henrion 			}
983a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_txmtag, txp->tx_map);
984b983c7b3SMaxime Henrion 		}
985a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_txmtag);
986b983c7b3SMaxime Henrion 	}
987c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
988c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
989b2badf02SMaxime Henrion 	if (sc->cbl_tag)
990b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
991b2badf02SMaxime Henrion 	if (sc->mcs_tag)
992b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
993fc74a9f9SBrooks Davis 	if (sc->ifp)
994fc74a9f9SBrooks Davis 		if_free(sc->ifp);
99572a32a26SJonathan Lemon 
9960f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
9976182fdbdSPeter Wemm }
9986182fdbdSPeter Wemm 
9996182fdbdSPeter Wemm /*
10006182fdbdSPeter Wemm  * Detach interface.
10016182fdbdSPeter Wemm  */
10026182fdbdSPeter Wemm static int
10036182fdbdSPeter Wemm fxp_detach(device_t dev)
10046182fdbdSPeter Wemm {
10056182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
10066182fdbdSPeter Wemm 
100740929967SGleb Smirnoff #ifdef DEVICE_POLLING
100840929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
100940929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
101040929967SGleb Smirnoff #endif
101140929967SGleb Smirnoff 
10124953bccaSNate Lawson 	FXP_LOCK(sc);
10136182fdbdSPeter Wemm 	/*
101432cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
101520f0c80fSMaxime Henrion 	 */
101620f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
101720f0c80fSMaxime Henrion 	fxp_stop(sc);
101832cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
10199eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
102020f0c80fSMaxime Henrion 
10216182fdbdSPeter Wemm 	/*
10223212724cSJohn Baldwin 	 * Close down routes etc.
10233212724cSJohn Baldwin 	 */
10243212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
10253212724cSJohn Baldwin 
10263212724cSJohn Baldwin 	/*
10274953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
10284953bccaSNate Lawson 	 * races with fxp_intr().
10296182fdbdSPeter Wemm 	 */
103005bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
10314953bccaSNate Lawson 	sc->ih = NULL;
10326182fdbdSPeter Wemm 
1033f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
1034f7788e8eSJonathan Lemon 	fxp_release(sc);
1035f7788e8eSJonathan Lemon 	return (0);
1036a17c678eSDavid Greenman }
1037a17c678eSDavid Greenman 
1038a17c678eSDavid Greenman /*
10394a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
1040a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
1041a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
1042a17c678eSDavid Greenman  */
10436182fdbdSPeter Wemm static int
10446182fdbdSPeter Wemm fxp_shutdown(device_t dev)
1045a17c678eSDavid Greenman {
10463212724cSJohn Baldwin 
10476182fdbdSPeter Wemm 	/*
10486182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
10496182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
10506182fdbdSPeter Wemm 	 * reboot before the driver initializes.
10516182fdbdSPeter Wemm 	 */
10527137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
1053a17c678eSDavid Greenman }
1054a17c678eSDavid Greenman 
10557dced78aSDavid Greenman /*
10567dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
10577dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10587dced78aSDavid Greenman  * resume.
10597dced78aSDavid Greenman  */
10607dced78aSDavid Greenman static int
10617dced78aSDavid Greenman fxp_suspend(device_t dev)
10627dced78aSDavid Greenman {
10637dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
10647137cea0SPyun YongHyeon 	struct ifnet *ifp;
10657137cea0SPyun YongHyeon 	int pmc;
10667137cea0SPyun YongHyeon 	uint16_t pmstat;
10677dced78aSDavid Greenman 
10684953bccaSNate Lawson 	FXP_LOCK(sc);
10697dced78aSDavid Greenman 
10707137cea0SPyun YongHyeon 	ifp = sc->ifp;
10713b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
10727137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10737137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
10747137cea0SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
10757137cea0SPyun YongHyeon 			/* Request PME. */
10767137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10777137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10787137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
10791845b5c3SMarius Strobl 			fxp_init_body(sc, 1);
10807137cea0SPyun YongHyeon 		}
10817137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
10827137cea0SPyun YongHyeon 	}
10837dced78aSDavid Greenman 	fxp_stop(sc);
10847dced78aSDavid Greenman 
10857dced78aSDavid Greenman 	sc->suspended = 1;
10867dced78aSDavid Greenman 
10874953bccaSNate Lawson 	FXP_UNLOCK(sc);
1088f7788e8eSJonathan Lemon 	return (0);
10897dced78aSDavid Greenman }
10907dced78aSDavid Greenman 
10917dced78aSDavid Greenman /*
109267ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
10937dced78aSDavid Greenman  * appropriate.
10947dced78aSDavid Greenman  */
10957dced78aSDavid Greenman static int
10967dced78aSDavid Greenman fxp_resume(device_t dev)
10977dced78aSDavid Greenman {
10987dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
1099fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
11007137cea0SPyun YongHyeon 	int pmc;
11017137cea0SPyun YongHyeon 	uint16_t pmstat;
11027dced78aSDavid Greenman 
11034953bccaSNate Lawson 	FXP_LOCK(sc);
11047dced78aSDavid Greenman 
11053b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
11067137cea0SPyun YongHyeon 		sc->flags &= ~FXP_FLAG_WOL;
11077137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
11087137cea0SPyun YongHyeon 		/* Disable PME and clear PME status. */
11097137cea0SPyun YongHyeon 		pmstat &= ~PCIM_PSTAT_PMEENABLE;
11107137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1111af75b654SPyun YongHyeon 		if ((sc->flags & FXP_FLAG_WOLCAP) != 0)
1112af75b654SPyun YongHyeon 			CSR_WRITE_1(sc, FXP_CSR_PMDR,
1113af75b654SPyun YongHyeon 			    CSR_READ_1(sc, FXP_CSR_PMDR));
11147137cea0SPyun YongHyeon 	}
11157137cea0SPyun YongHyeon 
11167dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
11177dced78aSDavid Greenman 	DELAY(10);
11187dced78aSDavid Greenman 
11197dced78aSDavid Greenman 	/* reinitialize interface if necessary */
11207dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
11211845b5c3SMarius Strobl 		fxp_init_body(sc, 1);
11227dced78aSDavid Greenman 
11237dced78aSDavid Greenman 	sc->suspended = 0;
11247dced78aSDavid Greenman 
11254953bccaSNate Lawson 	FXP_UNLOCK(sc);
1126ba8c6fd5SDavid Greenman 	return (0);
1127f7788e8eSJonathan Lemon }
1128ba8c6fd5SDavid Greenman 
112900c4116bSJonathan Lemon static void
113000c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
113100c4116bSJonathan Lemon {
113274d1ed23SMaxime Henrion 	uint16_t reg;
113300c4116bSJonathan Lemon 	int x;
113400c4116bSJonathan Lemon 
113500c4116bSJonathan Lemon 	/*
113600c4116bSJonathan Lemon 	 * Shift in data.
113700c4116bSJonathan Lemon 	 */
113800c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
113900c4116bSJonathan Lemon 		if (data & x)
114000c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
114100c4116bSJonathan Lemon 		else
114200c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
114300c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
114400c4116bSJonathan Lemon 		DELAY(1);
114500c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
114600c4116bSJonathan Lemon 		DELAY(1);
114700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
114800c4116bSJonathan Lemon 		DELAY(1);
114900c4116bSJonathan Lemon 	}
115000c4116bSJonathan Lemon }
115100c4116bSJonathan Lemon 
1152f7788e8eSJonathan Lemon /*
1153f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1154f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1155f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1156f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1157f7788e8eSJonathan Lemon  * every 16 bits of data.
1158f7788e8eSJonathan Lemon  */
115974d1ed23SMaxime Henrion static uint16_t
1160f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1161f7788e8eSJonathan Lemon {
116274d1ed23SMaxime Henrion 	uint16_t reg, data;
1163f7788e8eSJonathan Lemon 	int x;
1164ba8c6fd5SDavid Greenman 
1165f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1166f7788e8eSJonathan Lemon 	/*
1167f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1168f7788e8eSJonathan Lemon 	 */
116900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1170f7788e8eSJonathan Lemon 	/*
1171f7788e8eSJonathan Lemon 	 * Shift in address.
1172f7788e8eSJonathan Lemon 	 */
1173f7788e8eSJonathan Lemon 	data = 0;
1174f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1175f7788e8eSJonathan Lemon 		if (offset & x)
1176f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1177f7788e8eSJonathan Lemon 		else
1178f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1179f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1180f7788e8eSJonathan Lemon 		DELAY(1);
1181f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1182f7788e8eSJonathan Lemon 		DELAY(1);
1183f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1184f7788e8eSJonathan Lemon 		DELAY(1);
1185f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1186f7788e8eSJonathan Lemon 		data++;
1187f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1188f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1189f7788e8eSJonathan Lemon 			break;
1190f7788e8eSJonathan Lemon 		}
1191f7788e8eSJonathan Lemon 	}
1192f7788e8eSJonathan Lemon 	/*
1193f7788e8eSJonathan Lemon 	 * Shift out data.
1194f7788e8eSJonathan Lemon 	 */
1195f7788e8eSJonathan Lemon 	data = 0;
1196f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1197f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1198f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1199f7788e8eSJonathan Lemon 		DELAY(1);
1200f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1201f7788e8eSJonathan Lemon 			data |= x;
1202f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1203f7788e8eSJonathan Lemon 		DELAY(1);
1204f7788e8eSJonathan Lemon 	}
1205f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1206f7788e8eSJonathan Lemon 	DELAY(1);
1207f7788e8eSJonathan Lemon 
1208f7788e8eSJonathan Lemon 	return (data);
1209ba8c6fd5SDavid Greenman }
1210ba8c6fd5SDavid Greenman 
121100c4116bSJonathan Lemon static void
121274d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
121300c4116bSJonathan Lemon {
121400c4116bSJonathan Lemon 	int i;
121500c4116bSJonathan Lemon 
121600c4116bSJonathan Lemon 	/*
121700c4116bSJonathan Lemon 	 * Erase/write enable.
121800c4116bSJonathan Lemon 	 */
121900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
122000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
122100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
122200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
122300c4116bSJonathan Lemon 	DELAY(1);
122400c4116bSJonathan Lemon 	/*
122500c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
122600c4116bSJonathan Lemon 	 */
122700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
122800c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
122900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
123000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
123100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
123200c4116bSJonathan Lemon 	DELAY(1);
123300c4116bSJonathan Lemon 	/*
123400c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
123500c4116bSJonathan Lemon 	 */
123600c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
123700c4116bSJonathan Lemon 	DELAY(1);
123800c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
123900c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
124000c4116bSJonathan Lemon 			break;
124100c4116bSJonathan Lemon 		DELAY(50);
124200c4116bSJonathan Lemon 	}
124300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
124400c4116bSJonathan Lemon 	DELAY(1);
124500c4116bSJonathan Lemon 	/*
124600c4116bSJonathan Lemon 	 * Erase/write disable.
124700c4116bSJonathan Lemon 	 */
124800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
124900c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
125000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
125100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
125200c4116bSJonathan Lemon 	DELAY(1);
125300c4116bSJonathan Lemon }
125400c4116bSJonathan Lemon 
1255ba8c6fd5SDavid Greenman /*
1256e9bf2fa7SDavid Greenman  * From NetBSD:
1257e9bf2fa7SDavid Greenman  *
1258e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1259e9bf2fa7SDavid Greenman  *
1260e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1261e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1262e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1263e9bf2fa7SDavid Greenman  *
1264e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1265e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1266e9bf2fa7SDavid Greenman  *
1267e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1268e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1269e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1270e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1271e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1272e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1273e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1274e9bf2fa7SDavid Greenman  */
1275e9bf2fa7SDavid Greenman static void
1276f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1277e9bf2fa7SDavid Greenman {
1278e9bf2fa7SDavid Greenman 
1279f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1280f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1281f7788e8eSJonathan Lemon 
1282f7788e8eSJonathan Lemon 	/* autosize */
1283f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1284e9bf2fa7SDavid Greenman }
1285f7788e8eSJonathan Lemon 
1286ba8c6fd5SDavid Greenman static void
1287f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1288ba8c6fd5SDavid Greenman {
1289f7788e8eSJonathan Lemon 	int i;
1290ba8c6fd5SDavid Greenman 
1291f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1292f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1293ba8c6fd5SDavid Greenman }
1294ba8c6fd5SDavid Greenman 
129500c4116bSJonathan Lemon static void
129600c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
129700c4116bSJonathan Lemon {
129800c4116bSJonathan Lemon 	int i;
129900c4116bSJonathan Lemon 
130000c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
130100c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
130200c4116bSJonathan Lemon }
130300c4116bSJonathan Lemon 
1304*8262183eSPyun YongHyeon static void
1305*8262183eSPyun YongHyeon fxp_load_eeprom(struct fxp_softc *sc)
1306*8262183eSPyun YongHyeon {
1307*8262183eSPyun YongHyeon 	int i;
1308*8262183eSPyun YongHyeon 	uint16_t cksum;
1309*8262183eSPyun YongHyeon 
1310*8262183eSPyun YongHyeon 	fxp_read_eeprom(sc, sc->eeprom, 0, 1 << sc->eeprom_size);
1311*8262183eSPyun YongHyeon 	cksum = 0;
1312*8262183eSPyun YongHyeon 	for (i = 0; i < (1 << sc->eeprom_size) - 1; i++)
1313*8262183eSPyun YongHyeon 		cksum += sc->eeprom[i];
1314*8262183eSPyun YongHyeon 	cksum = 0xBABA - cksum;
1315*8262183eSPyun YongHyeon 	if (cksum != sc->eeprom[(1 << sc->eeprom_size) - 1])
1316*8262183eSPyun YongHyeon 		device_printf(sc->dev,
1317*8262183eSPyun YongHyeon 		    "EEPROM checksum mismatch! (0x%04x -> 0x%04x)\n",
1318*8262183eSPyun YongHyeon 		    cksum, sc->eeprom[(1 << sc->eeprom_size) - 1]);
1319*8262183eSPyun YongHyeon }
1320*8262183eSPyun YongHyeon 
1321a17c678eSDavid Greenman /*
13224953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1323a17c678eSDavid Greenman  */
1324a17c678eSDavid Greenman static void
1325f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1326a17c678eSDavid Greenman {
13279b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
13284953bccaSNate Lawson 
13294953bccaSNate Lawson 	FXP_LOCK(sc);
13304953bccaSNate Lawson 	fxp_start_body(ifp);
13314953bccaSNate Lawson 	FXP_UNLOCK(sc);
13324953bccaSNate Lawson }
13334953bccaSNate Lawson 
13344953bccaSNate Lawson /*
13354953bccaSNate Lawson  * Start packet transmission on the interface.
13364953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
13374953bccaSNate Lawson  * internal entry point only.
13384953bccaSNate Lawson  */
13394953bccaSNate Lawson static void
13404953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
13414953bccaSNate Lawson {
13424953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1343b2badf02SMaxime Henrion 	struct mbuf *mb_head;
13444e53f837SPyun YongHyeon 	int txqueued;
1345a17c678eSDavid Greenman 
134667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
134740c20505SMaxime Henrion 
1348c109e385SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1349c109e385SPyun YongHyeon 	    IFF_DRV_RUNNING)
1350c109e385SPyun YongHyeon 		return;
1351c109e385SPyun YongHyeon 
13524e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
13534e53f837SPyun YongHyeon 		fxp_txeof(sc);
1354483b9871SDavid Greenman 	/*
1355483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1356483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
13573114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
13583114fdb4SDavid Greenman 	 *       a NOP command when needed.
1359483b9871SDavid Greenman 	 */
136040c20505SMaxime Henrion 	txqueued = 0;
13617929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
13627929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1363483b9871SDavid Greenman 
1364dfe61cf1SDavid Greenman 		/*
1365dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1366dfe61cf1SDavid Greenman 		 */
13677929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
13687929aa03SMax Laier 		if (mb_head == NULL)
13697929aa03SMax Laier 			break;
1370a17c678eSDavid Greenman 
13714e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13724e53f837SPyun YongHyeon 			if (mb_head == NULL)
137340c20505SMaxime Henrion 				break;
13744e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
13754e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
137640c20505SMaxime Henrion 		}
13774e53f837SPyun YongHyeon 		txqueued++;
13784e53f837SPyun YongHyeon 		/*
13794e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13804e53f837SPyun YongHyeon 		 */
13814e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
13824e53f837SPyun YongHyeon 	}
138340c20505SMaxime Henrion 
138440c20505SMaxime Henrion 	/*
138540c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
138640c20505SMaxime Henrion 	 * going again if suspended.
138740c20505SMaxime Henrion 	 */
13884e53f837SPyun YongHyeon 	if (txqueued > 0) {
1389a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1390a2057a72SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
139140c20505SMaxime Henrion 		fxp_scb_wait(sc);
139240c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13934e53f837SPyun YongHyeon 		/*
13944e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
13954e53f837SPyun YongHyeon 		 * from the card again.
13964e53f837SPyun YongHyeon 		 */
13974e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
139840c20505SMaxime Henrion 	}
139940c20505SMaxime Henrion }
140040c20505SMaxime Henrion 
140140c20505SMaxime Henrion static int
14024e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
140340c20505SMaxime Henrion {
140440c20505SMaxime Henrion 	struct ifnet *ifp;
140540c20505SMaxime Henrion 	struct mbuf *m;
140640c20505SMaxime Henrion 	struct fxp_tx *txp;
140740c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1408c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
140940c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1410c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
141140c20505SMaxime Henrion 
141240c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1413fc74a9f9SBrooks Davis 	ifp = sc->ifp;
141440c20505SMaxime Henrion 
1415c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1416c21e84e4SPyun YongHyeon 	tcp = NULL;
1417dfe61cf1SDavid Greenman 	/*
1418483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1419dfe61cf1SDavid Greenman 	 */
1420b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1421c8bca6dcSBill Paul 
1422c8bca6dcSBill Paul 	/*
1423a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1424a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1425a35e7eaaSDon Lewis 	 * Developer Manual says:
1426a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1427a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1428a35e7eaaSDon Lewis 	 *   ...
1429a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1430a35e7eaaSDon Lewis 	 *   be used.
1431a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1432a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1433a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1434a35e7eaaSDon Lewis 	 */
1435a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1436a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1437a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1438a35e7eaaSDon Lewis 
14394e53f837SPyun YongHyeon 	m = *m_head;
1440c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1441c21e84e4SPyun YongHyeon 		/*
1442c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1443c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1444c21e84e4SPyun YongHyeon 		 */
1445c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1446c21e84e4SPyun YongHyeon 		struct ip *ip;
1447c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1448c21e84e4SPyun YongHyeon 
1449c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1450c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1451c21e84e4SPyun YongHyeon 			m = m_dup(*m_head, M_DONTWAIT);
1452c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1453c21e84e4SPyun YongHyeon 			if (m == NULL) {
1454c21e84e4SPyun YongHyeon 				*m_head = NULL;
1455c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1456c21e84e4SPyun YongHyeon 			}
1457c21e84e4SPyun YongHyeon 			*m_head = m;
1458c21e84e4SPyun YongHyeon 		}
1459c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1460c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1461c21e84e4SPyun YongHyeon 		if (m == NULL) {
1462c21e84e4SPyun YongHyeon 			*m_head = NULL;
1463c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1464c21e84e4SPyun YongHyeon 		}
1465c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1466c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1467c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1468c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1469c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1470c21e84e4SPyun YongHyeon 			if (m == NULL) {
1471c21e84e4SPyun YongHyeon 				*m_head = NULL;
1472c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1473c21e84e4SPyun YongHyeon 			}
1474c21e84e4SPyun YongHyeon 		}
1475c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1476c21e84e4SPyun YongHyeon 		if (m == NULL) {
1477c21e84e4SPyun YongHyeon 			*m_head = NULL;
1478c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1479c21e84e4SPyun YongHyeon 		}
1480c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1481c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1482c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1483c21e84e4SPyun YongHyeon 		if (m == NULL) {
1484c21e84e4SPyun YongHyeon 			*m_head = NULL;
1485c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1486c21e84e4SPyun YongHyeon 		}
1487c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1488cbecedb2SPyun YongHyeon 		m = m_pullup(m, poff + (tcp->th_off << 2));
1489c21e84e4SPyun YongHyeon 		if (m == NULL) {
1490c21e84e4SPyun YongHyeon 			*m_head = NULL;
1491c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1492c21e84e4SPyun YongHyeon 		}
1493c21e84e4SPyun YongHyeon 
1494c21e84e4SPyun YongHyeon 		/*
1495c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1496c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1497c21e84e4SPyun YongHyeon 		 */
149896486faaSPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
149996486faaSPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1500c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
15010685c824SPyun YongHyeon 		ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) +
15020685c824SPyun YongHyeon 		    (tcp->th_off << 2));
1503c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1504c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1505c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1506c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1507c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1508c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1509c21e84e4SPyun YongHyeon 		*m_head = m;
15106da6d0a9SPyun YongHyeon 	} else if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
15116da6d0a9SPyun YongHyeon 		/*
15126da6d0a9SPyun YongHyeon 		 * Deal with TCP/IP checksum offload. Note that
15136da6d0a9SPyun YongHyeon 		 * in order for TCP checksum offload to work,
15146da6d0a9SPyun YongHyeon 		 * the pseudo header checksum must have already
15156da6d0a9SPyun YongHyeon 		 * been computed and stored in the checksum field
15166da6d0a9SPyun YongHyeon 		 * in the TCP header. The stack should have
15176da6d0a9SPyun YongHyeon 		 * already done this for us.
15186da6d0a9SPyun YongHyeon 		 */
15196da6d0a9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
15206da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
15216da6d0a9SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
15226da6d0a9SPyun YongHyeon 
15236da6d0a9SPyun YongHyeon #ifdef FXP_IP_CSUM_WAR
15246da6d0a9SPyun YongHyeon 		/*
15256da6d0a9SPyun YongHyeon 		 * XXX The 82550 chip appears to have trouble
15266da6d0a9SPyun YongHyeon 		 * dealing with IP header checksums in very small
15276da6d0a9SPyun YongHyeon 		 * datagrams, namely fragments from 1 to 3 bytes
15286da6d0a9SPyun YongHyeon 		 * in size. For example, say you want to transmit
15296da6d0a9SPyun YongHyeon 		 * a UDP packet of 1473 bytes. The packet will be
15306da6d0a9SPyun YongHyeon 		 * fragmented over two IP datagrams, the latter
15316da6d0a9SPyun YongHyeon 		 * containing only one byte of data. The 82550 will
15326da6d0a9SPyun YongHyeon 		 * botch the header checksum on the 1-byte fragment.
15336da6d0a9SPyun YongHyeon 		 * As long as the datagram contains 4 or more bytes
15346da6d0a9SPyun YongHyeon 		 * of data, you're ok.
15356da6d0a9SPyun YongHyeon 		 *
15366da6d0a9SPyun YongHyeon                  * The following code attempts to work around this
15376da6d0a9SPyun YongHyeon 		 * problem: if the datagram is less than 38 bytes
15386da6d0a9SPyun YongHyeon 		 * in size (14 bytes ether header, 20 bytes IP header,
15396da6d0a9SPyun YongHyeon 		 * plus 4 bytes of data), we punt and compute the IP
15406da6d0a9SPyun YongHyeon 		 * header checksum by hand. This workaround doesn't
15416da6d0a9SPyun YongHyeon 		 * work very well, however, since it can be fooled
15426da6d0a9SPyun YongHyeon 		 * by things like VLAN tags and IP options that make
15436da6d0a9SPyun YongHyeon 		 * the header sizes/offsets vary.
15446da6d0a9SPyun YongHyeon 		 */
15456da6d0a9SPyun YongHyeon 
15466da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
15476da6d0a9SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
15486da6d0a9SPyun YongHyeon 				struct ip *ip;
15496da6d0a9SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
15506da6d0a9SPyun YongHyeon 				ip = mtod(m, struct ip *);
15516da6d0a9SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
15526da6d0a9SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
15536da6d0a9SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
15546da6d0a9SPyun YongHyeon 			} else {
15556da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_activation_high =
15566da6d0a9SPyun YongHyeon 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
15576da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_schedule |=
15586da6d0a9SPyun YongHyeon 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
15596da6d0a9SPyun YongHyeon 			}
15606da6d0a9SPyun YongHyeon 		}
15616da6d0a9SPyun YongHyeon #endif
1562c21e84e4SPyun YongHyeon 	}
1563c21e84e4SPyun YongHyeon 
1564a2057a72SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, *m_head,
15654e53f837SPyun YongHyeon 	    segs, &nseg, 0);
15664e53f837SPyun YongHyeon 	if (error == EFBIG) {
15674e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
15684e53f837SPyun YongHyeon 		if (m == NULL) {
15694e53f837SPyun YongHyeon 			m_freem(*m_head);
15704e53f837SPyun YongHyeon 			*m_head = NULL;
15714e53f837SPyun YongHyeon 			return (ENOMEM);
15721104779bSMike Silbersack 		}
15734e53f837SPyun YongHyeon 		*m_head = m;
1574a2057a72SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map,
15754e53f837SPyun YongHyeon 		    *m_head, segs, &nseg, 0);
15764e53f837SPyun YongHyeon 		if (error != 0) {
15774e53f837SPyun YongHyeon 			m_freem(*m_head);
15784e53f837SPyun YongHyeon 			*m_head = NULL;
15794e53f837SPyun YongHyeon 			return (ENOMEM);
15804e53f837SPyun YongHyeon 		}
15814e53f837SPyun YongHyeon 	} else if (error != 0)
15824e53f837SPyun YongHyeon 		return (error);
15834e53f837SPyun YongHyeon 	if (nseg == 0) {
15844e53f837SPyun YongHyeon 		m_freem(*m_head);
15854e53f837SPyun YongHyeon 		*m_head = NULL;
15864e53f837SPyun YongHyeon 		return (EIO);
158723a0ed7cSDavid Greenman 	}
158823a0ed7cSDavid Greenman 
158940c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
1590a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1591b2badf02SMaxime Henrion 
159240c20505SMaxime Henrion 	cbp = txp->tx_cb;
159340c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
159440c20505SMaxime Henrion 		/*
159540c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
159640c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
159740c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
159840c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
159940c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
160040c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
160140c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
160240c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
160340c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
160440c20505SMaxime Henrion 		 * starting from a different place depending on whether
160540c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
160640c20505SMaxime Henrion 		 */
160740c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
160868f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
160968f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
161040c20505SMaxime Henrion 		} else {
161140c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
161240c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
161340c20505SMaxime Henrion 		}
161440c20505SMaxime Henrion 	}
1615c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1616c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1617c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
161868f4ab9aSPyun YongHyeon 		cbp->tbd[nseg].tb_size |= htole32(0x8000);
1619c21e84e4SPyun YongHyeon 	} else
162040c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1621c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1622c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1623c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
162468f4ab9aSPyun YongHyeon 		cbp->tbd[1].tb_size |= htole32(tcp_payload << 16);
1625c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1626c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1627c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1628c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1629c21e84e4SPyun YongHyeon 	}
1630bd4fa9d9SPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
1631bd4fa9d9SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
1632bd4fa9d9SPyun YongHyeon 		cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag);
1633bd4fa9d9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_activation_high |=
1634bd4fa9d9SPyun YongHyeon 		    FXP_IPCB_INSERTVLAN_ENABLE;
1635bd4fa9d9SPyun YongHyeon 	}
163640c20505SMaxime Henrion 
16374e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1638b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1639b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
16404e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1641b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
164283e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
164383e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
16444e53f837SPyun YongHyeon 	else
1645b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
164683e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
164783e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1648c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1649b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1650a17c678eSDavid Greenman 
1651a17c678eSDavid Greenman 	/*
1652483b9871SDavid Greenman 	 * Advance the end of list forward.
1653a17c678eSDavid Greenman 	 */
165440c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
1655b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1656a17c678eSDavid Greenman 
1657a17c678eSDavid Greenman 	/*
16581cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1659b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1660483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1661a17c678eSDavid Greenman 	 */
16621cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1663b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1664a17c678eSDavid Greenman 
16651cd443acSDavid Greenman 	sc->tx_queued++;
16661cd443acSDavid Greenman 
166740c20505SMaxime Henrion 	return (0);
1668a17c678eSDavid Greenman }
1669a17c678eSDavid Greenman 
1670e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1671e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1672e4fc250cSLuigi Rizzo 
16731abcdbd1SAttilio Rao static int
1674e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1675e4fc250cSLuigi Rizzo {
1676e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
167774d1ed23SMaxime Henrion 	uint8_t statack;
16781abcdbd1SAttilio Rao 	int rx_npkts = 0;
1679e4fc250cSLuigi Rizzo 
16804953bccaSNate Lawson 	FXP_LOCK(sc);
168140929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
16824953bccaSNate Lawson 		FXP_UNLOCK(sc);
16831abcdbd1SAttilio Rao 		return (rx_npkts);
1684e4fc250cSLuigi Rizzo 	}
168540929967SGleb Smirnoff 
1686e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1687e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1688e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
168974d1ed23SMaxime Henrion 		uint8_t tmp;
16906481f301SPeter Wemm 
1691e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16924953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16934953bccaSNate Lawson 			FXP_UNLOCK(sc);
16941abcdbd1SAttilio Rao 			return (rx_npkts); /* nothing to do */
16954953bccaSNate Lawson 		}
1696e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1697e4fc250cSLuigi Rizzo 		/* ack what we can */
1698e4fc250cSLuigi Rizzo 		if (tmp != 0)
1699e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1700e4fc250cSLuigi Rizzo 		statack |= tmp;
1701e4fc250cSLuigi Rizzo 	}
17021abcdbd1SAttilio Rao 	rx_npkts = fxp_intr_body(sc, ifp, statack, count);
17034953bccaSNate Lawson 	FXP_UNLOCK(sc);
17041abcdbd1SAttilio Rao 	return (rx_npkts);
1705e4fc250cSLuigi Rizzo }
1706e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1707e4fc250cSLuigi Rizzo 
1708a17c678eSDavid Greenman /*
17099c7d2607SDavid Greenman  * Process interface interrupts.
1710a17c678eSDavid Greenman  */
171194927790SDavid Greenman static void
1712f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1713a17c678eSDavid Greenman {
1714f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1715fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
171674d1ed23SMaxime Henrion 	uint8_t statack;
17170f4dc94cSChuck Paterson 
17184953bccaSNate Lawson 	FXP_LOCK(sc);
1719704d1965SWarner Losh 	if (sc->suspended) {
1720704d1965SWarner Losh 		FXP_UNLOCK(sc);
1721704d1965SWarner Losh 		return;
1722704d1965SWarner Losh 	}
1723704d1965SWarner Losh 
1724e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
172540929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
17264953bccaSNate Lawson 		FXP_UNLOCK(sc);
1727e4fc250cSLuigi Rizzo 		return;
17284953bccaSNate Lawson 	}
1729e4fc250cSLuigi Rizzo #endif
1730b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1731a17c678eSDavid Greenman 		/*
173211457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
173311457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
173411457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
173511457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
173611457bbfSJonathan Lemon 		 */
17374953bccaSNate Lawson 		if (statack == 0xff) {
17384953bccaSNate Lawson 			FXP_UNLOCK(sc);
173911457bbfSJonathan Lemon 			return;
17404953bccaSNate Lawson 		}
174111457bbfSJonathan Lemon 
174211457bbfSJonathan Lemon 		/*
1743a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1744a17c678eSDavid Greenman 		 */
1745ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1746c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
17474953bccaSNate Lawson 			fxp_intr_body(sc, ifp, statack, -1);
1748e4fc250cSLuigi Rizzo 	}
17494953bccaSNate Lawson 	FXP_UNLOCK(sc);
1750e4fc250cSLuigi Rizzo }
1751e4fc250cSLuigi Rizzo 
1752e4fc250cSLuigi Rizzo static void
1753b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1754b2badf02SMaxime Henrion {
17554e53f837SPyun YongHyeon 	struct ifnet *ifp;
1756b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1757b2badf02SMaxime Henrion 
17584e53f837SPyun YongHyeon 	ifp = sc->ifp;
1759a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1760a2057a72SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1761b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
176283e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1763b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1764b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1765a2057a72SPyun YongHyeon 			bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
1766b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1767a2057a72SPyun YongHyeon 			bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
1768b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1769b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1770b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1771b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1772b2badf02SMaxime Henrion 		}
1773b2badf02SMaxime Henrion 		sc->tx_queued--;
17744e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1775b2badf02SMaxime Henrion 	}
1776b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1777a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1778a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17796b24912cSPyun YongHyeon 	if (sc->tx_queued == 0)
178025935344SPyun YongHyeon 		sc->watchdog_timer = 0;
1781b2badf02SMaxime Henrion }
1782b2badf02SMaxime Henrion 
1783b2badf02SMaxime Henrion static void
1784f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m,
1785f13075afSPyun YongHyeon     uint16_t status, int pos)
1786f13075afSPyun YongHyeon {
1787f13075afSPyun YongHyeon 	struct ether_header *eh;
1788f13075afSPyun YongHyeon 	struct ip *ip;
1789f13075afSPyun YongHyeon 	struct udphdr *uh;
1790f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1791f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1792f13075afSPyun YongHyeon 
1793f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1794f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1795f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1796f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1797f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1798f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1799f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1800f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1801f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1802f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1803f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1804f13075afSPyun YongHyeon 			}
1805f13075afSPyun YongHyeon 		}
1806f13075afSPyun YongHyeon 		return;
1807f13075afSPyun YongHyeon 	}
1808f13075afSPyun YongHyeon 
1809f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1810f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1811f13075afSPyun YongHyeon 		return;
1812f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1813f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1814f13075afSPyun YongHyeon 		return;
1815f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1816f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1817f13075afSPyun YongHyeon 		return;
1818f13075afSPyun YongHyeon 
1819f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1820f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1821f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1822f13075afSPyun YongHyeon 		return;
1823f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1824f13075afSPyun YongHyeon 		return;
1825f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1826f13075afSPyun YongHyeon 		return;
1827f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1828f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1829f13075afSPyun YongHyeon 
1830f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1831f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1832f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1833f13075afSPyun YongHyeon 			return;
1834f13075afSPyun YongHyeon 		break;
1835f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1836f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1837f13075afSPyun YongHyeon 			return;
1838f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1839f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1840f13075afSPyun YongHyeon 			return; /* no checksum */
1841f13075afSPyun YongHyeon 		break;
1842f13075afSPyun YongHyeon 	default:
1843f13075afSPyun YongHyeon 		return;
1844f13075afSPyun YongHyeon 	}
1845f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1846f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1847f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1848f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1849f13075afSPyun YongHyeon 	if (len > 0) {
1850f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1851f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1852f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1853f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1854f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1855f13075afSPyun YongHyeon 		}
1856f13075afSPyun YongHyeon 	}
1857f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1858f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1859f13075afSPyun YongHyeon }
1860f13075afSPyun YongHyeon 
18611abcdbd1SAttilio Rao static int
186274d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
18634953bccaSNate Lawson     int count)
1864e4fc250cSLuigi Rizzo {
18652b5989e9SLuigi Rizzo 	struct mbuf *m;
1866b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18672b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18682b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
18691abcdbd1SAttilio Rao 	int rx_npkts;
187060bb79ebSPyun YongHyeon 	uint16_t status;
18712b5989e9SLuigi Rizzo 
18721abcdbd1SAttilio Rao 	rx_npkts = 0;
187367fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18741abcdbd1SAttilio Rao 
18752b5989e9SLuigi Rizzo 	if (rnr)
18760f1db1d6SMaxime Henrion 		sc->rnr++;
1877947e3815SIan Dowse #ifdef DEVICE_POLLING
1878947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1879947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1880947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1881947e3815SIan Dowse 		rnr = 1;
1882947e3815SIan Dowse 	}
1883947e3815SIan Dowse #endif
1884a17c678eSDavid Greenman 
1885a17c678eSDavid Greenman 	/*
18863114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
188706936301SBill Paul 	 *
188806936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
188906936301SBill Paul 	 * be that this event (control unit not ready) was not
189006936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
189106936301SBill Paul 	 * The exact sequence of events that occur when the interface
189206936301SBill Paul 	 * is brought up are different now, and if this event
189306936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
189406936301SBill Paul 	 * can stall for several seconds. The result is that no
189506936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
189606936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
18973114fdb4SDavid Greenman 	 */
18984e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1899b2badf02SMaxime Henrion 		fxp_txeof(sc);
19003114fdb4SDavid Greenman 
19013114fdb4SDavid Greenman 	/*
19023114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
19033114fdb4SDavid Greenman 	 */
19047929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
19054953bccaSNate Lawson 		fxp_start_body(ifp);
19062b5989e9SLuigi Rizzo 
19072b5989e9SLuigi Rizzo 	/*
19082b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
19092b5989e9SLuigi Rizzo 	 */
1910947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
19111abcdbd1SAttilio Rao 		return (rx_npkts);
19122b5989e9SLuigi Rizzo 
19133114fdb4SDavid Greenman 	/*
1914a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1915a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1916a17c678eSDavid Greenman 	 * re-start the receiver.
1917947e3815SIan Dowse 	 *
19182b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
19192b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
19202b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
19212b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1922947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1923947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1924a17c678eSDavid Greenman 	 */
19252b5989e9SLuigi Rizzo 	for (;;) {
1926b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1927b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1928ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1929ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1930a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
19314812aef5SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1932a17c678eSDavid Greenman 
1933e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1934947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1935947e3815SIan Dowse 			if (rnr) {
1936947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1937947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1938947e3815SIan Dowse 				rnr = 0;
1939947e3815SIan Dowse 			}
19402b5989e9SLuigi Rizzo 			break;
1941947e3815SIan Dowse 		}
19422b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
19432b5989e9SLuigi Rizzo 
194460bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
194560bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
19462b5989e9SLuigi Rizzo 			break;
19472b5989e9SLuigi Rizzo 
1948f7a5f737SPyun YongHyeon 		if ((status & FXP_RFA_STATUS_RNR) != 0)
1949f7a5f737SPyun YongHyeon 			rnr++;
1950dfe61cf1SDavid Greenman 		/*
1951b2badf02SMaxime Henrion 		 * Advance head forward.
1952dfe61cf1SDavid Greenman 		 */
1953b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1954a17c678eSDavid Greenman 
1955dfe61cf1SDavid Greenman 		/*
1956ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1957ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1958ba8c6fd5SDavid Greenman 		 * instead.
1959dfe61cf1SDavid Greenman 		 */
196085050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1961aed53495SDavid Greenman 			int total_len;
1962a17c678eSDavid Greenman 
1963e8c8b728SJonathan Lemon 			/*
19642b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
19652b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
19662b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
19672b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1968e8c8b728SJonathan Lemon 			 */
1969bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1970f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
1971f13075afSPyun YongHyeon 			    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1972f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1973f13075afSPyun YongHyeon 				total_len -= 2;
1974f13075afSPyun YongHyeon 			}
1975991ae908SPyun YongHyeon 			if (total_len < (int)sizeof(struct ether_header) ||
1976f7a5f737SPyun YongHyeon 			    total_len > (MCLBYTES - RFA_ALIGNMENT_FUDGE -
1977f7a5f737SPyun YongHyeon 			    sc->rfa_size) ||
1978f7a5f737SPyun YongHyeon 			    status & (FXP_RFA_STATUS_CRC |
1979991ae908SPyun YongHyeon 			    FXP_RFA_STATUS_ALIGN | FXP_RFA_STATUS_OVERRUN)) {
1980e8c8b728SJonathan Lemon 				m_freem(m);
1981f7a5f737SPyun YongHyeon 				fxp_add_rfabuf(sc, rxp);
19822b5989e9SLuigi Rizzo 				continue;
1983e8c8b728SJonathan Lemon 			}
1984920b58e8SBrooks Davis 
19852e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1986673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1987673d9191SSam Leffler 
1988f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
1989f13075afSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1990f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
1991bd4fa9d9SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
1992bd4fa9d9SPyun YongHyeon 			    (status & FXP_RFA_STATUS_VLAN) != 0) {
1993bd4fa9d9SPyun YongHyeon 				m->m_pkthdr.ether_vtag =
1994bd4fa9d9SPyun YongHyeon 				    ntohs(rfa->rfax_vlan_id);
1995bd4fa9d9SPyun YongHyeon 				m->m_flags |= M_VLANTAG;
1996bd4fa9d9SPyun YongHyeon 			}
199705fb8c3fSNate Lawson 			/*
199805fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
199905fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
200005fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
200105fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
200205fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
200305fb8c3fSNate Lawson 			 * calling if_input() on each one.
200405fb8c3fSNate Lawson 			 */
200505fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
2006673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
200705fb8c3fSNate Lawson 			FXP_LOCK(sc);
20081abcdbd1SAttilio Rao 			rx_npkts++;
2009c109e385SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2010c109e385SPyun YongHyeon 				return (rx_npkts);
201185050421SPyun YongHyeon 		} else {
201285050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
201385050421SPyun YongHyeon 			ifp->if_iqdrops++;
201485050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
2015a17c678eSDavid Greenman 		}
201685050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
2017a17c678eSDavid Greenman 	}
20182b5989e9SLuigi Rizzo 	if (rnr) {
2019ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
2020ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
2021b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
20222e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2023a17c678eSDavid Greenman 	}
20241abcdbd1SAttilio Rao 	return (rx_npkts);
2025a17c678eSDavid Greenman }
2026a17c678eSDavid Greenman 
2027303b270bSEivind Eklund static void
20288da9c507SPyun YongHyeon fxp_update_stats(struct fxp_softc *sc)
2029a17c678eSDavid Greenman {
2030fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2031a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
20328da9c507SPyun YongHyeon 	struct fxp_hwstats *hsp;
20338da9c507SPyun YongHyeon 	uint32_t *status;
2034a17c678eSDavid Greenman 
20353212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
20368da9c507SPyun YongHyeon 
20378da9c507SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
20388da9c507SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
20398da9c507SPyun YongHyeon 	/* Update statistical counters. */
20408da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
20418da9c507SPyun YongHyeon 		status = &sp->completion_status;
20428da9c507SPyun YongHyeon 	else if (sc->revision >= FXP_REV_82558_A4)
20438da9c507SPyun YongHyeon 		status = (uint32_t *)&sp->tx_tco;
20448da9c507SPyun YongHyeon 	else
20458da9c507SPyun YongHyeon 		status = &sp->tx_pause;
20468da9c507SPyun YongHyeon 	if (*status == htole32(FXP_STATS_DR_COMPLETE)) {
20478da9c507SPyun YongHyeon 		hsp = &sc->fxp_hwstats;
20488da9c507SPyun YongHyeon 		hsp->tx_good += le32toh(sp->tx_good);
20498da9c507SPyun YongHyeon 		hsp->tx_maxcols += le32toh(sp->tx_maxcols);
20508da9c507SPyun YongHyeon 		hsp->tx_latecols += le32toh(sp->tx_latecols);
20518da9c507SPyun YongHyeon 		hsp->tx_underruns += le32toh(sp->tx_underruns);
20528da9c507SPyun YongHyeon 		hsp->tx_lostcrs += le32toh(sp->tx_lostcrs);
20538da9c507SPyun YongHyeon 		hsp->tx_deffered += le32toh(sp->tx_deffered);
20548da9c507SPyun YongHyeon 		hsp->tx_single_collisions += le32toh(sp->tx_single_collisions);
20558da9c507SPyun YongHyeon 		hsp->tx_multiple_collisions +=
20568da9c507SPyun YongHyeon 		    le32toh(sp->tx_multiple_collisions);
20578da9c507SPyun YongHyeon 		hsp->tx_total_collisions += le32toh(sp->tx_total_collisions);
20588da9c507SPyun YongHyeon 		hsp->rx_good += le32toh(sp->rx_good);
20598da9c507SPyun YongHyeon 		hsp->rx_crc_errors += le32toh(sp->rx_crc_errors);
20608da9c507SPyun YongHyeon 		hsp->rx_alignment_errors += le32toh(sp->rx_alignment_errors);
20618da9c507SPyun YongHyeon 		hsp->rx_rnr_errors += le32toh(sp->rx_rnr_errors);
20628da9c507SPyun YongHyeon 		hsp->rx_overrun_errors += le32toh(sp->rx_overrun_errors);
20638da9c507SPyun YongHyeon 		hsp->rx_cdt_errors += le32toh(sp->rx_cdt_errors);
20648da9c507SPyun YongHyeon 		hsp->rx_shortframes += le32toh(sp->rx_shortframes);
20658da9c507SPyun YongHyeon 		hsp->tx_pause += le32toh(sp->tx_pause);
20668da9c507SPyun YongHyeon 		hsp->rx_pause += le32toh(sp->rx_pause);
20678da9c507SPyun YongHyeon 		hsp->rx_controls += le32toh(sp->rx_controls);
20688da9c507SPyun YongHyeon 		hsp->tx_tco += le16toh(sp->tx_tco);
20698da9c507SPyun YongHyeon 		hsp->rx_tco += le16toh(sp->rx_tco);
20708da9c507SPyun YongHyeon 
207183e6547dSMaxime Henrion 		ifp->if_opackets += le32toh(sp->tx_good);
207283e6547dSMaxime Henrion 		ifp->if_collisions += le32toh(sp->tx_total_collisions);
2073397f9dfeSDavid Greenman 		if (sp->rx_good) {
207483e6547dSMaxime Henrion 			ifp->if_ipackets += le32toh(sp->rx_good);
2075397f9dfeSDavid Greenman 			sc->rx_idle_secs = 0;
207643d8b117SPyun YongHyeon 		} else if (sc->flags & FXP_FLAG_RXBUG) {
2077c8cc6fcaSDavid Greenman 			/*
2078c8cc6fcaSDavid Greenman 			 * Receiver's been idle for another second.
2079c8cc6fcaSDavid Greenman 			 */
2080397f9dfeSDavid Greenman 			sc->rx_idle_secs++;
2081397f9dfeSDavid Greenman 		}
20823ba65732SDavid Greenman 		ifp->if_ierrors +=
208383e6547dSMaxime Henrion 		    le32toh(sp->rx_crc_errors) +
208483e6547dSMaxime Henrion 		    le32toh(sp->rx_alignment_errors) +
208583e6547dSMaxime Henrion 		    le32toh(sp->rx_rnr_errors) +
208683e6547dSMaxime Henrion 		    le32toh(sp->rx_overrun_errors);
2087a17c678eSDavid Greenman 		/*
2088f9be9005SDavid Greenman 		 * If any transmit underruns occured, bump up the transmit
2089f9be9005SDavid Greenman 		 * threshold by another 512 bytes (64 * 8).
2090f9be9005SDavid Greenman 		 */
2091f9be9005SDavid Greenman 		if (sp->tx_underruns) {
209283e6547dSMaxime Henrion 			ifp->if_oerrors += le32toh(sp->tx_underruns);
2093f9be9005SDavid Greenman 			if (tx_threshold < 192)
2094f9be9005SDavid Greenman 				tx_threshold += 64;
2095f9be9005SDavid Greenman 		}
20968da9c507SPyun YongHyeon 		*status = 0;
20978da9c507SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
20988da9c507SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
20998da9c507SPyun YongHyeon 	}
21008da9c507SPyun YongHyeon }
21018da9c507SPyun YongHyeon 
21028da9c507SPyun YongHyeon /*
21038da9c507SPyun YongHyeon  * Update packet in/out/collision statistics. The i82557 doesn't
21048da9c507SPyun YongHyeon  * allow you to access these counters without doing a fairly
21058da9c507SPyun YongHyeon  * expensive DMA to get _all_ of the statistics it maintains, so
21068da9c507SPyun YongHyeon  * we do this operation here only once per second. The statistics
21078da9c507SPyun YongHyeon  * counters in the kernel are updated from the previous dump-stats
21088da9c507SPyun YongHyeon  * DMA and then a new dump-stats DMA is started. The on-chip
21098da9c507SPyun YongHyeon  * counters are zeroed when the DMA completes. If we can't start
21108da9c507SPyun YongHyeon  * the DMA immediately, we don't wait - we just prepare to read
21118da9c507SPyun YongHyeon  * them again next time.
21128da9c507SPyun YongHyeon  */
21138da9c507SPyun YongHyeon static void
21148da9c507SPyun YongHyeon fxp_tick(void *xsc)
21158da9c507SPyun YongHyeon {
21168da9c507SPyun YongHyeon 	struct fxp_softc *sc = xsc;
21178da9c507SPyun YongHyeon 	struct ifnet *ifp = sc->ifp;
21188da9c507SPyun YongHyeon 
21198da9c507SPyun YongHyeon 	FXP_LOCK_ASSERT(sc, MA_OWNED);
21208da9c507SPyun YongHyeon 
21218da9c507SPyun YongHyeon 	/* Update statistical counters. */
21228da9c507SPyun YongHyeon 	fxp_update_stats(sc);
21234953bccaSNate Lawson 
2124397f9dfeSDavid Greenman 	/*
2125c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
2126c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
2127c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
2128c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
2129c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
2130c8cc6fcaSDavid Greenman 	 */
2131b2badf02SMaxime Henrion 	fxp_txeof(sc);
2132b2badf02SMaxime Henrion 
2133c8cc6fcaSDavid Greenman 	/*
2134397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
2135397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2136397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2137397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2138397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
2139397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2140397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2141397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2142397f9dfeSDavid Greenman 	 */
2143397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2144397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
2145c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
21461845b5c3SMarius Strobl 			fxp_init_body(sc, 1);
21476b24912cSPyun YongHyeon 		return;
2148397f9dfeSDavid Greenman 	}
2149f9be9005SDavid Greenman 	/*
21503ba65732SDavid Greenman 	 * If there is no pending command, start another stats
21513ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2152a17c678eSDavid Greenman 	 */
2153397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2154a17c678eSDavid Greenman 		/*
2155397f9dfeSDavid Greenman 		 * Start another stats dump.
2156a17c678eSDavid Greenman 		 */
21572e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2158dfe61cf1SDavid Greenman 	}
2159f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2160f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
21614953bccaSNate Lawson 
2162a17c678eSDavid Greenman 	/*
216316f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2164df79d527SGleb Smirnoff 	 */
2165df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2166df79d527SGleb Smirnoff 
2167df79d527SGleb Smirnoff 	/*
2168a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2169a17c678eSDavid Greenman 	 */
217045276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2171a17c678eSDavid Greenman }
2172a17c678eSDavid Greenman 
2173a17c678eSDavid Greenman /*
2174a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2175a17c678eSDavid Greenman  * the interface.
2176a17c678eSDavid Greenman  */
2177a17c678eSDavid Greenman static void
2178f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2179a17c678eSDavid Greenman {
2180fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2181b2badf02SMaxime Henrion 	struct fxp_tx *txp;
21823ba65732SDavid Greenman 	int i;
2183a17c678eSDavid Greenman 
218413f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2185df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
21867dced78aSDavid Greenman 
2187a17c678eSDavid Greenman 	/*
2188a17c678eSDavid Greenman 	 * Cancel stats updater.
2189a17c678eSDavid Greenman 	 */
219045276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
21913ba65732SDavid Greenman 
21923ba65732SDavid Greenman 	/*
21937137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
21947137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
21953ba65732SDavid Greenman 	 */
21967137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
219772a32a26SJonathan Lemon 	DELAY(50);
21987137cea0SPyun YongHyeon 	/* Disable interrupts. */
21997137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2200a17c678eSDavid Greenman 
22018da9c507SPyun YongHyeon 	fxp_update_stats(sc);
22028da9c507SPyun YongHyeon 
22033ba65732SDavid Greenman 	/*
22043ba65732SDavid Greenman 	 * Release any xmit buffers.
22053ba65732SDavid Greenman 	 */
2206b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2207da91462dSDavid Greenman 	if (txp != NULL) {
2208da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
2209b2badf02SMaxime Henrion 			if (txp[i].tx_mbuf != NULL) {
2210a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp[i].tx_map,
2211b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
2212a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag,
2213a2057a72SPyun YongHyeon 				    txp[i].tx_map);
2214b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
2215b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
2216c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
2217b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
2218da91462dSDavid Greenman 			}
2219da91462dSDavid Greenman 		}
22203ba65732SDavid Greenman 	}
2221a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2222a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
22233ba65732SDavid Greenman 	sc->tx_queued = 0;
2224a17c678eSDavid Greenman }
2225a17c678eSDavid Greenman 
2226a17c678eSDavid Greenman /*
2227a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2228a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2229a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2230a17c678eSDavid Greenman  * card has wedged for some reason.
2231a17c678eSDavid Greenman  */
2232a17c678eSDavid Greenman static void
2233df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2234a17c678eSDavid Greenman {
2235ba8c6fd5SDavid Greenman 
2236df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2237df79d527SGleb Smirnoff 
2238df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2239df79d527SGleb Smirnoff 		return;
2240df79d527SGleb Smirnoff 
2241f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2242df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
2243a17c678eSDavid Greenman 
22441845b5c3SMarius Strobl 	fxp_init_body(sc, 1);
2245a17c678eSDavid Greenman }
2246a17c678eSDavid Greenman 
22474953bccaSNate Lawson /*
22484953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
22494953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
22504953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
22514953bccaSNate Lawson  */
2252a17c678eSDavid Greenman static void
2253f7788e8eSJonathan Lemon fxp_init(void *xsc)
2254a17c678eSDavid Greenman {
2255fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
22564953bccaSNate Lawson 
22574953bccaSNate Lawson 	FXP_LOCK(sc);
22581845b5c3SMarius Strobl 	fxp_init_body(sc, 1);
22594953bccaSNate Lawson 	FXP_UNLOCK(sc);
22604953bccaSNate Lawson }
22614953bccaSNate Lawson 
22624953bccaSNate Lawson /*
22634953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
22644953bccaSNate Lawson  * softc lock held.
22654953bccaSNate Lawson  */
22664953bccaSNate Lawson static void
22671845b5c3SMarius Strobl fxp_init_body(struct fxp_softc *sc, int setmedia)
22684953bccaSNate Lawson {
2269fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
22701845b5c3SMarius Strobl 	struct mii_data *mii;
2271a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2272a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2273b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2274b2badf02SMaxime Henrion 	struct fxp_tx *txp;
22753212724cSJohn Baldwin 	int i, prm;
2276a17c678eSDavid Greenman 
227767fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2278a17c678eSDavid Greenman 	/*
22793ba65732SDavid Greenman 	 * Cancel any pending I/O
2280a17c678eSDavid Greenman 	 */
22813ba65732SDavid Greenman 	fxp_stop(sc);
2282a17c678eSDavid Greenman 
22837137cea0SPyun YongHyeon 	/*
22847137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
22857137cea0SPyun YongHyeon 	 */
22867137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
22877137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
22887137cea0SPyun YongHyeon 	DELAY(50);
22897137cea0SPyun YongHyeon 
2290a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
2291a17c678eSDavid Greenman 
2292a17c678eSDavid Greenman 	/*
2293a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2294a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2295a17c678eSDavid Greenman 	 */
2296ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
22972e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2298a17c678eSDavid Greenman 
2299ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23002e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2301a17c678eSDavid Greenman 
2302a17c678eSDavid Greenman 	/*
2303a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2304a17c678eSDavid Greenman 	 */
2305ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23068da9c507SPyun YongHyeon 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
23078da9c507SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
23088da9c507SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2309b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
23102e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2311a17c678eSDavid Greenman 
2312a17c678eSDavid Greenman 	/*
231372a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
2314b96ad4b2SPyun YongHyeon 	 * For ICH based controllers do not load microcode.
231572a32a26SJonathan Lemon 	 */
2316b96ad4b2SPyun YongHyeon 	if (sc->ident->ich == 0) {
2317b96ad4b2SPyun YongHyeon 		if (ifp->if_flags & IFF_LINK0 &&
2318b96ad4b2SPyun YongHyeon 		    (sc->flags & FXP_FLAG_UCODE) == 0)
231972a32a26SJonathan Lemon 			fxp_load_ucode(sc);
2320b96ad4b2SPyun YongHyeon 	}
232172a32a26SJonathan Lemon 
232272a32a26SJonathan Lemon 	/*
23236b24912cSPyun YongHyeon 	 * Set IFF_ALLMULTI status. It's needed in configure action
23246b24912cSPyun YongHyeon 	 * command.
232509882363SJonathan Lemon 	 */
23266b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
232709882363SJonathan Lemon 
232809882363SJonathan Lemon 	/*
2329a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2330a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2331a17c678eSDavid Greenman 	 * later.
2332a17c678eSDavid Greenman 	 */
2333b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2334a17c678eSDavid Greenman 
2335a17c678eSDavid Greenman 	/*
2336a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2337a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2338a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2339a17c678eSDavid Greenman 	 */
2340b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2341a17c678eSDavid Greenman 
2342a17c678eSDavid Greenman 	cbp->cb_status =	0;
234383e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
234483e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
234583e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
23462c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2347001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2348001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2349a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2350f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2351f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2352f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2353f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2354001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2355001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2356f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2357a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2358f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2359f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
23603114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2361f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2362f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2363f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
23648ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2365a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2366f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2367f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2368c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2369c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2370f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2371f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2372f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
2373f13075afSPyun YongHyeon 	    (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0;
2374f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2375f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2376f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2377f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2378a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2379a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2380a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2381a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2382a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2383a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2384a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2385a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2386f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2387f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2388f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2389f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2390f7788e8eSJonathan Lemon 
2391a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2392a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2393a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2394f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2395f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
23967137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2397a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
23983ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2399a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2400a026a25bSPyun YongHyeon 	cbp->mc_all =		ifp->if_flags & IFF_ALLMULTI ? 1 : prm;
2401c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2402bd4fa9d9SPyun YongHyeon 	cbp->vlan_strip_en =	((sc->flags & FXP_FLAG_EXT_RFA) != 0 &&
2403bd4fa9d9SPyun YongHyeon 	    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0;
2404a17c678eSDavid Greenman 
24051845b5c3SMarius Strobl 	if (sc->revision == FXP_REV_82557) {
24063bd07cfdSJonathan Lemon 		/*
24073bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
24083bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
24093bd07cfdSJonathan Lemon 		 */
24103bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
24113bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
24123bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
24133bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
24143bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
24153bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
24163bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
24173bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
24183bd07cfdSJonathan Lemon 	} else {
24191845b5c3SMarius Strobl 		/* Set pause RX FIFO threshold to 1KB. */
24201845b5c3SMarius Strobl 		CSR_WRITE_1(sc, FXP_CSR_FC_THRESH, 1);
24211845b5c3SMarius Strobl 		/* Set pause time. */
24221845b5c3SMarius Strobl 		cbp->fc_delay_lsb =	0xff;
24231845b5c3SMarius Strobl 		cbp->fc_delay_msb =	0xff;
24243bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
24251845b5c3SMarius Strobl 		mii = device_get_softc(sc->miibus);
24261845b5c3SMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
24271845b5c3SMarius Strobl 		    IFM_ETH_TXPAUSE) != 0)
24281845b5c3SMarius Strobl 			/* enable transmit FC */
24291845b5c3SMarius Strobl 			cbp->tx_fc_dis = 0;
24301845b5c3SMarius Strobl 		else
24311845b5c3SMarius Strobl 			/* disable transmit FC */
24321845b5c3SMarius Strobl 			cbp->tx_fc_dis = 1;
24331845b5c3SMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
24341845b5c3SMarius Strobl 		    IFM_ETH_RXPAUSE) != 0) {
24351845b5c3SMarius Strobl 			/* enable FC restart/restop frames */
24361845b5c3SMarius Strobl 			cbp->rx_fc_restart = 1;
24371845b5c3SMarius Strobl 			cbp->rx_fc_restop = 1;
24381845b5c3SMarius Strobl 		} else {
24391845b5c3SMarius Strobl 			/* disable FC restart/restop frames */
24401845b5c3SMarius Strobl 			cbp->rx_fc_restart = 0;
24411845b5c3SMarius Strobl 			cbp->rx_fc_restop = 0;
24421845b5c3SMarius Strobl 		}
24433bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
24443bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
24453bd07cfdSJonathan Lemon 	}
24463bd07cfdSJonathan Lemon 
24478da9c507SPyun YongHyeon 	/* Enable 82558 and 82559 extended statistics functionality. */
24488da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4) {
24498da9c507SPyun YongHyeon 		if (sc->revision >= FXP_REV_82559_A0) {
24508da9c507SPyun YongHyeon 			/*
24518da9c507SPyun YongHyeon 			 * Extend configuration table size to 32
24528da9c507SPyun YongHyeon 			 * to include TCO configuration.
24538da9c507SPyun YongHyeon 			 */
24548da9c507SPyun YongHyeon 			cbp->byte_count = 32;
24558da9c507SPyun YongHyeon 			cbp->ext_stats_dis = 1;
24568da9c507SPyun YongHyeon 			/* Enable TCO stats. */
24578da9c507SPyun YongHyeon 			cbp->tno_int_or_tco_en = 1;
24588da9c507SPyun YongHyeon 			cbp->gamla_rx = 1;
24598da9c507SPyun YongHyeon 		} else
24608da9c507SPyun YongHyeon 			cbp->ext_stats_dis = 0;
24618da9c507SPyun YongHyeon 	}
24628da9c507SPyun YongHyeon 
2463a17c678eSDavid Greenman 	/*
2464a17c678eSDavid Greenman 	 * Start the config command/DMA.
2465a17c678eSDavid Greenman 	 */
2466ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24675986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24685986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2469b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24702e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2471a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2472209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2473a17c678eSDavid Greenman 
2474a17c678eSDavid Greenman 	/*
2475a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2476a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2477a17c678eSDavid Greenman 	 */
2478b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2479a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
248083e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
248183e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
24824a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2483a17c678eSDavid Greenman 
2484a17c678eSDavid Greenman 	/*
2485a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2486a17c678eSDavid Greenman 	 */
2487ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24885986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24895986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
24906b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24912e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2492a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2493209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2494a17c678eSDavid Greenman 
2495a17c678eSDavid Greenman 	/*
24966b24912cSPyun YongHyeon 	 * Initialize the multicast address list.
24976b24912cSPyun YongHyeon 	 */
24986b24912cSPyun YongHyeon 	fxp_mc_setup(sc);
24996b24912cSPyun YongHyeon 
25006b24912cSPyun YongHyeon 	/*
2501a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2502a17c678eSDavid Greenman 	 */
2503b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2504b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2505b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2506a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2507b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
250883e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
250983e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
251083e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
251183e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
25123bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2513b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
251483e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
25153bd07cfdSJonathan Lemon 		else
2516b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
251783e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2518b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2519a17c678eSDavid Greenman 	}
2520a17c678eSDavid Greenman 	/*
2521397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2522a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2523a17c678eSDavid Greenman 	 */
252483e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2525a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2526a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2527b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2528397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2529a17c678eSDavid Greenman 
2530ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
25316b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
25322e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2533a17c678eSDavid Greenman 
2534a17c678eSDavid Greenman 	/*
2535a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2536a17c678eSDavid Greenman 	 */
2537ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2538b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
25392e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2540a17c678eSDavid Greenman 
25411845b5c3SMarius Strobl 	if (sc->miibus != NULL && setmedia != 0)
2542f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2543dccee1a1SDavid Greenman 
254413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
254513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2546e8c8b728SJonathan Lemon 
2547e8c8b728SJonathan Lemon 	/*
2548e8c8b728SJonathan Lemon 	 * Enable interrupts.
2549e8c8b728SJonathan Lemon 	 */
25502b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
25512b5989e9SLuigi Rizzo 	/*
25522b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
25532b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
25542b5989e9SLuigi Rizzo 	 */
255540929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
25562b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
25572b5989e9SLuigi Rizzo 	else
25582b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2559e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2560a17c678eSDavid Greenman 
2561a17c678eSDavid Greenman 	/*
2562a17c678eSDavid Greenman 	 * Start stats updater.
2563a17c678eSDavid Greenman 	 */
256445276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2565f7788e8eSJonathan Lemon }
2566f7788e8eSJonathan Lemon 
2567f7788e8eSJonathan Lemon static int
2568f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2569f7788e8eSJonathan Lemon {
2570f7788e8eSJonathan Lemon 
2571f7788e8eSJonathan Lemon 	return (0);
2572a17c678eSDavid Greenman }
2573a17c678eSDavid Greenman 
2574303b270bSEivind Eklund static void
2575f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2576ba8c6fd5SDavid Greenman {
2577ba8c6fd5SDavid Greenman 
2578f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2579ba8c6fd5SDavid Greenman }
2580ba8c6fd5SDavid Greenman 
2581ba8c6fd5SDavid Greenman /*
2582ba8c6fd5SDavid Greenman  * Change media according to request.
2583ba8c6fd5SDavid Greenman  */
2584f7788e8eSJonathan Lemon static int
2585f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2586ba8c6fd5SDavid Greenman {
2587ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2588f7788e8eSJonathan Lemon 	struct mii_data *mii;
25893fcb7a53SMarius Strobl 	struct mii_softc	*miisc;
2590ba8c6fd5SDavid Greenman 
2591f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25923212724cSJohn Baldwin 	FXP_LOCK(sc);
25935aa0cdf4SJohn-Mark Gurney 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
25943fcb7a53SMarius Strobl 		PHY_RESET(miisc);
2595f7788e8eSJonathan Lemon 	mii_mediachg(mii);
25963212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2597ba8c6fd5SDavid Greenman 	return (0);
2598ba8c6fd5SDavid Greenman }
2599ba8c6fd5SDavid Greenman 
2600ba8c6fd5SDavid Greenman /*
2601ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2602ba8c6fd5SDavid Greenman  */
2603f7788e8eSJonathan Lemon static void
2604f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2605ba8c6fd5SDavid Greenman {
2606ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2607f7788e8eSJonathan Lemon 	struct mii_data *mii;
2608ba8c6fd5SDavid Greenman 
2609f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
26103212724cSJohn Baldwin 	FXP_LOCK(sc);
2611f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2612f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2613f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
26142e2b8238SJonathan Lemon 
26152b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
26162b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
26172e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
26182e2b8238SJonathan Lemon 	else
26192e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
26203212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2621ba8c6fd5SDavid Greenman }
2622ba8c6fd5SDavid Greenman 
2623a17c678eSDavid Greenman /*
2624a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2625a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
262685050421SPyun YongHyeon  * reusing the RFA buffer.
2627a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2628a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2629a17c678eSDavid Greenman  */
2630a17c678eSDavid Greenman static int
263185050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2632a17c678eSDavid Greenman {
2633a17c678eSDavid Greenman 	struct mbuf *m;
263485050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2635b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
263685050421SPyun YongHyeon 	int error;
2637a17c678eSDavid Greenman 
2638a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
263985050421SPyun YongHyeon 	if (m == NULL)
264085050421SPyun YongHyeon 		return (ENOBUFS);
2641ba8c6fd5SDavid Greenman 
2642ba8c6fd5SDavid Greenman 	/*
2643ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2644ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2645ba8c6fd5SDavid Greenman 	 */
2646ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2647ba8c6fd5SDavid Greenman 
2648eadd5e3aSDavid Greenman 	/*
2649eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2650eadd5e3aSDavid Greenman 	 * data start past it.
2651eadd5e3aSDavid Greenman 	 */
2652a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2653c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
265483e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2655eadd5e3aSDavid Greenman 
2656a17c678eSDavid Greenman 	rfa->rfa_status = 0;
265783e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2658a17c678eSDavid Greenman 	rfa->actual_size = 0;
265985050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
266085050421SPyun YongHyeon 	    sc->rfa_size;
2661ba8c6fd5SDavid Greenman 
266228935f27SMaxime Henrion 	/*
266328935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
266428935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
266528935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
266628935f27SMaxime Henrion 	 * is also alignment-safe.
266728935f27SMaxime Henrion 	 */
266883e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
266983e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2670ba8c6fd5SDavid Greenman 
2671b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2672a2057a72SPyun YongHyeon 	error = bus_dmamap_load(sc->fxp_rxmtag, sc->spare_map, rfa,
2673b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
267401e3ef82SPyun YongHyeon 	    &rxp->rx_addr, BUS_DMA_NOWAIT);
2675b2badf02SMaxime Henrion 	if (error) {
2676b2badf02SMaxime Henrion 		m_freem(m);
2677b2badf02SMaxime Henrion 		return (error);
2678b2badf02SMaxime Henrion 	}
2679b2badf02SMaxime Henrion 
2680e2157cf7SPyun YongHyeon 	if (rxp->rx_mbuf != NULL)
2681a2057a72SPyun YongHyeon 		bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
2682b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2683b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2684b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2685b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2686b2badf02SMaxime Henrion 
2687a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
2688b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
268985050421SPyun YongHyeon 	return (0);
269085050421SPyun YongHyeon }
269185050421SPyun YongHyeon 
269285050421SPyun YongHyeon static void
269385050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
269485050421SPyun YongHyeon {
269585050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
269685050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2697b2badf02SMaxime Henrion 
2698dfe61cf1SDavid Greenman 	/*
2699dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2700dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2701dfe61cf1SDavid Greenman 	 */
2702b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2703b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2704b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2705b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2706b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
270783e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2708aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2709a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, p_rx->rx_map,
27104812aef5SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2711a17c678eSDavid Greenman 	} else {
2712b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2713b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2714a17c678eSDavid Greenman 	}
2715b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
271685050421SPyun YongHyeon }
271785050421SPyun YongHyeon 
271885050421SPyun YongHyeon static void
271985050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
272085050421SPyun YongHyeon {
272185050421SPyun YongHyeon 	struct mbuf *m;
272285050421SPyun YongHyeon 	struct fxp_rfa *rfa;
272385050421SPyun YongHyeon 
272485050421SPyun YongHyeon 	m = rxp->rx_mbuf;
272585050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
272685050421SPyun YongHyeon 	/*
272785050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
272885050421SPyun YongHyeon 	 * will be 32-bit aligned.
272985050421SPyun YongHyeon 	 */
273085050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
273185050421SPyun YongHyeon 
273285050421SPyun YongHyeon 	/*
273385050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
273485050421SPyun YongHyeon 	 * data start past it.
273585050421SPyun YongHyeon 	 */
273685050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
273785050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
273885050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
273985050421SPyun YongHyeon 
274085050421SPyun YongHyeon 	rfa->rfa_status = 0;
274185050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
274285050421SPyun YongHyeon 	rfa->actual_size = 0;
274385050421SPyun YongHyeon 
274485050421SPyun YongHyeon 	/*
274585050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
274685050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
274785050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
274885050421SPyun YongHyeon 	 * is also alignment-safe.
274985050421SPyun YongHyeon 	 */
275085050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
275185050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
275285050421SPyun YongHyeon 
2753a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
275485050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2755a17c678eSDavid Greenman }
2756a17c678eSDavid Greenman 
2757f1928b0cSKevin Lo static int
2758f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2759dccee1a1SDavid Greenman {
2760f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2761dccee1a1SDavid Greenman 	int count = 10000;
27626ebc3153SDavid Greenman 	int value;
2763dccee1a1SDavid Greenman 
2764ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2765ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2766dccee1a1SDavid Greenman 
2767ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2768ba8c6fd5SDavid Greenman 	    && count--)
27696ebc3153SDavid Greenman 		DELAY(10);
2770dccee1a1SDavid Greenman 
2771dccee1a1SDavid Greenman 	if (count <= 0)
2772f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2773dccee1a1SDavid Greenman 
27746ebc3153SDavid Greenman 	return (value & 0xffff);
2775dccee1a1SDavid Greenman }
2776dccee1a1SDavid Greenman 
277716ec4b00SWarner Losh static int
2778f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2779dccee1a1SDavid Greenman {
2780f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2781dccee1a1SDavid Greenman 	int count = 10000;
2782dccee1a1SDavid Greenman 
2783ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2784ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2785ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2786dccee1a1SDavid Greenman 
2787ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2788ba8c6fd5SDavid Greenman 	    count--)
27896ebc3153SDavid Greenman 		DELAY(10);
2790dccee1a1SDavid Greenman 
2791dccee1a1SDavid Greenman 	if (count <= 0)
2792f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
279316ec4b00SWarner Losh 	return (0);
2794dccee1a1SDavid Greenman }
2795dccee1a1SDavid Greenman 
27961845b5c3SMarius Strobl static void
27971845b5c3SMarius Strobl fxp_miibus_statchg(device_t dev)
27981845b5c3SMarius Strobl {
27991845b5c3SMarius Strobl 	struct fxp_softc *sc;
28001845b5c3SMarius Strobl 	struct mii_data *mii;
28011845b5c3SMarius Strobl 	struct ifnet *ifp;
28021845b5c3SMarius Strobl 
28031845b5c3SMarius Strobl 	sc = device_get_softc(dev);
28041845b5c3SMarius Strobl 	mii = device_get_softc(sc->miibus);
28051845b5c3SMarius Strobl 	ifp = sc->ifp;
28061845b5c3SMarius Strobl 	if (mii == NULL || ifp == NULL ||
28071845b5c3SMarius Strobl 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
28081845b5c3SMarius Strobl 	    (mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) !=
28091845b5c3SMarius Strobl 	    (IFM_AVALID | IFM_ACTIVE))
28101845b5c3SMarius Strobl 		return;
28111845b5c3SMarius Strobl 
28121845b5c3SMarius Strobl 	/*
28131845b5c3SMarius Strobl 	 * Call fxp_init_body in order to adjust the flow control settings.
28141845b5c3SMarius Strobl 	 * Note that the 82557 doesn't support hardware flow control.
28151845b5c3SMarius Strobl 	 */
28161845b5c3SMarius Strobl 	if (sc->revision == FXP_REV_82557)
28171845b5c3SMarius Strobl 		return;
28181845b5c3SMarius Strobl 	fxp_init_body(sc, 0);
28191845b5c3SMarius Strobl }
28201845b5c3SMarius Strobl 
2821dccee1a1SDavid Greenman static int
2822f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2823a17c678eSDavid Greenman {
28249b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2825a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2826f7788e8eSJonathan Lemon 	struct mii_data *mii;
282760bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2828a17c678eSDavid Greenman 
2829a17c678eSDavid Greenman 	switch (command) {
2830a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
28313212724cSJohn Baldwin 		FXP_LOCK(sc);
2832a17c678eSDavid Greenman 		/*
2833a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2834a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2835a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2836a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2837a17c678eSDavid Greenman 		 */
2838a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
28396b24912cSPyun YongHyeon 			if (((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) &&
28406b24912cSPyun YongHyeon 			    ((ifp->if_flags ^ sc->if_flags) &
28416b24912cSPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI | IFF_LINK0)) != 0)
2842a461b201SPyun YongHyeon 				fxp_init_body(sc, 0);
28436b24912cSPyun YongHyeon 			else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
28441845b5c3SMarius Strobl 				fxp_init_body(sc, 1);
2845a17c678eSDavid Greenman 		} else {
28466b24912cSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
28474a5f1499SDavid Greenman 				fxp_stop(sc);
2848a17c678eSDavid Greenman 		}
28496b24912cSPyun YongHyeon 		sc->if_flags = ifp->if_flags;
28503212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2851a17c678eSDavid Greenman 		break;
2852a17c678eSDavid Greenman 
2853a17c678eSDavid Greenman 	case SIOCADDMULTI:
2854a17c678eSDavid Greenman 	case SIOCDELMULTI:
2855f6ff7180SPyun YongHyeon 		FXP_LOCK(sc);
28566b24912cSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2857f6ff7180SPyun YongHyeon 			fxp_init_body(sc, 0);
2858f6ff7180SPyun YongHyeon 		FXP_UNLOCK(sc);
2859ba8c6fd5SDavid Greenman 		break;
2860ba8c6fd5SDavid Greenman 
2861ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2862ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2863f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2864f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2865f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2866f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2867f7788e8eSJonathan Lemon 		} else {
2868ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2869f7788e8eSJonathan Lemon 		}
2870a17c678eSDavid Greenman 		break;
2871a17c678eSDavid Greenman 
2872fb917226SRuslan Ermilov 	case SIOCSIFCAP:
287360bb79ebSPyun YongHyeon 		reinit = 0;
28748ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
287540929967SGleb Smirnoff #ifdef DEVICE_POLLING
287640929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
287740929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
287840929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
287940929967SGleb Smirnoff 				if (error)
288040929967SGleb Smirnoff 					return(error);
288140929967SGleb Smirnoff 				FXP_LOCK(sc);
288240929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
288340929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
288440929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
288540929967SGleb Smirnoff 				FXP_UNLOCK(sc);
288640929967SGleb Smirnoff 			} else {
288740929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
288840929967SGleb Smirnoff 				/* Enable interrupts in any case */
288940929967SGleb Smirnoff 				FXP_LOCK(sc);
289040929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
289140929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
289240929967SGleb Smirnoff 				FXP_UNLOCK(sc);
289340929967SGleb Smirnoff 			}
289440929967SGleb Smirnoff 		}
289540929967SGleb Smirnoff #endif
289640929967SGleb Smirnoff 		FXP_LOCK(sc);
289760bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
289860bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
289960bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
290060bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
290160bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
290260bb79ebSPyun YongHyeon 			else
290360bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
290460bb79ebSPyun YongHyeon 		}
290560bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2906f13075afSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
290760bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2908f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2909f13075afSPyun YongHyeon 				reinit++;
2910f13075afSPyun YongHyeon 		}
2911c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
2912c21e84e4SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2913c21e84e4SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2914c21e84e4SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
2915c21e84e4SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2916c21e84e4SPyun YongHyeon 			else
2917c21e84e4SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2918c21e84e4SPyun YongHyeon 		}
29197137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
29207137cea0SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
29217137cea0SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
292260bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
292360bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
29248ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
29258ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
29268ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
29278ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
29288ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
29298ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
29308ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
293160bb79ebSPyun YongHyeon 				reinit++;
293260bb79ebSPyun YongHyeon 		}
2933713ca255SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2934713ca255SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2935713ca255SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2936713ca255SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
2937713ca255SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
2938713ca255SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
2939bd4fa9d9SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2940bd4fa9d9SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2941bd4fa9d9SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2942713ca255SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2943713ca255SPyun YongHyeon 				ifp->if_capenable &=
2944713ca255SPyun YongHyeon 				    ~(IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
2945bd4fa9d9SPyun YongHyeon 			reinit++;
2946bd4fa9d9SPyun YongHyeon 		}
2947bd4fa9d9SPyun YongHyeon 		if (reinit > 0 && ifp->if_flags & IFF_UP)
2948a461b201SPyun YongHyeon 			fxp_init_body(sc, 0);
29493212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2950bd4fa9d9SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
2951fb917226SRuslan Ermilov 		break;
2952fb917226SRuslan Ermilov 
2953a17c678eSDavid Greenman 	default:
2954673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2955a17c678eSDavid Greenman 	}
2956a17c678eSDavid Greenman 	return (error);
2957a17c678eSDavid Greenman }
2958397f9dfeSDavid Greenman 
2959397f9dfeSDavid Greenman /*
296009882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
296109882363SJonathan Lemon  */
296209882363SJonathan Lemon static int
296309882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
296409882363SJonathan Lemon {
296509882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2966fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
296709882363SJonathan Lemon 	struct ifmultiaddr *ifma;
296809882363SJonathan Lemon 	int nmcasts;
296909882363SJonathan Lemon 
297009882363SJonathan Lemon 	nmcasts = 0;
29716b24912cSPyun YongHyeon 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2972eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
297309882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
297409882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
297509882363SJonathan Lemon 				continue;
297609882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
29776b24912cSPyun YongHyeon 				ifp->if_flags |= IFF_ALLMULTI;
297809882363SJonathan Lemon 				nmcasts = 0;
297909882363SJonathan Lemon 				break;
298009882363SJonathan Lemon 			}
298109882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2982bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
298309882363SJonathan Lemon 			nmcasts++;
298409882363SJonathan Lemon 		}
2985eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
298609882363SJonathan Lemon 	}
2987bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
298809882363SJonathan Lemon 	return (nmcasts);
298909882363SJonathan Lemon }
299009882363SJonathan Lemon 
299109882363SJonathan Lemon /*
2992397f9dfeSDavid Greenman  * Program the multicast filter.
2993397f9dfeSDavid Greenman  *
2994397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2995397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
29963114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2997397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2998dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2999397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
3000397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
3001397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
3002397f9dfeSDavid Greenman  */
3003397f9dfeSDavid Greenman static void
3004f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
3005397f9dfeSDavid Greenman {
30066b24912cSPyun YongHyeon 	struct fxp_cb_mcs *mcsp;
30077dced78aSDavid Greenman 	int count;
3008397f9dfeSDavid Greenman 
300967fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
30103114fdb4SDavid Greenman 
30116b24912cSPyun YongHyeon 	mcsp = sc->mcsp;
3012397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
30136b24912cSPyun YongHyeon 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
30146b24912cSPyun YongHyeon 	mcsp->link_addr = 0xffffffff;
30156b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
3016397f9dfeSDavid Greenman 
3017397f9dfeSDavid Greenman 	/*
30186b24912cSPyun YongHyeon 	 * Wait until command unit is idle. This should never be the
30196b24912cSPyun YongHyeon 	 * case when nothing is queued, but make sure anyway.
3020397f9dfeSDavid Greenman 	 */
30217dced78aSDavid Greenman 	count = 100;
30226b24912cSPyun YongHyeon 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) !=
30236b24912cSPyun YongHyeon 	    FXP_SCB_CUS_IDLE && --count)
30247dced78aSDavid Greenman 		DELAY(10);
30257dced78aSDavid Greenman 	if (count == 0) {
3026f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
30277dced78aSDavid Greenman 		return;
30287dced78aSDavid Greenman 	}
3029397f9dfeSDavid Greenman 
3030397f9dfeSDavid Greenman 	/*
3031397f9dfeSDavid Greenman 	 * Start the multicast setup command.
3032397f9dfeSDavid Greenman 	 */
3033397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
3034a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
3035a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3036b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
30372e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
30386b24912cSPyun YongHyeon 	/* ...and wait for it to complete. */
30396b24912cSPyun YongHyeon 	fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
3040397f9dfeSDavid Greenman }
304172a32a26SJonathan Lemon 
304274d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
304374d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
304474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
304574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
304674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
304774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
3048de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
304972a32a26SJonathan Lemon 
305074d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
305172a32a26SJonathan Lemon 
3052e0fe5c6dSMarius Strobl static const struct ucode {
305374d1ed23SMaxime Henrion 	uint32_t	revision;
305474d1ed23SMaxime Henrion 	uint32_t	*ucode;
305572a32a26SJonathan Lemon 	int		length;
305672a32a26SJonathan Lemon 	u_short		int_delay_offset;
305772a32a26SJonathan Lemon 	u_short		bundle_max_offset;
3058e0fe5c6dSMarius Strobl } const ucode_table[] = {
305972a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
306072a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
306172a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
306272a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
306372a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
306472a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
306572a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
306672a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
306772a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
306872a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
3069507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
3070de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
307150df388dSPyun YongHyeon 	{ FXP_REV_82551_10, UCODE(fxp_ucode_d102e),
307250df388dSPyun YongHyeon 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
307372a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
307472a32a26SJonathan Lemon };
307572a32a26SJonathan Lemon 
307672a32a26SJonathan Lemon static void
307772a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
307872a32a26SJonathan Lemon {
3079e0fe5c6dSMarius Strobl 	const struct ucode *uc;
308072a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
308194a4f968SPyun YongHyeon 	int i;
308272a32a26SJonathan Lemon 
30831343a72fSPyun YongHyeon 	if (sc->flags & FXP_FLAG_NO_UCODE)
30841343a72fSPyun YongHyeon 		return;
30851343a72fSPyun YongHyeon 
308672a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
308772a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
308872a32a26SJonathan Lemon 			break;
308972a32a26SJonathan Lemon 	if (uc->ucode == NULL)
309072a32a26SJonathan Lemon 		return;
3091b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
309272a32a26SJonathan Lemon 	cbp->cb_status = 0;
309383e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
309483e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
309594a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
309694a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
309772a32a26SJonathan Lemon 	if (uc->int_delay_offset)
309874d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
309983e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
310072a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
310174d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
310283e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
310372a32a26SJonathan Lemon 	/*
310472a32a26SJonathan Lemon 	 * Download the ucode to the chip.
310572a32a26SJonathan Lemon 	 */
310672a32a26SJonathan Lemon 	fxp_scb_wait(sc);
31075986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
31085986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3109b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
311072a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
311172a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
3112209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
311372a32a26SJonathan Lemon 	device_printf(sc->dev,
311472a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
311572a32a26SJonathan Lemon 	    sc->tunable_int_delay,
311672a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
311772a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
31181343a72fSPyun YongHyeon 	bzero(cbp, FXP_TXCB_SZ);
311972a32a26SJonathan Lemon }
312072a32a26SJonathan Lemon 
31218da9c507SPyun YongHyeon #define FXP_SYSCTL_STAT_ADD(c, h, n, p, d)	\
31228da9c507SPyun YongHyeon 	SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
31238da9c507SPyun YongHyeon 
31248da9c507SPyun YongHyeon static void
31258da9c507SPyun YongHyeon fxp_sysctl_node(struct fxp_softc *sc)
31268da9c507SPyun YongHyeon {
31278da9c507SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
31288da9c507SPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
31298da9c507SPyun YongHyeon 	struct sysctl_oid *tree;
31308da9c507SPyun YongHyeon 	struct fxp_hwstats *hsp;
31318da9c507SPyun YongHyeon 
31328da9c507SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->dev);
31338da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
31348da9c507SPyun YongHyeon 
31358da9c507SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child,
31368da9c507SPyun YongHyeon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
31378da9c507SPyun YongHyeon 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
31388da9c507SPyun YongHyeon 	    "FXP driver receive interrupt microcode bundling delay");
31398da9c507SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child,
31408da9c507SPyun YongHyeon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
31418da9c507SPyun YongHyeon 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
31428da9c507SPyun YongHyeon 	    "FXP driver receive interrupt microcode bundle size limit");
31438da9c507SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, child,OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
31448da9c507SPyun YongHyeon 	    "FXP RNR events");
31458da9c507SPyun YongHyeon 
31468da9c507SPyun YongHyeon 	/*
31478da9c507SPyun YongHyeon 	 * Pull in device tunables.
31488da9c507SPyun YongHyeon 	 */
31498da9c507SPyun YongHyeon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
31508da9c507SPyun YongHyeon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
31518da9c507SPyun YongHyeon 	(void) resource_int_value(device_get_name(sc->dev),
31528da9c507SPyun YongHyeon 	    device_get_unit(sc->dev), "int_delay", &sc->tunable_int_delay);
31538da9c507SPyun YongHyeon 	(void) resource_int_value(device_get_name(sc->dev),
31548da9c507SPyun YongHyeon 	    device_get_unit(sc->dev), "bundle_max", &sc->tunable_bundle_max);
31558da9c507SPyun YongHyeon 	sc->rnr = 0;
31568da9c507SPyun YongHyeon 
31578da9c507SPyun YongHyeon 	hsp = &sc->fxp_hwstats;
31588da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
31598da9c507SPyun YongHyeon 	    NULL, "FXP statistics");
31608da9c507SPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
31618da9c507SPyun YongHyeon 
31628da9c507SPyun YongHyeon 	/* Rx MAC statistics. */
31638da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
31648da9c507SPyun YongHyeon 	    NULL, "Rx MAC statistics");
31658da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
31668da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "good_frames",
31678da9c507SPyun YongHyeon 	    &hsp->rx_good, "Good frames");
31688da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "crc_errors",
31698da9c507SPyun YongHyeon 	    &hsp->rx_crc_errors, "CRC errors");
31708da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "alignment_errors",
31718da9c507SPyun YongHyeon 	    &hsp->rx_alignment_errors, "Alignment errors");
31728da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "rnr_errors",
31738da9c507SPyun YongHyeon 	    &hsp->rx_rnr_errors, "RNR errors");
31748da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "overrun_errors",
31758da9c507SPyun YongHyeon 	    &hsp->rx_overrun_errors, "Overrun errors");
31768da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "cdt_errors",
31778da9c507SPyun YongHyeon 	    &hsp->rx_cdt_errors, "Collision detect errors");
31788da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "shortframes",
31798da9c507SPyun YongHyeon 	    &hsp->rx_shortframes, "Short frame errors");
31808da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4) {
31818da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "pause",
31828da9c507SPyun YongHyeon 		    &hsp->rx_pause, "Pause frames");
31838da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "controls",
31848da9c507SPyun YongHyeon 		    &hsp->rx_controls, "Unsupported control frames");
31858da9c507SPyun YongHyeon 	}
31868da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
31878da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "tco",
31888da9c507SPyun YongHyeon 		    &hsp->rx_tco, "TCO frames");
31898da9c507SPyun YongHyeon 
31908da9c507SPyun YongHyeon 	/* Tx MAC statistics. */
31918da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
31928da9c507SPyun YongHyeon 	    NULL, "Tx MAC statistics");
31938da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
31948da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "good_frames",
31958da9c507SPyun YongHyeon 	    &hsp->tx_good, "Good frames");
31968da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "maxcols",
31978da9c507SPyun YongHyeon 	    &hsp->tx_maxcols, "Maximum collisions errors");
31988da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "latecols",
31998da9c507SPyun YongHyeon 	    &hsp->tx_latecols, "Late collisions errors");
32008da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "underruns",
32018da9c507SPyun YongHyeon 	    &hsp->tx_underruns, "Underrun errors");
32028da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "lostcrs",
32038da9c507SPyun YongHyeon 	    &hsp->tx_lostcrs, "Lost carrier sense");
32048da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "deffered",
32058da9c507SPyun YongHyeon 	    &hsp->tx_deffered, "Deferred");
32068da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "single_collisions",
32078da9c507SPyun YongHyeon 	    &hsp->tx_single_collisions, "Single collisions");
32088da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "multiple_collisions",
32098da9c507SPyun YongHyeon 	    &hsp->tx_multiple_collisions, "Multiple collisions");
32108da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "total_collisions",
32118da9c507SPyun YongHyeon 	    &hsp->tx_total_collisions, "Total collisions");
32128da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4)
32138da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "pause",
32148da9c507SPyun YongHyeon 		    &hsp->tx_pause, "Pause frames");
32158da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
32168da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "tco",
32178da9c507SPyun YongHyeon 		    &hsp->tx_tco, "TCO frames");
32188da9c507SPyun YongHyeon }
32198da9c507SPyun YongHyeon 
32208da9c507SPyun YongHyeon #undef FXP_SYSCTL_STAT_ADD
32218da9c507SPyun YongHyeon 
322272a32a26SJonathan Lemon static int
322372a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
322472a32a26SJonathan Lemon {
322572a32a26SJonathan Lemon 	int error, value;
322672a32a26SJonathan Lemon 
322772a32a26SJonathan Lemon 	value = *(int *)arg1;
322872a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
322972a32a26SJonathan Lemon 	if (error || !req->newptr)
323072a32a26SJonathan Lemon 		return (error);
323172a32a26SJonathan Lemon 	if (value < low || value > high)
323272a32a26SJonathan Lemon 		return (EINVAL);
323372a32a26SJonathan Lemon 	*(int *)arg1 = value;
323472a32a26SJonathan Lemon 	return (0);
323572a32a26SJonathan Lemon }
323672a32a26SJonathan Lemon 
323772a32a26SJonathan Lemon /*
323872a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
323972a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
324072a32a26SJonathan Lemon  */
324172a32a26SJonathan Lemon static int
324272a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
324372a32a26SJonathan Lemon {
3244e0fe5c6dSMarius Strobl 
324572a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
324672a32a26SJonathan Lemon }
324772a32a26SJonathan Lemon 
324872a32a26SJonathan Lemon static int
324972a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
325072a32a26SJonathan Lemon {
3251e0fe5c6dSMarius Strobl 
325272a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
325372a32a26SJonathan Lemon }
3254