xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 50df388d0ef6237d473f88c68b07b2324b0c8d9b)
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" },
197b96ad4b2SPyun YongHyeon     { 0x1229,	0x0d,	0, "Intel 82550 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);
251f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
252f7788e8eSJonathan Lemon 			    int offset, int words);
25300c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
25400c4116bSJonathan Lemon 			    int offset, int words);
255f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
256f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
257f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
258f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
259f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
260f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
261f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
26216ec4b00SWarner Losh static int		fxp_miibus_writereg(device_t dev, int phy, int reg,
263f7788e8eSJonathan Lemon 			    int value);
2641845b5c3SMarius Strobl static void		fxp_miibus_statchg(device_t dev);
26572a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
2668da9c507SPyun YongHyeon static void		fxp_update_stats(struct fxp_softc *sc);
2678da9c507SPyun YongHyeon static void		fxp_sysctl_node(struct fxp_softc *sc);
26872a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
26972a32a26SJonathan Lemon 			    int low, int high);
27072a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
27172a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
27228935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
27328935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
27428935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
27574d1ed23SMaxime Henrion     			    volatile uint16_t *status, bus_dma_tag_t dmat,
276209b07bcSMaxime Henrion 			    bus_dmamap_t map);
277f7788e8eSJonathan Lemon 
278f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
279f7788e8eSJonathan Lemon 	/* Device interface */
280f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
281f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
282f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
283f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
284f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
285f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
286f7788e8eSJonathan Lemon 
287f7788e8eSJonathan Lemon 	/* MII interface */
288f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
289f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
2901845b5c3SMarius Strobl 	DEVMETHOD(miibus_statchg,	fxp_miibus_statchg),
291f7788e8eSJonathan Lemon 
292f7788e8eSJonathan Lemon 	{ 0, 0 }
293f7788e8eSJonathan Lemon };
294f7788e8eSJonathan Lemon 
295f7788e8eSJonathan Lemon static driver_t fxp_driver = {
296f7788e8eSJonathan Lemon 	"fxp",
297f7788e8eSJonathan Lemon 	fxp_methods,
298f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
299f7788e8eSJonathan Lemon };
300f7788e8eSJonathan Lemon 
301f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
302f7788e8eSJonathan Lemon 
303f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
304f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
305f7788e8eSJonathan Lemon 
30605bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
30705bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
30805bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30905bd8c22SMaxime Henrion 	{ -1, 0 }
31005bd8c22SMaxime Henrion };
31105bd8c22SMaxime Henrion 
31205bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
31305bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
31405bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
31505bd8c22SMaxime Henrion 	{ -1, 0 }
31605bd8c22SMaxime Henrion };
31705bd8c22SMaxime Henrion 
318f7788e8eSJonathan Lemon /*
319dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
320dfe61cf1SDavid Greenman  * completed).
321dfe61cf1SDavid Greenman  */
32228935f27SMaxime Henrion static void
323f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
324a17c678eSDavid Greenman {
3253cf09dd1SMarcel Moolenaar 	union {
3263cf09dd1SMarcel Moolenaar 		uint16_t w;
3273cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3283cf09dd1SMarcel Moolenaar 	} flowctl;
329a17c678eSDavid Greenman 	int i = 10000;
330a17c678eSDavid Greenman 
3317dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3327dced78aSDavid Greenman 		DELAY(2);
3333cf09dd1SMarcel Moolenaar 	if (i == 0) {
3341845b5c3SMarius Strobl 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FC_THRESH);
3351845b5c3SMarius Strobl 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FC_STATUS);
33600c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
337e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
338e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3393cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3403cf09dd1SMarcel Moolenaar 	}
3417dced78aSDavid Greenman }
3427dced78aSDavid Greenman 
34328935f27SMaxime Henrion static void
3442e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3452e2b8238SJonathan Lemon {
3462e2b8238SJonathan Lemon 
3472e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3482e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3492e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3502e2b8238SJonathan Lemon 	}
3512e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3522e2b8238SJonathan Lemon }
3532e2b8238SJonathan Lemon 
35428935f27SMaxime Henrion static void
35574d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
356209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3577dced78aSDavid Greenman {
3585986d0d2SPyun YongHyeon 	int i;
3597dced78aSDavid Greenman 
3605986d0d2SPyun YongHyeon 	for (i = 10000; i > 0; i--) {
3617dced78aSDavid Greenman 		DELAY(2);
3625986d0d2SPyun YongHyeon 		bus_dmamap_sync(dmat, map,
3635986d0d2SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3645986d0d2SPyun YongHyeon 		if ((le16toh(*status) & FXP_CB_STATUS_C) != 0)
3655986d0d2SPyun YongHyeon 			break;
366209b07bcSMaxime Henrion 	}
3677dced78aSDavid Greenman 	if (i == 0)
368f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
369a17c678eSDavid Greenman }
370a17c678eSDavid Greenman 
371e0fe5c6dSMarius Strobl static const struct fxp_ident *
372b96ad4b2SPyun YongHyeon fxp_find_ident(device_t dev)
373a17c678eSDavid Greenman {
37474d1ed23SMaxime Henrion 	uint16_t devid;
37574d1ed23SMaxime Henrion 	uint8_t revid;
376e0fe5c6dSMarius Strobl 	const struct fxp_ident *ident;
377f7788e8eSJonathan Lemon 
37855ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
379f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
380f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
381f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
382f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
383f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
384b96ad4b2SPyun YongHyeon 				return (ident);
385b96ad4b2SPyun YongHyeon 			}
386b96ad4b2SPyun YongHyeon 		}
387b96ad4b2SPyun YongHyeon 	}
388b96ad4b2SPyun YongHyeon 	return (NULL);
389b96ad4b2SPyun YongHyeon }
390b96ad4b2SPyun YongHyeon 
391b96ad4b2SPyun YongHyeon /*
392b96ad4b2SPyun YongHyeon  * Return identification string if this device is ours.
393b96ad4b2SPyun YongHyeon  */
394b96ad4b2SPyun YongHyeon static int
395b96ad4b2SPyun YongHyeon fxp_probe(device_t dev)
396b96ad4b2SPyun YongHyeon {
397e0fe5c6dSMarius Strobl 	const struct fxp_ident *ident;
398b96ad4b2SPyun YongHyeon 
399b96ad4b2SPyun YongHyeon 	ident = fxp_find_ident(dev);
400b96ad4b2SPyun YongHyeon 	if (ident != NULL) {
401f7788e8eSJonathan Lemon 		device_set_desc(dev, ident->name);
402538565c4SWarner Losh 		return (BUS_PROBE_DEFAULT);
40355ce7b51SDavid Greenman 	}
404f7788e8eSJonathan Lemon 	return (ENXIO);
4056182fdbdSPeter Wemm }
4066182fdbdSPeter Wemm 
407b2badf02SMaxime Henrion static void
408b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
409b2badf02SMaxime Henrion {
41074d1ed23SMaxime Henrion 	uint32_t *addr;
411b2badf02SMaxime Henrion 
412b2badf02SMaxime Henrion 	if (error)
413b2badf02SMaxime Henrion 		return;
414b2badf02SMaxime Henrion 
415b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
416b2badf02SMaxime Henrion 	addr = arg;
417b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
418b2badf02SMaxime Henrion }
419b2badf02SMaxime Henrion 
4206182fdbdSPeter Wemm static int
4216182fdbdSPeter Wemm fxp_attach(device_t dev)
422a17c678eSDavid Greenman {
4236720ebccSMaxime Henrion 	struct fxp_softc *sc;
4246720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4256720ebccSMaxime Henrion 	struct fxp_tx *txp;
426b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4276720ebccSMaxime Henrion 	struct ifnet *ifp;
42874d1ed23SMaxime Henrion 	uint32_t val;
42974d1ed23SMaxime Henrion 	uint16_t data, myea[ETHER_ADDR_LEN / 2];
430fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4311845b5c3SMarius Strobl 	int error, flags, i, pmc, prefer_iomap;
432a17c678eSDavid Greenman 
4336720ebccSMaxime Henrion 	error = 0;
4346720ebccSMaxime Henrion 	sc = device_get_softc(dev);
435f7788e8eSJonathan Lemon 	sc->dev = dev;
4366008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4374953bccaSNate Lawson 	    MTX_DEF);
4383212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4394953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4404953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
441a17c678eSDavid Greenman 
4427ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4437ba33d82SBrooks Davis 	if (ifp == NULL) {
4447ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4457ba33d82SBrooks Davis 		error = ENOSPC;
4467ba33d82SBrooks Davis 		goto fail;
4477ba33d82SBrooks Davis 	}
4487ba33d82SBrooks Davis 
449dfe61cf1SDavid Greenman 	/*
4502bce79a2SMaxim Sobolev 	 * Enable bus mastering.
451df373873SWes Peters 	 */
452cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4539fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
45479495006SWarner Losh 
455df373873SWes Peters 	/*
4569fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4579fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4589fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
459dfe61cf1SDavid Greenman 	 */
4602a05a4ebSMatt Jacob 	prefer_iomap = 0;
46105bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
46205bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
46305bd8c22SMaxime Henrion 	if (prefer_iomap)
46405bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
46505bd8c22SMaxime Henrion 	else
46605bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4679fa6ccfbSMatt Jacob 
46805bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
46905bd8c22SMaxime Henrion 	if (error) {
47005bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
47105bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
47205bd8c22SMaxime Henrion 		else
47305bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
47405bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4759fa6ccfbSMatt Jacob 	}
47605bd8c22SMaxime Henrion 	if (error) {
47705bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4786182fdbdSPeter Wemm 		error = ENXIO;
479a17c678eSDavid Greenman 		goto fail;
480a17c678eSDavid Greenman 	}
48105bd8c22SMaxime Henrion 
4829fa6ccfbSMatt Jacob 	if (bootverbose) {
4839fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
48405bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4856182fdbdSPeter Wemm 	}
4866182fdbdSPeter Wemm 
487f7788e8eSJonathan Lemon 	/*
488a996f023SPyun YongHyeon 	 * Put CU/RU idle state and prepare full reset.
489f7788e8eSJonathan Lemon 	 */
490f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
491f7788e8eSJonathan Lemon 	DELAY(10);
492a996f023SPyun YongHyeon 	/* Full reset and disable interrupts. */
493a996f023SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
494a996f023SPyun YongHyeon 	DELAY(10);
495a996f023SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
496f7788e8eSJonathan Lemon 
497f7788e8eSJonathan Lemon 	/*
498f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
499f7788e8eSJonathan Lemon 	 */
500f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
501f7788e8eSJonathan Lemon 
502f7788e8eSJonathan Lemon 	/*
50393b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
50493b6e2e6SMaxime Henrion 	 */
505b96ad4b2SPyun YongHyeon 	sc->ident = fxp_find_ident(dev);
506b96ad4b2SPyun YongHyeon 	if (sc->ident->ich > 0) {
507b96ad4b2SPyun YongHyeon 		/* Assume ICH controllers are 82559. */
508b96ad4b2SPyun YongHyeon 		sc->revision = FXP_REV_82559_A0;
509b96ad4b2SPyun YongHyeon 	} else {
51093b6e2e6SMaxime Henrion 		fxp_read_eeprom(sc, &data, 5, 1);
51193b6e2e6SMaxime Henrion 		if ((data >> 8) == 1)
51293b6e2e6SMaxime Henrion 			sc->revision = FXP_REV_82557;
51393b6e2e6SMaxime Henrion 		else
51493b6e2e6SMaxime Henrion 			sc->revision = pci_get_revid(dev);
515b96ad4b2SPyun YongHyeon 	}
51693b6e2e6SMaxime Henrion 
51793b6e2e6SMaxime Henrion 	/*
5187137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
5197137cea0SPyun YongHyeon 	 */
5207137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
5217137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
5227137cea0SPyun YongHyeon 		fxp_read_eeprom(sc, &data, 10, 1);
5237137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
5243b0a4aefSJohn Baldwin 		    pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0)
5257137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
5267137cea0SPyun YongHyeon 	}
5277137cea0SPyun YongHyeon 
52843d8b117SPyun YongHyeon 	/* Receiver lock-up workaround detection. */
5296e854927SPyun YongHyeon 	if (sc->revision < FXP_REV_82558_A4) {
53043d8b117SPyun YongHyeon 		fxp_read_eeprom(sc, &data, 3, 1);
53143d8b117SPyun YongHyeon 		if ((data & 0x03) != 0x03) {
53243d8b117SPyun YongHyeon 			sc->flags |= FXP_FLAG_RXBUG;
53343d8b117SPyun YongHyeon 			device_printf(dev, "Enabling Rx lock-up workaround\n");
53443d8b117SPyun YongHyeon 		}
5356e854927SPyun YongHyeon 	}
53643d8b117SPyun YongHyeon 
5377137cea0SPyun YongHyeon 	/*
5383bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
539f7788e8eSJonathan Lemon 	 */
540f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
54193b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5424ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
543dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
544f7788e8eSJonathan Lemon 
5458da9c507SPyun YongHyeon 	fxp_sysctl_node(sc);
54672a32a26SJonathan Lemon 	/*
5472e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
54800c4116bSJonathan Lemon 	 *
54972a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
55072a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
55172a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
55200c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
55300c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
55400c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
55500c4116bSJonathan Lemon 	 *
55600c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5572e2b8238SJonathan Lemon 	 */
558b96ad4b2SPyun YongHyeon 	if ((sc->ident->ich >= 2 && sc->ident->ich <= 3) ||
559b96ad4b2SPyun YongHyeon 	    (sc->ident->ich == 0 && sc->revision >= FXP_REV_82559_A0)) {
56000c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
56100c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
56274d1ed23SMaxime Henrion 			uint16_t cksum;
56300c4116bSJonathan Lemon 			int i;
56400c4116bSJonathan Lemon 
56500c4116bSJonathan Lemon 			device_printf(dev,
566001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
56700c4116bSJonathan Lemon 			data &= ~0x02;
56800c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
56900c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
57000c4116bSJonathan Lemon 			cksum = 0;
57100c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
57200c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
57300c4116bSJonathan Lemon 				cksum += data;
57400c4116bSJonathan Lemon 			}
57500c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
57600c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
57700c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
57800c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
57900c4116bSJonathan Lemon 			device_printf(dev,
58000c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
58100c4116bSJonathan Lemon 			    i, data, cksum);
58200c4116bSJonathan Lemon #if 1
58300c4116bSJonathan Lemon 			/*
58400c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
58500c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
58600c4116bSJonathan Lemon 			 */
5872e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
58800c4116bSJonathan Lemon #endif
58900c4116bSJonathan Lemon 		}
59000c4116bSJonathan Lemon 	}
5912e2b8238SJonathan Lemon 
5922e2b8238SJonathan Lemon 	/*
5933bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5943bd07cfdSJonathan Lemon 	 */
59572a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5963bd07cfdSJonathan Lemon 		/*
59774396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
59874396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
59974396a0aSJonathan Lemon 		 * the board to turn on MWI.
6003bd07cfdSJonathan Lemon 		 */
60174396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
60274396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
6033bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
6043bd07cfdSJonathan Lemon 
6053bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
6063bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
60744e0bc11SYaroslav Tykhiy 
60844e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
60944e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
61044e0bc11SYaroslav Tykhiy 	} else {
61144e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
61244e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6133bd07cfdSJonathan Lemon 	}
6143bd07cfdSJonathan Lemon 
615f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
616829b278eSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0) {
617829b278eSPyun YongHyeon 		/* 82559ER does not support Rx checksum offloading. */
618829b278eSPyun YongHyeon 		if (sc->ident->devid != 0x1209)
619f13075afSPyun YongHyeon 			sc->flags |= FXP_FLAG_82559_RXCSUM;
620829b278eSPyun YongHyeon 	}
6213bd07cfdSJonathan Lemon 	/*
622c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
623c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
624c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
625c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
626c8bca6dcSBill Paul 	 */
627507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
628507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
629507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
630c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
631c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
632c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
633f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
634f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
635c8bca6dcSBill Paul 	} else {
636c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
637c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
638c8bca6dcSBill Paul 	}
639c8bca6dcSBill Paul 
640c8bca6dcSBill Paul 	/*
641b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
642b2badf02SMaxime Henrion 	 */
64340c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
644c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
645c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
64640c20505SMaxime Henrion 		sc->maxtxseg--;
647c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
648c21e84e4SPyun YongHyeon 	}
649c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
650c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
651c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
652c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
653a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_txmtag);
654b2badf02SMaxime Henrion 	if (error) {
655a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TX DMA tag\n");
656a2057a72SPyun YongHyeon 		goto fail;
657a2057a72SPyun YongHyeon 	}
658a2057a72SPyun YongHyeon 
659a2057a72SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
660a2057a72SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
661a2057a72SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0,
662a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_rxmtag);
663a2057a72SPyun YongHyeon 	if (error) {
664a2057a72SPyun YongHyeon 		device_printf(dev, "could not create RX DMA tag\n");
665b2badf02SMaxime Henrion 		goto fail;
666b2badf02SMaxime Henrion 	}
667b2badf02SMaxime Henrion 
668c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
669c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
670c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
671c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
672b2badf02SMaxime Henrion 	if (error) {
673a2057a72SPyun YongHyeon 		device_printf(dev, "could not create stats DMA tag\n");
674b2badf02SMaxime Henrion 		goto fail;
675b2badf02SMaxime Henrion 	}
676b2badf02SMaxime Henrion 
677b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
678658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->fxp_smap);
679a2057a72SPyun YongHyeon 	if (error) {
680a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate stats DMA memory\n");
6814953bccaSNate Lawson 		goto fail;
682a2057a72SPyun YongHyeon 	}
683b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
684b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
685b2badf02SMaxime Henrion 	if (error) {
686a2057a72SPyun YongHyeon 		device_printf(dev, "could not load the stats DMA buffer\n");
687b2badf02SMaxime Henrion 		goto fail;
688b2badf02SMaxime Henrion 	}
689b2badf02SMaxime Henrion 
690c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
691c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
692c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
693c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
694b2badf02SMaxime Henrion 	if (error) {
695a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TxCB DMA tag\n");
696b2badf02SMaxime Henrion 		goto fail;
697b2badf02SMaxime Henrion 	}
698b2badf02SMaxime Henrion 
699b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
700658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->cbl_map);
701a2057a72SPyun YongHyeon 	if (error) {
702a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate TxCB DMA memory\n");
7034953bccaSNate Lawson 		goto fail;
704a2057a72SPyun YongHyeon 	}
705b2badf02SMaxime Henrion 
706b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
707b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
708b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
709b2badf02SMaxime Henrion 	if (error) {
710a2057a72SPyun YongHyeon 		device_printf(dev, "could not load TxCB DMA buffer\n");
711b2badf02SMaxime Henrion 		goto fail;
712b2badf02SMaxime Henrion 	}
713b2badf02SMaxime Henrion 
714c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
715c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
716c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
717c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
718b2badf02SMaxime Henrion 	if (error) {
719a2057a72SPyun YongHyeon 		device_printf(dev,
720a2057a72SPyun YongHyeon 		    "could not create multicast setup DMA tag\n");
721b2badf02SMaxime Henrion 		goto fail;
722b2badf02SMaxime Henrion 	}
723b2badf02SMaxime Henrion 
724b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
725658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->mcs_map);
726a2057a72SPyun YongHyeon 	if (error) {
727a2057a72SPyun YongHyeon 		device_printf(dev,
728a2057a72SPyun YongHyeon 		    "could not allocate multicast setup DMA memory\n");
7294953bccaSNate Lawson 		goto fail;
730a2057a72SPyun YongHyeon 	}
731b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
732b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
733b2badf02SMaxime Henrion 	if (error) {
734a2057a72SPyun YongHyeon 		device_printf(dev,
735a2057a72SPyun YongHyeon 		    "can't load the multicast setup DMA buffer\n");
736b2badf02SMaxime Henrion 		goto fail;
737b2badf02SMaxime Henrion 	}
738b2badf02SMaxime Henrion 
739b2badf02SMaxime Henrion 	/*
7406720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7416720ebccSMaxime Henrion 	 * the TX command blocks.
742b2badf02SMaxime Henrion 	 */
7436720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7446720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7454cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7466720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
747a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_txmtag, 0, &txp[i].tx_map);
748b2badf02SMaxime Henrion 		if (error) {
749b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
750b2badf02SMaxime Henrion 			goto fail;
751b2badf02SMaxime Henrion 		}
752b2badf02SMaxime Henrion 	}
753a2057a72SPyun YongHyeon 	error = bus_dmamap_create(sc->fxp_rxmtag, 0, &sc->spare_map);
754b2badf02SMaxime Henrion 	if (error) {
755b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
756b2badf02SMaxime Henrion 		goto fail;
757b2badf02SMaxime Henrion 	}
758b2badf02SMaxime Henrion 
759b2badf02SMaxime Henrion 	/*
760b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
761b2badf02SMaxime Henrion 	 */
762b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
763b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
764b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
765a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_rxmtag, 0, &rxp->rx_map);
766b2badf02SMaxime Henrion 		if (error) {
767b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
768b2badf02SMaxime Henrion 			goto fail;
769b2badf02SMaxime Henrion 		}
77085050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7714953bccaSNate Lawson 			error = ENOMEM;
7724953bccaSNate Lawson 			goto fail;
7734953bccaSNate Lawson 		}
77485050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
775b2badf02SMaxime Henrion 	}
776b2badf02SMaxime Henrion 
777b2badf02SMaxime Henrion 	/*
778f7788e8eSJonathan Lemon 	 * Read MAC address.
779f7788e8eSJonathan Lemon 	 */
78083e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
781fc74a9f9SBrooks Davis 	eaddr[0] = myea[0] & 0xff;
782fc74a9f9SBrooks Davis 	eaddr[1] = myea[0] >> 8;
783fc74a9f9SBrooks Davis 	eaddr[2] = myea[1] & 0xff;
784fc74a9f9SBrooks Davis 	eaddr[3] = myea[1] >> 8;
785fc74a9f9SBrooks Davis 	eaddr[4] = myea[2] & 0xff;
786fc74a9f9SBrooks Davis 	eaddr[5] = myea[2] >> 8;
787f7788e8eSJonathan Lemon 	if (bootverbose) {
7882e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
789f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7902e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7912e2b8238SJonathan Lemon 		    pci_get_revid(dev));
79272a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
79372a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
79472a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
795f7788e8eSJonathan Lemon 	}
796f7788e8eSJonathan Lemon 
797f7788e8eSJonathan Lemon 	/*
798f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
799f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
800f7788e8eSJonathan Lemon 	 *
801f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
802f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
803f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
804f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
805f7788e8eSJonathan Lemon 	 */
806f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
807f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
808f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
809f7788e8eSJonathan Lemon 	} else {
8108e5d93dbSMarius Strobl 		/*
8118e5d93dbSMarius Strobl 		 * i82557 wedge when isolating all of their PHYs.
8128e5d93dbSMarius Strobl 		 */
8131845b5c3SMarius Strobl 		flags = MIIF_NOISOLATE;
8141845b5c3SMarius Strobl 		if (sc->revision >= FXP_REV_82558_A4)
8151845b5c3SMarius Strobl 			flags |= MIIF_DOPAUSE;
8168e5d93dbSMarius Strobl 		error = mii_attach(dev, &sc->miibus, ifp, fxp_ifmedia_upd,
8178e5d93dbSMarius Strobl 		    fxp_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
8181845b5c3SMarius Strobl 		    MII_OFFSET_ANY, flags);
8198e5d93dbSMarius Strobl 		if (error != 0) {
8208e5d93dbSMarius Strobl 			device_printf(dev, "attaching PHYs failed\n");
821ba8c6fd5SDavid Greenman 			goto fail;
822a17c678eSDavid Greenman 		}
823f7788e8eSJonathan Lemon 	}
824dccee1a1SDavid Greenman 
8259bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
826fb583156SDavid Greenman 	ifp->if_init = fxp_init;
827ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
828ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
829ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
830ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
831a17c678eSDavid Greenman 
8325fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
8335fe9116bSYaroslav Tykhiy 
834c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
835c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
836c21e84e4SPyun YongHyeon 		ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO;
837c21e84e4SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4;
838c21e84e4SPyun YongHyeon 		ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4;
839c8bca6dcSBill Paul 	}
840c8bca6dcSBill Paul 
841f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
842f13075afSPyun YongHyeon 		ifp->if_capabilities |= IFCAP_RXCSUM;
843f13075afSPyun YongHyeon 		ifp->if_capenable |= IFCAP_RXCSUM;
844f13075afSPyun YongHyeon 	}
845f13075afSPyun YongHyeon 
8467137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
8477137cea0SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL_MAGIC;
8487137cea0SPyun YongHyeon 		ifp->if_capenable |= IFCAP_WOL_MAGIC;
8497137cea0SPyun YongHyeon 	}
8507137cea0SPyun YongHyeon 
851fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
852fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
853fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
854fb917226SRuslan Ermilov #endif
855fb917226SRuslan Ermilov 
856dfe61cf1SDavid Greenman 	/*
8574953bccaSNate Lawson 	 * Attach the interface.
8584953bccaSNate Lawson 	 */
859fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
8604953bccaSNate Lawson 
8614953bccaSNate Lawson 	/*
862e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
8635fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
8645fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
865e8c8b728SJonathan Lemon 	 */
866e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
867673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
86844e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
869bd4fa9d9SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) {
870bd4fa9d9SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING |
871713ca255SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
872bd4fa9d9SPyun YongHyeon 		ifp->if_capenable |= IFCAP_VLAN_HWTAGGING |
873713ca255SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
874bd4fa9d9SPyun YongHyeon 	}
875e8c8b728SJonathan Lemon 
876483b9871SDavid Greenman 	/*
8773114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8783114fdb4SDavid Greenman 	 * TX descriptors.
879483b9871SDavid Greenman 	 */
8807929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8817929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8827929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8834a684684SDavid Greenman 
884201afb0eSMaxime Henrion 	/*
8854953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
886201afb0eSMaxime Henrion 	 */
88705bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
888ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
889201afb0eSMaxime Henrion 	if (error) {
890201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
891fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
892201afb0eSMaxime Henrion 		goto fail;
893201afb0eSMaxime Henrion 	}
894201afb0eSMaxime Henrion 
8957137cea0SPyun YongHyeon 	/*
8967137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
8977137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
8987137cea0SPyun YongHyeon 	 */
8997137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
9007137cea0SPyun YongHyeon 		FXP_LOCK(sc);
9017137cea0SPyun YongHyeon 		/* Clear wakeup events. */
902af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
9031845b5c3SMarius Strobl 		fxp_init_body(sc, 1);
9047137cea0SPyun YongHyeon 		fxp_stop(sc);
9057137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
9067137cea0SPyun YongHyeon 	}
9077137cea0SPyun YongHyeon 
908a17c678eSDavid Greenman fail:
9091b5a39d3SBrooks Davis 	if (error)
910f7788e8eSJonathan Lemon 		fxp_release(sc);
911f7788e8eSJonathan Lemon 	return (error);
912f7788e8eSJonathan Lemon }
913f7788e8eSJonathan Lemon 
914f7788e8eSJonathan Lemon /*
9154953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
9164953bccaSNate Lawson  * interrupt should already be torn down.
917f7788e8eSJonathan Lemon  */
918f7788e8eSJonathan Lemon static void
919f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
920f7788e8eSJonathan Lemon {
921b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
922b2badf02SMaxime Henrion 	struct fxp_tx *txp;
923b2badf02SMaxime Henrion 	int i;
924b2badf02SMaxime Henrion 
92567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
926670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
927670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
9284953bccaSNate Lawson 	if (sc->miibus)
9294953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
9304953bccaSNate Lawson 	bus_generic_detach(sc->dev);
9314953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
932b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
933b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
934b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
935b2badf02SMaxime Henrion 		    sc->cbl_map);
936b2badf02SMaxime Henrion 	}
937b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
938b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
939b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
940b2badf02SMaxime Henrion 	}
941b2badf02SMaxime Henrion 	if (sc->mcsp) {
942b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
943b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
944b2badf02SMaxime Henrion 	}
94505bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
946a2057a72SPyun YongHyeon 	if (sc->fxp_rxmtag) {
947b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
948b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
949b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
950a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
951b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
952a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
953b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
954b983c7b3SMaxime Henrion 			}
955a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_rxmtag, rxp->rx_map);
956b983c7b3SMaxime Henrion 		}
957a2057a72SPyun YongHyeon 		bus_dmamap_destroy(sc->fxp_rxmtag, sc->spare_map);
958a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_rxmtag);
959a2057a72SPyun YongHyeon 	}
960a2057a72SPyun YongHyeon 	if (sc->fxp_txmtag) {
961b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
962b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
963b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
964a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
965b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
966a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
967b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
968b983c7b3SMaxime Henrion 			}
969a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_txmtag, txp->tx_map);
970b983c7b3SMaxime Henrion 		}
971a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_txmtag);
972b983c7b3SMaxime Henrion 	}
973c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
974c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
975b2badf02SMaxime Henrion 	if (sc->cbl_tag)
976b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
977b2badf02SMaxime Henrion 	if (sc->mcs_tag)
978b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
979fc74a9f9SBrooks Davis 	if (sc->ifp)
980fc74a9f9SBrooks Davis 		if_free(sc->ifp);
98172a32a26SJonathan Lemon 
9820f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
9836182fdbdSPeter Wemm }
9846182fdbdSPeter Wemm 
9856182fdbdSPeter Wemm /*
9866182fdbdSPeter Wemm  * Detach interface.
9876182fdbdSPeter Wemm  */
9886182fdbdSPeter Wemm static int
9896182fdbdSPeter Wemm fxp_detach(device_t dev)
9906182fdbdSPeter Wemm {
9916182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
9926182fdbdSPeter Wemm 
99340929967SGleb Smirnoff #ifdef DEVICE_POLLING
99440929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
99540929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
99640929967SGleb Smirnoff #endif
99740929967SGleb Smirnoff 
9984953bccaSNate Lawson 	FXP_LOCK(sc);
9996182fdbdSPeter Wemm 	/*
100032cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
100120f0c80fSMaxime Henrion 	 */
100220f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
100320f0c80fSMaxime Henrion 	fxp_stop(sc);
100432cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
10059eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
100620f0c80fSMaxime Henrion 
10076182fdbdSPeter Wemm 	/*
10083212724cSJohn Baldwin 	 * Close down routes etc.
10093212724cSJohn Baldwin 	 */
10103212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
10113212724cSJohn Baldwin 
10123212724cSJohn Baldwin 	/*
10134953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
10144953bccaSNate Lawson 	 * races with fxp_intr().
10156182fdbdSPeter Wemm 	 */
101605bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
10174953bccaSNate Lawson 	sc->ih = NULL;
10186182fdbdSPeter Wemm 
1019f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
1020f7788e8eSJonathan Lemon 	fxp_release(sc);
1021f7788e8eSJonathan Lemon 	return (0);
1022a17c678eSDavid Greenman }
1023a17c678eSDavid Greenman 
1024a17c678eSDavid Greenman /*
10254a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
1026a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
1027a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
1028a17c678eSDavid Greenman  */
10296182fdbdSPeter Wemm static int
10306182fdbdSPeter Wemm fxp_shutdown(device_t dev)
1031a17c678eSDavid Greenman {
10323212724cSJohn Baldwin 
10336182fdbdSPeter Wemm 	/*
10346182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
10356182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
10366182fdbdSPeter Wemm 	 * reboot before the driver initializes.
10376182fdbdSPeter Wemm 	 */
10387137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
1039a17c678eSDavid Greenman }
1040a17c678eSDavid Greenman 
10417dced78aSDavid Greenman /*
10427dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
10437dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10447dced78aSDavid Greenman  * resume.
10457dced78aSDavid Greenman  */
10467dced78aSDavid Greenman static int
10477dced78aSDavid Greenman fxp_suspend(device_t dev)
10487dced78aSDavid Greenman {
10497dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
10507137cea0SPyun YongHyeon 	struct ifnet *ifp;
10517137cea0SPyun YongHyeon 	int pmc;
10527137cea0SPyun YongHyeon 	uint16_t pmstat;
10537dced78aSDavid Greenman 
10544953bccaSNate Lawson 	FXP_LOCK(sc);
10557dced78aSDavid Greenman 
10567137cea0SPyun YongHyeon 	ifp = sc->ifp;
10573b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
10587137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10597137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
10607137cea0SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
10617137cea0SPyun YongHyeon 			/* Request PME. */
10627137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10637137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10647137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
10651845b5c3SMarius Strobl 			fxp_init_body(sc, 1);
10667137cea0SPyun YongHyeon 		}
10677137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
10687137cea0SPyun YongHyeon 	}
10697dced78aSDavid Greenman 	fxp_stop(sc);
10707dced78aSDavid Greenman 
10717dced78aSDavid Greenman 	sc->suspended = 1;
10727dced78aSDavid Greenman 
10734953bccaSNate Lawson 	FXP_UNLOCK(sc);
1074f7788e8eSJonathan Lemon 	return (0);
10757dced78aSDavid Greenman }
10767dced78aSDavid Greenman 
10777dced78aSDavid Greenman /*
107867ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
10797dced78aSDavid Greenman  * appropriate.
10807dced78aSDavid Greenman  */
10817dced78aSDavid Greenman static int
10827dced78aSDavid Greenman fxp_resume(device_t dev)
10837dced78aSDavid Greenman {
10847dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
1085fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
10867137cea0SPyun YongHyeon 	int pmc;
10877137cea0SPyun YongHyeon 	uint16_t pmstat;
10887dced78aSDavid Greenman 
10894953bccaSNate Lawson 	FXP_LOCK(sc);
10907dced78aSDavid Greenman 
10913b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
10927137cea0SPyun YongHyeon 		sc->flags &= ~FXP_FLAG_WOL;
10937137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10947137cea0SPyun YongHyeon 		/* Disable PME and clear PME status. */
10957137cea0SPyun YongHyeon 		pmstat &= ~PCIM_PSTAT_PMEENABLE;
10967137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1097af75b654SPyun YongHyeon 		if ((sc->flags & FXP_FLAG_WOLCAP) != 0)
1098af75b654SPyun YongHyeon 			CSR_WRITE_1(sc, FXP_CSR_PMDR,
1099af75b654SPyun YongHyeon 			    CSR_READ_1(sc, FXP_CSR_PMDR));
11007137cea0SPyun YongHyeon 	}
11017137cea0SPyun YongHyeon 
11027dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
11037dced78aSDavid Greenman 	DELAY(10);
11047dced78aSDavid Greenman 
11057dced78aSDavid Greenman 	/* reinitialize interface if necessary */
11067dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
11071845b5c3SMarius Strobl 		fxp_init_body(sc, 1);
11087dced78aSDavid Greenman 
11097dced78aSDavid Greenman 	sc->suspended = 0;
11107dced78aSDavid Greenman 
11114953bccaSNate Lawson 	FXP_UNLOCK(sc);
1112ba8c6fd5SDavid Greenman 	return (0);
1113f7788e8eSJonathan Lemon }
1114ba8c6fd5SDavid Greenman 
111500c4116bSJonathan Lemon static void
111600c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
111700c4116bSJonathan Lemon {
111874d1ed23SMaxime Henrion 	uint16_t reg;
111900c4116bSJonathan Lemon 	int x;
112000c4116bSJonathan Lemon 
112100c4116bSJonathan Lemon 	/*
112200c4116bSJonathan Lemon 	 * Shift in data.
112300c4116bSJonathan Lemon 	 */
112400c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
112500c4116bSJonathan Lemon 		if (data & x)
112600c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
112700c4116bSJonathan Lemon 		else
112800c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
112900c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
113000c4116bSJonathan Lemon 		DELAY(1);
113100c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
113200c4116bSJonathan Lemon 		DELAY(1);
113300c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
113400c4116bSJonathan Lemon 		DELAY(1);
113500c4116bSJonathan Lemon 	}
113600c4116bSJonathan Lemon }
113700c4116bSJonathan Lemon 
1138f7788e8eSJonathan Lemon /*
1139f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1140f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1141f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1142f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1143f7788e8eSJonathan Lemon  * every 16 bits of data.
1144f7788e8eSJonathan Lemon  */
114574d1ed23SMaxime Henrion static uint16_t
1146f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1147f7788e8eSJonathan Lemon {
114874d1ed23SMaxime Henrion 	uint16_t reg, data;
1149f7788e8eSJonathan Lemon 	int x;
1150ba8c6fd5SDavid Greenman 
1151f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1152f7788e8eSJonathan Lemon 	/*
1153f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1154f7788e8eSJonathan Lemon 	 */
115500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1156f7788e8eSJonathan Lemon 	/*
1157f7788e8eSJonathan Lemon 	 * Shift in address.
1158f7788e8eSJonathan Lemon 	 */
1159f7788e8eSJonathan Lemon 	data = 0;
1160f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1161f7788e8eSJonathan Lemon 		if (offset & x)
1162f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1163f7788e8eSJonathan Lemon 		else
1164f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1165f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1166f7788e8eSJonathan Lemon 		DELAY(1);
1167f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1168f7788e8eSJonathan Lemon 		DELAY(1);
1169f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1170f7788e8eSJonathan Lemon 		DELAY(1);
1171f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1172f7788e8eSJonathan Lemon 		data++;
1173f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1174f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1175f7788e8eSJonathan Lemon 			break;
1176f7788e8eSJonathan Lemon 		}
1177f7788e8eSJonathan Lemon 	}
1178f7788e8eSJonathan Lemon 	/*
1179f7788e8eSJonathan Lemon 	 * Shift out data.
1180f7788e8eSJonathan Lemon 	 */
1181f7788e8eSJonathan Lemon 	data = 0;
1182f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1183f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1184f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1185f7788e8eSJonathan Lemon 		DELAY(1);
1186f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1187f7788e8eSJonathan Lemon 			data |= x;
1188f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1189f7788e8eSJonathan Lemon 		DELAY(1);
1190f7788e8eSJonathan Lemon 	}
1191f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1192f7788e8eSJonathan Lemon 	DELAY(1);
1193f7788e8eSJonathan Lemon 
1194f7788e8eSJonathan Lemon 	return (data);
1195ba8c6fd5SDavid Greenman }
1196ba8c6fd5SDavid Greenman 
119700c4116bSJonathan Lemon static void
119874d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
119900c4116bSJonathan Lemon {
120000c4116bSJonathan Lemon 	int i;
120100c4116bSJonathan Lemon 
120200c4116bSJonathan Lemon 	/*
120300c4116bSJonathan Lemon 	 * Erase/write enable.
120400c4116bSJonathan Lemon 	 */
120500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
120600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
120700c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
120800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
120900c4116bSJonathan Lemon 	DELAY(1);
121000c4116bSJonathan Lemon 	/*
121100c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
121200c4116bSJonathan Lemon 	 */
121300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
121400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
121500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
121600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
121700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
121800c4116bSJonathan Lemon 	DELAY(1);
121900c4116bSJonathan Lemon 	/*
122000c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
122100c4116bSJonathan Lemon 	 */
122200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
122300c4116bSJonathan Lemon 	DELAY(1);
122400c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
122500c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
122600c4116bSJonathan Lemon 			break;
122700c4116bSJonathan Lemon 		DELAY(50);
122800c4116bSJonathan Lemon 	}
122900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
123000c4116bSJonathan Lemon 	DELAY(1);
123100c4116bSJonathan Lemon 	/*
123200c4116bSJonathan Lemon 	 * Erase/write disable.
123300c4116bSJonathan Lemon 	 */
123400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
123500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
123600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
123700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
123800c4116bSJonathan Lemon 	DELAY(1);
123900c4116bSJonathan Lemon }
124000c4116bSJonathan Lemon 
1241ba8c6fd5SDavid Greenman /*
1242e9bf2fa7SDavid Greenman  * From NetBSD:
1243e9bf2fa7SDavid Greenman  *
1244e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1245e9bf2fa7SDavid Greenman  *
1246e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1247e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1248e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1249e9bf2fa7SDavid Greenman  *
1250e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1251e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1252e9bf2fa7SDavid Greenman  *
1253e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1254e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1255e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1256e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1257e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1258e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1259e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1260e9bf2fa7SDavid Greenman  */
1261e9bf2fa7SDavid Greenman static void
1262f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1263e9bf2fa7SDavid Greenman {
1264e9bf2fa7SDavid Greenman 
1265f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1266f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1267f7788e8eSJonathan Lemon 
1268f7788e8eSJonathan Lemon 	/* autosize */
1269f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1270e9bf2fa7SDavid Greenman }
1271f7788e8eSJonathan Lemon 
1272ba8c6fd5SDavid Greenman static void
1273f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1274ba8c6fd5SDavid Greenman {
1275f7788e8eSJonathan Lemon 	int i;
1276ba8c6fd5SDavid Greenman 
1277f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1278f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1279ba8c6fd5SDavid Greenman }
1280ba8c6fd5SDavid Greenman 
128100c4116bSJonathan Lemon static void
128200c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
128300c4116bSJonathan Lemon {
128400c4116bSJonathan Lemon 	int i;
128500c4116bSJonathan Lemon 
128600c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
128700c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
128800c4116bSJonathan Lemon }
128900c4116bSJonathan Lemon 
1290a17c678eSDavid Greenman /*
12914953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1292a17c678eSDavid Greenman  */
1293a17c678eSDavid Greenman static void
1294f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1295a17c678eSDavid Greenman {
12969b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
12974953bccaSNate Lawson 
12984953bccaSNate Lawson 	FXP_LOCK(sc);
12994953bccaSNate Lawson 	fxp_start_body(ifp);
13004953bccaSNate Lawson 	FXP_UNLOCK(sc);
13014953bccaSNate Lawson }
13024953bccaSNate Lawson 
13034953bccaSNate Lawson /*
13044953bccaSNate Lawson  * Start packet transmission on the interface.
13054953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
13064953bccaSNate Lawson  * internal entry point only.
13074953bccaSNate Lawson  */
13084953bccaSNate Lawson static void
13094953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
13104953bccaSNate Lawson {
13114953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1312b2badf02SMaxime Henrion 	struct mbuf *mb_head;
13134e53f837SPyun YongHyeon 	int txqueued;
1314a17c678eSDavid Greenman 
131567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
131640c20505SMaxime Henrion 
1317c109e385SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1318c109e385SPyun YongHyeon 	    IFF_DRV_RUNNING)
1319c109e385SPyun YongHyeon 		return;
1320c109e385SPyun YongHyeon 
13214e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
13224e53f837SPyun YongHyeon 		fxp_txeof(sc);
1323483b9871SDavid Greenman 	/*
1324483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1325483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
13263114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
13273114fdb4SDavid Greenman 	 *       a NOP command when needed.
1328483b9871SDavid Greenman 	 */
132940c20505SMaxime Henrion 	txqueued = 0;
13307929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
13317929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1332483b9871SDavid Greenman 
1333dfe61cf1SDavid Greenman 		/*
1334dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1335dfe61cf1SDavid Greenman 		 */
13367929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
13377929aa03SMax Laier 		if (mb_head == NULL)
13387929aa03SMax Laier 			break;
1339a17c678eSDavid Greenman 
13404e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13414e53f837SPyun YongHyeon 			if (mb_head == NULL)
134240c20505SMaxime Henrion 				break;
13434e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
13444e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
134540c20505SMaxime Henrion 		}
13464e53f837SPyun YongHyeon 		txqueued++;
13474e53f837SPyun YongHyeon 		/*
13484e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13494e53f837SPyun YongHyeon 		 */
13504e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
13514e53f837SPyun YongHyeon 	}
135240c20505SMaxime Henrion 
135340c20505SMaxime Henrion 	/*
135440c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
135540c20505SMaxime Henrion 	 * going again if suspended.
135640c20505SMaxime Henrion 	 */
13574e53f837SPyun YongHyeon 	if (txqueued > 0) {
1358a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1359a2057a72SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
136040c20505SMaxime Henrion 		fxp_scb_wait(sc);
136140c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13624e53f837SPyun YongHyeon 		/*
13634e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
13644e53f837SPyun YongHyeon 		 * from the card again.
13654e53f837SPyun YongHyeon 		 */
13664e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
136740c20505SMaxime Henrion 	}
136840c20505SMaxime Henrion }
136940c20505SMaxime Henrion 
137040c20505SMaxime Henrion static int
13714e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
137240c20505SMaxime Henrion {
137340c20505SMaxime Henrion 	struct ifnet *ifp;
137440c20505SMaxime Henrion 	struct mbuf *m;
137540c20505SMaxime Henrion 	struct fxp_tx *txp;
137640c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1377c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
137840c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1379c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
138040c20505SMaxime Henrion 
138140c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1382fc74a9f9SBrooks Davis 	ifp = sc->ifp;
138340c20505SMaxime Henrion 
1384c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1385c21e84e4SPyun YongHyeon 	tcp = NULL;
1386dfe61cf1SDavid Greenman 	/*
1387483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1388dfe61cf1SDavid Greenman 	 */
1389b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1390c8bca6dcSBill Paul 
1391c8bca6dcSBill Paul 	/*
1392a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1393a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1394a35e7eaaSDon Lewis 	 * Developer Manual says:
1395a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1396a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1397a35e7eaaSDon Lewis 	 *   ...
1398a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1399a35e7eaaSDon Lewis 	 *   be used.
1400a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1401a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1402a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1403a35e7eaaSDon Lewis 	 */
1404a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1405a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1406a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1407a35e7eaaSDon Lewis 
14084e53f837SPyun YongHyeon 	m = *m_head;
1409c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1410c21e84e4SPyun YongHyeon 		/*
1411c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1412c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1413c21e84e4SPyun YongHyeon 		 */
1414c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1415c21e84e4SPyun YongHyeon 		struct ip *ip;
1416c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1417c21e84e4SPyun YongHyeon 
1418c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1419c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1420c21e84e4SPyun YongHyeon 			m = m_dup(*m_head, M_DONTWAIT);
1421c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1422c21e84e4SPyun YongHyeon 			if (m == NULL) {
1423c21e84e4SPyun YongHyeon 				*m_head = NULL;
1424c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1425c21e84e4SPyun YongHyeon 			}
1426c21e84e4SPyun YongHyeon 			*m_head = m;
1427c21e84e4SPyun YongHyeon 		}
1428c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1429c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1430c21e84e4SPyun YongHyeon 		if (m == NULL) {
1431c21e84e4SPyun YongHyeon 			*m_head = NULL;
1432c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1433c21e84e4SPyun YongHyeon 		}
1434c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1435c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1436c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1437c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1438c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1439c21e84e4SPyun YongHyeon 			if (m == NULL) {
1440c21e84e4SPyun YongHyeon 				*m_head = NULL;
1441c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1442c21e84e4SPyun YongHyeon 			}
1443c21e84e4SPyun YongHyeon 		}
1444c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1445c21e84e4SPyun YongHyeon 		if (m == NULL) {
1446c21e84e4SPyun YongHyeon 			*m_head = NULL;
1447c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1448c21e84e4SPyun YongHyeon 		}
1449c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1450c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1451c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1452c21e84e4SPyun YongHyeon 		if (m == NULL) {
1453c21e84e4SPyun YongHyeon 			*m_head = NULL;
1454c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1455c21e84e4SPyun YongHyeon 		}
1456c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1457c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr) + tcp->th_off);
1458c21e84e4SPyun YongHyeon 		if (m == NULL) {
1459c21e84e4SPyun YongHyeon 			*m_head = NULL;
1460c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1461c21e84e4SPyun YongHyeon 		}
1462c21e84e4SPyun YongHyeon 
1463c21e84e4SPyun YongHyeon 		/*
1464c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1465c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1466c21e84e4SPyun YongHyeon 		 */
146796486faaSPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
146896486faaSPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1469c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
14700685c824SPyun YongHyeon 		ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) +
14710685c824SPyun YongHyeon 		    (tcp->th_off << 2));
1472c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1473c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1474c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1475c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1476c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1477c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1478c21e84e4SPyun YongHyeon 		*m_head = m;
14796da6d0a9SPyun YongHyeon 	} else if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
14806da6d0a9SPyun YongHyeon 		/*
14816da6d0a9SPyun YongHyeon 		 * Deal with TCP/IP checksum offload. Note that
14826da6d0a9SPyun YongHyeon 		 * in order for TCP checksum offload to work,
14836da6d0a9SPyun YongHyeon 		 * the pseudo header checksum must have already
14846da6d0a9SPyun YongHyeon 		 * been computed and stored in the checksum field
14856da6d0a9SPyun YongHyeon 		 * in the TCP header. The stack should have
14866da6d0a9SPyun YongHyeon 		 * already done this for us.
14876da6d0a9SPyun YongHyeon 		 */
14886da6d0a9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
14896da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
14906da6d0a9SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
14916da6d0a9SPyun YongHyeon 
14926da6d0a9SPyun YongHyeon #ifdef FXP_IP_CSUM_WAR
14936da6d0a9SPyun YongHyeon 		/*
14946da6d0a9SPyun YongHyeon 		 * XXX The 82550 chip appears to have trouble
14956da6d0a9SPyun YongHyeon 		 * dealing with IP header checksums in very small
14966da6d0a9SPyun YongHyeon 		 * datagrams, namely fragments from 1 to 3 bytes
14976da6d0a9SPyun YongHyeon 		 * in size. For example, say you want to transmit
14986da6d0a9SPyun YongHyeon 		 * a UDP packet of 1473 bytes. The packet will be
14996da6d0a9SPyun YongHyeon 		 * fragmented over two IP datagrams, the latter
15006da6d0a9SPyun YongHyeon 		 * containing only one byte of data. The 82550 will
15016da6d0a9SPyun YongHyeon 		 * botch the header checksum on the 1-byte fragment.
15026da6d0a9SPyun YongHyeon 		 * As long as the datagram contains 4 or more bytes
15036da6d0a9SPyun YongHyeon 		 * of data, you're ok.
15046da6d0a9SPyun YongHyeon 		 *
15056da6d0a9SPyun YongHyeon                  * The following code attempts to work around this
15066da6d0a9SPyun YongHyeon 		 * problem: if the datagram is less than 38 bytes
15076da6d0a9SPyun YongHyeon 		 * in size (14 bytes ether header, 20 bytes IP header,
15086da6d0a9SPyun YongHyeon 		 * plus 4 bytes of data), we punt and compute the IP
15096da6d0a9SPyun YongHyeon 		 * header checksum by hand. This workaround doesn't
15106da6d0a9SPyun YongHyeon 		 * work very well, however, since it can be fooled
15116da6d0a9SPyun YongHyeon 		 * by things like VLAN tags and IP options that make
15126da6d0a9SPyun YongHyeon 		 * the header sizes/offsets vary.
15136da6d0a9SPyun YongHyeon 		 */
15146da6d0a9SPyun YongHyeon 
15156da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
15166da6d0a9SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
15176da6d0a9SPyun YongHyeon 				struct ip *ip;
15186da6d0a9SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
15196da6d0a9SPyun YongHyeon 				ip = mtod(m, struct ip *);
15206da6d0a9SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
15216da6d0a9SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
15226da6d0a9SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
15236da6d0a9SPyun YongHyeon 			} else {
15246da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_activation_high =
15256da6d0a9SPyun YongHyeon 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
15266da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_schedule |=
15276da6d0a9SPyun YongHyeon 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
15286da6d0a9SPyun YongHyeon 			}
15296da6d0a9SPyun YongHyeon 		}
15306da6d0a9SPyun YongHyeon #endif
1531c21e84e4SPyun YongHyeon 	}
1532c21e84e4SPyun YongHyeon 
1533a2057a72SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, *m_head,
15344e53f837SPyun YongHyeon 	    segs, &nseg, 0);
15354e53f837SPyun YongHyeon 	if (error == EFBIG) {
15364e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
15374e53f837SPyun YongHyeon 		if (m == NULL) {
15384e53f837SPyun YongHyeon 			m_freem(*m_head);
15394e53f837SPyun YongHyeon 			*m_head = NULL;
15404e53f837SPyun YongHyeon 			return (ENOMEM);
15411104779bSMike Silbersack 		}
15424e53f837SPyun YongHyeon 		*m_head = m;
1543a2057a72SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map,
15444e53f837SPyun YongHyeon 	    	    *m_head, segs, &nseg, 0);
15454e53f837SPyun YongHyeon 		if (error != 0) {
15464e53f837SPyun YongHyeon 			m_freem(*m_head);
15474e53f837SPyun YongHyeon 			*m_head = NULL;
15484e53f837SPyun YongHyeon 			return (ENOMEM);
15494e53f837SPyun YongHyeon 		}
15504e53f837SPyun YongHyeon 	} else if (error != 0)
15514e53f837SPyun YongHyeon 		return (error);
15524e53f837SPyun YongHyeon 	if (nseg == 0) {
15534e53f837SPyun YongHyeon 		m_freem(*m_head);
15544e53f837SPyun YongHyeon 		*m_head = NULL;
15554e53f837SPyun YongHyeon 		return (EIO);
155623a0ed7cSDavid Greenman 	}
155723a0ed7cSDavid Greenman 
155840c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
1559a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1560b2badf02SMaxime Henrion 
156140c20505SMaxime Henrion 	cbp = txp->tx_cb;
156240c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
156340c20505SMaxime Henrion 		/*
156440c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
156540c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
156640c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
156740c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
156840c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
156940c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
157040c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
157140c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
157240c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
157340c20505SMaxime Henrion 		 * starting from a different place depending on whether
157440c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
157540c20505SMaxime Henrion 		 */
157640c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
157768f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
157868f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
157940c20505SMaxime Henrion 		} else {
158040c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
158140c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
158240c20505SMaxime Henrion 		}
158340c20505SMaxime Henrion 	}
1584c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1585c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1586c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
158768f4ab9aSPyun YongHyeon 		cbp->tbd[nseg].tb_size |= htole32(0x8000);
1588c21e84e4SPyun YongHyeon 	} else
158940c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1590c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1591c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1592c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
159368f4ab9aSPyun YongHyeon 		cbp->tbd[1].tb_size |= htole32(tcp_payload << 16);
1594c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1595c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1596c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1597c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1598c21e84e4SPyun YongHyeon 	}
1599bd4fa9d9SPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
1600bd4fa9d9SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
1601bd4fa9d9SPyun YongHyeon 		cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag);
1602bd4fa9d9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_activation_high |=
1603bd4fa9d9SPyun YongHyeon 		    FXP_IPCB_INSERTVLAN_ENABLE;
1604bd4fa9d9SPyun YongHyeon 	}
160540c20505SMaxime Henrion 
16064e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1607b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1608b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
16094e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1610b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
161183e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
161283e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
16134e53f837SPyun YongHyeon 	else
1614b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
161583e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
161683e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1617c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1618b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1619a17c678eSDavid Greenman 
1620a17c678eSDavid Greenman 	/*
1621483b9871SDavid Greenman 	 * Advance the end of list forward.
1622a17c678eSDavid Greenman 	 */
162340c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
1624b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1625a17c678eSDavid Greenman 
1626a17c678eSDavid Greenman 	/*
16271cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1628b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1629483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1630a17c678eSDavid Greenman 	 */
16311cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1632b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1633a17c678eSDavid Greenman 
16341cd443acSDavid Greenman 	sc->tx_queued++;
16351cd443acSDavid Greenman 
163640c20505SMaxime Henrion 	return (0);
1637a17c678eSDavid Greenman }
1638a17c678eSDavid Greenman 
1639e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1640e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1641e4fc250cSLuigi Rizzo 
16421abcdbd1SAttilio Rao static int
1643e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1644e4fc250cSLuigi Rizzo {
1645e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
164674d1ed23SMaxime Henrion 	uint8_t statack;
16471abcdbd1SAttilio Rao 	int rx_npkts = 0;
1648e4fc250cSLuigi Rizzo 
16494953bccaSNate Lawson 	FXP_LOCK(sc);
165040929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
16514953bccaSNate Lawson 		FXP_UNLOCK(sc);
16521abcdbd1SAttilio Rao 		return (rx_npkts);
1653e4fc250cSLuigi Rizzo 	}
165440929967SGleb Smirnoff 
1655e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1656e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1657e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
165874d1ed23SMaxime Henrion 		uint8_t tmp;
16596481f301SPeter Wemm 
1660e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16614953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16624953bccaSNate Lawson 			FXP_UNLOCK(sc);
16631abcdbd1SAttilio Rao 			return (rx_npkts); /* nothing to do */
16644953bccaSNate Lawson 		}
1665e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1666e4fc250cSLuigi Rizzo 		/* ack what we can */
1667e4fc250cSLuigi Rizzo 		if (tmp != 0)
1668e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1669e4fc250cSLuigi Rizzo 		statack |= tmp;
1670e4fc250cSLuigi Rizzo 	}
16711abcdbd1SAttilio Rao 	rx_npkts = fxp_intr_body(sc, ifp, statack, count);
16724953bccaSNate Lawson 	FXP_UNLOCK(sc);
16731abcdbd1SAttilio Rao 	return (rx_npkts);
1674e4fc250cSLuigi Rizzo }
1675e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1676e4fc250cSLuigi Rizzo 
1677a17c678eSDavid Greenman /*
16789c7d2607SDavid Greenman  * Process interface interrupts.
1679a17c678eSDavid Greenman  */
168094927790SDavid Greenman static void
1681f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1682a17c678eSDavid Greenman {
1683f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1684fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
168574d1ed23SMaxime Henrion 	uint8_t statack;
16860f4dc94cSChuck Paterson 
16874953bccaSNate Lawson 	FXP_LOCK(sc);
1688704d1965SWarner Losh 	if (sc->suspended) {
1689704d1965SWarner Losh 		FXP_UNLOCK(sc);
1690704d1965SWarner Losh 		return;
1691704d1965SWarner Losh 	}
1692704d1965SWarner Losh 
1693e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
169440929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
16954953bccaSNate Lawson 		FXP_UNLOCK(sc);
1696e4fc250cSLuigi Rizzo 		return;
16974953bccaSNate Lawson 	}
1698e4fc250cSLuigi Rizzo #endif
1699b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1700a17c678eSDavid Greenman 		/*
170111457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
170211457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
170311457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
170411457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
170511457bbfSJonathan Lemon 		 */
17064953bccaSNate Lawson 		if (statack == 0xff) {
17074953bccaSNate Lawson 			FXP_UNLOCK(sc);
170811457bbfSJonathan Lemon 			return;
17094953bccaSNate Lawson 		}
171011457bbfSJonathan Lemon 
171111457bbfSJonathan Lemon 		/*
1712a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1713a17c678eSDavid Greenman 		 */
1714ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1715c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
17164953bccaSNate Lawson 			fxp_intr_body(sc, ifp, statack, -1);
1717e4fc250cSLuigi Rizzo 	}
17184953bccaSNate Lawson 	FXP_UNLOCK(sc);
1719e4fc250cSLuigi Rizzo }
1720e4fc250cSLuigi Rizzo 
1721e4fc250cSLuigi Rizzo static void
1722b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1723b2badf02SMaxime Henrion {
17244e53f837SPyun YongHyeon 	struct ifnet *ifp;
1725b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1726b2badf02SMaxime Henrion 
17274e53f837SPyun YongHyeon 	ifp = sc->ifp;
1728a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1729a2057a72SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1730b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
173183e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1732b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1733b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1734a2057a72SPyun YongHyeon 			bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
1735b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1736a2057a72SPyun YongHyeon 			bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
1737b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1738b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1739b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1740b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1741b2badf02SMaxime Henrion 		}
1742b2badf02SMaxime Henrion 		sc->tx_queued--;
17434e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1744b2badf02SMaxime Henrion 	}
1745b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1746a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1747a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17486b24912cSPyun YongHyeon 	if (sc->tx_queued == 0)
174925935344SPyun YongHyeon 		sc->watchdog_timer = 0;
1750b2badf02SMaxime Henrion }
1751b2badf02SMaxime Henrion 
1752b2badf02SMaxime Henrion static void
1753f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m,
1754f13075afSPyun YongHyeon     uint16_t status, int pos)
1755f13075afSPyun YongHyeon {
1756f13075afSPyun YongHyeon 	struct ether_header *eh;
1757f13075afSPyun YongHyeon 	struct ip *ip;
1758f13075afSPyun YongHyeon 	struct udphdr *uh;
1759f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1760f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1761f13075afSPyun YongHyeon 
1762f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1763f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1764f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1765f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1766f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1767f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1768f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1769f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1770f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1771f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1772f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1773f13075afSPyun YongHyeon 			}
1774f13075afSPyun YongHyeon 		}
1775f13075afSPyun YongHyeon 		return;
1776f13075afSPyun YongHyeon 	}
1777f13075afSPyun YongHyeon 
1778f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1779f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1780f13075afSPyun YongHyeon 		return;
1781f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1782f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1783f13075afSPyun YongHyeon 		return;
1784f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1785f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1786f13075afSPyun YongHyeon 		return;
1787f13075afSPyun YongHyeon 
1788f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1789f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1790f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1791f13075afSPyun YongHyeon 		return;
1792f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1793f13075afSPyun YongHyeon 		return;
1794f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1795f13075afSPyun YongHyeon 		return;
1796f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1797f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1798f13075afSPyun YongHyeon 
1799f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1800f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1801f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1802f13075afSPyun YongHyeon 			return;
1803f13075afSPyun YongHyeon 		break;
1804f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1805f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1806f13075afSPyun YongHyeon 			return;
1807f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1808f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1809f13075afSPyun YongHyeon 			return; /* no checksum */
1810f13075afSPyun YongHyeon 		break;
1811f13075afSPyun YongHyeon 	default:
1812f13075afSPyun YongHyeon 		return;
1813f13075afSPyun YongHyeon 	}
1814f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1815f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1816f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1817f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1818f13075afSPyun YongHyeon 	if (len > 0) {
1819f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1820f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1821f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1822f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1823f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1824f13075afSPyun YongHyeon 		}
1825f13075afSPyun YongHyeon 	}
1826f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1827f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1828f13075afSPyun YongHyeon }
1829f13075afSPyun YongHyeon 
18301abcdbd1SAttilio Rao static int
183174d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
18324953bccaSNate Lawson     int count)
1833e4fc250cSLuigi Rizzo {
18342b5989e9SLuigi Rizzo 	struct mbuf *m;
1835b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18362b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18372b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
18381abcdbd1SAttilio Rao 	int rx_npkts;
183960bb79ebSPyun YongHyeon 	uint16_t status;
18402b5989e9SLuigi Rizzo 
18411abcdbd1SAttilio Rao 	rx_npkts = 0;
184267fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18431abcdbd1SAttilio Rao 
18442b5989e9SLuigi Rizzo 	if (rnr)
18450f1db1d6SMaxime Henrion 		sc->rnr++;
1846947e3815SIan Dowse #ifdef DEVICE_POLLING
1847947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1848947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1849947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1850947e3815SIan Dowse 		rnr = 1;
1851947e3815SIan Dowse 	}
1852947e3815SIan Dowse #endif
1853a17c678eSDavid Greenman 
1854a17c678eSDavid Greenman 	/*
18553114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
185606936301SBill Paul 	 *
185706936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
185806936301SBill Paul 	 * be that this event (control unit not ready) was not
185906936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
186006936301SBill Paul 	 * The exact sequence of events that occur when the interface
186106936301SBill Paul 	 * is brought up are different now, and if this event
186206936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
186306936301SBill Paul 	 * can stall for several seconds. The result is that no
186406936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
186506936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
18663114fdb4SDavid Greenman 	 */
18674e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1868b2badf02SMaxime Henrion 		fxp_txeof(sc);
18693114fdb4SDavid Greenman 
18703114fdb4SDavid Greenman 	/*
18713114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
18723114fdb4SDavid Greenman 	 */
18737929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
18744953bccaSNate Lawson 		fxp_start_body(ifp);
18752b5989e9SLuigi Rizzo 
18762b5989e9SLuigi Rizzo 	/*
18772b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
18782b5989e9SLuigi Rizzo 	 */
1879947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
18801abcdbd1SAttilio Rao 		return (rx_npkts);
18812b5989e9SLuigi Rizzo 
18823114fdb4SDavid Greenman 	/*
1883a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1884a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1885a17c678eSDavid Greenman 	 * re-start the receiver.
1886947e3815SIan Dowse 	 *
18872b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
18882b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
18892b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
18902b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1891947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1892947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1893a17c678eSDavid Greenman 	 */
18942b5989e9SLuigi Rizzo 	for (;;) {
1895b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1896b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1897ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1898ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1899a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
19004812aef5SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1901a17c678eSDavid Greenman 
1902e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1903947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1904947e3815SIan Dowse 			if (rnr) {
1905947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1906947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1907947e3815SIan Dowse 				rnr = 0;
1908947e3815SIan Dowse 			}
19092b5989e9SLuigi Rizzo 			break;
1910947e3815SIan Dowse 		}
19112b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
19122b5989e9SLuigi Rizzo 
191360bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
191460bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
19152b5989e9SLuigi Rizzo 			break;
19162b5989e9SLuigi Rizzo 
1917f7a5f737SPyun YongHyeon 		if ((status & FXP_RFA_STATUS_RNR) != 0)
1918f7a5f737SPyun YongHyeon 			rnr++;
1919dfe61cf1SDavid Greenman 		/*
1920b2badf02SMaxime Henrion 		 * Advance head forward.
1921dfe61cf1SDavid Greenman 		 */
1922b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1923a17c678eSDavid Greenman 
1924dfe61cf1SDavid Greenman 		/*
1925ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1926ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1927ba8c6fd5SDavid Greenman 		 * instead.
1928dfe61cf1SDavid Greenman 		 */
192985050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1930aed53495SDavid Greenman 			int total_len;
1931a17c678eSDavid Greenman 
1932e8c8b728SJonathan Lemon 			/*
19332b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
19342b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
19352b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
19362b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1937e8c8b728SJonathan Lemon 			 */
1938bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1939f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
1940f13075afSPyun YongHyeon 			    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1941f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1942f13075afSPyun YongHyeon 				total_len -= 2;
1943f13075afSPyun YongHyeon 			}
1944991ae908SPyun YongHyeon 			if (total_len < (int)sizeof(struct ether_header) ||
1945f7a5f737SPyun YongHyeon 			    total_len > (MCLBYTES - RFA_ALIGNMENT_FUDGE -
1946f7a5f737SPyun YongHyeon 			    sc->rfa_size) ||
1947f7a5f737SPyun YongHyeon 			    status & (FXP_RFA_STATUS_CRC |
1948991ae908SPyun YongHyeon 			    FXP_RFA_STATUS_ALIGN | FXP_RFA_STATUS_OVERRUN)) {
1949e8c8b728SJonathan Lemon 				m_freem(m);
1950f7a5f737SPyun YongHyeon 				fxp_add_rfabuf(sc, rxp);
19512b5989e9SLuigi Rizzo 				continue;
1952e8c8b728SJonathan Lemon 			}
1953920b58e8SBrooks Davis 
19542e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1955673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1956673d9191SSam Leffler 
1957f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
1958f13075afSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1959f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
1960bd4fa9d9SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
1961bd4fa9d9SPyun YongHyeon 			    (status & FXP_RFA_STATUS_VLAN) != 0) {
1962bd4fa9d9SPyun YongHyeon 				m->m_pkthdr.ether_vtag =
1963bd4fa9d9SPyun YongHyeon 				    ntohs(rfa->rfax_vlan_id);
1964bd4fa9d9SPyun YongHyeon 				m->m_flags |= M_VLANTAG;
1965bd4fa9d9SPyun YongHyeon 			}
196605fb8c3fSNate Lawson 			/*
196705fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
196805fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
196905fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
197005fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
197105fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
197205fb8c3fSNate Lawson 			 * calling if_input() on each one.
197305fb8c3fSNate Lawson 			 */
197405fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
1975673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
197605fb8c3fSNate Lawson 			FXP_LOCK(sc);
19771abcdbd1SAttilio Rao 			rx_npkts++;
1978c109e385SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1979c109e385SPyun YongHyeon 				return (rx_npkts);
198085050421SPyun YongHyeon 		} else {
198185050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
198285050421SPyun YongHyeon 			ifp->if_iqdrops++;
198385050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
1984a17c678eSDavid Greenman 		}
198585050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
1986a17c678eSDavid Greenman 	}
19872b5989e9SLuigi Rizzo 	if (rnr) {
1988ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1989ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1990b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
19912e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1992a17c678eSDavid Greenman 	}
19931abcdbd1SAttilio Rao 	return (rx_npkts);
1994a17c678eSDavid Greenman }
1995a17c678eSDavid Greenman 
1996303b270bSEivind Eklund static void
19978da9c507SPyun YongHyeon fxp_update_stats(struct fxp_softc *sc)
1998a17c678eSDavid Greenman {
1999fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2000a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
20018da9c507SPyun YongHyeon 	struct fxp_hwstats *hsp;
20028da9c507SPyun YongHyeon 	uint32_t *status;
2003a17c678eSDavid Greenman 
20043212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
20058da9c507SPyun YongHyeon 
20068da9c507SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
20078da9c507SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
20088da9c507SPyun YongHyeon 	/* Update statistical counters. */
20098da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
20108da9c507SPyun YongHyeon 		status = &sp->completion_status;
20118da9c507SPyun YongHyeon 	else if (sc->revision >= FXP_REV_82558_A4)
20128da9c507SPyun YongHyeon 		status = (uint32_t *)&sp->tx_tco;
20138da9c507SPyun YongHyeon 	else
20148da9c507SPyun YongHyeon 		status = &sp->tx_pause;
20158da9c507SPyun YongHyeon 	if (*status == htole32(FXP_STATS_DR_COMPLETE)) {
20168da9c507SPyun YongHyeon 		hsp = &sc->fxp_hwstats;
20178da9c507SPyun YongHyeon 		hsp->tx_good += le32toh(sp->tx_good);
20188da9c507SPyun YongHyeon 		hsp->tx_maxcols += le32toh(sp->tx_maxcols);
20198da9c507SPyun YongHyeon 		hsp->tx_latecols += le32toh(sp->tx_latecols);
20208da9c507SPyun YongHyeon 		hsp->tx_underruns += le32toh(sp->tx_underruns);
20218da9c507SPyun YongHyeon 		hsp->tx_lostcrs += le32toh(sp->tx_lostcrs);
20228da9c507SPyun YongHyeon 		hsp->tx_deffered += le32toh(sp->tx_deffered);
20238da9c507SPyun YongHyeon 		hsp->tx_single_collisions += le32toh(sp->tx_single_collisions);
20248da9c507SPyun YongHyeon 		hsp->tx_multiple_collisions +=
20258da9c507SPyun YongHyeon 		    le32toh(sp->tx_multiple_collisions);
20268da9c507SPyun YongHyeon 		hsp->tx_total_collisions += le32toh(sp->tx_total_collisions);
20278da9c507SPyun YongHyeon 		hsp->rx_good += le32toh(sp->rx_good);
20288da9c507SPyun YongHyeon 		hsp->rx_crc_errors += le32toh(sp->rx_crc_errors);
20298da9c507SPyun YongHyeon 		hsp->rx_alignment_errors += le32toh(sp->rx_alignment_errors);
20308da9c507SPyun YongHyeon 		hsp->rx_rnr_errors += le32toh(sp->rx_rnr_errors);
20318da9c507SPyun YongHyeon 		hsp->rx_overrun_errors += le32toh(sp->rx_overrun_errors);
20328da9c507SPyun YongHyeon 		hsp->rx_cdt_errors += le32toh(sp->rx_cdt_errors);
20338da9c507SPyun YongHyeon 		hsp->rx_shortframes += le32toh(sp->rx_shortframes);
20348da9c507SPyun YongHyeon 		hsp->tx_pause += le32toh(sp->tx_pause);
20358da9c507SPyun YongHyeon 		hsp->rx_pause += le32toh(sp->rx_pause);
20368da9c507SPyun YongHyeon 		hsp->rx_controls += le32toh(sp->rx_controls);
20378da9c507SPyun YongHyeon 		hsp->tx_tco += le16toh(sp->tx_tco);
20388da9c507SPyun YongHyeon 		hsp->rx_tco += le16toh(sp->rx_tco);
20398da9c507SPyun YongHyeon 
204083e6547dSMaxime Henrion 		ifp->if_opackets += le32toh(sp->tx_good);
204183e6547dSMaxime Henrion 		ifp->if_collisions += le32toh(sp->tx_total_collisions);
2042397f9dfeSDavid Greenman 		if (sp->rx_good) {
204383e6547dSMaxime Henrion 			ifp->if_ipackets += le32toh(sp->rx_good);
2044397f9dfeSDavid Greenman 			sc->rx_idle_secs = 0;
204543d8b117SPyun YongHyeon 		} else if (sc->flags & FXP_FLAG_RXBUG) {
2046c8cc6fcaSDavid Greenman 			/*
2047c8cc6fcaSDavid Greenman 			 * Receiver's been idle for another second.
2048c8cc6fcaSDavid Greenman 			 */
2049397f9dfeSDavid Greenman 			sc->rx_idle_secs++;
2050397f9dfeSDavid Greenman 		}
20513ba65732SDavid Greenman 		ifp->if_ierrors +=
205283e6547dSMaxime Henrion 		    le32toh(sp->rx_crc_errors) +
205383e6547dSMaxime Henrion 		    le32toh(sp->rx_alignment_errors) +
205483e6547dSMaxime Henrion 		    le32toh(sp->rx_rnr_errors) +
205583e6547dSMaxime Henrion 		    le32toh(sp->rx_overrun_errors);
2056a17c678eSDavid Greenman 		/*
2057f9be9005SDavid Greenman 		 * If any transmit underruns occured, bump up the transmit
2058f9be9005SDavid Greenman 		 * threshold by another 512 bytes (64 * 8).
2059f9be9005SDavid Greenman 		 */
2060f9be9005SDavid Greenman 		if (sp->tx_underruns) {
206183e6547dSMaxime Henrion 			ifp->if_oerrors += le32toh(sp->tx_underruns);
2062f9be9005SDavid Greenman 			if (tx_threshold < 192)
2063f9be9005SDavid Greenman 				tx_threshold += 64;
2064f9be9005SDavid Greenman 		}
20658da9c507SPyun YongHyeon 		*status = 0;
20668da9c507SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
20678da9c507SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
20688da9c507SPyun YongHyeon 	}
20698da9c507SPyun YongHyeon }
20708da9c507SPyun YongHyeon 
20718da9c507SPyun YongHyeon /*
20728da9c507SPyun YongHyeon  * Update packet in/out/collision statistics. The i82557 doesn't
20738da9c507SPyun YongHyeon  * allow you to access these counters without doing a fairly
20748da9c507SPyun YongHyeon  * expensive DMA to get _all_ of the statistics it maintains, so
20758da9c507SPyun YongHyeon  * we do this operation here only once per second. The statistics
20768da9c507SPyun YongHyeon  * counters in the kernel are updated from the previous dump-stats
20778da9c507SPyun YongHyeon  * DMA and then a new dump-stats DMA is started. The on-chip
20788da9c507SPyun YongHyeon  * counters are zeroed when the DMA completes. If we can't start
20798da9c507SPyun YongHyeon  * the DMA immediately, we don't wait - we just prepare to read
20808da9c507SPyun YongHyeon  * them again next time.
20818da9c507SPyun YongHyeon  */
20828da9c507SPyun YongHyeon static void
20838da9c507SPyun YongHyeon fxp_tick(void *xsc)
20848da9c507SPyun YongHyeon {
20858da9c507SPyun YongHyeon 	struct fxp_softc *sc = xsc;
20868da9c507SPyun YongHyeon 	struct ifnet *ifp = sc->ifp;
20878da9c507SPyun YongHyeon 
20888da9c507SPyun YongHyeon 	FXP_LOCK_ASSERT(sc, MA_OWNED);
20898da9c507SPyun YongHyeon 
20908da9c507SPyun YongHyeon 	/* Update statistical counters. */
20918da9c507SPyun YongHyeon 	fxp_update_stats(sc);
20924953bccaSNate Lawson 
2093397f9dfeSDavid Greenman 	/*
2094c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
2095c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
2096c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
2097c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
2098c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
2099c8cc6fcaSDavid Greenman 	 */
2100b2badf02SMaxime Henrion 	fxp_txeof(sc);
2101b2badf02SMaxime Henrion 
2102c8cc6fcaSDavid Greenman 	/*
2103397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
2104397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2105397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2106397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2107397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
2108397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2109397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2110397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2111397f9dfeSDavid Greenman 	 */
2112397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2113397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
2114c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
21151845b5c3SMarius Strobl 			fxp_init_body(sc, 1);
21166b24912cSPyun YongHyeon 		return;
2117397f9dfeSDavid Greenman 	}
2118f9be9005SDavid Greenman 	/*
21193ba65732SDavid Greenman 	 * If there is no pending command, start another stats
21203ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2121a17c678eSDavid Greenman 	 */
2122397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2123a17c678eSDavid Greenman 		/*
2124397f9dfeSDavid Greenman 		 * Start another stats dump.
2125a17c678eSDavid Greenman 		 */
21262e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2127dfe61cf1SDavid Greenman 	}
2128f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2129f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
21304953bccaSNate Lawson 
2131a17c678eSDavid Greenman 	/*
213216f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2133df79d527SGleb Smirnoff 	 */
2134df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2135df79d527SGleb Smirnoff 
2136df79d527SGleb Smirnoff 	/*
2137a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2138a17c678eSDavid Greenman 	 */
213945276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2140a17c678eSDavid Greenman }
2141a17c678eSDavid Greenman 
2142a17c678eSDavid Greenman /*
2143a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2144a17c678eSDavid Greenman  * the interface.
2145a17c678eSDavid Greenman  */
2146a17c678eSDavid Greenman static void
2147f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2148a17c678eSDavid Greenman {
2149fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2150b2badf02SMaxime Henrion 	struct fxp_tx *txp;
21513ba65732SDavid Greenman 	int i;
2152a17c678eSDavid Greenman 
215313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2154df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
21557dced78aSDavid Greenman 
2156a17c678eSDavid Greenman 	/*
2157a17c678eSDavid Greenman 	 * Cancel stats updater.
2158a17c678eSDavid Greenman 	 */
215945276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
21603ba65732SDavid Greenman 
21613ba65732SDavid Greenman 	/*
21627137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
21637137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
21643ba65732SDavid Greenman 	 */
21657137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
216672a32a26SJonathan Lemon 	DELAY(50);
21677137cea0SPyun YongHyeon 	/* Disable interrupts. */
21687137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2169a17c678eSDavid Greenman 
21708da9c507SPyun YongHyeon 	fxp_update_stats(sc);
21718da9c507SPyun YongHyeon 
21723ba65732SDavid Greenman 	/*
21733ba65732SDavid Greenman 	 * Release any xmit buffers.
21743ba65732SDavid Greenman 	 */
2175b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2176da91462dSDavid Greenman 	if (txp != NULL) {
2177da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
2178b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
2179a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp[i].tx_map,
2180b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
2181a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag,
2182a2057a72SPyun YongHyeon 				    txp[i].tx_map);
2183b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
2184b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
2185c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
2186b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
2187da91462dSDavid Greenman 			}
2188da91462dSDavid Greenman 		}
21893ba65732SDavid Greenman 	}
2190a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2191a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
21923ba65732SDavid Greenman 	sc->tx_queued = 0;
2193a17c678eSDavid Greenman }
2194a17c678eSDavid Greenman 
2195a17c678eSDavid Greenman /*
2196a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2197a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2198a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2199a17c678eSDavid Greenman  * card has wedged for some reason.
2200a17c678eSDavid Greenman  */
2201a17c678eSDavid Greenman static void
2202df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2203a17c678eSDavid Greenman {
2204ba8c6fd5SDavid Greenman 
2205df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2206df79d527SGleb Smirnoff 
2207df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2208df79d527SGleb Smirnoff 		return;
2209df79d527SGleb Smirnoff 
2210f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2211df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
2212a17c678eSDavid Greenman 
22131845b5c3SMarius Strobl 	fxp_init_body(sc, 1);
2214a17c678eSDavid Greenman }
2215a17c678eSDavid Greenman 
22164953bccaSNate Lawson /*
22174953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
22184953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
22194953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
22204953bccaSNate Lawson  */
2221a17c678eSDavid Greenman static void
2222f7788e8eSJonathan Lemon fxp_init(void *xsc)
2223a17c678eSDavid Greenman {
2224fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
22254953bccaSNate Lawson 
22264953bccaSNate Lawson 	FXP_LOCK(sc);
22271845b5c3SMarius Strobl 	fxp_init_body(sc, 1);
22284953bccaSNate Lawson 	FXP_UNLOCK(sc);
22294953bccaSNate Lawson }
22304953bccaSNate Lawson 
22314953bccaSNate Lawson /*
22324953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
22334953bccaSNate Lawson  * softc lock held.
22344953bccaSNate Lawson  */
22354953bccaSNate Lawson static void
22361845b5c3SMarius Strobl fxp_init_body(struct fxp_softc *sc, int setmedia)
22374953bccaSNate Lawson {
2238fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
22391845b5c3SMarius Strobl 	struct mii_data *mii;
2240a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2241a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2242b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2243b2badf02SMaxime Henrion 	struct fxp_tx *txp;
22443212724cSJohn Baldwin 	int i, prm;
2245a17c678eSDavid Greenman 
224667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2247a17c678eSDavid Greenman 	/*
22483ba65732SDavid Greenman 	 * Cancel any pending I/O
2249a17c678eSDavid Greenman 	 */
22503ba65732SDavid Greenman 	fxp_stop(sc);
2251a17c678eSDavid Greenman 
22527137cea0SPyun YongHyeon 	/*
22537137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
22547137cea0SPyun YongHyeon 	 */
22557137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
22567137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
22577137cea0SPyun YongHyeon 	DELAY(50);
22587137cea0SPyun YongHyeon 
2259a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
2260a17c678eSDavid Greenman 
2261a17c678eSDavid Greenman 	/*
2262a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2263a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2264a17c678eSDavid Greenman 	 */
2265ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
22662e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2267a17c678eSDavid Greenman 
2268ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
22692e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2270a17c678eSDavid Greenman 
2271a17c678eSDavid Greenman 	/*
2272a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2273a17c678eSDavid Greenman 	 */
2274ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
22758da9c507SPyun YongHyeon 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
22768da9c507SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
22778da9c507SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2278b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
22792e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2280a17c678eSDavid Greenman 
2281a17c678eSDavid Greenman 	/*
228272a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
2283b96ad4b2SPyun YongHyeon 	 * For ICH based controllers do not load microcode.
228472a32a26SJonathan Lemon 	 */
2285b96ad4b2SPyun YongHyeon 	if (sc->ident->ich == 0) {
2286b96ad4b2SPyun YongHyeon 		if (ifp->if_flags & IFF_LINK0 &&
2287b96ad4b2SPyun YongHyeon 		    (sc->flags & FXP_FLAG_UCODE) == 0)
228872a32a26SJonathan Lemon 			fxp_load_ucode(sc);
2289b96ad4b2SPyun YongHyeon 	}
229072a32a26SJonathan Lemon 
229172a32a26SJonathan Lemon 	/*
22926b24912cSPyun YongHyeon 	 * Set IFF_ALLMULTI status. It's needed in configure action
22936b24912cSPyun YongHyeon 	 * command.
229409882363SJonathan Lemon 	 */
22956b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
229609882363SJonathan Lemon 
229709882363SJonathan Lemon 	/*
2298a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2299a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2300a17c678eSDavid Greenman 	 * later.
2301a17c678eSDavid Greenman 	 */
2302b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2303a17c678eSDavid Greenman 
2304a17c678eSDavid Greenman 	/*
2305a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2306a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2307a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2308a17c678eSDavid Greenman 	 */
2309b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2310a17c678eSDavid Greenman 
2311a17c678eSDavid Greenman 	cbp->cb_status =	0;
231283e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
231383e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
231483e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
23152c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2316001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2317001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2318a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2319f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2320f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2321f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2322f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2323001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2324001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2325f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2326a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2327f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2328f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
23293114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2330f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2331f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2332f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
23338ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2334a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2335f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2336f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2337c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2338c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2339f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2340f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2341f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
2342f13075afSPyun YongHyeon 	    (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0;
2343f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2344f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2345f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2346f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2347a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2348a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2349a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2350a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2351a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2352a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2353a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2354a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2355f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2356f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2357f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2358f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2359f7788e8eSJonathan Lemon 
2360a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2361a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2362a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2363f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2364f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
23657137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2366a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
23673ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2368a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2369a026a25bSPyun YongHyeon 	cbp->mc_all =		ifp->if_flags & IFF_ALLMULTI ? 1 : prm;
2370c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2371bd4fa9d9SPyun YongHyeon 	cbp->vlan_strip_en =	((sc->flags & FXP_FLAG_EXT_RFA) != 0 &&
2372bd4fa9d9SPyun YongHyeon 	    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0;
2373a17c678eSDavid Greenman 
23741845b5c3SMarius Strobl 	if (sc->revision == FXP_REV_82557) {
23753bd07cfdSJonathan Lemon 		/*
23763bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
23773bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
23783bd07cfdSJonathan Lemon 		 */
23793bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
23803bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
23813bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23823bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
23833bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
23843bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
23853bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
23863bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
23873bd07cfdSJonathan Lemon 	} else {
23881845b5c3SMarius Strobl 		/* Set pause RX FIFO threshold to 1KB. */
23891845b5c3SMarius Strobl 		CSR_WRITE_1(sc, FXP_CSR_FC_THRESH, 1);
23901845b5c3SMarius Strobl 		/* Set pause time. */
23911845b5c3SMarius Strobl 		cbp->fc_delay_lsb =	0xff;
23921845b5c3SMarius Strobl 		cbp->fc_delay_msb =	0xff;
23933bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23941845b5c3SMarius Strobl 		mii = device_get_softc(sc->miibus);
23951845b5c3SMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
23961845b5c3SMarius Strobl 		    IFM_ETH_TXPAUSE) != 0)
23971845b5c3SMarius Strobl 			/* enable transmit FC */
23981845b5c3SMarius Strobl 			cbp->tx_fc_dis = 0;
23991845b5c3SMarius Strobl 		else
24001845b5c3SMarius Strobl 			/* disable transmit FC */
24011845b5c3SMarius Strobl 			cbp->tx_fc_dis = 1;
24021845b5c3SMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
24031845b5c3SMarius Strobl 		    IFM_ETH_RXPAUSE) != 0) {
24041845b5c3SMarius Strobl 			/* enable FC restart/restop frames */
24051845b5c3SMarius Strobl 			cbp->rx_fc_restart = 1;
24061845b5c3SMarius Strobl 			cbp->rx_fc_restop = 1;
24071845b5c3SMarius Strobl 		} else {
24081845b5c3SMarius Strobl 			/* disable FC restart/restop frames */
24091845b5c3SMarius Strobl 			cbp->rx_fc_restart = 0;
24101845b5c3SMarius Strobl 			cbp->rx_fc_restop = 0;
24111845b5c3SMarius Strobl 		}
24123bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
24133bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
24143bd07cfdSJonathan Lemon 	}
24153bd07cfdSJonathan Lemon 
24168da9c507SPyun YongHyeon 	/* Enable 82558 and 82559 extended statistics functionality. */
24178da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4) {
24188da9c507SPyun YongHyeon 		if (sc->revision >= FXP_REV_82559_A0) {
24198da9c507SPyun YongHyeon 			/*
24208da9c507SPyun YongHyeon 			 * Extend configuration table size to 32
24218da9c507SPyun YongHyeon 			 * to include TCO configuration.
24228da9c507SPyun YongHyeon 			 */
24238da9c507SPyun YongHyeon 			cbp->byte_count = 32;
24248da9c507SPyun YongHyeon 			cbp->ext_stats_dis = 1;
24258da9c507SPyun YongHyeon 			/* Enable TCO stats. */
24268da9c507SPyun YongHyeon 			cbp->tno_int_or_tco_en = 1;
24278da9c507SPyun YongHyeon 			cbp->gamla_rx = 1;
24288da9c507SPyun YongHyeon 		} else
24298da9c507SPyun YongHyeon 			cbp->ext_stats_dis = 0;
24308da9c507SPyun YongHyeon 	}
24318da9c507SPyun YongHyeon 
2432a17c678eSDavid Greenman 	/*
2433a17c678eSDavid Greenman 	 * Start the config command/DMA.
2434a17c678eSDavid Greenman 	 */
2435ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24365986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24375986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2438b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24392e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2440a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2441209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2442a17c678eSDavid Greenman 
2443a17c678eSDavid Greenman 	/*
2444a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2445a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2446a17c678eSDavid Greenman 	 */
2447b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2448a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
244983e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
245083e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
24514a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2452a17c678eSDavid Greenman 
2453a17c678eSDavid Greenman 	/*
2454a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2455a17c678eSDavid Greenman 	 */
2456ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24575986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24585986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
24596b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24602e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2461a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2462209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2463a17c678eSDavid Greenman 
2464a17c678eSDavid Greenman 	/*
24656b24912cSPyun YongHyeon 	 * Initialize the multicast address list.
24666b24912cSPyun YongHyeon 	 */
24676b24912cSPyun YongHyeon 	fxp_mc_setup(sc);
24686b24912cSPyun YongHyeon 
24696b24912cSPyun YongHyeon 	/*
2470a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2471a17c678eSDavid Greenman 	 */
2472b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2473b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2474b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2475a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2476b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
247783e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
247883e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
247983e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
248083e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
24813bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2482b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
248383e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
24843bd07cfdSJonathan Lemon 		else
2485b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
248683e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2487b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2488a17c678eSDavid Greenman 	}
2489a17c678eSDavid Greenman 	/*
2490397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2491a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2492a17c678eSDavid Greenman 	 */
249383e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2494a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2495a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2496b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2497397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2498a17c678eSDavid Greenman 
2499ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
25006b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
25012e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2502a17c678eSDavid Greenman 
2503a17c678eSDavid Greenman 	/*
2504a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2505a17c678eSDavid Greenman 	 */
2506ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2507b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
25082e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2509a17c678eSDavid Greenman 
25101845b5c3SMarius Strobl 	if (sc->miibus != NULL && setmedia != 0)
2511f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2512dccee1a1SDavid Greenman 
251313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
251413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2515e8c8b728SJonathan Lemon 
2516e8c8b728SJonathan Lemon 	/*
2517e8c8b728SJonathan Lemon 	 * Enable interrupts.
2518e8c8b728SJonathan Lemon 	 */
25192b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
25202b5989e9SLuigi Rizzo 	/*
25212b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
25222b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
25232b5989e9SLuigi Rizzo 	 */
252440929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
25252b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
25262b5989e9SLuigi Rizzo 	else
25272b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2528e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2529a17c678eSDavid Greenman 
2530a17c678eSDavid Greenman 	/*
2531a17c678eSDavid Greenman 	 * Start stats updater.
2532a17c678eSDavid Greenman 	 */
253345276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2534f7788e8eSJonathan Lemon }
2535f7788e8eSJonathan Lemon 
2536f7788e8eSJonathan Lemon static int
2537f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2538f7788e8eSJonathan Lemon {
2539f7788e8eSJonathan Lemon 
2540f7788e8eSJonathan Lemon 	return (0);
2541a17c678eSDavid Greenman }
2542a17c678eSDavid Greenman 
2543303b270bSEivind Eklund static void
2544f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2545ba8c6fd5SDavid Greenman {
2546ba8c6fd5SDavid Greenman 
2547f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2548ba8c6fd5SDavid Greenman }
2549ba8c6fd5SDavid Greenman 
2550ba8c6fd5SDavid Greenman /*
2551ba8c6fd5SDavid Greenman  * Change media according to request.
2552ba8c6fd5SDavid Greenman  */
2553f7788e8eSJonathan Lemon static int
2554f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2555ba8c6fd5SDavid Greenman {
2556ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2557f7788e8eSJonathan Lemon 	struct mii_data *mii;
25583fcb7a53SMarius Strobl 		struct mii_softc	*miisc;
2559ba8c6fd5SDavid Greenman 
2560f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25613212724cSJohn Baldwin 	FXP_LOCK(sc);
25625aa0cdf4SJohn-Mark Gurney 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
25633fcb7a53SMarius Strobl 		PHY_RESET(miisc);
2564f7788e8eSJonathan Lemon 	mii_mediachg(mii);
25653212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2566ba8c6fd5SDavid Greenman 	return (0);
2567ba8c6fd5SDavid Greenman }
2568ba8c6fd5SDavid Greenman 
2569ba8c6fd5SDavid Greenman /*
2570ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2571ba8c6fd5SDavid Greenman  */
2572f7788e8eSJonathan Lemon static void
2573f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2574ba8c6fd5SDavid Greenman {
2575ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2576f7788e8eSJonathan Lemon 	struct mii_data *mii;
2577ba8c6fd5SDavid Greenman 
2578f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25793212724cSJohn Baldwin 	FXP_LOCK(sc);
2580f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2581f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2582f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
25832e2b8238SJonathan Lemon 
25842b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
25852b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
25862e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
25872e2b8238SJonathan Lemon 	else
25882e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
25893212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2590ba8c6fd5SDavid Greenman }
2591ba8c6fd5SDavid Greenman 
2592a17c678eSDavid Greenman /*
2593a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2594a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
259585050421SPyun YongHyeon  * reusing the RFA buffer.
2596a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2597a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2598a17c678eSDavid Greenman  */
2599a17c678eSDavid Greenman static int
260085050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2601a17c678eSDavid Greenman {
2602a17c678eSDavid Greenman 	struct mbuf *m;
260385050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2604b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
260585050421SPyun YongHyeon 	int error;
2606a17c678eSDavid Greenman 
2607a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
260885050421SPyun YongHyeon 	if (m == NULL)
260985050421SPyun YongHyeon 		return (ENOBUFS);
2610ba8c6fd5SDavid Greenman 
2611ba8c6fd5SDavid Greenman 	/*
2612ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2613ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2614ba8c6fd5SDavid Greenman 	 */
2615ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2616ba8c6fd5SDavid Greenman 
2617eadd5e3aSDavid Greenman 	/*
2618eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2619eadd5e3aSDavid Greenman 	 * data start past it.
2620eadd5e3aSDavid Greenman 	 */
2621a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2622c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
262383e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2624eadd5e3aSDavid Greenman 
2625a17c678eSDavid Greenman 	rfa->rfa_status = 0;
262683e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2627a17c678eSDavid Greenman 	rfa->actual_size = 0;
262885050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
262985050421SPyun YongHyeon 	    sc->rfa_size;
2630ba8c6fd5SDavid Greenman 
263128935f27SMaxime Henrion 	/*
263228935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
263328935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
263428935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
263528935f27SMaxime Henrion 	 * is also alignment-safe.
263628935f27SMaxime Henrion 	 */
263783e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
263883e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2639ba8c6fd5SDavid Greenman 
2640b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2641a2057a72SPyun YongHyeon 	error = bus_dmamap_load(sc->fxp_rxmtag, sc->spare_map, rfa,
2642b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
264301e3ef82SPyun YongHyeon 	    &rxp->rx_addr, BUS_DMA_NOWAIT);
2644b2badf02SMaxime Henrion 	if (error) {
2645b2badf02SMaxime Henrion 		m_freem(m);
2646b2badf02SMaxime Henrion 		return (error);
2647b2badf02SMaxime Henrion 	}
2648b2badf02SMaxime Henrion 
2649e2157cf7SPyun YongHyeon 	if (rxp->rx_mbuf != NULL)
2650a2057a72SPyun YongHyeon 		bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
2651b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2652b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2653b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2654b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2655b2badf02SMaxime Henrion 
2656a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
2657b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
265885050421SPyun YongHyeon 	return (0);
265985050421SPyun YongHyeon }
266085050421SPyun YongHyeon 
266185050421SPyun YongHyeon static void
266285050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
266385050421SPyun YongHyeon {
266485050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
266585050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2666b2badf02SMaxime Henrion 
2667dfe61cf1SDavid Greenman 	/*
2668dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2669dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2670dfe61cf1SDavid Greenman 	 */
2671b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2672b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2673b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2674b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2675b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
267683e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2677aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2678a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, p_rx->rx_map,
26794812aef5SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2680a17c678eSDavid Greenman 	} else {
2681b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2682b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2683a17c678eSDavid Greenman 	}
2684b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
268585050421SPyun YongHyeon }
268685050421SPyun YongHyeon 
268785050421SPyun YongHyeon static void
268885050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
268985050421SPyun YongHyeon {
269085050421SPyun YongHyeon 	struct mbuf *m;
269185050421SPyun YongHyeon 	struct fxp_rfa *rfa;
269285050421SPyun YongHyeon 
269385050421SPyun YongHyeon 	m = rxp->rx_mbuf;
269485050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
269585050421SPyun YongHyeon 	/*
269685050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
269785050421SPyun YongHyeon 	 * will be 32-bit aligned.
269885050421SPyun YongHyeon 	 */
269985050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
270085050421SPyun YongHyeon 
270185050421SPyun YongHyeon 	/*
270285050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
270385050421SPyun YongHyeon 	 * data start past it.
270485050421SPyun YongHyeon 	 */
270585050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
270685050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
270785050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
270885050421SPyun YongHyeon 
270985050421SPyun YongHyeon 	rfa->rfa_status = 0;
271085050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
271185050421SPyun YongHyeon 	rfa->actual_size = 0;
271285050421SPyun YongHyeon 
271385050421SPyun YongHyeon 	/*
271485050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
271585050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
271685050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
271785050421SPyun YongHyeon 	 * is also alignment-safe.
271885050421SPyun YongHyeon 	 */
271985050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
272085050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
272185050421SPyun YongHyeon 
2722a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
272385050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2724a17c678eSDavid Greenman }
2725a17c678eSDavid Greenman 
2726f1928b0cSKevin Lo static int
2727f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2728dccee1a1SDavid Greenman {
2729f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2730dccee1a1SDavid Greenman 	int count = 10000;
27316ebc3153SDavid Greenman 	int value;
2732dccee1a1SDavid Greenman 
2733ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2734ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2735dccee1a1SDavid Greenman 
2736ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2737ba8c6fd5SDavid Greenman 	    && count--)
27386ebc3153SDavid Greenman 		DELAY(10);
2739dccee1a1SDavid Greenman 
2740dccee1a1SDavid Greenman 	if (count <= 0)
2741f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2742dccee1a1SDavid Greenman 
27436ebc3153SDavid Greenman 	return (value & 0xffff);
2744dccee1a1SDavid Greenman }
2745dccee1a1SDavid Greenman 
274616ec4b00SWarner Losh static int
2747f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2748dccee1a1SDavid Greenman {
2749f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2750dccee1a1SDavid Greenman 	int count = 10000;
2751dccee1a1SDavid Greenman 
2752ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2753ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2754ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2755dccee1a1SDavid Greenman 
2756ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2757ba8c6fd5SDavid Greenman 	    count--)
27586ebc3153SDavid Greenman 		DELAY(10);
2759dccee1a1SDavid Greenman 
2760dccee1a1SDavid Greenman 	if (count <= 0)
2761f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
276216ec4b00SWarner Losh 	return (0);
2763dccee1a1SDavid Greenman }
2764dccee1a1SDavid Greenman 
27651845b5c3SMarius Strobl static void
27661845b5c3SMarius Strobl fxp_miibus_statchg(device_t dev)
27671845b5c3SMarius Strobl {
27681845b5c3SMarius Strobl 	struct fxp_softc *sc;
27691845b5c3SMarius Strobl 	struct mii_data *mii;
27701845b5c3SMarius Strobl 	struct ifnet *ifp;
27711845b5c3SMarius Strobl 
27721845b5c3SMarius Strobl 	sc = device_get_softc(dev);
27731845b5c3SMarius Strobl 	mii = device_get_softc(sc->miibus);
27741845b5c3SMarius Strobl 	ifp = sc->ifp;
27751845b5c3SMarius Strobl 	if (mii == NULL || ifp == NULL ||
27761845b5c3SMarius Strobl 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
27771845b5c3SMarius Strobl 	    (mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) !=
27781845b5c3SMarius Strobl 	    (IFM_AVALID | IFM_ACTIVE))
27791845b5c3SMarius Strobl 		return;
27801845b5c3SMarius Strobl 
27811845b5c3SMarius Strobl 	/*
27821845b5c3SMarius Strobl 	 * Call fxp_init_body in order to adjust the flow control settings.
27831845b5c3SMarius Strobl 	 * Note that the 82557 doesn't support hardware flow control.
27841845b5c3SMarius Strobl 	 */
27851845b5c3SMarius Strobl 	if (sc->revision == FXP_REV_82557)
27861845b5c3SMarius Strobl 		return;
27871845b5c3SMarius Strobl 	fxp_init_body(sc, 0);
27881845b5c3SMarius Strobl }
27891845b5c3SMarius Strobl 
2790dccee1a1SDavid Greenman static int
2791f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2792a17c678eSDavid Greenman {
27939b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2794a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2795f7788e8eSJonathan Lemon 	struct mii_data *mii;
279660bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2797a17c678eSDavid Greenman 
2798a17c678eSDavid Greenman 	switch (command) {
2799a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
28003212724cSJohn Baldwin 		FXP_LOCK(sc);
2801a17c678eSDavid Greenman 		/*
2802a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2803a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2804a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2805a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2806a17c678eSDavid Greenman 		 */
2807a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
28086b24912cSPyun YongHyeon 			if (((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) &&
28096b24912cSPyun YongHyeon 			    ((ifp->if_flags ^ sc->if_flags) &
28106b24912cSPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI | IFF_LINK0)) != 0)
28111845b5c3SMarius Strobl 				fxp_init_body(sc, 1);
28126b24912cSPyun YongHyeon 			else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
28131845b5c3SMarius Strobl 				fxp_init_body(sc, 1);
2814a17c678eSDavid Greenman 		} else {
28156b24912cSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
28164a5f1499SDavid Greenman 				fxp_stop(sc);
2817a17c678eSDavid Greenman 		}
28186b24912cSPyun YongHyeon 		sc->if_flags = ifp->if_flags;
28193212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2820a17c678eSDavid Greenman 		break;
2821a17c678eSDavid Greenman 
2822a17c678eSDavid Greenman 	case SIOCADDMULTI:
2823a17c678eSDavid Greenman 	case SIOCDELMULTI:
2824f6ff7180SPyun YongHyeon 		FXP_LOCK(sc);
28256b24912cSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2826f6ff7180SPyun YongHyeon 			fxp_init_body(sc, 0);
2827f6ff7180SPyun YongHyeon 		FXP_UNLOCK(sc);
2828ba8c6fd5SDavid Greenman 		break;
2829ba8c6fd5SDavid Greenman 
2830ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2831ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2832f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2833f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2834f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2835f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2836f7788e8eSJonathan Lemon 		} else {
2837ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2838f7788e8eSJonathan Lemon 		}
2839a17c678eSDavid Greenman 		break;
2840a17c678eSDavid Greenman 
2841fb917226SRuslan Ermilov 	case SIOCSIFCAP:
284260bb79ebSPyun YongHyeon 		reinit = 0;
28438ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
284440929967SGleb Smirnoff #ifdef DEVICE_POLLING
284540929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
284640929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
284740929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
284840929967SGleb Smirnoff 				if (error)
284940929967SGleb Smirnoff 					return(error);
285040929967SGleb Smirnoff 				FXP_LOCK(sc);
285140929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
285240929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
285340929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
285440929967SGleb Smirnoff 				FXP_UNLOCK(sc);
285540929967SGleb Smirnoff 			} else {
285640929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
285740929967SGleb Smirnoff 				/* Enable interrupts in any case */
285840929967SGleb Smirnoff 				FXP_LOCK(sc);
285940929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
286040929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
286140929967SGleb Smirnoff 				FXP_UNLOCK(sc);
286240929967SGleb Smirnoff 			}
286340929967SGleb Smirnoff 		}
286440929967SGleb Smirnoff #endif
286540929967SGleb Smirnoff 		FXP_LOCK(sc);
286660bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
286760bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
286860bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
286960bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
287060bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
287160bb79ebSPyun YongHyeon 			else
287260bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
287360bb79ebSPyun YongHyeon 		}
287460bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2875f13075afSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
287660bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2877f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2878f13075afSPyun YongHyeon 				reinit++;
2879f13075afSPyun YongHyeon 		}
2880c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
2881c21e84e4SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2882c21e84e4SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2883c21e84e4SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
2884c21e84e4SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2885c21e84e4SPyun YongHyeon 			else
2886c21e84e4SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2887c21e84e4SPyun YongHyeon 		}
28887137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
28897137cea0SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
28907137cea0SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
289160bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
289260bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
28938ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
28948ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
28958ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
28968ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
28978ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
28988ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
28998ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
290060bb79ebSPyun YongHyeon 				reinit++;
290160bb79ebSPyun YongHyeon 		}
2902713ca255SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2903713ca255SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2904713ca255SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2905713ca255SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
2906713ca255SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
2907713ca255SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
2908bd4fa9d9SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2909bd4fa9d9SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2910bd4fa9d9SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2911713ca255SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2912713ca255SPyun YongHyeon 				ifp->if_capenable &=
2913713ca255SPyun YongHyeon 				    ~(IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
2914bd4fa9d9SPyun YongHyeon 			reinit++;
2915bd4fa9d9SPyun YongHyeon 		}
2916bd4fa9d9SPyun YongHyeon 		if (reinit > 0 && ifp->if_flags & IFF_UP)
29171845b5c3SMarius Strobl 			fxp_init_body(sc, 1);
29183212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2919bd4fa9d9SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
2920fb917226SRuslan Ermilov 		break;
2921fb917226SRuslan Ermilov 
2922a17c678eSDavid Greenman 	default:
2923673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2924a17c678eSDavid Greenman 	}
2925a17c678eSDavid Greenman 	return (error);
2926a17c678eSDavid Greenman }
2927397f9dfeSDavid Greenman 
2928397f9dfeSDavid Greenman /*
292909882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
293009882363SJonathan Lemon  */
293109882363SJonathan Lemon static int
293209882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
293309882363SJonathan Lemon {
293409882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2935fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
293609882363SJonathan Lemon 	struct ifmultiaddr *ifma;
293709882363SJonathan Lemon 	int nmcasts;
293809882363SJonathan Lemon 
293909882363SJonathan Lemon 	nmcasts = 0;
29406b24912cSPyun YongHyeon 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2941eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
294209882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
294309882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
294409882363SJonathan Lemon 				continue;
294509882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
29466b24912cSPyun YongHyeon 				ifp->if_flags |= IFF_ALLMULTI;
294709882363SJonathan Lemon 				nmcasts = 0;
294809882363SJonathan Lemon 				break;
294909882363SJonathan Lemon 			}
295009882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2951bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
295209882363SJonathan Lemon 			nmcasts++;
295309882363SJonathan Lemon 		}
2954eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
295509882363SJonathan Lemon 	}
2956bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
295709882363SJonathan Lemon 	return (nmcasts);
295809882363SJonathan Lemon }
295909882363SJonathan Lemon 
296009882363SJonathan Lemon /*
2961397f9dfeSDavid Greenman  * Program the multicast filter.
2962397f9dfeSDavid Greenman  *
2963397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2964397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
29653114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2966397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2967dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2968397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2969397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2970397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2971397f9dfeSDavid Greenman  */
2972397f9dfeSDavid Greenman static void
2973f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2974397f9dfeSDavid Greenman {
29756b24912cSPyun YongHyeon 	struct fxp_cb_mcs *mcsp;
29767dced78aSDavid Greenman 	int count;
2977397f9dfeSDavid Greenman 
297867fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
29793114fdb4SDavid Greenman 
29806b24912cSPyun YongHyeon 	mcsp = sc->mcsp;
2981397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
29826b24912cSPyun YongHyeon 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
29836b24912cSPyun YongHyeon 	mcsp->link_addr = 0xffffffff;
29846b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
2985397f9dfeSDavid Greenman 
2986397f9dfeSDavid Greenman 	/*
29876b24912cSPyun YongHyeon 	 * Wait until command unit is idle. This should never be the
29886b24912cSPyun YongHyeon 	 * case when nothing is queued, but make sure anyway.
2989397f9dfeSDavid Greenman 	 */
29907dced78aSDavid Greenman 	count = 100;
29916b24912cSPyun YongHyeon 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) !=
29926b24912cSPyun YongHyeon 	    FXP_SCB_CUS_IDLE && --count)
29937dced78aSDavid Greenman 		DELAY(10);
29947dced78aSDavid Greenman 	if (count == 0) {
2995f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
29967dced78aSDavid Greenman 		return;
29977dced78aSDavid Greenman 	}
2998397f9dfeSDavid Greenman 
2999397f9dfeSDavid Greenman 	/*
3000397f9dfeSDavid Greenman 	 * Start the multicast setup command.
3001397f9dfeSDavid Greenman 	 */
3002397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
3003a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
3004a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3005b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
30062e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
30076b24912cSPyun YongHyeon 	/* ...and wait for it to complete. */
30086b24912cSPyun YongHyeon 	fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
3009397f9dfeSDavid Greenman }
301072a32a26SJonathan Lemon 
301174d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
301274d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
301374d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
301474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
3015e8449b79SPyun YongHyeon #ifdef notyet
301674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
301774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
3018e8449b79SPyun YongHyeon #endif
3019de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
302072a32a26SJonathan Lemon 
302174d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
302272a32a26SJonathan Lemon 
3023e0fe5c6dSMarius Strobl static const struct ucode {
302474d1ed23SMaxime Henrion 	uint32_t	revision;
302574d1ed23SMaxime Henrion 	uint32_t	*ucode;
302672a32a26SJonathan Lemon 	int		length;
302772a32a26SJonathan Lemon 	u_short		int_delay_offset;
302872a32a26SJonathan Lemon 	u_short		bundle_max_offset;
3029e0fe5c6dSMarius Strobl } const ucode_table[] = {
303072a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
303172a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
303272a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
303372a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
303472a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
303572a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
3036e8449b79SPyun YongHyeon #ifdef notyet
303772a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
303872a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
303972a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
304072a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
3041e8449b79SPyun YongHyeon #endif
3042507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
3043de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
3044*50df388dSPyun YongHyeon 	{ FXP_REV_82551_10, UCODE(fxp_ucode_d102e),
3045*50df388dSPyun YongHyeon 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
304672a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
304772a32a26SJonathan Lemon };
304872a32a26SJonathan Lemon 
304972a32a26SJonathan Lemon static void
305072a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
305172a32a26SJonathan Lemon {
3052e0fe5c6dSMarius Strobl 	const struct ucode *uc;
305372a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
305494a4f968SPyun YongHyeon 	int i;
305572a32a26SJonathan Lemon 
305672a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
305772a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
305872a32a26SJonathan Lemon 			break;
305972a32a26SJonathan Lemon 	if (uc->ucode == NULL)
306072a32a26SJonathan Lemon 		return;
3061b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
306272a32a26SJonathan Lemon 	cbp->cb_status = 0;
306383e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
306483e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
306594a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
306694a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
306772a32a26SJonathan Lemon 	if (uc->int_delay_offset)
306874d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
306983e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
307072a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
307174d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
307283e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
307372a32a26SJonathan Lemon 	/*
307472a32a26SJonathan Lemon 	 * Download the ucode to the chip.
307572a32a26SJonathan Lemon 	 */
307672a32a26SJonathan Lemon 	fxp_scb_wait(sc);
30775986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
30785986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3079b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
308072a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
308172a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
3082209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
308372a32a26SJonathan Lemon 	device_printf(sc->dev,
308472a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
308572a32a26SJonathan Lemon 	    sc->tunable_int_delay,
308672a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
308772a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
308872a32a26SJonathan Lemon }
308972a32a26SJonathan Lemon 
30908da9c507SPyun YongHyeon #define FXP_SYSCTL_STAT_ADD(c, h, n, p, d)	\
30918da9c507SPyun YongHyeon 	SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
30928da9c507SPyun YongHyeon 
30938da9c507SPyun YongHyeon static void
30948da9c507SPyun YongHyeon fxp_sysctl_node(struct fxp_softc *sc)
30958da9c507SPyun YongHyeon {
30968da9c507SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
30978da9c507SPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
30988da9c507SPyun YongHyeon 	struct sysctl_oid *tree;
30998da9c507SPyun YongHyeon 	struct fxp_hwstats *hsp;
31008da9c507SPyun YongHyeon 
31018da9c507SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->dev);
31028da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
31038da9c507SPyun YongHyeon 
31048da9c507SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child,
31058da9c507SPyun YongHyeon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
31068da9c507SPyun YongHyeon 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
31078da9c507SPyun YongHyeon 	    "FXP driver receive interrupt microcode bundling delay");
31088da9c507SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child,
31098da9c507SPyun YongHyeon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
31108da9c507SPyun YongHyeon 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
31118da9c507SPyun YongHyeon 	    "FXP driver receive interrupt microcode bundle size limit");
31128da9c507SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, child,OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
31138da9c507SPyun YongHyeon 	    "FXP RNR events");
31148da9c507SPyun YongHyeon 
31158da9c507SPyun YongHyeon 	/*
31168da9c507SPyun YongHyeon 	 * Pull in device tunables.
31178da9c507SPyun YongHyeon 	 */
31188da9c507SPyun YongHyeon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
31198da9c507SPyun YongHyeon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
31208da9c507SPyun YongHyeon 	(void) resource_int_value(device_get_name(sc->dev),
31218da9c507SPyun YongHyeon 	    device_get_unit(sc->dev), "int_delay", &sc->tunable_int_delay);
31228da9c507SPyun YongHyeon 	(void) resource_int_value(device_get_name(sc->dev),
31238da9c507SPyun YongHyeon 	    device_get_unit(sc->dev), "bundle_max", &sc->tunable_bundle_max);
31248da9c507SPyun YongHyeon 	sc->rnr = 0;
31258da9c507SPyun YongHyeon 
31268da9c507SPyun YongHyeon 	hsp = &sc->fxp_hwstats;
31278da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
31288da9c507SPyun YongHyeon 	    NULL, "FXP statistics");
31298da9c507SPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
31308da9c507SPyun YongHyeon 
31318da9c507SPyun YongHyeon 	/* Rx MAC statistics. */
31328da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
31338da9c507SPyun YongHyeon 	    NULL, "Rx MAC statistics");
31348da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
31358da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "good_frames",
31368da9c507SPyun YongHyeon 	    &hsp->rx_good, "Good frames");
31378da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "crc_errors",
31388da9c507SPyun YongHyeon 	    &hsp->rx_crc_errors, "CRC errors");
31398da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "alignment_errors",
31408da9c507SPyun YongHyeon 	    &hsp->rx_alignment_errors, "Alignment errors");
31418da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "rnr_errors",
31428da9c507SPyun YongHyeon 	    &hsp->rx_rnr_errors, "RNR errors");
31438da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "overrun_errors",
31448da9c507SPyun YongHyeon 	    &hsp->rx_overrun_errors, "Overrun errors");
31458da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "cdt_errors",
31468da9c507SPyun YongHyeon 	    &hsp->rx_cdt_errors, "Collision detect errors");
31478da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "shortframes",
31488da9c507SPyun YongHyeon 	    &hsp->rx_shortframes, "Short frame errors");
31498da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4) {
31508da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "pause",
31518da9c507SPyun YongHyeon 		    &hsp->rx_pause, "Pause frames");
31528da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "controls",
31538da9c507SPyun YongHyeon 		    &hsp->rx_controls, "Unsupported control frames");
31548da9c507SPyun YongHyeon 	}
31558da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
31568da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "tco",
31578da9c507SPyun YongHyeon 		    &hsp->rx_tco, "TCO frames");
31588da9c507SPyun YongHyeon 
31598da9c507SPyun YongHyeon 	/* Tx MAC statistics. */
31608da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
31618da9c507SPyun YongHyeon 	    NULL, "Tx MAC statistics");
31628da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
31638da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "good_frames",
31648da9c507SPyun YongHyeon 	    &hsp->tx_good, "Good frames");
31658da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "maxcols",
31668da9c507SPyun YongHyeon 	    &hsp->tx_maxcols, "Maximum collisions errors");
31678da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "latecols",
31688da9c507SPyun YongHyeon 	    &hsp->tx_latecols, "Late collisions errors");
31698da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "underruns",
31708da9c507SPyun YongHyeon 	    &hsp->tx_underruns, "Underrun errors");
31718da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "lostcrs",
31728da9c507SPyun YongHyeon 	    &hsp->tx_lostcrs, "Lost carrier sense");
31738da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "deffered",
31748da9c507SPyun YongHyeon 	    &hsp->tx_deffered, "Deferred");
31758da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "single_collisions",
31768da9c507SPyun YongHyeon 	    &hsp->tx_single_collisions, "Single collisions");
31778da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "multiple_collisions",
31788da9c507SPyun YongHyeon 	    &hsp->tx_multiple_collisions, "Multiple collisions");
31798da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "total_collisions",
31808da9c507SPyun YongHyeon 	    &hsp->tx_total_collisions, "Total collisions");
31818da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4)
31828da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "pause",
31838da9c507SPyun YongHyeon 		    &hsp->tx_pause, "Pause frames");
31848da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
31858da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "tco",
31868da9c507SPyun YongHyeon 		    &hsp->tx_tco, "TCO frames");
31878da9c507SPyun YongHyeon }
31888da9c507SPyun YongHyeon 
31898da9c507SPyun YongHyeon #undef FXP_SYSCTL_STAT_ADD
31908da9c507SPyun YongHyeon 
319172a32a26SJonathan Lemon static int
319272a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
319372a32a26SJonathan Lemon {
319472a32a26SJonathan Lemon 	int error, value;
319572a32a26SJonathan Lemon 
319672a32a26SJonathan Lemon 	value = *(int *)arg1;
319772a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
319872a32a26SJonathan Lemon 	if (error || !req->newptr)
319972a32a26SJonathan Lemon 		return (error);
320072a32a26SJonathan Lemon 	if (value < low || value > high)
320172a32a26SJonathan Lemon 		return (EINVAL);
320272a32a26SJonathan Lemon 	*(int *)arg1 = value;
320372a32a26SJonathan Lemon 	return (0);
320472a32a26SJonathan Lemon }
320572a32a26SJonathan Lemon 
320672a32a26SJonathan Lemon /*
320772a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
320872a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
320972a32a26SJonathan Lemon  */
321072a32a26SJonathan Lemon static int
321172a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
321272a32a26SJonathan Lemon {
3213e0fe5c6dSMarius Strobl 
321472a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
321572a32a26SJonathan Lemon }
321672a32a26SJonathan Lemon 
321772a32a26SJonathan Lemon static int
321872a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
321972a32a26SJonathan Lemon {
3220e0fe5c6dSMarius Strobl 
322172a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
322272a32a26SJonathan Lemon }
3223