xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1f7788e8eSJonathan Lemon /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3*718cf2ccSPedro F. Giffuni  *
4a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
53bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
6a17c678eSDavid Greenman  * All rights reserved.
7a17c678eSDavid Greenman  *
8a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
9a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
10a17c678eSDavid Greenman  * are met:
11a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
12a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
13a17c678eSDavid Greenman  *    disclaimer.
14a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
15a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
16a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
17a17c678eSDavid Greenman  *
18a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a17c678eSDavid Greenman  * SUCH DAMAGE.
29a17c678eSDavid Greenman  *
30a17c678eSDavid Greenman  */
31a17c678eSDavid Greenman 
32aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
33aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
34aad970f1SDavid E. O'Brien 
35a17c678eSDavid Greenman /*
36ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
37a17c678eSDavid Greenman  */
38a17c678eSDavid Greenman 
39f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
40f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
41f0796cd2SGleb Smirnoff #endif
42f0796cd2SGleb Smirnoff 
43a17c678eSDavid Greenman #include <sys/param.h>
44a17c678eSDavid Greenman #include <sys/systm.h>
458fae3bd4SPyun YongHyeon #include <sys/bus.h>
4683e6547dSMaxime Henrion #include <sys/endian.h>
47a17c678eSDavid Greenman #include <sys/kernel.h>
488fae3bd4SPyun YongHyeon #include <sys/mbuf.h>
496d7e1582SPyun YongHyeon #include <sys/lock.h>
508ec07310SGleb Smirnoff #include <sys/malloc.h>
51fe12f24bSPoul-Henning Kamp #include <sys/module.h>
526d7e1582SPyun YongHyeon #include <sys/mutex.h>
538fae3bd4SPyun YongHyeon #include <sys/rman.h>
544458ac71SBruce Evans #include <sys/socket.h>
558fae3bd4SPyun YongHyeon #include <sys/sockio.h>
5672a32a26SJonathan Lemon #include <sys/sysctl.h>
57a17c678eSDavid Greenman 
588fae3bd4SPyun YongHyeon #include <net/bpf.h>
598fae3bd4SPyun YongHyeon #include <net/ethernet.h>
60a17c678eSDavid Greenman #include <net/if.h>
6176039bc8SGleb Smirnoff #include <net/if_var.h>
628fae3bd4SPyun YongHyeon #include <net/if_arp.h>
63397f9dfeSDavid Greenman #include <net/if_dl.h>
64ba8c6fd5SDavid Greenman #include <net/if_media.h>
65e8c8b728SJonathan Lemon #include <net/if_types.h>
66e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
67e8c8b728SJonathan Lemon 
68c8bca6dcSBill Paul #include <netinet/in.h>
69c8bca6dcSBill Paul #include <netinet/in_systm.h>
70c8bca6dcSBill Paul #include <netinet/ip.h>
71f13075afSPyun YongHyeon #include <netinet/tcp.h>
72f13075afSPyun YongHyeon #include <netinet/udp.h>
73f13075afSPyun YongHyeon 
74f13075afSPyun YongHyeon #include <machine/bus.h>
75c8bca6dcSBill Paul #include <machine/in_cksum.h>
76f13075afSPyun YongHyeon #include <machine/resource.h>
77c8bca6dcSBill Paul 
784fbd232cSWarner Losh #include <dev/pci/pcivar.h>
794fbd232cSWarner Losh #include <dev/pci/pcireg.h>		/* for PCIM_CMD_xxx */
80a17c678eSDavid Greenman 
81f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
82f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
83f7788e8eSJonathan Lemon 
84f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
85f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8672a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
87f7788e8eSJonathan Lemon 
88f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
89f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
90f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
91f7788e8eSJonathan Lemon #include "miibus_if.h"
924fc1dda9SAndrew Gallatin 
93ba8c6fd5SDavid Greenman /*
94658c8398SMarius Strobl  * NOTE!  On !x86 we typically have an alignment constraint.  The
95ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
96ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
97ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
98ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
99ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
100ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
101ba8c6fd5SDavid Greenman  */
102ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
103ba8c6fd5SDavid Greenman 
104ba8c6fd5SDavid Greenman /*
105f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
106f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
107f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
108f7788e8eSJonathan Lemon  */
109f7788e8eSJonathan Lemon static int tx_threshold = 64;
110f7788e8eSJonathan Lemon 
111f7788e8eSJonathan Lemon /*
112f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
11372517829SPyun YongHyeon  * must be one or must be zero.  Set up a template for these bits.
114e0fe5c6dSMarius Strobl  * The actual configuration is performed in fxp_init_body.
115f7788e8eSJonathan Lemon  *
116f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
117f7788e8eSJonathan Lemon  */
11829658c96SDimitry Andric static const u_char fxp_cb_config_template[] = {
119f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
120f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
121f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
122f7788e8eSJonathan Lemon 	0x0,	/*  0 */
123f7788e8eSJonathan Lemon 	0x0,	/*  1 */
124f7788e8eSJonathan Lemon 	0x0,	/*  2 */
125f7788e8eSJonathan Lemon 	0x0,	/*  3 */
126f7788e8eSJonathan Lemon 	0x0,	/*  4 */
127f7788e8eSJonathan Lemon 	0x0,	/*  5 */
128f7788e8eSJonathan Lemon 	0x32,	/*  6 */
129f7788e8eSJonathan Lemon 	0x0,	/*  7 */
130f7788e8eSJonathan Lemon 	0x0,	/*  8 */
131f7788e8eSJonathan Lemon 	0x0,	/*  9 */
132f7788e8eSJonathan Lemon 	0x6,	/* 10 */
133f7788e8eSJonathan Lemon 	0x0,	/* 11 */
134f7788e8eSJonathan Lemon 	0x0,	/* 12 */
135f7788e8eSJonathan Lemon 	0x0,	/* 13 */
136f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
137f7788e8eSJonathan Lemon 	0x48,	/* 15 */
138f7788e8eSJonathan Lemon 	0x0,	/* 16 */
139f7788e8eSJonathan Lemon 	0x40,	/* 17 */
140f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
141f7788e8eSJonathan Lemon 	0x0,	/* 19 */
142f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
14372517829SPyun YongHyeon 	0x5,	/* 21 */
14472517829SPyun YongHyeon 	0x0,	/* 22 */
14572517829SPyun YongHyeon 	0x0,	/* 23 */
14672517829SPyun YongHyeon 	0x0,	/* 24 */
14772517829SPyun YongHyeon 	0x0,	/* 25 */
14872517829SPyun YongHyeon 	0x0,	/* 26 */
14972517829SPyun YongHyeon 	0x0,	/* 27 */
15072517829SPyun YongHyeon 	0x0,	/* 28 */
15172517829SPyun YongHyeon 	0x0,	/* 29 */
15272517829SPyun YongHyeon 	0x0,	/* 30 */
15372517829SPyun YongHyeon 	0x0	/* 31 */
154f7788e8eSJonathan Lemon };
155f7788e8eSJonathan Lemon 
156f7788e8eSJonathan Lemon /*
157f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
158f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
159f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
160f7788e8eSJonathan Lemon  * them.
161f7788e8eSJonathan Lemon  */
16229658c96SDimitry Andric static const struct fxp_ident fxp_ident_table[] = {
163aa6b24dcSWarner Losh     { 0x8086, 0x1029,	-1,	0, "Intel 82559 PCI/CardBus Pro/100" },
164aa6b24dcSWarner Losh     { 0x8086, 0x1030,	-1,	0, "Intel 82559 Pro/100 Ethernet" },
165aa6b24dcSWarner Losh     { 0x8086, 0x1031,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
166aa6b24dcSWarner Losh     { 0x8086, 0x1032,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
167aa6b24dcSWarner Losh     { 0x8086, 0x1033,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
168aa6b24dcSWarner Losh     { 0x8086, 0x1034,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
169aa6b24dcSWarner Losh     { 0x8086, 0x1035,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
170aa6b24dcSWarner Losh     { 0x8086, 0x1036,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
171aa6b24dcSWarner Losh     { 0x8086, 0x1037,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
172aa6b24dcSWarner Losh     { 0x8086, 0x1038,	-1,	3, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
173aa6b24dcSWarner Losh     { 0x8086, 0x1039,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
174aa6b24dcSWarner Losh     { 0x8086, 0x103A,	-1,	4, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
175aa6b24dcSWarner Losh     { 0x8086, 0x103B,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
176aa6b24dcSWarner Losh     { 0x8086, 0x103C,	-1,	4, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
177aa6b24dcSWarner Losh     { 0x8086, 0x103D,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
178aa6b24dcSWarner Losh     { 0x8086, 0x103E,	-1,	4, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
179aa6b24dcSWarner Losh     { 0x8086, 0x1050,	-1,	5, "Intel 82801BA (D865) Pro/100 VE Ethernet" },
180aa6b24dcSWarner Losh     { 0x8086, 0x1051,	-1,	5, "Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
181aa6b24dcSWarner Losh     { 0x8086, 0x1059,	-1,	0, "Intel 82551QM Pro/100 M Mobile Connection" },
182aa6b24dcSWarner Losh     { 0x8086, 0x1064,	-1,	6, "Intel 82562EZ (ICH6)" },
183aa6b24dcSWarner Losh     { 0x8086, 0x1065,	-1,	6, "Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
184aa6b24dcSWarner Losh     { 0x8086, 0x1068,	-1,	6, "Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
185aa6b24dcSWarner Losh     { 0x8086, 0x1069,	-1,	6, "Intel 82562EM/EX/GX Pro/100 Ethernet" },
186aa6b24dcSWarner Losh     { 0x8086, 0x1091,	-1,	7, "Intel 82562GX Pro/100 Ethernet" },
187aa6b24dcSWarner Losh     { 0x8086, 0x1092,	-1,	7, "Intel Pro/100 VE Network Connection" },
188aa6b24dcSWarner Losh     { 0x8086, 0x1093,	-1,	7, "Intel Pro/100 VM Network Connection" },
189aa6b24dcSWarner Losh     { 0x8086, 0x1094,	-1,	7, "Intel Pro/100 946GZ (ICH7) Network Connection" },
190aa6b24dcSWarner Losh     { 0x8086, 0x1209,	-1,	0, "Intel 82559ER Embedded 10/100 Ethernet" },
191aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x01,	0, "Intel 82557 Pro/100 Ethernet" },
192aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x02,	0, "Intel 82557 Pro/100 Ethernet" },
193aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x03,	0, "Intel 82557 Pro/100 Ethernet" },
194aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x04,	0, "Intel 82558 Pro/100 Ethernet" },
195aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x05,	0, "Intel 82558 Pro/100 Ethernet" },
196aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x06,	0, "Intel 82559 Pro/100 Ethernet" },
197aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x07,	0, "Intel 82559 Pro/100 Ethernet" },
198aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x08,	0, "Intel 82559 Pro/100 Ethernet" },
199aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x09,	0, "Intel 82559ER Pro/100 Ethernet" },
200aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x0c,	0, "Intel 82550 Pro/100 Ethernet" },
201aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x0d,	0, "Intel 82550C Pro/100 Ethernet" },
202aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x0e,	0, "Intel 82550 Pro/100 Ethernet" },
203aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x0f,	0, "Intel 82551 Pro/100 Ethernet" },
204aa6b24dcSWarner Losh     { 0x8086, 0x1229,	0x10,	0, "Intel 82551 Pro/100 Ethernet" },
205aa6b24dcSWarner Losh     { 0x8086, 0x1229,	-1,	0, "Intel 82557/8/9 Pro/100 Ethernet" },
206aa6b24dcSWarner Losh     { 0x8086, 0x2449,	-1,	2, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
207aa6b24dcSWarner Losh     { 0x8086, 0x27dc,	-1,	7, "Intel 82801GB (ICH7) 10/100 Ethernet" },
208aa6b24dcSWarner Losh     { 0,      0,	-1,	0, NULL },
209f7788e8eSJonathan Lemon };
210f7788e8eSJonathan Lemon 
211c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
212c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
213c8bca6dcSBill Paul #else
214c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
215c8bca6dcSBill Paul #endif
216c8bca6dcSBill Paul 
217f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
218f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
219f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
220f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
221f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
222f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
223f7788e8eSJonathan Lemon 
224e0fe5c6dSMarius Strobl static const struct fxp_ident *fxp_find_ident(device_t dev);
225f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
22641eb5ac3SMarcel Moolenaar static void		fxp_rxcsum(struct fxp_softc *sc, if_t ifp,
227f13075afSPyun YongHyeon 			    struct mbuf *m, uint16_t status, int pos);
22841eb5ac3SMarcel Moolenaar static int		fxp_intr_body(struct fxp_softc *sc, if_t ifp,
22974d1ed23SMaxime Henrion 			    uint8_t statack, int count);
230f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2311845b5c3SMarius Strobl static void 		fxp_init_body(struct fxp_softc *sc, int);
232f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
23341eb5ac3SMarcel Moolenaar static void 		fxp_start(if_t ifp);
23441eb5ac3SMarcel Moolenaar static void 		fxp_start_body(if_t ifp);
2354e53f837SPyun YongHyeon static int		fxp_encap(struct fxp_softc *sc, struct mbuf **m_head);
2364e53f837SPyun YongHyeon static void		fxp_txeof(struct fxp_softc *sc);
237f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
238f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
23941eb5ac3SMarcel Moolenaar static int		fxp_ioctl(if_t ifp, u_long command,
240f7788e8eSJonathan Lemon 			    caddr_t data);
241df79d527SGleb Smirnoff static void 		fxp_watchdog(struct fxp_softc *sc);
24285050421SPyun YongHyeon static void		fxp_add_rfabuf(struct fxp_softc *sc,
24385050421SPyun YongHyeon 			    struct fxp_rx *rxp);
24485050421SPyun YongHyeon static void		fxp_discard_rfabuf(struct fxp_softc *sc,
24585050421SPyun YongHyeon 			    struct fxp_rx *rxp);
24685050421SPyun YongHyeon static int		fxp_new_rfabuf(struct fxp_softc *sc,
24785050421SPyun YongHyeon 			    struct fxp_rx *rxp);
24809882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
249f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
25074d1ed23SMaxime Henrion static uint16_t		fxp_eeprom_getword(struct fxp_softc *sc, int offset,
251f7788e8eSJonathan Lemon 			    int autosize);
25200c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
25374d1ed23SMaxime Henrion 			    uint16_t data);
254f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
2558262183eSPyun YongHyeon static void		fxp_load_eeprom(struct fxp_softc *sc);
256f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
257f7788e8eSJonathan Lemon 			    int offset, int words);
25800c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
25900c4116bSJonathan Lemon 			    int offset, int words);
26041eb5ac3SMarcel Moolenaar static int		fxp_ifmedia_upd(if_t ifp);
26141eb5ac3SMarcel Moolenaar static void		fxp_ifmedia_sts(if_t ifp,
262f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
26341eb5ac3SMarcel Moolenaar static int		fxp_serial_ifmedia_upd(if_t ifp);
26441eb5ac3SMarcel Moolenaar static void		fxp_serial_ifmedia_sts(if_t ifp,
265f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
266f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
26716ec4b00SWarner Losh static int		fxp_miibus_writereg(device_t dev, int phy, int reg,
268f7788e8eSJonathan Lemon 			    int value);
2691845b5c3SMarius Strobl static void		fxp_miibus_statchg(device_t dev);
27072a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
2718da9c507SPyun YongHyeon static void		fxp_update_stats(struct fxp_softc *sc);
2728da9c507SPyun YongHyeon static void		fxp_sysctl_node(struct fxp_softc *sc);
27372a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
27472a32a26SJonathan Lemon 			    int low, int high);
27572a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
27672a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
27728935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
27828935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
27928935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
28074d1ed23SMaxime Henrion 			    volatile uint16_t *status, bus_dma_tag_t dmat,
281209b07bcSMaxime Henrion 			    bus_dmamap_t map);
282f7788e8eSJonathan Lemon 
283f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
284f7788e8eSJonathan Lemon 	/* Device interface */
285f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
286f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
287f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
288f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
289f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
290f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
291f7788e8eSJonathan Lemon 
292f7788e8eSJonathan Lemon 	/* MII interface */
293f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
294f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
2951845b5c3SMarius Strobl 	DEVMETHOD(miibus_statchg,	fxp_miibus_statchg),
296f7788e8eSJonathan Lemon 
297e4029d4cSMarius Strobl 	DEVMETHOD_END
298f7788e8eSJonathan Lemon };
299f7788e8eSJonathan Lemon 
300f7788e8eSJonathan Lemon static driver_t fxp_driver = {
301f7788e8eSJonathan Lemon 	"fxp",
302f7788e8eSJonathan Lemon 	fxp_methods,
303f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
304f7788e8eSJonathan Lemon };
305f7788e8eSJonathan Lemon 
306f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
307f7788e8eSJonathan Lemon 
308e4029d4cSMarius Strobl DRIVER_MODULE_ORDERED(fxp, pci, fxp_driver, fxp_devclass, NULL, NULL,
309e4029d4cSMarius Strobl     SI_ORDER_ANY);
310e4029d4cSMarius Strobl DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, NULL, NULL);
311f7788e8eSJonathan Lemon 
31205bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
31305bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
31405bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
31505bd8c22SMaxime Henrion 	{ -1, 0 }
31605bd8c22SMaxime Henrion };
31705bd8c22SMaxime Henrion 
31805bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
31905bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
32005bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
32105bd8c22SMaxime Henrion 	{ -1, 0 }
32205bd8c22SMaxime Henrion };
32305bd8c22SMaxime Henrion 
324f7788e8eSJonathan Lemon /*
325dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
326dfe61cf1SDavid Greenman  * completed).
327dfe61cf1SDavid Greenman  */
32828935f27SMaxime Henrion static void
329f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
330a17c678eSDavid Greenman {
3313cf09dd1SMarcel Moolenaar 	union {
3323cf09dd1SMarcel Moolenaar 		uint16_t w;
3333cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3343cf09dd1SMarcel Moolenaar 	} flowctl;
335a17c678eSDavid Greenman 	int i = 10000;
336a17c678eSDavid Greenman 
3377dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3387dced78aSDavid Greenman 		DELAY(2);
3393cf09dd1SMarcel Moolenaar 	if (i == 0) {
3401845b5c3SMarius Strobl 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FC_THRESH);
3411845b5c3SMarius Strobl 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FC_STATUS);
34200c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
343e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
344e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3453cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3463cf09dd1SMarcel Moolenaar 	}
3477dced78aSDavid Greenman }
3487dced78aSDavid Greenman 
34928935f27SMaxime Henrion static void
3502e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3512e2b8238SJonathan Lemon {
3522e2b8238SJonathan Lemon 
3532e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3542e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3552e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3562e2b8238SJonathan Lemon 	}
3572e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3582e2b8238SJonathan Lemon }
3592e2b8238SJonathan Lemon 
36028935f27SMaxime Henrion static void
36174d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
362209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3637dced78aSDavid Greenman {
3645986d0d2SPyun YongHyeon 	int i;
3657dced78aSDavid Greenman 
3665986d0d2SPyun YongHyeon 	for (i = 10000; i > 0; i--) {
3677dced78aSDavid Greenman 		DELAY(2);
3685986d0d2SPyun YongHyeon 		bus_dmamap_sync(dmat, map,
3695986d0d2SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3705986d0d2SPyun YongHyeon 		if ((le16toh(*status) & FXP_CB_STATUS_C) != 0)
3715986d0d2SPyun YongHyeon 			break;
372209b07bcSMaxime Henrion 	}
3737dced78aSDavid Greenman 	if (i == 0)
374f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
375a17c678eSDavid Greenman }
376a17c678eSDavid Greenman 
377e0fe5c6dSMarius Strobl static const struct fxp_ident *
378b96ad4b2SPyun YongHyeon fxp_find_ident(device_t dev)
379a17c678eSDavid Greenman {
380aa6b24dcSWarner Losh 	uint16_t vendor;
381aa6b24dcSWarner Losh 	uint16_t device;
38274d1ed23SMaxime Henrion 	uint8_t revid;
383e0fe5c6dSMarius Strobl 	const struct fxp_ident *ident;
384f7788e8eSJonathan Lemon 
385aa6b24dcSWarner Losh 	vendor = pci_get_vendor(dev);
386aa6b24dcSWarner Losh 	device = pci_get_device(dev);
387f19fc5d8SJohn Polstra 	revid = pci_get_revid(dev);
388f7788e8eSJonathan Lemon 	for (ident = fxp_ident_table; ident->name != NULL; ident++) {
389aa6b24dcSWarner Losh 		if (ident->vendor == vendor && ident->device == device &&
390f19fc5d8SJohn Polstra 		    (ident->revid == revid || ident->revid == -1)) {
391b96ad4b2SPyun YongHyeon 			return (ident);
392b96ad4b2SPyun YongHyeon 		}
393b96ad4b2SPyun YongHyeon 	}
394b96ad4b2SPyun YongHyeon 	return (NULL);
395b96ad4b2SPyun YongHyeon }
396b96ad4b2SPyun YongHyeon 
397b96ad4b2SPyun YongHyeon /*
398b96ad4b2SPyun YongHyeon  * Return identification string if this device is ours.
399b96ad4b2SPyun YongHyeon  */
400b96ad4b2SPyun YongHyeon static int
401b96ad4b2SPyun YongHyeon fxp_probe(device_t dev)
402b96ad4b2SPyun YongHyeon {
403e0fe5c6dSMarius Strobl 	const struct fxp_ident *ident;
404b96ad4b2SPyun YongHyeon 
405b96ad4b2SPyun YongHyeon 	ident = fxp_find_ident(dev);
406b96ad4b2SPyun YongHyeon 	if (ident != NULL) {
407f7788e8eSJonathan Lemon 		device_set_desc(dev, ident->name);
408538565c4SWarner Losh 		return (BUS_PROBE_DEFAULT);
40955ce7b51SDavid Greenman 	}
410f7788e8eSJonathan Lemon 	return (ENXIO);
4116182fdbdSPeter Wemm }
4126182fdbdSPeter Wemm 
413b2badf02SMaxime Henrion static void
414b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
415b2badf02SMaxime Henrion {
41674d1ed23SMaxime Henrion 	uint32_t *addr;
417b2badf02SMaxime Henrion 
418b2badf02SMaxime Henrion 	if (error)
419b2badf02SMaxime Henrion 		return;
420b2badf02SMaxime Henrion 
421b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
422b2badf02SMaxime Henrion 	addr = arg;
423b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
424b2badf02SMaxime Henrion }
425b2badf02SMaxime Henrion 
4266182fdbdSPeter Wemm static int
4276182fdbdSPeter Wemm fxp_attach(device_t dev)
428a17c678eSDavid Greenman {
4296720ebccSMaxime Henrion 	struct fxp_softc *sc;
4306720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4316720ebccSMaxime Henrion 	struct fxp_tx *txp;
432b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
43341eb5ac3SMarcel Moolenaar 	if_t ifp;
43474d1ed23SMaxime Henrion 	uint32_t val;
4358262183eSPyun YongHyeon 	uint16_t data;
436fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
4371845b5c3SMarius Strobl 	int error, flags, i, pmc, prefer_iomap;
438a17c678eSDavid Greenman 
4396720ebccSMaxime Henrion 	error = 0;
4406720ebccSMaxime Henrion 	sc = device_get_softc(dev);
441f7788e8eSJonathan Lemon 	sc->dev = dev;
4426008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4434953bccaSNate Lawson 	    MTX_DEF);
4443212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
44509a8241fSGleb Smirnoff 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4464953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
447a17c678eSDavid Greenman 
44841eb5ac3SMarcel Moolenaar 	ifp = sc->ifp = if_gethandle(IFT_ETHER);
44941eb5ac3SMarcel Moolenaar 	if (ifp == (void *)NULL) {
4507ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4517ba33d82SBrooks Davis 		error = ENOSPC;
4527ba33d82SBrooks Davis 		goto fail;
4537ba33d82SBrooks Davis 	}
4547ba33d82SBrooks Davis 
455dfe61cf1SDavid Greenman 	/*
4562bce79a2SMaxim Sobolev 	 * Enable bus mastering.
457df373873SWes Peters 	 */
458cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
45979495006SWarner Losh 
460df373873SWes Peters 	/*
4619fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4629fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4639fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
464dfe61cf1SDavid Greenman 	 */
4652a05a4ebSMatt Jacob 	prefer_iomap = 0;
46605bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
46705bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
46805bd8c22SMaxime Henrion 	if (prefer_iomap)
46905bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
47005bd8c22SMaxime Henrion 	else
47105bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4729fa6ccfbSMatt Jacob 
47305bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
47405bd8c22SMaxime Henrion 	if (error) {
47505bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
47605bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
47705bd8c22SMaxime Henrion 		else
47805bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
47905bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4809fa6ccfbSMatt Jacob 	}
48105bd8c22SMaxime Henrion 	if (error) {
48205bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4836182fdbdSPeter Wemm 		error = ENXIO;
484a17c678eSDavid Greenman 		goto fail;
485a17c678eSDavid Greenman 	}
48605bd8c22SMaxime Henrion 
4879fa6ccfbSMatt Jacob 	if (bootverbose) {
4889fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
48905bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4906182fdbdSPeter Wemm 	}
4916182fdbdSPeter Wemm 
492f7788e8eSJonathan Lemon 	/*
493a996f023SPyun YongHyeon 	 * Put CU/RU idle state and prepare full reset.
494f7788e8eSJonathan Lemon 	 */
495f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
496f7788e8eSJonathan Lemon 	DELAY(10);
497a996f023SPyun YongHyeon 	/* Full reset and disable interrupts. */
498a996f023SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
499a996f023SPyun YongHyeon 	DELAY(10);
500a996f023SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
501f7788e8eSJonathan Lemon 
502f7788e8eSJonathan Lemon 	/*
503f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
504f7788e8eSJonathan Lemon 	 */
505f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
5068262183eSPyun YongHyeon 	fxp_load_eeprom(sc);
507f7788e8eSJonathan Lemon 
508f7788e8eSJonathan Lemon 	/*
50993b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
51093b6e2e6SMaxime Henrion 	 */
511b96ad4b2SPyun YongHyeon 	sc->ident = fxp_find_ident(dev);
512b96ad4b2SPyun YongHyeon 	if (sc->ident->ich > 0) {
513b96ad4b2SPyun YongHyeon 		/* Assume ICH controllers are 82559. */
514b96ad4b2SPyun YongHyeon 		sc->revision = FXP_REV_82559_A0;
515b96ad4b2SPyun YongHyeon 	} else {
5168262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_CNTR];
51793b6e2e6SMaxime Henrion 		if ((data >> 8) == 1)
51893b6e2e6SMaxime Henrion 			sc->revision = FXP_REV_82557;
51993b6e2e6SMaxime Henrion 		else
52093b6e2e6SMaxime Henrion 			sc->revision = pci_get_revid(dev);
521b96ad4b2SPyun YongHyeon 	}
52293b6e2e6SMaxime Henrion 
52393b6e2e6SMaxime Henrion 	/*
5247137cea0SPyun YongHyeon 	 * Check availability of WOL. 82559ER does not support WOL.
5257137cea0SPyun YongHyeon 	 */
5267137cea0SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4 &&
5277137cea0SPyun YongHyeon 	    sc->revision != FXP_REV_82559S_A) {
5288262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_ID];
5297137cea0SPyun YongHyeon 		if ((data & 0x20) != 0 &&
5303b0a4aefSJohn Baldwin 		    pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0)
5317137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOLCAP;
5327137cea0SPyun YongHyeon 	}
5337137cea0SPyun YongHyeon 
5341343a72fSPyun YongHyeon 	if (sc->revision == FXP_REV_82550_C) {
5351343a72fSPyun YongHyeon 		/*
5361343a72fSPyun YongHyeon 		 * 82550C with server extension requires microcode to
5371343a72fSPyun YongHyeon 		 * receive fragmented UDP datagrams.  However if the
5381343a72fSPyun YongHyeon 		 * microcode is used for client-only featured 82550C
5391343a72fSPyun YongHyeon 		 * it locks up controller.
5401343a72fSPyun YongHyeon 		 */
5418262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_COMPAT];
5421343a72fSPyun YongHyeon 		if ((data & 0x0400) == 0)
5431343a72fSPyun YongHyeon 			sc->flags |= FXP_FLAG_NO_UCODE;
5441343a72fSPyun YongHyeon 	}
5451343a72fSPyun YongHyeon 
54643d8b117SPyun YongHyeon 	/* Receiver lock-up workaround detection. */
5476e854927SPyun YongHyeon 	if (sc->revision < FXP_REV_82558_A4) {
5488262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_COMPAT];
54943d8b117SPyun YongHyeon 		if ((data & 0x03) != 0x03) {
55043d8b117SPyun YongHyeon 			sc->flags |= FXP_FLAG_RXBUG;
55143d8b117SPyun YongHyeon 			device_printf(dev, "Enabling Rx lock-up workaround\n");
55243d8b117SPyun YongHyeon 		}
5536e854927SPyun YongHyeon 	}
55443d8b117SPyun YongHyeon 
5557137cea0SPyun YongHyeon 	/*
5563bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
557f7788e8eSJonathan Lemon 	 */
5588262183eSPyun YongHyeon 	data = sc->eeprom[FXP_EEPROM_MAP_PRI_PHY];
55993b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
5604ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
561dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
562f7788e8eSJonathan Lemon 
5638da9c507SPyun YongHyeon 	fxp_sysctl_node(sc);
56472a32a26SJonathan Lemon 	/*
5652e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
56600c4116bSJonathan Lemon 	 *
56772a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
56872a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
56972a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
57000c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
57100c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
57200c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
57300c4116bSJonathan Lemon 	 *
57400c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5752e2b8238SJonathan Lemon 	 */
576b96ad4b2SPyun YongHyeon 	if ((sc->ident->ich >= 2 && sc->ident->ich <= 3) ||
577b96ad4b2SPyun YongHyeon 	    (sc->ident->ich == 0 && sc->revision >= FXP_REV_82559_A0)) {
5788262183eSPyun YongHyeon 		data = sc->eeprom[FXP_EEPROM_MAP_ID];
57900c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
58074d1ed23SMaxime Henrion 			uint16_t cksum;
58100c4116bSJonathan Lemon 			int i;
58200c4116bSJonathan Lemon 
58300c4116bSJonathan Lemon 			device_printf(dev,
584001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
58500c4116bSJonathan Lemon 			data &= ~0x02;
5868262183eSPyun YongHyeon 			sc->eeprom[FXP_EEPROM_MAP_ID] = data;
5878262183eSPyun YongHyeon 			fxp_write_eeprom(sc, &data, FXP_EEPROM_MAP_ID, 1);
58800c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
58900c4116bSJonathan Lemon 			cksum = 0;
5908262183eSPyun YongHyeon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++)
5918262183eSPyun YongHyeon 				cksum += sc->eeprom[i];
59200c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
59300c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
59400c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
59500c4116bSJonathan Lemon 			device_printf(dev,
59600c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
5978262183eSPyun YongHyeon 			    i, sc->eeprom[i], cksum);
5988262183eSPyun YongHyeon 			sc->eeprom[i] = cksum;
59900c4116bSJonathan Lemon 			/*
60000c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
60100c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
60200c4116bSJonathan Lemon 			 */
6032e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
60400c4116bSJonathan Lemon 		}
60500c4116bSJonathan Lemon 	}
6062e2b8238SJonathan Lemon 
6072e2b8238SJonathan Lemon 	/*
6083bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
6093bd07cfdSJonathan Lemon 	 */
61072a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
6113bd07cfdSJonathan Lemon 		/*
61274396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
61374396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
61474396a0aSJonathan Lemon 		 * the board to turn on MWI.
6153bd07cfdSJonathan Lemon 		 */
616c68534f1SScott Long 		val = pci_read_config(dev, PCIR_COMMAND, 2);
61774396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
61874396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
6193bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
6203bd07cfdSJonathan Lemon 
6213bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
6223bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
62344e0bc11SYaroslav Tykhiy 
62444e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
62544e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
62644e0bc11SYaroslav Tykhiy 	} else {
62744e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
62844e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
6293bd07cfdSJonathan Lemon 	}
6303bd07cfdSJonathan Lemon 
631f13075afSPyun YongHyeon 	/* For 82559 or later chips, Rx checksum offload is supported. */
632829b278eSPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0) {
633829b278eSPyun YongHyeon 		/* 82559ER does not support Rx checksum offloading. */
634aa6b24dcSWarner Losh 		if (sc->ident->device != 0x1209)
635f13075afSPyun YongHyeon 			sc->flags |= FXP_FLAG_82559_RXCSUM;
636829b278eSPyun YongHyeon 	}
6373bd07cfdSJonathan Lemon 	/*
638c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
639c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
640c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
641c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
642c8bca6dcSBill Paul 	 */
643507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
644507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
645507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
646c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
647c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
648c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
649f13075afSPyun YongHyeon 		/* Use extended RFA instead of 82559 checksum mode. */
650f13075afSPyun YongHyeon 		sc->flags &= ~FXP_FLAG_82559_RXCSUM;
651c8bca6dcSBill Paul 	} else {
652c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
653c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
654c8bca6dcSBill Paul 	}
655c8bca6dcSBill Paul 
656c8bca6dcSBill Paul 	/*
657b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
658b2badf02SMaxime Henrion 	 */
65940c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
660c21e84e4SPyun YongHyeon 	sc->maxsegsize = MCLBYTES;
661c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
66240c20505SMaxime Henrion 		sc->maxtxseg--;
663c21e84e4SPyun YongHyeon 		sc->maxsegsize = FXP_TSO_SEGSIZE;
664c21e84e4SPyun YongHyeon 	}
665c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
666c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
667c21e84e4SPyun YongHyeon 	    sc->maxsegsize * sc->maxtxseg + sizeof(struct ether_vlan_header),
668c21e84e4SPyun YongHyeon 	    sc->maxtxseg, sc->maxsegsize, 0,
669a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_txmtag);
670b2badf02SMaxime Henrion 	if (error) {
671a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TX DMA tag\n");
672a2057a72SPyun YongHyeon 		goto fail;
673a2057a72SPyun YongHyeon 	}
674a2057a72SPyun YongHyeon 
675a2057a72SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
676a2057a72SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
677a2057a72SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0,
678a2057a72SPyun YongHyeon 	    busdma_lock_mutex, &Giant, &sc->fxp_rxmtag);
679a2057a72SPyun YongHyeon 	if (error) {
680a2057a72SPyun YongHyeon 		device_printf(dev, "could not create RX DMA tag\n");
681b2badf02SMaxime Henrion 		goto fail;
682b2badf02SMaxime Henrion 	}
683b2badf02SMaxime Henrion 
684c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
685c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
686c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
687c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
688b2badf02SMaxime Henrion 	if (error) {
689a2057a72SPyun YongHyeon 		device_printf(dev, "could not create stats DMA tag\n");
690b2badf02SMaxime Henrion 		goto fail;
691b2badf02SMaxime Henrion 	}
692b2badf02SMaxime Henrion 
693b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
694658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->fxp_smap);
695a2057a72SPyun YongHyeon 	if (error) {
696a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate stats DMA memory\n");
6974953bccaSNate Lawson 		goto fail;
698a2057a72SPyun YongHyeon 	}
699b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
700f9d050a8SPyun YongHyeon 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr,
701f9d050a8SPyun YongHyeon 	    BUS_DMA_NOWAIT);
702b2badf02SMaxime Henrion 	if (error) {
703a2057a72SPyun YongHyeon 		device_printf(dev, "could not load the stats DMA buffer\n");
704b2badf02SMaxime Henrion 		goto fail;
705b2badf02SMaxime Henrion 	}
706b2badf02SMaxime Henrion 
707c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
708c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
709c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
710c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
711b2badf02SMaxime Henrion 	if (error) {
712a2057a72SPyun YongHyeon 		device_printf(dev, "could not create TxCB DMA tag\n");
713b2badf02SMaxime Henrion 		goto fail;
714b2badf02SMaxime Henrion 	}
715b2badf02SMaxime Henrion 
716b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
717658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->cbl_map);
718a2057a72SPyun YongHyeon 	if (error) {
719a2057a72SPyun YongHyeon 		device_printf(dev, "could not allocate TxCB DMA memory\n");
7204953bccaSNate Lawson 		goto fail;
721a2057a72SPyun YongHyeon 	}
722b2badf02SMaxime Henrion 
723b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
724b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
725f9d050a8SPyun YongHyeon 	    &sc->fxp_desc.cbl_addr, BUS_DMA_NOWAIT);
726b2badf02SMaxime Henrion 	if (error) {
727a2057a72SPyun YongHyeon 		device_printf(dev, "could not load TxCB DMA buffer\n");
728b2badf02SMaxime Henrion 		goto fail;
729b2badf02SMaxime Henrion 	}
730b2badf02SMaxime Henrion 
731c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
732c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
733c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
734c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
735b2badf02SMaxime Henrion 	if (error) {
736a2057a72SPyun YongHyeon 		device_printf(dev,
737a2057a72SPyun YongHyeon 		    "could not create multicast setup DMA tag\n");
738b2badf02SMaxime Henrion 		goto fail;
739b2badf02SMaxime Henrion 	}
740b2badf02SMaxime Henrion 
741b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
742658c8398SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->mcs_map);
743a2057a72SPyun YongHyeon 	if (error) {
744a2057a72SPyun YongHyeon 		device_printf(dev,
745a2057a72SPyun YongHyeon 		    "could not allocate multicast setup DMA memory\n");
7464953bccaSNate Lawson 		goto fail;
747a2057a72SPyun YongHyeon 	}
748b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
749f9d050a8SPyun YongHyeon 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr,
750f9d050a8SPyun YongHyeon 	    BUS_DMA_NOWAIT);
751b2badf02SMaxime Henrion 	if (error) {
752a2057a72SPyun YongHyeon 		device_printf(dev,
753a2057a72SPyun YongHyeon 		    "can't load the multicast setup DMA buffer\n");
754b2badf02SMaxime Henrion 		goto fail;
755b2badf02SMaxime Henrion 	}
756b2badf02SMaxime Henrion 
757b2badf02SMaxime Henrion 	/*
7586720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
7596720ebccSMaxime Henrion 	 * the TX command blocks.
760b2badf02SMaxime Henrion 	 */
7616720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
7626720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
7634cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
7646720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
765a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_txmtag, 0, &txp[i].tx_map);
766b2badf02SMaxime Henrion 		if (error) {
767b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
768b2badf02SMaxime Henrion 			goto fail;
769b2badf02SMaxime Henrion 		}
770b2badf02SMaxime Henrion 	}
771a2057a72SPyun YongHyeon 	error = bus_dmamap_create(sc->fxp_rxmtag, 0, &sc->spare_map);
772b2badf02SMaxime Henrion 	if (error) {
773b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
774b2badf02SMaxime Henrion 		goto fail;
775b2badf02SMaxime Henrion 	}
776b2badf02SMaxime Henrion 
777b2badf02SMaxime Henrion 	/*
778b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
779b2badf02SMaxime Henrion 	 */
780b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
781b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
782b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
783a2057a72SPyun YongHyeon 		error = bus_dmamap_create(sc->fxp_rxmtag, 0, &rxp->rx_map);
784b2badf02SMaxime Henrion 		if (error) {
785b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
786b2badf02SMaxime Henrion 			goto fail;
787b2badf02SMaxime Henrion 		}
78885050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7894953bccaSNate Lawson 			error = ENOMEM;
7904953bccaSNate Lawson 			goto fail;
7914953bccaSNate Lawson 		}
79285050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
793b2badf02SMaxime Henrion 	}
794b2badf02SMaxime Henrion 
795b2badf02SMaxime Henrion 	/*
796f7788e8eSJonathan Lemon 	 * Read MAC address.
797f7788e8eSJonathan Lemon 	 */
7988262183eSPyun YongHyeon 	eaddr[0] = sc->eeprom[FXP_EEPROM_MAP_IA0] & 0xff;
7998262183eSPyun YongHyeon 	eaddr[1] = sc->eeprom[FXP_EEPROM_MAP_IA0] >> 8;
8008262183eSPyun YongHyeon 	eaddr[2] = sc->eeprom[FXP_EEPROM_MAP_IA1] & 0xff;
8018262183eSPyun YongHyeon 	eaddr[3] = sc->eeprom[FXP_EEPROM_MAP_IA1] >> 8;
8028262183eSPyun YongHyeon 	eaddr[4] = sc->eeprom[FXP_EEPROM_MAP_IA2] & 0xff;
8038262183eSPyun YongHyeon 	eaddr[5] = sc->eeprom[FXP_EEPROM_MAP_IA2] >> 8;
804f7788e8eSJonathan Lemon 	if (bootverbose) {
8052e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
806f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
8072e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
8082e2b8238SJonathan Lemon 		    pci_get_revid(dev));
80972a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
8108262183eSPyun YongHyeon 		    sc->eeprom[FXP_EEPROM_MAP_ID] & 0x02 ? "enabled" :
8118262183eSPyun YongHyeon 		    "disabled");
812f7788e8eSJonathan Lemon 	}
813f7788e8eSJonathan Lemon 
814f7788e8eSJonathan Lemon 	/*
815f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
816f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
817f7788e8eSJonathan Lemon 	 *
818f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
819f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
820f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
821f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
822f7788e8eSJonathan Lemon 	 */
823f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
824f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
825f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
826f7788e8eSJonathan Lemon 	} else {
8278e5d93dbSMarius Strobl 		/*
8288e5d93dbSMarius Strobl 		 * i82557 wedge when isolating all of their PHYs.
8298e5d93dbSMarius Strobl 		 */
8301845b5c3SMarius Strobl 		flags = MIIF_NOISOLATE;
8311845b5c3SMarius Strobl 		if (sc->revision >= FXP_REV_82558_A4)
8321845b5c3SMarius Strobl 			flags |= MIIF_DOPAUSE;
83341eb5ac3SMarcel Moolenaar 		error = mii_attach(dev, &sc->miibus, ifp,
83441eb5ac3SMarcel Moolenaar 		    (ifm_change_cb_t)fxp_ifmedia_upd,
83541eb5ac3SMarcel Moolenaar 		    (ifm_stat_cb_t)fxp_ifmedia_sts, BMSR_DEFCAPMASK,
83641eb5ac3SMarcel Moolenaar 		    MII_PHY_ANY, MII_OFFSET_ANY, flags);
8378e5d93dbSMarius Strobl 		if (error != 0) {
8388e5d93dbSMarius Strobl 			device_printf(dev, "attaching PHYs failed\n");
839ba8c6fd5SDavid Greenman 			goto fail;
840a17c678eSDavid Greenman 		}
841f7788e8eSJonathan Lemon 	}
842dccee1a1SDavid Greenman 
84309a8241fSGleb Smirnoff 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
84441eb5ac3SMarcel Moolenaar 	if_setdev(ifp, dev);
84541eb5ac3SMarcel Moolenaar 	if_setinitfn(ifp, fxp_init);
84641eb5ac3SMarcel Moolenaar 	if_setsoftc(ifp, sc);
84741eb5ac3SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
84841eb5ac3SMarcel Moolenaar 	if_setioctlfn(ifp, fxp_ioctl);
84941eb5ac3SMarcel Moolenaar 	if_setstartfn(ifp, fxp_start);
850a17c678eSDavid Greenman 
85141eb5ac3SMarcel Moolenaar 	if_setcapabilities(ifp, 0);
85241eb5ac3SMarcel Moolenaar 	if_setcapenable(ifp, 0);
8535fe9116bSYaroslav Tykhiy 
854c21e84e4SPyun YongHyeon 	/* Enable checksum offload/TSO for 82550 or better chips */
855c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
85641eb5ac3SMarcel Moolenaar 		if_sethwassist(ifp, FXP_CSUM_FEATURES | CSUM_TSO);
85741eb5ac3SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_HWCSUM | IFCAP_TSO4, 0);
85841eb5ac3SMarcel Moolenaar 		if_setcapenablebit(ifp, IFCAP_HWCSUM | IFCAP_TSO4, 0);
859c8bca6dcSBill Paul 	}
860c8bca6dcSBill Paul 
861f13075afSPyun YongHyeon 	if (sc->flags & FXP_FLAG_82559_RXCSUM) {
86241eb5ac3SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_RXCSUM, 0);
86341eb5ac3SMarcel Moolenaar 		if_setcapenablebit(ifp, IFCAP_RXCSUM, 0);
864f13075afSPyun YongHyeon 	}
865f13075afSPyun YongHyeon 
8667137cea0SPyun YongHyeon 	if (sc->flags & FXP_FLAG_WOLCAP) {
86741eb5ac3SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC, 0);
86841eb5ac3SMarcel Moolenaar 		if_setcapenablebit(ifp, IFCAP_WOL_MAGIC, 0);
8697137cea0SPyun YongHyeon 	}
8707137cea0SPyun YongHyeon 
871fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
872fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
87341eb5ac3SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
874fb917226SRuslan Ermilov #endif
875fb917226SRuslan Ermilov 
876dfe61cf1SDavid Greenman 	/*
8774953bccaSNate Lawson 	 * Attach the interface.
8784953bccaSNate Lawson 	 */
87909a8241fSGleb Smirnoff 	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 	 */
88641eb5ac3SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
88741eb5ac3SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
88841eb5ac3SMarcel Moolenaar 	if_setcapenablebit(ifp, IFCAP_VLAN_MTU, 0);
889bd4fa9d9SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_EXT_RFA) != 0) {
89041eb5ac3SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING |
89141eb5ac3SMarcel Moolenaar 		    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO, 0);
89241eb5ac3SMarcel Moolenaar 		if_setcapenablebit(ifp, IFCAP_VLAN_HWTAGGING |
89341eb5ac3SMarcel Moolenaar 		    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO, 0);
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 	 */
90041eb5ac3SMarcel Moolenaar 	if_setsendqlen(ifp, FXP_NTXCB - 1);
90141eb5ac3SMarcel Moolenaar 	if_setsendqready(ifp);
9024a684684SDavid Greenman 
903201afb0eSMaxime Henrion 	/*
9044953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
905201afb0eSMaxime Henrion 	 */
90605bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
907ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
908201afb0eSMaxime Henrion 	if (error) {
909201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
91009a8241fSGleb Smirnoff 		ether_ifdetach(sc->ifp);
911201afb0eSMaxime Henrion 		goto fail;
912201afb0eSMaxime Henrion 	}
913201afb0eSMaxime Henrion 
9147137cea0SPyun YongHyeon 	/*
9157137cea0SPyun YongHyeon 	 * Configure hardware to reject magic frames otherwise
9167137cea0SPyun YongHyeon 	 * system will hang on recipt of magic frames.
9177137cea0SPyun YongHyeon 	 */
9187137cea0SPyun YongHyeon 	if ((sc->flags & FXP_FLAG_WOLCAP) != 0) {
9197137cea0SPyun YongHyeon 		FXP_LOCK(sc);
9207137cea0SPyun YongHyeon 		/* Clear wakeup events. */
921af75b654SPyun YongHyeon 		CSR_WRITE_1(sc, FXP_CSR_PMDR, CSR_READ_1(sc, FXP_CSR_PMDR));
922a461b201SPyun YongHyeon 		fxp_init_body(sc, 0);
9237137cea0SPyun YongHyeon 		fxp_stop(sc);
9247137cea0SPyun YongHyeon 		FXP_UNLOCK(sc);
9257137cea0SPyun YongHyeon 	}
9267137cea0SPyun YongHyeon 
927a17c678eSDavid Greenman fail:
9281b5a39d3SBrooks Davis 	if (error)
929f7788e8eSJonathan Lemon 		fxp_release(sc);
930f7788e8eSJonathan Lemon 	return (error);
931f7788e8eSJonathan Lemon }
932f7788e8eSJonathan Lemon 
933f7788e8eSJonathan Lemon /*
9344953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
9354953bccaSNate Lawson  * interrupt should already be torn down.
936f7788e8eSJonathan Lemon  */
937f7788e8eSJonathan Lemon static void
938f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
939f7788e8eSJonathan Lemon {
940b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
941b2badf02SMaxime Henrion 	struct fxp_tx *txp;
942b2badf02SMaxime Henrion 	int i;
943b2badf02SMaxime Henrion 
94467fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
945670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
946670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
9474953bccaSNate Lawson 	if (sc->miibus)
9484953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
9494953bccaSNate Lawson 	bus_generic_detach(sc->dev);
9504953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
951b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
952b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
953b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
954b2badf02SMaxime Henrion 		    sc->cbl_map);
955b2badf02SMaxime Henrion 	}
956b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
957b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
958b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
959b2badf02SMaxime Henrion 	}
960b2badf02SMaxime Henrion 	if (sc->mcsp) {
961b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
962b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
963b2badf02SMaxime Henrion 	}
96405bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
965a2057a72SPyun YongHyeon 	if (sc->fxp_rxmtag) {
966b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
967b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
968b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
969a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
970b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
971a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
972b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
973b983c7b3SMaxime Henrion 			}
974a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_rxmtag, rxp->rx_map);
975b983c7b3SMaxime Henrion 		}
976a2057a72SPyun YongHyeon 		bus_dmamap_destroy(sc->fxp_rxmtag, sc->spare_map);
977a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_rxmtag);
978a2057a72SPyun YongHyeon 	}
979a2057a72SPyun YongHyeon 	if (sc->fxp_txmtag) {
980b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
981b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
982b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
983a2057a72SPyun YongHyeon 				bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
984b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
985a2057a72SPyun YongHyeon 				bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
986b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
987b983c7b3SMaxime Henrion 			}
988a2057a72SPyun YongHyeon 			bus_dmamap_destroy(sc->fxp_txmtag, txp->tx_map);
989b983c7b3SMaxime Henrion 		}
990a2057a72SPyun YongHyeon 		bus_dma_tag_destroy(sc->fxp_txmtag);
991b983c7b3SMaxime Henrion 	}
992c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
993c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
994b2badf02SMaxime Henrion 	if (sc->cbl_tag)
995b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
996b2badf02SMaxime Henrion 	if (sc->mcs_tag)
997b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
998fc74a9f9SBrooks Davis 	if (sc->ifp)
99909a8241fSGleb Smirnoff 		if_free(sc->ifp);
100072a32a26SJonathan Lemon 
10010f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
10026182fdbdSPeter Wemm }
10036182fdbdSPeter Wemm 
10046182fdbdSPeter Wemm /*
10056182fdbdSPeter Wemm  * Detach interface.
10066182fdbdSPeter Wemm  */
10076182fdbdSPeter Wemm static int
10086182fdbdSPeter Wemm fxp_detach(device_t dev)
10096182fdbdSPeter Wemm {
10106182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
10116182fdbdSPeter Wemm 
101240929967SGleb Smirnoff #ifdef DEVICE_POLLING
101341eb5ac3SMarcel Moolenaar 	if (if_getcapenable(sc->ifp) & IFCAP_POLLING)
1014bd071d4dSGleb Smirnoff 		ether_poll_deregister(sc->ifp);
101540929967SGleb Smirnoff #endif
101640929967SGleb Smirnoff 
10174953bccaSNate Lawson 	FXP_LOCK(sc);
10186182fdbdSPeter Wemm 	/*
101932cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
102020f0c80fSMaxime Henrion 	 */
102120f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
102220f0c80fSMaxime Henrion 	fxp_stop(sc);
102332cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
10249eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
102520f0c80fSMaxime Henrion 
10266182fdbdSPeter Wemm 	/*
10273212724cSJohn Baldwin 	 * Close down routes etc.
10283212724cSJohn Baldwin 	 */
102909a8241fSGleb Smirnoff 	ether_ifdetach(sc->ifp);
10303212724cSJohn Baldwin 
10313212724cSJohn Baldwin 	/*
10324953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
10334953bccaSNate Lawson 	 * races with fxp_intr().
10346182fdbdSPeter Wemm 	 */
103505bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
10364953bccaSNate Lawson 	sc->ih = NULL;
10376182fdbdSPeter Wemm 
1038f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
1039f7788e8eSJonathan Lemon 	fxp_release(sc);
1040f7788e8eSJonathan Lemon 	return (0);
1041a17c678eSDavid Greenman }
1042a17c678eSDavid Greenman 
1043a17c678eSDavid Greenman /*
10444a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
1045a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
1046a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
1047a17c678eSDavid Greenman  */
10486182fdbdSPeter Wemm static int
10496182fdbdSPeter Wemm fxp_shutdown(device_t dev)
1050a17c678eSDavid Greenman {
10513212724cSJohn Baldwin 
10526182fdbdSPeter Wemm 	/*
10536182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
10546182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
10556182fdbdSPeter Wemm 	 * reboot before the driver initializes.
10566182fdbdSPeter Wemm 	 */
10577137cea0SPyun YongHyeon 	return (fxp_suspend(dev));
1058a17c678eSDavid Greenman }
1059a17c678eSDavid Greenman 
10607dced78aSDavid Greenman /*
10617dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
10627dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
10637dced78aSDavid Greenman  * resume.
10647dced78aSDavid Greenman  */
10657dced78aSDavid Greenman static int
10667dced78aSDavid Greenman fxp_suspend(device_t dev)
10677dced78aSDavid Greenman {
10687dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
106941eb5ac3SMarcel Moolenaar 	if_t ifp;
10707137cea0SPyun YongHyeon 	int pmc;
10717137cea0SPyun YongHyeon 	uint16_t pmstat;
10727dced78aSDavid Greenman 
10734953bccaSNate Lawson 	FXP_LOCK(sc);
10747dced78aSDavid Greenman 
10757137cea0SPyun YongHyeon 	ifp = sc->ifp;
10763b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
10777137cea0SPyun YongHyeon 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
10787137cea0SPyun YongHyeon 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
107941eb5ac3SMarcel Moolenaar 		if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0) {
10807137cea0SPyun YongHyeon 			/* Request PME. */
10817137cea0SPyun YongHyeon 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
10827137cea0SPyun YongHyeon 			sc->flags |= FXP_FLAG_WOL;
10837137cea0SPyun YongHyeon 			/* Reconfigure hardware to accept magic frames. */
108441eb5ac3SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
10855506afefSPyun YongHyeon 			fxp_init_body(sc, 0);
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);
110541eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
11067137cea0SPyun YongHyeon 	int pmc;
11077137cea0SPyun YongHyeon 	uint16_t pmstat;
11087dced78aSDavid Greenman 
11094953bccaSNate Lawson 	FXP_LOCK(sc);
11107dced78aSDavid Greenman 
11113b0a4aefSJohn Baldwin 	if (pci_find_cap(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 */
112641eb5ac3SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP)
11271845b5c3SMarius Strobl 		fxp_init_body(sc, 1);
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
1268453130d9SPedro F. Giffuni  * talks about the existence 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 
13108262183eSPyun YongHyeon static void
13118262183eSPyun YongHyeon fxp_load_eeprom(struct fxp_softc *sc)
13128262183eSPyun YongHyeon {
13138262183eSPyun YongHyeon 	int i;
13148262183eSPyun YongHyeon 	uint16_t cksum;
13158262183eSPyun YongHyeon 
13168262183eSPyun YongHyeon 	fxp_read_eeprom(sc, sc->eeprom, 0, 1 << sc->eeprom_size);
13178262183eSPyun YongHyeon 	cksum = 0;
13188262183eSPyun YongHyeon 	for (i = 0; i < (1 << sc->eeprom_size) - 1; i++)
13198262183eSPyun YongHyeon 		cksum += sc->eeprom[i];
13208262183eSPyun YongHyeon 	cksum = 0xBABA - cksum;
13218262183eSPyun YongHyeon 	if (cksum != sc->eeprom[(1 << sc->eeprom_size) - 1])
13228262183eSPyun YongHyeon 		device_printf(sc->dev,
13238262183eSPyun YongHyeon 		    "EEPROM checksum mismatch! (0x%04x -> 0x%04x)\n",
13248262183eSPyun YongHyeon 		    cksum, sc->eeprom[(1 << sc->eeprom_size) - 1]);
13258262183eSPyun YongHyeon }
13268262183eSPyun YongHyeon 
1327a17c678eSDavid Greenman /*
13284953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1329a17c678eSDavid Greenman  */
1330a17c678eSDavid Greenman static void
133141eb5ac3SMarcel Moolenaar fxp_start(if_t ifp)
1332a17c678eSDavid Greenman {
133341eb5ac3SMarcel Moolenaar 	struct fxp_softc *sc = if_getsoftc(ifp);
13344953bccaSNate Lawson 
13354953bccaSNate Lawson 	FXP_LOCK(sc);
13364953bccaSNate Lawson 	fxp_start_body(ifp);
13374953bccaSNate Lawson 	FXP_UNLOCK(sc);
13384953bccaSNate Lawson }
13394953bccaSNate Lawson 
13404953bccaSNate Lawson /*
13414953bccaSNate Lawson  * Start packet transmission on the interface.
13424953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
13434953bccaSNate Lawson  * internal entry point only.
13444953bccaSNate Lawson  */
13454953bccaSNate Lawson static void
134641eb5ac3SMarcel Moolenaar fxp_start_body(if_t ifp)
13474953bccaSNate Lawson {
134841eb5ac3SMarcel Moolenaar 	struct fxp_softc *sc = if_getsoftc(ifp);
1349b2badf02SMaxime Henrion 	struct mbuf *mb_head;
13504e53f837SPyun YongHyeon 	int txqueued;
1351a17c678eSDavid Greenman 
135267fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
135340c20505SMaxime Henrion 
135441eb5ac3SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1355c109e385SPyun YongHyeon 	    IFF_DRV_RUNNING)
1356c109e385SPyun YongHyeon 		return;
1357c109e385SPyun YongHyeon 
13584e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
13594e53f837SPyun YongHyeon 		fxp_txeof(sc);
1360483b9871SDavid Greenman 	/*
1361483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1362483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
13633114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
13643114fdb4SDavid Greenman 	 *       a NOP command when needed.
1365483b9871SDavid Greenman 	 */
136640c20505SMaxime Henrion 	txqueued = 0;
136741eb5ac3SMarcel Moolenaar 	while (!if_sendq_empty(ifp) && sc->tx_queued < FXP_NTXCB - 1) {
1368483b9871SDavid Greenman 
1369dfe61cf1SDavid Greenman 		/*
1370dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1371dfe61cf1SDavid Greenman 		 */
137241eb5ac3SMarcel Moolenaar 		mb_head = if_dequeue(ifp);
13737929aa03SMax Laier 		if (mb_head == NULL)
13747929aa03SMax Laier 			break;
1375a17c678eSDavid Greenman 
13764e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
13774e53f837SPyun YongHyeon 			if (mb_head == NULL)
137840c20505SMaxime Henrion 				break;
137941eb5ac3SMarcel Moolenaar 			if_sendq_prepend(ifp, mb_head);
138041eb5ac3SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
138140c20505SMaxime Henrion 		}
13824e53f837SPyun YongHyeon 		txqueued++;
13834e53f837SPyun YongHyeon 		/*
13844e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
13854e53f837SPyun YongHyeon 		 */
138641eb5ac3SMarcel Moolenaar 		if_bpfmtap(ifp, mb_head);
13874e53f837SPyun YongHyeon 	}
138840c20505SMaxime Henrion 
138940c20505SMaxime Henrion 	/*
139040c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
139140c20505SMaxime Henrion 	 * going again if suspended.
139240c20505SMaxime Henrion 	 */
13934e53f837SPyun YongHyeon 	if (txqueued > 0) {
1394a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1395a2057a72SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
139640c20505SMaxime Henrion 		fxp_scb_wait(sc);
139740c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
13984e53f837SPyun YongHyeon 		/*
13994e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
14004e53f837SPyun YongHyeon 		 * from the card again.
14014e53f837SPyun YongHyeon 		 */
14024e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
140340c20505SMaxime Henrion 	}
140440c20505SMaxime Henrion }
140540c20505SMaxime Henrion 
140640c20505SMaxime Henrion static int
14074e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
140840c20505SMaxime Henrion {
140941eb5ac3SMarcel Moolenaar 	if_t ifp;
141040c20505SMaxime Henrion 	struct mbuf *m;
141140c20505SMaxime Henrion 	struct fxp_tx *txp;
141240c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
1413c21e84e4SPyun YongHyeon 	struct tcphdr *tcp;
141440c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
1415c21e84e4SPyun YongHyeon 	int error, i, nseg, tcp_payload;
141640c20505SMaxime Henrion 
141740c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1418fc74a9f9SBrooks Davis 	ifp = sc->ifp;
141940c20505SMaxime Henrion 
1420c21e84e4SPyun YongHyeon 	tcp_payload = 0;
1421c21e84e4SPyun YongHyeon 	tcp = NULL;
1422dfe61cf1SDavid Greenman 	/*
1423483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1424dfe61cf1SDavid Greenman 	 */
1425b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1426c8bca6dcSBill Paul 
1427c8bca6dcSBill Paul 	/*
1428a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1429a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1430a35e7eaaSDon Lewis 	 * Developer Manual says:
1431a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1432a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1433a35e7eaaSDon Lewis 	 *   ...
1434a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1435a35e7eaaSDon Lewis 	 *   be used.
1436a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1437a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1438a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1439a35e7eaaSDon Lewis 	 */
1440a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1441a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1442a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1443a35e7eaaSDon Lewis 
14444e53f837SPyun YongHyeon 	m = *m_head;
1445c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1446c21e84e4SPyun YongHyeon 		/*
1447c21e84e4SPyun YongHyeon 		 * 82550/82551 requires ethernet/IP/TCP headers must be
1448c21e84e4SPyun YongHyeon 		 * contained in the first active transmit buffer.
1449c21e84e4SPyun YongHyeon 		 */
1450c21e84e4SPyun YongHyeon 		struct ether_header *eh;
1451c21e84e4SPyun YongHyeon 		struct ip *ip;
1452c21e84e4SPyun YongHyeon 		uint32_t ip_off, poff;
1453c21e84e4SPyun YongHyeon 
1454c21e84e4SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
1455c21e84e4SPyun YongHyeon 			/* Get a writable copy. */
1456c6499eccSGleb Smirnoff 			m = m_dup(*m_head, M_NOWAIT);
1457c21e84e4SPyun YongHyeon 			m_freem(*m_head);
1458c21e84e4SPyun YongHyeon 			if (m == NULL) {
1459c21e84e4SPyun YongHyeon 				*m_head = NULL;
1460c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1461c21e84e4SPyun YongHyeon 			}
1462c21e84e4SPyun YongHyeon 			*m_head = m;
1463c21e84e4SPyun YongHyeon 		}
1464c21e84e4SPyun YongHyeon 		ip_off = sizeof(struct ether_header);
1465c21e84e4SPyun YongHyeon 		m = m_pullup(*m_head, ip_off);
1466c21e84e4SPyun YongHyeon 		if (m == NULL) {
1467c21e84e4SPyun YongHyeon 			*m_head = NULL;
1468c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1469c21e84e4SPyun YongHyeon 		}
1470c21e84e4SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
1471c21e84e4SPyun YongHyeon 		/* Check the existence of VLAN tag. */
1472c21e84e4SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1473c21e84e4SPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
1474c21e84e4SPyun YongHyeon 			m = m_pullup(m, ip_off);
1475c21e84e4SPyun YongHyeon 			if (m == NULL) {
1476c21e84e4SPyun YongHyeon 				*m_head = NULL;
1477c21e84e4SPyun YongHyeon 				return (ENOBUFS);
1478c21e84e4SPyun YongHyeon 			}
1479c21e84e4SPyun YongHyeon 		}
1480c21e84e4SPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
1481c21e84e4SPyun YongHyeon 		if (m == NULL) {
1482c21e84e4SPyun YongHyeon 			*m_head = NULL;
1483c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1484c21e84e4SPyun YongHyeon 		}
1485c21e84e4SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1486c21e84e4SPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
1487c21e84e4SPyun YongHyeon 		m = m_pullup(m, poff + sizeof(struct tcphdr));
1488c21e84e4SPyun YongHyeon 		if (m == NULL) {
1489c21e84e4SPyun YongHyeon 			*m_head = NULL;
1490c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1491c21e84e4SPyun YongHyeon 		}
1492c21e84e4SPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1493cbecedb2SPyun YongHyeon 		m = m_pullup(m, poff + (tcp->th_off << 2));
1494c21e84e4SPyun YongHyeon 		if (m == NULL) {
1495c21e84e4SPyun YongHyeon 			*m_head = NULL;
1496c21e84e4SPyun YongHyeon 			return (ENOBUFS);
1497c21e84e4SPyun YongHyeon 		}
1498c21e84e4SPyun YongHyeon 
1499c21e84e4SPyun YongHyeon 		/*
1500c21e84e4SPyun YongHyeon 		 * Since 82550/82551 doesn't modify IP length and pseudo
1501c21e84e4SPyun YongHyeon 		 * checksum in the first frame driver should compute it.
1502c21e84e4SPyun YongHyeon 		 */
150396486faaSPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
150496486faaSPyun YongHyeon 		tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1505c21e84e4SPyun YongHyeon 		ip->ip_sum = 0;
15060685c824SPyun YongHyeon 		ip->ip_len = htons(m->m_pkthdr.tso_segsz + (ip->ip_hl << 2) +
15070685c824SPyun YongHyeon 		    (tcp->th_off << 2));
1508c21e84e4SPyun YongHyeon 		tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1509c21e84e4SPyun YongHyeon 		    htons(IPPROTO_TCP + (tcp->th_off << 2) +
1510c21e84e4SPyun YongHyeon 		    m->m_pkthdr.tso_segsz));
1511c21e84e4SPyun YongHyeon 		/* Compute total TCP payload. */
1512c21e84e4SPyun YongHyeon 		tcp_payload = m->m_pkthdr.len - ip_off - (ip->ip_hl << 2);
1513c21e84e4SPyun YongHyeon 		tcp_payload -= tcp->th_off << 2;
1514c21e84e4SPyun YongHyeon 		*m_head = m;
15156da6d0a9SPyun YongHyeon 	} else if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
15166da6d0a9SPyun YongHyeon 		/*
15176da6d0a9SPyun YongHyeon 		 * Deal with TCP/IP checksum offload. Note that
15186da6d0a9SPyun YongHyeon 		 * in order for TCP checksum offload to work,
15196da6d0a9SPyun YongHyeon 		 * the pseudo header checksum must have already
15206da6d0a9SPyun YongHyeon 		 * been computed and stored in the checksum field
15216da6d0a9SPyun YongHyeon 		 * in the TCP header. The stack should have
15226da6d0a9SPyun YongHyeon 		 * already done this for us.
15236da6d0a9SPyun YongHyeon 		 */
15246da6d0a9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
15256da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
15266da6d0a9SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
15276da6d0a9SPyun YongHyeon 
15286da6d0a9SPyun YongHyeon #ifdef FXP_IP_CSUM_WAR
15296da6d0a9SPyun YongHyeon 		/*
15306da6d0a9SPyun YongHyeon 		 * XXX The 82550 chip appears to have trouble
15316da6d0a9SPyun YongHyeon 		 * dealing with IP header checksums in very small
15326da6d0a9SPyun YongHyeon 		 * datagrams, namely fragments from 1 to 3 bytes
15336da6d0a9SPyun YongHyeon 		 * in size. For example, say you want to transmit
15346da6d0a9SPyun YongHyeon 		 * a UDP packet of 1473 bytes. The packet will be
15356da6d0a9SPyun YongHyeon 		 * fragmented over two IP datagrams, the latter
15366da6d0a9SPyun YongHyeon 		 * containing only one byte of data. The 82550 will
15376da6d0a9SPyun YongHyeon 		 * botch the header checksum on the 1-byte fragment.
15386da6d0a9SPyun YongHyeon 		 * As long as the datagram contains 4 or more bytes
15396da6d0a9SPyun YongHyeon 		 * of data, you're ok.
15406da6d0a9SPyun YongHyeon 		 *
15416da6d0a9SPyun YongHyeon                  * The following code attempts to work around this
15426da6d0a9SPyun YongHyeon 		 * problem: if the datagram is less than 38 bytes
15436da6d0a9SPyun YongHyeon 		 * in size (14 bytes ether header, 20 bytes IP header,
15446da6d0a9SPyun YongHyeon 		 * plus 4 bytes of data), we punt and compute the IP
15456da6d0a9SPyun YongHyeon 		 * header checksum by hand. This workaround doesn't
15466da6d0a9SPyun YongHyeon 		 * work very well, however, since it can be fooled
15476da6d0a9SPyun YongHyeon 		 * by things like VLAN tags and IP options that make
15486da6d0a9SPyun YongHyeon 		 * the header sizes/offsets vary.
15496da6d0a9SPyun YongHyeon 		 */
15506da6d0a9SPyun YongHyeon 
15516da6d0a9SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
15526da6d0a9SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
15536da6d0a9SPyun YongHyeon 				struct ip *ip;
15546da6d0a9SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
15556da6d0a9SPyun YongHyeon 				ip = mtod(m, struct ip *);
15566da6d0a9SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
15576da6d0a9SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
15586da6d0a9SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
15596da6d0a9SPyun YongHyeon 			} else {
15606da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_activation_high =
15616da6d0a9SPyun YongHyeon 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
15626da6d0a9SPyun YongHyeon 				txp->tx_cb->ipcb_ip_schedule |=
15636da6d0a9SPyun YongHyeon 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
15646da6d0a9SPyun YongHyeon 			}
15656da6d0a9SPyun YongHyeon 		}
15666da6d0a9SPyun YongHyeon #endif
1567c21e84e4SPyun YongHyeon 	}
1568c21e84e4SPyun YongHyeon 
1569a2057a72SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map, *m_head,
15704e53f837SPyun YongHyeon 	    segs, &nseg, 0);
15714e53f837SPyun YongHyeon 	if (error == EFBIG) {
1572c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, sc->maxtxseg);
15734e53f837SPyun YongHyeon 		if (m == NULL) {
15744e53f837SPyun YongHyeon 			m_freem(*m_head);
15754e53f837SPyun YongHyeon 			*m_head = NULL;
15764e53f837SPyun YongHyeon 			return (ENOMEM);
15771104779bSMike Silbersack 		}
15784e53f837SPyun YongHyeon 		*m_head = m;
1579a2057a72SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->fxp_txmtag, txp->tx_map,
15804e53f837SPyun YongHyeon 		    *m_head, segs, &nseg, 0);
15814e53f837SPyun YongHyeon 		if (error != 0) {
15824e53f837SPyun YongHyeon 			m_freem(*m_head);
15834e53f837SPyun YongHyeon 			*m_head = NULL;
15844e53f837SPyun YongHyeon 			return (ENOMEM);
15854e53f837SPyun YongHyeon 		}
15864e53f837SPyun YongHyeon 	} else if (error != 0)
15874e53f837SPyun YongHyeon 		return (error);
15884e53f837SPyun YongHyeon 	if (nseg == 0) {
15894e53f837SPyun YongHyeon 		m_freem(*m_head);
15904e53f837SPyun YongHyeon 		*m_head = NULL;
15914e53f837SPyun YongHyeon 		return (EIO);
159223a0ed7cSDavid Greenman 	}
159323a0ed7cSDavid Greenman 
159440c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
1595a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1596b2badf02SMaxime Henrion 
159740c20505SMaxime Henrion 	cbp = txp->tx_cb;
159840c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
159940c20505SMaxime Henrion 		/*
160040c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
160140c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
160240c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
160340c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
160440c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
160540c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
160640c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
160740c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
160840c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
160940c20505SMaxime Henrion 		 * starting from a different place depending on whether
161040c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
161140c20505SMaxime Henrion 		 */
161240c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
161368f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
161468f4ab9aSPyun YongHyeon 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
161540c20505SMaxime Henrion 		} else {
161640c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
161740c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
161840c20505SMaxime Henrion 		}
161940c20505SMaxime Henrion 	}
1620c21e84e4SPyun YongHyeon 	if (sc->flags & FXP_FLAG_EXT_RFA) {
1621c21e84e4SPyun YongHyeon 		/* Configure dynamic TBD for 82550/82551. */
1622c21e84e4SPyun YongHyeon 		cbp->tbd_number = 0xFF;
162368f4ab9aSPyun YongHyeon 		cbp->tbd[nseg].tb_size |= htole32(0x8000);
1624c21e84e4SPyun YongHyeon 	} else
162540c20505SMaxime Henrion 		cbp->tbd_number = nseg;
1626c21e84e4SPyun YongHyeon 	/* Configure TSO. */
1627c21e84e4SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1628c21e84e4SPyun YongHyeon 		cbp->tbd[-1].tb_size = htole32(m->m_pkthdr.tso_segsz << 16);
162968f4ab9aSPyun YongHyeon 		cbp->tbd[1].tb_size |= htole32(tcp_payload << 16);
1630c21e84e4SPyun YongHyeon 		cbp->ipcb_ip_schedule |= FXP_IPCB_LARGESEND_ENABLE |
1631c21e84e4SPyun YongHyeon 		    FXP_IPCB_IP_CHECKSUM_ENABLE |
1632c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCP_PACKET |
1633c21e84e4SPyun YongHyeon 		    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
1634c21e84e4SPyun YongHyeon 	}
1635bd4fa9d9SPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
1636bd4fa9d9SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
1637bd4fa9d9SPyun YongHyeon 		cbp->ipcb_vlan_id = htons(m->m_pkthdr.ether_vtag);
1638bd4fa9d9SPyun YongHyeon 		txp->tx_cb->ipcb_ip_activation_high |=
1639bd4fa9d9SPyun YongHyeon 		    FXP_IPCB_INSERTVLAN_ENABLE;
1640bd4fa9d9SPyun YongHyeon 	}
164140c20505SMaxime Henrion 
16424e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1643b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1644b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
16454e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1646b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
164783e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
164883e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
16494e53f837SPyun YongHyeon 	else
1650b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
165183e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
165283e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1653c21e84e4SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0)
1654b2badf02SMaxime Henrion 		txp->tx_cb->tx_threshold = tx_threshold;
1655a17c678eSDavid Greenman 
1656a17c678eSDavid Greenman 	/*
1657483b9871SDavid Greenman 	 * Advance the end of list forward.
1658a17c678eSDavid Greenman 	 */
165940c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
1660b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1661a17c678eSDavid Greenman 
1662a17c678eSDavid Greenman 	/*
16631cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1664b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1665483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1666a17c678eSDavid Greenman 	 */
16671cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1668b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1669a17c678eSDavid Greenman 
16701cd443acSDavid Greenman 	sc->tx_queued++;
16711cd443acSDavid Greenman 
167240c20505SMaxime Henrion 	return (0);
1673a17c678eSDavid Greenman }
1674a17c678eSDavid Greenman 
1675e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1676bd071d4dSGleb Smirnoff static poll_handler_t fxp_poll;
1677e4fc250cSLuigi Rizzo 
16781abcdbd1SAttilio Rao static int
167941eb5ac3SMarcel Moolenaar fxp_poll(if_t ifp, enum poll_cmd cmd, int count)
1680e4fc250cSLuigi Rizzo {
168141eb5ac3SMarcel Moolenaar 	struct fxp_softc *sc = if_getsoftc(ifp);
168274d1ed23SMaxime Henrion 	uint8_t statack;
16831abcdbd1SAttilio Rao 	int rx_npkts = 0;
1684e4fc250cSLuigi Rizzo 
16854953bccaSNate Lawson 	FXP_LOCK(sc);
168641eb5ac3SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
16874953bccaSNate Lawson 		FXP_UNLOCK(sc);
16881abcdbd1SAttilio Rao 		return (rx_npkts);
1689e4fc250cSLuigi Rizzo 	}
169040929967SGleb Smirnoff 
1691e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1692e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1693e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
169474d1ed23SMaxime Henrion 		uint8_t tmp;
16956481f301SPeter Wemm 
1696e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
16974953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
16984953bccaSNate Lawson 			FXP_UNLOCK(sc);
16991abcdbd1SAttilio Rao 			return (rx_npkts); /* nothing to do */
17004953bccaSNate Lawson 		}
1701e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1702e4fc250cSLuigi Rizzo 		/* ack what we can */
1703e4fc250cSLuigi Rizzo 		if (tmp != 0)
1704e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1705e4fc250cSLuigi Rizzo 		statack |= tmp;
1706e4fc250cSLuigi Rizzo 	}
17071abcdbd1SAttilio Rao 	rx_npkts = fxp_intr_body(sc, ifp, statack, count);
17084953bccaSNate Lawson 	FXP_UNLOCK(sc);
17091abcdbd1SAttilio Rao 	return (rx_npkts);
1710e4fc250cSLuigi Rizzo }
1711e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1712e4fc250cSLuigi Rizzo 
1713a17c678eSDavid Greenman /*
17149c7d2607SDavid Greenman  * Process interface interrupts.
1715a17c678eSDavid Greenman  */
171694927790SDavid Greenman static void
1717f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1718a17c678eSDavid Greenman {
1719f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
172041eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
172174d1ed23SMaxime Henrion 	uint8_t statack;
17220f4dc94cSChuck Paterson 
17234953bccaSNate Lawson 	FXP_LOCK(sc);
1724704d1965SWarner Losh 	if (sc->suspended) {
1725704d1965SWarner Losh 		FXP_UNLOCK(sc);
1726704d1965SWarner Losh 		return;
1727704d1965SWarner Losh 	}
1728704d1965SWarner Losh 
1729e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
173041eb5ac3SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
17314953bccaSNate Lawson 		FXP_UNLOCK(sc);
1732e4fc250cSLuigi Rizzo 		return;
17334953bccaSNate Lawson 	}
1734e4fc250cSLuigi Rizzo #endif
1735b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1736a17c678eSDavid Greenman 		/*
173711457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
173811457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
173911457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
174011457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
174111457bbfSJonathan Lemon 		 */
17424953bccaSNate Lawson 		if (statack == 0xff) {
17434953bccaSNate Lawson 			FXP_UNLOCK(sc);
174411457bbfSJonathan Lemon 			return;
17454953bccaSNate Lawson 		}
174611457bbfSJonathan Lemon 
174711457bbfSJonathan Lemon 		/*
1748a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1749a17c678eSDavid Greenman 		 */
1750ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
175141eb5ac3SMarcel Moolenaar 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
17524953bccaSNate Lawson 			fxp_intr_body(sc, ifp, statack, -1);
1753e4fc250cSLuigi Rizzo 	}
17544953bccaSNate Lawson 	FXP_UNLOCK(sc);
1755e4fc250cSLuigi Rizzo }
1756e4fc250cSLuigi Rizzo 
1757e4fc250cSLuigi Rizzo static void
1758b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1759b2badf02SMaxime Henrion {
176041eb5ac3SMarcel Moolenaar 	if_t ifp;
1761b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1762b2badf02SMaxime Henrion 
17634e53f837SPyun YongHyeon 	ifp = sc->ifp;
1764a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1765a2057a72SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1766b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
176783e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1768b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1769b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1770a2057a72SPyun YongHyeon 			bus_dmamap_sync(sc->fxp_txmtag, txp->tx_map,
1771b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1772a2057a72SPyun YongHyeon 			bus_dmamap_unload(sc->fxp_txmtag, txp->tx_map);
1773b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1774b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1775b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1776b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1777b2badf02SMaxime Henrion 		}
1778b2badf02SMaxime Henrion 		sc->tx_queued--;
177941eb5ac3SMarcel Moolenaar 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1780b2badf02SMaxime Henrion 	}
1781b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1782a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
1783a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17846b24912cSPyun YongHyeon 	if (sc->tx_queued == 0)
178525935344SPyun YongHyeon 		sc->watchdog_timer = 0;
1786b2badf02SMaxime Henrion }
1787b2badf02SMaxime Henrion 
1788b2badf02SMaxime Henrion static void
178941eb5ac3SMarcel Moolenaar fxp_rxcsum(struct fxp_softc *sc, if_t ifp, struct mbuf *m,
1790f13075afSPyun YongHyeon     uint16_t status, int pos)
1791f13075afSPyun YongHyeon {
1792f13075afSPyun YongHyeon 	struct ether_header *eh;
1793f13075afSPyun YongHyeon 	struct ip *ip;
1794f13075afSPyun YongHyeon 	struct udphdr *uh;
1795f13075afSPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
1796f13075afSPyun YongHyeon 	uint16_t csum, *opts;
1797f13075afSPyun YongHyeon 
1798f13075afSPyun YongHyeon 	if ((sc->flags & FXP_FLAG_82559_RXCSUM) == 0) {
1799f13075afSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_PARSE) != 0) {
1800f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1801f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1802f13075afSPyun YongHyeon 			if (status & FXP_RFDX_CS_IP_CSUM_VALID)
1803f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1804f13075afSPyun YongHyeon 			if ((status & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1805f13075afSPyun YongHyeon 			    (status & FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1806f13075afSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
1807f13075afSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
1808f13075afSPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
1809f13075afSPyun YongHyeon 			}
1810f13075afSPyun YongHyeon 		}
1811f13075afSPyun YongHyeon 		return;
1812f13075afSPyun YongHyeon 	}
1813f13075afSPyun YongHyeon 
1814f13075afSPyun YongHyeon 	pktlen = m->m_pkthdr.len;
1815f13075afSPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
1816f13075afSPyun YongHyeon 		return;
1817f13075afSPyun YongHyeon 	eh = mtod(m, struct ether_header *);
1818f13075afSPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
1819f13075afSPyun YongHyeon 		return;
1820f13075afSPyun YongHyeon 	ip = (struct ip *)(eh + 1);
1821f13075afSPyun YongHyeon 	if (ip->ip_v != IPVERSION)
1822f13075afSPyun YongHyeon 		return;
1823f13075afSPyun YongHyeon 
1824f13075afSPyun YongHyeon 	hlen = ip->ip_hl << 2;
1825f13075afSPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
1826f13075afSPyun YongHyeon 	if (hlen < sizeof(struct ip))
1827f13075afSPyun YongHyeon 		return;
1828f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
1829f13075afSPyun YongHyeon 		return;
1830f13075afSPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
1831f13075afSPyun YongHyeon 		return;
1832f13075afSPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1833f13075afSPyun YongHyeon 		return;	/* can't handle fragmented packet */
1834f13075afSPyun YongHyeon 
1835f13075afSPyun YongHyeon 	switch (ip->ip_p) {
1836f13075afSPyun YongHyeon 	case IPPROTO_TCP:
1837f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
1838f13075afSPyun YongHyeon 			return;
1839f13075afSPyun YongHyeon 		break;
1840f13075afSPyun YongHyeon 	case IPPROTO_UDP:
1841f13075afSPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
1842f13075afSPyun YongHyeon 			return;
1843f13075afSPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
1844f13075afSPyun YongHyeon 		if (uh->uh_sum == 0)
1845f13075afSPyun YongHyeon 			return; /* no checksum */
1846f13075afSPyun YongHyeon 		break;
1847f13075afSPyun YongHyeon 	default:
1848f13075afSPyun YongHyeon 		return;
1849f13075afSPyun YongHyeon 	}
1850f13075afSPyun YongHyeon 	/* Extract computed checksum. */
1851f13075afSPyun YongHyeon 	csum = be16dec(mtod(m, char *) + pos);
1852f13075afSPyun YongHyeon 	/* checksum fixup for IP options */
1853f13075afSPyun YongHyeon 	len = hlen - sizeof(struct ip);
1854f13075afSPyun YongHyeon 	if (len > 0) {
1855f13075afSPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
1856f13075afSPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
1857f13075afSPyun YongHyeon 			temp32 = csum - *opts;
1858f13075afSPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
1859f13075afSPyun YongHyeon 			csum = temp32 & 65535;
1860f13075afSPyun YongHyeon 		}
1861f13075afSPyun YongHyeon 	}
1862f13075afSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1863f13075afSPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
1864f13075afSPyun YongHyeon }
1865f13075afSPyun YongHyeon 
18661abcdbd1SAttilio Rao static int
186741eb5ac3SMarcel Moolenaar fxp_intr_body(struct fxp_softc *sc, if_t ifp, uint8_t statack,
18684953bccaSNate Lawson     int count)
1869e4fc250cSLuigi Rizzo {
18702b5989e9SLuigi Rizzo 	struct mbuf *m;
1871b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
18722b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
18732b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
18741abcdbd1SAttilio Rao 	int rx_npkts;
187560bb79ebSPyun YongHyeon 	uint16_t status;
18762b5989e9SLuigi Rizzo 
18771abcdbd1SAttilio Rao 	rx_npkts = 0;
187867fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
18791abcdbd1SAttilio Rao 
18802b5989e9SLuigi Rizzo 	if (rnr)
18810f1db1d6SMaxime Henrion 		sc->rnr++;
1882947e3815SIan Dowse #ifdef DEVICE_POLLING
1883947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1884947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1885947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1886947e3815SIan Dowse 		rnr = 1;
1887947e3815SIan Dowse 	}
1888947e3815SIan Dowse #endif
1889a17c678eSDavid Greenman 
1890a17c678eSDavid Greenman 	/*
18913114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
189206936301SBill Paul 	 *
189306936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
189406936301SBill Paul 	 * be that this event (control unit not ready) was not
189506936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
189606936301SBill Paul 	 * The exact sequence of events that occur when the interface
189706936301SBill Paul 	 * is brought up are different now, and if this event
189806936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
189906936301SBill Paul 	 * can stall for several seconds. The result is that no
190006936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
190106936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
19023114fdb4SDavid Greenman 	 */
19034e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1904b2badf02SMaxime Henrion 		fxp_txeof(sc);
19053114fdb4SDavid Greenman 
19063114fdb4SDavid Greenman 	/*
19073114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
19083114fdb4SDavid Greenman 	 */
190941eb5ac3SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
19104953bccaSNate Lawson 		fxp_start_body(ifp);
19112b5989e9SLuigi Rizzo 
19122b5989e9SLuigi Rizzo 	/*
19132b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
19142b5989e9SLuigi Rizzo 	 */
1915947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
19161abcdbd1SAttilio Rao 		return (rx_npkts);
19172b5989e9SLuigi Rizzo 
19183114fdb4SDavid Greenman 	/*
1919a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1920a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1921a17c678eSDavid Greenman 	 * re-start the receiver.
1922947e3815SIan Dowse 	 *
19232b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
19242b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
19252b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
19262b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1927947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1928947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1929a17c678eSDavid Greenman 	 */
19302b5989e9SLuigi Rizzo 	for (;;) {
1931b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1932b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1933ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1934ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1935a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
19364812aef5SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1937a17c678eSDavid Greenman 
1938e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1939947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1940947e3815SIan Dowse 			if (rnr) {
1941947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1942947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1943947e3815SIan Dowse 				rnr = 0;
1944947e3815SIan Dowse 			}
19452b5989e9SLuigi Rizzo 			break;
1946947e3815SIan Dowse 		}
19472b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
19482b5989e9SLuigi Rizzo 
194960bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
195060bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
19512b5989e9SLuigi Rizzo 			break;
19522b5989e9SLuigi Rizzo 
1953f7a5f737SPyun YongHyeon 		if ((status & FXP_RFA_STATUS_RNR) != 0)
1954f7a5f737SPyun YongHyeon 			rnr++;
1955dfe61cf1SDavid Greenman 		/*
1956b2badf02SMaxime Henrion 		 * Advance head forward.
1957dfe61cf1SDavid Greenman 		 */
1958b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1959a17c678eSDavid Greenman 
1960dfe61cf1SDavid Greenman 		/*
1961ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1962ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1963ba8c6fd5SDavid Greenman 		 * instead.
1964dfe61cf1SDavid Greenman 		 */
196585050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1966aed53495SDavid Greenman 			int total_len;
1967a17c678eSDavid Greenman 
1968e8c8b728SJonathan Lemon 			/*
19692b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
19702b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
19712b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
19722b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1973e8c8b728SJonathan Lemon 			 */
1974bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
1975f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
197641eb5ac3SMarcel Moolenaar 			    (if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) {
1977f13075afSPyun YongHyeon 				/* Adjust for appended checksum bytes. */
1978f13075afSPyun YongHyeon 				total_len -= 2;
1979f13075afSPyun YongHyeon 			}
1980991ae908SPyun YongHyeon 			if (total_len < (int)sizeof(struct ether_header) ||
1981f7a5f737SPyun YongHyeon 			    total_len > (MCLBYTES - RFA_ALIGNMENT_FUDGE -
1982f7a5f737SPyun YongHyeon 			    sc->rfa_size) ||
1983f7a5f737SPyun YongHyeon 			    status & (FXP_RFA_STATUS_CRC |
1984991ae908SPyun YongHyeon 			    FXP_RFA_STATUS_ALIGN | FXP_RFA_STATUS_OVERRUN)) {
1985e8c8b728SJonathan Lemon 				m_freem(m);
1986f7a5f737SPyun YongHyeon 				fxp_add_rfabuf(sc, rxp);
19872b5989e9SLuigi Rizzo 				continue;
1988e8c8b728SJonathan Lemon 			}
1989920b58e8SBrooks Davis 
19902e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
199141eb5ac3SMarcel Moolenaar 			if_setrcvif(m, ifp);
1992673d9191SSam Leffler 
1993f13075afSPyun YongHyeon                         /* Do IP checksum checking. */
199441eb5ac3SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
1995f13075afSPyun YongHyeon 				fxp_rxcsum(sc, ifp, m, status, total_len);
199641eb5ac3SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0 &&
1997bd4fa9d9SPyun YongHyeon 			    (status & FXP_RFA_STATUS_VLAN) != 0) {
1998bd4fa9d9SPyun YongHyeon 				m->m_pkthdr.ether_vtag =
1999bd4fa9d9SPyun YongHyeon 				    ntohs(rfa->rfax_vlan_id);
2000bd4fa9d9SPyun YongHyeon 				m->m_flags |= M_VLANTAG;
2001bd4fa9d9SPyun YongHyeon 			}
200205fb8c3fSNate Lawson 			/*
200305fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
200405fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
200505fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
200605fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
200705fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
200805fb8c3fSNate Lawson 			 * calling if_input() on each one.
200905fb8c3fSNate Lawson 			 */
201005fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
201141eb5ac3SMarcel Moolenaar 			if_input(ifp, m);
201205fb8c3fSNate Lawson 			FXP_LOCK(sc);
20131abcdbd1SAttilio Rao 			rx_npkts++;
201441eb5ac3SMarcel Moolenaar 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
2015c109e385SPyun YongHyeon 				return (rx_npkts);
201685050421SPyun YongHyeon 		} else {
201785050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
2018df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
201985050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
2020a17c678eSDavid Greenman 		}
202185050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
2022a17c678eSDavid Greenman 	}
20232b5989e9SLuigi Rizzo 	if (rnr) {
2024ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
2025ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
2026b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
20272e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2028a17c678eSDavid Greenman 	}
20291abcdbd1SAttilio Rao 	return (rx_npkts);
2030a17c678eSDavid Greenman }
2031a17c678eSDavid Greenman 
2032303b270bSEivind Eklund static void
20338da9c507SPyun YongHyeon fxp_update_stats(struct fxp_softc *sc)
2034a17c678eSDavid Greenman {
203541eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
2036a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
20378da9c507SPyun YongHyeon 	struct fxp_hwstats *hsp;
20388da9c507SPyun YongHyeon 	uint32_t *status;
2039a17c678eSDavid Greenman 
20403212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
20418da9c507SPyun YongHyeon 
20428da9c507SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
20438da9c507SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
20448da9c507SPyun YongHyeon 	/* Update statistical counters. */
20458da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
20468da9c507SPyun YongHyeon 		status = &sp->completion_status;
20478da9c507SPyun YongHyeon 	else if (sc->revision >= FXP_REV_82558_A4)
20488da9c507SPyun YongHyeon 		status = (uint32_t *)&sp->tx_tco;
20498da9c507SPyun YongHyeon 	else
20508da9c507SPyun YongHyeon 		status = &sp->tx_pause;
20518da9c507SPyun YongHyeon 	if (*status == htole32(FXP_STATS_DR_COMPLETE)) {
20528da9c507SPyun YongHyeon 		hsp = &sc->fxp_hwstats;
20538da9c507SPyun YongHyeon 		hsp->tx_good += le32toh(sp->tx_good);
20548da9c507SPyun YongHyeon 		hsp->tx_maxcols += le32toh(sp->tx_maxcols);
20558da9c507SPyun YongHyeon 		hsp->tx_latecols += le32toh(sp->tx_latecols);
20568da9c507SPyun YongHyeon 		hsp->tx_underruns += le32toh(sp->tx_underruns);
20578da9c507SPyun YongHyeon 		hsp->tx_lostcrs += le32toh(sp->tx_lostcrs);
20588da9c507SPyun YongHyeon 		hsp->tx_deffered += le32toh(sp->tx_deffered);
20598da9c507SPyun YongHyeon 		hsp->tx_single_collisions += le32toh(sp->tx_single_collisions);
20608da9c507SPyun YongHyeon 		hsp->tx_multiple_collisions +=
20618da9c507SPyun YongHyeon 		    le32toh(sp->tx_multiple_collisions);
20628da9c507SPyun YongHyeon 		hsp->tx_total_collisions += le32toh(sp->tx_total_collisions);
20638da9c507SPyun YongHyeon 		hsp->rx_good += le32toh(sp->rx_good);
20648da9c507SPyun YongHyeon 		hsp->rx_crc_errors += le32toh(sp->rx_crc_errors);
20658da9c507SPyun YongHyeon 		hsp->rx_alignment_errors += le32toh(sp->rx_alignment_errors);
20668da9c507SPyun YongHyeon 		hsp->rx_rnr_errors += le32toh(sp->rx_rnr_errors);
20678da9c507SPyun YongHyeon 		hsp->rx_overrun_errors += le32toh(sp->rx_overrun_errors);
20688da9c507SPyun YongHyeon 		hsp->rx_cdt_errors += le32toh(sp->rx_cdt_errors);
20698da9c507SPyun YongHyeon 		hsp->rx_shortframes += le32toh(sp->rx_shortframes);
20708da9c507SPyun YongHyeon 		hsp->tx_pause += le32toh(sp->tx_pause);
20718da9c507SPyun YongHyeon 		hsp->rx_pause += le32toh(sp->rx_pause);
20728da9c507SPyun YongHyeon 		hsp->rx_controls += le32toh(sp->rx_controls);
20738da9c507SPyun YongHyeon 		hsp->tx_tco += le16toh(sp->tx_tco);
20748da9c507SPyun YongHyeon 		hsp->rx_tco += le16toh(sp->rx_tco);
20758da9c507SPyun YongHyeon 
2076df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, le32toh(sp->tx_good));
2077df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
2078df360178SGleb Smirnoff 		    le32toh(sp->tx_total_collisions));
2079397f9dfeSDavid Greenman 		if (sp->rx_good) {
2080df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IPACKETS,
2081df360178SGleb Smirnoff 			    le32toh(sp->rx_good));
2082397f9dfeSDavid Greenman 			sc->rx_idle_secs = 0;
208343d8b117SPyun YongHyeon 		} else if (sc->flags & FXP_FLAG_RXBUG) {
2084c8cc6fcaSDavid Greenman 			/*
2085c8cc6fcaSDavid Greenman 			 * Receiver's been idle for another second.
2086c8cc6fcaSDavid Greenman 			 */
2087397f9dfeSDavid Greenman 			sc->rx_idle_secs++;
2088397f9dfeSDavid Greenman 		}
2089df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IERRORS,
209083e6547dSMaxime Henrion 		    le32toh(sp->rx_crc_errors) +
209183e6547dSMaxime Henrion 		    le32toh(sp->rx_alignment_errors) +
209283e6547dSMaxime Henrion 		    le32toh(sp->rx_rnr_errors) +
209341eb5ac3SMarcel Moolenaar 		    le32toh(sp->rx_overrun_errors));
2094a17c678eSDavid Greenman 		/*
2095453130d9SPedro F. Giffuni 		 * If any transmit underruns occurred, bump up the transmit
2096f9be9005SDavid Greenman 		 * threshold by another 512 bytes (64 * 8).
2097f9be9005SDavid Greenman 		 */
2098f9be9005SDavid Greenman 		if (sp->tx_underruns) {
2099df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS,
2100df360178SGleb Smirnoff 			    le32toh(sp->tx_underruns));
2101f9be9005SDavid Greenman 			if (tx_threshold < 192)
2102f9be9005SDavid Greenman 				tx_threshold += 64;
2103f9be9005SDavid Greenman 		}
21048da9c507SPyun YongHyeon 		*status = 0;
21058da9c507SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
21068da9c507SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
21078da9c507SPyun YongHyeon 	}
21088da9c507SPyun YongHyeon }
21098da9c507SPyun YongHyeon 
21108da9c507SPyun YongHyeon /*
21118da9c507SPyun YongHyeon  * Update packet in/out/collision statistics. The i82557 doesn't
21128da9c507SPyun YongHyeon  * allow you to access these counters without doing a fairly
21138da9c507SPyun YongHyeon  * expensive DMA to get _all_ of the statistics it maintains, so
21148da9c507SPyun YongHyeon  * we do this operation here only once per second. The statistics
21158da9c507SPyun YongHyeon  * counters in the kernel are updated from the previous dump-stats
21168da9c507SPyun YongHyeon  * DMA and then a new dump-stats DMA is started. The on-chip
21178da9c507SPyun YongHyeon  * counters are zeroed when the DMA completes. If we can't start
21188da9c507SPyun YongHyeon  * the DMA immediately, we don't wait - we just prepare to read
21198da9c507SPyun YongHyeon  * them again next time.
21208da9c507SPyun YongHyeon  */
21218da9c507SPyun YongHyeon static void
21228da9c507SPyun YongHyeon fxp_tick(void *xsc)
21238da9c507SPyun YongHyeon {
21248da9c507SPyun YongHyeon 	struct fxp_softc *sc = xsc;
212541eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
21268da9c507SPyun YongHyeon 
21278da9c507SPyun YongHyeon 	FXP_LOCK_ASSERT(sc, MA_OWNED);
21288da9c507SPyun YongHyeon 
21298da9c507SPyun YongHyeon 	/* Update statistical counters. */
21308da9c507SPyun YongHyeon 	fxp_update_stats(sc);
21314953bccaSNate Lawson 
2132397f9dfeSDavid Greenman 	/*
2133c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
2134c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
2135c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
2136c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
2137c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
2138c8cc6fcaSDavid Greenman 	 */
2139b2badf02SMaxime Henrion 	fxp_txeof(sc);
2140b2badf02SMaxime Henrion 
2141c8cc6fcaSDavid Greenman 	/*
2142397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
2143397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
2144397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
2145397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
2146453130d9SPedro F. Giffuni 	 * up if it gets certain types of garbage in the synchronization
2147397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
2148397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
2149397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
2150397f9dfeSDavid Greenman 	 */
2151397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
2152397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
215341eb5ac3SMarcel Moolenaar 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
215441eb5ac3SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
21551845b5c3SMarius Strobl 			fxp_init_body(sc, 1);
21565506afefSPyun YongHyeon 		}
21576b24912cSPyun YongHyeon 		return;
2158397f9dfeSDavid Greenman 	}
2159f9be9005SDavid Greenman 	/*
21603ba65732SDavid Greenman 	 * If there is no pending command, start another stats
21613ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
2162a17c678eSDavid Greenman 	 */
2163397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
2164a17c678eSDavid Greenman 		/*
2165397f9dfeSDavid Greenman 		 * Start another stats dump.
2166a17c678eSDavid Greenman 		 */
21672e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
2168dfe61cf1SDavid Greenman 	}
2169f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2170f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
21714953bccaSNate Lawson 
2172a17c678eSDavid Greenman 	/*
217316f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
2174df79d527SGleb Smirnoff 	 */
2175df79d527SGleb Smirnoff 	fxp_watchdog(sc);
2176df79d527SGleb Smirnoff 
2177df79d527SGleb Smirnoff 	/*
2178a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
2179a17c678eSDavid Greenman 	 */
218045276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2181a17c678eSDavid Greenman }
2182a17c678eSDavid Greenman 
2183a17c678eSDavid Greenman /*
2184a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
2185a17c678eSDavid Greenman  * the interface.
2186a17c678eSDavid Greenman  */
2187a17c678eSDavid Greenman static void
2188f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
2189a17c678eSDavid Greenman {
219041eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
2191b2badf02SMaxime Henrion 	struct fxp_tx *txp;
21923ba65732SDavid Greenman 	int i;
2193a17c678eSDavid Greenman 
219441eb5ac3SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
2195df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
21967dced78aSDavid Greenman 
2197a17c678eSDavid Greenman 	/*
2198a17c678eSDavid Greenman 	 * Cancel stats updater.
2199a17c678eSDavid Greenman 	 */
220045276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
22013ba65732SDavid Greenman 
22023ba65732SDavid Greenman 	/*
22037137cea0SPyun YongHyeon 	 * Preserve PCI configuration, configure, IA/multicast
22047137cea0SPyun YongHyeon 	 * setup and put RU and CU into idle state.
22053ba65732SDavid Greenman 	 */
22067137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
220772a32a26SJonathan Lemon 	DELAY(50);
22087137cea0SPyun YongHyeon 	/* Disable interrupts. */
22097137cea0SPyun YongHyeon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
2210a17c678eSDavid Greenman 
22118da9c507SPyun YongHyeon 	fxp_update_stats(sc);
22128da9c507SPyun YongHyeon 
22133ba65732SDavid Greenman 	/*
22143ba65732SDavid Greenman 	 * Release any xmit buffers.
22153ba65732SDavid Greenman 	 */
2216b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2217da91462dSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2218b2badf02SMaxime Henrion 		if (txp[i].tx_mbuf != NULL) {
2219a2057a72SPyun YongHyeon 			bus_dmamap_sync(sc->fxp_txmtag, txp[i].tx_map,
2220b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
222190b45a32SPyun YongHyeon 			bus_dmamap_unload(sc->fxp_txmtag, txp[i].tx_map);
2222b2badf02SMaxime Henrion 			m_freem(txp[i].tx_mbuf);
2223b2badf02SMaxime Henrion 			txp[i].tx_mbuf = NULL;
2224c8bca6dcSBill Paul 			/* clear this to reset csum offload bits */
2225b2badf02SMaxime Henrion 			txp[i].tx_cb->tbd[0].tb_addr = 0;
2226da91462dSDavid Greenman 		}
2227da91462dSDavid Greenman 	}
2228a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2229a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
22303ba65732SDavid Greenman 	sc->tx_queued = 0;
2231a17c678eSDavid Greenman }
2232a17c678eSDavid Greenman 
2233a17c678eSDavid Greenman /*
2234a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
2235a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
2236a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
2237a17c678eSDavid Greenman  * card has wedged for some reason.
2238a17c678eSDavid Greenman  */
2239a17c678eSDavid Greenman static void
2240df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
2241a17c678eSDavid Greenman {
224241eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
2243ba8c6fd5SDavid Greenman 
2244df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
2245df79d527SGleb Smirnoff 
2246df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
2247df79d527SGleb Smirnoff 		return;
2248df79d527SGleb Smirnoff 
2249f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
2250df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2251a17c678eSDavid Greenman 
225241eb5ac3SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
22531845b5c3SMarius Strobl 	fxp_init_body(sc, 1);
2254a17c678eSDavid Greenman }
2255a17c678eSDavid Greenman 
22564953bccaSNate Lawson /*
22574953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
22584953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
22594953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
22604953bccaSNate Lawson  */
2261a17c678eSDavid Greenman static void
2262f7788e8eSJonathan Lemon fxp_init(void *xsc)
2263a17c678eSDavid Greenman {
2264fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
22654953bccaSNate Lawson 
22664953bccaSNate Lawson 	FXP_LOCK(sc);
22671845b5c3SMarius Strobl 	fxp_init_body(sc, 1);
22684953bccaSNate Lawson 	FXP_UNLOCK(sc);
22694953bccaSNate Lawson }
22704953bccaSNate Lawson 
22714953bccaSNate Lawson /*
22724953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
22734953bccaSNate Lawson  * softc lock held.
22744953bccaSNate Lawson  */
22754953bccaSNate Lawson static void
22761845b5c3SMarius Strobl fxp_init_body(struct fxp_softc *sc, int setmedia)
22774953bccaSNate Lawson {
227841eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
22791845b5c3SMarius Strobl 	struct mii_data *mii;
2280a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
2281a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
2282b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
2283b2badf02SMaxime Henrion 	struct fxp_tx *txp;
22843212724cSJohn Baldwin 	int i, prm;
2285a17c678eSDavid Greenman 
228667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
22875506afefSPyun YongHyeon 
228841eb5ac3SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
22895506afefSPyun YongHyeon 		return;
22905506afefSPyun YongHyeon 
2291a17c678eSDavid Greenman 	/*
22923ba65732SDavid Greenman 	 * Cancel any pending I/O
2293a17c678eSDavid Greenman 	 */
22943ba65732SDavid Greenman 	fxp_stop(sc);
2295a17c678eSDavid Greenman 
22967137cea0SPyun YongHyeon 	/*
22977137cea0SPyun YongHyeon 	 * Issue software reset, which also unloads the microcode.
22987137cea0SPyun YongHyeon 	 */
22997137cea0SPyun YongHyeon 	sc->flags &= ~FXP_FLAG_UCODE;
23007137cea0SPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
23017137cea0SPyun YongHyeon 	DELAY(50);
23027137cea0SPyun YongHyeon 
230341eb5ac3SMarcel Moolenaar 	prm = (if_getflags(ifp) & IFF_PROMISC) ? 1 : 0;
2304a17c678eSDavid Greenman 
2305a17c678eSDavid Greenman 	/*
2306a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
2307a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
2308a17c678eSDavid Greenman 	 */
2309ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
23102e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
2311a17c678eSDavid Greenman 
2312ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23132e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
2314a17c678eSDavid Greenman 
2315a17c678eSDavid Greenman 	/*
2316a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
2317a17c678eSDavid Greenman 	 */
2318ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
23198da9c507SPyun YongHyeon 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
23208da9c507SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
23218da9c507SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2322b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
23232e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
2324a17c678eSDavid Greenman 
2325a17c678eSDavid Greenman 	/*
232672a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
2327b96ad4b2SPyun YongHyeon 	 * For ICH based controllers do not load microcode.
232872a32a26SJonathan Lemon 	 */
2329b96ad4b2SPyun YongHyeon 	if (sc->ident->ich == 0) {
233041eb5ac3SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_LINK0 &&
2331b96ad4b2SPyun YongHyeon 		    (sc->flags & FXP_FLAG_UCODE) == 0)
233272a32a26SJonathan Lemon 			fxp_load_ucode(sc);
2333b96ad4b2SPyun YongHyeon 	}
233472a32a26SJonathan Lemon 
233572a32a26SJonathan Lemon 	/*
23366b24912cSPyun YongHyeon 	 * Set IFF_ALLMULTI status. It's needed in configure action
23376b24912cSPyun YongHyeon 	 * command.
233809882363SJonathan Lemon 	 */
23396b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
234009882363SJonathan Lemon 
234109882363SJonathan Lemon 	/*
2342a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
2343a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
2344a17c678eSDavid Greenman 	 * later.
2345a17c678eSDavid Greenman 	 */
2346b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
2347a17c678eSDavid Greenman 
2348a17c678eSDavid Greenman 	/*
2349a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
2350a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
2351a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
2352a17c678eSDavid Greenman 	 */
2353b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
2354a17c678eSDavid Greenman 
2355a17c678eSDavid Greenman 	cbp->cb_status =	0;
235683e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
235783e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
235883e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
23592c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2360001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2361001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2362a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2363f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2364f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2365f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2366f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2367001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2368001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2369f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2370a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2371f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2372f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
23733114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2374f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2375f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2376f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
23778ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2378a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2379f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2380f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2381c21e84e4SPyun YongHyeon 	cbp->dyn_tbd =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2382c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2383f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2384f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2385f13075afSPyun YongHyeon 	cbp->tcp_udp_cksum =	((sc->flags & FXP_FLAG_82559_RXCSUM) != 0 &&
238641eb5ac3SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) ? 1 : 0;
2387f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2388f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2389f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2390f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2391a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2392a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2393a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2394a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2395a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2396a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2397a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2398a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2399f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2400f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2401f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2402f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2403f7788e8eSJonathan Lemon 
2404a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2405a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2406a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2407f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2408f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
24097137cea0SPyun YongHyeon 	cbp->magic_pkt_dis =	sc->flags & FXP_FLAG_WOL ? 0 : 1;
2410a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
24113ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2412a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
241341eb5ac3SMarcel Moolenaar 	cbp->mc_all =		if_getflags(ifp) & IFF_ALLMULTI ? 1 : prm;
2414c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2415bd4fa9d9SPyun YongHyeon 	cbp->vlan_strip_en =	((sc->flags & FXP_FLAG_EXT_RFA) != 0 &&
241641eb5ac3SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) ? 1 : 0;
2417a17c678eSDavid Greenman 
24181845b5c3SMarius Strobl 	if (sc->revision == FXP_REV_82557) {
24193bd07cfdSJonathan Lemon 		/*
24203bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
24213bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
24223bd07cfdSJonathan Lemon 		 */
24233bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
24243bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
24253bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
24263bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
24273bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
24283bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
24293bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
24303bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
24313bd07cfdSJonathan Lemon 	} else {
24321845b5c3SMarius Strobl 		/* Set pause RX FIFO threshold to 1KB. */
24331845b5c3SMarius Strobl 		CSR_WRITE_1(sc, FXP_CSR_FC_THRESH, 1);
24341845b5c3SMarius Strobl 		/* Set pause time. */
24351845b5c3SMarius Strobl 		cbp->fc_delay_lsb =	0xff;
24361845b5c3SMarius Strobl 		cbp->fc_delay_msb =	0xff;
24373bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
24381845b5c3SMarius Strobl 		mii = device_get_softc(sc->miibus);
24391845b5c3SMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
24401845b5c3SMarius Strobl 		    IFM_ETH_TXPAUSE) != 0)
24411845b5c3SMarius Strobl 			/* enable transmit FC */
24421845b5c3SMarius Strobl 			cbp->tx_fc_dis = 0;
24431845b5c3SMarius Strobl 		else
24441845b5c3SMarius Strobl 			/* disable transmit FC */
24451845b5c3SMarius Strobl 			cbp->tx_fc_dis = 1;
24461845b5c3SMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
24471845b5c3SMarius Strobl 		    IFM_ETH_RXPAUSE) != 0) {
24481845b5c3SMarius Strobl 			/* enable FC restart/restop frames */
24491845b5c3SMarius Strobl 			cbp->rx_fc_restart = 1;
24501845b5c3SMarius Strobl 			cbp->rx_fc_restop = 1;
24511845b5c3SMarius Strobl 		} else {
24521845b5c3SMarius Strobl 			/* disable FC restart/restop frames */
24531845b5c3SMarius Strobl 			cbp->rx_fc_restart = 0;
24541845b5c3SMarius Strobl 			cbp->rx_fc_restop = 0;
24551845b5c3SMarius Strobl 		}
24563bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
24573bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
24583bd07cfdSJonathan Lemon 	}
24593bd07cfdSJonathan Lemon 
24608da9c507SPyun YongHyeon 	/* Enable 82558 and 82559 extended statistics functionality. */
24618da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4) {
24628da9c507SPyun YongHyeon 		if (sc->revision >= FXP_REV_82559_A0) {
24638da9c507SPyun YongHyeon 			/*
24648da9c507SPyun YongHyeon 			 * Extend configuration table size to 32
24658da9c507SPyun YongHyeon 			 * to include TCO configuration.
24668da9c507SPyun YongHyeon 			 */
24678da9c507SPyun YongHyeon 			cbp->byte_count = 32;
24688da9c507SPyun YongHyeon 			cbp->ext_stats_dis = 1;
24698da9c507SPyun YongHyeon 			/* Enable TCO stats. */
24708da9c507SPyun YongHyeon 			cbp->tno_int_or_tco_en = 1;
24718da9c507SPyun YongHyeon 			cbp->gamla_rx = 1;
24728da9c507SPyun YongHyeon 		} else
24738da9c507SPyun YongHyeon 			cbp->ext_stats_dis = 0;
24748da9c507SPyun YongHyeon 	}
24758da9c507SPyun YongHyeon 
2476a17c678eSDavid Greenman 	/*
2477a17c678eSDavid Greenman 	 * Start the config command/DMA.
2478a17c678eSDavid Greenman 	 */
2479ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
24805986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
24815986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2482b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
24832e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2484a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2485209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2486a17c678eSDavid Greenman 
2487a17c678eSDavid Greenman 	/*
2488a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2489a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2490a17c678eSDavid Greenman 	 */
2491b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2492a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
249383e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
249483e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
249541eb5ac3SMarcel Moolenaar 	bcopy(if_getlladdr(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2496a17c678eSDavid Greenman 
2497a17c678eSDavid Greenman 	/*
2498a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2499a17c678eSDavid Greenman 	 */
2500ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
25015986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
25025986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
25036b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
25042e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2505a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2506209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2507a17c678eSDavid Greenman 
2508a17c678eSDavid Greenman 	/*
25096b24912cSPyun YongHyeon 	 * Initialize the multicast address list.
25106b24912cSPyun YongHyeon 	 */
25116b24912cSPyun YongHyeon 	fxp_mc_setup(sc);
25126b24912cSPyun YongHyeon 
25136b24912cSPyun YongHyeon 	/*
2514a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2515a17c678eSDavid Greenman 	 */
2516b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2517b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2518b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2519a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2520b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
252183e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
252283e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
252383e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
252483e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
25253bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2526b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
252783e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
25283bd07cfdSJonathan Lemon 		else
2529b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
253083e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2531b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2532a17c678eSDavid Greenman 	}
2533a17c678eSDavid Greenman 	/*
2534397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2535a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2536a17c678eSDavid Greenman 	 */
253783e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2538a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
2539a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2540b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2541397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2542a17c678eSDavid Greenman 
2543ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
25446b24912cSPyun YongHyeon 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
25452e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2546a17c678eSDavid Greenman 
2547a17c678eSDavid Greenman 	/*
2548a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2549a17c678eSDavid Greenman 	 */
2550ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2551b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
25522e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2553a17c678eSDavid Greenman 
25541845b5c3SMarius Strobl 	if (sc->miibus != NULL && setmedia != 0)
2555f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2556dccee1a1SDavid Greenman 
255741eb5ac3SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
2558e8c8b728SJonathan Lemon 
2559e8c8b728SJonathan Lemon 	/*
2560e8c8b728SJonathan Lemon 	 * Enable interrupts.
2561e8c8b728SJonathan Lemon 	 */
25622b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
25632b5989e9SLuigi Rizzo 	/*
25642b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
25652b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
25662b5989e9SLuigi Rizzo 	 */
256741eb5ac3SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING )
25682b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
25692b5989e9SLuigi Rizzo 	else
25702b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2571e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2572a17c678eSDavid Greenman 
2573a17c678eSDavid Greenman 	/*
2574a17c678eSDavid Greenman 	 * Start stats updater.
2575a17c678eSDavid Greenman 	 */
257645276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2577f7788e8eSJonathan Lemon }
2578f7788e8eSJonathan Lemon 
2579f7788e8eSJonathan Lemon static int
258041eb5ac3SMarcel Moolenaar fxp_serial_ifmedia_upd(if_t ifp)
2581f7788e8eSJonathan Lemon {
2582f7788e8eSJonathan Lemon 
2583f7788e8eSJonathan Lemon 	return (0);
2584a17c678eSDavid Greenman }
2585a17c678eSDavid Greenman 
2586303b270bSEivind Eklund static void
258741eb5ac3SMarcel Moolenaar fxp_serial_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
2588ba8c6fd5SDavid Greenman {
2589ba8c6fd5SDavid Greenman 
2590f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2591ba8c6fd5SDavid Greenman }
2592ba8c6fd5SDavid Greenman 
2593ba8c6fd5SDavid Greenman /*
2594ba8c6fd5SDavid Greenman  * Change media according to request.
2595ba8c6fd5SDavid Greenman  */
2596f7788e8eSJonathan Lemon static int
259741eb5ac3SMarcel Moolenaar fxp_ifmedia_upd(if_t ifp)
2598ba8c6fd5SDavid Greenman {
259941eb5ac3SMarcel Moolenaar 	struct fxp_softc *sc = if_getsoftc(ifp);
2600f7788e8eSJonathan Lemon 	struct mii_data *mii;
26013fcb7a53SMarius Strobl 	struct mii_softc	*miisc;
2602ba8c6fd5SDavid Greenman 
2603f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
26043212724cSJohn Baldwin 	FXP_LOCK(sc);
26055aa0cdf4SJohn-Mark Gurney 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
26063fcb7a53SMarius Strobl 		PHY_RESET(miisc);
2607f7788e8eSJonathan Lemon 	mii_mediachg(mii);
26083212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2609ba8c6fd5SDavid Greenman 	return (0);
2610ba8c6fd5SDavid Greenman }
2611ba8c6fd5SDavid Greenman 
2612ba8c6fd5SDavid Greenman /*
2613ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2614ba8c6fd5SDavid Greenman  */
2615f7788e8eSJonathan Lemon static void
261641eb5ac3SMarcel Moolenaar fxp_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
2617ba8c6fd5SDavid Greenman {
261841eb5ac3SMarcel Moolenaar 	struct fxp_softc *sc = if_getsoftc(ifp);
2619f7788e8eSJonathan Lemon 	struct mii_data *mii;
2620ba8c6fd5SDavid Greenman 
2621f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
26223212724cSJohn Baldwin 	FXP_LOCK(sc);
2623f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2624f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2625f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
26263212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2627ba8c6fd5SDavid Greenman }
2628ba8c6fd5SDavid Greenman 
2629a17c678eSDavid Greenman /*
2630a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2631a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
263285050421SPyun YongHyeon  * reusing the RFA buffer.
2633a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2634a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2635a17c678eSDavid Greenman  */
2636a17c678eSDavid Greenman static int
263785050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2638a17c678eSDavid Greenman {
2639a17c678eSDavid Greenman 	struct mbuf *m;
264085050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2641b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
264285050421SPyun YongHyeon 	int error;
2643a17c678eSDavid Greenman 
2644c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
264585050421SPyun YongHyeon 	if (m == NULL)
264685050421SPyun YongHyeon 		return (ENOBUFS);
2647ba8c6fd5SDavid Greenman 
2648ba8c6fd5SDavid Greenman 	/*
2649ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2650ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2651ba8c6fd5SDavid Greenman 	 */
2652ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2653ba8c6fd5SDavid Greenman 
2654eadd5e3aSDavid Greenman 	/*
2655eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2656eadd5e3aSDavid Greenman 	 * data start past it.
2657eadd5e3aSDavid Greenman 	 */
2658a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2659c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
266083e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2661eadd5e3aSDavid Greenman 
2662a17c678eSDavid Greenman 	rfa->rfa_status = 0;
266383e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2664a17c678eSDavid Greenman 	rfa->actual_size = 0;
266585050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
266685050421SPyun YongHyeon 	    sc->rfa_size;
2667ba8c6fd5SDavid Greenman 
266828935f27SMaxime Henrion 	/*
266928935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
267028935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
267128935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
267228935f27SMaxime Henrion 	 * is also alignment-safe.
267328935f27SMaxime Henrion 	 */
267483e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
267583e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2676ba8c6fd5SDavid Greenman 
2677b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2678a2057a72SPyun YongHyeon 	error = bus_dmamap_load(sc->fxp_rxmtag, sc->spare_map, rfa,
2679b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
268001e3ef82SPyun YongHyeon 	    &rxp->rx_addr, BUS_DMA_NOWAIT);
2681b2badf02SMaxime Henrion 	if (error) {
2682b2badf02SMaxime Henrion 		m_freem(m);
2683b2badf02SMaxime Henrion 		return (error);
2684b2badf02SMaxime Henrion 	}
2685b2badf02SMaxime Henrion 
2686e2157cf7SPyun YongHyeon 	if (rxp->rx_mbuf != NULL)
2687a2057a72SPyun YongHyeon 		bus_dmamap_unload(sc->fxp_rxmtag, rxp->rx_map);
2688b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2689b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2690b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2691b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2692b2badf02SMaxime Henrion 
2693a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
2694b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
269585050421SPyun YongHyeon 	return (0);
269685050421SPyun YongHyeon }
269785050421SPyun YongHyeon 
269885050421SPyun YongHyeon static void
269985050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
270085050421SPyun YongHyeon {
270185050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
270285050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2703b2badf02SMaxime Henrion 
2704dfe61cf1SDavid Greenman 	/*
2705dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2706dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2707dfe61cf1SDavid Greenman 	 */
2708b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2709b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2710b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2711b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2712b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
271383e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2714aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2715a2057a72SPyun YongHyeon 		bus_dmamap_sync(sc->fxp_rxmtag, p_rx->rx_map,
27164812aef5SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2717a17c678eSDavid Greenman 	} else {
2718b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2719b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2720a17c678eSDavid Greenman 	}
2721b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
272285050421SPyun YongHyeon }
272385050421SPyun YongHyeon 
272485050421SPyun YongHyeon static void
272585050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
272685050421SPyun YongHyeon {
272785050421SPyun YongHyeon 	struct mbuf *m;
272885050421SPyun YongHyeon 	struct fxp_rfa *rfa;
272985050421SPyun YongHyeon 
273085050421SPyun YongHyeon 	m = rxp->rx_mbuf;
273185050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
273285050421SPyun YongHyeon 	/*
273385050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
273485050421SPyun YongHyeon 	 * will be 32-bit aligned.
273585050421SPyun YongHyeon 	 */
273685050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
273785050421SPyun YongHyeon 
273885050421SPyun YongHyeon 	/*
273985050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
274085050421SPyun YongHyeon 	 * data start past it.
274185050421SPyun YongHyeon 	 */
274285050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
274385050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
274485050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
274585050421SPyun YongHyeon 
274685050421SPyun YongHyeon 	rfa->rfa_status = 0;
274785050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
274885050421SPyun YongHyeon 	rfa->actual_size = 0;
274985050421SPyun YongHyeon 
275085050421SPyun YongHyeon 	/*
275185050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
275285050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
275385050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
275485050421SPyun YongHyeon 	 * is also alignment-safe.
275585050421SPyun YongHyeon 	 */
275685050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
275785050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
275885050421SPyun YongHyeon 
2759a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_rxmtag, rxp->rx_map,
276085050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2761a17c678eSDavid Greenman }
2762a17c678eSDavid Greenman 
2763f1928b0cSKevin Lo static int
2764f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2765dccee1a1SDavid Greenman {
2766f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2767dccee1a1SDavid Greenman 	int count = 10000;
27686ebc3153SDavid Greenman 	int value;
2769dccee1a1SDavid Greenman 
2770ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2771ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2772dccee1a1SDavid Greenman 
2773ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2774ba8c6fd5SDavid Greenman 	    && count--)
27756ebc3153SDavid Greenman 		DELAY(10);
2776dccee1a1SDavid Greenman 
2777dccee1a1SDavid Greenman 	if (count <= 0)
2778f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2779dccee1a1SDavid Greenman 
27806ebc3153SDavid Greenman 	return (value & 0xffff);
2781dccee1a1SDavid Greenman }
2782dccee1a1SDavid Greenman 
278316ec4b00SWarner Losh static int
2784f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2785dccee1a1SDavid Greenman {
2786f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2787dccee1a1SDavid Greenman 	int count = 10000;
2788dccee1a1SDavid Greenman 
2789ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2790ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2791ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2792dccee1a1SDavid Greenman 
2793ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2794ba8c6fd5SDavid Greenman 	    count--)
27956ebc3153SDavid Greenman 		DELAY(10);
2796dccee1a1SDavid Greenman 
2797dccee1a1SDavid Greenman 	if (count <= 0)
2798f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
279916ec4b00SWarner Losh 	return (0);
2800dccee1a1SDavid Greenman }
2801dccee1a1SDavid Greenman 
28021845b5c3SMarius Strobl static void
28031845b5c3SMarius Strobl fxp_miibus_statchg(device_t dev)
28041845b5c3SMarius Strobl {
28051845b5c3SMarius Strobl 	struct fxp_softc *sc;
28061845b5c3SMarius Strobl 	struct mii_data *mii;
280741eb5ac3SMarcel Moolenaar 	if_t ifp;
28081845b5c3SMarius Strobl 
28091845b5c3SMarius Strobl 	sc = device_get_softc(dev);
28101845b5c3SMarius Strobl 	mii = device_get_softc(sc->miibus);
28111845b5c3SMarius Strobl 	ifp = sc->ifp;
281241eb5ac3SMarcel Moolenaar 	if (mii == NULL || ifp == (void *)NULL ||
281341eb5ac3SMarcel Moolenaar 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
28141845b5c3SMarius Strobl 	    (mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) !=
28151845b5c3SMarius Strobl 	    (IFM_AVALID | IFM_ACTIVE))
28161845b5c3SMarius Strobl 		return;
28171845b5c3SMarius Strobl 
2818c3f52a31SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T &&
2819c3f52a31SPyun YongHyeon 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
2820c3f52a31SPyun YongHyeon 		sc->cu_resume_bug = 1;
2821c3f52a31SPyun YongHyeon 	else
2822c3f52a31SPyun YongHyeon 		sc->cu_resume_bug = 0;
28231845b5c3SMarius Strobl 	/*
28241845b5c3SMarius Strobl 	 * Call fxp_init_body in order to adjust the flow control settings.
28251845b5c3SMarius Strobl 	 * Note that the 82557 doesn't support hardware flow control.
28261845b5c3SMarius Strobl 	 */
28271845b5c3SMarius Strobl 	if (sc->revision == FXP_REV_82557)
28281845b5c3SMarius Strobl 		return;
282941eb5ac3SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
28301845b5c3SMarius Strobl 	fxp_init_body(sc, 0);
28311845b5c3SMarius Strobl }
28321845b5c3SMarius Strobl 
2833dccee1a1SDavid Greenman static int
283441eb5ac3SMarcel Moolenaar fxp_ioctl(if_t ifp, u_long command, caddr_t data)
2835a17c678eSDavid Greenman {
283641eb5ac3SMarcel Moolenaar 	struct fxp_softc *sc = if_getsoftc(ifp);
2837a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2838f7788e8eSJonathan Lemon 	struct mii_data *mii;
283960bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2840a17c678eSDavid Greenman 
2841a17c678eSDavid Greenman 	switch (command) {
2842a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
28433212724cSJohn Baldwin 		FXP_LOCK(sc);
2844a17c678eSDavid Greenman 		/*
2845a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2846a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2847a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2848a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2849a17c678eSDavid Greenman 		 */
285041eb5ac3SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
285141eb5ac3SMarcel Moolenaar 			if (((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) &&
285241eb5ac3SMarcel Moolenaar 			    ((if_getflags(ifp) ^ sc->if_flags) &
28535506afefSPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI | IFF_LINK0)) != 0) {
285441eb5ac3SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2855a461b201SPyun YongHyeon 				fxp_init_body(sc, 0);
285641eb5ac3SMarcel Moolenaar 			} else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
28571845b5c3SMarius Strobl 				fxp_init_body(sc, 1);
2858a17c678eSDavid Greenman 		} else {
285941eb5ac3SMarcel Moolenaar 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
28604a5f1499SDavid Greenman 				fxp_stop(sc);
2861a17c678eSDavid Greenman 		}
286241eb5ac3SMarcel Moolenaar 		sc->if_flags = if_getflags(ifp);
28633212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2864a17c678eSDavid Greenman 		break;
2865a17c678eSDavid Greenman 
2866a17c678eSDavid Greenman 	case SIOCADDMULTI:
2867a17c678eSDavid Greenman 	case SIOCDELMULTI:
2868f6ff7180SPyun YongHyeon 		FXP_LOCK(sc);
286941eb5ac3SMarcel Moolenaar 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
287041eb5ac3SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2871f6ff7180SPyun YongHyeon 			fxp_init_body(sc, 0);
28725506afefSPyun YongHyeon 		}
2873f6ff7180SPyun YongHyeon 		FXP_UNLOCK(sc);
2874ba8c6fd5SDavid Greenman 		break;
2875ba8c6fd5SDavid Greenman 
2876ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2877ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2878f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2879f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
288009a8241fSGleb Smirnoff                         error = ifmedia_ioctl(ifp, ifr,
2881f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2882f7788e8eSJonathan Lemon 		} else {
288309a8241fSGleb Smirnoff                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2884f7788e8eSJonathan Lemon 		}
2885a17c678eSDavid Greenman 		break;
2886a17c678eSDavid Greenman 
2887fb917226SRuslan Ermilov 	case SIOCSIFCAP:
288860bb79ebSPyun YongHyeon 		reinit = 0;
288941eb5ac3SMarcel Moolenaar 		mask = if_getcapenable(ifp) ^ ifr->ifr_reqcap;
289040929967SGleb Smirnoff #ifdef DEVICE_POLLING
289140929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
289240929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
2893bd071d4dSGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
289440929967SGleb Smirnoff 				if (error)
289540929967SGleb Smirnoff 					return(error);
289640929967SGleb Smirnoff 				FXP_LOCK(sc);
289740929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
289840929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
289941eb5ac3SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
290040929967SGleb Smirnoff 				FXP_UNLOCK(sc);
290140929967SGleb Smirnoff 			} else {
2902bd071d4dSGleb Smirnoff 				error = ether_poll_deregister(ifp);
290340929967SGleb Smirnoff 				/* Enable interrupts in any case */
290440929967SGleb Smirnoff 				FXP_LOCK(sc);
290540929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
290641eb5ac3SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
290740929967SGleb Smirnoff 				FXP_UNLOCK(sc);
290840929967SGleb Smirnoff 			}
290940929967SGleb Smirnoff 		}
291040929967SGleb Smirnoff #endif
291140929967SGleb Smirnoff 		FXP_LOCK(sc);
291260bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
291341eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
291441eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
291541eb5ac3SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
291641eb5ac3SMarcel Moolenaar 				if_sethwassistbits(ifp, FXP_CSUM_FEATURES, 0);
291760bb79ebSPyun YongHyeon 			else
291841eb5ac3SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, FXP_CSUM_FEATURES);
291960bb79ebSPyun YongHyeon 		}
292060bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
292141eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0) {
292241eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
2923f13075afSPyun YongHyeon 			if ((sc->flags & FXP_FLAG_82559_RXCSUM) != 0)
2924f13075afSPyun YongHyeon 				reinit++;
2925f13075afSPyun YongHyeon 		}
2926c21e84e4SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
292741eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
292841eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
292941eb5ac3SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
293041eb5ac3SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
2931c21e84e4SPyun YongHyeon 			else
293241eb5ac3SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
2933c21e84e4SPyun YongHyeon 		}
29347137cea0SPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
293541eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) != 0)
293641eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
293760bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
293841eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) != 0) {
293941eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
29408ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
29418ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
29428ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
29438ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
29448ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
294541eb5ac3SMarcel Moolenaar 			if (if_getflags(ifp) & IFF_UP)
294660bb79ebSPyun YongHyeon 				reinit++;
294760bb79ebSPyun YongHyeon 		}
2948713ca255SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
294941eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWCSUM) != 0)
295041eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
2951713ca255SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
295241eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
295341eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
2954bd4fa9d9SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
295541eb5ac3SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
295641eb5ac3SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
295741eb5ac3SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
295841eb5ac3SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO |
295941eb5ac3SMarcel Moolenaar 				    IFCAP_VLAN_HWCSUM);
2960bd4fa9d9SPyun YongHyeon 			reinit++;
2961bd4fa9d9SPyun YongHyeon 		}
296241eb5ac3SMarcel Moolenaar 		if (reinit > 0 &&
296341eb5ac3SMarcel Moolenaar 		    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
296441eb5ac3SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2965a461b201SPyun YongHyeon 			fxp_init_body(sc, 0);
29665506afefSPyun YongHyeon 		}
29673212724cSJohn Baldwin 		FXP_UNLOCK(sc);
296841eb5ac3SMarcel Moolenaar 		if_vlancap(ifp);
2969fb917226SRuslan Ermilov 		break;
2970fb917226SRuslan Ermilov 
2971a17c678eSDavid Greenman 	default:
297209a8241fSGleb Smirnoff 		error = ether_ioctl(ifp, command, data);
2973a17c678eSDavid Greenman 	}
2974a17c678eSDavid Greenman 	return (error);
2975a17c678eSDavid Greenman }
2976397f9dfeSDavid Greenman 
2977397f9dfeSDavid Greenman /*
297809882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
297909882363SJonathan Lemon  */
298009882363SJonathan Lemon static int
298109882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
298209882363SJonathan Lemon {
298309882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
298441eb5ac3SMarcel Moolenaar 	if_t ifp = sc->ifp;
298541eb5ac3SMarcel Moolenaar 	int nmcasts = 0;
298609882363SJonathan Lemon 
298741eb5ac3SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_ALLMULTI) == 0) {
2988eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
298941eb5ac3SMarcel Moolenaar 		if_setupmultiaddr(ifp, mcsp->mc_addr, &nmcasts, MAXMCADDR);
299009882363SJonathan Lemon 		if (nmcasts >= MAXMCADDR) {
299141eb5ac3SMarcel Moolenaar 			if_setflagbits(ifp, IFF_ALLMULTI, 0);
299209882363SJonathan Lemon 			nmcasts = 0;
299309882363SJonathan Lemon 		}
2994eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
299509882363SJonathan Lemon 	}
2996bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
299709882363SJonathan Lemon 	return (nmcasts);
299809882363SJonathan Lemon }
299909882363SJonathan Lemon 
300009882363SJonathan Lemon /*
3001397f9dfeSDavid Greenman  * Program the multicast filter.
3002397f9dfeSDavid Greenman  *
3003397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
3004397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
30053114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
3006397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
3007dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
3008397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
3009397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
3010397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
3011397f9dfeSDavid Greenman  */
3012397f9dfeSDavid Greenman static void
3013f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
3014397f9dfeSDavid Greenman {
30156b24912cSPyun YongHyeon 	struct fxp_cb_mcs *mcsp;
30167dced78aSDavid Greenman 	int count;
3017397f9dfeSDavid Greenman 
301867fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
30193114fdb4SDavid Greenman 
30206b24912cSPyun YongHyeon 	mcsp = sc->mcsp;
3021397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
30226b24912cSPyun YongHyeon 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
30236b24912cSPyun YongHyeon 	mcsp->link_addr = 0xffffffff;
30246b24912cSPyun YongHyeon 	fxp_mc_addrs(sc);
3025397f9dfeSDavid Greenman 
3026397f9dfeSDavid Greenman 	/*
30276b24912cSPyun YongHyeon 	 * Wait until command unit is idle. This should never be the
30286b24912cSPyun YongHyeon 	 * case when nothing is queued, but make sure anyway.
3029397f9dfeSDavid Greenman 	 */
30307dced78aSDavid Greenman 	count = 100;
30316b24912cSPyun YongHyeon 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) !=
30326b24912cSPyun YongHyeon 	    FXP_SCB_CUS_IDLE && --count)
30337dced78aSDavid Greenman 		DELAY(10);
30347dced78aSDavid Greenman 	if (count == 0) {
3035f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
30367dced78aSDavid Greenman 		return;
30377dced78aSDavid Greenman 	}
3038397f9dfeSDavid Greenman 
3039397f9dfeSDavid Greenman 	/*
3040397f9dfeSDavid Greenman 	 * Start the multicast setup command.
3041397f9dfeSDavid Greenman 	 */
3042397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
3043a2057a72SPyun YongHyeon 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
3044a2057a72SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3045b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
30462e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
30476b24912cSPyun YongHyeon 	/* ...and wait for it to complete. */
30486b24912cSPyun YongHyeon 	fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
3049397f9dfeSDavid Greenman }
305072a32a26SJonathan Lemon 
305174d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
305274d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
305374d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
305474d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
305574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
305674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
3057de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
305872a32a26SJonathan Lemon 
305974d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
306072a32a26SJonathan Lemon 
3061e0fe5c6dSMarius Strobl static const struct ucode {
306274d1ed23SMaxime Henrion 	uint32_t	revision;
306374d1ed23SMaxime Henrion 	uint32_t	*ucode;
306472a32a26SJonathan Lemon 	int		length;
306572a32a26SJonathan Lemon 	u_short		int_delay_offset;
306672a32a26SJonathan Lemon 	u_short		bundle_max_offset;
306729658c96SDimitry Andric } ucode_table[] = {
306872a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
306972a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
307072a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
307172a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
307272a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
307372a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
307472a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
307572a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
307672a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
307772a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
3078507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
3079de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
308050df388dSPyun YongHyeon 	{ FXP_REV_82551_10, UCODE(fxp_ucode_d102e),
308150df388dSPyun YongHyeon 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
308272a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
308372a32a26SJonathan Lemon };
308472a32a26SJonathan Lemon 
308572a32a26SJonathan Lemon static void
308672a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
308772a32a26SJonathan Lemon {
3088e0fe5c6dSMarius Strobl 	const struct ucode *uc;
308972a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
309094a4f968SPyun YongHyeon 	int i;
309172a32a26SJonathan Lemon 
30921343a72fSPyun YongHyeon 	if (sc->flags & FXP_FLAG_NO_UCODE)
30931343a72fSPyun YongHyeon 		return;
30941343a72fSPyun YongHyeon 
309572a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
309672a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
309772a32a26SJonathan Lemon 			break;
309872a32a26SJonathan Lemon 	if (uc->ucode == NULL)
309972a32a26SJonathan Lemon 		return;
3100b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
310172a32a26SJonathan Lemon 	cbp->cb_status = 0;
310283e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
310383e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
310494a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
310594a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
310672a32a26SJonathan Lemon 	if (uc->int_delay_offset)
310774d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
310883e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
310972a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
311074d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
311183e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
311272a32a26SJonathan Lemon 	/*
311372a32a26SJonathan Lemon 	 * Download the ucode to the chip.
311472a32a26SJonathan Lemon 	 */
311572a32a26SJonathan Lemon 	fxp_scb_wait(sc);
31165986d0d2SPyun YongHyeon 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map,
31175986d0d2SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3118b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
311972a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
312072a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
3121209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
312272a32a26SJonathan Lemon 	device_printf(sc->dev,
312372a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
312472a32a26SJonathan Lemon 	    sc->tunable_int_delay,
312572a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
312672a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
31271343a72fSPyun YongHyeon 	bzero(cbp, FXP_TXCB_SZ);
312872a32a26SJonathan Lemon }
312972a32a26SJonathan Lemon 
31308da9c507SPyun YongHyeon #define FXP_SYSCTL_STAT_ADD(c, h, n, p, d)	\
31318da9c507SPyun YongHyeon 	SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
31328da9c507SPyun YongHyeon 
31338da9c507SPyun YongHyeon static void
31348da9c507SPyun YongHyeon fxp_sysctl_node(struct fxp_softc *sc)
31358da9c507SPyun YongHyeon {
31368da9c507SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
31378da9c507SPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
31388da9c507SPyun YongHyeon 	struct sysctl_oid *tree;
31398da9c507SPyun YongHyeon 	struct fxp_hwstats *hsp;
31408da9c507SPyun YongHyeon 
31418da9c507SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->dev);
31428da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
31438da9c507SPyun YongHyeon 
31448da9c507SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child,
31458da9c507SPyun YongHyeon 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
31468da9c507SPyun YongHyeon 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
31478da9c507SPyun YongHyeon 	    "FXP driver receive interrupt microcode bundling delay");
31488da9c507SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child,
31498da9c507SPyun YongHyeon 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
31508da9c507SPyun YongHyeon 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
31518da9c507SPyun YongHyeon 	    "FXP driver receive interrupt microcode bundle size limit");
31528da9c507SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, child,OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
31538da9c507SPyun YongHyeon 	    "FXP RNR events");
31548da9c507SPyun YongHyeon 
31558da9c507SPyun YongHyeon 	/*
31568da9c507SPyun YongHyeon 	 * Pull in device tunables.
31578da9c507SPyun YongHyeon 	 */
31588da9c507SPyun YongHyeon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
31598da9c507SPyun YongHyeon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
31608da9c507SPyun YongHyeon 	(void) resource_int_value(device_get_name(sc->dev),
31618da9c507SPyun YongHyeon 	    device_get_unit(sc->dev), "int_delay", &sc->tunable_int_delay);
31628da9c507SPyun YongHyeon 	(void) resource_int_value(device_get_name(sc->dev),
31638da9c507SPyun YongHyeon 	    device_get_unit(sc->dev), "bundle_max", &sc->tunable_bundle_max);
31648da9c507SPyun YongHyeon 	sc->rnr = 0;
31658da9c507SPyun YongHyeon 
31668da9c507SPyun YongHyeon 	hsp = &sc->fxp_hwstats;
31678da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
31688da9c507SPyun YongHyeon 	    NULL, "FXP statistics");
31698da9c507SPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
31708da9c507SPyun YongHyeon 
31718da9c507SPyun YongHyeon 	/* Rx MAC statistics. */
31728da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
31738da9c507SPyun YongHyeon 	    NULL, "Rx MAC statistics");
31748da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
31758da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "good_frames",
31768da9c507SPyun YongHyeon 	    &hsp->rx_good, "Good frames");
31778da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "crc_errors",
31788da9c507SPyun YongHyeon 	    &hsp->rx_crc_errors, "CRC errors");
31798da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "alignment_errors",
31808da9c507SPyun YongHyeon 	    &hsp->rx_alignment_errors, "Alignment errors");
31818da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "rnr_errors",
31828da9c507SPyun YongHyeon 	    &hsp->rx_rnr_errors, "RNR errors");
31838da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "overrun_errors",
31848da9c507SPyun YongHyeon 	    &hsp->rx_overrun_errors, "Overrun errors");
31858da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "cdt_errors",
31868da9c507SPyun YongHyeon 	    &hsp->rx_cdt_errors, "Collision detect errors");
31878da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "shortframes",
31888da9c507SPyun YongHyeon 	    &hsp->rx_shortframes, "Short frame errors");
31898da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4) {
31908da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "pause",
31918da9c507SPyun YongHyeon 		    &hsp->rx_pause, "Pause frames");
31928da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "controls",
31938da9c507SPyun YongHyeon 		    &hsp->rx_controls, "Unsupported control frames");
31948da9c507SPyun YongHyeon 	}
31958da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
31968da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "tco",
31978da9c507SPyun YongHyeon 		    &hsp->rx_tco, "TCO frames");
31988da9c507SPyun YongHyeon 
31998da9c507SPyun YongHyeon 	/* Tx MAC statistics. */
32008da9c507SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
32018da9c507SPyun YongHyeon 	    NULL, "Tx MAC statistics");
32028da9c507SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
32038da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "good_frames",
32048da9c507SPyun YongHyeon 	    &hsp->tx_good, "Good frames");
32058da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "maxcols",
32068da9c507SPyun YongHyeon 	    &hsp->tx_maxcols, "Maximum collisions errors");
32078da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "latecols",
32088da9c507SPyun YongHyeon 	    &hsp->tx_latecols, "Late collisions errors");
32098da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "underruns",
32108da9c507SPyun YongHyeon 	    &hsp->tx_underruns, "Underrun errors");
32118da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "lostcrs",
32128da9c507SPyun YongHyeon 	    &hsp->tx_lostcrs, "Lost carrier sense");
32138da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "deffered",
32148da9c507SPyun YongHyeon 	    &hsp->tx_deffered, "Deferred");
32158da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "single_collisions",
32168da9c507SPyun YongHyeon 	    &hsp->tx_single_collisions, "Single collisions");
32178da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "multiple_collisions",
32188da9c507SPyun YongHyeon 	    &hsp->tx_multiple_collisions, "Multiple collisions");
32198da9c507SPyun YongHyeon 	FXP_SYSCTL_STAT_ADD(ctx, child, "total_collisions",
32208da9c507SPyun YongHyeon 	    &hsp->tx_total_collisions, "Total collisions");
32218da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82558_A4)
32228da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "pause",
32238da9c507SPyun YongHyeon 		    &hsp->tx_pause, "Pause frames");
32248da9c507SPyun YongHyeon 	if (sc->revision >= FXP_REV_82559_A0)
32258da9c507SPyun YongHyeon 		FXP_SYSCTL_STAT_ADD(ctx, child, "tco",
32268da9c507SPyun YongHyeon 		    &hsp->tx_tco, "TCO frames");
32278da9c507SPyun YongHyeon }
32288da9c507SPyun YongHyeon 
32298da9c507SPyun YongHyeon #undef FXP_SYSCTL_STAT_ADD
32308da9c507SPyun YongHyeon 
323172a32a26SJonathan Lemon static int
323272a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
323372a32a26SJonathan Lemon {
323472a32a26SJonathan Lemon 	int error, value;
323572a32a26SJonathan Lemon 
323672a32a26SJonathan Lemon 	value = *(int *)arg1;
323772a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
323872a32a26SJonathan Lemon 	if (error || !req->newptr)
323972a32a26SJonathan Lemon 		return (error);
324072a32a26SJonathan Lemon 	if (value < low || value > high)
324172a32a26SJonathan Lemon 		return (EINVAL);
324272a32a26SJonathan Lemon 	*(int *)arg1 = value;
324372a32a26SJonathan Lemon 	return (0);
324472a32a26SJonathan Lemon }
324572a32a26SJonathan Lemon 
324672a32a26SJonathan Lemon /*
324772a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
324872a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
324972a32a26SJonathan Lemon  */
325072a32a26SJonathan Lemon static int
325172a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
325272a32a26SJonathan Lemon {
3253e0fe5c6dSMarius Strobl 
325472a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
325572a32a26SJonathan Lemon }
325672a32a26SJonathan Lemon 
325772a32a26SJonathan Lemon static int
325872a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
325972a32a26SJonathan Lemon {
3260e0fe5c6dSMarius Strobl 
326172a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
326272a32a26SJonathan Lemon }
3263