xref: /freebsd/sys/dev/fxp/if_fxp.c (revision e9bf2fa7b3afa03b18ba99c969acef3bd3d94797)
1a17c678eSDavid Greenman /*
2a17c678eSDavid Greenman  * Copyright (c) 1995, David Greenman
3a17c678eSDavid Greenman  * All rights reserved.
4a17c678eSDavid Greenman  *
5ba8c6fd5SDavid Greenman  * Modifications to support NetBSD and media selection:
6ba8c6fd5SDavid Greenman  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
7ba8c6fd5SDavid Greenman  *
8a17c678eSDavid Greenman  * Redistribution and use in source and binary forms, with or without
9a17c678eSDavid Greenman  * modification, are permitted provided that the following conditions
10a17c678eSDavid Greenman  * are met:
11a17c678eSDavid Greenman  * 1. Redistributions of source code must retain the above copyright
12a17c678eSDavid Greenman  *    notice unmodified, this list of conditions, and the following
13a17c678eSDavid Greenman  *    disclaimer.
14a17c678eSDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
15a17c678eSDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
16a17c678eSDavid Greenman  *    documentation and/or other materials provided with the distribution.
17a17c678eSDavid Greenman  *
18a17c678eSDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a17c678eSDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a17c678eSDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a17c678eSDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a17c678eSDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a17c678eSDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a17c678eSDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a17c678eSDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a17c678eSDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a17c678eSDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a17c678eSDavid Greenman  * SUCH DAMAGE.
29a17c678eSDavid Greenman  *
30c3aac50fSPeter Wemm  * $FreeBSD$
31a17c678eSDavid Greenman  */
32a17c678eSDavid Greenman 
33a17c678eSDavid Greenman /*
34ae12cddaSDavid Greenman  * Intel EtherExpress Pro/100B PCI Fast Ethernet driver
35a17c678eSDavid Greenman  */
36a17c678eSDavid Greenman 
37a17c678eSDavid Greenman #include <sys/param.h>
38a17c678eSDavid Greenman #include <sys/systm.h>
39a17c678eSDavid Greenman #include <sys/mbuf.h>
40a17c678eSDavid Greenman #include <sys/malloc.h>
41a17c678eSDavid Greenman #include <sys/kernel.h>
424458ac71SBruce Evans #include <sys/socket.h>
43a17c678eSDavid Greenman 
44a17c678eSDavid Greenman #include <net/if.h>
45397f9dfeSDavid Greenman #include <net/if_dl.h>
46ba8c6fd5SDavid Greenman #include <net/if_media.h>
47a17c678eSDavid Greenman 
48a17c678eSDavid Greenman #ifdef NS
49a17c678eSDavid Greenman #include <netns/ns.h>
50a17c678eSDavid Greenman #include <netns/ns_if.h>
51a17c678eSDavid Greenman #endif
52a17c678eSDavid Greenman 
53a17c678eSDavid Greenman #include <net/bpf.h>
54a17c678eSDavid Greenman 
55ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
56ba8c6fd5SDavid Greenman 
57ba8c6fd5SDavid Greenman #include <sys/ioctl.h>
58ba8c6fd5SDavid Greenman #include <sys/errno.h>
59ba8c6fd5SDavid Greenman #include <sys/device.h>
60ba8c6fd5SDavid Greenman 
61ba8c6fd5SDavid Greenman #include <net/if_dl.h>
62ba8c6fd5SDavid Greenman #include <net/if_ether.h>
63ba8c6fd5SDavid Greenman 
64ba8c6fd5SDavid Greenman #include <netinet/if_inarp.h>
65ba8c6fd5SDavid Greenman 
66ba8c6fd5SDavid Greenman #include <vm/vm.h>
67ba8c6fd5SDavid Greenman 
68ba8c6fd5SDavid Greenman #include <machine/cpu.h>
69ba8c6fd5SDavid Greenman #include <machine/bus.h>
70ba8c6fd5SDavid Greenman #include <machine/intr.h>
71ba8c6fd5SDavid Greenman 
72ba8c6fd5SDavid Greenman #include <dev/pci/if_fxpreg.h>
73ba8c6fd5SDavid Greenman #include <dev/pci/if_fxpvar.h>
74ba8c6fd5SDavid Greenman 
75ba8c6fd5SDavid Greenman #include <dev/pci/pcivar.h>
76ba8c6fd5SDavid Greenman #include <dev/pci/pcireg.h>
77ba8c6fd5SDavid Greenman #include <dev/pci/pcidevs.h>
78ba8c6fd5SDavid Greenman 
79ba8c6fd5SDavid Greenman 
80ba8c6fd5SDavid Greenman #else /* __FreeBSD__ */
81ba8c6fd5SDavid Greenman 
82ba8c6fd5SDavid Greenman #include <sys/sockio.h>
836182fdbdSPeter Wemm #include <sys/bus.h>
846182fdbdSPeter Wemm #include <machine/bus.h>
856182fdbdSPeter Wemm #include <sys/rman.h>
866182fdbdSPeter Wemm #include <machine/resource.h>
87ba8c6fd5SDavid Greenman 
881d5e9e22SEivind Eklund #include <net/ethernet.h>
891d5e9e22SEivind Eklund #include <net/if_arp.h>
90ba8c6fd5SDavid Greenman 
91dfe61cf1SDavid Greenman #include <vm/vm.h>		/* for vtophys */
92efeaf95aSDavid Greenman #include <vm/pmap.h>		/* for vtophys */
93dfe61cf1SDavid Greenman #include <machine/clock.h>	/* for DELAY */
94a17c678eSDavid Greenman 
95a17c678eSDavid Greenman #include <pci/pcivar.h>
96df373873SWes Peters #include <pci/pcireg.h>		/* for PCIM_CMD_xxx */
97a17c678eSDavid Greenman #include <pci/if_fxpreg.h>
98ba8c6fd5SDavid Greenman #include <pci/if_fxpvar.h>
99a17c678eSDavid Greenman 
100ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
101a17c678eSDavid Greenman 
1024fc1dda9SAndrew Gallatin #ifdef __alpha__		/* XXX */
1034fc1dda9SAndrew Gallatin /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
1044fc1dda9SAndrew Gallatin #undef vtophys
1054fc1dda9SAndrew Gallatin #define	vtophys(va)	alpha_XXX_dmamap((vm_offset_t)(va))
1064fc1dda9SAndrew Gallatin #endif /* __alpha__ */
1074fc1dda9SAndrew Gallatin 
1084fc1dda9SAndrew Gallatin 
109ab090e5bSLuigi Rizzo #include "opt_bdg.h"
110ab090e5bSLuigi Rizzo #ifdef BRIDGE
111ab090e5bSLuigi Rizzo #include <net/if_types.h>
112ab090e5bSLuigi Rizzo #include <net/bridge.h>
113ab090e5bSLuigi Rizzo #endif
114ab090e5bSLuigi Rizzo 
115ba8c6fd5SDavid Greenman /*
116ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
117ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
118ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
119ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
120ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
121ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
122ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
123ba8c6fd5SDavid Greenman  */
124ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
125ba8c6fd5SDavid Greenman 
126ba8c6fd5SDavid Greenman /*
127ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
128ba8c6fd5SDavid Greenman  */
129ba8c6fd5SDavid Greenman static __inline void fxp_lwcopy __P((volatile u_int32_t *,
130ba8c6fd5SDavid Greenman 	volatile u_int32_t *));
131ba8c6fd5SDavid Greenman static __inline void
132ba8c6fd5SDavid Greenman fxp_lwcopy(src, dst)
133ba8c6fd5SDavid Greenman 	volatile u_int32_t *src, *dst;
134ba8c6fd5SDavid Greenman {
135fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
136fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
137ba8c6fd5SDavid Greenman 
138ba8c6fd5SDavid Greenman 	b[0] = a[0];
139ba8c6fd5SDavid Greenman 	b[1] = a[1];
140ba8c6fd5SDavid Greenman }
141a17c678eSDavid Greenman 
142a17c678eSDavid Greenman /*
143a17c678eSDavid Greenman  * Template for default configuration parameters.
144a17c678eSDavid Greenman  * See struct fxp_cb_config for the bit definitions.
145a17c678eSDavid Greenman  */
146a17c678eSDavid Greenman static u_char fxp_cb_config_template[] = {
147a17c678eSDavid Greenman 	0x0, 0x0,		/* cb_status */
148a17c678eSDavid Greenman 	0x80, 0x2,		/* cb_command */
149a17c678eSDavid Greenman 	0xff, 0xff, 0xff, 0xff,	/* link_addr */
150a17c678eSDavid Greenman 	0x16,	/*  0 */
151a17c678eSDavid Greenman 	0x8,	/*  1 */
152a17c678eSDavid Greenman 	0x0,	/*  2 */
153a17c678eSDavid Greenman 	0x0,	/*  3 */
154a17c678eSDavid Greenman 	0x0,	/*  4 */
155a17c678eSDavid Greenman 	0x80,	/*  5 */
156a17c678eSDavid Greenman 	0xb2,	/*  6 */
157a17c678eSDavid Greenman 	0x3,	/*  7 */
158a17c678eSDavid Greenman 	0x1,	/*  8 */
159a17c678eSDavid Greenman 	0x0,	/*  9 */
160a17c678eSDavid Greenman 	0x26,	/* 10 */
161a17c678eSDavid Greenman 	0x0,	/* 11 */
162a17c678eSDavid Greenman 	0x60,	/* 12 */
163a17c678eSDavid Greenman 	0x0,	/* 13 */
164a17c678eSDavid Greenman 	0xf2,	/* 14 */
165a17c678eSDavid Greenman 	0x48,	/* 15 */
166a17c678eSDavid Greenman 	0x0,	/* 16 */
167a17c678eSDavid Greenman 	0x40,	/* 17 */
168a17c678eSDavid Greenman 	0xf3,	/* 18 */
169a17c678eSDavid Greenman 	0x0,	/* 19 */
170a17c678eSDavid Greenman 	0x3f,	/* 20 */
171397f9dfeSDavid Greenman 	0x5	/* 21 */
172a17c678eSDavid Greenman };
173a17c678eSDavid Greenman 
174ba8c6fd5SDavid Greenman /* Supported media types. */
175ba8c6fd5SDavid Greenman struct fxp_supported_media {
176ba8c6fd5SDavid Greenman 	const int	fsm_phy;	/* PHY type */
177ba8c6fd5SDavid Greenman 	const int	*fsm_media;	/* the media array */
178ba8c6fd5SDavid Greenman 	const int	fsm_nmedia;	/* the number of supported media */
179ba8c6fd5SDavid Greenman 	const int	fsm_defmedia;	/* default media for this PHY */
180ba8c6fd5SDavid Greenman };
181ba8c6fd5SDavid Greenman 
182303b270bSEivind Eklund static const int fxp_media_standard[] = {
183ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_10_T,
184ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_10_T|IFM_FDX,
185ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_100_TX,
186ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_100_TX|IFM_FDX,
187ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_AUTO,
188ba8c6fd5SDavid Greenman };
189ba8c6fd5SDavid Greenman #define	FXP_MEDIA_STANDARD_DEFMEDIA	(IFM_ETHER|IFM_AUTO)
190ba8c6fd5SDavid Greenman 
191303b270bSEivind Eklund static const int fxp_media_default[] = {
192ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_MANUAL,		/* XXX IFM_AUTO ? */
193ba8c6fd5SDavid Greenman };
194ba8c6fd5SDavid Greenman #define	FXP_MEDIA_DEFAULT_DEFMEDIA	(IFM_ETHER|IFM_MANUAL)
195ba8c6fd5SDavid Greenman 
196303b270bSEivind Eklund static const struct fxp_supported_media fxp_media[] = {
197ba8c6fd5SDavid Greenman 	{ FXP_PHY_DP83840, fxp_media_standard,
198ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
199ba8c6fd5SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
200ba8c6fd5SDavid Greenman 	{ FXP_PHY_DP83840A, fxp_media_standard,
201ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
202ba8c6fd5SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
20392924291SDavid Greenman 	{ FXP_PHY_82553A, fxp_media_standard,
20492924291SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
20592924291SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
20692924291SDavid Greenman 	{ FXP_PHY_82553C, fxp_media_standard,
20792924291SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
20892924291SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
209ba8c6fd5SDavid Greenman 	{ FXP_PHY_82555, fxp_media_standard,
210ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
211ba8c6fd5SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
21292924291SDavid Greenman 	{ FXP_PHY_82555B, fxp_media_standard,
21392924291SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
21492924291SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
215ba8c6fd5SDavid Greenman 	{ FXP_PHY_80C24, fxp_media_default,
216ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_default) / sizeof(fxp_media_default[0]),
217ba8c6fd5SDavid Greenman 	  FXP_MEDIA_DEFAULT_DEFMEDIA },
218ba8c6fd5SDavid Greenman };
219ba8c6fd5SDavid Greenman #define	NFXPMEDIA (sizeof(fxp_media) / sizeof(fxp_media[0]))
220ba8c6fd5SDavid Greenman 
221ba8c6fd5SDavid Greenman static int fxp_mediachange	__P((struct ifnet *));
222ba8c6fd5SDavid Greenman static void fxp_mediastatus	__P((struct ifnet *, struct ifmediareq *));
223303b270bSEivind Eklund static void fxp_set_media	__P((struct fxp_softc *, int));
224c1087c13SBruce Evans static __inline void fxp_scb_wait __P((struct fxp_softc *));
225ba8c6fd5SDavid Greenman static FXP_INTR_TYPE fxp_intr	__P((void *));
226a17c678eSDavid Greenman static void fxp_start		__P((struct ifnet *));
227ba8c6fd5SDavid Greenman static int fxp_ioctl		__P((struct ifnet *,
228ba8c6fd5SDavid Greenman 				     FXP_IOCTLCMD_TYPE, caddr_t));
229fb583156SDavid Greenman static void fxp_init		__P((void *));
2304a5f1499SDavid Greenman static void fxp_stop		__P((struct fxp_softc *));
2314a5f1499SDavid Greenman static void fxp_watchdog	__P((struct ifnet *));
232a17c678eSDavid Greenman static int fxp_add_rfabuf	__P((struct fxp_softc *, struct mbuf *));
233ba8c6fd5SDavid Greenman static int fxp_mdi_read		__P((struct fxp_softc *, int, int));
234ba8c6fd5SDavid Greenman static void fxp_mdi_write	__P((struct fxp_softc *, int, int, int));
235e9bf2fa7SDavid Greenman static void fxp_autosize_eeprom __P((struct fxp_softc *));
236ba8c6fd5SDavid Greenman static void fxp_read_eeprom	__P((struct fxp_softc *, u_int16_t *,
237ba8c6fd5SDavid Greenman 				     int, int));
238ba8c6fd5SDavid Greenman static int fxp_attach_common	__P((struct fxp_softc *, u_int8_t *));
239303b270bSEivind Eklund static void fxp_stats_update	__P((void *));
240397f9dfeSDavid Greenman static void fxp_mc_setup	__P((struct fxp_softc *));
241a17c678eSDavid Greenman 
242a17c678eSDavid Greenman /*
243f9be9005SDavid Greenman  * Set initial transmit threshold at 64 (512 bytes). This is
244f9be9005SDavid Greenman  * increased by 64 (512 bytes) at a time, to maximum of 192
245f9be9005SDavid Greenman  * (1536 bytes), if an underrun occurs.
246f9be9005SDavid Greenman  */
247f9be9005SDavid Greenman static int tx_threshold = 64;
248f9be9005SDavid Greenman 
249f9be9005SDavid Greenman /*
250a17c678eSDavid Greenman  * Number of transmit control blocks. This determines the number
251a17c678eSDavid Greenman  * of transmit buffers that can be chained in the CB list.
252a17c678eSDavid Greenman  * This must be a power of two.
253a17c678eSDavid Greenman  */
2541cd443acSDavid Greenman #define FXP_NTXCB	128
255a17c678eSDavid Greenman 
256a17c678eSDavid Greenman /*
2573114fdb4SDavid Greenman  * Number of completed TX commands at which point an interrupt
2583114fdb4SDavid Greenman  * will be generated to garbage collect the attached buffers.
2593114fdb4SDavid Greenman  * Must be at least one less than FXP_NTXCB, and should be
2603114fdb4SDavid Greenman  * enough less so that the transmitter doesn't becomes idle
2613114fdb4SDavid Greenman  * during the buffer rundown (which would reduce performance).
2623114fdb4SDavid Greenman  */
2633114fdb4SDavid Greenman #define FXP_CXINT_THRESH 120
2643114fdb4SDavid Greenman 
2653114fdb4SDavid Greenman /*
266a17c678eSDavid Greenman  * TxCB list index mask. This is used to do list wrap-around.
267a17c678eSDavid Greenman  */
268a17c678eSDavid Greenman #define FXP_TXCB_MASK	(FXP_NTXCB - 1)
269a17c678eSDavid Greenman 
270a17c678eSDavid Greenman /*
271a17c678eSDavid Greenman  * Number of receive frame area buffers. These are large so chose
272a17c678eSDavid Greenman  * wisely.
273a17c678eSDavid Greenman  */
2746f5818b0SDavid Greenman #define FXP_NRFABUFS	64
275a17c678eSDavid Greenman 
276dfe61cf1SDavid Greenman /*
277397f9dfeSDavid Greenman  * Maximum number of seconds that the receiver can be idle before we
278397f9dfeSDavid Greenman  * assume it's dead and attempt to reset it by reprogramming the
279397f9dfeSDavid Greenman  * multicast filter. This is part of a work-around for a bug in the
280397f9dfeSDavid Greenman  * NIC. See fxp_stats_update().
281397f9dfeSDavid Greenman  */
282397f9dfeSDavid Greenman #define FXP_MAX_RX_IDLE	15
283397f9dfeSDavid Greenman 
284397f9dfeSDavid Greenman /*
285dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
286dfe61cf1SDavid Greenman  * completed).
287dfe61cf1SDavid Greenman  */
288c1087c13SBruce Evans static __inline void
289ba8c6fd5SDavid Greenman fxp_scb_wait(sc)
290ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
291a17c678eSDavid Greenman {
292a17c678eSDavid Greenman 	int i = 10000;
293a17c678eSDavid Greenman 
294397f9dfeSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i);
295a17c678eSDavid Greenman }
296a17c678eSDavid Greenman 
297ba8c6fd5SDavid Greenman /*************************************************************
298ba8c6fd5SDavid Greenman  * Operating system-specific autoconfiguration glue
299ba8c6fd5SDavid Greenman  *************************************************************/
300ba8c6fd5SDavid Greenman 
301ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
302ba8c6fd5SDavid Greenman 
303ba8c6fd5SDavid Greenman #ifdef __BROKEN_INDIRECT_CONFIG
304ba8c6fd5SDavid Greenman static int fxp_match __P((struct device *, void *, void *));
305ba8c6fd5SDavid Greenman #else
306ba8c6fd5SDavid Greenman static int fxp_match __P((struct device *, struct cfdata *, void *));
307ba8c6fd5SDavid Greenman #endif
308ba8c6fd5SDavid Greenman static void fxp_attach __P((struct device *, struct device *, void *));
309ba8c6fd5SDavid Greenman 
310ba8c6fd5SDavid Greenman static void	fxp_shutdown __P((void *));
311ba8c6fd5SDavid Greenman 
312ba8c6fd5SDavid Greenman /* Compensate for lack of a generic ether_ioctl() */
313ba8c6fd5SDavid Greenman static int	fxp_ether_ioctl __P((struct ifnet *,
314ba8c6fd5SDavid Greenman 				    FXP_IOCTLCMD_TYPE, caddr_t));
315ba8c6fd5SDavid Greenman #define	ether_ioctl	fxp_ether_ioctl
316ba8c6fd5SDavid Greenman 
317ba8c6fd5SDavid Greenman struct cfattach fxp_ca = {
318ba8c6fd5SDavid Greenman 	sizeof(struct fxp_softc), fxp_match, fxp_attach
319ba8c6fd5SDavid Greenman };
320ba8c6fd5SDavid Greenman 
321ba8c6fd5SDavid Greenman struct cfdriver fxp_cd = {
322ba8c6fd5SDavid Greenman 	NULL, "fxp", DV_IFNET
323ba8c6fd5SDavid Greenman };
324ba8c6fd5SDavid Greenman 
325ba8c6fd5SDavid Greenman /*
326ba8c6fd5SDavid Greenman  * Check if a device is an 82557.
327ba8c6fd5SDavid Greenman  */
328ba8c6fd5SDavid Greenman static int
329ba8c6fd5SDavid Greenman fxp_match(parent, match, aux)
330ba8c6fd5SDavid Greenman 	struct device *parent;
331ba8c6fd5SDavid Greenman #ifdef __BROKEN_INDIRECT_CONFIG
332ba8c6fd5SDavid Greenman 	void *match;
333ba8c6fd5SDavid Greenman #else
334ba8c6fd5SDavid Greenman 	struct cfdata *match;
335ba8c6fd5SDavid Greenman #endif
336ba8c6fd5SDavid Greenman 	void *aux;
337ba8c6fd5SDavid Greenman {
338ba8c6fd5SDavid Greenman 	struct pci_attach_args *pa = aux;
339ba8c6fd5SDavid Greenman 
340ba8c6fd5SDavid Greenman 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
341ba8c6fd5SDavid Greenman 		return (0);
342ba8c6fd5SDavid Greenman 
343ba8c6fd5SDavid Greenman 	switch (PCI_PRODUCT(pa->pa_id)) {
344ba8c6fd5SDavid Greenman 	case PCI_PRODUCT_INTEL_82557:
345ba8c6fd5SDavid Greenman 		return (1);
346ba8c6fd5SDavid Greenman 	}
347ba8c6fd5SDavid Greenman 
348ba8c6fd5SDavid Greenman 	return (0);
349ba8c6fd5SDavid Greenman }
350ba8c6fd5SDavid Greenman 
351ba8c6fd5SDavid Greenman static void
352ba8c6fd5SDavid Greenman fxp_attach(parent, self, aux)
353ba8c6fd5SDavid Greenman 	struct device *parent, *self;
354ba8c6fd5SDavid Greenman 	void *aux;
355ba8c6fd5SDavid Greenman {
356ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = (struct fxp_softc *)self;
357ba8c6fd5SDavid Greenman 	struct pci_attach_args *pa = aux;
358ba8c6fd5SDavid Greenman 	pci_chipset_tag_t pc = pa->pa_pc;
359ba8c6fd5SDavid Greenman 	pci_intr_handle_t ih;
360ba8c6fd5SDavid Greenman 	const char *intrstr = NULL;
361ba8c6fd5SDavid Greenman 	u_int8_t enaddr[6];
362ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
363ba8c6fd5SDavid Greenman 
364ba8c6fd5SDavid Greenman 	/*
365ba8c6fd5SDavid Greenman 	 * Map control/status registers.
366ba8c6fd5SDavid Greenman 	 */
367ba8c6fd5SDavid Greenman 	if (pci_mapreg_map(pa, FXP_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0,
368ba8c6fd5SDavid Greenman 	    &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
369ba8c6fd5SDavid Greenman 		printf(": can't map registers\n");
370ba8c6fd5SDavid Greenman 		return;
371ba8c6fd5SDavid Greenman 	}
372ba8c6fd5SDavid Greenman 	printf(": Intel EtherExpress Pro 10/100B Ethernet\n");
373ba8c6fd5SDavid Greenman 
374ba8c6fd5SDavid Greenman 	/*
375ba8c6fd5SDavid Greenman 	 * Allocate our interrupt.
376ba8c6fd5SDavid Greenman 	 */
377ba8c6fd5SDavid Greenman 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
378ba8c6fd5SDavid Greenman 	    pa->pa_intrline, &ih)) {
379ba8c6fd5SDavid Greenman 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
380ba8c6fd5SDavid Greenman 		return;
381ba8c6fd5SDavid Greenman 	}
382ba8c6fd5SDavid Greenman 	intrstr = pci_intr_string(pc, ih);
383ba8c6fd5SDavid Greenman 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
384ba8c6fd5SDavid Greenman 	if (sc->sc_ih == NULL) {
385ba8c6fd5SDavid Greenman 		printf("%s: couldn't establish interrupt",
386ba8c6fd5SDavid Greenman 		    sc->sc_dev.dv_xname);
387ba8c6fd5SDavid Greenman 		if (intrstr != NULL)
388ba8c6fd5SDavid Greenman 			printf(" at %s", intrstr);
389ba8c6fd5SDavid Greenman 		printf("\n");
390ba8c6fd5SDavid Greenman 		return;
391ba8c6fd5SDavid Greenman 	}
392ba8c6fd5SDavid Greenman 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
393ba8c6fd5SDavid Greenman 
394ba8c6fd5SDavid Greenman 	/* Do generic parts of attach. */
395ba8c6fd5SDavid Greenman 	if (fxp_attach_common(sc, enaddr)) {
396ba8c6fd5SDavid Greenman 		/* Failed! */
397ba8c6fd5SDavid Greenman 		return;
398ba8c6fd5SDavid Greenman 	}
399ba8c6fd5SDavid Greenman 
400ba8c6fd5SDavid Greenman 	printf("%s: Ethernet address %s%s\n", sc->sc_dev.dv_xname,
401ba8c6fd5SDavid Greenman 	    ether_sprintf(enaddr), sc->phy_10Mbps_only ? ", 10Mbps" : "");
402ba8c6fd5SDavid Greenman 
403ba8c6fd5SDavid Greenman 	ifp = &sc->sc_ethercom.ec_if;
404ba8c6fd5SDavid Greenman 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
405ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
406ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
407ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
408ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
409ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
410ba8c6fd5SDavid Greenman 
411ba8c6fd5SDavid Greenman 	/*
412ba8c6fd5SDavid Greenman 	 * Attach the interface.
413ba8c6fd5SDavid Greenman 	 */
414ba8c6fd5SDavid Greenman 	if_attach(ifp);
415483b9871SDavid Greenman 	/*
4163114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
4173114fdb4SDavid Greenman 	 * TX descriptors.
418483b9871SDavid Greenman 	 */
4193114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
420ba8c6fd5SDavid Greenman 	ether_ifattach(ifp, enaddr);
421ba8c6fd5SDavid Greenman 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
422ba8c6fd5SDavid Greenman 	    sizeof(struct ether_header));
423ba8c6fd5SDavid Greenman 
424ba8c6fd5SDavid Greenman 	/*
425ba8c6fd5SDavid Greenman 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
426ba8c6fd5SDavid Greenman 	 * doing do could allow DMA to corrupt kernel memory during the
427ba8c6fd5SDavid Greenman 	 * reboot before the driver initializes.
428ba8c6fd5SDavid Greenman 	 */
429ba8c6fd5SDavid Greenman 	shutdownhook_establish(fxp_shutdown, sc);
430ba8c6fd5SDavid Greenman }
431ba8c6fd5SDavid Greenman 
432ba8c6fd5SDavid Greenman /*
433ba8c6fd5SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
434ba8c6fd5SDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
435ba8c6fd5SDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
436ba8c6fd5SDavid Greenman  */
437ba8c6fd5SDavid Greenman static void
438ba8c6fd5SDavid Greenman fxp_shutdown(sc)
439ba8c6fd5SDavid Greenman 	void *sc;
440ba8c6fd5SDavid Greenman {
441ba8c6fd5SDavid Greenman 	fxp_stop((struct fxp_softc *) sc);
442ba8c6fd5SDavid Greenman }
443ba8c6fd5SDavid Greenman 
444ba8c6fd5SDavid Greenman static int
445ba8c6fd5SDavid Greenman fxp_ether_ioctl(ifp, cmd, data)
446ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
447ba8c6fd5SDavid Greenman 	FXP_IOCTLCMD_TYPE cmd;
448ba8c6fd5SDavid Greenman 	caddr_t data;
449ba8c6fd5SDavid Greenman {
450ba8c6fd5SDavid Greenman 	struct ifaddr *ifa = (struct ifaddr *) data;
451ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
452ba8c6fd5SDavid Greenman 
453ba8c6fd5SDavid Greenman 	switch (cmd) {
454ba8c6fd5SDavid Greenman 	case SIOCSIFADDR:
455ba8c6fd5SDavid Greenman 		ifp->if_flags |= IFF_UP;
456ba8c6fd5SDavid Greenman 
457ba8c6fd5SDavid Greenman 		switch (ifa->ifa_addr->sa_family) {
458ba8c6fd5SDavid Greenman #ifdef INET
459ba8c6fd5SDavid Greenman 		case AF_INET:
460ba8c6fd5SDavid Greenman 			fxp_init(sc);
461ba8c6fd5SDavid Greenman 			arp_ifinit(ifp, ifa);
462ba8c6fd5SDavid Greenman 			break;
463ba8c6fd5SDavid Greenman #endif
464ba8c6fd5SDavid Greenman #ifdef NS
465ba8c6fd5SDavid Greenman 		case AF_NS:
466ba8c6fd5SDavid Greenman 		    {
467ba8c6fd5SDavid Greenman 			 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
468ba8c6fd5SDavid Greenman 
469ba8c6fd5SDavid Greenman 			 if (ns_nullhost(*ina))
470ba8c6fd5SDavid Greenman 				ina->x_host = *(union ns_host *)
471ba8c6fd5SDavid Greenman 				    LLADDR(ifp->if_sadl);
472ba8c6fd5SDavid Greenman 			 else
473ba8c6fd5SDavid Greenman 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
474ba8c6fd5SDavid Greenman 				    ifp->if_addrlen);
475ba8c6fd5SDavid Greenman 			 /* Set new address. */
476ba8c6fd5SDavid Greenman 			 fxp_init(sc);
477ba8c6fd5SDavid Greenman 			 break;
478ba8c6fd5SDavid Greenman 		    }
479ba8c6fd5SDavid Greenman #endif
480ba8c6fd5SDavid Greenman 		default:
481ba8c6fd5SDavid Greenman 			fxp_init(sc);
482ba8c6fd5SDavid Greenman 			break;
483ba8c6fd5SDavid Greenman 		}
484ba8c6fd5SDavid Greenman 		break;
485ba8c6fd5SDavid Greenman 
486ba8c6fd5SDavid Greenman 	default:
487ba8c6fd5SDavid Greenman 		return (EINVAL);
488ba8c6fd5SDavid Greenman 	}
489ba8c6fd5SDavid Greenman 
490ba8c6fd5SDavid Greenman 	return (0);
491ba8c6fd5SDavid Greenman }
492ba8c6fd5SDavid Greenman 
493ba8c6fd5SDavid Greenman #else /* __FreeBSD__ */
494ba8c6fd5SDavid Greenman 
495dfe61cf1SDavid Greenman /*
496dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
497dfe61cf1SDavid Greenman  */
4986182fdbdSPeter Wemm static int
4996182fdbdSPeter Wemm fxp_probe(device_t dev)
500a17c678eSDavid Greenman {
5016182fdbdSPeter Wemm 	if ((pci_get_vendor(dev) == FXP_VENDORID_INTEL) &&
5026182fdbdSPeter Wemm 	    (pci_get_device(dev) == FXP_DEVICEID_i82557)) {
5036182fdbdSPeter Wemm 		device_set_desc(dev, "Intel EtherExpress Pro 10/100B Ethernet");
5046182fdbdSPeter Wemm 		return 0;
505a17c678eSDavid Greenman 	}
506dd68ef16SPeter Wemm 	if ((pci_get_vendor(dev) == FXP_VENDORID_INTEL) &&
507dd68ef16SPeter Wemm 	    (pci_get_device(dev) == FXP_DEVICEID_i82559)) {
508dd68ef16SPeter Wemm 		device_set_desc(dev, "Intel InBusiness 10/100 Ethernet");
509dd68ef16SPeter Wemm 		return 0;
510dd68ef16SPeter Wemm 	}
511a17c678eSDavid Greenman 
5126182fdbdSPeter Wemm 	return ENXIO;
5136182fdbdSPeter Wemm }
5146182fdbdSPeter Wemm 
5156182fdbdSPeter Wemm static int
5166182fdbdSPeter Wemm fxp_attach(device_t dev)
517a17c678eSDavid Greenman {
5186182fdbdSPeter Wemm 	int error = 0;
5196182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
520ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
521ba8c6fd5SDavid Greenman 	int s;
522df373873SWes Peters 	u_long val;
5236182fdbdSPeter Wemm 	int rid;
524a17c678eSDavid Greenman 
5256c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
526a17c678eSDavid Greenman 
527a17c678eSDavid Greenman 	s = splimp();
528a17c678eSDavid Greenman 
529dfe61cf1SDavid Greenman 	/*
530df373873SWes Peters 	 * Enable bus mastering.
531df373873SWes Peters 	 */
5326182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
533df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
5346182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
535df373873SWes Peters 
536df373873SWes Peters 	/*
537dfe61cf1SDavid Greenman 	 * Map control/status registers.
538dfe61cf1SDavid Greenman 	 */
5396182fdbdSPeter Wemm 	rid = FXP_PCI_MMBA;
5406182fdbdSPeter Wemm 	sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
5416182fdbdSPeter Wemm 				     0, ~0, 1, RF_ACTIVE);
5426182fdbdSPeter Wemm 	if (!sc->mem) {
5436182fdbdSPeter Wemm 		device_printf(dev, "could not map memory\n");
5446182fdbdSPeter Wemm 		error = ENXIO;
545a17c678eSDavid Greenman 		goto fail;
546a17c678eSDavid Greenman         }
5474fc1dda9SAndrew Gallatin 
5484fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
5494fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
550a17c678eSDavid Greenman 
551a17c678eSDavid Greenman 	/*
552dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
553dfe61cf1SDavid Greenman 	 */
5546182fdbdSPeter Wemm 	rid = 0;
5556182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
5566182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
5576182fdbdSPeter Wemm 	if (sc->irq == NULL) {
5586182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
5596182fdbdSPeter Wemm 		error = ENXIO;
5606182fdbdSPeter Wemm 		goto fail;
5616182fdbdSPeter Wemm 	}
5626182fdbdSPeter Wemm 
563566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
564566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
5656182fdbdSPeter Wemm 	if (error) {
5666182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
567a17c678eSDavid Greenman 		goto fail;
568a17c678eSDavid Greenman 	}
569a17c678eSDavid Greenman 
570ba8c6fd5SDavid Greenman 	/* Do generic parts of attach. */
571ba8c6fd5SDavid Greenman 	if (fxp_attach_common(sc, sc->arpcom.ac_enaddr)) {
572ba8c6fd5SDavid Greenman 		/* Failed! */
5736182fdbdSPeter Wemm 		bus_teardown_intr(dev, sc->irq, sc->ih);
5746182fdbdSPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
5756182fdbdSPeter Wemm 		bus_release_resource(dev, SYS_RES_MEMORY, FXP_PCI_MMBA, sc->mem);
5766182fdbdSPeter Wemm 		error = ENXIO;
577ba8c6fd5SDavid Greenman 		goto fail;
578a17c678eSDavid Greenman 	}
579a17c678eSDavid Greenman 
5806182fdbdSPeter Wemm 	device_printf(dev, "Ethernet address %6D%s\n",
581ba8c6fd5SDavid Greenman 	    sc->arpcom.ac_enaddr, ":", sc->phy_10Mbps_only ? ", 10Mbps" : "");
582dccee1a1SDavid Greenman 
583a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
5846182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
585a17c678eSDavid Greenman 	ifp->if_name = "fxp";
586a17c678eSDavid Greenman 	ifp->if_output = ether_output;
587a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
588fb583156SDavid Greenman 	ifp->if_init = fxp_init;
589ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
590ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
591ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
592ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
593ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
594a17c678eSDavid Greenman 
595dfe61cf1SDavid Greenman 	/*
596dfe61cf1SDavid Greenman 	 * Attach the interface.
597dfe61cf1SDavid Greenman 	 */
598a17c678eSDavid Greenman 	if_attach(ifp);
599483b9871SDavid Greenman 	/*
6003114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
6013114fdb4SDavid Greenman 	 * TX descriptors.
602483b9871SDavid Greenman 	 */
6033114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
6049b44ff22SGarrett Wollman 	ether_ifattach(ifp);
6059b44ff22SGarrett Wollman 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
6064a684684SDavid Greenman 
607a17c678eSDavid Greenman 	splx(s);
6086182fdbdSPeter Wemm 	return 0;
609a17c678eSDavid Greenman 
610a17c678eSDavid Greenman  fail:
611a17c678eSDavid Greenman 	splx(s);
6126182fdbdSPeter Wemm 	return error;
6136182fdbdSPeter Wemm }
6146182fdbdSPeter Wemm 
6156182fdbdSPeter Wemm /*
6166182fdbdSPeter Wemm  * Detach interface.
6176182fdbdSPeter Wemm  */
6186182fdbdSPeter Wemm static int
6196182fdbdSPeter Wemm fxp_detach(device_t dev)
6206182fdbdSPeter Wemm {
6216182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
6226182fdbdSPeter Wemm 	int s;
6236182fdbdSPeter Wemm 
6246182fdbdSPeter Wemm 	s = splimp();
6256182fdbdSPeter Wemm 
6266182fdbdSPeter Wemm 	/*
6276182fdbdSPeter Wemm 	 * Close down routes etc.
6286182fdbdSPeter Wemm 	 */
6296182fdbdSPeter Wemm 	if_detach(&sc->arpcom.ac_if);
6306182fdbdSPeter Wemm 
6316182fdbdSPeter Wemm 	/*
6326182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
6336182fdbdSPeter Wemm 	 */
6346182fdbdSPeter Wemm 	fxp_stop(sc);
6356182fdbdSPeter Wemm 
6366182fdbdSPeter Wemm 	/*
6376182fdbdSPeter Wemm 	 * Deallocate resources.
6386182fdbdSPeter Wemm 	 */
6396182fdbdSPeter Wemm 	bus_teardown_intr(dev, sc->irq, sc->ih);
6406182fdbdSPeter Wemm 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
6416182fdbdSPeter Wemm 	bus_release_resource(dev, SYS_RES_MEMORY, FXP_PCI_MMBA, sc->mem);
6426182fdbdSPeter Wemm 
6436182fdbdSPeter Wemm 	/*
6446182fdbdSPeter Wemm 	 * Free all the receive buffers.
6456182fdbdSPeter Wemm 	 */
6466182fdbdSPeter Wemm 	if (sc->rfa_headm != NULL)
6476182fdbdSPeter Wemm 		m_freem(sc->rfa_headm);
6486182fdbdSPeter Wemm 
6496182fdbdSPeter Wemm 	/*
6506182fdbdSPeter Wemm 	 * Free all media structures.
6516182fdbdSPeter Wemm 	 */
6526182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
6536182fdbdSPeter Wemm 
6546182fdbdSPeter Wemm 	/*
6556182fdbdSPeter Wemm 	 * Free anciliary structures.
6566182fdbdSPeter Wemm 	 */
6576182fdbdSPeter Wemm 	free(sc->cbl_base, M_DEVBUF);
6586182fdbdSPeter Wemm 	free(sc->fxp_stats, M_DEVBUF);
6596182fdbdSPeter Wemm 	free(sc->mcsp, M_DEVBUF);
6606182fdbdSPeter Wemm 
6616182fdbdSPeter Wemm 	splx(s);
6626182fdbdSPeter Wemm 
6636182fdbdSPeter Wemm 	return 0;
664a17c678eSDavid Greenman }
665a17c678eSDavid Greenman 
666a17c678eSDavid Greenman /*
6674a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
668a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
669a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
670a17c678eSDavid Greenman  */
6716182fdbdSPeter Wemm static int
6726182fdbdSPeter Wemm fxp_shutdown(device_t dev)
673a17c678eSDavid Greenman {
6746182fdbdSPeter Wemm 	/*
6756182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
6766182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
6776182fdbdSPeter Wemm 	 * reboot before the driver initializes.
6786182fdbdSPeter Wemm 	 */
6796182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
6806182fdbdSPeter Wemm 	return 0;
681a17c678eSDavid Greenman }
682a17c678eSDavid Greenman 
6836182fdbdSPeter Wemm static device_method_t fxp_methods[] = {
6846182fdbdSPeter Wemm 	/* Device interface */
6856182fdbdSPeter Wemm 	DEVMETHOD(device_probe,		fxp_probe),
6866182fdbdSPeter Wemm 	DEVMETHOD(device_attach,	fxp_attach),
6876182fdbdSPeter Wemm 	DEVMETHOD(device_detach,	fxp_detach),
6886182fdbdSPeter Wemm 	DEVMETHOD(device_shutdown,	fxp_shutdown),
6896182fdbdSPeter Wemm 
6906182fdbdSPeter Wemm 	{ 0, 0 }
6916182fdbdSPeter Wemm };
6926182fdbdSPeter Wemm 
6936182fdbdSPeter Wemm static driver_t fxp_driver = {
6946182fdbdSPeter Wemm 	"fxp",
6956182fdbdSPeter Wemm 	fxp_methods,
6966182fdbdSPeter Wemm 	sizeof(struct fxp_softc),
6976182fdbdSPeter Wemm };
6986182fdbdSPeter Wemm 
6996182fdbdSPeter Wemm static devclass_t fxp_devclass;
7006182fdbdSPeter Wemm 
7019e4c647cSBill Paul DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
7026182fdbdSPeter Wemm 
703ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
704ba8c6fd5SDavid Greenman 
705ba8c6fd5SDavid Greenman /*************************************************************
706ba8c6fd5SDavid Greenman  * End of operating system-specific autoconfiguration glue
707ba8c6fd5SDavid Greenman  *************************************************************/
708ba8c6fd5SDavid Greenman 
709ba8c6fd5SDavid Greenman /*
710ba8c6fd5SDavid Greenman  * Do generic parts of attach.
711ba8c6fd5SDavid Greenman  */
712ba8c6fd5SDavid Greenman static int
713ba8c6fd5SDavid Greenman fxp_attach_common(sc, enaddr)
714ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
715ba8c6fd5SDavid Greenman 	u_int8_t *enaddr;
716ba8c6fd5SDavid Greenman {
717ba8c6fd5SDavid Greenman 	u_int16_t data;
718ba8c6fd5SDavid Greenman 	int i, nmedia, defmedia;
719ba8c6fd5SDavid Greenman 	const int *media;
720ba8c6fd5SDavid Greenman 
721ba8c6fd5SDavid Greenman 	/*
722ba8c6fd5SDavid Greenman 	 * Reset to a stable state.
723ba8c6fd5SDavid Greenman 	 */
724ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
725ba8c6fd5SDavid Greenman 	DELAY(10);
726ba8c6fd5SDavid Greenman 
727ba8c6fd5SDavid Greenman 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
728ba8c6fd5SDavid Greenman 	    M_DEVBUF, M_NOWAIT);
729ba8c6fd5SDavid Greenman 	if (sc->cbl_base == NULL)
730ba8c6fd5SDavid Greenman 		goto fail;
73191aa9f90SDavid Greenman 	bzero(sc->cbl_base, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
732ba8c6fd5SDavid Greenman 
733ba8c6fd5SDavid Greenman 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT);
734ba8c6fd5SDavid Greenman 	if (sc->fxp_stats == NULL)
735ba8c6fd5SDavid Greenman 		goto fail;
736ba8c6fd5SDavid Greenman 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
737ba8c6fd5SDavid Greenman 
738397f9dfeSDavid Greenman 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
739397f9dfeSDavid Greenman 	if (sc->mcsp == NULL)
740397f9dfeSDavid Greenman 		goto fail;
741397f9dfeSDavid Greenman 
742ba8c6fd5SDavid Greenman 	/*
743ba8c6fd5SDavid Greenman 	 * Pre-allocate our receive buffers.
744ba8c6fd5SDavid Greenman 	 */
745ba8c6fd5SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
746ba8c6fd5SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
747ba8c6fd5SDavid Greenman 			goto fail;
748ba8c6fd5SDavid Greenman 		}
749ba8c6fd5SDavid Greenman 	}
750ba8c6fd5SDavid Greenman 
751ba8c6fd5SDavid Greenman 	/*
752e9bf2fa7SDavid Greenman 	 * Find out how large of an SEEPROM we have.
753e9bf2fa7SDavid Greenman 	 */
754e9bf2fa7SDavid Greenman 	fxp_autosize_eeprom(sc);
755e9bf2fa7SDavid Greenman 
756e9bf2fa7SDavid Greenman 	/*
757ba8c6fd5SDavid Greenman 	 * Get info about the primary PHY
758ba8c6fd5SDavid Greenman 	 */
759ba8c6fd5SDavid Greenman 	fxp_read_eeprom(sc, (u_int16_t *)&data, 6, 1);
760ba8c6fd5SDavid Greenman 	sc->phy_primary_addr = data & 0xff;
761ba8c6fd5SDavid Greenman 	sc->phy_primary_device = (data >> 8) & 0x3f;
762ba8c6fd5SDavid Greenman 	sc->phy_10Mbps_only = data >> 15;
763ba8c6fd5SDavid Greenman 
764ba8c6fd5SDavid Greenman 	/*
765ba8c6fd5SDavid Greenman 	 * Read MAC address.
766ba8c6fd5SDavid Greenman 	 */
767ba8c6fd5SDavid Greenman 	fxp_read_eeprom(sc, (u_int16_t *)enaddr, 0, 3);
768ba8c6fd5SDavid Greenman 
769ba8c6fd5SDavid Greenman 	/*
770ba8c6fd5SDavid Greenman 	 * Initialize the media structures.
771ba8c6fd5SDavid Greenman 	 */
772ba8c6fd5SDavid Greenman 
773ba8c6fd5SDavid Greenman 	media = fxp_media_default;
774ba8c6fd5SDavid Greenman 	nmedia = sizeof(fxp_media_default) / sizeof(fxp_media_default[0]);
775ba8c6fd5SDavid Greenman 	defmedia = FXP_MEDIA_DEFAULT_DEFMEDIA;
776ba8c6fd5SDavid Greenman 
777ba8c6fd5SDavid Greenman 	for (i = 0; i < NFXPMEDIA; i++) {
778ba8c6fd5SDavid Greenman 		if (sc->phy_primary_device == fxp_media[i].fsm_phy) {
779ba8c6fd5SDavid Greenman 			media = fxp_media[i].fsm_media;
780ba8c6fd5SDavid Greenman 			nmedia = fxp_media[i].fsm_nmedia;
781ba8c6fd5SDavid Greenman 			defmedia = fxp_media[i].fsm_defmedia;
782ba8c6fd5SDavid Greenman 		}
783ba8c6fd5SDavid Greenman 	}
784ba8c6fd5SDavid Greenman 
785ba8c6fd5SDavid Greenman 	ifmedia_init(&sc->sc_media, 0, fxp_mediachange, fxp_mediastatus);
786ba8c6fd5SDavid Greenman 	for (i = 0; i < nmedia; i++) {
787ba8c6fd5SDavid Greenman 		if (IFM_SUBTYPE(media[i]) == IFM_100_TX && sc->phy_10Mbps_only)
788ba8c6fd5SDavid Greenman 			continue;
789ba8c6fd5SDavid Greenman 		ifmedia_add(&sc->sc_media, media[i], 0, NULL);
790ba8c6fd5SDavid Greenman 	}
791ba8c6fd5SDavid Greenman 	ifmedia_set(&sc->sc_media, defmedia);
792ba8c6fd5SDavid Greenman 
793ba8c6fd5SDavid Greenman 	return (0);
794ba8c6fd5SDavid Greenman 
795ba8c6fd5SDavid Greenman  fail:
796ba8c6fd5SDavid Greenman 	printf(FXP_FORMAT ": Failed to malloc memory\n", FXP_ARGS(sc));
797ba8c6fd5SDavid Greenman 	if (sc->cbl_base)
798ba8c6fd5SDavid Greenman 		free(sc->cbl_base, M_DEVBUF);
799ba8c6fd5SDavid Greenman 	if (sc->fxp_stats)
800ba8c6fd5SDavid Greenman 		free(sc->fxp_stats, M_DEVBUF);
801397f9dfeSDavid Greenman 	if (sc->mcsp)
802397f9dfeSDavid Greenman 		free(sc->mcsp, M_DEVBUF);
803ba8c6fd5SDavid Greenman 	/* frees entire chain */
804ba8c6fd5SDavid Greenman 	if (sc->rfa_headm)
805ba8c6fd5SDavid Greenman 		m_freem(sc->rfa_headm);
806ba8c6fd5SDavid Greenman 
807ba8c6fd5SDavid Greenman 	return (ENOMEM);
808ba8c6fd5SDavid Greenman }
809ba8c6fd5SDavid Greenman 
810ba8c6fd5SDavid Greenman /*
811e9bf2fa7SDavid Greenman  * From NetBSD:
812e9bf2fa7SDavid Greenman  *
813e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
814e9bf2fa7SDavid Greenman  *
815e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
816e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
817e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
818e9bf2fa7SDavid Greenman  *
819e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
820e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
821e9bf2fa7SDavid Greenman  *
822e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
823e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
824e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
825e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
826e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
827e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
828e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
829e9bf2fa7SDavid Greenman  *
830e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
831e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
832e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
833e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
834e9bf2fa7SDavid Greenman  *
835e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
836e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
837e9bf2fa7SDavid Greenman  */
838e9bf2fa7SDavid Greenman static void
839e9bf2fa7SDavid Greenman fxp_autosize_eeprom(sc)
840e9bf2fa7SDavid Greenman 	struct fxp_softc *sc;
841e9bf2fa7SDavid Greenman {
842e9bf2fa7SDavid Greenman 	u_int16_t reg;
843e9bf2fa7SDavid Greenman 	int x;
844e9bf2fa7SDavid Greenman 
845e9bf2fa7SDavid Greenman 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
846e9bf2fa7SDavid Greenman 	/*
847e9bf2fa7SDavid Greenman 	 * Shift in read opcode.
848e9bf2fa7SDavid Greenman 	 */
849e9bf2fa7SDavid Greenman 	for (x = 3; x > 0; x--) {
850e9bf2fa7SDavid Greenman 		if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
851e9bf2fa7SDavid Greenman 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
852e9bf2fa7SDavid Greenman 		} else {
853e9bf2fa7SDavid Greenman 			reg = FXP_EEPROM_EECS;
854e9bf2fa7SDavid Greenman 		}
855e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
856e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
857e9bf2fa7SDavid Greenman 		    reg | FXP_EEPROM_EESK);
858e9bf2fa7SDavid Greenman 		DELAY(1);
859e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
860e9bf2fa7SDavid Greenman 		DELAY(1);
861e9bf2fa7SDavid Greenman 	}
862e9bf2fa7SDavid Greenman 	/*
863e9bf2fa7SDavid Greenman 	 * Shift in address.
864e9bf2fa7SDavid Greenman 	 * Wait for the dummy zero following a correct address shift.
865e9bf2fa7SDavid Greenman 	 */
866e9bf2fa7SDavid Greenman 	for (x = 1; x <= 8; x++) {
867e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
868e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
869e9bf2fa7SDavid Greenman 			FXP_EEPROM_EECS | FXP_EEPROM_EESK);
870e9bf2fa7SDavid Greenman 		DELAY(1);
871e9bf2fa7SDavid Greenman 		if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) == 0)
872e9bf2fa7SDavid Greenman 			break;
873e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
874e9bf2fa7SDavid Greenman 		DELAY(1);
875e9bf2fa7SDavid Greenman 	}
876e9bf2fa7SDavid Greenman 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
877e9bf2fa7SDavid Greenman 	DELAY(1);
878e9bf2fa7SDavid Greenman 	sc->eeprom_size = x;
879e9bf2fa7SDavid Greenman }
880e9bf2fa7SDavid Greenman /*
881ba8c6fd5SDavid Greenman  * Read from the serial EEPROM. Basically, you manually shift in
882ba8c6fd5SDavid Greenman  * the read opcode (one bit at a time) and then shift in the address,
883ba8c6fd5SDavid Greenman  * and then you shift out the data (all of this one bit at a time).
884ba8c6fd5SDavid Greenman  * The word size is 16 bits, so you have to provide the address for
885ba8c6fd5SDavid Greenman  * every 16 bits of data.
886ba8c6fd5SDavid Greenman  */
887ba8c6fd5SDavid Greenman static void
888ba8c6fd5SDavid Greenman fxp_read_eeprom(sc, data, offset, words)
889ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
890ba8c6fd5SDavid Greenman 	u_short *data;
891ba8c6fd5SDavid Greenman 	int offset;
892ba8c6fd5SDavid Greenman 	int words;
893ba8c6fd5SDavid Greenman {
894ba8c6fd5SDavid Greenman 	u_int16_t reg;
895ba8c6fd5SDavid Greenman 	int i, x;
896ba8c6fd5SDavid Greenman 
897ba8c6fd5SDavid Greenman 	for (i = 0; i < words; i++) {
898ba8c6fd5SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
899ba8c6fd5SDavid Greenman 		/*
900ba8c6fd5SDavid Greenman 		 * Shift in read opcode.
901ba8c6fd5SDavid Greenman 		 */
902ba8c6fd5SDavid Greenman 		for (x = 3; x > 0; x--) {
903ba8c6fd5SDavid Greenman 			if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
904ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
905ba8c6fd5SDavid Greenman 			} else {
906ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS;
907ba8c6fd5SDavid Greenman 			}
908ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
909ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
910ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
911ba8c6fd5SDavid Greenman 			DELAY(1);
912ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
913ba8c6fd5SDavid Greenman 			DELAY(1);
914ba8c6fd5SDavid Greenman 		}
915ba8c6fd5SDavid Greenman 		/*
916ba8c6fd5SDavid Greenman 		 * Shift in address.
917ba8c6fd5SDavid Greenman 		 */
918e9bf2fa7SDavid Greenman 		for (x = sc->eeprom_size; x > 0; x--) {
919ba8c6fd5SDavid Greenman 			if ((i + offset) & (1 << (x - 1))) {
920ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
921ba8c6fd5SDavid Greenman 			} else {
922ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS;
923ba8c6fd5SDavid Greenman 			}
924ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
925ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
926ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
927ba8c6fd5SDavid Greenman 			DELAY(1);
928ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
929ba8c6fd5SDavid Greenman 			DELAY(1);
930ba8c6fd5SDavid Greenman 		}
931ba8c6fd5SDavid Greenman 		reg = FXP_EEPROM_EECS;
932ba8c6fd5SDavid Greenman 		data[i] = 0;
933ba8c6fd5SDavid Greenman 		/*
934ba8c6fd5SDavid Greenman 		 * Shift out data.
935ba8c6fd5SDavid Greenman 		 */
936ba8c6fd5SDavid Greenman 		for (x = 16; x > 0; x--) {
937ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
938ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
939ba8c6fd5SDavid Greenman 			DELAY(1);
940ba8c6fd5SDavid Greenman 			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
941ba8c6fd5SDavid Greenman 			    FXP_EEPROM_EEDO)
942ba8c6fd5SDavid Greenman 				data[i] |= (1 << (x - 1));
943ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
944ba8c6fd5SDavid Greenman 			DELAY(1);
945ba8c6fd5SDavid Greenman 		}
946ba8c6fd5SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
947ba8c6fd5SDavid Greenman 		DELAY(1);
948ba8c6fd5SDavid Greenman 	}
949ba8c6fd5SDavid Greenman }
950ba8c6fd5SDavid Greenman 
951a17c678eSDavid Greenman /*
952a17c678eSDavid Greenman  * Start packet transmission on the interface.
953a17c678eSDavid Greenman  */
954a17c678eSDavid Greenman static void
955a17c678eSDavid Greenman fxp_start(ifp)
956a17c678eSDavid Greenman 	struct ifnet *ifp;
957a17c678eSDavid Greenman {
9589b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
959a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
960a17c678eSDavid Greenman 
961a17c678eSDavid Greenman 	/*
962483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
963483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
964483b9871SDavid Greenman 	 * of the command chain).
965a17c678eSDavid Greenman 	 */
966483b9871SDavid Greenman 	if (sc->need_mcsetup)
967a17c678eSDavid Greenman 		return;
9681cd443acSDavid Greenman 
969483b9871SDavid Greenman 	txp = NULL;
970483b9871SDavid Greenman 
971483b9871SDavid Greenman 	/*
972483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
973483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
9743114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
9753114fdb4SDavid Greenman 	 *       a NOP command when needed.
976483b9871SDavid Greenman 	 */
9773114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
978483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
979483b9871SDavid Greenman 		int segment;
980483b9871SDavid Greenman 
981dfe61cf1SDavid Greenman 		/*
982dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
983dfe61cf1SDavid Greenman 		 */
9846318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
985a17c678eSDavid Greenman 
986dfe61cf1SDavid Greenman 		/*
987483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
988dfe61cf1SDavid Greenman 		 */
989a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
990a17c678eSDavid Greenman 
991a17c678eSDavid Greenman 		/*
992a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
993483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
994a17c678eSDavid Greenman 		 * and size of the mbuf.
995a17c678eSDavid Greenman 		 */
99623a0ed7cSDavid Greenman tbdinit:
997a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
998a17c678eSDavid Greenman 			if (m->m_len != 0) {
999a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1000a17c678eSDavid Greenman 					break;
1001a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1002a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1003a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1004a17c678eSDavid Greenman 				segment++;
1005a17c678eSDavid Greenman 			}
1006a17c678eSDavid Greenman 		}
1007fb583156SDavid Greenman 		if (m != NULL) {
100823a0ed7cSDavid Greenman 			struct mbuf *mn;
100923a0ed7cSDavid Greenman 
1010a17c678eSDavid Greenman 			/*
1011a17c678eSDavid Greenman 			 * We ran out of segments. We have to recopy this mbuf
1012483b9871SDavid Greenman 			 * chain first. Bail out if we can't get the new buffers.
1013a17c678eSDavid Greenman 			 */
101423a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
101523a0ed7cSDavid Greenman 			if (mn == NULL) {
101623a0ed7cSDavid Greenman 				m_freem(mb_head);
1017483b9871SDavid Greenman 				break;
1018a17c678eSDavid Greenman 			}
101923a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
102023a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
102123a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
102223a0ed7cSDavid Greenman 					m_freem(mn);
102323a0ed7cSDavid Greenman 					m_freem(mb_head);
1024483b9871SDavid Greenman 					break;
102523a0ed7cSDavid Greenman 				}
102623a0ed7cSDavid Greenman 			}
1027ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1028ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
102923a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
103023a0ed7cSDavid Greenman 			m_freem(mb_head);
103123a0ed7cSDavid Greenman 			mb_head = mn;
103223a0ed7cSDavid Greenman 			goto tbdinit;
103323a0ed7cSDavid Greenman 		}
103423a0ed7cSDavid Greenman 
103523a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10361cd443acSDavid Greenman 		txp->mb_head = mb_head;
1037a17c678eSDavid Greenman 		txp->cb_status = 0;
10383114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1039a17c678eSDavid Greenman 			txp->cb_command =
1040a17c678eSDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S;
10413114fdb4SDavid Greenman 		} else {
10423114fdb4SDavid Greenman 			txp->cb_command =
10433114fdb4SDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
10443114fdb4SDavid Greenman 			/*
10453114fdb4SDavid Greenman 			 * Set a 5 second timer just in case we don't hear from the
10463114fdb4SDavid Greenman 			 * card again.
10473114fdb4SDavid Greenman 			 */
10483114fdb4SDavid Greenman 			ifp->if_timer = 5;
10493114fdb4SDavid Greenman 		}
1050f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1051a17c678eSDavid Greenman 
1052a17c678eSDavid Greenman 		/*
1053483b9871SDavid Greenman 		 * Advance the end of list forward.
1054a17c678eSDavid Greenman 		 */
1055a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
1056a17c678eSDavid Greenman 		sc->cbl_last = txp;
1057a17c678eSDavid Greenman 
1058a17c678eSDavid Greenman 		/*
10591cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
10601cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1061483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1062a17c678eSDavid Greenman 		 */
10631cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1064a17c678eSDavid Greenman 			sc->cbl_first = txp;
1065a17c678eSDavid Greenman 
10661cd443acSDavid Greenman 		sc->tx_queued++;
10671cd443acSDavid Greenman 
1068a17c678eSDavid Greenman 		/*
1069a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1070a17c678eSDavid Greenman 		 */
1071fb583156SDavid Greenman 		if (ifp->if_bpf)
1072ba8c6fd5SDavid Greenman 			bpf_mtap(FXP_BPFTAP_ARG(ifp), mb_head);
1073483b9871SDavid Greenman 	}
1074483b9871SDavid Greenman 
1075483b9871SDavid Greenman 	/*
1076483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1077483b9871SDavid Greenman 	 * going again if suspended.
1078483b9871SDavid Greenman 	 */
1079483b9871SDavid Greenman 	if (txp != NULL) {
1080483b9871SDavid Greenman 		fxp_scb_wait(sc);
1081483b9871SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
1082483b9871SDavid Greenman 	}
1083a17c678eSDavid Greenman }
1084a17c678eSDavid Greenman 
1085a17c678eSDavid Greenman /*
10869c7d2607SDavid Greenman  * Process interface interrupts.
1087a17c678eSDavid Greenman  */
1088ba8c6fd5SDavid Greenman static FXP_INTR_TYPE
1089a17c678eSDavid Greenman fxp_intr(arg)
1090a17c678eSDavid Greenman 	void *arg;
1091a17c678eSDavid Greenman {
1092a17c678eSDavid Greenman 	struct fxp_softc *sc = arg;
1093ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
10941cd443acSDavid Greenman 	u_int8_t statack;
1095ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1096ba8c6fd5SDavid Greenman 	int claimed = 0;
1097ba8c6fd5SDavid Greenman #endif
1098a17c678eSDavid Greenman 
1099ba8c6fd5SDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1100ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1101ba8c6fd5SDavid Greenman 		claimed = 1;
1102ba8c6fd5SDavid Greenman #endif
1103a17c678eSDavid Greenman 		/*
1104a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1105a17c678eSDavid Greenman 		 */
1106ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1107a17c678eSDavid Greenman 
1108a17c678eSDavid Greenman 		/*
11093114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
11103114fdb4SDavid Greenman 		 */
11113114fdb4SDavid Greenman 		if (statack & FXP_SCB_STATACK_CXTNO) {
11123114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
11133114fdb4SDavid Greenman 
11143114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
11153114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
11163114fdb4SDavid Greenman 			    txp = txp->next) {
11173114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
11183114fdb4SDavid Greenman 					m_freem(txp->mb_head);
11193114fdb4SDavid Greenman 					txp->mb_head = NULL;
11203114fdb4SDavid Greenman 				}
11213114fdb4SDavid Greenman 				sc->tx_queued--;
11223114fdb4SDavid Greenman 			}
11233114fdb4SDavid Greenman 			sc->cbl_first = txp;
11243114fdb4SDavid Greenman 			ifp->if_timer = 0;
11253114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
11263114fdb4SDavid Greenman 				if (sc->need_mcsetup)
11273114fdb4SDavid Greenman 					fxp_mc_setup(sc);
11283114fdb4SDavid Greenman 			}
11293114fdb4SDavid Greenman 			/*
11303114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
11313114fdb4SDavid Greenman 			 */
11323114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
11333114fdb4SDavid Greenman 				fxp_start(ifp);
11343114fdb4SDavid Greenman 		}
11353114fdb4SDavid Greenman 		/*
1136a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1137a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1138a17c678eSDavid Greenman 		 * re-start the receiver.
1139a17c678eSDavid Greenman 		 */
1140a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1141a17c678eSDavid Greenman 			struct mbuf *m;
1142a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1143a17c678eSDavid Greenman rcvloop:
1144a17c678eSDavid Greenman 			m = sc->rfa_headm;
1145ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1146ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1147a17c678eSDavid Greenman 
1148a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1149dfe61cf1SDavid Greenman 				/*
1150dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1151dfe61cf1SDavid Greenman 				 */
1152a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1153a17c678eSDavid Greenman 				m->m_next = NULL;
1154a17c678eSDavid Greenman 
1155dfe61cf1SDavid Greenman 				/*
1156ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1157ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1158ba8c6fd5SDavid Greenman 				 * instead.
1159dfe61cf1SDavid Greenman 				 */
1160a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1161a17c678eSDavid Greenman 					struct ether_header *eh;
1162ba8c6fd5SDavid Greenman 					u_int16_t total_len;
1163a17c678eSDavid Greenman 
1164ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1165ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1166ba8c6fd5SDavid Greenman 					if (total_len <
1167ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
116806339180SDavid Greenman 						m_freem(m);
116906339180SDavid Greenman 						goto rcvloop;
117006339180SDavid Greenman 					}
1171a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
1172ba8c6fd5SDavid Greenman 					m->m_pkthdr.len = m->m_len =
1173ab090e5bSLuigi Rizzo 					    total_len ;
1174a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1175ab090e5bSLuigi Rizzo 					if (ifp->if_bpf)
1176ba8c6fd5SDavid Greenman 						bpf_tap(FXP_BPFTAP_ARG(ifp),
1177ba8c6fd5SDavid Greenman 						    mtod(m, caddr_t),
1178ba8c6fd5SDavid Greenman 						    total_len);
1179ab090e5bSLuigi Rizzo #ifdef BRIDGE
1180ab090e5bSLuigi Rizzo                                         if (do_bridge) {
1181ab090e5bSLuigi Rizzo                                             struct ifnet *bdg_ifp ;
1182ab090e5bSLuigi Rizzo                                             bdg_ifp = bridge_in(m);
1183ab090e5bSLuigi Rizzo                                             if (bdg_ifp == BDG_DROP)
1184ab090e5bSLuigi Rizzo                                                 goto dropit ;
1185ab090e5bSLuigi Rizzo                                             if (bdg_ifp != BDG_LOCAL)
1186ab090e5bSLuigi Rizzo                                                 bdg_forward(&m, bdg_ifp);
1187ab090e5bSLuigi Rizzo                                             if (bdg_ifp != BDG_LOCAL &&
1188ab090e5bSLuigi Rizzo                                                     bdg_ifp != BDG_BCAST &&
1189ab090e5bSLuigi Rizzo                                                     bdg_ifp != BDG_MCAST)
1190ab090e5bSLuigi Rizzo                                                 goto dropit ;
1191ab090e5bSLuigi Rizzo                                             goto getit ;
1192ab090e5bSLuigi Rizzo                                         }
1193ab090e5bSLuigi Rizzo #endif
1194a17c678eSDavid Greenman 					/*
1195ba8c6fd5SDavid Greenman 					 * Only pass this packet up
1196ba8c6fd5SDavid Greenman 					 * if it is for us.
1197a17c678eSDavid Greenman 					 */
1198ba8c6fd5SDavid Greenman 					if ((ifp->if_flags &
1199ba8c6fd5SDavid Greenman 					    IFF_PROMISC) &&
1200ba8c6fd5SDavid Greenman 					    (rfa->rfa_status &
1201ba8c6fd5SDavid Greenman 					    FXP_RFA_STATUS_IAMATCH) &&
1202ba8c6fd5SDavid Greenman 					    (eh->ether_dhost[0] & 1)
1203ba8c6fd5SDavid Greenman 					    == 0) {
12043f745407SPeter Wemm #ifdef BRIDGE
1205ab090e5bSLuigi Rizzo dropit:
12063f745407SPeter Wemm #endif
1207ab090e5bSLuigi Rizzo 					    if (m)
1208a17c678eSDavid Greenman 						m_freem(m);
1209a17c678eSDavid Greenman 					    goto rcvloop;
1210a17c678eSDavid Greenman 					}
12113f745407SPeter Wemm #ifdef BRIDGE
1212ab090e5bSLuigi Rizzo getit:
12133f745407SPeter Wemm #endif
1214ba8c6fd5SDavid Greenman 					m->m_data +=
1215ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1216ab090e5bSLuigi Rizzo 					m->m_len -=
1217ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1218ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len ;
1219a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1220a17c678eSDavid Greenman 				}
1221a17c678eSDavid Greenman 				goto rcvloop;
1222a17c678eSDavid Greenman 			}
1223a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1224ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1225ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1226ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1227ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
1228ba8c6fd5SDavid Greenman 				CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1229ba8c6fd5SDavid Greenman 				    FXP_SCB_COMMAND_RU_START);
1230a17c678eSDavid Greenman 			}
1231a17c678eSDavid Greenman 		}
1232a17c678eSDavid Greenman 	}
1233ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1234ba8c6fd5SDavid Greenman 	return (claimed);
1235ba8c6fd5SDavid Greenman #endif
1236a17c678eSDavid Greenman }
1237a17c678eSDavid Greenman 
1238dfe61cf1SDavid Greenman /*
1239dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1240dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1241dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1242dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1243dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1244dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1245dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1246dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1247dfe61cf1SDavid Greenman  * them again next time.
1248dfe61cf1SDavid Greenman  */
1249303b270bSEivind Eklund static void
1250a17c678eSDavid Greenman fxp_stats_update(arg)
1251a17c678eSDavid Greenman 	void *arg;
1252a17c678eSDavid Greenman {
1253a17c678eSDavid Greenman 	struct fxp_softc *sc = arg;
1254ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1255a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1256c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1257397f9dfeSDavid Greenman 	int s;
1258a17c678eSDavid Greenman 
1259a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1260a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1261397f9dfeSDavid Greenman 	if (sp->rx_good) {
1262397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1263397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1264397f9dfeSDavid Greenman 	} else {
1265c8cc6fcaSDavid Greenman 		/*
1266c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1267c8cc6fcaSDavid Greenman 		 */
1268397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1269397f9dfeSDavid Greenman 	}
12703ba65732SDavid Greenman 	ifp->if_ierrors +=
12713ba65732SDavid Greenman 	    sp->rx_crc_errors +
12723ba65732SDavid Greenman 	    sp->rx_alignment_errors +
12733ba65732SDavid Greenman 	    sp->rx_rnr_errors +
12746e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1275a17c678eSDavid Greenman 	/*
1276f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1277f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1278f9be9005SDavid Greenman 	 */
1279f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1280f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1281f9be9005SDavid Greenman 		if (tx_threshold < 192)
1282f9be9005SDavid Greenman 			tx_threshold += 64;
1283f9be9005SDavid Greenman 	}
1284397f9dfeSDavid Greenman 	s = splimp();
1285397f9dfeSDavid Greenman 	/*
1286c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1287c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1288c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1289c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1290c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1291c8cc6fcaSDavid Greenman 	 */
1292c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1293c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1294c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1295c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1296c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1297c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1298c8cc6fcaSDavid Greenman 		}
1299c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1300c8cc6fcaSDavid Greenman 	}
1301c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1302c8cc6fcaSDavid Greenman 	/*
1303397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1304397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1305397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1306397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1307397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1308397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1309397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1310397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1311397f9dfeSDavid Greenman 	 */
1312397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1313397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1314397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1315397f9dfeSDavid Greenman 	}
1316f9be9005SDavid Greenman 	/*
13173ba65732SDavid Greenman 	 * If there is no pending command, start another stats
13183ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1319a17c678eSDavid Greenman 	 */
1320397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1321a17c678eSDavid Greenman 		/*
1322397f9dfeSDavid Greenman 		 * Start another stats dump.
1323a17c678eSDavid Greenman 		 */
1324ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1325ba8c6fd5SDavid Greenman 		    FXP_SCB_COMMAND_CU_DUMPRESET);
1326dfe61cf1SDavid Greenman 	} else {
1327dfe61cf1SDavid Greenman 		/*
1328dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1329dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
13303ba65732SDavid Greenman 		 * next timer event to update them.
1331dfe61cf1SDavid Greenman 		 */
1332dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1333f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1334dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
13353ba65732SDavid Greenman 
1336dfe61cf1SDavid Greenman 		sp->rx_good = 0;
13373ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
13383ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
13393ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
13403ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1341dfe61cf1SDavid Greenman 	}
1342397f9dfeSDavid Greenman 	splx(s);
1343a17c678eSDavid Greenman 	/*
1344a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1345a17c678eSDavid Greenman 	 */
1346397f9dfeSDavid Greenman 	sc->stat_ch = timeout(fxp_stats_update, sc, hz);
1347a17c678eSDavid Greenman }
1348a17c678eSDavid Greenman 
1349a17c678eSDavid Greenman /*
1350a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1351a17c678eSDavid Greenman  * the interface.
1352a17c678eSDavid Greenman  */
1353a17c678eSDavid Greenman static void
13544a5f1499SDavid Greenman fxp_stop(sc)
13554a5f1499SDavid Greenman 	struct fxp_softc *sc;
1356a17c678eSDavid Greenman {
1357ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
13583ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
13593ba65732SDavid Greenman 	int i;
1360a17c678eSDavid Greenman 
1361a17c678eSDavid Greenman 	/*
1362a17c678eSDavid Greenman 	 * Cancel stats updater.
1363a17c678eSDavid Greenman 	 */
13646c951b44SJustin T. Gibbs 	untimeout(fxp_stats_update, sc, sc->stat_ch);
13653ba65732SDavid Greenman 
13663ba65732SDavid Greenman 	/*
13673ba65732SDavid Greenman 	 * Issue software reset
13683ba65732SDavid Greenman 	 */
1369ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1370a17c678eSDavid Greenman 	DELAY(10);
1371a17c678eSDavid Greenman 
13723ba65732SDavid Greenman 	/*
13733ba65732SDavid Greenman 	 * Release any xmit buffers.
13743ba65732SDavid Greenman 	 */
1375da91462dSDavid Greenman 	txp = sc->cbl_base;
1376da91462dSDavid Greenman 	if (txp != NULL) {
1377da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1378da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1379da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1380da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1381da91462dSDavid Greenman 			}
1382da91462dSDavid Greenman 		}
13833ba65732SDavid Greenman 	}
13843ba65732SDavid Greenman 	sc->tx_queued = 0;
13853ba65732SDavid Greenman 
13863ba65732SDavid Greenman 	/*
13873ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
13883ba65732SDavid Greenman 	 */
13893ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
13903ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
13913ba65732SDavid Greenman 	sc->rfa_headm = NULL;
13923ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
13933ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
13943ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
13953ba65732SDavid Greenman 			/*
13963ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
13973ba65732SDavid Greenman 			 * and we just freed all the buffers we need
13983ba65732SDavid Greenman 			 * above.
13993ba65732SDavid Greenman 			 */
14003ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
14013ba65732SDavid Greenman 		}
14023ba65732SDavid Greenman 	}
14033ba65732SDavid Greenman 
14043ba65732SDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
14053ba65732SDavid Greenman 	ifp->if_timer = 0;
1406a17c678eSDavid Greenman }
1407a17c678eSDavid Greenman 
1408a17c678eSDavid Greenman /*
1409a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1410a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1411a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1412a17c678eSDavid Greenman  * card has wedged for some reason.
1413a17c678eSDavid Greenman  */
1414a17c678eSDavid Greenman static void
14154a5f1499SDavid Greenman fxp_watchdog(ifp)
14164a5f1499SDavid Greenman 	struct ifnet *ifp;
1417a17c678eSDavid Greenman {
1418ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1419ba8c6fd5SDavid Greenman 
1420397f9dfeSDavid Greenman 	printf(FXP_FORMAT ": device timeout\n", FXP_ARGS(sc));
14214a5f1499SDavid Greenman 	ifp->if_oerrors++;
1422a17c678eSDavid Greenman 
1423ba8c6fd5SDavid Greenman 	fxp_init(sc);
1424a17c678eSDavid Greenman }
1425a17c678eSDavid Greenman 
1426a17c678eSDavid Greenman static void
1427fb583156SDavid Greenman fxp_init(xsc)
1428fb583156SDavid Greenman 	void *xsc;
1429a17c678eSDavid Greenman {
1430fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1431ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1432a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1433a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1434a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1435397f9dfeSDavid Greenman 	int i, s, prm;
1436a17c678eSDavid Greenman 
1437a17c678eSDavid Greenman 	s = splimp();
1438a17c678eSDavid Greenman 	/*
14393ba65732SDavid Greenman 	 * Cancel any pending I/O
1440a17c678eSDavid Greenman 	 */
14413ba65732SDavid Greenman 	fxp_stop(sc);
1442a17c678eSDavid Greenman 
1443a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1444a17c678eSDavid Greenman 
1445a17c678eSDavid Greenman 	/*
1446a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1447a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1448a17c678eSDavid Greenman 	 */
1449ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1450ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1451a17c678eSDavid Greenman 
1452ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1453ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1454a17c678eSDavid Greenman 
1455a17c678eSDavid Greenman 	/*
1456a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1457a17c678eSDavid Greenman 	 */
1458ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1459ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
1460ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1461a17c678eSDavid Greenman 
1462a17c678eSDavid Greenman 	/*
1463a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1464a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1465a17c678eSDavid Greenman 	 * later.
1466a17c678eSDavid Greenman 	 */
1467a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1468a17c678eSDavid Greenman 
1469a17c678eSDavid Greenman 	/*
1470a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1471a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1472a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1473a17c678eSDavid Greenman 	 */
1474697457a1SMatthew Dillon 	bcopy(fxp_cb_config_template, (volatile void *)&cbp->cb_status,
1475397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1476a17c678eSDavid Greenman 
1477a17c678eSDavid Greenman 	cbp->cb_status =	0;
1478a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1479a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1480a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1481001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1482001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1483a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1484001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1485001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1486001696daSDavid Greenman 	cbp->dma_bce =		0;	/* (disable) dma max counters */
1487a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1488a17c678eSDavid Greenman 	cbp->tno_int =		0;	/* (disable) tx not okay interrupt */
14893114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1490a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1491a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1492a17c678eSDavid Greenman 	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
1493dccee1a1SDavid Greenman 	cbp->mediatype =	!sc->phy_10Mbps_only; /* interface mode */
1494a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1495a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1496a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1497a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1498a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1499a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1500a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1501a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1502001696daSDavid Greenman 	cbp->crscdt =		0;	/* (CRS only) */
1503a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1504a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1505a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1506a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
15073ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1508a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1509397f9dfeSDavid Greenman 	cbp->mc_all =		sc->all_mcasts;/* accept all multicasts */
1510a17c678eSDavid Greenman 
1511a17c678eSDavid Greenman 	/*
1512a17c678eSDavid Greenman 	 * Start the config command/DMA.
1513a17c678eSDavid Greenman 	 */
1514ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1515397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
1516ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1517a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
1518a17c678eSDavid Greenman 	while (!(cbp->cb_status & FXP_CB_STATUS_C));
1519a17c678eSDavid Greenman 
1520a17c678eSDavid Greenman 	/*
1521a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1522a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1523a17c678eSDavid Greenman 	 */
1524a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1525a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1526a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1527a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1528ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1529ba8c6fd5SDavid Greenman 	bcopy(LLADDR(ifp->if_sadl), (void *)cb_ias->macaddr, 6);
1530ba8c6fd5SDavid Greenman #else
15318aef1712SMatthew Dillon 	bcopy(sc->arpcom.ac_enaddr, (volatile void *)cb_ias->macaddr,
1532a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1533ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
1534a17c678eSDavid Greenman 
1535a17c678eSDavid Greenman 	/*
1536a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1537a17c678eSDavid Greenman 	 */
1538ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1539ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1540a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
1541a17c678eSDavid Greenman 	while (!(cb_ias->cb_status & FXP_CB_STATUS_C));
1542a17c678eSDavid Greenman 
1543a17c678eSDavid Greenman 	/*
1544a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1545a17c678eSDavid Greenman 	 */
1546a17c678eSDavid Greenman 
1547a17c678eSDavid Greenman 	txp = sc->cbl_base;
1548a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1549a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1550a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1551a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
1552397f9dfeSDavid Greenman 		txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
1553a17c678eSDavid Greenman 		txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1554a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1555a17c678eSDavid Greenman 	}
1556a17c678eSDavid Greenman 	/*
1557397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1558a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1559a17c678eSDavid Greenman 	 */
1560a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1561a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1562397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1563a17c678eSDavid Greenman 
1564ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1565ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1566a17c678eSDavid Greenman 
1567a17c678eSDavid Greenman 	/*
1568a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1569a17c678eSDavid Greenman 	 */
1570ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1571ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1572ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
1573ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1574a17c678eSDavid Greenman 
1575dccee1a1SDavid Greenman 	/*
1576ba8c6fd5SDavid Greenman 	 * Set current media.
1577dccee1a1SDavid Greenman 	 */
1578ba8c6fd5SDavid Greenman 	fxp_set_media(sc, sc->sc_media.ifm_cur->ifm_media);
1579dccee1a1SDavid Greenman 
1580a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1581a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1582a17c678eSDavid Greenman 	splx(s);
1583a17c678eSDavid Greenman 
1584a17c678eSDavid Greenman 	/*
1585a17c678eSDavid Greenman 	 * Start stats updater.
1586a17c678eSDavid Greenman 	 */
15876c951b44SJustin T. Gibbs 	sc->stat_ch = timeout(fxp_stats_update, sc, hz);
1588a17c678eSDavid Greenman }
1589a17c678eSDavid Greenman 
1590303b270bSEivind Eklund static void
1591ba8c6fd5SDavid Greenman fxp_set_media(sc, media)
1592ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1593ba8c6fd5SDavid Greenman 	int media;
1594ba8c6fd5SDavid Greenman {
1595ba8c6fd5SDavid Greenman 
1596ba8c6fd5SDavid Greenman 	switch (sc->phy_primary_device) {
1597ba8c6fd5SDavid Greenman 	case FXP_PHY_DP83840:
1598ba8c6fd5SDavid Greenman 	case FXP_PHY_DP83840A:
1599ba8c6fd5SDavid Greenman 		fxp_mdi_write(sc, sc->phy_primary_addr, FXP_DP83840_PCR,
1600ba8c6fd5SDavid Greenman 		    fxp_mdi_read(sc, sc->phy_primary_addr, FXP_DP83840_PCR) |
1601ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_LED4_MODE |	/* LED4 always indicates duplex */
1602ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_F_CONNECT |	/* force link disconnect bypass */
1603ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_BIT10);	/* XXX I have no idea */
1604ba8c6fd5SDavid Greenman 		/* fall through */
160592924291SDavid Greenman 	case FXP_PHY_82553A:
160692924291SDavid Greenman 	case FXP_PHY_82553C: /* untested */
1607ba8c6fd5SDavid Greenman 	case FXP_PHY_82555:
160892924291SDavid Greenman 	case FXP_PHY_82555B:
1609ba8c6fd5SDavid Greenman 		if (IFM_SUBTYPE(media) != IFM_AUTO) {
1610ba8c6fd5SDavid Greenman 			int flags;
1611ba8c6fd5SDavid Greenman 
1612ba8c6fd5SDavid Greenman 			flags = (IFM_SUBTYPE(media) == IFM_100_TX) ?
1613ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR_SPEED_100M : 0;
1614ba8c6fd5SDavid Greenman 			flags |= (media & IFM_FDX) ?
1615ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR_FULLDUPLEX : 0;
1616ba8c6fd5SDavid Greenman 			fxp_mdi_write(sc, sc->phy_primary_addr,
1617ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR,
1618ba8c6fd5SDavid Greenman 			    (fxp_mdi_read(sc, sc->phy_primary_addr,
1619ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR) &
1620ba8c6fd5SDavid Greenman 			    ~(FXP_PHY_BMCR_AUTOEN | FXP_PHY_BMCR_SPEED_100M |
1621ba8c6fd5SDavid Greenman 			     FXP_PHY_BMCR_FULLDUPLEX)) | flags);
1622ba8c6fd5SDavid Greenman 		} else {
1623ba8c6fd5SDavid Greenman 			fxp_mdi_write(sc, sc->phy_primary_addr,
1624ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR,
1625ba8c6fd5SDavid Greenman 			    (fxp_mdi_read(sc, sc->phy_primary_addr,
1626ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR) | FXP_PHY_BMCR_AUTOEN));
1627ba8c6fd5SDavid Greenman 		}
1628ba8c6fd5SDavid Greenman 		break;
1629ba8c6fd5SDavid Greenman 	/*
1630ba8c6fd5SDavid Greenman 	 * The Seeq 80c24 doesn't have a PHY programming interface, so do
1631ba8c6fd5SDavid Greenman 	 * nothing.
1632ba8c6fd5SDavid Greenman 	 */
1633ba8c6fd5SDavid Greenman 	case FXP_PHY_80C24:
1634ba8c6fd5SDavid Greenman 		break;
1635ba8c6fd5SDavid Greenman 	default:
1636ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT
1637ba8c6fd5SDavid Greenman 		    ": warning: unsupported PHY, type = %d, addr = %d\n",
1638ba8c6fd5SDavid Greenman 		     FXP_ARGS(sc), sc->phy_primary_device,
1639ba8c6fd5SDavid Greenman 		     sc->phy_primary_addr);
1640ba8c6fd5SDavid Greenman 	}
1641ba8c6fd5SDavid Greenman }
1642ba8c6fd5SDavid Greenman 
1643ba8c6fd5SDavid Greenman /*
1644ba8c6fd5SDavid Greenman  * Change media according to request.
1645ba8c6fd5SDavid Greenman  */
1646ba8c6fd5SDavid Greenman int
1647ba8c6fd5SDavid Greenman fxp_mediachange(ifp)
1648ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
1649ba8c6fd5SDavid Greenman {
1650ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1651ba8c6fd5SDavid Greenman 	struct ifmedia *ifm = &sc->sc_media;
1652ba8c6fd5SDavid Greenman 
1653ba8c6fd5SDavid Greenman 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1654ba8c6fd5SDavid Greenman 		return (EINVAL);
1655ba8c6fd5SDavid Greenman 
1656ba8c6fd5SDavid Greenman 	fxp_set_media(sc, ifm->ifm_media);
1657ba8c6fd5SDavid Greenman 	return (0);
1658ba8c6fd5SDavid Greenman }
1659ba8c6fd5SDavid Greenman 
1660ba8c6fd5SDavid Greenman /*
1661ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1662ba8c6fd5SDavid Greenman  */
1663ba8c6fd5SDavid Greenman void
1664ba8c6fd5SDavid Greenman fxp_mediastatus(ifp, ifmr)
1665ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
1666ba8c6fd5SDavid Greenman 	struct ifmediareq *ifmr;
1667ba8c6fd5SDavid Greenman {
1668ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1669a7280784SJulian Elischer 	int flags, stsflags;
1670ba8c6fd5SDavid Greenman 
1671ba8c6fd5SDavid Greenman 	switch (sc->phy_primary_device) {
1672ba8c6fd5SDavid Greenman 	case FXP_PHY_82555:
167335517ab7SDavid Greenman 	case FXP_PHY_82555B:
1674a7280784SJulian Elischer 	case FXP_PHY_DP83840:
1675a7280784SJulian Elischer 	case FXP_PHY_DP83840A:
1676a7280784SJulian Elischer 		ifmr->ifm_status = IFM_AVALID; /* IFM_ACTIVE will be valid */
1677ba8c6fd5SDavid Greenman 		ifmr->ifm_active = IFM_ETHER;
1678a7280784SJulian Elischer 		/*
1679a7280784SJulian Elischer 		 * the following is not an error.
1680a7280784SJulian Elischer 		 * You need to read this register twice to get current
1681a7280784SJulian Elischer 		 * status. This is correct documented behaviour, the
1682a7280784SJulian Elischer 		 * first read gets latched values.
1683a7280784SJulian Elischer 		 */
1684a7280784SJulian Elischer 		stsflags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_STS);
1685a7280784SJulian Elischer 		stsflags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_STS);
1686a7280784SJulian Elischer 		if (stsflags & FXP_PHY_STS_LINK_STS)
1687a7280784SJulian Elischer 				ifmr->ifm_status |= IFM_ACTIVE;
1688a7280784SJulian Elischer 
1689a7280784SJulian Elischer 		/*
1690a7280784SJulian Elischer 		 * If we are in auto mode, then try report the result.
1691a7280784SJulian Elischer 		 */
1692a7280784SJulian Elischer 		flags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_BMCR);
1693f1bf08c2SJulian Elischer 		if (flags & FXP_PHY_BMCR_AUTOEN) {
1694f1bf08c2SJulian Elischer 			ifmr->ifm_active |= IFM_AUTO; /* XXX presently 0 */
1695a7280784SJulian Elischer 			if (stsflags & FXP_PHY_STS_AUTO_DONE) {
1696f1bf08c2SJulian Elischer 				/*
1697a7280784SJulian Elischer 				 * Intel and National parts report
1698a7280784SJulian Elischer 				 * differently on what they found.
1699f1bf08c2SJulian Elischer 				 */
1700f1bf08c2SJulian Elischer 				if ((sc->phy_primary_device == FXP_PHY_82555)
1701f1bf08c2SJulian Elischer 				|| (sc->phy_primary_device == FXP_PHY_82555B)) {
1702f1bf08c2SJulian Elischer 					flags = fxp_mdi_read(sc,
1703a7280784SJulian Elischer 						sc->phy_primary_addr,
1704a7280784SJulian Elischer 						FXP_PHY_USC);
1705f1bf08c2SJulian Elischer 
1706f1bf08c2SJulian Elischer 					if (flags & FXP_PHY_USC_SPEED)
1707f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_100_TX;
1708f1bf08c2SJulian Elischer 					else
1709f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_10_T;
1710f1bf08c2SJulian Elischer 
1711f1bf08c2SJulian Elischer 					if (flags & FXP_PHY_USC_DUPLEX)
1712f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_FDX;
1713a7280784SJulian Elischer 				} else { /* it's National. only know speed  */
1714a7280784SJulian Elischer 					flags = fxp_mdi_read(sc,
1715a7280784SJulian Elischer 						sc->phy_primary_addr,
1716a7280784SJulian Elischer 						FXP_DP83840_PAR);
1717a7280784SJulian Elischer 
1718a7280784SJulian Elischer 					if (flags & FXP_DP83840_PAR_SPEED_10)
1719a7280784SJulian Elischer 						ifmr->ifm_active |= IFM_10_T;
1720a7280784SJulian Elischer 					else
1721a7280784SJulian Elischer 						ifmr->ifm_active |= IFM_100_TX;
1722f1bf08c2SJulian Elischer 				}
1723a7280784SJulian Elischer 			}
1724a7280784SJulian Elischer 		} else { /* in manual mode.. just report what we were set to */
1725ba8c6fd5SDavid Greenman 			if (flags & FXP_PHY_BMCR_SPEED_100M)
1726ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_100_TX;
1727ba8c6fd5SDavid Greenman 			else
1728ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_10_T;
1729ba8c6fd5SDavid Greenman 
1730ba8c6fd5SDavid Greenman 			if (flags & FXP_PHY_BMCR_FULLDUPLEX)
1731ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_FDX;
1732ba8c6fd5SDavid Greenman 		}
1733ba8c6fd5SDavid Greenman 		break;
1734ba8c6fd5SDavid Greenman 
1735ba8c6fd5SDavid Greenman 	case FXP_PHY_80C24:
1736ba8c6fd5SDavid Greenman 	default:
1737ba8c6fd5SDavid Greenman 		ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; /* XXX IFM_AUTO ? */
1738ba8c6fd5SDavid Greenman 	}
1739ba8c6fd5SDavid Greenman }
1740ba8c6fd5SDavid Greenman 
1741a17c678eSDavid Greenman /*
1742a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1743a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1744a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1745dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1746a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1747a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1748a17c678eSDavid Greenman  */
1749a17c678eSDavid Greenman static int
1750a17c678eSDavid Greenman fxp_add_rfabuf(sc, oldm)
1751a17c678eSDavid Greenman 	struct fxp_softc *sc;
1752a17c678eSDavid Greenman 	struct mbuf *oldm;
1753a17c678eSDavid Greenman {
1754ba8c6fd5SDavid Greenman 	u_int32_t v;
1755a17c678eSDavid Greenman 	struct mbuf *m;
1756a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1757a17c678eSDavid Greenman 
1758a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1759a17c678eSDavid Greenman 	if (m != NULL) {
1760a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1761a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1762a17c678eSDavid Greenman 			m_freem(m);
1763eadd5e3aSDavid Greenman 			if (oldm == NULL)
1764eadd5e3aSDavid Greenman 				return 1;
1765a17c678eSDavid Greenman 			m = oldm;
1766eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1767a17c678eSDavid Greenman 		}
1768a17c678eSDavid Greenman 	} else {
1769eadd5e3aSDavid Greenman 		if (oldm == NULL)
1770a17c678eSDavid Greenman 			return 1;
1771eadd5e3aSDavid Greenman 		m = oldm;
1772eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1773eadd5e3aSDavid Greenman 	}
1774ba8c6fd5SDavid Greenman 
1775ba8c6fd5SDavid Greenman 	/*
1776ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1777ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1778ba8c6fd5SDavid Greenman 	 */
1779ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1780ba8c6fd5SDavid Greenman 
1781eadd5e3aSDavid Greenman 	/*
1782eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1783eadd5e3aSDavid Greenman 	 * data start past it.
1784eadd5e3aSDavid Greenman 	 */
1785a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1786eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
17874fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1788eadd5e3aSDavid Greenman 
1789ba8c6fd5SDavid Greenman 	/*
1790ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1791ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1792ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1793ba8c6fd5SDavid Greenman 	 */
17944fc1dda9SAndrew Gallatin 
1795a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1796a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1797a17c678eSDavid Greenman 	rfa->actual_size = 0;
1798ba8c6fd5SDavid Greenman 
1799ba8c6fd5SDavid Greenman 	v = -1;
18004fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
18014fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1802ba8c6fd5SDavid Greenman 
1803dfe61cf1SDavid Greenman 	/*
1804dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1805dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1806dfe61cf1SDavid Greenman 	 */
1807a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1808ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1809ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1810a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1811ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
18124fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1813a17c678eSDavid Greenman 		p_rfa->rfa_control &= ~FXP_RFA_CONTROL_EL;
1814a17c678eSDavid Greenman 	} else {
1815a17c678eSDavid Greenman 		sc->rfa_headm = m;
1816a17c678eSDavid Greenman 	}
1817a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1818a17c678eSDavid Greenman 
1819dfe61cf1SDavid Greenman 	return (m == oldm);
1820a17c678eSDavid Greenman }
1821a17c678eSDavid Greenman 
18226ebc3153SDavid Greenman static volatile int
1823ba8c6fd5SDavid Greenman fxp_mdi_read(sc, phy, reg)
1824ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1825dccee1a1SDavid Greenman 	int phy;
1826dccee1a1SDavid Greenman 	int reg;
1827dccee1a1SDavid Greenman {
1828dccee1a1SDavid Greenman 	int count = 10000;
18296ebc3153SDavid Greenman 	int value;
1830dccee1a1SDavid Greenman 
1831ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1832ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1833dccee1a1SDavid Greenman 
1834ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1835ba8c6fd5SDavid Greenman 	    && count--)
18366ebc3153SDavid Greenman 		DELAY(10);
1837dccee1a1SDavid Greenman 
1838dccee1a1SDavid Greenman 	if (count <= 0)
1839ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT ": fxp_mdi_read: timed out\n",
1840ba8c6fd5SDavid Greenman 		    FXP_ARGS(sc));
1841dccee1a1SDavid Greenman 
18426ebc3153SDavid Greenman 	return (value & 0xffff);
1843dccee1a1SDavid Greenman }
1844dccee1a1SDavid Greenman 
1845dccee1a1SDavid Greenman static void
1846ba8c6fd5SDavid Greenman fxp_mdi_write(sc, phy, reg, value)
1847ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1848dccee1a1SDavid Greenman 	int phy;
1849dccee1a1SDavid Greenman 	int reg;
1850dccee1a1SDavid Greenman 	int value;
1851dccee1a1SDavid Greenman {
1852dccee1a1SDavid Greenman 	int count = 10000;
1853dccee1a1SDavid Greenman 
1854ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1855ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1856ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1857dccee1a1SDavid Greenman 
1858ba8c6fd5SDavid Greenman 	while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1859ba8c6fd5SDavid Greenman 	    count--)
18606ebc3153SDavid Greenman 		DELAY(10);
1861dccee1a1SDavid Greenman 
1862dccee1a1SDavid Greenman 	if (count <= 0)
1863ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT ": fxp_mdi_write: timed out\n",
1864ba8c6fd5SDavid Greenman 		    FXP_ARGS(sc));
1865dccee1a1SDavid Greenman }
1866dccee1a1SDavid Greenman 
1867dccee1a1SDavid Greenman static int
1868a17c678eSDavid Greenman fxp_ioctl(ifp, command, data)
1869a17c678eSDavid Greenman 	struct ifnet *ifp;
1870ba8c6fd5SDavid Greenman 	FXP_IOCTLCMD_TYPE command;
1871a17c678eSDavid Greenman 	caddr_t data;
1872a17c678eSDavid Greenman {
18739b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1874a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1875a17c678eSDavid Greenman 	int s, error = 0;
1876a17c678eSDavid Greenman 
1877a17c678eSDavid Greenman 	s = splimp();
1878a17c678eSDavid Greenman 
1879a17c678eSDavid Greenman 	switch (command) {
1880a17c678eSDavid Greenman 
1881a17c678eSDavid Greenman 	case SIOCSIFADDR:
1882ba8c6fd5SDavid Greenman #if !defined(__NetBSD__)
1883a17c678eSDavid Greenman 	case SIOCGIFADDR:
1884fb583156SDavid Greenman 	case SIOCSIFMTU:
1885ba8c6fd5SDavid Greenman #endif
1886fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1887a17c678eSDavid Greenman 		break;
1888a17c678eSDavid Greenman 
1889a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1890397f9dfeSDavid Greenman 		sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1891a17c678eSDavid Greenman 
1892a17c678eSDavid Greenman 		/*
1893a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1894a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1895a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1896a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1897a17c678eSDavid Greenman 		 */
1898a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1899fb583156SDavid Greenman 			fxp_init(sc);
1900a17c678eSDavid Greenman 		} else {
1901a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
19024a5f1499SDavid Greenman 				fxp_stop(sc);
1903a17c678eSDavid Greenman 		}
1904a17c678eSDavid Greenman 		break;
1905a17c678eSDavid Greenman 
1906a17c678eSDavid Greenman 	case SIOCADDMULTI:
1907a17c678eSDavid Greenman 	case SIOCDELMULTI:
1908397f9dfeSDavid Greenman 		sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1909ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1910ba8c6fd5SDavid Greenman 		error = (command == SIOCADDMULTI) ?
1911ba8c6fd5SDavid Greenman 		    ether_addmulti(ifr, &sc->sc_ethercom) :
1912ba8c6fd5SDavid Greenman 		    ether_delmulti(ifr, &sc->sc_ethercom);
1913ba8c6fd5SDavid Greenman 
1914ba8c6fd5SDavid Greenman 		if (error == ENETRESET) {
1915ba8c6fd5SDavid Greenman 			/*
1916ba8c6fd5SDavid Greenman 			 * Multicast list has changed; set the hardware
1917ba8c6fd5SDavid Greenman 			 * filter accordingly.
1918ba8c6fd5SDavid Greenman 			 */
1919397f9dfeSDavid Greenman 			if (!sc->all_mcasts)
1920397f9dfeSDavid Greenman 				fxp_mc_setup(sc);
1921397f9dfeSDavid Greenman 			/*
1922397f9dfeSDavid Greenman 			 * fxp_mc_setup() can turn on all_mcasts if we run
1923397f9dfeSDavid Greenman 			 * out of space, so check it again rather than else {}.
1924397f9dfeSDavid Greenman 			 */
1925397f9dfeSDavid Greenman 			if (sc->all_mcasts)
1926ba8c6fd5SDavid Greenman 				fxp_init(sc);
1927ba8c6fd5SDavid Greenman 			error = 0;
1928ba8c6fd5SDavid Greenman 		}
1929ba8c6fd5SDavid Greenman #else /* __FreeBSD__ */
1930a17c678eSDavid Greenman 		/*
1931a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1932a17c678eSDavid Greenman 		 * accordingly.
1933a17c678eSDavid Greenman 		 */
1934397f9dfeSDavid Greenman 		if (!sc->all_mcasts)
1935397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1936397f9dfeSDavid Greenman 		/*
1937397f9dfeSDavid Greenman 		 * fxp_mc_setup() can turn on sc->all_mcasts, so check it
1938397f9dfeSDavid Greenman 		 * again rather than else {}.
1939397f9dfeSDavid Greenman 		 */
1940397f9dfeSDavid Greenman 		if (sc->all_mcasts)
1941fb583156SDavid Greenman 			fxp_init(sc);
1942a17c678eSDavid Greenman 		error = 0;
1943ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
1944ba8c6fd5SDavid Greenman 		break;
1945ba8c6fd5SDavid Greenman 
1946ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1947ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1948ba8c6fd5SDavid Greenman 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1949a17c678eSDavid Greenman 		break;
1950a17c678eSDavid Greenman 
1951a17c678eSDavid Greenman 	default:
1952a17c678eSDavid Greenman 		error = EINVAL;
1953a17c678eSDavid Greenman 	}
1954a17c678eSDavid Greenman 	(void) splx(s);
1955a17c678eSDavid Greenman 	return (error);
1956a17c678eSDavid Greenman }
1957397f9dfeSDavid Greenman 
1958397f9dfeSDavid Greenman /*
1959397f9dfeSDavid Greenman  * Program the multicast filter.
1960397f9dfeSDavid Greenman  *
1961397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1962397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
19633114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1964397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1965dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1966397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1967397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1968397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1969397f9dfeSDavid Greenman  *
1970397f9dfeSDavid Greenman  * This function must be called at splimp.
1971397f9dfeSDavid Greenman  */
1972397f9dfeSDavid Greenman static void
1973397f9dfeSDavid Greenman fxp_mc_setup(sc)
1974397f9dfeSDavid Greenman 	struct fxp_softc *sc;
1975397f9dfeSDavid Greenman {
1976397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1977397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1978397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1979397f9dfeSDavid Greenman 	int nmcasts;
1980397f9dfeSDavid Greenman 
19813114fdb4SDavid Greenman 	/*
19823114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
19833114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
19843114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
19853114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
19863114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
19873114fdb4SDavid Greenman 	 */
1988397f9dfeSDavid Greenman 	if (sc->tx_queued) {
19893114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
19903114fdb4SDavid Greenman 
19913114fdb4SDavid Greenman 		/*
19923114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
19933114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
19943114fdb4SDavid Greenman 		 */
19953114fdb4SDavid Greenman 		if (sc->need_mcsetup)
19963114fdb4SDavid Greenman 			return;
1997397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
19983114fdb4SDavid Greenman 
19993114fdb4SDavid Greenman 		/*
20003114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
20013114fdb4SDavid Greenman 		 * TX commands have been processed.
20023114fdb4SDavid Greenman 		 */
20033114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
20043114fdb4SDavid Greenman 		txp->mb_head = NULL;
20053114fdb4SDavid Greenman 		txp->cb_status = 0;
20063114fdb4SDavid Greenman 		txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
20073114fdb4SDavid Greenman 		/*
20083114fdb4SDavid Greenman 		 * Advance the end of list forward.
20093114fdb4SDavid Greenman 		 */
20103114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
20113114fdb4SDavid Greenman 		sc->cbl_last = txp;
20123114fdb4SDavid Greenman 		sc->tx_queued++;
20133114fdb4SDavid Greenman 		/*
20143114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
20153114fdb4SDavid Greenman 		 */
20163114fdb4SDavid Greenman 		fxp_scb_wait(sc);
20173114fdb4SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
20183114fdb4SDavid Greenman 		/*
20193114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
20203114fdb4SDavid Greenman 		 * card again.
20213114fdb4SDavid Greenman 		 */
20223114fdb4SDavid Greenman 		ifp->if_timer = 5;
20233114fdb4SDavid Greenman 
2024397f9dfeSDavid Greenman 		return;
2025397f9dfeSDavid Greenman 	}
2026397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2027397f9dfeSDavid Greenman 
2028397f9dfeSDavid Greenman 	/*
2029397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2030397f9dfeSDavid Greenman 	 */
2031397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
2032397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
2033397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
20343114fdb4SDavid Greenman 	mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
2035397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
2036397f9dfeSDavid Greenman 
2037397f9dfeSDavid Greenman 	nmcasts = 0;
2038397f9dfeSDavid Greenman 	if (!sc->all_mcasts) {
2039397f9dfeSDavid Greenman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
2040397f9dfeSDavid Greenman 		    ifma = ifma->ifma_link.le_next) {
2041397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
2042397f9dfeSDavid Greenman 				continue;
2043397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
2044397f9dfeSDavid Greenman 				sc->all_mcasts = 1;
2045397f9dfeSDavid Greenman 				nmcasts = 0;
2046397f9dfeSDavid Greenman 				break;
2047397f9dfeSDavid Greenman 			}
2048397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
20498aef1712SMatthew Dillon 			    (volatile void *) &sc->mcsp->mc_addr[nmcasts][0], 6);
2050397f9dfeSDavid Greenman 			nmcasts++;
2051397f9dfeSDavid Greenman 		}
2052397f9dfeSDavid Greenman 	}
2053397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
2054397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2055397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2056397f9dfeSDavid Greenman 
2057397f9dfeSDavid Greenman 	/*
2058397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2059397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2060397f9dfeSDavid Greenman 	 */
2061397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
2062397f9dfeSDavid Greenman 	    FXP_SCB_CUS_ACTIVE) ;
2063397f9dfeSDavid Greenman 
2064397f9dfeSDavid Greenman 	/*
2065397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2066397f9dfeSDavid Greenman 	 */
2067397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2068397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
2069397f9dfeSDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
2070397f9dfeSDavid Greenman 
20713114fdb4SDavid Greenman 	ifp->if_timer = 2;
2072397f9dfeSDavid Greenman 	return;
2073397f9dfeSDavid Greenman }
2074