xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 85050421787dc3c435fe9cd2782d0389c6bd4246)
1f7788e8eSJonathan Lemon /*-
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
33bd07cfdSJonathan Lemon  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
4a17c678eSDavid Greenman  * All rights reserved.
5a17c678eSDavid Greenman  *
6a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
7a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
8a17c678eSDavid Greenman  * are met:
9a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
10a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
11a17c678eSDavid Greenman  *    disclaimer.
12a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
13a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
14a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
15a17c678eSDavid Greenman  *
16a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a17c678eSDavid Greenman  * SUCH DAMAGE.
27a17c678eSDavid Greenman  *
28a17c678eSDavid Greenman  */
29a17c678eSDavid Greenman 
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
32aad970f1SDavid E. O'Brien 
33a17c678eSDavid Greenman /*
34ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
35a17c678eSDavid Greenman  */
36a17c678eSDavid Greenman 
37f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
38f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
39f0796cd2SGleb Smirnoff #endif
40f0796cd2SGleb Smirnoff 
41a17c678eSDavid Greenman #include <sys/param.h>
42a17c678eSDavid Greenman #include <sys/systm.h>
438fae3bd4SPyun YongHyeon #include <sys/bus.h>
4483e6547dSMaxime Henrion #include <sys/endian.h>
45a17c678eSDavid Greenman #include <sys/kernel.h>
468fae3bd4SPyun YongHyeon #include <sys/mbuf.h>
47fe12f24bSPoul-Henning Kamp #include <sys/module.h>
488fae3bd4SPyun YongHyeon #include <sys/rman.h>
494458ac71SBruce Evans #include <sys/socket.h>
508fae3bd4SPyun YongHyeon #include <sys/sockio.h>
5172a32a26SJonathan Lemon #include <sys/sysctl.h>
52a17c678eSDavid Greenman 
538fae3bd4SPyun YongHyeon #include <net/bpf.h>
548fae3bd4SPyun YongHyeon #include <net/ethernet.h>
55a17c678eSDavid Greenman #include <net/if.h>
568fae3bd4SPyun YongHyeon #include <net/if_arp.h>
57397f9dfeSDavid Greenman #include <net/if_dl.h>
58ba8c6fd5SDavid Greenman #include <net/if_media.h>
59e8c8b728SJonathan Lemon #include <net/if_types.h>
60e8c8b728SJonathan Lemon #include <net/if_vlan_var.h>
61e8c8b728SJonathan Lemon 
628fae3bd4SPyun YongHyeon #include <machine/bus.h>
638fae3bd4SPyun YongHyeon #include <machine/resource.h>
648fae3bd4SPyun YongHyeon 
65c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
66c8bca6dcSBill Paul #include <netinet/in.h>
67c8bca6dcSBill Paul #include <netinet/in_systm.h>
68c8bca6dcSBill Paul #include <netinet/ip.h>
69c8bca6dcSBill Paul #include <machine/in_cksum.h>
70c8bca6dcSBill Paul #endif
71c8bca6dcSBill Paul 
724fbd232cSWarner Losh #include <dev/pci/pcivar.h>
734fbd232cSWarner Losh #include <dev/pci/pcireg.h>		/* for PCIM_CMD_xxx */
74a17c678eSDavid Greenman 
75f7788e8eSJonathan Lemon #include <dev/mii/mii.h>
76f7788e8eSJonathan Lemon #include <dev/mii/miivar.h>
77f7788e8eSJonathan Lemon 
78f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpreg.h>
79f7788e8eSJonathan Lemon #include <dev/fxp/if_fxpvar.h>
8072a32a26SJonathan Lemon #include <dev/fxp/rcvbundl.h>
81f7788e8eSJonathan Lemon 
82f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, pci, 1, 1, 1);
83f246e4a1SMatthew N. Dodd MODULE_DEPEND(fxp, ether, 1, 1, 1);
84f7788e8eSJonathan Lemon MODULE_DEPEND(fxp, miibus, 1, 1, 1);
85f7788e8eSJonathan Lemon #include "miibus_if.h"
864fc1dda9SAndrew Gallatin 
87ba8c6fd5SDavid Greenman /*
88ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
89ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
90ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
91ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
92ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
93ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
94ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
95ba8c6fd5SDavid Greenman  */
96ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
97ba8c6fd5SDavid Greenman 
98ba8c6fd5SDavid Greenman /*
99f7788e8eSJonathan Lemon  * Set initial transmit threshold at 64 (512 bytes). This is
100f7788e8eSJonathan Lemon  * increased by 64 (512 bytes) at a time, to maximum of 192
101f7788e8eSJonathan Lemon  * (1536 bytes), if an underrun occurs.
102f7788e8eSJonathan Lemon  */
103f7788e8eSJonathan Lemon static int tx_threshold = 64;
104f7788e8eSJonathan Lemon 
105f7788e8eSJonathan Lemon /*
106f7788e8eSJonathan Lemon  * The configuration byte map has several undefined fields which
107f7788e8eSJonathan Lemon  * must be one or must be zero.  Set up a template for these bits
108f7788e8eSJonathan Lemon  * only, (assuming a 82557 chip) leaving the actual configuration
109f7788e8eSJonathan Lemon  * to fxp_init.
110f7788e8eSJonathan Lemon  *
111f7788e8eSJonathan Lemon  * See struct fxp_cb_config for the bit definitions.
112f7788e8eSJonathan Lemon  */
113f7788e8eSJonathan Lemon static u_char fxp_cb_config_template[] = {
114f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_status */
115f7788e8eSJonathan Lemon 	0x0, 0x0,		/* cb_command */
116f7788e8eSJonathan Lemon 	0x0, 0x0, 0x0, 0x0,	/* link_addr */
117f7788e8eSJonathan Lemon 	0x0,	/*  0 */
118f7788e8eSJonathan Lemon 	0x0,	/*  1 */
119f7788e8eSJonathan Lemon 	0x0,	/*  2 */
120f7788e8eSJonathan Lemon 	0x0,	/*  3 */
121f7788e8eSJonathan Lemon 	0x0,	/*  4 */
122f7788e8eSJonathan Lemon 	0x0,	/*  5 */
123f7788e8eSJonathan Lemon 	0x32,	/*  6 */
124f7788e8eSJonathan Lemon 	0x0,	/*  7 */
125f7788e8eSJonathan Lemon 	0x0,	/*  8 */
126f7788e8eSJonathan Lemon 	0x0,	/*  9 */
127f7788e8eSJonathan Lemon 	0x6,	/* 10 */
128f7788e8eSJonathan Lemon 	0x0,	/* 11 */
129f7788e8eSJonathan Lemon 	0x0,	/* 12 */
130f7788e8eSJonathan Lemon 	0x0,	/* 13 */
131f7788e8eSJonathan Lemon 	0xf2,	/* 14 */
132f7788e8eSJonathan Lemon 	0x48,	/* 15 */
133f7788e8eSJonathan Lemon 	0x0,	/* 16 */
134f7788e8eSJonathan Lemon 	0x40,	/* 17 */
135f7788e8eSJonathan Lemon 	0xf0,	/* 18 */
136f7788e8eSJonathan Lemon 	0x0,	/* 19 */
137f7788e8eSJonathan Lemon 	0x3f,	/* 20 */
138f7788e8eSJonathan Lemon 	0x5	/* 21 */
139f7788e8eSJonathan Lemon };
140f7788e8eSJonathan Lemon 
141f7788e8eSJonathan Lemon struct fxp_ident {
14274d1ed23SMaxime Henrion 	uint16_t	devid;
143f19fc5d8SJohn Polstra 	int16_t		revid;		/* -1 matches anything */
144f7788e8eSJonathan Lemon 	char 		*name;
145f7788e8eSJonathan Lemon };
146f7788e8eSJonathan Lemon 
147f7788e8eSJonathan Lemon /*
148f7788e8eSJonathan Lemon  * Claim various Intel PCI device identifiers for this driver.  The
149f7788e8eSJonathan Lemon  * sub-vendor and sub-device field are extensively used to identify
150f7788e8eSJonathan Lemon  * particular variants, but we don't currently differentiate between
151f7788e8eSJonathan Lemon  * them.
152f7788e8eSJonathan Lemon  */
153f7788e8eSJonathan Lemon static struct fxp_ident fxp_ident_table[] = {
154f19fc5d8SJohn Polstra     { 0x1029,	-1,	"Intel 82559 PCI/CardBus Pro/100" },
155f19fc5d8SJohn Polstra     { 0x1030,	-1,	"Intel 82559 Pro/100 Ethernet" },
156f19fc5d8SJohn Polstra     { 0x1031,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
157f19fc5d8SJohn Polstra     { 0x1032,	-1,	"Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
158f19fc5d8SJohn Polstra     { 0x1033,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
159f19fc5d8SJohn Polstra     { 0x1034,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
160f19fc5d8SJohn Polstra     { 0x1035,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
161f19fc5d8SJohn Polstra     { 0x1036,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
162f19fc5d8SJohn Polstra     { 0x1037,	-1,	"Intel 82801CAM (ICH3) Pro/100 Ethernet" },
163f19fc5d8SJohn Polstra     { 0x1038,	-1,	"Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
164f19fc5d8SJohn Polstra     { 0x1039,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
165f19fc5d8SJohn Polstra     { 0x103A,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
166f19fc5d8SJohn Polstra     { 0x103B,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
167f19fc5d8SJohn Polstra     { 0x103C,	-1,	"Intel 82801DB (ICH4) Pro/100 Ethernet" },
168f19fc5d8SJohn Polstra     { 0x103D,	-1,	"Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
169f19fc5d8SJohn Polstra     { 0x103E,	-1,	"Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
170f19fc5d8SJohn Polstra     { 0x1050,	-1,	"Intel 82801BA (D865) Pro/100 VE Ethernet" },
171c2b37819SWarner Losh     { 0x1051,	-1,	"Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
172f19fc5d8SJohn Polstra     { 0x1059,	-1,	"Intel 82551QM Pro/100 M Mobile Connection" },
173048ca166SMaxime Henrion     { 0x1064,	-1,	"Intel 82562EZ (ICH6)" },
17442a4336aSRink Springer     { 0x1065,	-1,	"Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
17529a8929dSMaxime Henrion     { 0x1068,	-1,	"Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
17652dfd9cdSMaxime Henrion     { 0x1069,	-1,	"Intel 82562EM/EX/GX Pro/100 Ethernet" },
177847f5310SRemko Lodder     { 0x1091,	-1,	"Intel 82562GX Pro/100 Ethernet" },
178c943ffccSMatteo Riondato     { 0x1092,	-1,	"Intel Pro/100 VE Network Connection" },
179597d4fe4SRink Springer     { 0x1093,	-1,	"Intel Pro/100 VM Network Connection" },
18042a4336aSRink Springer     { 0x1094,	-1,	"Intel Pro/100 946GZ (ICH7) Network Connection" },
181f19fc5d8SJohn Polstra     { 0x1209,	-1,	"Intel 82559ER Embedded 10/100 Ethernet" },
182f19fc5d8SJohn Polstra     { 0x1229,	0x01,	"Intel 82557 Pro/100 Ethernet" },
183f19fc5d8SJohn Polstra     { 0x1229,	0x02,	"Intel 82557 Pro/100 Ethernet" },
184f19fc5d8SJohn Polstra     { 0x1229,	0x03,	"Intel 82557 Pro/100 Ethernet" },
185f19fc5d8SJohn Polstra     { 0x1229,	0x04,	"Intel 82558 Pro/100 Ethernet" },
186f19fc5d8SJohn Polstra     { 0x1229,	0x05,	"Intel 82558 Pro/100 Ethernet" },
187f19fc5d8SJohn Polstra     { 0x1229,	0x06,	"Intel 82559 Pro/100 Ethernet" },
188f19fc5d8SJohn Polstra     { 0x1229,	0x07,	"Intel 82559 Pro/100 Ethernet" },
189f19fc5d8SJohn Polstra     { 0x1229,	0x08,	"Intel 82559 Pro/100 Ethernet" },
190f19fc5d8SJohn Polstra     { 0x1229,	0x09,	"Intel 82559ER Pro/100 Ethernet" },
191f19fc5d8SJohn Polstra     { 0x1229,	0x0c,	"Intel 82550 Pro/100 Ethernet" },
192f19fc5d8SJohn Polstra     { 0x1229,	0x0d,	"Intel 82550 Pro/100 Ethernet" },
193f19fc5d8SJohn Polstra     { 0x1229,	0x0e,	"Intel 82550 Pro/100 Ethernet" },
194f19fc5d8SJohn Polstra     { 0x1229,	0x0f,	"Intel 82551 Pro/100 Ethernet" },
195f19fc5d8SJohn Polstra     { 0x1229,	0x10,	"Intel 82551 Pro/100 Ethernet" },
196f19fc5d8SJohn Polstra     { 0x1229,	-1,	"Intel 82557/8/9 Pro/100 Ethernet" },
197f19fc5d8SJohn Polstra     { 0x2449,	-1,	"Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
19886c8aacbSMaxime Henrion     { 0x27dc,	-1,	"Intel 82801GB (ICH7) 10/100 Ethernet" },
199f19fc5d8SJohn Polstra     { 0,	-1,	NULL },
200f7788e8eSJonathan Lemon };
201f7788e8eSJonathan Lemon 
202c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
203c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
204c8bca6dcSBill Paul #else
205c8bca6dcSBill Paul #define FXP_CSUM_FEATURES    (CSUM_TCP | CSUM_UDP)
206c8bca6dcSBill Paul #endif
207c8bca6dcSBill Paul 
208f7788e8eSJonathan Lemon static int		fxp_probe(device_t dev);
209f7788e8eSJonathan Lemon static int		fxp_attach(device_t dev);
210f7788e8eSJonathan Lemon static int		fxp_detach(device_t dev);
211f7788e8eSJonathan Lemon static int		fxp_shutdown(device_t dev);
212f7788e8eSJonathan Lemon static int		fxp_suspend(device_t dev);
213f7788e8eSJonathan Lemon static int		fxp_resume(device_t dev);
214f7788e8eSJonathan Lemon 
215f7788e8eSJonathan Lemon static void		fxp_intr(void *xsc);
2164953bccaSNate Lawson static void		fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp,
21774d1ed23SMaxime Henrion 			    uint8_t statack, int count);
218f7788e8eSJonathan Lemon static void 		fxp_init(void *xsc);
2194953bccaSNate Lawson static void 		fxp_init_body(struct fxp_softc *sc);
220f7788e8eSJonathan Lemon static void 		fxp_tick(void *xsc);
221f7788e8eSJonathan Lemon static void 		fxp_start(struct ifnet *ifp);
2224953bccaSNate Lawson static void 		fxp_start_body(struct ifnet *ifp);
2234e53f837SPyun YongHyeon static int		fxp_encap(struct fxp_softc *sc, struct mbuf **m_head);
2244e53f837SPyun YongHyeon static void		fxp_txeof(struct fxp_softc *sc);
225f7788e8eSJonathan Lemon static void		fxp_stop(struct fxp_softc *sc);
226f7788e8eSJonathan Lemon static void 		fxp_release(struct fxp_softc *sc);
227f7788e8eSJonathan Lemon static int		fxp_ioctl(struct ifnet *ifp, u_long command,
228f7788e8eSJonathan Lemon 			    caddr_t data);
229df79d527SGleb Smirnoff static void 		fxp_watchdog(struct fxp_softc *sc);
23085050421SPyun YongHyeon static void		fxp_add_rfabuf(struct fxp_softc *sc,
23185050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23285050421SPyun YongHyeon static void		fxp_discard_rfabuf(struct fxp_softc *sc,
23385050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23485050421SPyun YongHyeon static int		fxp_new_rfabuf(struct fxp_softc *sc,
23585050421SPyun YongHyeon     			    struct fxp_rx *rxp);
23609882363SJonathan Lemon static int		fxp_mc_addrs(struct fxp_softc *sc);
237f7788e8eSJonathan Lemon static void		fxp_mc_setup(struct fxp_softc *sc);
23874d1ed23SMaxime Henrion static uint16_t		fxp_eeprom_getword(struct fxp_softc *sc, int offset,
239f7788e8eSJonathan Lemon 			    int autosize);
24000c4116bSJonathan Lemon static void 		fxp_eeprom_putword(struct fxp_softc *sc, int offset,
24174d1ed23SMaxime Henrion 			    uint16_t data);
242f7788e8eSJonathan Lemon static void		fxp_autosize_eeprom(struct fxp_softc *sc);
243f7788e8eSJonathan Lemon static void		fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
244f7788e8eSJonathan Lemon 			    int offset, int words);
24500c4116bSJonathan Lemon static void		fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
24600c4116bSJonathan Lemon 			    int offset, int words);
247f7788e8eSJonathan Lemon static int		fxp_ifmedia_upd(struct ifnet *ifp);
248f7788e8eSJonathan Lemon static void		fxp_ifmedia_sts(struct ifnet *ifp,
249f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
250f7788e8eSJonathan Lemon static int		fxp_serial_ifmedia_upd(struct ifnet *ifp);
251f7788e8eSJonathan Lemon static void		fxp_serial_ifmedia_sts(struct ifnet *ifp,
252f7788e8eSJonathan Lemon 			    struct ifmediareq *ifmr);
253f1928b0cSKevin Lo static int		fxp_miibus_readreg(device_t dev, int phy, int reg);
254f7788e8eSJonathan Lemon static void		fxp_miibus_writereg(device_t dev, int phy, int reg,
255f7788e8eSJonathan Lemon 			    int value);
25672a32a26SJonathan Lemon static void		fxp_load_ucode(struct fxp_softc *sc);
25772a32a26SJonathan Lemon static int		sysctl_int_range(SYSCTL_HANDLER_ARGS,
25872a32a26SJonathan Lemon 			    int low, int high);
25972a32a26SJonathan Lemon static int		sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
26072a32a26SJonathan Lemon static int		sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
26128935f27SMaxime Henrion static void 		fxp_scb_wait(struct fxp_softc *sc);
26228935f27SMaxime Henrion static void		fxp_scb_cmd(struct fxp_softc *sc, int cmd);
26328935f27SMaxime Henrion static void		fxp_dma_wait(struct fxp_softc *sc,
26474d1ed23SMaxime Henrion     			    volatile uint16_t *status, bus_dma_tag_t dmat,
265209b07bcSMaxime Henrion 			    bus_dmamap_t map);
266f7788e8eSJonathan Lemon 
267f7788e8eSJonathan Lemon static device_method_t fxp_methods[] = {
268f7788e8eSJonathan Lemon 	/* Device interface */
269f7788e8eSJonathan Lemon 	DEVMETHOD(device_probe,		fxp_probe),
270f7788e8eSJonathan Lemon 	DEVMETHOD(device_attach,	fxp_attach),
271f7788e8eSJonathan Lemon 	DEVMETHOD(device_detach,	fxp_detach),
272f7788e8eSJonathan Lemon 	DEVMETHOD(device_shutdown,	fxp_shutdown),
273f7788e8eSJonathan Lemon 	DEVMETHOD(device_suspend,	fxp_suspend),
274f7788e8eSJonathan Lemon 	DEVMETHOD(device_resume,	fxp_resume),
275f7788e8eSJonathan Lemon 
276f7788e8eSJonathan Lemon 	/* MII interface */
277f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_readreg,	fxp_miibus_readreg),
278f7788e8eSJonathan Lemon 	DEVMETHOD(miibus_writereg,	fxp_miibus_writereg),
279f7788e8eSJonathan Lemon 
280f7788e8eSJonathan Lemon 	{ 0, 0 }
281f7788e8eSJonathan Lemon };
282f7788e8eSJonathan Lemon 
283f7788e8eSJonathan Lemon static driver_t fxp_driver = {
284f7788e8eSJonathan Lemon 	"fxp",
285f7788e8eSJonathan Lemon 	fxp_methods,
286f7788e8eSJonathan Lemon 	sizeof(struct fxp_softc),
287f7788e8eSJonathan Lemon };
288f7788e8eSJonathan Lemon 
289f7788e8eSJonathan Lemon static devclass_t fxp_devclass;
290f7788e8eSJonathan Lemon 
291f246e4a1SMatthew N. Dodd DRIVER_MODULE(fxp, pci, fxp_driver, fxp_devclass, 0, 0);
292347934faSWarner Losh DRIVER_MODULE(fxp, cardbus, fxp_driver, fxp_devclass, 0, 0);
293f7788e8eSJonathan Lemon DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, 0, 0);
294f7788e8eSJonathan Lemon 
29505bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_mem[] = {
29605bd8c22SMaxime Henrion 	{ SYS_RES_MEMORY,	FXP_PCI_MMBA,	RF_ACTIVE },
29705bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
29805bd8c22SMaxime Henrion 	{ -1, 0 }
29905bd8c22SMaxime Henrion };
30005bd8c22SMaxime Henrion 
30105bd8c22SMaxime Henrion static struct resource_spec fxp_res_spec_io[] = {
30205bd8c22SMaxime Henrion 	{ SYS_RES_IOPORT,	FXP_PCI_IOBA,	RF_ACTIVE },
30305bd8c22SMaxime Henrion 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
30405bd8c22SMaxime Henrion 	{ -1, 0 }
30505bd8c22SMaxime Henrion };
30605bd8c22SMaxime Henrion 
307f7788e8eSJonathan Lemon /*
308dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
309dfe61cf1SDavid Greenman  * completed).
310dfe61cf1SDavid Greenman  */
31128935f27SMaxime Henrion static void
312f7788e8eSJonathan Lemon fxp_scb_wait(struct fxp_softc *sc)
313a17c678eSDavid Greenman {
3143cf09dd1SMarcel Moolenaar 	union {
3153cf09dd1SMarcel Moolenaar 		uint16_t w;
3163cf09dd1SMarcel Moolenaar 		uint8_t b[2];
3173cf09dd1SMarcel Moolenaar 	} flowctl;
318a17c678eSDavid Greenman 	int i = 10000;
319a17c678eSDavid Greenman 
3207dced78aSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
3217dced78aSDavid Greenman 		DELAY(2);
3223cf09dd1SMarcel Moolenaar 	if (i == 0) {
3233cf09dd1SMarcel Moolenaar 		flowctl.b[0] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL);
3243cf09dd1SMarcel Moolenaar 		flowctl.b[1] = CSR_READ_1(sc, FXP_CSR_FLOWCONTROL + 1);
32500c4116bSJonathan Lemon 		device_printf(sc->dev, "SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
326e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
327e8c8b728SJonathan Lemon 		    CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
3283cf09dd1SMarcel Moolenaar 		    CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS), flowctl.w);
3293cf09dd1SMarcel Moolenaar 	}
3307dced78aSDavid Greenman }
3317dced78aSDavid Greenman 
33228935f27SMaxime Henrion static void
3332e2b8238SJonathan Lemon fxp_scb_cmd(struct fxp_softc *sc, int cmd)
3342e2b8238SJonathan Lemon {
3352e2b8238SJonathan Lemon 
3362e2b8238SJonathan Lemon 	if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
3372e2b8238SJonathan Lemon 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
3382e2b8238SJonathan Lemon 		fxp_scb_wait(sc);
3392e2b8238SJonathan Lemon 	}
3402e2b8238SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
3412e2b8238SJonathan Lemon }
3422e2b8238SJonathan Lemon 
34328935f27SMaxime Henrion static void
34474d1ed23SMaxime Henrion fxp_dma_wait(struct fxp_softc *sc, volatile uint16_t *status,
345209b07bcSMaxime Henrion     bus_dma_tag_t dmat, bus_dmamap_t map)
3467dced78aSDavid Greenman {
3477dced78aSDavid Greenman 	int i = 10000;
3487dced78aSDavid Greenman 
349209b07bcSMaxime Henrion 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
350209b07bcSMaxime Henrion 	while (!(le16toh(*status) & FXP_CB_STATUS_C) && --i) {
3517dced78aSDavid Greenman 		DELAY(2);
352209b07bcSMaxime Henrion 		bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTREAD);
353209b07bcSMaxime Henrion 	}
3547dced78aSDavid Greenman 	if (i == 0)
355f7788e8eSJonathan Lemon 		device_printf(sc->dev, "DMA timeout\n");
356a17c678eSDavid Greenman }
357a17c678eSDavid Greenman 
358dfe61cf1SDavid Greenman /*
35928935f27SMaxime Henrion  * Return identification string if this device is ours.
360dfe61cf1SDavid Greenman  */
3616182fdbdSPeter Wemm static int
3626182fdbdSPeter Wemm fxp_probe(device_t dev)
363a17c678eSDavid Greenman {
36474d1ed23SMaxime Henrion 	uint16_t devid;
36574d1ed23SMaxime Henrion 	uint8_t revid;
366f7788e8eSJonathan Lemon 	struct fxp_ident *ident;
367f7788e8eSJonathan Lemon 
36855ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
369f7788e8eSJonathan Lemon 		devid = pci_get_device(dev);
370f19fc5d8SJohn Polstra 		revid = pci_get_revid(dev);
371f7788e8eSJonathan Lemon 		for (ident = fxp_ident_table; ident->name != NULL; ident++) {
372f19fc5d8SJohn Polstra 			if (ident->devid == devid &&
373f19fc5d8SJohn Polstra 			    (ident->revid == revid || ident->revid == -1)) {
374f7788e8eSJonathan Lemon 				device_set_desc(dev, ident->name);
375538565c4SWarner Losh 				return (BUS_PROBE_DEFAULT);
37655ce7b51SDavid Greenman 			}
377dd68ef16SPeter Wemm 		}
378f7788e8eSJonathan Lemon 	}
379f7788e8eSJonathan Lemon 	return (ENXIO);
3806182fdbdSPeter Wemm }
3816182fdbdSPeter Wemm 
382b2badf02SMaxime Henrion static void
383b2badf02SMaxime Henrion fxp_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
384b2badf02SMaxime Henrion {
38574d1ed23SMaxime Henrion 	uint32_t *addr;
386b2badf02SMaxime Henrion 
387b2badf02SMaxime Henrion 	if (error)
388b2badf02SMaxime Henrion 		return;
389b2badf02SMaxime Henrion 
390b2badf02SMaxime Henrion 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
391b2badf02SMaxime Henrion 	addr = arg;
392b2badf02SMaxime Henrion 	*addr = segs->ds_addr;
393b2badf02SMaxime Henrion }
394b2badf02SMaxime Henrion 
3956182fdbdSPeter Wemm static int
3966182fdbdSPeter Wemm fxp_attach(device_t dev)
397a17c678eSDavid Greenman {
3986720ebccSMaxime Henrion 	struct fxp_softc *sc;
3996720ebccSMaxime Henrion 	struct fxp_cb_tx *tcbp;
4006720ebccSMaxime Henrion 	struct fxp_tx *txp;
401b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
4026720ebccSMaxime Henrion 	struct ifnet *ifp;
40374d1ed23SMaxime Henrion 	uint32_t val;
40474d1ed23SMaxime Henrion 	uint16_t data, myea[ETHER_ADDR_LEN / 2];
405fc74a9f9SBrooks Davis 	u_char eaddr[ETHER_ADDR_LEN];
40605bd8c22SMaxime Henrion 	int i, prefer_iomap;
4073212724cSJohn Baldwin 	int error;
408a17c678eSDavid Greenman 
4096720ebccSMaxime Henrion 	error = 0;
4106720ebccSMaxime Henrion 	sc = device_get_softc(dev);
411f7788e8eSJonathan Lemon 	sc->dev = dev;
4126008862bSJohn Baldwin 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4134953bccaSNate Lawson 	    MTX_DEF);
4143212724cSJohn Baldwin 	callout_init_mtx(&sc->stat_ch, &sc->sc_mtx, 0);
4154953bccaSNate Lawson 	ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
4164953bccaSNate Lawson 	    fxp_serial_ifmedia_sts);
417a17c678eSDavid Greenman 
4187ba33d82SBrooks Davis 	ifp = sc->ifp = if_alloc(IFT_ETHER);
4197ba33d82SBrooks Davis 	if (ifp == NULL) {
4207ba33d82SBrooks Davis 		device_printf(dev, "can not if_alloc()\n");
4217ba33d82SBrooks Davis 		error = ENOSPC;
4227ba33d82SBrooks Davis 		goto fail;
4237ba33d82SBrooks Davis 	}
4247ba33d82SBrooks Davis 
425dfe61cf1SDavid Greenman 	/*
4262bce79a2SMaxim Sobolev 	 * Enable bus mastering.
427df373873SWes Peters 	 */
428cf0d8a1eSMaxim Sobolev 	pci_enable_busmaster(dev);
4299fa6ccfbSMatt Jacob 	val = pci_read_config(dev, PCIR_COMMAND, 2);
43079495006SWarner Losh 
431df373873SWes Peters 	/*
4329fa6ccfbSMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
4339fa6ccfbSMatt Jacob 	 * We default to memory mapping. Then we accept an override from the
4349fa6ccfbSMatt Jacob 	 * command line. Then we check to see which one is enabled.
435dfe61cf1SDavid Greenman 	 */
4362a05a4ebSMatt Jacob 	prefer_iomap = 0;
43705bd8c22SMaxime Henrion 	resource_int_value(device_get_name(dev), device_get_unit(dev),
43805bd8c22SMaxime Henrion 	    "prefer_iomap", &prefer_iomap);
43905bd8c22SMaxime Henrion 	if (prefer_iomap)
44005bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_io;
44105bd8c22SMaxime Henrion 	else
44205bd8c22SMaxime Henrion 		sc->fxp_spec = fxp_res_spec_mem;
4439fa6ccfbSMatt Jacob 
44405bd8c22SMaxime Henrion 	error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
44505bd8c22SMaxime Henrion 	if (error) {
44605bd8c22SMaxime Henrion 		if (sc->fxp_spec == fxp_res_spec_mem)
44705bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_io;
44805bd8c22SMaxime Henrion 		else
44905bd8c22SMaxime Henrion 			sc->fxp_spec = fxp_res_spec_mem;
45005bd8c22SMaxime Henrion 		error = bus_alloc_resources(dev, sc->fxp_spec, sc->fxp_res);
4519fa6ccfbSMatt Jacob 	}
45205bd8c22SMaxime Henrion 	if (error) {
45305bd8c22SMaxime Henrion 		device_printf(dev, "could not allocate resources\n");
4546182fdbdSPeter Wemm 		error = ENXIO;
455a17c678eSDavid Greenman 		goto fail;
456a17c678eSDavid Greenman 	}
45705bd8c22SMaxime Henrion 
4589fa6ccfbSMatt Jacob 	if (bootverbose) {
4599fa6ccfbSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
46005bd8c22SMaxime Henrion 		   sc->fxp_spec == fxp_res_spec_mem ? "memory" : "I/O");
4616182fdbdSPeter Wemm 	}
4626182fdbdSPeter Wemm 
463f7788e8eSJonathan Lemon 	/*
464f7788e8eSJonathan Lemon 	 * Reset to a stable state.
465f7788e8eSJonathan Lemon 	 */
466f7788e8eSJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
467f7788e8eSJonathan Lemon 	DELAY(10);
468f7788e8eSJonathan Lemon 
469f7788e8eSJonathan Lemon 	/*
470f7788e8eSJonathan Lemon 	 * Find out how large of an SEEPROM we have.
471f7788e8eSJonathan Lemon 	 */
472f7788e8eSJonathan Lemon 	fxp_autosize_eeprom(sc);
473f7788e8eSJonathan Lemon 
474f7788e8eSJonathan Lemon 	/*
47593b6e2e6SMaxime Henrion 	 * Find out the chip revision; lump all 82557 revs together.
47693b6e2e6SMaxime Henrion 	 */
47793b6e2e6SMaxime Henrion 	fxp_read_eeprom(sc, &data, 5, 1);
47893b6e2e6SMaxime Henrion 	if ((data >> 8) == 1)
47993b6e2e6SMaxime Henrion 		sc->revision = FXP_REV_82557;
48093b6e2e6SMaxime Henrion 	else
48193b6e2e6SMaxime Henrion 		sc->revision = pci_get_revid(dev);
48293b6e2e6SMaxime Henrion 
48393b6e2e6SMaxime Henrion 	/*
4843bd07cfdSJonathan Lemon 	 * Determine whether we must use the 503 serial interface.
485f7788e8eSJonathan Lemon 	 */
486f7788e8eSJonathan Lemon 	fxp_read_eeprom(sc, &data, 6, 1);
48793b6e2e6SMaxime Henrion 	if (sc->revision == FXP_REV_82557 && (data & FXP_PHY_DEVICE_MASK) != 0
4884ed53076SMaxime Henrion 	    && (data & FXP_PHY_SERIAL_ONLY))
489dedabebfSJonathan Lemon 		sc->flags |= FXP_FLAG_SERIAL_MEDIA;
490f7788e8eSJonathan Lemon 
4910f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
4920f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
49350a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW,
494858b84f5SPoul-Henning Kamp 	    &sc->tunable_int_delay, 0, sysctl_hw_fxp_int_delay, "I",
49572a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundling delay");
4960f1db1d6SMaxime Henrion 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
4970f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
49850a33b6aSPawel Jakub Dawidek 	    OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW,
499858b84f5SPoul-Henning Kamp 	    &sc->tunable_bundle_max, 0, sysctl_hw_fxp_bundle_max, "I",
50072a32a26SJonathan Lemon 	    "FXP driver receive interrupt microcode bundle size limit");
5010f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5020f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5030f1db1d6SMaxime Henrion 	    OID_AUTO, "rnr", CTLFLAG_RD, &sc->rnr, 0,
5040f1db1d6SMaxime Henrion 	    "FXP RNR events");
5050f1db1d6SMaxime Henrion 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
5060f1db1d6SMaxime Henrion 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
5070f1db1d6SMaxime Henrion 	    OID_AUTO, "noflow", CTLFLAG_RW, &sc->tunable_noflow, 0,
5080f1db1d6SMaxime Henrion 	    "FXP flow control disabled");
50972a32a26SJonathan Lemon 
51072a32a26SJonathan Lemon 	/*
51172a32a26SJonathan Lemon 	 * Pull in device tunables.
51272a32a26SJonathan Lemon 	 */
51372a32a26SJonathan Lemon 	sc->tunable_int_delay = TUNABLE_INT_DELAY;
51472a32a26SJonathan Lemon 	sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
51503edfff3SRobert Watson 	sc->tunable_noflow = 1;
51672a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
51772a32a26SJonathan Lemon 	    "int_delay", &sc->tunable_int_delay);
51872a32a26SJonathan Lemon 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
51972a32a26SJonathan Lemon 	    "bundle_max", &sc->tunable_bundle_max);
5200f1db1d6SMaxime Henrion 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
5210f1db1d6SMaxime Henrion 	    "noflow", &sc->tunable_noflow);
5220f1db1d6SMaxime Henrion 	sc->rnr = 0;
52372a32a26SJonathan Lemon 
52472a32a26SJonathan Lemon 	/*
5252e2b8238SJonathan Lemon 	 * Enable workarounds for certain chip revision deficiencies.
52600c4116bSJonathan Lemon 	 *
52772a32a26SJonathan Lemon 	 * Systems based on the ICH2/ICH2-M chip from Intel, and possibly
52872a32a26SJonathan Lemon 	 * some systems based a normal 82559 design, have a defect where
52972a32a26SJonathan Lemon 	 * the chip can cause a PCI protocol violation if it receives
53000c4116bSJonathan Lemon 	 * a CU_RESUME command when it is entering the IDLE state.  The
53100c4116bSJonathan Lemon 	 * workaround is to disable Dynamic Standby Mode, so the chip never
53200c4116bSJonathan Lemon 	 * deasserts CLKRUN#, and always remains in an active state.
53300c4116bSJonathan Lemon 	 *
53400c4116bSJonathan Lemon 	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
5352e2b8238SJonathan Lemon 	 */
5362e2b8238SJonathan Lemon 	i = pci_get_device(dev);
53772a32a26SJonathan Lemon 	if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
53872a32a26SJonathan Lemon 	    sc->revision >= FXP_REV_82559_A0) {
53900c4116bSJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
54000c4116bSJonathan Lemon 		if (data & 0x02) {			/* STB enable */
54174d1ed23SMaxime Henrion 			uint16_t cksum;
54200c4116bSJonathan Lemon 			int i;
54300c4116bSJonathan Lemon 
54400c4116bSJonathan Lemon 			device_printf(dev,
545001cfa92SJonathan Lemon 			    "Disabling dynamic standby mode in EEPROM\n");
54600c4116bSJonathan Lemon 			data &= ~0x02;
54700c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &data, 10, 1);
54800c4116bSJonathan Lemon 			device_printf(dev, "New EEPROM ID: 0x%x\n", data);
54900c4116bSJonathan Lemon 			cksum = 0;
55000c4116bSJonathan Lemon 			for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
55100c4116bSJonathan Lemon 				fxp_read_eeprom(sc, &data, i, 1);
55200c4116bSJonathan Lemon 				cksum += data;
55300c4116bSJonathan Lemon 			}
55400c4116bSJonathan Lemon 			i = (1 << sc->eeprom_size) - 1;
55500c4116bSJonathan Lemon 			cksum = 0xBABA - cksum;
55600c4116bSJonathan Lemon 			fxp_read_eeprom(sc, &data, i, 1);
55700c4116bSJonathan Lemon 			fxp_write_eeprom(sc, &cksum, i, 1);
55800c4116bSJonathan Lemon 			device_printf(dev,
55900c4116bSJonathan Lemon 			    "EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
56000c4116bSJonathan Lemon 			    i, data, cksum);
56100c4116bSJonathan Lemon #if 1
56200c4116bSJonathan Lemon 			/*
56300c4116bSJonathan Lemon 			 * If the user elects to continue, try the software
56400c4116bSJonathan Lemon 			 * workaround, as it is better than nothing.
56500c4116bSJonathan Lemon 			 */
5662e2b8238SJonathan Lemon 			sc->flags |= FXP_FLAG_CU_RESUME_BUG;
56700c4116bSJonathan Lemon #endif
56800c4116bSJonathan Lemon 		}
56900c4116bSJonathan Lemon 	}
5702e2b8238SJonathan Lemon 
5712e2b8238SJonathan Lemon 	/*
5723bd07cfdSJonathan Lemon 	 * If we are not a 82557 chip, we can enable extended features.
5733bd07cfdSJonathan Lemon 	 */
57472a32a26SJonathan Lemon 	if (sc->revision != FXP_REV_82557) {
5753bd07cfdSJonathan Lemon 		/*
57674396a0aSJonathan Lemon 		 * If MWI is enabled in the PCI configuration, and there
57774396a0aSJonathan Lemon 		 * is a valid cacheline size (8 or 16 dwords), then tell
57874396a0aSJonathan Lemon 		 * the board to turn on MWI.
5793bd07cfdSJonathan Lemon 		 */
58074396a0aSJonathan Lemon 		if (val & PCIM_CMD_MWRICEN &&
58174396a0aSJonathan Lemon 		    pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
5823bd07cfdSJonathan Lemon 			sc->flags |= FXP_FLAG_MWI_ENABLE;
5833bd07cfdSJonathan Lemon 
5843bd07cfdSJonathan Lemon 		/* turn on the extended TxCB feature */
5853bd07cfdSJonathan Lemon 		sc->flags |= FXP_FLAG_EXT_TXCB;
58644e0bc11SYaroslav Tykhiy 
58744e0bc11SYaroslav Tykhiy 		/* enable reception of long frames for VLAN */
58844e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_LONG_PKT_EN;
58944e0bc11SYaroslav Tykhiy 	} else {
59044e0bc11SYaroslav Tykhiy 		/* a hack to get long VLAN frames on a 82557 */
59144e0bc11SYaroslav Tykhiy 		sc->flags |= FXP_FLAG_SAVE_BAD;
5923bd07cfdSJonathan Lemon 	}
5933bd07cfdSJonathan Lemon 
5943bd07cfdSJonathan Lemon 	/*
595c8bca6dcSBill Paul 	 * Enable use of extended RFDs and TCBs for 82550
596c8bca6dcSBill Paul 	 * and later chips. Note: we need extended TXCB support
597c8bca6dcSBill Paul 	 * too, but that's already enabled by the code above.
598c8bca6dcSBill Paul 	 * Be careful to do this only on the right devices.
599c8bca6dcSBill Paul 	 */
600507feeafSMaxime Henrion 	if (sc->revision == FXP_REV_82550 || sc->revision == FXP_REV_82550_C ||
601507feeafSMaxime Henrion 	    sc->revision == FXP_REV_82551_E || sc->revision == FXP_REV_82551_F
602507feeafSMaxime Henrion 	    || sc->revision == FXP_REV_82551_10) {
603c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa);
604c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_IPCBXMIT;
605c8bca6dcSBill Paul 		sc->flags |= FXP_FLAG_EXT_RFA;
606c8bca6dcSBill Paul 	} else {
607c8bca6dcSBill Paul 		sc->rfa_size = sizeof (struct fxp_rfa) - FXP_RFAX_LEN;
608c8bca6dcSBill Paul 		sc->tx_cmd = FXP_CB_COMMAND_XMIT;
609c8bca6dcSBill Paul 	}
610c8bca6dcSBill Paul 
611c8bca6dcSBill Paul 	/*
612b2badf02SMaxime Henrion 	 * Allocate DMA tags and DMA safe memory.
613b2badf02SMaxime Henrion 	 */
61440c20505SMaxime Henrion 	sc->maxtxseg = FXP_NTXSEG;
61540c20505SMaxime Henrion 	if (sc->flags & FXP_FLAG_EXT_RFA)
61640c20505SMaxime Henrion 		sc->maxtxseg--;
617c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
618c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
619c2175ff5SMarius Strobl 	    MCLBYTES * sc->maxtxseg, sc->maxtxseg, MCLBYTES, 0,
620c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_mtag);
621b2badf02SMaxime Henrion 	if (error) {
622b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
623b2badf02SMaxime Henrion 		goto fail;
624b2badf02SMaxime Henrion 	}
625b2badf02SMaxime Henrion 
626c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
627c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
628c2175ff5SMarius Strobl 	    sizeof(struct fxp_stats), 1, sizeof(struct fxp_stats), 0,
629c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->fxp_stag);
630b2badf02SMaxime Henrion 	if (error) {
631b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
632b2badf02SMaxime Henrion 		goto fail;
633b2badf02SMaxime Henrion 	}
634b2badf02SMaxime Henrion 
635b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->fxp_stag, (void **)&sc->fxp_stats,
636aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->fxp_smap);
637b2badf02SMaxime Henrion 	if (error)
6384953bccaSNate Lawson 		goto fail;
639b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_stag, sc->fxp_smap, sc->fxp_stats,
640b2badf02SMaxime Henrion 	    sizeof(struct fxp_stats), fxp_dma_map_addr, &sc->stats_addr, 0);
641b2badf02SMaxime Henrion 	if (error) {
642b2badf02SMaxime Henrion 		device_printf(dev, "could not map the stats buffer\n");
643b2badf02SMaxime Henrion 		goto fail;
644b2badf02SMaxime Henrion 	}
645b2badf02SMaxime Henrion 
646c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
647c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
648c2175ff5SMarius Strobl 	    FXP_TXCB_SZ, 1, FXP_TXCB_SZ, 0,
649c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->cbl_tag);
650b2badf02SMaxime Henrion 	if (error) {
651b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
652b2badf02SMaxime Henrion 		goto fail;
653b2badf02SMaxime Henrion 	}
654b2badf02SMaxime Henrion 
655b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->cbl_tag, (void **)&sc->fxp_desc.cbl_list,
656aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->cbl_map);
657b2badf02SMaxime Henrion 	if (error)
6584953bccaSNate Lawson 		goto fail;
659b2badf02SMaxime Henrion 
660b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->cbl_tag, sc->cbl_map,
661b2badf02SMaxime Henrion 	    sc->fxp_desc.cbl_list, FXP_TXCB_SZ, fxp_dma_map_addr,
662b2badf02SMaxime Henrion 	    &sc->fxp_desc.cbl_addr, 0);
663b2badf02SMaxime Henrion 	if (error) {
664b2badf02SMaxime Henrion 		device_printf(dev, "could not map DMA memory\n");
665b2badf02SMaxime Henrion 		goto fail;
666b2badf02SMaxime Henrion 	}
667b2badf02SMaxime Henrion 
668c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
669c2175ff5SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
670c2175ff5SMarius Strobl 	    sizeof(struct fxp_cb_mcs), 1, sizeof(struct fxp_cb_mcs), 0,
671c2175ff5SMarius Strobl 	    busdma_lock_mutex, &Giant, &sc->mcs_tag);
672b2badf02SMaxime Henrion 	if (error) {
673b2badf02SMaxime Henrion 		device_printf(dev, "could not allocate dma tag\n");
674b2badf02SMaxime Henrion 		goto fail;
675b2badf02SMaxime Henrion 	}
676b2badf02SMaxime Henrion 
677b2badf02SMaxime Henrion 	error = bus_dmamem_alloc(sc->mcs_tag, (void **)&sc->mcsp,
678b2badf02SMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->mcs_map);
679b2badf02SMaxime Henrion 	if (error)
6804953bccaSNate Lawson 		goto fail;
681b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->mcs_tag, sc->mcs_map, sc->mcsp,
682b2badf02SMaxime Henrion 	    sizeof(struct fxp_cb_mcs), fxp_dma_map_addr, &sc->mcs_addr, 0);
683b2badf02SMaxime Henrion 	if (error) {
684b2badf02SMaxime Henrion 		device_printf(dev, "can't map the multicast setup command\n");
685b2badf02SMaxime Henrion 		goto fail;
686b2badf02SMaxime Henrion 	}
687b2badf02SMaxime Henrion 
688b2badf02SMaxime Henrion 	/*
6896720ebccSMaxime Henrion 	 * Pre-allocate the TX DMA maps and setup the pointers to
6906720ebccSMaxime Henrion 	 * the TX command blocks.
691b2badf02SMaxime Henrion 	 */
6926720ebccSMaxime Henrion 	txp = sc->fxp_desc.tx_list;
6936720ebccSMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
6944cec1653SMaxime Henrion 	for (i = 0; i < FXP_NTXCB; i++) {
6956720ebccSMaxime Henrion 		txp[i].tx_cb = tcbp + i;
6966720ebccSMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &txp[i].tx_map);
697b2badf02SMaxime Henrion 		if (error) {
698b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for TX\n");
699b2badf02SMaxime Henrion 			goto fail;
700b2badf02SMaxime Henrion 		}
701b2badf02SMaxime Henrion 	}
702b2badf02SMaxime Henrion 	error = bus_dmamap_create(sc->fxp_mtag, 0, &sc->spare_map);
703b2badf02SMaxime Henrion 	if (error) {
704b2badf02SMaxime Henrion 		device_printf(dev, "can't create spare DMA map\n");
705b2badf02SMaxime Henrion 		goto fail;
706b2badf02SMaxime Henrion 	}
707b2badf02SMaxime Henrion 
708b2badf02SMaxime Henrion 	/*
709b2badf02SMaxime Henrion 	 * Pre-allocate our receive buffers.
710b2badf02SMaxime Henrion 	 */
711b2badf02SMaxime Henrion 	sc->fxp_desc.rx_head = sc->fxp_desc.rx_tail = NULL;
712b2badf02SMaxime Henrion 	for (i = 0; i < FXP_NRFABUFS; i++) {
713b2badf02SMaxime Henrion 		rxp = &sc->fxp_desc.rx_list[i];
714b2badf02SMaxime Henrion 		error = bus_dmamap_create(sc->fxp_mtag, 0, &rxp->rx_map);
715b2badf02SMaxime Henrion 		if (error) {
716b2badf02SMaxime Henrion 			device_printf(dev, "can't create DMA map for RX\n");
717b2badf02SMaxime Henrion 			goto fail;
718b2badf02SMaxime Henrion 		}
71985050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) != 0) {
7204953bccaSNate Lawson 			error = ENOMEM;
7214953bccaSNate Lawson 			goto fail;
7224953bccaSNate Lawson 		}
72385050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
724b2badf02SMaxime Henrion 	}
725b2badf02SMaxime Henrion 
726b2badf02SMaxime Henrion 	/*
727f7788e8eSJonathan Lemon 	 * Read MAC address.
728f7788e8eSJonathan Lemon 	 */
72983e6547dSMaxime Henrion 	fxp_read_eeprom(sc, myea, 0, 3);
730fc74a9f9SBrooks Davis 	eaddr[0] = myea[0] & 0xff;
731fc74a9f9SBrooks Davis 	eaddr[1] = myea[0] >> 8;
732fc74a9f9SBrooks Davis 	eaddr[2] = myea[1] & 0xff;
733fc74a9f9SBrooks Davis 	eaddr[3] = myea[1] >> 8;
734fc74a9f9SBrooks Davis 	eaddr[4] = myea[2] & 0xff;
735fc74a9f9SBrooks Davis 	eaddr[5] = myea[2] >> 8;
736f7788e8eSJonathan Lemon 	if (bootverbose) {
7372e2b8238SJonathan Lemon 		device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
738f7788e8eSJonathan Lemon 		    pci_get_vendor(dev), pci_get_device(dev),
7392e2b8238SJonathan Lemon 		    pci_get_subvendor(dev), pci_get_subdevice(dev),
7402e2b8238SJonathan Lemon 		    pci_get_revid(dev));
74172a32a26SJonathan Lemon 		fxp_read_eeprom(sc, &data, 10, 1);
74272a32a26SJonathan Lemon 		device_printf(dev, "Dynamic Standby mode is %s\n",
74372a32a26SJonathan Lemon 		    data & 0x02 ? "enabled" : "disabled");
744f7788e8eSJonathan Lemon 	}
745f7788e8eSJonathan Lemon 
746f7788e8eSJonathan Lemon 	/*
747f7788e8eSJonathan Lemon 	 * If this is only a 10Mbps device, then there is no MII, and
748f7788e8eSJonathan Lemon 	 * the PHY will use a serial interface instead.
749f7788e8eSJonathan Lemon 	 *
750f7788e8eSJonathan Lemon 	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
751f7788e8eSJonathan Lemon 	 * doesn't have a programming interface of any sort.  The
752f7788e8eSJonathan Lemon 	 * media is sensed automatically based on how the link partner
753f7788e8eSJonathan Lemon 	 * is configured.  This is, in essence, manual configuration.
754f7788e8eSJonathan Lemon 	 */
755f7788e8eSJonathan Lemon 	if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
756f7788e8eSJonathan Lemon 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
757f7788e8eSJonathan Lemon 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
758f7788e8eSJonathan Lemon 	} else {
759f7788e8eSJonathan Lemon 		if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
760f7788e8eSJonathan Lemon 		    fxp_ifmedia_sts)) {
761f7788e8eSJonathan Lemon 	                device_printf(dev, "MII without any PHY!\n");
7626182fdbdSPeter Wemm 			error = ENXIO;
763ba8c6fd5SDavid Greenman 			goto fail;
764a17c678eSDavid Greenman 		}
765f7788e8eSJonathan Lemon 	}
766dccee1a1SDavid Greenman 
7679bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
768fb583156SDavid Greenman 	ifp->if_init = fxp_init;
769ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
770ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
771ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
772ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
773a17c678eSDavid Greenman 
7745fe9116bSYaroslav Tykhiy 	ifp->if_capabilities = ifp->if_capenable = 0;
7755fe9116bSYaroslav Tykhiy 
776c8bca6dcSBill Paul 	/* Enable checksum offload for 82550 or better chips */
777c8bca6dcSBill Paul 	if (sc->flags & FXP_FLAG_EXT_RFA) {
778c8bca6dcSBill Paul 		ifp->if_hwassist = FXP_CSUM_FEATURES;
7795fe9116bSYaroslav Tykhiy 		ifp->if_capabilities |= IFCAP_HWCSUM;
7805fe9116bSYaroslav Tykhiy 		ifp->if_capenable |= IFCAP_HWCSUM;
781c8bca6dcSBill Paul 	}
782c8bca6dcSBill Paul 
783fb917226SRuslan Ermilov #ifdef DEVICE_POLLING
784fb917226SRuslan Ermilov 	/* Inform the world we support polling. */
785fb917226SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
786fb917226SRuslan Ermilov #endif
787fb917226SRuslan Ermilov 
788dfe61cf1SDavid Greenman 	/*
7894953bccaSNate Lawson 	 * Attach the interface.
7904953bccaSNate Lawson 	 */
791fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
7924953bccaSNate Lawson 
7934953bccaSNate Lawson 	/*
794e8c8b728SJonathan Lemon 	 * Tell the upper layer(s) we support long frames.
7955fe9116bSYaroslav Tykhiy 	 * Must appear after the call to ether_ifattach() because
7965fe9116bSYaroslav Tykhiy 	 * ether_ifattach() sets ifi_hdrlen to the default value.
797e8c8b728SJonathan Lemon 	 */
798e8c8b728SJonathan Lemon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
799673d9191SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
80044e0bc11SYaroslav Tykhiy 	ifp->if_capenable |= IFCAP_VLAN_MTU; /* the hw bits already set */
801e8c8b728SJonathan Lemon 
802483b9871SDavid Greenman 	/*
8033114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
8043114fdb4SDavid Greenman 	 * TX descriptors.
805483b9871SDavid Greenman 	 */
8067929aa03SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, FXP_NTXCB - 1);
8077929aa03SMax Laier 	ifp->if_snd.ifq_drv_maxlen = FXP_NTXCB - 1;
8087929aa03SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
8094a684684SDavid Greenman 
810201afb0eSMaxime Henrion 	/*
8114953bccaSNate Lawson 	 * Hook our interrupt after all initialization is complete.
812201afb0eSMaxime Henrion 	 */
81305bd8c22SMaxime Henrion 	error = bus_setup_intr(dev, sc->fxp_res[1], INTR_TYPE_NET | INTR_MPSAFE,
814ef544f63SPaolo Pisati 			       NULL, fxp_intr, sc, &sc->ih);
815201afb0eSMaxime Henrion 	if (error) {
816201afb0eSMaxime Henrion 		device_printf(dev, "could not setup irq\n");
817fc74a9f9SBrooks Davis 		ether_ifdetach(sc->ifp);
818201afb0eSMaxime Henrion 		goto fail;
819201afb0eSMaxime Henrion 	}
820201afb0eSMaxime Henrion 
821a17c678eSDavid Greenman fail:
8221b5a39d3SBrooks Davis 	if (error)
823f7788e8eSJonathan Lemon 		fxp_release(sc);
824f7788e8eSJonathan Lemon 	return (error);
825f7788e8eSJonathan Lemon }
826f7788e8eSJonathan Lemon 
827f7788e8eSJonathan Lemon /*
8284953bccaSNate Lawson  * Release all resources.  The softc lock should not be held and the
8294953bccaSNate Lawson  * interrupt should already be torn down.
830f7788e8eSJonathan Lemon  */
831f7788e8eSJonathan Lemon static void
832f7788e8eSJonathan Lemon fxp_release(struct fxp_softc *sc)
833f7788e8eSJonathan Lemon {
834b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
835b2badf02SMaxime Henrion 	struct fxp_tx *txp;
836b2badf02SMaxime Henrion 	int i;
837b2badf02SMaxime Henrion 
83867fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_NOTOWNED);
839670f5d73SMaxime Henrion 	KASSERT(sc->ih == NULL,
840670f5d73SMaxime Henrion 	    ("fxp_release() called with intr handle still active"));
8414953bccaSNate Lawson 	if (sc->miibus)
8424953bccaSNate Lawson 		device_delete_child(sc->dev, sc->miibus);
8434953bccaSNate Lawson 	bus_generic_detach(sc->dev);
8444953bccaSNate Lawson 	ifmedia_removeall(&sc->sc_media);
845b2badf02SMaxime Henrion 	if (sc->fxp_desc.cbl_list) {
846b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->cbl_tag, sc->cbl_map);
847b2badf02SMaxime Henrion 		bus_dmamem_free(sc->cbl_tag, sc->fxp_desc.cbl_list,
848b2badf02SMaxime Henrion 		    sc->cbl_map);
849b2badf02SMaxime Henrion 	}
850b2badf02SMaxime Henrion 	if (sc->fxp_stats) {
851b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->fxp_stag, sc->fxp_smap);
852b2badf02SMaxime Henrion 		bus_dmamem_free(sc->fxp_stag, sc->fxp_stats, sc->fxp_smap);
853b2badf02SMaxime Henrion 	}
854b2badf02SMaxime Henrion 	if (sc->mcsp) {
855b2badf02SMaxime Henrion 		bus_dmamap_unload(sc->mcs_tag, sc->mcs_map);
856b2badf02SMaxime Henrion 		bus_dmamem_free(sc->mcs_tag, sc->mcsp, sc->mcs_map);
857b2badf02SMaxime Henrion 	}
85805bd8c22SMaxime Henrion 	bus_release_resources(sc->dev, sc->fxp_spec, sc->fxp_res);
859b983c7b3SMaxime Henrion 	if (sc->fxp_mtag) {
860b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NRFABUFS; i++) {
861b983c7b3SMaxime Henrion 			rxp = &sc->fxp_desc.rx_list[i];
862b983c7b3SMaxime Henrion 			if (rxp->rx_mbuf != NULL) {
863b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
864b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTREAD);
865b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
866b983c7b3SMaxime Henrion 				m_freem(rxp->rx_mbuf);
867b983c7b3SMaxime Henrion 			}
868b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map);
869b983c7b3SMaxime Henrion 		}
870b983c7b3SMaxime Henrion 		bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map);
871b983c7b3SMaxime Henrion 		for (i = 0; i < FXP_NTXCB; i++) {
872b983c7b3SMaxime Henrion 			txp = &sc->fxp_desc.tx_list[i];
873b983c7b3SMaxime Henrion 			if (txp->tx_mbuf != NULL) {
874b983c7b3SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
875b983c7b3SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
876b983c7b3SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
877b983c7b3SMaxime Henrion 				m_freem(txp->tx_mbuf);
878b983c7b3SMaxime Henrion 			}
879b983c7b3SMaxime Henrion 			bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map);
880b983c7b3SMaxime Henrion 		}
881c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_mtag);
882b983c7b3SMaxime Henrion 	}
883c4bf1e90SMaxime Henrion 	if (sc->fxp_stag)
884c4bf1e90SMaxime Henrion 		bus_dma_tag_destroy(sc->fxp_stag);
885b2badf02SMaxime Henrion 	if (sc->cbl_tag)
886b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->cbl_tag);
887b2badf02SMaxime Henrion 	if (sc->mcs_tag)
888b2badf02SMaxime Henrion 		bus_dma_tag_destroy(sc->mcs_tag);
889fc74a9f9SBrooks Davis 	if (sc->ifp)
890fc74a9f9SBrooks Davis 		if_free(sc->ifp);
89172a32a26SJonathan Lemon 
8920f4dc94cSChuck Paterson 	mtx_destroy(&sc->sc_mtx);
8936182fdbdSPeter Wemm }
8946182fdbdSPeter Wemm 
8956182fdbdSPeter Wemm /*
8966182fdbdSPeter Wemm  * Detach interface.
8976182fdbdSPeter Wemm  */
8986182fdbdSPeter Wemm static int
8996182fdbdSPeter Wemm fxp_detach(device_t dev)
9006182fdbdSPeter Wemm {
9016182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
9026182fdbdSPeter Wemm 
90340929967SGleb Smirnoff #ifdef DEVICE_POLLING
90440929967SGleb Smirnoff 	if (sc->ifp->if_capenable & IFCAP_POLLING)
90540929967SGleb Smirnoff 		ether_poll_deregister(sc->ifp);
90640929967SGleb Smirnoff #endif
90740929967SGleb Smirnoff 
9084953bccaSNate Lawson 	FXP_LOCK(sc);
9091d2945d5SWarner Losh 	sc->suspended = 1;	/* Do same thing as we do for suspend */
9106182fdbdSPeter Wemm 	/*
91132cd7a9cSWarner Losh 	 * Stop DMA and drop transmit queue, but disable interrupts first.
91220f0c80fSMaxime Henrion 	 */
91320f0c80fSMaxime Henrion 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
91420f0c80fSMaxime Henrion 	fxp_stop(sc);
91532cd7a9cSWarner Losh 	FXP_UNLOCK(sc);
9169eda9d7aSJohn Baldwin 	callout_drain(&sc->stat_ch);
91720f0c80fSMaxime Henrion 
9186182fdbdSPeter Wemm 	/*
9193212724cSJohn Baldwin 	 * Close down routes etc.
9203212724cSJohn Baldwin 	 */
9213212724cSJohn Baldwin 	ether_ifdetach(sc->ifp);
9223212724cSJohn Baldwin 
9233212724cSJohn Baldwin 	/*
9244953bccaSNate Lawson 	 * Unhook interrupt before dropping lock. This is to prevent
9254953bccaSNate Lawson 	 * races with fxp_intr().
9266182fdbdSPeter Wemm 	 */
92705bd8c22SMaxime Henrion 	bus_teardown_intr(sc->dev, sc->fxp_res[1], sc->ih);
9284953bccaSNate Lawson 	sc->ih = NULL;
9296182fdbdSPeter Wemm 
930f7788e8eSJonathan Lemon 	/* Release our allocated resources. */
931f7788e8eSJonathan Lemon 	fxp_release(sc);
932f7788e8eSJonathan Lemon 	return (0);
933a17c678eSDavid Greenman }
934a17c678eSDavid Greenman 
935a17c678eSDavid Greenman /*
9364a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
937a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
938a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
939a17c678eSDavid Greenman  */
9406182fdbdSPeter Wemm static int
9416182fdbdSPeter Wemm fxp_shutdown(device_t dev)
942a17c678eSDavid Greenman {
9433212724cSJohn Baldwin 	struct fxp_softc *sc = device_get_softc(dev);
9443212724cSJohn Baldwin 
9456182fdbdSPeter Wemm 	/*
9466182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
9476182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
9486182fdbdSPeter Wemm 	 * reboot before the driver initializes.
9496182fdbdSPeter Wemm 	 */
9503212724cSJohn Baldwin 	FXP_LOCK(sc);
9513212724cSJohn Baldwin 	fxp_stop(sc);
9523212724cSJohn Baldwin 	FXP_UNLOCK(sc);
953f7788e8eSJonathan Lemon 	return (0);
954a17c678eSDavid Greenman }
955a17c678eSDavid Greenman 
9567dced78aSDavid Greenman /*
9577dced78aSDavid Greenman  * Device suspend routine.  Stop the interface and save some PCI
9587dced78aSDavid Greenman  * settings in case the BIOS doesn't restore them properly on
9597dced78aSDavid Greenman  * resume.
9607dced78aSDavid Greenman  */
9617dced78aSDavid Greenman static int
9627dced78aSDavid Greenman fxp_suspend(device_t dev)
9637dced78aSDavid Greenman {
9647dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
9657dced78aSDavid Greenman 
9664953bccaSNate Lawson 	FXP_LOCK(sc);
9677dced78aSDavid Greenman 
9687dced78aSDavid Greenman 	fxp_stop(sc);
9697dced78aSDavid Greenman 
9707dced78aSDavid Greenman 	sc->suspended = 1;
9717dced78aSDavid Greenman 
9724953bccaSNate Lawson 	FXP_UNLOCK(sc);
973f7788e8eSJonathan Lemon 	return (0);
9747dced78aSDavid Greenman }
9757dced78aSDavid Greenman 
9767dced78aSDavid Greenman /*
97767ba6566SWarner Losh  * Device resume routine. re-enable busmastering, and restart the interface if
9787dced78aSDavid Greenman  * appropriate.
9797dced78aSDavid Greenman  */
9807dced78aSDavid Greenman static int
9817dced78aSDavid Greenman fxp_resume(device_t dev)
9827dced78aSDavid Greenman {
9837dced78aSDavid Greenman 	struct fxp_softc *sc = device_get_softc(dev);
984fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
9857dced78aSDavid Greenman 
9864953bccaSNate Lawson 	FXP_LOCK(sc);
9877dced78aSDavid Greenman 
9887dced78aSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
9897dced78aSDavid Greenman 	DELAY(10);
9907dced78aSDavid Greenman 
9917dced78aSDavid Greenman 	/* reinitialize interface if necessary */
9927dced78aSDavid Greenman 	if (ifp->if_flags & IFF_UP)
9934953bccaSNate Lawson 		fxp_init_body(sc);
9947dced78aSDavid Greenman 
9957dced78aSDavid Greenman 	sc->suspended = 0;
9967dced78aSDavid Greenman 
9974953bccaSNate Lawson 	FXP_UNLOCK(sc);
998ba8c6fd5SDavid Greenman 	return (0);
999f7788e8eSJonathan Lemon }
1000ba8c6fd5SDavid Greenman 
100100c4116bSJonathan Lemon static void
100200c4116bSJonathan Lemon fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
100300c4116bSJonathan Lemon {
100474d1ed23SMaxime Henrion 	uint16_t reg;
100500c4116bSJonathan Lemon 	int x;
100600c4116bSJonathan Lemon 
100700c4116bSJonathan Lemon 	/*
100800c4116bSJonathan Lemon 	 * Shift in data.
100900c4116bSJonathan Lemon 	 */
101000c4116bSJonathan Lemon 	for (x = 1 << (length - 1); x; x >>= 1) {
101100c4116bSJonathan Lemon 		if (data & x)
101200c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
101300c4116bSJonathan Lemon 		else
101400c4116bSJonathan Lemon 			reg = FXP_EEPROM_EECS;
101500c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
101600c4116bSJonathan Lemon 		DELAY(1);
101700c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
101800c4116bSJonathan Lemon 		DELAY(1);
101900c4116bSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
102000c4116bSJonathan Lemon 		DELAY(1);
102100c4116bSJonathan Lemon 	}
102200c4116bSJonathan Lemon }
102300c4116bSJonathan Lemon 
1024f7788e8eSJonathan Lemon /*
1025f7788e8eSJonathan Lemon  * Read from the serial EEPROM. Basically, you manually shift in
1026f7788e8eSJonathan Lemon  * the read opcode (one bit at a time) and then shift in the address,
1027f7788e8eSJonathan Lemon  * and then you shift out the data (all of this one bit at a time).
1028f7788e8eSJonathan Lemon  * The word size is 16 bits, so you have to provide the address for
1029f7788e8eSJonathan Lemon  * every 16 bits of data.
1030f7788e8eSJonathan Lemon  */
103174d1ed23SMaxime Henrion static uint16_t
1032f7788e8eSJonathan Lemon fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
1033f7788e8eSJonathan Lemon {
103474d1ed23SMaxime Henrion 	uint16_t reg, data;
1035f7788e8eSJonathan Lemon 	int x;
1036ba8c6fd5SDavid Greenman 
1037f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
1038f7788e8eSJonathan Lemon 	/*
1039f7788e8eSJonathan Lemon 	 * Shift in read opcode.
1040f7788e8eSJonathan Lemon 	 */
104100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
1042f7788e8eSJonathan Lemon 	/*
1043f7788e8eSJonathan Lemon 	 * Shift in address.
1044f7788e8eSJonathan Lemon 	 */
1045f7788e8eSJonathan Lemon 	data = 0;
1046f7788e8eSJonathan Lemon 	for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
1047f7788e8eSJonathan Lemon 		if (offset & x)
1048f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
1049f7788e8eSJonathan Lemon 		else
1050f7788e8eSJonathan Lemon 			reg = FXP_EEPROM_EECS;
1051f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1052f7788e8eSJonathan Lemon 		DELAY(1);
1053f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1054f7788e8eSJonathan Lemon 		DELAY(1);
1055f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1056f7788e8eSJonathan Lemon 		DELAY(1);
1057f7788e8eSJonathan Lemon 		reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
1058f7788e8eSJonathan Lemon 		data++;
1059f7788e8eSJonathan Lemon 		if (autosize && reg == 0) {
1060f7788e8eSJonathan Lemon 			sc->eeprom_size = data;
1061f7788e8eSJonathan Lemon 			break;
1062f7788e8eSJonathan Lemon 		}
1063f7788e8eSJonathan Lemon 	}
1064f7788e8eSJonathan Lemon 	/*
1065f7788e8eSJonathan Lemon 	 * Shift out data.
1066f7788e8eSJonathan Lemon 	 */
1067f7788e8eSJonathan Lemon 	data = 0;
1068f7788e8eSJonathan Lemon 	reg = FXP_EEPROM_EECS;
1069f7788e8eSJonathan Lemon 	for (x = 1 << 15; x; x >>= 1) {
1070f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
1071f7788e8eSJonathan Lemon 		DELAY(1);
1072f7788e8eSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
1073f7788e8eSJonathan Lemon 			data |= x;
1074f7788e8eSJonathan Lemon 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
1075f7788e8eSJonathan Lemon 		DELAY(1);
1076f7788e8eSJonathan Lemon 	}
1077f7788e8eSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
1078f7788e8eSJonathan Lemon 	DELAY(1);
1079f7788e8eSJonathan Lemon 
1080f7788e8eSJonathan Lemon 	return (data);
1081ba8c6fd5SDavid Greenman }
1082ba8c6fd5SDavid Greenman 
108300c4116bSJonathan Lemon static void
108474d1ed23SMaxime Henrion fxp_eeprom_putword(struct fxp_softc *sc, int offset, uint16_t data)
108500c4116bSJonathan Lemon {
108600c4116bSJonathan Lemon 	int i;
108700c4116bSJonathan Lemon 
108800c4116bSJonathan Lemon 	/*
108900c4116bSJonathan Lemon 	 * Erase/write enable.
109000c4116bSJonathan Lemon 	 */
109100c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
109200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
109300c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
109400c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
109500c4116bSJonathan Lemon 	DELAY(1);
109600c4116bSJonathan Lemon 	/*
109700c4116bSJonathan Lemon 	 * Shift in write opcode, address, data.
109800c4116bSJonathan Lemon 	 */
109900c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
110000c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
110100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
110200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, data, 16);
110300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
110400c4116bSJonathan Lemon 	DELAY(1);
110500c4116bSJonathan Lemon 	/*
110600c4116bSJonathan Lemon 	 * Wait for EEPROM to finish up.
110700c4116bSJonathan Lemon 	 */
110800c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
110900c4116bSJonathan Lemon 	DELAY(1);
111000c4116bSJonathan Lemon 	for (i = 0; i < 1000; i++) {
111100c4116bSJonathan Lemon 		if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
111200c4116bSJonathan Lemon 			break;
111300c4116bSJonathan Lemon 		DELAY(50);
111400c4116bSJonathan Lemon 	}
111500c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
111600c4116bSJonathan Lemon 	DELAY(1);
111700c4116bSJonathan Lemon 	/*
111800c4116bSJonathan Lemon 	 * Erase/write disable.
111900c4116bSJonathan Lemon 	 */
112000c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
112100c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0x4, 3);
112200c4116bSJonathan Lemon 	fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
112300c4116bSJonathan Lemon 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
112400c4116bSJonathan Lemon 	DELAY(1);
112500c4116bSJonathan Lemon }
112600c4116bSJonathan Lemon 
1127ba8c6fd5SDavid Greenman /*
1128e9bf2fa7SDavid Greenman  * From NetBSD:
1129e9bf2fa7SDavid Greenman  *
1130e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
1131e9bf2fa7SDavid Greenman  *
1132e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
1133e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
1134e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
1135e9bf2fa7SDavid Greenman  *
1136e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
1137e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
1138e9bf2fa7SDavid Greenman  *
1139e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
1140e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
1141e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
1142e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
1143e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
1144e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
1145e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
1146e9bf2fa7SDavid Greenman  */
1147e9bf2fa7SDavid Greenman static void
1148f7788e8eSJonathan Lemon fxp_autosize_eeprom(struct fxp_softc *sc)
1149e9bf2fa7SDavid Greenman {
1150e9bf2fa7SDavid Greenman 
1151f7788e8eSJonathan Lemon 	/* guess maximum size of 256 words */
1152f7788e8eSJonathan Lemon 	sc->eeprom_size = 8;
1153f7788e8eSJonathan Lemon 
1154f7788e8eSJonathan Lemon 	/* autosize */
1155f7788e8eSJonathan Lemon 	(void) fxp_eeprom_getword(sc, 0, 1);
1156e9bf2fa7SDavid Greenman }
1157f7788e8eSJonathan Lemon 
1158ba8c6fd5SDavid Greenman static void
1159f7788e8eSJonathan Lemon fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
1160ba8c6fd5SDavid Greenman {
1161f7788e8eSJonathan Lemon 	int i;
1162ba8c6fd5SDavid Greenman 
1163f7788e8eSJonathan Lemon 	for (i = 0; i < words; i++)
1164f7788e8eSJonathan Lemon 		data[i] = fxp_eeprom_getword(sc, offset + i, 0);
1165ba8c6fd5SDavid Greenman }
1166ba8c6fd5SDavid Greenman 
116700c4116bSJonathan Lemon static void
116800c4116bSJonathan Lemon fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
116900c4116bSJonathan Lemon {
117000c4116bSJonathan Lemon 	int i;
117100c4116bSJonathan Lemon 
117200c4116bSJonathan Lemon 	for (i = 0; i < words; i++)
117300c4116bSJonathan Lemon 		fxp_eeprom_putword(sc, offset + i, data[i]);
117400c4116bSJonathan Lemon }
117500c4116bSJonathan Lemon 
1176a17c678eSDavid Greenman /*
11774953bccaSNate Lawson  * Grab the softc lock and call the real fxp_start_body() routine
1178a17c678eSDavid Greenman  */
1179a17c678eSDavid Greenman static void
1180f7788e8eSJonathan Lemon fxp_start(struct ifnet *ifp)
1181a17c678eSDavid Greenman {
11829b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
11834953bccaSNate Lawson 
11844953bccaSNate Lawson 	FXP_LOCK(sc);
11854953bccaSNate Lawson 	fxp_start_body(ifp);
11864953bccaSNate Lawson 	FXP_UNLOCK(sc);
11874953bccaSNate Lawson }
11884953bccaSNate Lawson 
11894953bccaSNate Lawson /*
11904953bccaSNate Lawson  * Start packet transmission on the interface.
11914953bccaSNate Lawson  * This routine must be called with the softc lock held, and is an
11924953bccaSNate Lawson  * internal entry point only.
11934953bccaSNate Lawson  */
11944953bccaSNate Lawson static void
11954953bccaSNate Lawson fxp_start_body(struct ifnet *ifp)
11964953bccaSNate Lawson {
11974953bccaSNate Lawson 	struct fxp_softc *sc = ifp->if_softc;
1198b2badf02SMaxime Henrion 	struct mbuf *mb_head;
11994e53f837SPyun YongHyeon 	int txqueued;
1200a17c678eSDavid Greenman 
120167fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
120240c20505SMaxime Henrion 
1203a17c678eSDavid Greenman 	/*
1204483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
1205483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
1206483b9871SDavid Greenman 	 * of the command chain).
1207a17c678eSDavid Greenman 	 */
120840c20505SMaxime Henrion 	if (sc->need_mcsetup)
1209a17c678eSDavid Greenman 		return;
1210483b9871SDavid Greenman 
12114e53f837SPyun YongHyeon 	if (sc->tx_queued > FXP_NTXCB_HIWAT)
12124e53f837SPyun YongHyeon 		fxp_txeof(sc);
1213483b9871SDavid Greenman 	/*
1214483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
1215483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
12163114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
12173114fdb4SDavid Greenman 	 *       a NOP command when needed.
1218483b9871SDavid Greenman 	 */
121940c20505SMaxime Henrion 	txqueued = 0;
12207929aa03SMax Laier 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
12217929aa03SMax Laier 	    sc->tx_queued < FXP_NTXCB - 1) {
1222483b9871SDavid Greenman 
1223dfe61cf1SDavid Greenman 		/*
1224dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
1225dfe61cf1SDavid Greenman 		 */
12267929aa03SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
12277929aa03SMax Laier 		if (mb_head == NULL)
12287929aa03SMax Laier 			break;
1229a17c678eSDavid Greenman 
12304e53f837SPyun YongHyeon 		if (fxp_encap(sc, &mb_head)) {
12314e53f837SPyun YongHyeon 			if (mb_head == NULL)
123240c20505SMaxime Henrion 				break;
12334e53f837SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
12344e53f837SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
123540c20505SMaxime Henrion 		}
12364e53f837SPyun YongHyeon 		txqueued++;
12374e53f837SPyun YongHyeon 		/*
12384e53f837SPyun YongHyeon 		 * Pass packet to bpf if there is a listener.
12394e53f837SPyun YongHyeon 		 */
12404e53f837SPyun YongHyeon 		BPF_MTAP(ifp, mb_head);
12414e53f837SPyun YongHyeon 	}
124240c20505SMaxime Henrion 
124340c20505SMaxime Henrion 	/*
124440c20505SMaxime Henrion 	 * We're finished. If we added to the list, issue a RESUME to get DMA
124540c20505SMaxime Henrion 	 * going again if suspended.
124640c20505SMaxime Henrion 	 */
12474e53f837SPyun YongHyeon 	if (txqueued > 0) {
12484e53f837SPyun YongHyeon 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
124940c20505SMaxime Henrion 		fxp_scb_wait(sc);
125040c20505SMaxime Henrion 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
12514e53f837SPyun YongHyeon 		/*
12524e53f837SPyun YongHyeon 		 * Set a 5 second timer just in case we don't hear
12534e53f837SPyun YongHyeon 		 * from the card again.
12544e53f837SPyun YongHyeon 		 */
12554e53f837SPyun YongHyeon 		sc->watchdog_timer = 5;
125640c20505SMaxime Henrion 	}
125740c20505SMaxime Henrion }
125840c20505SMaxime Henrion 
125940c20505SMaxime Henrion static int
12604e53f837SPyun YongHyeon fxp_encap(struct fxp_softc *sc, struct mbuf **m_head)
126140c20505SMaxime Henrion {
126240c20505SMaxime Henrion 	struct ifnet *ifp;
126340c20505SMaxime Henrion 	struct mbuf *m;
126440c20505SMaxime Henrion 	struct fxp_tx *txp;
126540c20505SMaxime Henrion 	struct fxp_cb_tx *cbp;
126640c20505SMaxime Henrion 	bus_dma_segment_t segs[FXP_NTXSEG];
12674e53f837SPyun YongHyeon 	int error, i, nseg;
126840c20505SMaxime Henrion 
126940c20505SMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1270fc74a9f9SBrooks Davis 	ifp = sc->ifp;
127140c20505SMaxime Henrion 
1272dfe61cf1SDavid Greenman 	/*
1273483b9871SDavid Greenman 	 * Get pointer to next available tx desc.
1274dfe61cf1SDavid Greenman 	 */
1275b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_last->tx_next;
1276c8bca6dcSBill Paul 
1277c8bca6dcSBill Paul 	/*
1278a35e7eaaSDon Lewis 	 * A note in Appendix B of the Intel 8255x 10/100 Mbps
1279a35e7eaaSDon Lewis 	 * Ethernet Controller Family Open Source Software
1280a35e7eaaSDon Lewis 	 * Developer Manual says:
1281a35e7eaaSDon Lewis 	 *   Using software parsing is only allowed with legal
1282a35e7eaaSDon Lewis 	 *   TCP/IP or UDP/IP packets.
1283a35e7eaaSDon Lewis 	 *   ...
1284a35e7eaaSDon Lewis 	 *   For all other datagrams, hardware parsing must
1285a35e7eaaSDon Lewis 	 *   be used.
1286a35e7eaaSDon Lewis 	 * Software parsing appears to truncate ICMP and
1287a35e7eaaSDon Lewis 	 * fragmented UDP packets that contain one to three
1288a35e7eaaSDon Lewis 	 * bytes in the second (and final) mbuf of the packet.
1289a35e7eaaSDon Lewis 	 */
1290a35e7eaaSDon Lewis 	if (sc->flags & FXP_FLAG_EXT_RFA)
1291a35e7eaaSDon Lewis 		txp->tx_cb->ipcb_ip_activation_high =
1292a35e7eaaSDon Lewis 		    FXP_IPCB_HARDWAREPARSING_ENABLE;
1293a35e7eaaSDon Lewis 
12944e53f837SPyun YongHyeon 	m = *m_head;
1295a35e7eaaSDon Lewis 	/*
1296c8bca6dcSBill Paul 	 * Deal with TCP/IP checksum offload. Note that
1297c8bca6dcSBill Paul 	 * in order for TCP checksum offload to work,
1298c8bca6dcSBill Paul 	 * the pseudo header checksum must have already
1299c8bca6dcSBill Paul 	 * been computed and stored in the checksum field
1300c8bca6dcSBill Paul 	 * in the TCP header. The stack should have
1301c8bca6dcSBill Paul 	 * already done this for us.
1302c8bca6dcSBill Paul 	 */
1303c583cc04SPyun YongHyeon 	if (m->m_pkthdr.csum_flags & FXP_CSUM_FEATURES) {
1304c583cc04SPyun YongHyeon 		txp->tx_cb->ipcb_ip_schedule = FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
13054e53f837SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
1306c583cc04SPyun YongHyeon 			txp->tx_cb->ipcb_ip_schedule |= FXP_IPCB_TCP_PACKET;
130740c20505SMaxime Henrion 
1308c8bca6dcSBill Paul #ifdef FXP_IP_CSUM_WAR
1309c8bca6dcSBill Paul 		/*
1310c8bca6dcSBill Paul 		 * XXX The 82550 chip appears to have trouble
1311c8bca6dcSBill Paul 		 * dealing with IP header checksums in very small
1312c8bca6dcSBill Paul 		 * datagrams, namely fragments from 1 to 3 bytes
1313c8bca6dcSBill Paul 		 * in size. For example, say you want to transmit
1314c8bca6dcSBill Paul 		 * a UDP packet of 1473 bytes. The packet will be
1315c8bca6dcSBill Paul 		 * fragmented over two IP datagrams, the latter
1316c8bca6dcSBill Paul 		 * containing only one byte of data. The 82550 will
1317c8bca6dcSBill Paul 		 * botch the header checksum on the 1-byte fragment.
1318c8bca6dcSBill Paul 		 * As long as the datagram contains 4 or more bytes
1319c8bca6dcSBill Paul 		 * of data, you're ok.
1320c8bca6dcSBill Paul 		 *
1321c8bca6dcSBill Paul                  * The following code attempts to work around this
1322c8bca6dcSBill Paul 		 * problem: if the datagram is less than 38 bytes
1323c8bca6dcSBill Paul 		 * in size (14 bytes ether header, 20 bytes IP header,
1324c8bca6dcSBill Paul 		 * plus 4 bytes of data), we punt and compute the IP
1325c8bca6dcSBill Paul 		 * header checksum by hand. This workaround doesn't
1326c8bca6dcSBill Paul 		 * work very well, however, since it can be fooled
1327c8bca6dcSBill Paul 		 * by things like VLAN tags and IP options that make
1328c8bca6dcSBill Paul 		 * the header sizes/offsets vary.
1329c8bca6dcSBill Paul 		 */
1330c8bca6dcSBill Paul 
13314e53f837SPyun YongHyeon 		if (m->m_pkthdr.csum_flags & CSUM_IP) {
13324e53f837SPyun YongHyeon 			if (m->m_pkthdr.len < 38) {
1333c8bca6dcSBill Paul 				struct ip *ip;
13344e53f837SPyun YongHyeon 				m->m_data += ETHER_HDR_LEN;
13354e53f837SPyun YongHyeon 				ip = mtod(m, struct ip *);
13364e53f837SPyun YongHyeon 				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
13374e53f837SPyun YongHyeon 				m->m_data -= ETHER_HDR_LEN;
1338c583cc04SPyun YongHyeon 				m->m_pkthdr.csum_flags &= ~CSUM_IP;
1339c8bca6dcSBill Paul 			} else {
1340b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_activation_high =
1341c8bca6dcSBill Paul 				    FXP_IPCB_HARDWAREPARSING_ENABLE;
1342b2badf02SMaxime Henrion 				txp->tx_cb->ipcb_ip_schedule |=
1343c8bca6dcSBill Paul 				    FXP_IPCB_IP_CHECKSUM_ENABLE;
1344c8bca6dcSBill Paul 			}
1345c8bca6dcSBill Paul 		}
1346c8bca6dcSBill Paul #endif
1347c8bca6dcSBill Paul 	}
1348c8bca6dcSBill Paul 
13494e53f837SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->fxp_mtag, txp->tx_map, *m_head,
13504e53f837SPyun YongHyeon 	    segs, &nseg, 0);
13514e53f837SPyun YongHyeon 	if (error == EFBIG) {
13524e53f837SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, sc->maxtxseg);
13534e53f837SPyun YongHyeon 		if (m == NULL) {
13544e53f837SPyun YongHyeon 			m_freem(*m_head);
13554e53f837SPyun YongHyeon 			*m_head = NULL;
13564e53f837SPyun YongHyeon 			return (ENOMEM);
13571104779bSMike Silbersack 		}
13584e53f837SPyun YongHyeon 		*m_head = m;
135940c20505SMaxime Henrion 		error = bus_dmamap_load_mbuf_sg(sc->fxp_mtag, txp->tx_map,
13604e53f837SPyun YongHyeon 	    	    *m_head, segs, &nseg, 0);
13614e53f837SPyun YongHyeon 		if (error != 0) {
13624e53f837SPyun YongHyeon 			m_freem(*m_head);
13634e53f837SPyun YongHyeon 			*m_head = NULL;
13644e53f837SPyun YongHyeon 			return (ENOMEM);
13654e53f837SPyun YongHyeon 		}
13664e53f837SPyun YongHyeon 	} else if (error != 0)
13674e53f837SPyun YongHyeon 		return (error);
13684e53f837SPyun YongHyeon 	if (nseg == 0) {
13694e53f837SPyun YongHyeon 		m_freem(*m_head);
13704e53f837SPyun YongHyeon 		*m_head = NULL;
13714e53f837SPyun YongHyeon 		return (EIO);
137223a0ed7cSDavid Greenman 	}
137323a0ed7cSDavid Greenman 
137440c20505SMaxime Henrion 	KASSERT(nseg <= sc->maxtxseg, ("too many DMA segments"));
13754e53f837SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, BUS_DMASYNC_PREWRITE);
1376b2badf02SMaxime Henrion 
137740c20505SMaxime Henrion 	cbp = txp->tx_cb;
137840c20505SMaxime Henrion 	for (i = 0; i < nseg; i++) {
137940c20505SMaxime Henrion 		KASSERT(segs[i].ds_len <= MCLBYTES, ("segment size too large"));
138040c20505SMaxime Henrion 		/*
138140c20505SMaxime Henrion 		 * If this is an 82550/82551, then we're using extended
138240c20505SMaxime Henrion 		 * TxCBs _and_ we're using checksum offload. This means
138340c20505SMaxime Henrion 		 * that the TxCB is really an IPCB. One major difference
138440c20505SMaxime Henrion 		 * between the two is that with plain extended TxCBs,
138540c20505SMaxime Henrion 		 * the bottom half of the TxCB contains two entries from
138640c20505SMaxime Henrion 		 * the TBD array, whereas IPCBs contain just one entry:
138740c20505SMaxime Henrion 		 * one entry (8 bytes) has been sacrificed for the TCP/IP
138840c20505SMaxime Henrion 		 * checksum offload control bits. So to make things work
138940c20505SMaxime Henrion 		 * right, we have to start filling in the TBD array
139040c20505SMaxime Henrion 		 * starting from a different place depending on whether
139140c20505SMaxime Henrion 		 * the chip is an 82550/82551 or not.
139240c20505SMaxime Henrion 		 */
139340c20505SMaxime Henrion 		if (sc->flags & FXP_FLAG_EXT_RFA) {
139440c20505SMaxime Henrion 			cbp->tbd[i + 1].tb_addr = htole32(segs[i].ds_addr);
139540c20505SMaxime Henrion 			cbp->tbd[i + 1].tb_size = htole32(segs[i].ds_len);
139640c20505SMaxime Henrion 		} else {
139740c20505SMaxime Henrion 			cbp->tbd[i].tb_addr = htole32(segs[i].ds_addr);
139840c20505SMaxime Henrion 			cbp->tbd[i].tb_size = htole32(segs[i].ds_len);
139940c20505SMaxime Henrion 		}
140040c20505SMaxime Henrion 	}
140140c20505SMaxime Henrion 	cbp->tbd_number = nseg;
140240c20505SMaxime Henrion 
14034e53f837SPyun YongHyeon 	txp->tx_mbuf = m;
1404b2badf02SMaxime Henrion 	txp->tx_cb->cb_status = 0;
1405b2badf02SMaxime Henrion 	txp->tx_cb->byte_count = 0;
14064e53f837SPyun YongHyeon 	if (sc->tx_queued != FXP_CXINT_THRESH - 1)
1407b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
140883e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
140983e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S);
14104e53f837SPyun YongHyeon 	else
1411b2badf02SMaxime Henrion 		txp->tx_cb->cb_command =
141283e6547dSMaxime Henrion 		    htole16(sc->tx_cmd | FXP_CB_COMMAND_SF |
141383e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
1414b2badf02SMaxime Henrion 	txp->tx_cb->tx_threshold = tx_threshold;
1415a17c678eSDavid Greenman 
1416a17c678eSDavid Greenman 	/*
1417483b9871SDavid Greenman 	 * Advance the end of list forward.
1418a17c678eSDavid Greenman 	 */
141906175228SAndrew Gallatin 
142050d81222SMaxime Henrion #ifdef __alpha__
142106175228SAndrew Gallatin 	/*
142206175228SAndrew Gallatin 	 * On platforms which can't access memory in 16-bit
142306175228SAndrew Gallatin 	 * granularities, we must prevent the card from DMA'ing
142406175228SAndrew Gallatin 	 * up the status while we update the command field.
142506175228SAndrew Gallatin 	 * This could cause us to overwrite the completion status.
142614fd1071SMaxime Henrion 	 * XXX This is probably bogus and we're _not_ looking
142714fd1071SMaxime Henrion 	 * for atomicity here.
142806175228SAndrew Gallatin 	 */
142914fd1071SMaxime Henrion 	atomic_clear_16(&sc->fxp_desc.tx_last->tx_cb->cb_command,
1430bafb64afSMaxime Henrion 	    htole16(FXP_CB_COMMAND_S));
143150d81222SMaxime Henrion #else
143240c20505SMaxime Henrion 	sc->fxp_desc.tx_last->tx_cb->cb_command &= htole16(~FXP_CB_COMMAND_S);
143350d81222SMaxime Henrion #endif /*__alpha__*/
1434b2badf02SMaxime Henrion 	sc->fxp_desc.tx_last = txp;
1435a17c678eSDavid Greenman 
1436a17c678eSDavid Greenman 	/*
14371cd443acSDavid Greenman 	 * Advance the beginning of the list forward if there are
1438b2badf02SMaxime Henrion 	 * no other packets queued (when nothing is queued, tx_first
1439483b9871SDavid Greenman 	 * sits on the last TxCB that was sent out).
1440a17c678eSDavid Greenman 	 */
14411cd443acSDavid Greenman 	if (sc->tx_queued == 0)
1442b2badf02SMaxime Henrion 		sc->fxp_desc.tx_first = txp;
1443a17c678eSDavid Greenman 
14441cd443acSDavid Greenman 	sc->tx_queued++;
14451cd443acSDavid Greenman 
144640c20505SMaxime Henrion 	return (0);
1447a17c678eSDavid Greenman }
1448a17c678eSDavid Greenman 
1449e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
1450e4fc250cSLuigi Rizzo static poll_handler_t fxp_poll;
1451e4fc250cSLuigi Rizzo 
1452e4fc250cSLuigi Rizzo static void
1453e4fc250cSLuigi Rizzo fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1454e4fc250cSLuigi Rizzo {
1455e4fc250cSLuigi Rizzo 	struct fxp_softc *sc = ifp->if_softc;
145674d1ed23SMaxime Henrion 	uint8_t statack;
1457e4fc250cSLuigi Rizzo 
14584953bccaSNate Lawson 	FXP_LOCK(sc);
145940929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
14604953bccaSNate Lawson 		FXP_UNLOCK(sc);
1461e4fc250cSLuigi Rizzo 		return;
1462e4fc250cSLuigi Rizzo 	}
146340929967SGleb Smirnoff 
1464e4fc250cSLuigi Rizzo 	statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
1465e4fc250cSLuigi Rizzo 	    FXP_SCB_STATACK_FR;
1466e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) {
146774d1ed23SMaxime Henrion 		uint8_t tmp;
14686481f301SPeter Wemm 
1469e4fc250cSLuigi Rizzo 		tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
14704953bccaSNate Lawson 		if (tmp == 0xff || tmp == 0) {
14714953bccaSNate Lawson 			FXP_UNLOCK(sc);
1472e4fc250cSLuigi Rizzo 			return; /* nothing to do */
14734953bccaSNate Lawson 		}
1474e4fc250cSLuigi Rizzo 		tmp &= ~statack;
1475e4fc250cSLuigi Rizzo 		/* ack what we can */
1476e4fc250cSLuigi Rizzo 		if (tmp != 0)
1477e4fc250cSLuigi Rizzo 			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
1478e4fc250cSLuigi Rizzo 		statack |= tmp;
1479e4fc250cSLuigi Rizzo 	}
14804953bccaSNate Lawson 	fxp_intr_body(sc, ifp, statack, count);
14814953bccaSNate Lawson 	FXP_UNLOCK(sc);
1482e4fc250cSLuigi Rizzo }
1483e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
1484e4fc250cSLuigi Rizzo 
1485a17c678eSDavid Greenman /*
14869c7d2607SDavid Greenman  * Process interface interrupts.
1487a17c678eSDavid Greenman  */
148894927790SDavid Greenman static void
1489f7788e8eSJonathan Lemon fxp_intr(void *xsc)
1490a17c678eSDavid Greenman {
1491f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1492fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
149374d1ed23SMaxime Henrion 	uint8_t statack;
14940f4dc94cSChuck Paterson 
14954953bccaSNate Lawson 	FXP_LOCK(sc);
1496704d1965SWarner Losh 	if (sc->suspended) {
1497704d1965SWarner Losh 		FXP_UNLOCK(sc);
1498704d1965SWarner Losh 		return;
1499704d1965SWarner Losh 	}
1500704d1965SWarner Losh 
1501e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
150240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
15034953bccaSNate Lawson 		FXP_UNLOCK(sc);
1504e4fc250cSLuigi Rizzo 		return;
15054953bccaSNate Lawson 	}
1506e4fc250cSLuigi Rizzo #endif
1507b184b38eSDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1508a17c678eSDavid Greenman 		/*
150911457bbfSJonathan Lemon 		 * It should not be possible to have all bits set; the
151011457bbfSJonathan Lemon 		 * FXP_SCB_INTR_SWI bit always returns 0 on a read.  If
151111457bbfSJonathan Lemon 		 * all bits are set, this may indicate that the card has
151211457bbfSJonathan Lemon 		 * been physically ejected, so ignore it.
151311457bbfSJonathan Lemon 		 */
15144953bccaSNate Lawson 		if (statack == 0xff) {
15154953bccaSNate Lawson 			FXP_UNLOCK(sc);
151611457bbfSJonathan Lemon 			return;
15174953bccaSNate Lawson 		}
151811457bbfSJonathan Lemon 
151911457bbfSJonathan Lemon 		/*
1520a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1521a17c678eSDavid Greenman 		 */
1522ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
15234953bccaSNate Lawson 		fxp_intr_body(sc, ifp, statack, -1);
1524e4fc250cSLuigi Rizzo 	}
15254953bccaSNate Lawson 	FXP_UNLOCK(sc);
1526e4fc250cSLuigi Rizzo }
1527e4fc250cSLuigi Rizzo 
1528e4fc250cSLuigi Rizzo static void
1529b2badf02SMaxime Henrion fxp_txeof(struct fxp_softc *sc)
1530b2badf02SMaxime Henrion {
15314e53f837SPyun YongHyeon 	struct ifnet *ifp;
1532b2badf02SMaxime Henrion 	struct fxp_tx *txp;
1533b2badf02SMaxime Henrion 
15344e53f837SPyun YongHyeon 	ifp = sc->ifp;
1535b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREREAD);
1536b2badf02SMaxime Henrion 	for (txp = sc->fxp_desc.tx_first; sc->tx_queued &&
153783e6547dSMaxime Henrion 	    (le16toh(txp->tx_cb->cb_status) & FXP_CB_STATUS_C) != 0;
1538b2badf02SMaxime Henrion 	    txp = txp->tx_next) {
1539b2badf02SMaxime Henrion 		if (txp->tx_mbuf != NULL) {
1540b2badf02SMaxime Henrion 			bus_dmamap_sync(sc->fxp_mtag, txp->tx_map,
1541b2badf02SMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
1542b2badf02SMaxime Henrion 			bus_dmamap_unload(sc->fxp_mtag, txp->tx_map);
1543b2badf02SMaxime Henrion 			m_freem(txp->tx_mbuf);
1544b2badf02SMaxime Henrion 			txp->tx_mbuf = NULL;
1545b2badf02SMaxime Henrion 			/* clear this to reset csum offload bits */
1546b2badf02SMaxime Henrion 			txp->tx_cb->tbd[0].tb_addr = 0;
1547b2badf02SMaxime Henrion 		}
1548b2badf02SMaxime Henrion 		sc->tx_queued--;
15494e53f837SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1550b2badf02SMaxime Henrion 	}
1551b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = txp;
1552b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
155325935344SPyun YongHyeon 	if (sc->tx_queued == 0) {
155425935344SPyun YongHyeon 		sc->watchdog_timer = 0;
155525935344SPyun YongHyeon 		if (sc->need_mcsetup)
155625935344SPyun YongHyeon 			fxp_mc_setup(sc);
155725935344SPyun YongHyeon 	}
1558b2badf02SMaxime Henrion }
1559b2badf02SMaxime Henrion 
1560b2badf02SMaxime Henrion static void
156174d1ed23SMaxime Henrion fxp_intr_body(struct fxp_softc *sc, struct ifnet *ifp, uint8_t statack,
15624953bccaSNate Lawson     int count)
1563e4fc250cSLuigi Rizzo {
15642b5989e9SLuigi Rizzo 	struct mbuf *m;
1565b2badf02SMaxime Henrion 	struct fxp_rx *rxp;
15662b5989e9SLuigi Rizzo 	struct fxp_rfa *rfa;
15672b5989e9SLuigi Rizzo 	int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
156860bb79ebSPyun YongHyeon 	uint16_t status;
15692b5989e9SLuigi Rizzo 
157067fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
15712b5989e9SLuigi Rizzo 	if (rnr)
15720f1db1d6SMaxime Henrion 		sc->rnr++;
1573947e3815SIan Dowse #ifdef DEVICE_POLLING
1574947e3815SIan Dowse 	/* Pick up a deferred RNR condition if `count' ran out last time. */
1575947e3815SIan Dowse 	if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
1576947e3815SIan Dowse 		sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
1577947e3815SIan Dowse 		rnr = 1;
1578947e3815SIan Dowse 	}
1579947e3815SIan Dowse #endif
1580a17c678eSDavid Greenman 
1581a17c678eSDavid Greenman 	/*
15823114fdb4SDavid Greenman 	 * Free any finished transmit mbuf chains.
158306936301SBill Paul 	 *
158406936301SBill Paul 	 * Handle the CNA event likt a CXTNO event. It used to
158506936301SBill Paul 	 * be that this event (control unit not ready) was not
158606936301SBill Paul 	 * encountered, but it is now with the SMPng modifications.
158706936301SBill Paul 	 * The exact sequence of events that occur when the interface
158806936301SBill Paul 	 * is brought up are different now, and if this event
158906936301SBill Paul 	 * goes unhandled, the configuration/rxfilter setup sequence
159006936301SBill Paul 	 * can stall for several seconds. The result is that no
159106936301SBill Paul 	 * packets go out onto the wire for about 5 to 10 seconds
159206936301SBill Paul 	 * after the interface is ifconfig'ed for the first time.
15933114fdb4SDavid Greenman 	 */
15944e53f837SPyun YongHyeon 	if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA))
1595b2badf02SMaxime Henrion 		fxp_txeof(sc);
15963114fdb4SDavid Greenman 
15973114fdb4SDavid Greenman 	/*
15983114fdb4SDavid Greenman 	 * Try to start more packets transmitting.
15993114fdb4SDavid Greenman 	 */
16007929aa03SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
16014953bccaSNate Lawson 		fxp_start_body(ifp);
16022b5989e9SLuigi Rizzo 
16032b5989e9SLuigi Rizzo 	/*
16042b5989e9SLuigi Rizzo 	 * Just return if nothing happened on the receive side.
16052b5989e9SLuigi Rizzo 	 */
1606947e3815SIan Dowse 	if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
16072b5989e9SLuigi Rizzo 		return;
16082b5989e9SLuigi Rizzo 
16093114fdb4SDavid Greenman 	/*
1610a17c678eSDavid Greenman 	 * Process receiver interrupts. If a no-resource (RNR)
1611a17c678eSDavid Greenman 	 * condition exists, get whatever packets we can and
1612a17c678eSDavid Greenman 	 * re-start the receiver.
1613947e3815SIan Dowse 	 *
16142b5989e9SLuigi Rizzo 	 * When using polling, we do not process the list to completion,
16152b5989e9SLuigi Rizzo 	 * so when we get an RNR interrupt we must defer the restart
16162b5989e9SLuigi Rizzo 	 * until we hit the last buffer with the C bit set.
16172b5989e9SLuigi Rizzo 	 * If we run out of cycles and rfa_headm has the C bit set,
1618947e3815SIan Dowse 	 * record the pending RNR in the FXP_FLAG_DEFERRED_RNR flag so
1619947e3815SIan Dowse 	 * that the info will be used in the subsequent polling cycle.
1620a17c678eSDavid Greenman 	 */
16212b5989e9SLuigi Rizzo 	for (;;) {
1622b2badf02SMaxime Henrion 		rxp = sc->fxp_desc.rx_head;
1623b2badf02SMaxime Henrion 		m = rxp->rx_mbuf;
1624ba8c6fd5SDavid Greenman 		rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1625ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1626b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
1627b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
1628a17c678eSDavid Greenman 
1629e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING /* loop at most count times if count >=0 */
1630947e3815SIan Dowse 		if (count >= 0 && count-- == 0) {
1631947e3815SIan Dowse 			if (rnr) {
1632947e3815SIan Dowse 				/* Defer RNR processing until the next time. */
1633947e3815SIan Dowse 				sc->flags |= FXP_FLAG_DEFERRED_RNR;
1634947e3815SIan Dowse 				rnr = 0;
1635947e3815SIan Dowse 			}
16362b5989e9SLuigi Rizzo 			break;
1637947e3815SIan Dowse 		}
16382b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
16392b5989e9SLuigi Rizzo 
164060bb79ebSPyun YongHyeon 		status = le16toh(rfa->rfa_status);
164160bb79ebSPyun YongHyeon 		if ((status & FXP_RFA_STATUS_C) == 0)
16422b5989e9SLuigi Rizzo 			break;
16432b5989e9SLuigi Rizzo 
1644dfe61cf1SDavid Greenman 		/*
1645b2badf02SMaxime Henrion 		 * Advance head forward.
1646dfe61cf1SDavid Greenman 		 */
1647b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp->rx_next;
1648a17c678eSDavid Greenman 
1649dfe61cf1SDavid Greenman 		/*
1650ba8c6fd5SDavid Greenman 		 * Add a new buffer to the receive chain.
1651ba8c6fd5SDavid Greenman 		 * If this fails, the old buffer is recycled
1652ba8c6fd5SDavid Greenman 		 * instead.
1653dfe61cf1SDavid Greenman 		 */
165485050421SPyun YongHyeon 		if (fxp_new_rfabuf(sc, rxp) == 0) {
1655aed53495SDavid Greenman 			int total_len;
1656a17c678eSDavid Greenman 
1657e8c8b728SJonathan Lemon 			/*
16582b5989e9SLuigi Rizzo 			 * Fetch packet length (the top 2 bits of
16592b5989e9SLuigi Rizzo 			 * actual_size are flags set by the controller
16602b5989e9SLuigi Rizzo 			 * upon completion), and drop the packet in case
16612b5989e9SLuigi Rizzo 			 * of bogus length or CRC errors.
1662e8c8b728SJonathan Lemon 			 */
1663bafb64afSMaxime Henrion 			total_len = le16toh(rfa->actual_size) & 0x3fff;
16642b5989e9SLuigi Rizzo 			if (total_len < sizeof(struct ether_header) ||
16652b5989e9SLuigi Rizzo 			    total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
166660bb79ebSPyun YongHyeon 				sc->rfa_size || status & FXP_RFA_STATUS_CRC) {
1667e8c8b728SJonathan Lemon 				m_freem(m);
16682b5989e9SLuigi Rizzo 				continue;
1669e8c8b728SJonathan Lemon 			}
1670920b58e8SBrooks Davis 
1671c8bca6dcSBill Paul                         /* Do IP checksum checking. */
167260bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0 &&
167360bb79ebSPyun YongHyeon 			    (status & FXP_RFA_STATUS_PARSE)) {
1674c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1675c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_BIT_VALID)
1676c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1677c8bca6dcSBill Paul 					    CSUM_IP_CHECKED;
1678c8bca6dcSBill Paul 				if (rfa->rfax_csum_sts &
1679c8bca6dcSBill Paul 				    FXP_RFDX_CS_IP_CSUM_VALID)
1680c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1681c8bca6dcSBill Paul 					    CSUM_IP_VALID;
1682c8bca6dcSBill Paul 				if ((rfa->rfax_csum_sts &
1683c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) &&
1684c8bca6dcSBill Paul 				    (rfa->rfax_csum_sts &
1685c8bca6dcSBill Paul 				    FXP_RFDX_CS_TCPUDP_CSUM_VALID)) {
1686c8bca6dcSBill Paul 					m->m_pkthdr.csum_flags |=
1687c8bca6dcSBill Paul 					    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1688c8bca6dcSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1689c8bca6dcSBill Paul 				}
1690c8bca6dcSBill Paul 			}
1691c8bca6dcSBill Paul 
16922e2de7f2SArchie Cobbs 			m->m_pkthdr.len = m->m_len = total_len;
1693673d9191SSam Leffler 			m->m_pkthdr.rcvif = ifp;
1694673d9191SSam Leffler 
169505fb8c3fSNate Lawson 			/*
169605fb8c3fSNate Lawson 			 * Drop locks before calling if_input() since it
169705fb8c3fSNate Lawson 			 * may re-enter fxp_start() in the netisr case.
169805fb8c3fSNate Lawson 			 * This would result in a lock reversal.  Better
169905fb8c3fSNate Lawson 			 * performance might be obtained by chaining all
170005fb8c3fSNate Lawson 			 * packets received, dropping the lock, and then
170105fb8c3fSNate Lawson 			 * calling if_input() on each one.
170205fb8c3fSNate Lawson 			 */
170305fb8c3fSNate Lawson 			FXP_UNLOCK(sc);
1704673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
170505fb8c3fSNate Lawson 			FXP_LOCK(sc);
170685050421SPyun YongHyeon 		} else {
170785050421SPyun YongHyeon 			/* Reuse RFA and loaded DMA map. */
170885050421SPyun YongHyeon 			ifp->if_iqdrops++;
170985050421SPyun YongHyeon 			fxp_discard_rfabuf(sc, rxp);
1710a17c678eSDavid Greenman 		}
171185050421SPyun YongHyeon 		fxp_add_rfabuf(sc, rxp);
1712a17c678eSDavid Greenman 	}
17132b5989e9SLuigi Rizzo 	if (rnr) {
1714ba8c6fd5SDavid Greenman 		fxp_scb_wait(sc);
1715ba8c6fd5SDavid Greenman 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1716b2badf02SMaxime Henrion 		    sc->fxp_desc.rx_head->rx_addr);
17172e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1718a17c678eSDavid Greenman 	}
1719a17c678eSDavid Greenman }
1720a17c678eSDavid Greenman 
1721dfe61cf1SDavid Greenman /*
1722dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1723dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1724dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1725dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1726dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1727dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1728dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1729dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1730dfe61cf1SDavid Greenman  * them again next time.
1731dfe61cf1SDavid Greenman  */
1732303b270bSEivind Eklund static void
1733f7788e8eSJonathan Lemon fxp_tick(void *xsc)
1734a17c678eSDavid Greenman {
1735f7788e8eSJonathan Lemon 	struct fxp_softc *sc = xsc;
1736fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
1737a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1738a17c678eSDavid Greenman 
17393212724cSJohn Baldwin 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1740b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_POSTREAD);
174183e6547dSMaxime Henrion 	ifp->if_opackets += le32toh(sp->tx_good);
174283e6547dSMaxime Henrion 	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1743397f9dfeSDavid Greenman 	if (sp->rx_good) {
174483e6547dSMaxime Henrion 		ifp->if_ipackets += le32toh(sp->rx_good);
1745397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1746397f9dfeSDavid Greenman 	} else {
1747c8cc6fcaSDavid Greenman 		/*
1748c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1749c8cc6fcaSDavid Greenman 		 */
1750397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1751397f9dfeSDavid Greenman 	}
17523ba65732SDavid Greenman 	ifp->if_ierrors +=
175383e6547dSMaxime Henrion 	    le32toh(sp->rx_crc_errors) +
175483e6547dSMaxime Henrion 	    le32toh(sp->rx_alignment_errors) +
175583e6547dSMaxime Henrion 	    le32toh(sp->rx_rnr_errors) +
175683e6547dSMaxime Henrion 	    le32toh(sp->rx_overrun_errors);
1757a17c678eSDavid Greenman 	/*
1758f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1759f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1760f9be9005SDavid Greenman 	 */
1761f9be9005SDavid Greenman 	if (sp->tx_underruns) {
176283e6547dSMaxime Henrion 		ifp->if_oerrors += le32toh(sp->tx_underruns);
1763f9be9005SDavid Greenman 		if (tx_threshold < 192)
1764f9be9005SDavid Greenman 			tx_threshold += 64;
1765f9be9005SDavid Greenman 	}
17664953bccaSNate Lawson 
1767397f9dfeSDavid Greenman 	/*
1768c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1769c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1770c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1771c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1772c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1773c8cc6fcaSDavid Greenman 	 */
1774b2badf02SMaxime Henrion 	fxp_txeof(sc);
1775b2badf02SMaxime Henrion 
1776c8cc6fcaSDavid Greenman 	/*
1777397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1778397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1779397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1780397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1781397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1782397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1783397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1784397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1785397f9dfeSDavid Greenman 	 */
1786397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1787397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1788397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1789397f9dfeSDavid Greenman 	}
1790f9be9005SDavid Greenman 	/*
17913ba65732SDavid Greenman 	 * If there is no pending command, start another stats
17923ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1793a17c678eSDavid Greenman 	 */
1794397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1795a17c678eSDavid Greenman 		/*
1796397f9dfeSDavid Greenman 		 * Start another stats dump.
1797a17c678eSDavid Greenman 		 */
1798b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap,
1799b2badf02SMaxime Henrion 		    BUS_DMASYNC_PREREAD);
18002e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1801dfe61cf1SDavid Greenman 	} else {
1802dfe61cf1SDavid Greenman 		/*
1803dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1804dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
18053ba65732SDavid Greenman 		 * next timer event to update them.
1806dfe61cf1SDavid Greenman 		 */
1807dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1808f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1809dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
18103ba65732SDavid Greenman 
1811dfe61cf1SDavid Greenman 		sp->rx_good = 0;
18123ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
18133ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
18143ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
18153ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1816dfe61cf1SDavid Greenman 	}
1817f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
1818f7788e8eSJonathan Lemon 		mii_tick(device_get_softc(sc->miibus));
18194953bccaSNate Lawson 
1820a17c678eSDavid Greenman 	/*
182116f1e614SRuslan Ermilov 	 * Check that chip hasn't hung.
1822df79d527SGleb Smirnoff 	 */
1823df79d527SGleb Smirnoff 	fxp_watchdog(sc);
1824df79d527SGleb Smirnoff 
1825df79d527SGleb Smirnoff 	/*
1826a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1827a17c678eSDavid Greenman 	 */
182845276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
1829a17c678eSDavid Greenman }
1830a17c678eSDavid Greenman 
1831a17c678eSDavid Greenman /*
1832a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1833a17c678eSDavid Greenman  * the interface.
1834a17c678eSDavid Greenman  */
1835a17c678eSDavid Greenman static void
1836f7788e8eSJonathan Lemon fxp_stop(struct fxp_softc *sc)
1837a17c678eSDavid Greenman {
1838fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
1839b2badf02SMaxime Henrion 	struct fxp_tx *txp;
18403ba65732SDavid Greenman 	int i;
1841a17c678eSDavid Greenman 
184213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1843df79d527SGleb Smirnoff 	sc->watchdog_timer = 0;
18447dced78aSDavid Greenman 
1845a17c678eSDavid Greenman 	/*
1846a17c678eSDavid Greenman 	 * Cancel stats updater.
1847a17c678eSDavid Greenman 	 */
184845276e4aSSam Leffler 	callout_stop(&sc->stat_ch);
18493ba65732SDavid Greenman 
18503ba65732SDavid Greenman 	/*
185172a32a26SJonathan Lemon 	 * Issue software reset, which also unloads the microcode.
18523ba65732SDavid Greenman 	 */
185372a32a26SJonathan Lemon 	sc->flags &= ~FXP_FLAG_UCODE;
185409882363SJonathan Lemon 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
185572a32a26SJonathan Lemon 	DELAY(50);
1856a17c678eSDavid Greenman 
18573ba65732SDavid Greenman 	/*
18583ba65732SDavid Greenman 	 * Release any xmit buffers.
18593ba65732SDavid Greenman 	 */
1860b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
1861da91462dSDavid Greenman 	if (txp != NULL) {
1862da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1863b2badf02SMaxime Henrion  			if (txp[i].tx_mbuf != NULL) {
1864b2badf02SMaxime Henrion 				bus_dmamap_sync(sc->fxp_mtag, txp[i].tx_map,
1865b2badf02SMaxime Henrion 				    BUS_DMASYNC_POSTWRITE);
1866b2badf02SMaxime Henrion 				bus_dmamap_unload(sc->fxp_mtag, txp[i].tx_map);
1867b2badf02SMaxime Henrion 				m_freem(txp[i].tx_mbuf);
1868b2badf02SMaxime Henrion 				txp[i].tx_mbuf = NULL;
1869c8bca6dcSBill Paul 				/* clear this to reset csum offload bits */
1870b2badf02SMaxime Henrion 				txp[i].tx_cb->tbd[0].tb_addr = 0;
1871da91462dSDavid Greenman 			}
1872da91462dSDavid Greenman 		}
18733ba65732SDavid Greenman 	}
1874b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
18753ba65732SDavid Greenman 	sc->tx_queued = 0;
1876a17c678eSDavid Greenman }
1877a17c678eSDavid Greenman 
1878a17c678eSDavid Greenman /*
1879a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1880a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1881a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1882a17c678eSDavid Greenman  * card has wedged for some reason.
1883a17c678eSDavid Greenman  */
1884a17c678eSDavid Greenman static void
1885df79d527SGleb Smirnoff fxp_watchdog(struct fxp_softc *sc)
1886a17c678eSDavid Greenman {
1887ba8c6fd5SDavid Greenman 
1888df79d527SGleb Smirnoff 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1889df79d527SGleb Smirnoff 
1890df79d527SGleb Smirnoff 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
1891df79d527SGleb Smirnoff 		return;
1892df79d527SGleb Smirnoff 
1893f7788e8eSJonathan Lemon 	device_printf(sc->dev, "device timeout\n");
1894df79d527SGleb Smirnoff 	sc->ifp->if_oerrors++;
1895a17c678eSDavid Greenman 
18964953bccaSNate Lawson 	fxp_init_body(sc);
1897a17c678eSDavid Greenman }
1898a17c678eSDavid Greenman 
18994953bccaSNate Lawson /*
19004953bccaSNate Lawson  * Acquire locks and then call the real initialization function.  This
19014953bccaSNate Lawson  * is necessary because ether_ioctl() calls if_init() and this would
19024953bccaSNate Lawson  * result in mutex recursion if the mutex was held.
19034953bccaSNate Lawson  */
1904a17c678eSDavid Greenman static void
1905f7788e8eSJonathan Lemon fxp_init(void *xsc)
1906a17c678eSDavid Greenman {
1907fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
19084953bccaSNate Lawson 
19094953bccaSNate Lawson 	FXP_LOCK(sc);
19104953bccaSNate Lawson 	fxp_init_body(sc);
19114953bccaSNate Lawson 	FXP_UNLOCK(sc);
19124953bccaSNate Lawson }
19134953bccaSNate Lawson 
19144953bccaSNate Lawson /*
19154953bccaSNate Lawson  * Perform device initialization. This routine must be called with the
19164953bccaSNate Lawson  * softc lock held.
19174953bccaSNate Lawson  */
19184953bccaSNate Lawson static void
19194953bccaSNate Lawson fxp_init_body(struct fxp_softc *sc)
19204953bccaSNate Lawson {
1921fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
1922a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1923a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1924b2badf02SMaxime Henrion 	struct fxp_cb_tx *tcbp;
1925b2badf02SMaxime Henrion 	struct fxp_tx *txp;
192609882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp;
19273212724cSJohn Baldwin 	int i, prm;
1928a17c678eSDavid Greenman 
192967fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
1930a17c678eSDavid Greenman 	/*
19313ba65732SDavid Greenman 	 * Cancel any pending I/O
1932a17c678eSDavid Greenman 	 */
19333ba65732SDavid Greenman 	fxp_stop(sc);
1934a17c678eSDavid Greenman 
1935a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1936a17c678eSDavid Greenman 
1937a17c678eSDavid Greenman 	/*
1938a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1939a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1940a17c678eSDavid Greenman 	 */
1941ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
19422e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1943a17c678eSDavid Greenman 
1944ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
19452e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1946a17c678eSDavid Greenman 
1947a17c678eSDavid Greenman 	/*
1948a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1949a17c678eSDavid Greenman 	 */
1950ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1951b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->fxp_stag, sc->fxp_smap, BUS_DMASYNC_PREREAD);
1952b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->stats_addr);
19532e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1954a17c678eSDavid Greenman 
1955a17c678eSDavid Greenman 	/*
195672a32a26SJonathan Lemon 	 * Attempt to load microcode if requested.
195772a32a26SJonathan Lemon 	 */
195872a32a26SJonathan Lemon 	if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
195972a32a26SJonathan Lemon 		fxp_load_ucode(sc);
196072a32a26SJonathan Lemon 
196172a32a26SJonathan Lemon 	/*
196209882363SJonathan Lemon 	 * Initialize the multicast address list.
196309882363SJonathan Lemon 	 */
196409882363SJonathan Lemon 	if (fxp_mc_addrs(sc)) {
196509882363SJonathan Lemon 		mcsp = sc->mcsp;
196609882363SJonathan Lemon 		mcsp->cb_status = 0;
196783e6547dSMaxime Henrion 		mcsp->cb_command =
196883e6547dSMaxime Henrion 		    htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
196983e6547dSMaxime Henrion 		mcsp->link_addr = 0xffffffff;
197009882363SJonathan Lemon 		/*
197109882363SJonathan Lemon 	 	 * Start the multicast setup command.
197209882363SJonathan Lemon 		 */
197309882363SJonathan Lemon 		fxp_scb_wait(sc);
1974b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
1975b2badf02SMaxime Henrion 		CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
197609882363SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
197709882363SJonathan Lemon 		/* ...and wait for it to complete. */
1978209b07bcSMaxime Henrion 		fxp_dma_wait(sc, &mcsp->cb_status, sc->mcs_tag, sc->mcs_map);
1979b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->mcs_tag, sc->mcs_map,
1980b2badf02SMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
198109882363SJonathan Lemon 	}
198209882363SJonathan Lemon 
198309882363SJonathan Lemon 	/*
1984a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1985a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1986a17c678eSDavid Greenman 	 * later.
1987a17c678eSDavid Greenman 	 */
1988b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_config *)sc->fxp_desc.cbl_list;
1989a17c678eSDavid Greenman 
1990a17c678eSDavid Greenman 	/*
1991a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1992a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1993a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1994a17c678eSDavid Greenman 	 */
1995b2badf02SMaxime Henrion 	bcopy(fxp_cb_config_template, cbp, sizeof(fxp_cb_config_template));
1996a17c678eSDavid Greenman 
1997a17c678eSDavid Greenman 	cbp->cb_status =	0;
199883e6547dSMaxime Henrion 	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
199983e6547dSMaxime Henrion 	    FXP_CB_COMMAND_EL);
200083e6547dSMaxime Henrion 	cbp->link_addr =	0xffffffff;	/* (no) next command */
20012c6c0947SMaxime Henrion 	cbp->byte_count =	sc->flags & FXP_FLAG_EXT_RFA ? 32 : 22;
2002001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
2003001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
2004a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
2005f7788e8eSJonathan Lemon 	cbp->mwi_enable =	sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
2006f7788e8eSJonathan Lemon 	cbp->type_enable =	0;	/* actually reserved */
2007f7788e8eSJonathan Lemon 	cbp->read_align_en =	sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
2008f7788e8eSJonathan Lemon 	cbp->end_wr_on_cl =	sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
2009001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
2010001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
2011f7788e8eSJonathan Lemon 	cbp->dma_mbce =		0;	/* (disable) dma max counters */
2012a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
2013f7788e8eSJonathan Lemon 	cbp->direct_dma_dis =	1;	/* disable direct rcv dma mode */
2014f7788e8eSJonathan Lemon 	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
20153114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
2016f7788e8eSJonathan Lemon 	cbp->ext_txcb_dis = 	sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
2017f7788e8eSJonathan Lemon 	cbp->ext_stats_dis = 	1;	/* disable extended counters */
2018f7788e8eSJonathan Lemon 	cbp->keep_overrun_rx = 	0;	/* don't pass overrun frames to host */
20198ef1f631SYaroslav Tykhiy 	cbp->save_bf =		sc->flags & FXP_FLAG_SAVE_BAD ? 1 : prm;
2020a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
2021f7788e8eSJonathan Lemon 	cbp->underrun_retry =	1;	/* retry mode (once) on DMA underrun */
2022f7788e8eSJonathan Lemon 	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
2023f7788e8eSJonathan Lemon 	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
2024c8bca6dcSBill Paul 	cbp->ext_rfa =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2025f7788e8eSJonathan Lemon 	cbp->mediatype =	sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
2026f7788e8eSJonathan Lemon 	cbp->csma_dis =		0;	/* (don't) disable link */
2027f7788e8eSJonathan Lemon 	cbp->tcp_udp_cksum =	0;	/* (don't) enable checksum */
2028f7788e8eSJonathan Lemon 	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
2029f7788e8eSJonathan Lemon 	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
2030f7788e8eSJonathan Lemon 	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
2031f7788e8eSJonathan Lemon 	cbp->mc_wake_en =	0;	/* (don't) enable PME# on mcmatch */
2032a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
2033a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
2034a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
2035a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
2036a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
2037a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
2038a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
2039a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
2040f7788e8eSJonathan Lemon 	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
2041f7788e8eSJonathan Lemon 	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
2042f7788e8eSJonathan Lemon 	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
2043f7788e8eSJonathan Lemon 	cbp->crscdt =		sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
2044f7788e8eSJonathan Lemon 
2045a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
2046a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
2047a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
2048f7788e8eSJonathan Lemon 	cbp->long_rx_en =	sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
2049f7788e8eSJonathan Lemon 	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
2050f7788e8eSJonathan Lemon 	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
2051f7788e8eSJonathan Lemon 					/* must set wake_en in PMCSR also */
2052a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
20533ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
2054a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
2055f7788e8eSJonathan Lemon 	cbp->mc_all =		sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
2056c8bca6dcSBill Paul 	cbp->gamla_rx =		sc->flags & FXP_FLAG_EXT_RFA ? 1 : 0;
2057a17c678eSDavid Greenman 
20580f1db1d6SMaxime Henrion 	if (sc->tunable_noflow || sc->revision == FXP_REV_82557) {
20593bd07cfdSJonathan Lemon 		/*
20603bd07cfdSJonathan Lemon 		 * The 82557 has no hardware flow control, the values
20613bd07cfdSJonathan Lemon 		 * below are the defaults for the chip.
20623bd07cfdSJonathan Lemon 		 */
20633bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0;
20643bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x40;
20653bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
20663bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;
20673bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	0;
20683bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	0;
20693bd07cfdSJonathan Lemon 		cbp->fc_filter =	0;
20703bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;
20713bd07cfdSJonathan Lemon 	} else {
20723bd07cfdSJonathan Lemon 		cbp->fc_delay_lsb =	0x1f;
20733bd07cfdSJonathan Lemon 		cbp->fc_delay_msb =	0x01;
20743bd07cfdSJonathan Lemon 		cbp->pri_fc_thresh =	3;
20753bd07cfdSJonathan Lemon 		cbp->tx_fc_dis =	0;	/* enable transmit FC */
20763bd07cfdSJonathan Lemon 		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
20773bd07cfdSJonathan Lemon 		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
20783bd07cfdSJonathan Lemon 		cbp->fc_filter =	!prm;	/* drop FC frames to host */
20793bd07cfdSJonathan Lemon 		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
20803bd07cfdSJonathan Lemon 	}
20813bd07cfdSJonathan Lemon 
2082a17c678eSDavid Greenman 	/*
2083a17c678eSDavid Greenman 	 * Start the config command/DMA.
2084a17c678eSDavid Greenman 	 */
2085ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2086b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2087b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
20882e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2089a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2090209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2091b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2092a17c678eSDavid Greenman 
2093a17c678eSDavid Greenman 	/*
2094a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
2095a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
2096a17c678eSDavid Greenman 	 */
2097b2badf02SMaxime Henrion 	cb_ias = (struct fxp_cb_ias *)sc->fxp_desc.cbl_list;
2098a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
209983e6547dSMaxime Henrion 	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
210083e6547dSMaxime Henrion 	cb_ias->link_addr = 0xffffffff;
21014a0d6638SRuslan Ermilov 	bcopy(IF_LLADDR(sc->ifp), cb_ias->macaddr, ETHER_ADDR_LEN);
2102a17c678eSDavid Greenman 
2103a17c678eSDavid Greenman 	/*
2104a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
2105a17c678eSDavid Greenman 	 */
2106ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2107b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
21082e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2109a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
2110209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cb_ias->cb_status, sc->cbl_tag, sc->cbl_map);
2111b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
2112a17c678eSDavid Greenman 
2113a17c678eSDavid Greenman 	/*
2114a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
2115a17c678eSDavid Greenman 	 */
2116b2badf02SMaxime Henrion 	txp = sc->fxp_desc.tx_list;
2117b2badf02SMaxime Henrion 	tcbp = sc->fxp_desc.cbl_list;
2118b2badf02SMaxime Henrion 	bzero(tcbp, FXP_TXCB_SZ);
2119a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
2120b2badf02SMaxime Henrion 		txp[i].tx_mbuf = NULL;
212183e6547dSMaxime Henrion 		tcbp[i].cb_status = htole16(FXP_CB_STATUS_C | FXP_CB_STATUS_OK);
212283e6547dSMaxime Henrion 		tcbp[i].cb_command = htole16(FXP_CB_COMMAND_NOP);
212383e6547dSMaxime Henrion 		tcbp[i].link_addr = htole32(sc->fxp_desc.cbl_addr +
212483e6547dSMaxime Henrion 		    (((i + 1) & FXP_TXCB_MASK) * sizeof(struct fxp_cb_tx)));
21253bd07cfdSJonathan Lemon 		if (sc->flags & FXP_FLAG_EXT_TXCB)
2126b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
212783e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[2]));
21283bd07cfdSJonathan Lemon 		else
2129b2badf02SMaxime Henrion 			tcbp[i].tbd_array_addr =
213083e6547dSMaxime Henrion 			    htole32(FXP_TXCB_DMA_ADDR(sc, &tcbp[i].tbd[0]));
2131b2badf02SMaxime Henrion 		txp[i].tx_next = &txp[(i + 1) & FXP_TXCB_MASK];
2132a17c678eSDavid Greenman 	}
2133a17c678eSDavid Greenman 	/*
2134397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
2135a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
2136a17c678eSDavid Greenman 	 */
213783e6547dSMaxime Henrion 	tcbp->cb_command = htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
2138b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2139b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2140397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2141a17c678eSDavid Greenman 
2142ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
21432e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2144a17c678eSDavid Greenman 
2145a17c678eSDavid Greenman 	/*
2146a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
2147a17c678eSDavid Greenman 	 */
2148ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
2149b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.rx_head->rx_addr);
21502e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2151a17c678eSDavid Greenman 
2152dccee1a1SDavid Greenman 	/*
2153ba8c6fd5SDavid Greenman 	 * Set current media.
2154dccee1a1SDavid Greenman 	 */
2155f7788e8eSJonathan Lemon 	if (sc->miibus != NULL)
2156f7788e8eSJonathan Lemon 		mii_mediachg(device_get_softc(sc->miibus));
2157dccee1a1SDavid Greenman 
215813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
215913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2160e8c8b728SJonathan Lemon 
2161e8c8b728SJonathan Lemon 	/*
2162e8c8b728SJonathan Lemon 	 * Enable interrupts.
2163e8c8b728SJonathan Lemon 	 */
21642b5989e9SLuigi Rizzo #ifdef DEVICE_POLLING
21652b5989e9SLuigi Rizzo 	/*
21662b5989e9SLuigi Rizzo 	 * ... but only do that if we are not polling. And because (presumably)
21672b5989e9SLuigi Rizzo 	 * the default is interrupts on, we need to disable them explicitly!
21682b5989e9SLuigi Rizzo 	 */
216940929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING )
21702b5989e9SLuigi Rizzo 		CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
21712b5989e9SLuigi Rizzo 	else
21722b5989e9SLuigi Rizzo #endif /* DEVICE_POLLING */
2173e8c8b728SJonathan Lemon 	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
2174a17c678eSDavid Greenman 
2175a17c678eSDavid Greenman 	/*
2176a17c678eSDavid Greenman 	 * Start stats updater.
2177a17c678eSDavid Greenman 	 */
217845276e4aSSam Leffler 	callout_reset(&sc->stat_ch, hz, fxp_tick, sc);
2179f7788e8eSJonathan Lemon }
2180f7788e8eSJonathan Lemon 
2181f7788e8eSJonathan Lemon static int
2182f7788e8eSJonathan Lemon fxp_serial_ifmedia_upd(struct ifnet *ifp)
2183f7788e8eSJonathan Lemon {
2184f7788e8eSJonathan Lemon 
2185f7788e8eSJonathan Lemon 	return (0);
2186a17c678eSDavid Greenman }
2187a17c678eSDavid Greenman 
2188303b270bSEivind Eklund static void
2189f7788e8eSJonathan Lemon fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2190ba8c6fd5SDavid Greenman {
2191ba8c6fd5SDavid Greenman 
2192f7788e8eSJonathan Lemon 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2193ba8c6fd5SDavid Greenman }
2194ba8c6fd5SDavid Greenman 
2195ba8c6fd5SDavid Greenman /*
2196ba8c6fd5SDavid Greenman  * Change media according to request.
2197ba8c6fd5SDavid Greenman  */
2198f7788e8eSJonathan Lemon static int
2199f7788e8eSJonathan Lemon fxp_ifmedia_upd(struct ifnet *ifp)
2200ba8c6fd5SDavid Greenman {
2201ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2202f7788e8eSJonathan Lemon 	struct mii_data *mii;
2203ba8c6fd5SDavid Greenman 
2204f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
22053212724cSJohn Baldwin 	FXP_LOCK(sc);
22065aa0cdf4SJohn-Mark Gurney 	if (mii->mii_instance) {
22075aa0cdf4SJohn-Mark Gurney 		struct mii_softc	*miisc;
22085aa0cdf4SJohn-Mark Gurney 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
22095aa0cdf4SJohn-Mark Gurney 			mii_phy_reset(miisc);
22105aa0cdf4SJohn-Mark Gurney 	}
2211f7788e8eSJonathan Lemon 	mii_mediachg(mii);
22123212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2213ba8c6fd5SDavid Greenman 	return (0);
2214ba8c6fd5SDavid Greenman }
2215ba8c6fd5SDavid Greenman 
2216ba8c6fd5SDavid Greenman /*
2217ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
2218ba8c6fd5SDavid Greenman  */
2219f7788e8eSJonathan Lemon static void
2220f7788e8eSJonathan Lemon fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2221ba8c6fd5SDavid Greenman {
2222ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
2223f7788e8eSJonathan Lemon 	struct mii_data *mii;
2224ba8c6fd5SDavid Greenman 
2225f7788e8eSJonathan Lemon 	mii = device_get_softc(sc->miibus);
22263212724cSJohn Baldwin 	FXP_LOCK(sc);
2227f7788e8eSJonathan Lemon 	mii_pollstat(mii);
2228f7788e8eSJonathan Lemon 	ifmr->ifm_active = mii->mii_media_active;
2229f7788e8eSJonathan Lemon 	ifmr->ifm_status = mii->mii_media_status;
22302e2b8238SJonathan Lemon 
22312b6fb51fSWarner Losh 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T &&
22322b6fb51fSWarner Losh 	    sc->flags & FXP_FLAG_CU_RESUME_BUG)
22332e2b8238SJonathan Lemon 		sc->cu_resume_bug = 1;
22342e2b8238SJonathan Lemon 	else
22352e2b8238SJonathan Lemon 		sc->cu_resume_bug = 0;
22363212724cSJohn Baldwin 	FXP_UNLOCK(sc);
2237ba8c6fd5SDavid Greenman }
2238ba8c6fd5SDavid Greenman 
2239a17c678eSDavid Greenman /*
2240a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
2241a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
224285050421SPyun YongHyeon  * reusing the RFA buffer.
2243a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
2244a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
2245a17c678eSDavid Greenman  */
2246a17c678eSDavid Greenman static int
224785050421SPyun YongHyeon fxp_new_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
2248a17c678eSDavid Greenman {
2249a17c678eSDavid Greenman 	struct mbuf *m;
225085050421SPyun YongHyeon 	struct fxp_rfa *rfa;
2251b2badf02SMaxime Henrion 	bus_dmamap_t tmp_map;
225285050421SPyun YongHyeon 	int error;
2253a17c678eSDavid Greenman 
2254a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
225585050421SPyun YongHyeon 	if (m == NULL)
225685050421SPyun YongHyeon 		return (ENOBUFS);
2257ba8c6fd5SDavid Greenman 
2258ba8c6fd5SDavid Greenman 	/*
2259ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
2260ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
2261ba8c6fd5SDavid Greenman 	 */
2262ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
2263ba8c6fd5SDavid Greenman 
2264eadd5e3aSDavid Greenman 	/*
2265eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
2266eadd5e3aSDavid Greenman 	 * data start past it.
2267eadd5e3aSDavid Greenman 	 */
2268a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
2269c8bca6dcSBill Paul 	m->m_data += sc->rfa_size;
227083e6547dSMaxime Henrion 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
2271eadd5e3aSDavid Greenman 
2272a17c678eSDavid Greenman 	rfa->rfa_status = 0;
227383e6547dSMaxime Henrion 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
2274a17c678eSDavid Greenman 	rfa->actual_size = 0;
227585050421SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES - RFA_ALIGNMENT_FUDGE -
227685050421SPyun YongHyeon 	    sc->rfa_size;
2277ba8c6fd5SDavid Greenman 
227828935f27SMaxime Henrion 	/*
227928935f27SMaxime Henrion 	 * Initialize the rest of the RFA.  Note that since the RFA
228028935f27SMaxime Henrion 	 * is misaligned, we cannot store values directly.  We're thus
228128935f27SMaxime Henrion 	 * using the le32enc() function which handles endianness and
228228935f27SMaxime Henrion 	 * is also alignment-safe.
228328935f27SMaxime Henrion 	 */
228483e6547dSMaxime Henrion 	le32enc(&rfa->link_addr, 0xffffffff);
228583e6547dSMaxime Henrion 	le32enc(&rfa->rbd_addr, 0xffffffff);
2286ba8c6fd5SDavid Greenman 
2287b2badf02SMaxime Henrion 	/* Map the RFA into DMA memory. */
2288b2badf02SMaxime Henrion 	error = bus_dmamap_load(sc->fxp_mtag, sc->spare_map, rfa,
2289b2badf02SMaxime Henrion 	    MCLBYTES - RFA_ALIGNMENT_FUDGE, fxp_dma_map_addr,
2290b2badf02SMaxime Henrion 	    &rxp->rx_addr, 0);
2291b2badf02SMaxime Henrion 	if (error) {
2292b2badf02SMaxime Henrion 		m_freem(m);
2293b2badf02SMaxime Henrion 		return (error);
2294b2badf02SMaxime Henrion 	}
2295b2badf02SMaxime Henrion 
2296b2badf02SMaxime Henrion 	bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map);
2297b2badf02SMaxime Henrion 	tmp_map = sc->spare_map;
2298b2badf02SMaxime Henrion 	sc->spare_map = rxp->rx_map;
2299b2badf02SMaxime Henrion 	rxp->rx_map = tmp_map;
2300b2badf02SMaxime Henrion 	rxp->rx_mbuf = m;
2301b2badf02SMaxime Henrion 
2302b983c7b3SMaxime Henrion 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
2303b983c7b3SMaxime Henrion 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
230485050421SPyun YongHyeon 	return (0);
230585050421SPyun YongHyeon }
230685050421SPyun YongHyeon 
230785050421SPyun YongHyeon static void
230885050421SPyun YongHyeon fxp_add_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
230985050421SPyun YongHyeon {
231085050421SPyun YongHyeon 	struct fxp_rfa *p_rfa;
231185050421SPyun YongHyeon 	struct fxp_rx *p_rx;
2312b2badf02SMaxime Henrion 
2313dfe61cf1SDavid Greenman 	/*
2314dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
2315dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
2316dfe61cf1SDavid Greenman 	 */
2317b2badf02SMaxime Henrion 	if (sc->fxp_desc.rx_head != NULL) {
2318b2badf02SMaxime Henrion 		p_rx = sc->fxp_desc.rx_tail;
2319b2badf02SMaxime Henrion 		p_rfa = (struct fxp_rfa *)
2320b2badf02SMaxime Henrion 		    (p_rx->rx_mbuf->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE);
2321b2badf02SMaxime Henrion 		p_rx->rx_next = rxp;
232283e6547dSMaxime Henrion 		le32enc(&p_rfa->link_addr, rxp->rx_addr);
2323aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
2324b2badf02SMaxime Henrion 		bus_dmamap_sync(sc->fxp_mtag, p_rx->rx_map,
23254cec1653SMaxime Henrion 		    BUS_DMASYNC_PREWRITE);
2326a17c678eSDavid Greenman 	} else {
2327b2badf02SMaxime Henrion 		rxp->rx_next = NULL;
2328b2badf02SMaxime Henrion 		sc->fxp_desc.rx_head = rxp;
2329a17c678eSDavid Greenman 	}
2330b2badf02SMaxime Henrion 	sc->fxp_desc.rx_tail = rxp;
233185050421SPyun YongHyeon }
233285050421SPyun YongHyeon 
233385050421SPyun YongHyeon static void
233485050421SPyun YongHyeon fxp_discard_rfabuf(struct fxp_softc *sc, struct fxp_rx *rxp)
233585050421SPyun YongHyeon {
233685050421SPyun YongHyeon 	struct mbuf *m;
233785050421SPyun YongHyeon 	struct fxp_rfa *rfa;
233885050421SPyun YongHyeon 
233985050421SPyun YongHyeon 	m = rxp->rx_mbuf;
234085050421SPyun YongHyeon 	m->m_data = m->m_ext.ext_buf;
234185050421SPyun YongHyeon 	/*
234285050421SPyun YongHyeon 	 * Move the data pointer up so that the incoming data packet
234385050421SPyun YongHyeon 	 * will be 32-bit aligned.
234485050421SPyun YongHyeon 	 */
234585050421SPyun YongHyeon 	m->m_data += RFA_ALIGNMENT_FUDGE;
234685050421SPyun YongHyeon 
234785050421SPyun YongHyeon 	/*
234885050421SPyun YongHyeon 	 * Get a pointer to the base of the mbuf cluster and move
234985050421SPyun YongHyeon 	 * data start past it.
235085050421SPyun YongHyeon 	 */
235185050421SPyun YongHyeon 	rfa = mtod(m, struct fxp_rfa *);
235285050421SPyun YongHyeon 	m->m_data += sc->rfa_size;
235385050421SPyun YongHyeon 	rfa->size = htole16(MCLBYTES - sc->rfa_size - RFA_ALIGNMENT_FUDGE);
235485050421SPyun YongHyeon 
235585050421SPyun YongHyeon 	rfa->rfa_status = 0;
235685050421SPyun YongHyeon 	rfa->rfa_control = htole16(FXP_RFA_CONTROL_EL);
235785050421SPyun YongHyeon 	rfa->actual_size = 0;
235885050421SPyun YongHyeon 
235985050421SPyun YongHyeon 	/*
236085050421SPyun YongHyeon 	 * Initialize the rest of the RFA.  Note that since the RFA
236185050421SPyun YongHyeon 	 * is misaligned, we cannot store values directly.  We're thus
236285050421SPyun YongHyeon 	 * using the le32enc() function which handles endianness and
236385050421SPyun YongHyeon 	 * is also alignment-safe.
236485050421SPyun YongHyeon 	 */
236585050421SPyun YongHyeon 	le32enc(&rfa->link_addr, 0xffffffff);
236685050421SPyun YongHyeon 	le32enc(&rfa->rbd_addr, 0xffffffff);
236785050421SPyun YongHyeon 
236885050421SPyun YongHyeon 	bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map,
236985050421SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2370a17c678eSDavid Greenman }
2371a17c678eSDavid Greenman 
2372f1928b0cSKevin Lo static int
2373f7788e8eSJonathan Lemon fxp_miibus_readreg(device_t dev, int phy, int reg)
2374dccee1a1SDavid Greenman {
2375f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2376dccee1a1SDavid Greenman 	int count = 10000;
23776ebc3153SDavid Greenman 	int value;
2378dccee1a1SDavid Greenman 
2379ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2380ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2381dccee1a1SDavid Greenman 
2382ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
2383ba8c6fd5SDavid Greenman 	    && count--)
23846ebc3153SDavid Greenman 		DELAY(10);
2385dccee1a1SDavid Greenman 
2386dccee1a1SDavid Greenman 	if (count <= 0)
2387f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_readreg: timed out\n");
2388dccee1a1SDavid Greenman 
23896ebc3153SDavid Greenman 	return (value & 0xffff);
2390dccee1a1SDavid Greenman }
2391dccee1a1SDavid Greenman 
2392dccee1a1SDavid Greenman static void
2393f7788e8eSJonathan Lemon fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
2394dccee1a1SDavid Greenman {
2395f7788e8eSJonathan Lemon 	struct fxp_softc *sc = device_get_softc(dev);
2396dccee1a1SDavid Greenman 	int count = 10000;
2397dccee1a1SDavid Greenman 
2398ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2399ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2400ba8c6fd5SDavid Greenman 	    (value & 0xffff));
2401dccee1a1SDavid Greenman 
2402ba8c6fd5SDavid Greenman 	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2403ba8c6fd5SDavid Greenman 	    count--)
24046ebc3153SDavid Greenman 		DELAY(10);
2405dccee1a1SDavid Greenman 
2406dccee1a1SDavid Greenman 	if (count <= 0)
2407f7788e8eSJonathan Lemon 		device_printf(dev, "fxp_miibus_writereg: timed out\n");
2408dccee1a1SDavid Greenman }
2409dccee1a1SDavid Greenman 
2410dccee1a1SDavid Greenman static int
2411f7788e8eSJonathan Lemon fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2412a17c678eSDavid Greenman {
24139b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
2414a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
2415f7788e8eSJonathan Lemon 	struct mii_data *mii;
241660bb79ebSPyun YongHyeon 	int flag, mask, error = 0, reinit;
2417a17c678eSDavid Greenman 
2418a17c678eSDavid Greenman 	switch (command) {
2419a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
24203212724cSJohn Baldwin 		FXP_LOCK(sc);
2421f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2422f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2423f7788e8eSJonathan Lemon 		else
2424f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2425a17c678eSDavid Greenman 
2426a17c678eSDavid Greenman 		/*
2427a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
2428a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
2429a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
2430a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
2431a17c678eSDavid Greenman 		 */
2432a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
24334953bccaSNate Lawson 			fxp_init_body(sc);
2434a17c678eSDavid Greenman 		} else {
243513f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
24364a5f1499SDavid Greenman 				fxp_stop(sc);
2437a17c678eSDavid Greenman 		}
24383212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2439a17c678eSDavid Greenman 		break;
2440a17c678eSDavid Greenman 
2441a17c678eSDavid Greenman 	case SIOCADDMULTI:
2442a17c678eSDavid Greenman 	case SIOCDELMULTI:
24433212724cSJohn Baldwin 		FXP_LOCK(sc);
2444f7788e8eSJonathan Lemon 		if (ifp->if_flags & IFF_ALLMULTI)
2445f7788e8eSJonathan Lemon 			sc->flags |= FXP_FLAG_ALL_MCAST;
2446f7788e8eSJonathan Lemon 		else
2447f7788e8eSJonathan Lemon 			sc->flags &= ~FXP_FLAG_ALL_MCAST;
2448a17c678eSDavid Greenman 		/*
2449a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
2450a17c678eSDavid Greenman 		 * accordingly.
2451a17c678eSDavid Greenman 		 */
2452f7788e8eSJonathan Lemon 		if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
2453397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
2454397f9dfeSDavid Greenman 		/*
2455f7788e8eSJonathan Lemon 		 * fxp_mc_setup() can set FXP_FLAG_ALL_MCAST, so check it
2456397f9dfeSDavid Greenman 		 * again rather than else {}.
2457397f9dfeSDavid Greenman 		 */
2458f7788e8eSJonathan Lemon 		if (sc->flags & FXP_FLAG_ALL_MCAST)
24594953bccaSNate Lawson 			fxp_init_body(sc);
24603212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2461a17c678eSDavid Greenman 		error = 0;
2462ba8c6fd5SDavid Greenman 		break;
2463ba8c6fd5SDavid Greenman 
2464ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
2465ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
2466f7788e8eSJonathan Lemon 		if (sc->miibus != NULL) {
2467f7788e8eSJonathan Lemon 			mii = device_get_softc(sc->miibus);
2468f7788e8eSJonathan Lemon                         error = ifmedia_ioctl(ifp, ifr,
2469f7788e8eSJonathan Lemon                             &mii->mii_media, command);
2470f7788e8eSJonathan Lemon 		} else {
2471ba8c6fd5SDavid Greenman                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
2472f7788e8eSJonathan Lemon 		}
2473a17c678eSDavid Greenman 		break;
2474a17c678eSDavid Greenman 
2475fb917226SRuslan Ermilov 	case SIOCSIFCAP:
247660bb79ebSPyun YongHyeon 		reinit = 0;
24778ef1f631SYaroslav Tykhiy 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
247840929967SGleb Smirnoff #ifdef DEVICE_POLLING
247940929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
248040929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
248140929967SGleb Smirnoff 				error = ether_poll_register(fxp_poll, ifp);
248240929967SGleb Smirnoff 				if (error)
248340929967SGleb Smirnoff 					return(error);
248440929967SGleb Smirnoff 				FXP_LOCK(sc);
248540929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
248640929967SGleb Smirnoff 				    FXP_SCB_INTR_DISABLE);
248740929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
248840929967SGleb Smirnoff 				FXP_UNLOCK(sc);
248940929967SGleb Smirnoff 			} else {
249040929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
249140929967SGleb Smirnoff 				/* Enable interrupts in any case */
249240929967SGleb Smirnoff 				FXP_LOCK(sc);
249340929967SGleb Smirnoff 				CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
249440929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
249540929967SGleb Smirnoff 				FXP_UNLOCK(sc);
249640929967SGleb Smirnoff 			}
249740929967SGleb Smirnoff 		}
249840929967SGleb Smirnoff #endif
249940929967SGleb Smirnoff 		FXP_LOCK(sc);
250060bb79ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
250160bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
250260bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
250360bb79ebSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
250460bb79ebSPyun YongHyeon 				ifp->if_hwassist |= FXP_CSUM_FEATURES;
250560bb79ebSPyun YongHyeon 			else
250660bb79ebSPyun YongHyeon 				ifp->if_hwassist &= ~FXP_CSUM_FEATURES;
250760bb79ebSPyun YongHyeon 		}
250860bb79ebSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
250960bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
251060bb79ebSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
251160bb79ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_MTU) != 0 &&
251260bb79ebSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0) {
25138ef1f631SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
25148ef1f631SYaroslav Tykhiy 			if (sc->revision != FXP_REV_82557)
25158ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_LONG_PKT_EN;
25168ef1f631SYaroslav Tykhiy 			else /* a hack to get long frames on the old chip */
25178ef1f631SYaroslav Tykhiy 				flag = FXP_FLAG_SAVE_BAD;
25188ef1f631SYaroslav Tykhiy 			sc->flags ^= flag;
25198ef1f631SYaroslav Tykhiy 			if (ifp->if_flags & IFF_UP)
252060bb79ebSPyun YongHyeon 				reinit++;
252160bb79ebSPyun YongHyeon 		}
252260bb79ebSPyun YongHyeon 		if (reinit > 0)
25238ef1f631SYaroslav Tykhiy 			fxp_init_body(sc);
25243212724cSJohn Baldwin 		FXP_UNLOCK(sc);
2525fb917226SRuslan Ermilov 		break;
2526fb917226SRuslan Ermilov 
2527a17c678eSDavid Greenman 	default:
2528673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2529a17c678eSDavid Greenman 	}
2530a17c678eSDavid Greenman 	return (error);
2531a17c678eSDavid Greenman }
2532397f9dfeSDavid Greenman 
2533397f9dfeSDavid Greenman /*
253409882363SJonathan Lemon  * Fill in the multicast address list and return number of entries.
253509882363SJonathan Lemon  */
253609882363SJonathan Lemon static int
253709882363SJonathan Lemon fxp_mc_addrs(struct fxp_softc *sc)
253809882363SJonathan Lemon {
253909882363SJonathan Lemon 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2540fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->ifp;
254109882363SJonathan Lemon 	struct ifmultiaddr *ifma;
254209882363SJonathan Lemon 	int nmcasts;
254309882363SJonathan Lemon 
254409882363SJonathan Lemon 	nmcasts = 0;
254509882363SJonathan Lemon 	if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
254613b203d0SRobert Watson 		IF_ADDR_LOCK(ifp);
254709882363SJonathan Lemon 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
254809882363SJonathan Lemon 			if (ifma->ifma_addr->sa_family != AF_LINK)
254909882363SJonathan Lemon 				continue;
255009882363SJonathan Lemon 			if (nmcasts >= MAXMCADDR) {
255109882363SJonathan Lemon 				sc->flags |= FXP_FLAG_ALL_MCAST;
255209882363SJonathan Lemon 				nmcasts = 0;
255309882363SJonathan Lemon 				break;
255409882363SJonathan Lemon 			}
255509882363SJonathan Lemon 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2556bafb64afSMaxime Henrion 			    &sc->mcsp->mc_addr[nmcasts][0], ETHER_ADDR_LEN);
255709882363SJonathan Lemon 			nmcasts++;
255809882363SJonathan Lemon 		}
255913b203d0SRobert Watson 		IF_ADDR_UNLOCK(ifp);
256009882363SJonathan Lemon 	}
2561bafb64afSMaxime Henrion 	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
256209882363SJonathan Lemon 	return (nmcasts);
256309882363SJonathan Lemon }
256409882363SJonathan Lemon 
256509882363SJonathan Lemon /*
2566397f9dfeSDavid Greenman  * Program the multicast filter.
2567397f9dfeSDavid Greenman  *
2568397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
2569397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
25703114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
2571397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
2572dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
2573397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
2574397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
2575397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
2576397f9dfeSDavid Greenman  *
2577397f9dfeSDavid Greenman  * This function must be called at splimp.
2578397f9dfeSDavid Greenman  */
2579397f9dfeSDavid Greenman static void
2580f7788e8eSJonathan Lemon fxp_mc_setup(struct fxp_softc *sc)
2581397f9dfeSDavid Greenman {
2582397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
2583b2badf02SMaxime Henrion 	struct fxp_tx *txp;
25847dced78aSDavid Greenman 	int count;
2585397f9dfeSDavid Greenman 
258667fc050fSMaxime Henrion 	FXP_LOCK_ASSERT(sc, MA_OWNED);
25873114fdb4SDavid Greenman 	/*
25883114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
25893114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
25903114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
25913114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
25923114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
25933114fdb4SDavid Greenman 	 */
2594397f9dfeSDavid Greenman 	if (sc->tx_queued) {
25953114fdb4SDavid Greenman 		/*
25963114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
25973114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
25983114fdb4SDavid Greenman 		 */
25993114fdb4SDavid Greenman 		if (sc->need_mcsetup)
26003114fdb4SDavid Greenman 			return;
2601397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
26023114fdb4SDavid Greenman 
26033114fdb4SDavid Greenman 		/*
260472a32a26SJonathan Lemon 		 * Add a NOP command with interrupt so that we are notified
260572a32a26SJonathan Lemon 		 * when all TX commands have been processed.
26063114fdb4SDavid Greenman 		 */
2607b2badf02SMaxime Henrion 		txp = sc->fxp_desc.tx_last->tx_next;
2608b2badf02SMaxime Henrion 		txp->tx_mbuf = NULL;
2609b2badf02SMaxime Henrion 		txp->tx_cb->cb_status = 0;
261083e6547dSMaxime Henrion 		txp->tx_cb->cb_command = htole16(FXP_CB_COMMAND_NOP |
261183e6547dSMaxime Henrion 		    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
26123114fdb4SDavid Greenman 		/*
26133114fdb4SDavid Greenman 		 * Advance the end of list forward.
26143114fdb4SDavid Greenman 		 */
261583e6547dSMaxime Henrion 		sc->fxp_desc.tx_last->tx_cb->cb_command &=
261683e6547dSMaxime Henrion 		    htole16(~FXP_CB_COMMAND_S);
26175f361cbeSMaxime Henrion 		bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2618b2badf02SMaxime Henrion 		sc->fxp_desc.tx_last = txp;
26193114fdb4SDavid Greenman 		sc->tx_queued++;
26203114fdb4SDavid Greenman 		/*
26213114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
26223114fdb4SDavid Greenman 		 */
26233114fdb4SDavid Greenman 		fxp_scb_wait(sc);
26242e2b8238SJonathan Lemon 		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
26253114fdb4SDavid Greenman 		/*
26263114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
26273114fdb4SDavid Greenman 		 * card again.
26283114fdb4SDavid Greenman 		 */
2629df79d527SGleb Smirnoff 		sc->watchdog_timer = 5;
26303114fdb4SDavid Greenman 
2631397f9dfeSDavid Greenman 		return;
2632397f9dfeSDavid Greenman 	}
2633397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2634397f9dfeSDavid Greenman 
2635397f9dfeSDavid Greenman 	/*
2636397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2637397f9dfeSDavid Greenman 	 */
2638397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
263983e6547dSMaxime Henrion 	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS |
264083e6547dSMaxime Henrion 	    FXP_CB_COMMAND_S | FXP_CB_COMMAND_I);
264183e6547dSMaxime Henrion 	mcsp->link_addr = htole32(sc->fxp_desc.cbl_addr);
2642b2badf02SMaxime Henrion 	txp = &sc->fxp_desc.mcs_tx;
2643b2badf02SMaxime Henrion 	txp->tx_mbuf = NULL;
2644b2badf02SMaxime Henrion 	txp->tx_cb = (struct fxp_cb_tx *)sc->mcsp;
2645b2badf02SMaxime Henrion 	txp->tx_next = sc->fxp_desc.tx_list;
264609882363SJonathan Lemon 	(void) fxp_mc_addrs(sc);
2647b2badf02SMaxime Henrion 	sc->fxp_desc.tx_first = sc->fxp_desc.tx_last = txp;
2648397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2649397f9dfeSDavid Greenman 
2650397f9dfeSDavid Greenman 	/*
2651397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2652397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2653397f9dfeSDavid Greenman 	 */
26547dced78aSDavid Greenman 	count = 100;
2655397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
26567dced78aSDavid Greenman 	    FXP_SCB_CUS_ACTIVE && --count)
26577dced78aSDavid Greenman 		DELAY(10);
26587dced78aSDavid Greenman 	if (count == 0) {
2659f7788e8eSJonathan Lemon 		device_printf(sc->dev, "command queue timeout\n");
26607dced78aSDavid Greenman 		return;
26617dced78aSDavid Greenman 	}
2662397f9dfeSDavid Greenman 
2663397f9dfeSDavid Greenman 	/*
2664397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2665397f9dfeSDavid Greenman 	 */
2666397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2667b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->mcs_tag, sc->mcs_map, BUS_DMASYNC_PREWRITE);
2668b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->mcs_addr);
26692e2b8238SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2670397f9dfeSDavid Greenman 
2671df79d527SGleb Smirnoff 	sc->watchdog_timer = 2;
2672397f9dfeSDavid Greenman 	return;
2673397f9dfeSDavid Greenman }
267472a32a26SJonathan Lemon 
267574d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
267674d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
267774d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
267874d1ed23SMaxime Henrion static uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
267974d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
268074d1ed23SMaxime Henrion static uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2681de571603SMaxime Henrion static uint32_t fxp_ucode_d102e[] = D102_E_RCVBUNDLE_UCODE;
268272a32a26SJonathan Lemon 
268374d1ed23SMaxime Henrion #define UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
268472a32a26SJonathan Lemon 
268572a32a26SJonathan Lemon struct ucode {
268674d1ed23SMaxime Henrion 	uint32_t	revision;
268774d1ed23SMaxime Henrion 	uint32_t	*ucode;
268872a32a26SJonathan Lemon 	int		length;
268972a32a26SJonathan Lemon 	u_short		int_delay_offset;
269072a32a26SJonathan Lemon 	u_short		bundle_max_offset;
269172a32a26SJonathan Lemon } ucode_table[] = {
269272a32a26SJonathan Lemon 	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
269372a32a26SJonathan Lemon 	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
269472a32a26SJonathan Lemon 	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
269572a32a26SJonathan Lemon 	    D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
269672a32a26SJonathan Lemon 	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
269772a32a26SJonathan Lemon 	    D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
269872a32a26SJonathan Lemon 	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
269972a32a26SJonathan Lemon 	    D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
270072a32a26SJonathan Lemon 	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
270172a32a26SJonathan Lemon 	    D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2702507feeafSMaxime Henrion 	{ FXP_REV_82551_F, UCODE(fxp_ucode_d102e),
2703de571603SMaxime Henrion 	    D102_E_CPUSAVER_DWORD, D102_E_CPUSAVER_BUNDLE_MAX_DWORD },
270472a32a26SJonathan Lemon 	{ 0, NULL, 0, 0, 0 }
270572a32a26SJonathan Lemon };
270672a32a26SJonathan Lemon 
270772a32a26SJonathan Lemon static void
270872a32a26SJonathan Lemon fxp_load_ucode(struct fxp_softc *sc)
270972a32a26SJonathan Lemon {
271072a32a26SJonathan Lemon 	struct ucode *uc;
271172a32a26SJonathan Lemon 	struct fxp_cb_ucode *cbp;
271294a4f968SPyun YongHyeon 	int i;
271372a32a26SJonathan Lemon 
271472a32a26SJonathan Lemon 	for (uc = ucode_table; uc->ucode != NULL; uc++)
271572a32a26SJonathan Lemon 		if (sc->revision == uc->revision)
271672a32a26SJonathan Lemon 			break;
271772a32a26SJonathan Lemon 	if (uc->ucode == NULL)
271872a32a26SJonathan Lemon 		return;
2719b2badf02SMaxime Henrion 	cbp = (struct fxp_cb_ucode *)sc->fxp_desc.cbl_list;
272072a32a26SJonathan Lemon 	cbp->cb_status = 0;
272183e6547dSMaxime Henrion 	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
272283e6547dSMaxime Henrion 	cbp->link_addr = 0xffffffff;    	/* (no) next command */
272394a4f968SPyun YongHyeon 	for (i = 0; i < uc->length; i++)
272494a4f968SPyun YongHyeon 		cbp->ucode[i] = htole32(uc->ucode[i]);
272572a32a26SJonathan Lemon 	if (uc->int_delay_offset)
272674d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->int_delay_offset] =
272783e6547dSMaxime Henrion 		    htole16(sc->tunable_int_delay + sc->tunable_int_delay / 2);
272872a32a26SJonathan Lemon 	if (uc->bundle_max_offset)
272974d1ed23SMaxime Henrion 		*(uint16_t *)&cbp->ucode[uc->bundle_max_offset] =
273083e6547dSMaxime Henrion 		    htole16(sc->tunable_bundle_max);
273172a32a26SJonathan Lemon 	/*
273272a32a26SJonathan Lemon 	 * Download the ucode to the chip.
273372a32a26SJonathan Lemon 	 */
273472a32a26SJonathan Lemon 	fxp_scb_wait(sc);
2735b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_PREWRITE);
2736b2badf02SMaxime Henrion 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->fxp_desc.cbl_addr);
273772a32a26SJonathan Lemon 	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
273872a32a26SJonathan Lemon 	/* ...and wait for it to complete. */
2739209b07bcSMaxime Henrion 	fxp_dma_wait(sc, &cbp->cb_status, sc->cbl_tag, sc->cbl_map);
2740b2badf02SMaxime Henrion 	bus_dmamap_sync(sc->cbl_tag, sc->cbl_map, BUS_DMASYNC_POSTWRITE);
274172a32a26SJonathan Lemon 	device_printf(sc->dev,
274272a32a26SJonathan Lemon 	    "Microcode loaded, int_delay: %d usec  bundle_max: %d\n",
274372a32a26SJonathan Lemon 	    sc->tunable_int_delay,
274472a32a26SJonathan Lemon 	    uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
274572a32a26SJonathan Lemon 	sc->flags |= FXP_FLAG_UCODE;
274672a32a26SJonathan Lemon }
274772a32a26SJonathan Lemon 
274872a32a26SJonathan Lemon static int
274972a32a26SJonathan Lemon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
275072a32a26SJonathan Lemon {
275172a32a26SJonathan Lemon 	int error, value;
275272a32a26SJonathan Lemon 
275372a32a26SJonathan Lemon 	value = *(int *)arg1;
275472a32a26SJonathan Lemon 	error = sysctl_handle_int(oidp, &value, 0, req);
275572a32a26SJonathan Lemon 	if (error || !req->newptr)
275672a32a26SJonathan Lemon 		return (error);
275772a32a26SJonathan Lemon 	if (value < low || value > high)
275872a32a26SJonathan Lemon 		return (EINVAL);
275972a32a26SJonathan Lemon 	*(int *)arg1 = value;
276072a32a26SJonathan Lemon 	return (0);
276172a32a26SJonathan Lemon }
276272a32a26SJonathan Lemon 
276372a32a26SJonathan Lemon /*
276472a32a26SJonathan Lemon  * Interrupt delay is expressed in microseconds, a multiplier is used
276572a32a26SJonathan Lemon  * to convert this to the appropriate clock ticks before using.
276672a32a26SJonathan Lemon  */
276772a32a26SJonathan Lemon static int
276872a32a26SJonathan Lemon sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
276972a32a26SJonathan Lemon {
277072a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
277172a32a26SJonathan Lemon }
277272a32a26SJonathan Lemon 
277372a32a26SJonathan Lemon static int
277472a32a26SJonathan Lemon sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
277572a32a26SJonathan Lemon {
277672a32a26SJonathan Lemon 	return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
277772a32a26SJonathan Lemon }
2778