xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 7251782989820f799d9720f0415d724ea1be71a4)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28a17c678eSDavid Greenman  */
29a17c678eSDavid Greenman 
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
32aad970f1SDavid E. O'Brien 
33a17c678eSDavid Greenman /*
34ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
35a17c678eSDavid Greenman  */
36a17c678eSDavid Greenman 
37f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
38f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
39f0796cd2SGleb Smirnoff #endif
40f0796cd2SGleb Smirnoff 
41a17c678eSDavid Greenman #include <sys/param.h>
42a17c678eSDavid Greenman #include <sys/systm.h>
438fae3bd4SPyun YongHyeon #include <sys/bus.h>
4483e6547dSMaxime Henrion #include <sys/endian.h>
45a17c678eSDavid Greenman #include <sys/kernel.h>
468fae3bd4SPyun YongHyeon #include <sys/mbuf.h>
476d7e1582SPyun YongHyeon #include <sys/lock.h>
48fe12f24bSPoul-Henning Kamp #include <sys/module.h>
496d7e1582SPyun YongHyeon #include <sys/mutex.h>
508fae3bd4SPyun YongHyeon #include <sys/rman.h>
514458ac71SBruce Evans #include <sys/socket.h>
528fae3bd4SPyun YongHyeon #include <sys/sockio.h>
5372a32a26SJonathan Lemon #include <sys/sysctl.h>
54a17c678eSDavid Greenman 
558fae3bd4SPyun YongHyeon #include <net/bpf.h>
568fae3bd4SPyun YongHyeon #include <net/ethernet.h>
57a17c678eSDavid Greenman #include <net/if.h>
588fae3bd4SPyun YongHyeon #include <net/if_arp.h>
59397f9dfeSDavid Greenman #include <net/if_dl.h>
60ba8c6fd5SDavid Greenman #include <net/if_media.h>
61e8c8b728SJonathan Lemon #include <net/if_types.h>
62e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
63e8c8b728SJonathan Lemon 
64c8bca6dcSBill Paul #include <netinet/in.h>
65c8bca6dcSBill Paul #include <netinet/in_systm.h>
66c8bca6dcSBill Paul #include <netinet/ip.h>
67f13075afSPyun YongHyeon #include <netinet/tcp.h>
68f13075afSPyun YongHyeon #include <netinet/udp.h>
69f13075afSPyun YongHyeon 
70f13075afSPyun YongHyeon #include <machine/bus.h>
71c8bca6dcSBill Paul #include <machine/in_cksum.h>
72f13075afSPyun YongHyeon #include <machine/resource.h>
73c8bca6dcSBill Paul 
744fbd232cSWarner Losh #include <dev/pci/pcivar.h>
754fbd232cSWarner Losh #include <dev/pci/pcireg.h>		/* for PCIM_CMD_xxx */
76a17c678eSDavid Greenman 
77f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
78f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
79f7788e8eSJonathan Lemon 
80f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
81f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8272a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
83f7788e8eSJonathan Lemon 
84f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
85f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
86f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
87f7788e8eSJonathan Lemon #include "miibus_if.h"
884fc1dda9SAndrew Gallatin 
89ba8c6fd5SDavid Greenman /*
90ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
91ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
92ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
93ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
94ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
95ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
96ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
97ba8c6fd5SDavid Greenman  */
98ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
99ba8c6fd5SDavid Greenman 
100ba8c6fd5SDavid Greenman /*
101f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
102f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
103f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
104f7788e8eSJonathan Lemon  */
105f7788e8eSJonathan Lemon static int tx_threshold = 64;
106f7788e8eSJonathan Lemon 
107f7788e8eSJonathan Lemon /*
108f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
109*72517829SPyun YongHyeon  * must be one or must be zero.  Set up a template for these bits.
110*72517829SPyun YongHyeon  * The actual configuration is performed in fxp_init.
111f7788e8eSJonathan Lemon  *
112f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
113f7788e8eSJonathan Lemon  */
114f7788e8eSJonathan Lemon static u_char 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 */
139*72517829SPyun YongHyeon 	0x5,	/* 21 */
140*72517829SPyun YongHyeon 	0x0,	/* 22 */
141*72517829SPyun YongHyeon 	0x0,	/* 23 */
142*72517829SPyun YongHyeon 	0x0,	/* 24 */
143*72517829SPyun YongHyeon 	0x0,	/* 25 */
144*72517829SPyun YongHyeon 	0x0,	/* 26 */
145*72517829SPyun YongHyeon 	0x0,	/* 27 */
146*72517829SPyun YongHyeon 	0x0,	/* 28 */
147*72517829SPyun YongHyeon 	0x0,	/* 29 */
148*72517829SPyun YongHyeon 	0x0,	/* 30 */
149*72517829SPyun 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  */
158f7788e8eSJonathan Lemon static struct fxp_ident 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 
220b96ad4b2SPyun YongHyeon static 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);
2274953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
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);
26472a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
26572a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
26672a32a26SJonathan Lemon 			    int low, int high);
26772a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
26872a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
26928935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
27028935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
27128935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
27274d1ed23SMaxime Henrion     			    volatile uint16_t *status, bus_dma_tag_t dmat,
273209b07bcSMaxime Henrion 			    bus_dmamap_t map);
274f7788e8eSJonathan Lemon 
275f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
276f7788e8eSJonathan Lemon 	/* Device interface */
277f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
278f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
279f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
280f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
281f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
282f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
283f7788e8eSJonathan Lemon 
284f7788e8eSJonathan Lemon 	/* MII interface */
285f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
286f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
287f7788e8eSJonathan Lemon 
288f7788e8eSJonathan Lemon 	{ 0, 0 }
289f7788e8eSJonathan Lemon };
290f7788e8eSJonathan Lemon 
291f7788e8eSJonathan Lemon static driver_t fxp_driver = {
292f7788e8eSJonathan Lemon 	"fxp",
293f7788e8eSJonathan Lemon 	fxp_methods,
294f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
295f7788e8eSJonathan Lemon };
296f7788e8eSJonathan Lemon 
297f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
298f7788e8eSJonathan Lemon 
299f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
300f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
301f7788e8eSJonathan Lemon 
30205bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
30305bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
30405bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30505bd8c22SMaxime Henrion 	{ -1, 0 }
30605bd8c22SMaxime Henrion };
30705bd8c22SMaxime Henrion 
30805bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
30905bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
31005bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
31105bd8c22SMaxime Henrion 	{ -1, 0 }
31205bd8c22SMaxime Henrion };
31305bd8c22SMaxime Henrion 
314f7788e8eSJonathan Lemon /*
315dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
316dfe61cf1SDavid Greenman  * completed).
317dfe61cf1SDavid Greenman  */
31828935f27SMaxime Henrion static void
319f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
320a17c678eSDavid Greenman {
3213cf09dd1SMarcel Moolenaar 	union {
3223cf09dd1SMarcel Moolenaar 		uint16_t w;
3233cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3243cf09dd1SMarcel Moolenaar 	} flowctl;
325a17c678eSDavid Greenman 	int i = 10000;
326a17c678eSDavid Greenman 
3277dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3287dced78aSDavid Greenman 		DELAY(2);
3293cf09dd1SMarcel Moolenaar 	if (i == 0) {
3303cf09dd1SMarcel Moolenaar 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL);
3313cf09dd1SMarcel Moolenaar 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL + 1);
33200c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
333e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
334e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3353cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3363cf09dd1SMarcel Moolenaar 	}
3377dced78aSDavid Greenman }
3387dced78aSDavid Greenman 
33928935f27SMaxime Henrion static void
3402e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3412e2b8238SJonathan Lemon {
3422e2b8238SJonathan Lemon 
3432e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3442e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3452e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3462e2b8238SJonathan Lemon 	}
3472e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3482e2b8238SJonathan Lemon }
3492e2b8238SJonathan Lemon 
35028935f27SMaxime Henrion static void
35174d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
352209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3537dced78aSDavid Greenman {
3545986d0d2SPyun YongHyeon 	int i;
3557dced78aSDavid Greenman 
3565986d0d2SPyun YongHyeon 	for (i = 10000; i > 0; i--) {
3577dced78aSDavid Greenman 		DELAY(2);
3585986d0d2SPyun YongHyeon 		bus_dmamap_sync(dmat, map,
3595986d0d2SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3605986d0d2SPyun YongHyeon 		if ((le16toh(*status) & FXP_CB_STATUS_C) != 0)
3615986d0d2SPyun YongHyeon 			break;
362209b07bcSMaxime Henrion 	}
3637dced78aSDavid Greenman 	if (i == 0)
364f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
365a17c678eSDavid Greenman }
366a17c678eSDavid Greenman 
367b96ad4b2SPyun YongHyeon static struct fxp_ident *
368b96ad4b2SPyun YongHyeon fxp_find_ident(device_t dev)
369a17c678eSDavid Greenman {
37074d1ed23SMaxime Henrion 	uint16_t devid;
37174d1ed23SMaxime Henrion 	uint8_t revid;
372f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
373f7788e8eSJonathan Lemon 
37455ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
375f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
376f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
377f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
378f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
379f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
380b96ad4b2SPyun YongHyeon 				return (ident);
381b96ad4b2SPyun YongHyeon 			}
382b96ad4b2SPyun YongHyeon 		}
383b96ad4b2SPyun YongHyeon 	}
384b96ad4b2SPyun YongHyeon 	return (NULL);
385b96ad4b2SPyun YongHyeon }
386b96ad4b2SPyun YongHyeon 
387b96ad4b2SPyun YongHyeon /*
388b96ad4b2SPyun YongHyeon  * Return identification string if this device is ours.
389b96ad4b2SPyun YongHyeon  */
390b96ad4b2SPyun YongHyeon static int
391b96ad4b2SPyun YongHyeon fxp_probe(device_t dev)
392b96ad4b2SPyun YongHyeon {
393b96ad4b2SPyun YongHyeon 	struct fxp_ident *ident;
394b96ad4b2SPyun YongHyeon 
395b96ad4b2SPyun YongHyeon 	ident = fxp_find_ident(dev);
396b96ad4b2SPyun YongHyeon 	if (ident != NULL) {
397f7788e8eSJonathan Lemon 		device_set_desc(dev, ident->name);
398538565c4SWarner Losh 		return (BUS_PROBE_DEFAULT);
39955ce7b51SDavid Greenman 	}
400f7788e8eSJonathan Lemon 	return (ENXIO);
4016182fdbdSPeter Wemm }
4026182fdbdSPeter Wemm 
403b2badf02SMaxime Henrion static void
404b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
405b2badf02SMaxime Henrion {
40674d1ed23SMaxime Henrion 	uint32_t *addr;
407b2badf02SMaxime Henrion 
408b2badf02SMaxime Henrion 	if (error)
409b2badf02SMaxime Henrion 		return;
410b2badf02SMaxime Henrion 
411b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
412b2badf02SMaxime Henrion 	addr = arg;
413b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
414b2badf02SMaxime Henrion }
415b2badf02SMaxime Henrion 
4166182fdbdSPeter Wemm static int
4176182fdbdSPeter Wemm fxp_attach(device_t dev)
418a17c678eSDavid Greenman {
4196720ebccSMaxime Henrion 	struct fxp_softc *sc;
4206720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4216720ebccSMaxime Henrion 	struct fxp_tx *txp;
422b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4236720ebccSMaxime Henrion 	struct ifnet *ifp;
42474d1ed23SMaxime Henrion 	uint32_t val;
42574d1ed23SMaxime Henrion 	uint16_t data, myea[ETHER_ADDR_LEN / 2];
426fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4277137cea0SPyun YongHyeon 	int i, pmc, prefer_iomap;
4283212724cSJohn Baldwin 	int error;
429a17c678eSDavid Greenman 
4306720ebccSMaxime Henrion 	error = 0;
4316720ebccSMaxime Henrion 	sc = device_get_softc(dev);
432f7788e8eSJonathan Lemon 	sc->dev = dev;
4336008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4344953bccaSNate Lawson 	    MTX_DEF);
4353212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4364953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4374953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
438a17c678eSDavid Greenman 
4397ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4407ba33d82SBrooks Davis 	if (ifp == NULL) {
4417ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4427ba33d82SBrooks Davis 		error = ENOSPC;
4437ba33d82SBrooks Davis 		goto fail;
4447ba33d82SBrooks Davis 	}
4457ba33d82SBrooks Davis 
446dfe61cf1SDavid Greenman 	/*
4472bce79a2SMaxim Sobolev 	 * Enable bus mastering.
448df373873SWes Peters 	 */
449cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4509fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
45179495006SWarner Losh 
452df373873SWes Peters 	/*
4539fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4549fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4559fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
456dfe61cf1SDavid Greenman 	 */
4572a05a4ebSMatt Jacob 	prefer_iomap = 0;
45805bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
45905bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
46005bd8c22SMaxime Henrion 	if (prefer_iomap)
46105bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
46205bd8c22SMaxime Henrion 	else
46305bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4649fa6ccfbSMatt Jacob 
46505bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
46605bd8c22SMaxime Henrion 	if (error) {
46705bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
46805bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
46905bd8c22SMaxime Henrion 		else
47005bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
47105bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4729fa6ccfbSMatt Jacob 	}
47305bd8c22SMaxime Henrion 	if (error) {
47405bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4756182fdbdSPeter Wemm 		error = ENXIO;
476a17c678eSDavid Greenman 		goto fail;
477a17c678eSDavid Greenman 	}
47805bd8c22SMaxime Henrion 
4799fa6ccfbSMatt Jacob 	if (bootverbose) {
4809fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
48105bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4826182fdbdSPeter Wemm 	}
4836182fdbdSPeter Wemm 
484f7788e8eSJonathan Lemon 	/*
485a996f023SPyun YongHyeon 	 * Put CU/RU idle state and prepare full reset.
486f7788e8eSJonathan Lemon 	 */
487f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
488f7788e8eSJonathan Lemon 	DELAY(10);
489a996f023SPyun YongHyeon 	/* Full reset and disable interrupts. */
490a996f023SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
491a996f023SPyun YongHyeon 	DELAY(10);
492a996f023SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
493f7788e8eSJonathan Lemon 
494f7788e8eSJonathan Lemon 	/*
495f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
496f7788e8eSJonathan Lemon 	 */
497f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
498f7788e8eSJonathan Lemon 
499f7788e8eSJonathan Lemon 	/*
50093b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
50193b6e2e6SMaxime Henrion 	 */
502b96ad4b2SPyun YongHyeon 	sc->ident = fxp_find_ident(dev);
503b96ad4b2SPyun YongHyeon 	if (sc->ident->ich > 0) {
504b96ad4b2SPyun YongHyeon 		/* Assume ICH controllers are 82559. */
505b96ad4b2SPyun YongHyeon 		sc->revision = FXP_REV_82559_A0;
506b96ad4b2SPyun YongHyeon 	} else {
50793b6e2e6SMaxime Henrion 		fxp_read_eeprom(sc, &data, 5, 1);
50893b6e2e6SMaxime Henrion 		if ((data >> 8) == 1)
50993b6e2e6SMaxime Henrion 			sc->revision = FXP_REV_82557;
51093b6e2e6SMaxime Henrion 		else
51193b6e2e6SMaxime Henrion 			sc->revision = pci_get_revid(dev);
512b96ad4b2SPyun YongHyeon 	}
51393b6e2e6SMaxime Henrion 
51493b6e2e6SMaxime Henrion 	/*
5157137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
5167137cea0SPyun YongHyeon 	 */
5177137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
5187137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
5197137cea0SPyun YongHyeon 		fxp_read_eeprom(sc, &data, 10, 1);
5207137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
5217137cea0SPyun YongHyeon 		    pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0)
5227137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
5237137cea0SPyun YongHyeon 	}
5247137cea0SPyun YongHyeon 
52543d8b117SPyun YongHyeon 	/* Receiver lock-up workaround detection. */
52643d8b117SPyun YongHyeon 	fxp_read_eeprom(sc, &data, 3, 1);
52743d8b117SPyun YongHyeon 	if ((data & 0x03) != 0x03) {
52843d8b117SPyun YongHyeon 		sc->flags |= FXP_FLAG_RXBUG;
52943d8b117SPyun YongHyeon 		device_printf(dev, "Enabling Rx lock-up workaround\n");
53043d8b117SPyun YongHyeon 	}
53143d8b117SPyun YongHyeon 
5327137cea0SPyun YongHyeon 	/*
5333bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
534f7788e8eSJonathan Lemon 	 */
535f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
53693b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5374ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
538dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
539f7788e8eSJonathan Lemon 
5400f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5410f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
54250a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
543858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
54472a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
5450f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
5460f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
54750a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
548858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
54972a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
5500f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5510f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5520f1db1d6SMaxime Henrion 	    OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
5530f1db1d6SMaxime Henrion 	    "FXP RNR events");
5540f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5550f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5560f1db1d6SMaxime Henrion 	    OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0,
5570f1db1d6SMaxime Henrion 	    "FXP flow control disabled");
55872a32a26SJonathan Lemon 
55972a32a26SJonathan Lemon 	/*
56072a32a26SJonathan Lemon 	 * Pull in device tunables.
56172a32a26SJonathan Lemon 	 */
56272a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
56372a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
56403edfff3SRobert Watson 	sc->tunable_noflow = 1;
56572a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
56672a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
56772a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
56872a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
5690f1db1d6SMaxime Henrion 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
5700f1db1d6SMaxime Henrion 	    "noflow", &sc->tunable_noflow);
5710f1db1d6SMaxime Henrion 	sc->rnr = 0;
57272a32a26SJonathan Lemon 
57372a32a26SJonathan Lemon 	/*
5742e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
57500c4116bSJonathan Lemon 	 *
57672a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
57772a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
57872a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
57900c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
58000c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
58100c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
58200c4116bSJonathan Lemon 	 *
58300c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5842e2b8238SJonathan Lemon 	 */
585b96ad4b2SPyun YongHyeon 	if ((sc->ident->ich >= 2 && sc->ident->ich <= 3) ||
586b96ad4b2SPyun YongHyeon 	    (sc->ident->ich == 0 && sc->revision >= FXP_REV_82559_A0)) {
58700c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
58800c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
58974d1ed23SMaxime Henrion 			uint16_t cksum;
59000c4116bSJonathan Lemon 			int i;
59100c4116bSJonathan Lemon 
59200c4116bSJonathan Lemon 			device_printf(dev,
593001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
59400c4116bSJonathan Lemon 			data &= ~0x02;
59500c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
59600c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
59700c4116bSJonathan Lemon 			cksum = 0;
59800c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
59900c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
60000c4116bSJonathan Lemon 				cksum += data;
60100c4116bSJonathan Lemon 			}
60200c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
60300c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
60400c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
60500c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
60600c4116bSJonathan Lemon 			device_printf(dev,
60700c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
60800c4116bSJonathan Lemon 			    i, data, cksum);
60900c4116bSJonathan Lemon #if 1
61000c4116bSJonathan Lemon 			/*
61100c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
61200c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
61300c4116bSJonathan Lemon 			 */
6142e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
61500c4116bSJonathan Lemon #endif
61600c4116bSJonathan Lemon 		}
61700c4116bSJonathan Lemon 	}
6182e2b8238SJonathan Lemon 
6192e2b8238SJonathan Lemon 	/*
6203bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
6213bd07cfdSJonathan Lemon 	 */
62272a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
6233bd07cfdSJonathan Lemon 		/*
62474396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
62574396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
62674396a0aSJonathan Lemon 		 * the board to turn on MWI.
6273bd07cfdSJonathan Lemon 		 */
62874396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
62974396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
6303bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
6313bd07cfdSJonathan Lemon 
6323bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
6333bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
63444e0bc11SYaroslav Tykhiy 
63544e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
63644e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
63744e0bc11SYaroslav Tykhiy 	} else {
63844e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
63944e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6403bd07cfdSJonathan Lemon 	}
6413bd07cfdSJonathan Lemon 
642f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
643829b278eSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0) {
644829b278eSPyun YongHyeon 		/* 82559ER does not support Rx checksum offloading. */
645829b278eSPyun YongHyeon 		if (sc->ident->devid != 0x1209)
646f13075afSPyun YongHyeon 			sc->flags |= FXP_FLAG_82559_RXCSUM;
647829b278eSPyun YongHyeon 	}
6483bd07cfdSJonathan Lemon 	/*
649c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
650c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
651c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
652c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
653c8bca6dcSBill Paul 	 */
654507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
655507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
656507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
657c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
658c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
659c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
660f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
661f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
662c8bca6dcSBill Paul 	} else {
663c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
664c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
665c8bca6dcSBill Paul 	}
666c8bca6dcSBill Paul 
667c8bca6dcSBill Paul 	/*
668b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
669b2badf02SMaxime Henrion 	 */
67040c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
671c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
672c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
67340c20505SMaxime Henrion 		sc->maxtxseg--;
674c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
675c21e84e4SPyun YongHyeon 	}
676c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
677c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
678c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
679c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
680a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_txmtag);
681b2badf02SMaxime Henrion 	if (error) {
682a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TX DMA tag\n");
683a2057a72SPyun YongHyeon 		goto fail;
684a2057a72SPyun YongHyeon 	}
685a2057a72SPyun YongHyeon 
686a2057a72SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
687a2057a72SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
688a2057a72SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0,
689a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_rxmtag);
690a2057a72SPyun YongHyeon 	if (error) {
691a2057a72SPyun YongHyeon 		device_printf(dev, "could not create RX DMA tag\n");
692b2badf02SMaxime Henrion 		goto fail;
693b2badf02SMaxime Henrion 	}
694b2badf02SMaxime Henrion 
695c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
696c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
697c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
698c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
699b2badf02SMaxime Henrion 	if (error) {
700a2057a72SPyun YongHyeon 		device_printf(dev, "could not create stats DMA tag\n");
701b2badf02SMaxime Henrion 		goto fail;
702b2badf02SMaxime Henrion 	}
703b2badf02SMaxime Henrion 
704b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
705aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap);
706a2057a72SPyun YongHyeon 	if (error) {
707a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate stats DMA memory\n");
7084953bccaSNate Lawson 		goto fail;
709a2057a72SPyun YongHyeon 	}
710b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
711b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
712b2badf02SMaxime Henrion 	if (error) {
713a2057a72SPyun YongHyeon 		device_printf(dev, "could not load the stats DMA buffer\n");
714b2badf02SMaxime Henrion 		goto fail;
715b2badf02SMaxime Henrion 	}
716b2badf02SMaxime Henrion 
717c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
718c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
719c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
720c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
721b2badf02SMaxime Henrion 	if (error) {
722a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TxCB DMA tag\n");
723b2badf02SMaxime Henrion 		goto fail;
724b2badf02SMaxime Henrion 	}
725b2badf02SMaxime Henrion 
726b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
727aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map);
728a2057a72SPyun YongHyeon 	if (error) {
729a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate TxCB DMA memory\n");
7304953bccaSNate Lawson 		goto fail;
731a2057a72SPyun YongHyeon 	}
732b2badf02SMaxime Henrion 
733b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
734b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
735b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
736b2badf02SMaxime Henrion 	if (error) {
737a2057a72SPyun YongHyeon 		device_printf(dev, "could not load TxCB DMA buffer\n");
738b2badf02SMaxime Henrion 		goto fail;
739b2badf02SMaxime Henrion 	}
740b2badf02SMaxime Henrion 
741c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
742c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
743c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
744c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
745b2badf02SMaxime Henrion 	if (error) {
746a2057a72SPyun YongHyeon 		device_printf(dev,
747a2057a72SPyun YongHyeon 		    "could not create multicast setup DMA tag\n");
748b2badf02SMaxime Henrion 		goto fail;
749b2badf02SMaxime Henrion 	}
750b2badf02SMaxime Henrion 
751b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
752a2057a72SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->mcs_map);
753a2057a72SPyun YongHyeon 	if (error) {
754a2057a72SPyun YongHyeon 		device_printf(dev,
755a2057a72SPyun YongHyeon 		    "could not allocate multicast setup DMA memory\n");
7564953bccaSNate Lawson 		goto fail;
757a2057a72SPyun YongHyeon 	}
758b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
759b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
760b2badf02SMaxime Henrion 	if (error) {
761a2057a72SPyun YongHyeon 		device_printf(dev,
762a2057a72SPyun YongHyeon 		    "can't load the multicast setup DMA buffer\n");
763b2badf02SMaxime Henrion 		goto fail;
764b2badf02SMaxime Henrion 	}
765b2badf02SMaxime Henrion 
766b2badf02SMaxime Henrion 	/*
7676720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7686720ebccSMaxime Henrion 	 * the TX command blocks.
769b2badf02SMaxime Henrion 	 */
7706720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7716720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7724cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7736720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
774a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_txmtag, 0, &txp[i].tx_map);
775b2badf02SMaxime Henrion 		if (error) {
776b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
777b2badf02SMaxime Henrion 			goto fail;
778b2badf02SMaxime Henrion 		}
779b2badf02SMaxime Henrion 	}
780a2057a72SPyun YongHyeon 	error = bus_dmamap_create(sc->fxp_rxmtag, 0, &sc->spare_map);
781b2badf02SMaxime Henrion 	if (error) {
782b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
783b2badf02SMaxime Henrion 		goto fail;
784b2badf02SMaxime Henrion 	}
785b2badf02SMaxime Henrion 
786b2badf02SMaxime Henrion 	/*
787b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
788b2badf02SMaxime Henrion 	 */
789b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
790b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
791b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
792a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_rxmtag, 0, &rxp->rx_map);
793b2badf02SMaxime Henrion 		if (error) {
794b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
795b2badf02SMaxime Henrion 			goto fail;
796b2badf02SMaxime Henrion 		}
79785050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7984953bccaSNate Lawson 			error = ENOMEM;
7994953bccaSNate Lawson 			goto fail;
8004953bccaSNate Lawson 		}
80185050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
802b2badf02SMaxime Henrion 	}
803b2badf02SMaxime Henrion 
804b2badf02SMaxime Henrion 	/*
805f7788e8eSJonathan Lemon 	 * Read MAC address.
806f7788e8eSJonathan Lemon 	 */
80783e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
808fc74a9f9SBrooks Davis 	eaddr[0] = myea[0] & 0xff;
809fc74a9f9SBrooks Davis 	eaddr[1] = myea[0] >> 8;
810fc74a9f9SBrooks Davis 	eaddr[2] = myea[1] & 0xff;
811fc74a9f9SBrooks Davis 	eaddr[3] = myea[1] >> 8;
812fc74a9f9SBrooks Davis 	eaddr[4] = myea[2] & 0xff;
813fc74a9f9SBrooks Davis 	eaddr[5] = myea[2] >> 8;
814f7788e8eSJonathan Lemon 	if (bootverbose) {
8152e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
816f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
8172e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
8182e2b8238SJonathan Lemon 		    pci_get_revid(dev));
81972a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
82072a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
82172a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
822f7788e8eSJonathan Lemon 	}
823f7788e8eSJonathan Lemon 
824f7788e8eSJonathan Lemon 	/*
825f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
826f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
827f7788e8eSJonathan Lemon 	 *
828f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
829f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
830f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
831f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
832f7788e8eSJonathan Lemon 	 */
833f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
834f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
835f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
836f7788e8eSJonathan Lemon 	} else {
837f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
838f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
839f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
8406182fdbdSPeter Wemm 			error = ENXIO;
841ba8c6fd5SDavid Greenman 			goto fail;
842a17c678eSDavid Greenman 		}
843f7788e8eSJonathan Lemon 	}
844dccee1a1SDavid Greenman 
8459bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
846fb583156SDavid Greenman 	ifp->if_init = fxp_init;
847ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
848ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
849ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
850ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
851a17c678eSDavid Greenman 
8525fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
8535fe9116bSYaroslav Tykhiy 
854c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
855c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
856c21e84e4SPyun YongHyeon 		ifp->if_hwassist = FXP_CSUM_FEATURES | CSUM_TSO;
857c21e84e4SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSO4;
858c21e84e4SPyun YongHyeon 		ifp->if_capenable |= IFCAP_HWCSUM | IFCAP_TSO4;
859c8bca6dcSBill Paul 	}
860c8bca6dcSBill Paul 
861f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
862f13075afSPyun YongHyeon 		ifp->if_capabilities |= IFCAP_RXCSUM;
863f13075afSPyun YongHyeon 		ifp->if_capenable |= IFCAP_RXCSUM;
864f13075afSPyun YongHyeon 	}
865f13075afSPyun YongHyeon 
8667137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
8677137cea0SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL_MAGIC;
8687137cea0SPyun YongHyeon 		ifp->if_capenable |= IFCAP_WOL_MAGIC;
8697137cea0SPyun YongHyeon 	}
8707137cea0SPyun YongHyeon 
871fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
872fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
873fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
874fb917226SRuslan Ermilov #endif
875fb917226SRuslan Ermilov 
876dfe61cf1SDavid Greenman 	/*
8774953bccaSNate Lawson 	 * Attach the interface.
8784953bccaSNate Lawson 	 */
879fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
8804953bccaSNate Lawson 
8814953bccaSNate Lawson 	/*
882e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
8835fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
8845fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
885e8c8b728SJonathan Lemon 	 */
886e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
887673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
88844e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
889bd4fa9d9SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) {
890bd4fa9d9SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING |
891bd4fa9d9SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM;
892bd4fa9d9SPyun YongHyeon 		ifp->if_capenable |= IFCAP_VLAN_HWTAGGING |
893bd4fa9d9SPyun YongHyeon 		    IFCAP_VLAN_HWCSUM;
894bd4fa9d9SPyun YongHyeon 	}
895e8c8b728SJonathan Lemon 
896483b9871SDavid Greenman 	/*
8973114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8983114fdb4SDavid Greenman 	 * TX descriptors.
899483b9871SDavid Greenman 	 */
9007929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
9017929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
9027929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
9034a684684SDavid Greenman 
904201afb0eSMaxime Henrion 	/*
9054953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
906201afb0eSMaxime Henrion 	 */
90705bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
908ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
909201afb0eSMaxime Henrion 	if (error) {
910201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
911fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
912201afb0eSMaxime Henrion 		goto fail;
913201afb0eSMaxime Henrion 	}
914201afb0eSMaxime Henrion 
9157137cea0SPyun YongHyeon 	/*
9167137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
9177137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
9187137cea0SPyun YongHyeon 	 */
9197137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
9207137cea0SPyun YongHyeon 		FXP_LOCK(sc);
9217137cea0SPyun YongHyeon 		/* Clear wakeup events. */
922af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
9237137cea0SPyun YongHyeon 		fxp_init_body(sc);
9247137cea0SPyun YongHyeon 		fxp_stop(sc);
9257137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
9267137cea0SPyun YongHyeon 	}
9277137cea0SPyun YongHyeon 
928a17c678eSDavid Greenman fail:
9291b5a39d3SBrooks Davis 	if (error)
930f7788e8eSJonathan Lemon 		fxp_release(sc);
931f7788e8eSJonathan Lemon 	return (error);
932f7788e8eSJonathan Lemon }
933f7788e8eSJonathan Lemon 
934f7788e8eSJonathan Lemon /*
9354953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
9364953bccaSNate Lawson  * interrupt should already be torn down.
937f7788e8eSJonathan Lemon  */
938f7788e8eSJonathan Lemon static void
939f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
940f7788e8eSJonathan Lemon {
941b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
942b2badf02SMaxime Henrion 	struct fxp_tx *txp;
943b2badf02SMaxime Henrion 	int i;
944b2badf02SMaxime Henrion 
94567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
946670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
947670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
9484953bccaSNate Lawson 	if (sc->miibus)
9494953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
9504953bccaSNate Lawson 	bus_generic_detach(sc->dev);
9514953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
952b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
953b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
954b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
955b2badf02SMaxime Henrion 		    sc->cbl_map);
956b2badf02SMaxime Henrion 	}
957b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
958b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
959b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
960b2badf02SMaxime Henrion 	}
961b2badf02SMaxime Henrion 	if (sc->mcsp) {
962b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
963b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
964b2badf02SMaxime Henrion 	}
96505bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
966a2057a72SPyun YongHyeon 	if (sc->fxp_rxmtag) {
967b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
968b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
969b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
970a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
971b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
972a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
973b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
974b983c7b3SMaxime Henrion 			}
975a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_rxmtag, rxp->rx_map);
976b983c7b3SMaxime Henrion 		}
977a2057a72SPyun YongHyeon 		bus_dmamap_destroy(sc->fxp_rxmtag, sc->spare_map);
978a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_rxmtag);
979a2057a72SPyun YongHyeon 	}
980a2057a72SPyun YongHyeon 	if (sc->fxp_txmtag) {
981b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
982b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
983b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
984a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
985b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
986a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
987b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
988b983c7b3SMaxime Henrion 			}
989a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_txmtag, txp->tx_map);
990b983c7b3SMaxime Henrion 		}
991a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_txmtag);
992b983c7b3SMaxime Henrion 	}
993c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
994c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
995b2badf02SMaxime Henrion 	if (sc->cbl_tag)
996b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
997b2badf02SMaxime Henrion 	if (sc->mcs_tag)
998b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
999fc74a9f9SBrooks Davis 	if (sc->ifp)
1000fc74a9f9SBrooks Davis 		if_free(sc->ifp);
100172a32a26SJonathan Lemon 
10020f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
10036182fdbdSPeter Wemm }
10046182fdbdSPeter Wemm 
10056182fdbdSPeter Wemm /*
10066182fdbdSPeter Wemm  * Detach interface.
10076182fdbdSPeter Wemm  */
10086182fdbdSPeter Wemm static int
10096182fdbdSPeter Wemm fxp_detach(device_t dev)
10106182fdbdSPeter Wemm {
10116182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
10126182fdbdSPeter Wemm 
101340929967SGleb Smirnoff #ifdef DEVICE_POLLING
101440929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
101540929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
101640929967SGleb Smirnoff #endif
101740929967SGleb Smirnoff 
10184953bccaSNate Lawson 	FXP_LOCK(sc);
10196182fdbdSPeter Wemm 	/*
102032cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
102120f0c80fSMaxime Henrion 	 */
102220f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
102320f0c80fSMaxime Henrion 	fxp_stop(sc);
102432cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
10259eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
102620f0c80fSMaxime Henrion 
10276182fdbdSPeter Wemm 	/*
10283212724cSJohn Baldwin 	 * Close down routes etc.
10293212724cSJohn Baldwin 	 */
10303212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
10313212724cSJohn Baldwin 
10323212724cSJohn Baldwin 	/*
10334953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
10344953bccaSNate Lawson 	 * races with fxp_intr().
10356182fdbdSPeter Wemm 	 */
103605bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
10374953bccaSNate Lawson 	sc->ih = NULL;
10386182fdbdSPeter Wemm 
1039f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
1040f7788e8eSJonathan Lemon 	fxp_release(sc);
1041f7788e8eSJonathan Lemon 	return (0);
1042a17c678eSDavid Greenman }
1043a17c678eSDavid Greenman 
1044a17c678eSDavid Greenman /*
10454a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
1046a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
1047a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
1048a17c678eSDavid Greenman  */
10496182fdbdSPeter Wemm static int
10506182fdbdSPeter Wemm fxp_shutdown(device_t dev)
1051a17c678eSDavid Greenman {
10523212724cSJohn Baldwin 
10536182fdbdSPeter Wemm 	/*
10546182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
10556182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
10566182fdbdSPeter Wemm 	 * reboot before the driver initializes.
10576182fdbdSPeter Wemm 	 */
10587137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
1059a17c678eSDavid Greenman }
1060a17c678eSDavid Greenman 
10617dced78aSDavid Greenman /*
10627dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
10637dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10647dced78aSDavid Greenman  * resume.
10657dced78aSDavid Greenman  */
10667dced78aSDavid Greenman static int
10677dced78aSDavid Greenman fxp_suspend(device_t dev)
10687dced78aSDavid Greenman {
10697dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
10707137cea0SPyun YongHyeon 	struct ifnet *ifp;
10717137cea0SPyun YongHyeon 	int pmc;
10727137cea0SPyun YongHyeon 	uint16_t pmstat;
10737dced78aSDavid Greenman 
10744953bccaSNate Lawson 	FXP_LOCK(sc);
10757dced78aSDavid Greenman 
10767137cea0SPyun YongHyeon 	ifp = sc->ifp;
10777137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
10787137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10797137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
10807137cea0SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) {
10817137cea0SPyun YongHyeon 			/* Request PME. */
10827137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10837137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10847137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
10857137cea0SPyun YongHyeon 			fxp_init_body(sc);
10867137cea0SPyun YongHyeon 		}
10877137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
10887137cea0SPyun YongHyeon 	}
10897dced78aSDavid Greenman 	fxp_stop(sc);
10907dced78aSDavid Greenman 
10917dced78aSDavid Greenman 	sc->suspended = 1;
10927dced78aSDavid Greenman 
10934953bccaSNate Lawson 	FXP_UNLOCK(sc);
1094f7788e8eSJonathan Lemon 	return (0);
10957dced78aSDavid Greenman }
10967dced78aSDavid Greenman 
10977dced78aSDavid Greenman /*
109867ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
10997dced78aSDavid Greenman  * appropriate.
11007dced78aSDavid Greenman  */
11017dced78aSDavid Greenman static int
11027dced78aSDavid Greenman fxp_resume(device_t dev)
11037dced78aSDavid Greenman {
11047dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
1105fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
11067137cea0SPyun YongHyeon 	int pmc;
11077137cea0SPyun YongHyeon 	uint16_t pmstat;
11087dced78aSDavid Greenman 
11094953bccaSNate Lawson 	FXP_LOCK(sc);
11107dced78aSDavid Greenman 
11117137cea0SPyun YongHyeon 	if (pci_find_extcap(sc->dev, PCIY_PMG, &pmc) == 0) {
11127137cea0SPyun YongHyeon 		sc->flags &= ~FXP_FLAG_WOL;
11137137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
11147137cea0SPyun YongHyeon 		/* Disable PME and clear PME status. */
11157137cea0SPyun YongHyeon 		pmstat &= ~PCIM_PSTAT_PMEENABLE;
11167137cea0SPyun YongHyeon 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1117af75b654SPyun YongHyeon 		if ((sc->flags & FXP_FLAG_WOLCAP) != 0)
1118af75b654SPyun YongHyeon 			CSR_WRITE_1(sc, FXP_CSR_PMDR,
1119af75b654SPyun YongHyeon 			    CSR_READ_1(sc, FXP_CSR_PMDR));
11207137cea0SPyun YongHyeon 	}
11217137cea0SPyun YongHyeon 
11227dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
11237dced78aSDavid Greenman 	DELAY(10);
11247dced78aSDavid Greenman 
11257dced78aSDavid Greenman 	/* reinitialize interface if necessary */
11267dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
11274953bccaSNate Lawson 		fxp_init_body(sc);
11287dced78aSDavid Greenman 
11297dced78aSDavid Greenman 	sc->suspended = 0;
11307dced78aSDavid Greenman 
11314953bccaSNate Lawson 	FXP_UNLOCK(sc);
1132ba8c6fd5SDavid Greenman 	return (0);
1133f7788e8eSJonathan Lemon }
1134ba8c6fd5SDavid Greenman 
113500c4116bSJonathan Lemon static void
113600c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
113700c4116bSJonathan Lemon {
113874d1ed23SMaxime Henrion 	uint16_t reg;
113900c4116bSJonathan Lemon 	int x;
114000c4116bSJonathan Lemon 
114100c4116bSJonathan Lemon 	/*
114200c4116bSJonathan Lemon 	 * Shift in data.
114300c4116bSJonathan Lemon 	 */
114400c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
114500c4116bSJonathan Lemon 		if (data & x)
114600c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
114700c4116bSJonathan Lemon 		else
114800c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
114900c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
115000c4116bSJonathan Lemon 		DELAY(1);
115100c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
115200c4116bSJonathan Lemon 		DELAY(1);
115300c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
115400c4116bSJonathan Lemon 		DELAY(1);
115500c4116bSJonathan Lemon 	}
115600c4116bSJonathan Lemon }
115700c4116bSJonathan Lemon 
1158f7788e8eSJonathan Lemon /*
1159f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1160f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1161f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1162f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1163f7788e8eSJonathan Lemon  * every 16 bits of data.
1164f7788e8eSJonathan Lemon  */
116574d1ed23SMaxime Henrion static uint16_t
1166f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1167f7788e8eSJonathan Lemon {
116874d1ed23SMaxime Henrion 	uint16_t reg, data;
1169f7788e8eSJonathan Lemon 	int x;
1170ba8c6fd5SDavid Greenman 
1171f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1172f7788e8eSJonathan Lemon 	/*
1173f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1174f7788e8eSJonathan Lemon 	 */
117500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1176f7788e8eSJonathan Lemon 	/*
1177f7788e8eSJonathan Lemon 	 * Shift in address.
1178f7788e8eSJonathan Lemon 	 */
1179f7788e8eSJonathan Lemon 	data = 0;
1180f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1181f7788e8eSJonathan Lemon 		if (offset & x)
1182f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1183f7788e8eSJonathan Lemon 		else
1184f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1185f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1186f7788e8eSJonathan Lemon 		DELAY(1);
1187f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1188f7788e8eSJonathan Lemon 		DELAY(1);
1189f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1190f7788e8eSJonathan Lemon 		DELAY(1);
1191f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1192f7788e8eSJonathan Lemon 		data++;
1193f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1194f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1195f7788e8eSJonathan Lemon 			break;
1196f7788e8eSJonathan Lemon 		}
1197f7788e8eSJonathan Lemon 	}
1198f7788e8eSJonathan Lemon 	/*
1199f7788e8eSJonathan Lemon 	 * Shift out data.
1200f7788e8eSJonathan Lemon 	 */
1201f7788e8eSJonathan Lemon 	data = 0;
1202f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1203f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1204f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1205f7788e8eSJonathan Lemon 		DELAY(1);
1206f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1207f7788e8eSJonathan Lemon 			data |= x;
1208f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1209f7788e8eSJonathan Lemon 		DELAY(1);
1210f7788e8eSJonathan Lemon 	}
1211f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1212f7788e8eSJonathan Lemon 	DELAY(1);
1213f7788e8eSJonathan Lemon 
1214f7788e8eSJonathan Lemon 	return (data);
1215ba8c6fd5SDavid Greenman }
1216ba8c6fd5SDavid Greenman 
121700c4116bSJonathan Lemon static void
121874d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
121900c4116bSJonathan Lemon {
122000c4116bSJonathan Lemon 	int i;
122100c4116bSJonathan Lemon 
122200c4116bSJonathan Lemon 	/*
122300c4116bSJonathan Lemon 	 * Erase/write enable.
122400c4116bSJonathan Lemon 	 */
122500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
122600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
122700c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
122800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
122900c4116bSJonathan Lemon 	DELAY(1);
123000c4116bSJonathan Lemon 	/*
123100c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
123200c4116bSJonathan Lemon 	 */
123300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
123400c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
123500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
123600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
123700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
123800c4116bSJonathan Lemon 	DELAY(1);
123900c4116bSJonathan Lemon 	/*
124000c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
124100c4116bSJonathan Lemon 	 */
124200c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
124300c4116bSJonathan Lemon 	DELAY(1);
124400c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
124500c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
124600c4116bSJonathan Lemon 			break;
124700c4116bSJonathan Lemon 		DELAY(50);
124800c4116bSJonathan Lemon 	}
124900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
125000c4116bSJonathan Lemon 	DELAY(1);
125100c4116bSJonathan Lemon 	/*
125200c4116bSJonathan Lemon 	 * Erase/write disable.
125300c4116bSJonathan Lemon 	 */
125400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
125500c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
125600c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
125700c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
125800c4116bSJonathan Lemon 	DELAY(1);
125900c4116bSJonathan Lemon }
126000c4116bSJonathan Lemon 
1261ba8c6fd5SDavid Greenman /*
1262e9bf2fa7SDavid Greenman  * From NetBSD:
1263e9bf2fa7SDavid Greenman  *
1264e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1265e9bf2fa7SDavid Greenman  *
1266e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1267e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1268e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1269e9bf2fa7SDavid Greenman  *
1270e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1271e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1272e9bf2fa7SDavid Greenman  *
1273e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1274e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1275e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1276e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1277e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1278e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1279e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1280e9bf2fa7SDavid Greenman  */
1281e9bf2fa7SDavid Greenman static void
1282f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1283e9bf2fa7SDavid Greenman {
1284e9bf2fa7SDavid Greenman 
1285f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1286f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1287f7788e8eSJonathan Lemon 
1288f7788e8eSJonathan Lemon 	/* autosize */
1289f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1290e9bf2fa7SDavid Greenman }
1291f7788e8eSJonathan Lemon 
1292ba8c6fd5SDavid Greenman static void
1293f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1294ba8c6fd5SDavid Greenman {
1295f7788e8eSJonathan Lemon 	int i;
1296ba8c6fd5SDavid Greenman 
1297f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1298f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1299ba8c6fd5SDavid Greenman }
1300ba8c6fd5SDavid Greenman 
130100c4116bSJonathan Lemon static void
130200c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
130300c4116bSJonathan Lemon {
130400c4116bSJonathan Lemon 	int i;
130500c4116bSJonathan Lemon 
130600c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
130700c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
130800c4116bSJonathan Lemon }
130900c4116bSJonathan Lemon 
1310a17c678eSDavid Greenman /*
13114953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1312a17c678eSDavid Greenman  */
1313a17c678eSDavid Greenman static void
1314f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1315a17c678eSDavid Greenman {
13169b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
13174953bccaSNate Lawson 
13184953bccaSNate Lawson 	FXP_LOCK(sc);
13194953bccaSNate Lawson 	fxp_start_body(ifp);
13204953bccaSNate Lawson 	FXP_UNLOCK(sc);
13214953bccaSNate Lawson }
13224953bccaSNate Lawson 
13234953bccaSNate Lawson /*
13244953bccaSNate Lawson  * Start packet transmission on the interface.
13254953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
13264953bccaSNate Lawson  * internal entry point only.
13274953bccaSNate Lawson  */
13284953bccaSNate Lawson static void
13294953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
13304953bccaSNate Lawson {
13314953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1332b2badf02SMaxime Henrion 	struct mbuf *mb_head;
13334e53f837SPyun YongHyeon 	int txqueued;
1334a17c678eSDavid Greenman 
133567fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
133640c20505SMaxime Henrion 
1337c109e385SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1338c109e385SPyun YongHyeon 	    IFF_DRV_RUNNING)
1339c109e385SPyun YongHyeon 		return;
1340c109e385SPyun YongHyeon 
13414e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
13424e53f837SPyun YongHyeon 		fxp_txeof(sc);
1343483b9871SDavid Greenman 	/*
1344483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1345483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
13463114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
13473114fdb4SDavid Greenman 	 *       a NOP command when needed.
1348483b9871SDavid Greenman 	 */
134940c20505SMaxime Henrion 	txqueued = 0;
13507929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
13517929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1352483b9871SDavid Greenman 
1353dfe61cf1SDavid Greenman 		/*
1354dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1355dfe61cf1SDavid Greenman 		 */
13567929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
13577929aa03SMax Laier 		if (mb_head == NULL)
13587929aa03SMax Laier 			break;
1359a17c678eSDavid Greenman 
13604e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13614e53f837SPyun YongHyeon 			if (mb_head == NULL)
136240c20505SMaxime Henrion 				break;
13634e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
13644e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
136540c20505SMaxime Henrion 		}
13664e53f837SPyun YongHyeon 		txqueued++;
13674e53f837SPyun YongHyeon 		/*
13684e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13694e53f837SPyun YongHyeon 		 */
13704e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
13714e53f837SPyun YongHyeon 	}
137240c20505SMaxime Henrion 
137340c20505SMaxime Henrion 	/*
137440c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
137540c20505SMaxime Henrion 	 * going again if suspended.
137640c20505SMaxime Henrion 	 */
13774e53f837SPyun YongHyeon 	if (txqueued > 0) {
1378a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1379a2057a72SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
138040c20505SMaxime Henrion 		fxp_scb_wait(sc);
138140c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13824e53f837SPyun YongHyeon 		/*
13834e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
13844e53f837SPyun YongHyeon 		 * from the card again.
13854e53f837SPyun YongHyeon 		 */
13864e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
138740c20505SMaxime Henrion 	}
138840c20505SMaxime Henrion }
138940c20505SMaxime Henrion 
139040c20505SMaxime Henrion static int
13914e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
139240c20505SMaxime Henrion {
139340c20505SMaxime Henrion 	struct ifnet *ifp;
139440c20505SMaxime Henrion 	struct mbuf *m;
139540c20505SMaxime Henrion 	struct fxp_tx *txp;
139640c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1397c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
139840c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1399c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
140040c20505SMaxime Henrion 
140140c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1402fc74a9f9SBrooks Davis 	ifp = sc->ifp;
140340c20505SMaxime Henrion 
1404c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1405c21e84e4SPyun YongHyeon 	tcp = NULL;
1406dfe61cf1SDavid Greenman 	/*
1407483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1408dfe61cf1SDavid Greenman 	 */
1409b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1410c8bca6dcSBill Paul 
1411c8bca6dcSBill Paul 	/*
1412a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1413a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1414a35e7eaaSDon Lewis 	 * Developer Manual says:
1415a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1416a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1417a35e7eaaSDon Lewis 	 *   ...
1418a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1419a35e7eaaSDon Lewis 	 *   be used.
1420a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1421a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1422a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1423a35e7eaaSDon Lewis 	 */
1424a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1425a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1426a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1427a35e7eaaSDon Lewis 
14284e53f837SPyun YongHyeon 	m = *m_head;
1429c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1430c21e84e4SPyun YongHyeon 		/*
1431c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1432c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1433c21e84e4SPyun YongHyeon 		 */
1434c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1435c21e84e4SPyun YongHyeon 		struct ip *ip;
1436c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1437c21e84e4SPyun YongHyeon 
1438c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1439c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1440c21e84e4SPyun YongHyeon 			m = m_dup(*m_head, M_DONTWAIT);
1441c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1442c21e84e4SPyun YongHyeon 			if (m == NULL) {
1443c21e84e4SPyun YongHyeon 				*m_head = NULL;
1444c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1445c21e84e4SPyun YongHyeon 			}
1446c21e84e4SPyun YongHyeon 			*m_head = m;
1447c21e84e4SPyun YongHyeon 		}
1448c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1449c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1450c21e84e4SPyun YongHyeon 		if (m == NULL) {
1451c21e84e4SPyun YongHyeon 			*m_head = NULL;
1452c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1453c21e84e4SPyun YongHyeon 		}
1454c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1455c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1456c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1457c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1458c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1459c21e84e4SPyun YongHyeon 			if (m == NULL) {
1460c21e84e4SPyun YongHyeon 				*m_head = NULL;
1461c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1462c21e84e4SPyun YongHyeon 			}
1463c21e84e4SPyun YongHyeon 		}
1464c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1465c21e84e4SPyun YongHyeon 		if (m == NULL) {
1466c21e84e4SPyun YongHyeon 			*m_head = NULL;
1467c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1468c21e84e4SPyun YongHyeon 		}
1469c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1470c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1471c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1472c21e84e4SPyun YongHyeon 		if (m == NULL) {
1473c21e84e4SPyun YongHyeon 			*m_head = NULL;
1474c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1475c21e84e4SPyun YongHyeon 		}
1476c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1477c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr) + tcp->th_off);
1478c21e84e4SPyun YongHyeon 		if (m == NULL) {
1479c21e84e4SPyun YongHyeon 			*m_head = NULL;
1480c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1481c21e84e4SPyun YongHyeon 		}
1482c21e84e4SPyun YongHyeon 
1483c21e84e4SPyun YongHyeon 		/*
1484c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1485c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1486c21e84e4SPyun YongHyeon 		 */
1487c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
14880685c824SPyun YongHyeon 		ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) +
14890685c824SPyun YongHyeon 		    (tcp->th_off << 2));
1490c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1491c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1492c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1493c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1494c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1495c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1496c21e84e4SPyun YongHyeon 		*m_head = m;
14976da6d0a9SPyun YongHyeon 	} else if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
14986da6d0a9SPyun YongHyeon 		/*
14996da6d0a9SPyun YongHyeon 		 * Deal with TCP/IP checksum offload. Note that
15006da6d0a9SPyun YongHyeon 		 * in order for TCP checksum offload to work,
15016da6d0a9SPyun YongHyeon 		 * the pseudo header checksum must have already
15026da6d0a9SPyun YongHyeon 		 * been computed and stored in the checksum field
15036da6d0a9SPyun YongHyeon 		 * in the TCP header. The stack should have
15046da6d0a9SPyun YongHyeon 		 * already done this for us.
15056da6d0a9SPyun YongHyeon 		 */
15066da6d0a9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
15076da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
15086da6d0a9SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
15096da6d0a9SPyun YongHyeon 
15106da6d0a9SPyun YongHyeon #ifdef FXP_IP_CSUM_WAR
15116da6d0a9SPyun YongHyeon 		/*
15126da6d0a9SPyun YongHyeon 		 * XXX The 82550 chip appears to have trouble
15136da6d0a9SPyun YongHyeon 		 * dealing with IP header checksums in very small
15146da6d0a9SPyun YongHyeon 		 * datagrams, namely fragments from 1 to 3 bytes
15156da6d0a9SPyun YongHyeon 		 * in size. For example, say you want to transmit
15166da6d0a9SPyun YongHyeon 		 * a UDP packet of 1473 bytes. The packet will be
15176da6d0a9SPyun YongHyeon 		 * fragmented over two IP datagrams, the latter
15186da6d0a9SPyun YongHyeon 		 * containing only one byte of data. The 82550 will
15196da6d0a9SPyun YongHyeon 		 * botch the header checksum on the 1-byte fragment.
15206da6d0a9SPyun YongHyeon 		 * As long as the datagram contains 4 or more bytes
15216da6d0a9SPyun YongHyeon 		 * of data, you're ok.
15226da6d0a9SPyun YongHyeon 		 *
15236da6d0a9SPyun YongHyeon                  * The following code attempts to work around this
15246da6d0a9SPyun YongHyeon 		 * problem: if the datagram is less than 38 bytes
15256da6d0a9SPyun YongHyeon 		 * in size (14 bytes ether header, 20 bytes IP header,
15266da6d0a9SPyun YongHyeon 		 * plus 4 bytes of data), we punt and compute the IP
15276da6d0a9SPyun YongHyeon 		 * header checksum by hand. This workaround doesn't
15286da6d0a9SPyun YongHyeon 		 * work very well, however, since it can be fooled
15296da6d0a9SPyun YongHyeon 		 * by things like VLAN tags and IP options that make
15306da6d0a9SPyun YongHyeon 		 * the header sizes/offsets vary.
15316da6d0a9SPyun YongHyeon 		 */
15326da6d0a9SPyun YongHyeon 
15336da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
15346da6d0a9SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
15356da6d0a9SPyun YongHyeon 				struct ip *ip;
15366da6d0a9SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
15376da6d0a9SPyun YongHyeon 				ip = mtod(m, struct ip *);
15386da6d0a9SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
15396da6d0a9SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
15406da6d0a9SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
15416da6d0a9SPyun YongHyeon 			} else {
15426da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_activation_high =
15436da6d0a9SPyun YongHyeon 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
15446da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_schedule |=
15456da6d0a9SPyun YongHyeon 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
15466da6d0a9SPyun YongHyeon 			}
15476da6d0a9SPyun YongHyeon 		}
15486da6d0a9SPyun YongHyeon #endif
1549c21e84e4SPyun YongHyeon 	}
1550c21e84e4SPyun YongHyeon 
1551a2057a72SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, *m_head,
15524e53f837SPyun YongHyeon 	    segs, &nseg, 0);
15534e53f837SPyun YongHyeon 	if (error == EFBIG) {
15544e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
15554e53f837SPyun YongHyeon 		if (m == NULL) {
15564e53f837SPyun YongHyeon 			m_freem(*m_head);
15574e53f837SPyun YongHyeon 			*m_head = NULL;
15584e53f837SPyun YongHyeon 			return (ENOMEM);
15591104779bSMike Silbersack 		}
15604e53f837SPyun YongHyeon 		*m_head = m;
1561a2057a72SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map,
15624e53f837SPyun YongHyeon 	    	    *m_head, segs, &nseg, 0);
15634e53f837SPyun YongHyeon 		if (error != 0) {
15644e53f837SPyun YongHyeon 			m_freem(*m_head);
15654e53f837SPyun YongHyeon 			*m_head = NULL;
15664e53f837SPyun YongHyeon 			return (ENOMEM);
15674e53f837SPyun YongHyeon 		}
15684e53f837SPyun YongHyeon 	} else if (error != 0)
15694e53f837SPyun YongHyeon 		return (error);
15704e53f837SPyun YongHyeon 	if (nseg == 0) {
15714e53f837SPyun YongHyeon 		m_freem(*m_head);
15724e53f837SPyun YongHyeon 		*m_head = NULL;
15734e53f837SPyun YongHyeon 		return (EIO);
157423a0ed7cSDavid Greenman 	}
157523a0ed7cSDavid Greenman 
157640c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
1577a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1578b2badf02SMaxime Henrion 
157940c20505SMaxime Henrion 	cbp = txp->tx_cb;
158040c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
158140c20505SMaxime Henrion 		/*
158240c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
158340c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
158440c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
158540c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
158640c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
158740c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
158840c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
158940c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
159040c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
159140c20505SMaxime Henrion 		 * starting from a different place depending on whether
159240c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
159340c20505SMaxime Henrion 		 */
159440c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
159568f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
159668f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
159740c20505SMaxime Henrion 		} else {
159840c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
159940c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
160040c20505SMaxime Henrion 		}
160140c20505SMaxime Henrion 	}
1602c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1603c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1604c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
160568f4ab9aSPyun YongHyeon 		cbp->tbd[nseg].tb_size |= htole32(0x8000);
1606c21e84e4SPyun YongHyeon 	} else
160740c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1608c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1609c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1610c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
161168f4ab9aSPyun YongHyeon 		cbp->tbd[1].tb_size |= htole32(tcp_payload << 16);
1612c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1613c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1614c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1615c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1616c21e84e4SPyun YongHyeon 	}
1617bd4fa9d9SPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
1618bd4fa9d9SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
1619bd4fa9d9SPyun YongHyeon 		cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag);
1620bd4fa9d9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_activation_high |=
1621bd4fa9d9SPyun YongHyeon 		    FXP_IPCB_INSERTVLAN_ENABLE;
1622bd4fa9d9SPyun YongHyeon 	}
162340c20505SMaxime Henrion 
16244e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1625b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1626b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
16274e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1628b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
162983e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
163083e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
16314e53f837SPyun YongHyeon 	else
1632b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
163383e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
163483e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1635c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1636b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1637a17c678eSDavid Greenman 
1638a17c678eSDavid Greenman 	/*
1639483b9871SDavid Greenman 	 * Advance the end of list forward.
1640a17c678eSDavid Greenman 	 */
164106175228SAndrew Gallatin 
164250d81222SMaxime Henrion #ifdef __alpha__
164306175228SAndrew Gallatin 	/*
164406175228SAndrew Gallatin 	 * On platforms which can't access memory in 16-bit
164506175228SAndrew Gallatin 	 * granularities, we must prevent the card from DMA'ing
164606175228SAndrew Gallatin 	 * up the status while we update the command field.
164706175228SAndrew Gallatin 	 * This could cause us to overwrite the completion status.
164814fd1071SMaxime Henrion 	 * XXX This is probably bogus and we're _not_ looking
164914fd1071SMaxime Henrion 	 * for atomicity here.
165006175228SAndrew Gallatin 	 */
165114fd1071SMaxime Henrion 	atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1652bafb64afSMaxime Henrion 	    htole16(FXP_CB_COMMAND_S));
165350d81222SMaxime Henrion #else
165440c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
165550d81222SMaxime Henrion #endif /*__alpha__*/
1656b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1657a17c678eSDavid Greenman 
1658a17c678eSDavid Greenman 	/*
16591cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1660b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1661483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1662a17c678eSDavid Greenman 	 */
16631cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1664b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1665a17c678eSDavid Greenman 
16661cd443acSDavid Greenman 	sc->tx_queued++;
16671cd443acSDavid Greenman 
166840c20505SMaxime Henrion 	return (0);
1669a17c678eSDavid Greenman }
1670a17c678eSDavid Greenman 
1671e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1672e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1673e4fc250cSLuigi Rizzo 
16741abcdbd1SAttilio Rao static int
1675e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1676e4fc250cSLuigi Rizzo {
1677e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
167874d1ed23SMaxime Henrion 	uint8_t statack;
16791abcdbd1SAttilio Rao 	int rx_npkts = 0;
1680e4fc250cSLuigi Rizzo 
16814953bccaSNate Lawson 	FXP_LOCK(sc);
168240929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
16834953bccaSNate Lawson 		FXP_UNLOCK(sc);
16841abcdbd1SAttilio Rao 		return (rx_npkts);
1685e4fc250cSLuigi Rizzo 	}
168640929967SGleb Smirnoff 
1687e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1688e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1689e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
169074d1ed23SMaxime Henrion 		uint8_t tmp;
16916481f301SPeter Wemm 
1692e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16934953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16944953bccaSNate Lawson 			FXP_UNLOCK(sc);
16951abcdbd1SAttilio Rao 			return (rx_npkts); /* nothing to do */
16964953bccaSNate Lawson 		}
1697e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1698e4fc250cSLuigi Rizzo 		/* ack what we can */
1699e4fc250cSLuigi Rizzo 		if (tmp != 0)
1700e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1701e4fc250cSLuigi Rizzo 		statack |= tmp;
1702e4fc250cSLuigi Rizzo 	}
17031abcdbd1SAttilio Rao 	rx_npkts = fxp_intr_body(sc, ifp, statack, count);
17044953bccaSNate Lawson 	FXP_UNLOCK(sc);
17051abcdbd1SAttilio Rao 	return (rx_npkts);
1706e4fc250cSLuigi Rizzo }
1707e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1708e4fc250cSLuigi Rizzo 
1709a17c678eSDavid Greenman /*
17109c7d2607SDavid Greenman  * Process interface interrupts.
1711a17c678eSDavid Greenman  */
171294927790SDavid Greenman static void
1713f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1714a17c678eSDavid Greenman {
1715f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1716fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
171774d1ed23SMaxime Henrion 	uint8_t statack;
17180f4dc94cSChuck Paterson 
17194953bccaSNate Lawson 	FXP_LOCK(sc);
1720704d1965SWarner Losh 	if (sc->suspended) {
1721704d1965SWarner Losh 		FXP_UNLOCK(sc);
1722704d1965SWarner Losh 		return;
1723704d1965SWarner Losh 	}
1724704d1965SWarner Losh 
1725e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
172640929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
17274953bccaSNate Lawson 		FXP_UNLOCK(sc);
1728e4fc250cSLuigi Rizzo 		return;
17294953bccaSNate Lawson 	}
1730e4fc250cSLuigi Rizzo #endif
1731b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1732a17c678eSDavid Greenman 		/*
173311457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
173411457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
173511457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
173611457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
173711457bbfSJonathan Lemon 		 */
17384953bccaSNate Lawson 		if (statack == 0xff) {
17394953bccaSNate Lawson 			FXP_UNLOCK(sc);
174011457bbfSJonathan Lemon 			return;
17414953bccaSNate Lawson 		}
174211457bbfSJonathan Lemon 
174311457bbfSJonathan Lemon 		/*
1744a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1745a17c678eSDavid Greenman 		 */
1746ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1747c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
17484953bccaSNate Lawson 			fxp_intr_body(sc, ifp, statack, -1);
1749e4fc250cSLuigi Rizzo 	}
17504953bccaSNate Lawson 	FXP_UNLOCK(sc);
1751e4fc250cSLuigi Rizzo }
1752e4fc250cSLuigi Rizzo 
1753e4fc250cSLuigi Rizzo static void
1754b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1755b2badf02SMaxime Henrion {
17564e53f837SPyun YongHyeon 	struct ifnet *ifp;
1757b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1758b2badf02SMaxime Henrion 
17594e53f837SPyun YongHyeon 	ifp = sc->ifp;
1760a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1761a2057a72SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1762b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
176383e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1764b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1765b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1766a2057a72SPyun YongHyeon 			bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
1767b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1768a2057a72SPyun YongHyeon 			bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
1769b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1770b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1771b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1772b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1773b2badf02SMaxime Henrion 		}
1774b2badf02SMaxime Henrion 		sc->tx_queued--;
17754e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1776b2badf02SMaxime Henrion 	}
1777b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1778a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1779a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17806b24912cSPyun YongHyeon 	if (sc->tx_queued == 0)
178125935344SPyun YongHyeon 		sc->watchdog_timer = 0;
1782b2badf02SMaxime Henrion }
1783b2badf02SMaxime Henrion 
1784b2badf02SMaxime Henrion static void
1785f13075afSPyun YongHyeon fxp_rxcsum(struct fxp_softc *sc, struct ifnet *ifp, struct mbuf *m,
1786f13075afSPyun YongHyeon     uint16_t status, int pos)
1787f13075afSPyun YongHyeon {
1788f13075afSPyun YongHyeon 	struct ether_header *eh;
1789f13075afSPyun YongHyeon 	struct ip *ip;
1790f13075afSPyun YongHyeon 	struct udphdr *uh;
1791f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1792f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1793f13075afSPyun YongHyeon 
1794f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1795f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1796f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1797f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1798f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1799f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1800f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1801f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1802f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1803f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1804f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1805f13075afSPyun YongHyeon 			}
1806f13075afSPyun YongHyeon 		}
1807f13075afSPyun YongHyeon 		return;
1808f13075afSPyun YongHyeon 	}
1809f13075afSPyun YongHyeon 
1810f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1811f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1812f13075afSPyun YongHyeon 		return;
1813f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1814f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1815f13075afSPyun YongHyeon 		return;
1816f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1817f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1818f13075afSPyun YongHyeon 		return;
1819f13075afSPyun YongHyeon 
1820f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1821f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1822f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1823f13075afSPyun YongHyeon 		return;
1824f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1825f13075afSPyun YongHyeon 		return;
1826f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1827f13075afSPyun YongHyeon 		return;
1828f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1829f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1830f13075afSPyun YongHyeon 
1831f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1832f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1833f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1834f13075afSPyun YongHyeon 			return;
1835f13075afSPyun YongHyeon 		break;
1836f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1837f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1838f13075afSPyun YongHyeon 			return;
1839f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1840f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1841f13075afSPyun YongHyeon 			return; /* no checksum */
1842f13075afSPyun YongHyeon 		break;
1843f13075afSPyun YongHyeon 	default:
1844f13075afSPyun YongHyeon 		return;
1845f13075afSPyun YongHyeon 	}
1846f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1847f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1848f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1849f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1850f13075afSPyun YongHyeon 	if (len > 0) {
1851f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1852f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1853f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1854f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1855f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1856f13075afSPyun YongHyeon 		}
1857f13075afSPyun YongHyeon 	}
1858f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1859f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1860f13075afSPyun YongHyeon }
1861f13075afSPyun YongHyeon 
18621abcdbd1SAttilio Rao static int
186374d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
18644953bccaSNate Lawson     int count)
1865e4fc250cSLuigi Rizzo {
18662b5989e9SLuigi Rizzo 	struct mbuf *m;
1867b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18682b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18692b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
18701abcdbd1SAttilio Rao 	int rx_npkts;
187160bb79ebSPyun YongHyeon 	uint16_t status;
18722b5989e9SLuigi Rizzo 
18731abcdbd1SAttilio Rao 	rx_npkts = 0;
187467fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18751abcdbd1SAttilio Rao 
18762b5989e9SLuigi Rizzo 	if (rnr)
18770f1db1d6SMaxime Henrion 		sc->rnr++;
1878947e3815SIan Dowse #ifdef DEVICE_POLLING
1879947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1880947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1881947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1882947e3815SIan Dowse 		rnr = 1;
1883947e3815SIan Dowse 	}
1884947e3815SIan Dowse #endif
1885a17c678eSDavid Greenman 
1886a17c678eSDavid Greenman 	/*
18873114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
188806936301SBill Paul 	 *
188906936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
189006936301SBill Paul 	 * be that this event (control unit not ready) was not
189106936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
189206936301SBill Paul 	 * The exact sequence of events that occur when the interface
189306936301SBill Paul 	 * is brought up are different now, and if this event
189406936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
189506936301SBill Paul 	 * can stall for several seconds. The result is that no
189606936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
189706936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
18983114fdb4SDavid Greenman 	 */
18994e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1900b2badf02SMaxime Henrion 		fxp_txeof(sc);
19013114fdb4SDavid Greenman 
19023114fdb4SDavid Greenman 	/*
19033114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
19043114fdb4SDavid Greenman 	 */
19057929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
19064953bccaSNate Lawson 		fxp_start_body(ifp);
19072b5989e9SLuigi Rizzo 
19082b5989e9SLuigi Rizzo 	/*
19092b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
19102b5989e9SLuigi Rizzo 	 */
1911947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
19121abcdbd1SAttilio Rao 		return (rx_npkts);
19132b5989e9SLuigi Rizzo 
19143114fdb4SDavid Greenman 	/*
1915a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1916a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1917a17c678eSDavid Greenman 	 * re-start the receiver.
1918947e3815SIan Dowse 	 *
19192b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
19202b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
19212b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
19222b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1923947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1924947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1925a17c678eSDavid Greenman 	 */
19262b5989e9SLuigi Rizzo 	for (;;) {
1927b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1928b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1929ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1930ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1931a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
1932b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1933a17c678eSDavid Greenman 
1934e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1935947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1936947e3815SIan Dowse 			if (rnr) {
1937947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1938947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1939947e3815SIan Dowse 				rnr = 0;
1940947e3815SIan Dowse 			}
19412b5989e9SLuigi Rizzo 			break;
1942947e3815SIan Dowse 		}
19432b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
19442b5989e9SLuigi Rizzo 
194560bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
194660bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
19472b5989e9SLuigi Rizzo 			break;
19482b5989e9SLuigi Rizzo 
1949dfe61cf1SDavid Greenman 		/*
1950b2badf02SMaxime Henrion 		 * Advance head forward.
1951dfe61cf1SDavid Greenman 		 */
1952b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1953a17c678eSDavid Greenman 
1954dfe61cf1SDavid Greenman 		/*
1955ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1956ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1957ba8c6fd5SDavid Greenman 		 * instead.
1958dfe61cf1SDavid Greenman 		 */
195985050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1960aed53495SDavid Greenman 			int total_len;
1961a17c678eSDavid Greenman 
1962e8c8b728SJonathan Lemon 			/*
19632b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
19642b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
19652b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
19662b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1967e8c8b728SJonathan Lemon 			 */
1968bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1969f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
1970f13075afSPyun YongHyeon 			    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1971f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1972f13075afSPyun YongHyeon 				total_len -= 2;
1973f13075afSPyun YongHyeon 			}
19742b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
19752b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
197660bb79ebSPyun YongHyeon 				sc->rfa_size || status & FXP_RFA_STATUS_CRC) {
1977e8c8b728SJonathan Lemon 				m_freem(m);
19782b5989e9SLuigi Rizzo 				continue;
1979e8c8b728SJonathan Lemon 			}
1980920b58e8SBrooks Davis 
19812e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1982673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1983673d9191SSam Leffler 
1984f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
1985f13075afSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1986f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
1987bd4fa9d9SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
1988bd4fa9d9SPyun YongHyeon 			    (status & FXP_RFA_STATUS_VLAN) != 0) {
1989bd4fa9d9SPyun YongHyeon 				m->m_pkthdr.ether_vtag =
1990bd4fa9d9SPyun YongHyeon 				    ntohs(rfa->rfax_vlan_id);
1991bd4fa9d9SPyun YongHyeon 				m->m_flags |= M_VLANTAG;
1992bd4fa9d9SPyun YongHyeon 			}
199305fb8c3fSNate Lawson 			/*
199405fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
199505fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
199605fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
199705fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
199805fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
199905fb8c3fSNate Lawson 			 * calling if_input() on each one.
200005fb8c3fSNate Lawson 			 */
200105fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
2002673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
200305fb8c3fSNate Lawson 			FXP_LOCK(sc);
20041abcdbd1SAttilio Rao 			rx_npkts++;
2005c109e385SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2006c109e385SPyun YongHyeon 				return (rx_npkts);
200785050421SPyun YongHyeon 		} else {
200885050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
200985050421SPyun YongHyeon 			ifp->if_iqdrops++;
201085050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
2011a17c678eSDavid Greenman 		}
201285050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
2013a17c678eSDavid Greenman 	}
20142b5989e9SLuigi Rizzo 	if (rnr) {
2015ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
2016ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
2017b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
20182e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2019a17c678eSDavid Greenman 	}
20201abcdbd1SAttilio Rao 	return (rx_npkts);
2021a17c678eSDavid Greenman }
2022a17c678eSDavid Greenman 
2023dfe61cf1SDavid Greenman /*
2024dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
2025dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
2026dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
2027dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
2028dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
2029dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
2030dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
2031dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
2032dfe61cf1SDavid Greenman  * them again next time.
2033dfe61cf1SDavid Greenman  */
2034303b270bSEivind Eklund static void
2035f7788e8eSJonathan Lemon fxp_tick(void *xsc)
2036a17c678eSDavid Greenman {
2037f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
2038fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2039a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
2040a17c678eSDavid Greenman 
20413212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2042b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
204383e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
204483e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
2045397f9dfeSDavid Greenman 	if (sp->rx_good) {
204683e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
2047397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
204843d8b117SPyun YongHyeon 	} else if (sc->flags & FXP_FLAG_RXBUG) {
2049c8cc6fcaSDavid Greenman 		/*
2050c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
2051c8cc6fcaSDavid Greenman 		 */
2052397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
2053397f9dfeSDavid Greenman 	}
20543ba65732SDavid Greenman 	ifp->if_ierrors +=
205583e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
205683e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
205783e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
205883e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
2059a17c678eSDavid Greenman 	/*
2060f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
2061f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
2062f9be9005SDavid Greenman 	 */
2063f9be9005SDavid Greenman 	if (sp->tx_underruns) {
206483e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
2065f9be9005SDavid Greenman 		if (tx_threshold < 192)
2066f9be9005SDavid Greenman 			tx_threshold += 64;
2067f9be9005SDavid Greenman 	}
20684953bccaSNate Lawson 
2069397f9dfeSDavid Greenman 	/*
2070c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
2071c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
2072c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
2073c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
2074c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
2075c8cc6fcaSDavid Greenman 	 */
2076b2badf02SMaxime Henrion 	fxp_txeof(sc);
2077b2badf02SMaxime Henrion 
2078c8cc6fcaSDavid Greenman 	/*
2079397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
2080397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2081397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2082397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2083397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
2084397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2085397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2086397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2087397f9dfeSDavid Greenman 	 */
2088397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2089397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
2090c109e385SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
20916b24912cSPyun YongHyeon 			fxp_init_body(sc);
20926b24912cSPyun YongHyeon 		return;
2093397f9dfeSDavid Greenman 	}
2094f9be9005SDavid Greenman 	/*
20953ba65732SDavid Greenman 	 * If there is no pending command, start another stats
20963ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2097a17c678eSDavid Greenman 	 */
2098397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2099a17c678eSDavid Greenman 		/*
2100397f9dfeSDavid Greenman 		 * Start another stats dump.
2101a17c678eSDavid Greenman 		 */
2102b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
2103b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
21042e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2105dfe61cf1SDavid Greenman 	} else {
2106dfe61cf1SDavid Greenman 		/*
2107dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
2108dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
21093ba65732SDavid Greenman 		 * next timer event to update them.
2110dfe61cf1SDavid Greenman 		 */
2111dfe61cf1SDavid Greenman 		sp->tx_good = 0;
2112f9be9005SDavid Greenman 		sp->tx_underruns = 0;
2113dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
21143ba65732SDavid Greenman 
2115dfe61cf1SDavid Greenman 		sp->rx_good = 0;
21163ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
21173ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
21183ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
21193ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
2120dfe61cf1SDavid Greenman 	}
2121f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2122f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
21234953bccaSNate Lawson 
2124a17c678eSDavid Greenman 	/*
212516f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2126df79d527SGleb Smirnoff 	 */
2127df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2128df79d527SGleb Smirnoff 
2129df79d527SGleb Smirnoff 	/*
2130a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2131a17c678eSDavid Greenman 	 */
213245276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2133a17c678eSDavid Greenman }
2134a17c678eSDavid Greenman 
2135a17c678eSDavid Greenman /*
2136a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2137a17c678eSDavid Greenman  * the interface.
2138a17c678eSDavid Greenman  */
2139a17c678eSDavid Greenman static void
2140f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2141a17c678eSDavid Greenman {
2142fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2143b2badf02SMaxime Henrion 	struct fxp_tx *txp;
21443ba65732SDavid Greenman 	int i;
2145a17c678eSDavid Greenman 
214613f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2147df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
21487dced78aSDavid Greenman 
2149a17c678eSDavid Greenman 	/*
2150a17c678eSDavid Greenman 	 * Cancel stats updater.
2151a17c678eSDavid Greenman 	 */
215245276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
21533ba65732SDavid Greenman 
21543ba65732SDavid Greenman 	/*
21557137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
21567137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
21573ba65732SDavid Greenman 	 */
21587137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
215972a32a26SJonathan Lemon 	DELAY(50);
21607137cea0SPyun YongHyeon 	/* Disable interrupts. */
21617137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2162a17c678eSDavid Greenman 
21633ba65732SDavid Greenman 	/*
21643ba65732SDavid Greenman 	 * Release any xmit buffers.
21653ba65732SDavid Greenman 	 */
2166b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2167da91462dSDavid Greenman 	if (txp != NULL) {
2168da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
2169b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
2170a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp[i].tx_map,
2171b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
2172a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag,
2173a2057a72SPyun YongHyeon 				    txp[i].tx_map);
2174b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
2175b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
2176c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
2177b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
2178da91462dSDavid Greenman 			}
2179da91462dSDavid Greenman 		}
21803ba65732SDavid Greenman 	}
2181a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2182a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
21833ba65732SDavid Greenman 	sc->tx_queued = 0;
2184a17c678eSDavid Greenman }
2185a17c678eSDavid Greenman 
2186a17c678eSDavid Greenman /*
2187a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2188a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2189a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2190a17c678eSDavid Greenman  * card has wedged for some reason.
2191a17c678eSDavid Greenman  */
2192a17c678eSDavid Greenman static void
2193df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2194a17c678eSDavid Greenman {
2195ba8c6fd5SDavid Greenman 
2196df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2197df79d527SGleb Smirnoff 
2198df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2199df79d527SGleb Smirnoff 		return;
2200df79d527SGleb Smirnoff 
2201f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2202df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
2203a17c678eSDavid Greenman 
22044953bccaSNate Lawson 	fxp_init_body(sc);
2205a17c678eSDavid Greenman }
2206a17c678eSDavid Greenman 
22074953bccaSNate Lawson /*
22084953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
22094953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
22104953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
22114953bccaSNate Lawson  */
2212a17c678eSDavid Greenman static void
2213f7788e8eSJonathan Lemon fxp_init(void *xsc)
2214a17c678eSDavid Greenman {
2215fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
22164953bccaSNate Lawson 
22174953bccaSNate Lawson 	FXP_LOCK(sc);
22184953bccaSNate Lawson 	fxp_init_body(sc);
22194953bccaSNate Lawson 	FXP_UNLOCK(sc);
22204953bccaSNate Lawson }
22214953bccaSNate Lawson 
22224953bccaSNate Lawson /*
22234953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
22244953bccaSNate Lawson  * softc lock held.
22254953bccaSNate Lawson  */
22264953bccaSNate Lawson static void
22274953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
22284953bccaSNate Lawson {
2229fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
2230a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2231a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2232b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2233b2badf02SMaxime Henrion 	struct fxp_tx *txp;
22343212724cSJohn Baldwin 	int i, prm;
2235a17c678eSDavid Greenman 
223667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2237a17c678eSDavid Greenman 	/*
22383ba65732SDavid Greenman 	 * Cancel any pending I/O
2239a17c678eSDavid Greenman 	 */
22403ba65732SDavid Greenman 	fxp_stop(sc);
2241a17c678eSDavid Greenman 
22427137cea0SPyun YongHyeon 	/*
22437137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
22447137cea0SPyun YongHyeon 	 */
22457137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
22467137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
22477137cea0SPyun YongHyeon 	DELAY(50);
22487137cea0SPyun YongHyeon 
2249a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
2250a17c678eSDavid Greenman 
2251a17c678eSDavid Greenman 	/*
2252a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2253a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2254a17c678eSDavid Greenman 	 */
2255ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
22562e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2257a17c678eSDavid Greenman 
2258ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
22592e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2260a17c678eSDavid Greenman 
2261a17c678eSDavid Greenman 	/*
2262a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2263a17c678eSDavid Greenman 	 */
2264ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2265b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
2266b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
22672e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2268a17c678eSDavid Greenman 
2269a17c678eSDavid Greenman 	/*
227072a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
2271b96ad4b2SPyun YongHyeon 	 * For ICH based controllers do not load microcode.
227272a32a26SJonathan Lemon 	 */
2273b96ad4b2SPyun YongHyeon 	if (sc->ident->ich == 0) {
2274b96ad4b2SPyun YongHyeon 		if (ifp->if_flags & IFF_LINK0 &&
2275b96ad4b2SPyun YongHyeon 		    (sc->flags & FXP_FLAG_UCODE) == 0)
227672a32a26SJonathan Lemon 			fxp_load_ucode(sc);
2277b96ad4b2SPyun YongHyeon 	}
227872a32a26SJonathan Lemon 
227972a32a26SJonathan Lemon 	/*
22806b24912cSPyun YongHyeon 	 * Set IFF_ALLMULTI status. It's needed in configure action
22816b24912cSPyun YongHyeon 	 * command.
228209882363SJonathan Lemon 	 */
22836b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
228409882363SJonathan Lemon 
228509882363SJonathan Lemon 	/*
2286a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2287a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2288a17c678eSDavid Greenman 	 * later.
2289a17c678eSDavid Greenman 	 */
2290b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2291a17c678eSDavid Greenman 
2292a17c678eSDavid Greenman 	/*
2293a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2294a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2295a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2296a17c678eSDavid Greenman 	 */
2297b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2298a17c678eSDavid Greenman 
2299a17c678eSDavid Greenman 	cbp->cb_status =	0;
230083e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
230183e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
230283e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
23032c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2304001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2305001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2306a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2307f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2308f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2309f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2310f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2311001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2312001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2313f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2314a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2315f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2316f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
23173114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2318f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2319f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2320f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
23218ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2322a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2323f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2324f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2325c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2326c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2327f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2328f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2329f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
2330f13075afSPyun YongHyeon 	    (ifp->if_capenable & IFCAP_RXCSUM) != 0) ? 1 : 0;
2331f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2332f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2333f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2334f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2335a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2336a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2337a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2338a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2339a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2340a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2341a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2342a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2343f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2344f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2345f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2346f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2347f7788e8eSJonathan Lemon 
2348a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2349a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2350a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2351f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2352f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
23537137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2354a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
23553ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2356a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2357a026a25bSPyun YongHyeon 	cbp->mc_all =		ifp->if_flags & IFF_ALLMULTI ? 1 : prm;
2358c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2359bd4fa9d9SPyun YongHyeon 	cbp->vlan_strip_en =	((sc->flags & FXP_FLAG_EXT_RFA) != 0 &&
2360bd4fa9d9SPyun YongHyeon 	    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0;
2361a17c678eSDavid Greenman 
23620f1db1d6SMaxime Henrion 	if (sc->tunable_noflow || sc->revision == FXP_REV_82557) {
23633bd07cfdSJonathan Lemon 		/*
23643bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
23653bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
23663bd07cfdSJonathan Lemon 		 */
23673bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
23683bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
23693bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23703bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
23713bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
23723bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
23733bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
23743bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
23753bd07cfdSJonathan Lemon 	} else {
23763bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
23773bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
23783bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
23793bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
23803bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
23813bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
23823bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
23833bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
23843bd07cfdSJonathan Lemon 	}
23853bd07cfdSJonathan Lemon 
2386a17c678eSDavid Greenman 	/*
2387a17c678eSDavid Greenman 	 * Start the config command/DMA.
2388a17c678eSDavid Greenman 	 */
2389ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23905986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
23915986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2392b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
23932e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2394a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2395209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2396a17c678eSDavid Greenman 
2397a17c678eSDavid Greenman 	/*
2398a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2399a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2400a17c678eSDavid Greenman 	 */
2401b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2402a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
240383e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
240483e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
24054a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2406a17c678eSDavid Greenman 
2407a17c678eSDavid Greenman 	/*
2408a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2409a17c678eSDavid Greenman 	 */
2410ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24115986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24125986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
24136b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24142e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2415a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2416209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2417a17c678eSDavid Greenman 
2418a17c678eSDavid Greenman 	/*
24196b24912cSPyun YongHyeon 	 * Initialize the multicast address list.
24206b24912cSPyun YongHyeon 	 */
24216b24912cSPyun YongHyeon 	fxp_mc_setup(sc);
24226b24912cSPyun YongHyeon 
24236b24912cSPyun YongHyeon 	/*
2424a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2425a17c678eSDavid Greenman 	 */
2426b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2427b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2428b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2429a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2430b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
243183e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
243283e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
243383e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
243483e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
24353bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2436b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
243783e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
24383bd07cfdSJonathan Lemon 		else
2439b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
244083e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2441b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2442a17c678eSDavid Greenman 	}
2443a17c678eSDavid Greenman 	/*
2444397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2445a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2446a17c678eSDavid Greenman 	 */
244783e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2448a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2449a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2450b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2451397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2452a17c678eSDavid Greenman 
2453ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24546b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24552e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2456a17c678eSDavid Greenman 
2457a17c678eSDavid Greenman 	/*
2458a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2459a17c678eSDavid Greenman 	 */
2460ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2461b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
24622e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2463a17c678eSDavid Greenman 
2464dccee1a1SDavid Greenman 	/*
2465ba8c6fd5SDavid Greenman 	 * Set current media.
2466dccee1a1SDavid Greenman 	 */
2467f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2468f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2469dccee1a1SDavid Greenman 
247013f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
247113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2472e8c8b728SJonathan Lemon 
2473e8c8b728SJonathan Lemon 	/*
2474e8c8b728SJonathan Lemon 	 * Enable interrupts.
2475e8c8b728SJonathan Lemon 	 */
24762b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
24772b5989e9SLuigi Rizzo 	/*
24782b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
24792b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
24802b5989e9SLuigi Rizzo 	 */
248140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
24822b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
24832b5989e9SLuigi Rizzo 	else
24842b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2485e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2486a17c678eSDavid Greenman 
2487a17c678eSDavid Greenman 	/*
2488a17c678eSDavid Greenman 	 * Start stats updater.
2489a17c678eSDavid Greenman 	 */
249045276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2491f7788e8eSJonathan Lemon }
2492f7788e8eSJonathan Lemon 
2493f7788e8eSJonathan Lemon static int
2494f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2495f7788e8eSJonathan Lemon {
2496f7788e8eSJonathan Lemon 
2497f7788e8eSJonathan Lemon 	return (0);
2498a17c678eSDavid Greenman }
2499a17c678eSDavid Greenman 
2500303b270bSEivind Eklund static void
2501f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2502ba8c6fd5SDavid Greenman {
2503ba8c6fd5SDavid Greenman 
2504f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2505ba8c6fd5SDavid Greenman }
2506ba8c6fd5SDavid Greenman 
2507ba8c6fd5SDavid Greenman /*
2508ba8c6fd5SDavid Greenman  * Change media according to request.
2509ba8c6fd5SDavid Greenman  */
2510f7788e8eSJonathan Lemon static int
2511f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2512ba8c6fd5SDavid Greenman {
2513ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2514f7788e8eSJonathan Lemon 	struct mii_data *mii;
2515ba8c6fd5SDavid Greenman 
2516f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25173212724cSJohn Baldwin 	FXP_LOCK(sc);
25185aa0cdf4SJohn-Mark Gurney 	if (mii->mii_instance) {
25195aa0cdf4SJohn-Mark Gurney 		struct mii_softc	*miisc;
25205aa0cdf4SJohn-Mark Gurney 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
25215aa0cdf4SJohn-Mark Gurney 			mii_phy_reset(miisc);
25225aa0cdf4SJohn-Mark Gurney 	}
2523f7788e8eSJonathan Lemon 	mii_mediachg(mii);
25243212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2525ba8c6fd5SDavid Greenman 	return (0);
2526ba8c6fd5SDavid Greenman }
2527ba8c6fd5SDavid Greenman 
2528ba8c6fd5SDavid Greenman /*
2529ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2530ba8c6fd5SDavid Greenman  */
2531f7788e8eSJonathan Lemon static void
2532f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2533ba8c6fd5SDavid Greenman {
2534ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2535f7788e8eSJonathan Lemon 	struct mii_data *mii;
2536ba8c6fd5SDavid Greenman 
2537f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
25383212724cSJohn Baldwin 	FXP_LOCK(sc);
2539f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2540f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2541f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
25422e2b8238SJonathan Lemon 
25432b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
25442b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
25452e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
25462e2b8238SJonathan Lemon 	else
25472e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
25483212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2549ba8c6fd5SDavid Greenman }
2550ba8c6fd5SDavid Greenman 
2551a17c678eSDavid Greenman /*
2552a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2553a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
255485050421SPyun YongHyeon  * reusing the RFA buffer.
2555a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2556a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2557a17c678eSDavid Greenman  */
2558a17c678eSDavid Greenman static int
255985050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2560a17c678eSDavid Greenman {
2561a17c678eSDavid Greenman 	struct mbuf *m;
256285050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2563b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
256485050421SPyun YongHyeon 	int error;
2565a17c678eSDavid Greenman 
2566a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
256785050421SPyun YongHyeon 	if (m == NULL)
256885050421SPyun YongHyeon 		return (ENOBUFS);
2569ba8c6fd5SDavid Greenman 
2570ba8c6fd5SDavid Greenman 	/*
2571ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2572ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2573ba8c6fd5SDavid Greenman 	 */
2574ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2575ba8c6fd5SDavid Greenman 
2576eadd5e3aSDavid Greenman 	/*
2577eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2578eadd5e3aSDavid Greenman 	 * data start past it.
2579eadd5e3aSDavid Greenman 	 */
2580a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2581c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
258283e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2583eadd5e3aSDavid Greenman 
2584a17c678eSDavid Greenman 	rfa->rfa_status = 0;
258583e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2586a17c678eSDavid Greenman 	rfa->actual_size = 0;
258785050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
258885050421SPyun YongHyeon 	    sc->rfa_size;
2589ba8c6fd5SDavid Greenman 
259028935f27SMaxime Henrion 	/*
259128935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
259228935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
259328935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
259428935f27SMaxime Henrion 	 * is also alignment-safe.
259528935f27SMaxime Henrion 	 */
259683e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
259783e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2598ba8c6fd5SDavid Greenman 
2599b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2600a2057a72SPyun YongHyeon 	error = bus_dmamap_load(sc->fxp_rxmtag, sc->spare_map, rfa,
2601b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2602b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2603b2badf02SMaxime Henrion 	if (error) {
2604b2badf02SMaxime Henrion 		m_freem(m);
2605b2badf02SMaxime Henrion 		return (error);
2606b2badf02SMaxime Henrion 	}
2607b2badf02SMaxime Henrion 
2608e2157cf7SPyun YongHyeon 	if (rxp->rx_mbuf != NULL)
2609a2057a72SPyun YongHyeon 		bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
2610b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2611b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2612b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2613b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2614b2badf02SMaxime Henrion 
2615a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
2616b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
261785050421SPyun YongHyeon 	return (0);
261885050421SPyun YongHyeon }
261985050421SPyun YongHyeon 
262085050421SPyun YongHyeon static void
262185050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
262285050421SPyun YongHyeon {
262385050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
262485050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2625b2badf02SMaxime Henrion 
2626dfe61cf1SDavid Greenman 	/*
2627dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2628dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2629dfe61cf1SDavid Greenman 	 */
2630b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2631b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2632b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2633b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2634b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
263583e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2636aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2637a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, p_rx->rx_map,
26384cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2639a17c678eSDavid Greenman 	} else {
2640b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2641b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2642a17c678eSDavid Greenman 	}
2643b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
264485050421SPyun YongHyeon }
264585050421SPyun YongHyeon 
264685050421SPyun YongHyeon static void
264785050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
264885050421SPyun YongHyeon {
264985050421SPyun YongHyeon 	struct mbuf *m;
265085050421SPyun YongHyeon 	struct fxp_rfa *rfa;
265185050421SPyun YongHyeon 
265285050421SPyun YongHyeon 	m = rxp->rx_mbuf;
265385050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
265485050421SPyun YongHyeon 	/*
265585050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
265685050421SPyun YongHyeon 	 * will be 32-bit aligned.
265785050421SPyun YongHyeon 	 */
265885050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
265985050421SPyun YongHyeon 
266085050421SPyun YongHyeon 	/*
266185050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
266285050421SPyun YongHyeon 	 * data start past it.
266385050421SPyun YongHyeon 	 */
266485050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
266585050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
266685050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
266785050421SPyun YongHyeon 
266885050421SPyun YongHyeon 	rfa->rfa_status = 0;
266985050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
267085050421SPyun YongHyeon 	rfa->actual_size = 0;
267185050421SPyun YongHyeon 
267285050421SPyun YongHyeon 	/*
267385050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
267485050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
267585050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
267685050421SPyun YongHyeon 	 * is also alignment-safe.
267785050421SPyun YongHyeon 	 */
267885050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
267985050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
268085050421SPyun YongHyeon 
2681a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
268285050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2683a17c678eSDavid Greenman }
2684a17c678eSDavid Greenman 
2685f1928b0cSKevin Lo static int
2686f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2687dccee1a1SDavid Greenman {
2688f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2689dccee1a1SDavid Greenman 	int count = 10000;
26906ebc3153SDavid Greenman 	int value;
2691dccee1a1SDavid Greenman 
2692ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2693ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2694dccee1a1SDavid Greenman 
2695ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2696ba8c6fd5SDavid Greenman 	    && count--)
26976ebc3153SDavid Greenman 		DELAY(10);
2698dccee1a1SDavid Greenman 
2699dccee1a1SDavid Greenman 	if (count <= 0)
2700f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2701dccee1a1SDavid Greenman 
27026ebc3153SDavid Greenman 	return (value & 0xffff);
2703dccee1a1SDavid Greenman }
2704dccee1a1SDavid Greenman 
270516ec4b00SWarner Losh static int
2706f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2707dccee1a1SDavid Greenman {
2708f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2709dccee1a1SDavid Greenman 	int count = 10000;
2710dccee1a1SDavid Greenman 
2711ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2712ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2713ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2714dccee1a1SDavid Greenman 
2715ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2716ba8c6fd5SDavid Greenman 	    count--)
27176ebc3153SDavid Greenman 		DELAY(10);
2718dccee1a1SDavid Greenman 
2719dccee1a1SDavid Greenman 	if (count <= 0)
2720f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
272116ec4b00SWarner Losh 	return (0);
2722dccee1a1SDavid Greenman }
2723dccee1a1SDavid Greenman 
2724dccee1a1SDavid Greenman static int
2725f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2726a17c678eSDavid Greenman {
27279b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2728a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2729f7788e8eSJonathan Lemon 	struct mii_data *mii;
273060bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2731a17c678eSDavid Greenman 
2732a17c678eSDavid Greenman 	switch (command) {
2733a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
27343212724cSJohn Baldwin 		FXP_LOCK(sc);
2735a17c678eSDavid Greenman 		/*
2736a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2737a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2738a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2739a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2740a17c678eSDavid Greenman 		 */
2741a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
27426b24912cSPyun YongHyeon 			if (((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) &&
27436b24912cSPyun YongHyeon 			    ((ifp->if_flags ^ sc->if_flags) &
27446b24912cSPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI | IFF_LINK0)) != 0)
27456b24912cSPyun YongHyeon 				fxp_init_body(sc);
27466b24912cSPyun YongHyeon 			else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
27474953bccaSNate Lawson 				fxp_init_body(sc);
2748a17c678eSDavid Greenman 		} else {
27496b24912cSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
27504a5f1499SDavid Greenman 				fxp_stop(sc);
2751a17c678eSDavid Greenman 		}
27526b24912cSPyun YongHyeon 		sc->if_flags = ifp->if_flags;
27533212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2754a17c678eSDavid Greenman 		break;
2755a17c678eSDavid Greenman 
2756a17c678eSDavid Greenman 	case SIOCADDMULTI:
2757a17c678eSDavid Greenman 	case SIOCDELMULTI:
27586b24912cSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
27596b24912cSPyun YongHyeon 			fxp_init(sc);
2760ba8c6fd5SDavid Greenman 		break;
2761ba8c6fd5SDavid Greenman 
2762ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2763ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2764f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2765f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2766f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2767f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2768f7788e8eSJonathan Lemon 		} else {
2769ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2770f7788e8eSJonathan Lemon 		}
2771a17c678eSDavid Greenman 		break;
2772a17c678eSDavid Greenman 
2773fb917226SRuslan Ermilov 	case SIOCSIFCAP:
277460bb79ebSPyun YongHyeon 		reinit = 0;
27758ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
277640929967SGleb Smirnoff #ifdef DEVICE_POLLING
277740929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
277840929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
277940929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
278040929967SGleb Smirnoff 				if (error)
278140929967SGleb Smirnoff 					return(error);
278240929967SGleb Smirnoff 				FXP_LOCK(sc);
278340929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
278440929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
278540929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
278640929967SGleb Smirnoff 				FXP_UNLOCK(sc);
278740929967SGleb Smirnoff 			} else {
278840929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
278940929967SGleb Smirnoff 				/* Enable interrupts in any case */
279040929967SGleb Smirnoff 				FXP_LOCK(sc);
279140929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
279240929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
279340929967SGleb Smirnoff 				FXP_UNLOCK(sc);
279440929967SGleb Smirnoff 			}
279540929967SGleb Smirnoff 		}
279640929967SGleb Smirnoff #endif
279740929967SGleb Smirnoff 		FXP_LOCK(sc);
279860bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
279960bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
280060bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
280160bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
280260bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
280360bb79ebSPyun YongHyeon 			else
280460bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
280560bb79ebSPyun YongHyeon 		}
280660bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2807f13075afSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
280860bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2809f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2810f13075afSPyun YongHyeon 				reinit++;
2811f13075afSPyun YongHyeon 		}
2812c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
2813c21e84e4SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2814c21e84e4SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2815c21e84e4SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
2816c21e84e4SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2817c21e84e4SPyun YongHyeon 			else
2818c21e84e4SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2819c21e84e4SPyun YongHyeon 		}
28207137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
28217137cea0SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
28227137cea0SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
282360bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
282460bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
28258ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
28268ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
28278ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
28288ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
28298ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
28308ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
28318ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
283260bb79ebSPyun YongHyeon 				reinit++;
283360bb79ebSPyun YongHyeon 		}
2834bd4fa9d9SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2835bd4fa9d9SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2836bd4fa9d9SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2837bd4fa9d9SPyun YongHyeon 				reinit++;
2838bd4fa9d9SPyun YongHyeon 		}
2839bd4fa9d9SPyun YongHyeon 		if (reinit > 0 && ifp->if_flags & IFF_UP)
28408ef1f631SYaroslav Tykhiy 			fxp_init_body(sc);
28413212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2842bd4fa9d9SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
2843fb917226SRuslan Ermilov 		break;
2844fb917226SRuslan Ermilov 
2845a17c678eSDavid Greenman 	default:
2846673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2847a17c678eSDavid Greenman 	}
2848a17c678eSDavid Greenman 	return (error);
2849a17c678eSDavid Greenman }
2850397f9dfeSDavid Greenman 
2851397f9dfeSDavid Greenman /*
285209882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
285309882363SJonathan Lemon  */
285409882363SJonathan Lemon static int
285509882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
285609882363SJonathan Lemon {
285709882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2858fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
285909882363SJonathan Lemon 	struct ifmultiaddr *ifma;
286009882363SJonathan Lemon 	int nmcasts;
286109882363SJonathan Lemon 
286209882363SJonathan Lemon 	nmcasts = 0;
28636b24912cSPyun YongHyeon 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2864eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
286509882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
286609882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
286709882363SJonathan Lemon 				continue;
286809882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
28696b24912cSPyun YongHyeon 				ifp->if_flags |= IFF_ALLMULTI;
287009882363SJonathan Lemon 				nmcasts = 0;
287109882363SJonathan Lemon 				break;
287209882363SJonathan Lemon 			}
287309882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2874bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
287509882363SJonathan Lemon 			nmcasts++;
287609882363SJonathan Lemon 		}
2877eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
287809882363SJonathan Lemon 	}
2879bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
288009882363SJonathan Lemon 	return (nmcasts);
288109882363SJonathan Lemon }
288209882363SJonathan Lemon 
288309882363SJonathan Lemon /*
2884397f9dfeSDavid Greenman  * Program the multicast filter.
2885397f9dfeSDavid Greenman  *
2886397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2887397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
28883114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2889397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2890dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2891397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2892397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2893397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2894397f9dfeSDavid Greenman  */
2895397f9dfeSDavid Greenman static void
2896f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2897397f9dfeSDavid Greenman {
28986b24912cSPyun YongHyeon 	struct fxp_cb_mcs *mcsp;
28997dced78aSDavid Greenman 	int count;
2900397f9dfeSDavid Greenman 
290167fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
29023114fdb4SDavid Greenman 
29036b24912cSPyun YongHyeon 	mcsp = sc->mcsp;
2904397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
29056b24912cSPyun YongHyeon 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
29066b24912cSPyun YongHyeon 	mcsp->link_addr = 0xffffffff;
29076b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
2908397f9dfeSDavid Greenman 
2909397f9dfeSDavid Greenman 	/*
29106b24912cSPyun YongHyeon 	 * Wait until command unit is idle. This should never be the
29116b24912cSPyun YongHyeon 	 * case when nothing is queued, but make sure anyway.
2912397f9dfeSDavid Greenman 	 */
29137dced78aSDavid Greenman 	count = 100;
29146b24912cSPyun YongHyeon 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) !=
29156b24912cSPyun YongHyeon 	    FXP_SCB_CUS_IDLE && --count)
29167dced78aSDavid Greenman 		DELAY(10);
29177dced78aSDavid Greenman 	if (count == 0) {
2918f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
29197dced78aSDavid Greenman 		return;
29207dced78aSDavid Greenman 	}
2921397f9dfeSDavid Greenman 
2922397f9dfeSDavid Greenman 	/*
2923397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2924397f9dfeSDavid Greenman 	 */
2925397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2926a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
2927a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2928b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
29292e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
29306b24912cSPyun YongHyeon 	/* ...and wait for it to complete. */
29316b24912cSPyun YongHyeon 	fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
2932397f9dfeSDavid Greenman }
293372a32a26SJonathan Lemon 
293474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
293574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
293674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
293774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
293874d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
293974d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2940de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
294172a32a26SJonathan Lemon 
294274d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
294372a32a26SJonathan Lemon 
294472a32a26SJonathan Lemon struct ucode {
294574d1ed23SMaxime Henrion 	uint32_t	revision;
294674d1ed23SMaxime Henrion 	uint32_t	*ucode;
294772a32a26SJonathan Lemon 	int		length;
294872a32a26SJonathan Lemon 	u_short		int_delay_offset;
294972a32a26SJonathan Lemon 	u_short		bundle_max_offset;
295072a32a26SJonathan Lemon } ucode_table[] = {
295172a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
295272a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
295372a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
295472a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
295572a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
295672a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
295772a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
295872a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
295972a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
296072a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2961507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
2962de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
296372a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
296472a32a26SJonathan Lemon };
296572a32a26SJonathan Lemon 
296672a32a26SJonathan Lemon static void
296772a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
296872a32a26SJonathan Lemon {
296972a32a26SJonathan Lemon 	struct ucode *uc;
297072a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
297194a4f968SPyun YongHyeon 	int i;
297272a32a26SJonathan Lemon 
297372a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
297472a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
297572a32a26SJonathan Lemon 			break;
297672a32a26SJonathan Lemon 	if (uc->ucode == NULL)
297772a32a26SJonathan Lemon 		return;
2978b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
297972a32a26SJonathan Lemon 	cbp->cb_status = 0;
298083e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
298183e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
298294a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
298394a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
298472a32a26SJonathan Lemon 	if (uc->int_delay_offset)
298574d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
298683e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
298772a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
298874d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
298983e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
299072a32a26SJonathan Lemon 	/*
299172a32a26SJonathan Lemon 	 * Download the ucode to the chip.
299272a32a26SJonathan Lemon 	 */
299372a32a26SJonathan Lemon 	fxp_scb_wait(sc);
29945986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
29955986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2996b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
299772a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
299872a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2999209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
300072a32a26SJonathan Lemon 	device_printf(sc->dev,
300172a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
300272a32a26SJonathan Lemon 	    sc->tunable_int_delay,
300372a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
300472a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
300572a32a26SJonathan Lemon }
300672a32a26SJonathan Lemon 
300772a32a26SJonathan Lemon static int
300872a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
300972a32a26SJonathan Lemon {
301072a32a26SJonathan Lemon 	int error, value;
301172a32a26SJonathan Lemon 
301272a32a26SJonathan Lemon 	value = *(int *)arg1;
301372a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
301472a32a26SJonathan Lemon 	if (error || !req->newptr)
301572a32a26SJonathan Lemon 		return (error);
301672a32a26SJonathan Lemon 	if (value < low || value > high)
301772a32a26SJonathan Lemon 		return (EINVAL);
301872a32a26SJonathan Lemon 	*(int *)arg1 = value;
301972a32a26SJonathan Lemon 	return (0);
302072a32a26SJonathan Lemon }
302172a32a26SJonathan Lemon 
302272a32a26SJonathan Lemon /*
302372a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
302472a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
302572a32a26SJonathan Lemon  */
302672a32a26SJonathan Lemon static int
302772a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
302872a32a26SJonathan Lemon {
302972a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
303072a32a26SJonathan Lemon }
303172a32a26SJonathan Lemon 
303272a32a26SJonathan Lemon static int
303372a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
303472a32a26SJonathan Lemon {
303572a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
303672a32a26SJonathan Lemon }
3037