xref: /freebsd/sys/dev/fxp/if_fxp.c (revision aed53495985682b369c050d28c7469767e6e6351)
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 
108ba8c6fd5SDavid Greenman /*
109ba8c6fd5SDavid Greenman  * NOTE!  On the Alpha, we have an alignment constraint.  The
110ba8c6fd5SDavid Greenman  * card DMAs the packet immediately following the RFA.  However,
111ba8c6fd5SDavid Greenman  * the first thing in the packet is a 14-byte Ethernet header.
112ba8c6fd5SDavid Greenman  * This means that the packet is misaligned.  To compensate,
113ba8c6fd5SDavid Greenman  * we actually offset the RFA 2 bytes into the cluster.  This
114ba8c6fd5SDavid Greenman  * alignes the packet after the Ethernet header at a 32-bit
115ba8c6fd5SDavid Greenman  * boundary.  HOWEVER!  This means that the RFA is misaligned!
116ba8c6fd5SDavid Greenman  */
117ba8c6fd5SDavid Greenman #define	RFA_ALIGNMENT_FUDGE	2
118ba8c6fd5SDavid Greenman 
119ba8c6fd5SDavid Greenman /*
120ba8c6fd5SDavid Greenman  * Inline function to copy a 16-bit aligned 32-bit quantity.
121ba8c6fd5SDavid Greenman  */
122ba8c6fd5SDavid Greenman static __inline void fxp_lwcopy __P((volatile u_int32_t *,
123ba8c6fd5SDavid Greenman 	volatile u_int32_t *));
124ba8c6fd5SDavid Greenman static __inline void
125ba8c6fd5SDavid Greenman fxp_lwcopy(src, dst)
126ba8c6fd5SDavid Greenman 	volatile u_int32_t *src, *dst;
127ba8c6fd5SDavid Greenman {
128aed53495SDavid Greenman #ifdef __i386__
129aed53495SDavid Greenman 	*dst = *src;
130aed53495SDavid Greenman #else
131fe08c21aSMatthew Dillon 	volatile u_int16_t *a = (volatile u_int16_t *)src;
132fe08c21aSMatthew Dillon 	volatile u_int16_t *b = (volatile u_int16_t *)dst;
133ba8c6fd5SDavid Greenman 
134ba8c6fd5SDavid Greenman 	b[0] = a[0];
135ba8c6fd5SDavid Greenman 	b[1] = a[1];
136aed53495SDavid Greenman #endif
137ba8c6fd5SDavid Greenman }
138a17c678eSDavid Greenman 
139a17c678eSDavid Greenman /*
140a17c678eSDavid Greenman  * Template for default configuration parameters.
141a17c678eSDavid Greenman  * See struct fxp_cb_config for the bit definitions.
142a17c678eSDavid Greenman  */
143a17c678eSDavid Greenman static u_char fxp_cb_config_template[] = {
144a17c678eSDavid Greenman 	0x0, 0x0,		/* cb_status */
145a17c678eSDavid Greenman 	0x80, 0x2,		/* cb_command */
146a17c678eSDavid Greenman 	0xff, 0xff, 0xff, 0xff,	/* link_addr */
147a17c678eSDavid Greenman 	0x16,	/*  0 */
148a17c678eSDavid Greenman 	0x8,	/*  1 */
149a17c678eSDavid Greenman 	0x0,	/*  2 */
150a17c678eSDavid Greenman 	0x0,	/*  3 */
151a17c678eSDavid Greenman 	0x0,	/*  4 */
152a17c678eSDavid Greenman 	0x80,	/*  5 */
153a17c678eSDavid Greenman 	0xb2,	/*  6 */
154a17c678eSDavid Greenman 	0x3,	/*  7 */
155a17c678eSDavid Greenman 	0x1,	/*  8 */
156a17c678eSDavid Greenman 	0x0,	/*  9 */
157a17c678eSDavid Greenman 	0x26,	/* 10 */
158a17c678eSDavid Greenman 	0x0,	/* 11 */
159a17c678eSDavid Greenman 	0x60,	/* 12 */
160a17c678eSDavid Greenman 	0x0,	/* 13 */
161a17c678eSDavid Greenman 	0xf2,	/* 14 */
162a17c678eSDavid Greenman 	0x48,	/* 15 */
163a17c678eSDavid Greenman 	0x0,	/* 16 */
164a17c678eSDavid Greenman 	0x40,	/* 17 */
165a17c678eSDavid Greenman 	0xf3,	/* 18 */
166a17c678eSDavid Greenman 	0x0,	/* 19 */
167a17c678eSDavid Greenman 	0x3f,	/* 20 */
168397f9dfeSDavid Greenman 	0x5	/* 21 */
169a17c678eSDavid Greenman };
170a17c678eSDavid Greenman 
171ba8c6fd5SDavid Greenman /* Supported media types. */
172ba8c6fd5SDavid Greenman struct fxp_supported_media {
173ba8c6fd5SDavid Greenman 	const int	fsm_phy;	/* PHY type */
174ba8c6fd5SDavid Greenman 	const int	*fsm_media;	/* the media array */
175ba8c6fd5SDavid Greenman 	const int	fsm_nmedia;	/* the number of supported media */
176ba8c6fd5SDavid Greenman 	const int	fsm_defmedia;	/* default media for this PHY */
177ba8c6fd5SDavid Greenman };
178ba8c6fd5SDavid Greenman 
179303b270bSEivind Eklund static const int fxp_media_standard[] = {
180ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_10_T,
181ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_10_T|IFM_FDX,
182ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_100_TX,
183ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_100_TX|IFM_FDX,
184ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_AUTO,
185ba8c6fd5SDavid Greenman };
186ba8c6fd5SDavid Greenman #define	FXP_MEDIA_STANDARD_DEFMEDIA	(IFM_ETHER|IFM_AUTO)
187ba8c6fd5SDavid Greenman 
188303b270bSEivind Eklund static const int fxp_media_default[] = {
189ba8c6fd5SDavid Greenman 	IFM_ETHER|IFM_MANUAL,		/* XXX IFM_AUTO ? */
190ba8c6fd5SDavid Greenman };
191ba8c6fd5SDavid Greenman #define	FXP_MEDIA_DEFAULT_DEFMEDIA	(IFM_ETHER|IFM_MANUAL)
192ba8c6fd5SDavid Greenman 
193303b270bSEivind Eklund static const struct fxp_supported_media fxp_media[] = {
194ba8c6fd5SDavid Greenman 	{ FXP_PHY_DP83840, fxp_media_standard,
195ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
196ba8c6fd5SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
197ba8c6fd5SDavid Greenman 	{ FXP_PHY_DP83840A, fxp_media_standard,
198ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
199ba8c6fd5SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
20092924291SDavid Greenman 	{ FXP_PHY_82553A, fxp_media_standard,
20192924291SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
20292924291SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
20392924291SDavid Greenman 	{ FXP_PHY_82553C, fxp_media_standard,
20492924291SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
20592924291SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
206ba8c6fd5SDavid Greenman 	{ FXP_PHY_82555, fxp_media_standard,
207ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
208ba8c6fd5SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
20992924291SDavid Greenman 	{ FXP_PHY_82555B, fxp_media_standard,
21092924291SDavid Greenman 	  sizeof(fxp_media_standard) / sizeof(fxp_media_standard[0]),
21192924291SDavid Greenman 	  FXP_MEDIA_STANDARD_DEFMEDIA },
212ba8c6fd5SDavid Greenman 	{ FXP_PHY_80C24, fxp_media_default,
213ba8c6fd5SDavid Greenman 	  sizeof(fxp_media_default) / sizeof(fxp_media_default[0]),
214ba8c6fd5SDavid Greenman 	  FXP_MEDIA_DEFAULT_DEFMEDIA },
215ba8c6fd5SDavid Greenman };
216ba8c6fd5SDavid Greenman #define	NFXPMEDIA (sizeof(fxp_media) / sizeof(fxp_media[0]))
217ba8c6fd5SDavid Greenman 
218ba8c6fd5SDavid Greenman static int fxp_mediachange	__P((struct ifnet *));
219ba8c6fd5SDavid Greenman static void fxp_mediastatus	__P((struct ifnet *, struct ifmediareq *));
220303b270bSEivind Eklund static void fxp_set_media	__P((struct fxp_softc *, int));
221c1087c13SBruce Evans static __inline void fxp_scb_wait __P((struct fxp_softc *));
222ba8c6fd5SDavid Greenman static FXP_INTR_TYPE fxp_intr	__P((void *));
223a17c678eSDavid Greenman static void fxp_start		__P((struct ifnet *));
224ba8c6fd5SDavid Greenman static int fxp_ioctl		__P((struct ifnet *,
225ba8c6fd5SDavid Greenman 				     FXP_IOCTLCMD_TYPE, caddr_t));
226fb583156SDavid Greenman static void fxp_init		__P((void *));
2274a5f1499SDavid Greenman static void fxp_stop		__P((struct fxp_softc *));
2284a5f1499SDavid Greenman static void fxp_watchdog	__P((struct ifnet *));
229a17c678eSDavid Greenman static int fxp_add_rfabuf	__P((struct fxp_softc *, struct mbuf *));
230ba8c6fd5SDavid Greenman static int fxp_mdi_read		__P((struct fxp_softc *, int, int));
231ba8c6fd5SDavid Greenman static void fxp_mdi_write	__P((struct fxp_softc *, int, int, int));
232e9bf2fa7SDavid Greenman static void fxp_autosize_eeprom __P((struct fxp_softc *));
233ba8c6fd5SDavid Greenman static void fxp_read_eeprom	__P((struct fxp_softc *, u_int16_t *,
234ba8c6fd5SDavid Greenman 				     int, int));
235ba8c6fd5SDavid Greenman static int fxp_attach_common	__P((struct fxp_softc *, u_int8_t *));
236303b270bSEivind Eklund static void fxp_stats_update	__P((void *));
237397f9dfeSDavid Greenman static void fxp_mc_setup	__P((struct fxp_softc *));
238a17c678eSDavid Greenman 
239a17c678eSDavid Greenman /*
240f9be9005SDavid Greenman  * Set initial transmit threshold at 64 (512 bytes). This is
241f9be9005SDavid Greenman  * increased by 64 (512 bytes) at a time, to maximum of 192
242f9be9005SDavid Greenman  * (1536 bytes), if an underrun occurs.
243f9be9005SDavid Greenman  */
244f9be9005SDavid Greenman static int tx_threshold = 64;
245f9be9005SDavid Greenman 
246f9be9005SDavid Greenman /*
247a17c678eSDavid Greenman  * Number of transmit control blocks. This determines the number
248a17c678eSDavid Greenman  * of transmit buffers that can be chained in the CB list.
249a17c678eSDavid Greenman  * This must be a power of two.
250a17c678eSDavid Greenman  */
2511cd443acSDavid Greenman #define FXP_NTXCB	128
252a17c678eSDavid Greenman 
253a17c678eSDavid Greenman /*
2543114fdb4SDavid Greenman  * Number of completed TX commands at which point an interrupt
2553114fdb4SDavid Greenman  * will be generated to garbage collect the attached buffers.
2563114fdb4SDavid Greenman  * Must be at least one less than FXP_NTXCB, and should be
2573114fdb4SDavid Greenman  * enough less so that the transmitter doesn't becomes idle
2583114fdb4SDavid Greenman  * during the buffer rundown (which would reduce performance).
2593114fdb4SDavid Greenman  */
2603114fdb4SDavid Greenman #define FXP_CXINT_THRESH 120
2613114fdb4SDavid Greenman 
2623114fdb4SDavid Greenman /*
263a17c678eSDavid Greenman  * TxCB list index mask. This is used to do list wrap-around.
264a17c678eSDavid Greenman  */
265a17c678eSDavid Greenman #define FXP_TXCB_MASK	(FXP_NTXCB - 1)
266a17c678eSDavid Greenman 
267a17c678eSDavid Greenman /*
268a17c678eSDavid Greenman  * Number of receive frame area buffers. These are large so chose
269a17c678eSDavid Greenman  * wisely.
270a17c678eSDavid Greenman  */
2716f5818b0SDavid Greenman #define FXP_NRFABUFS	64
272a17c678eSDavid Greenman 
273dfe61cf1SDavid Greenman /*
274397f9dfeSDavid Greenman  * Maximum number of seconds that the receiver can be idle before we
275397f9dfeSDavid Greenman  * assume it's dead and attempt to reset it by reprogramming the
276397f9dfeSDavid Greenman  * multicast filter. This is part of a work-around for a bug in the
277397f9dfeSDavid Greenman  * NIC. See fxp_stats_update().
278397f9dfeSDavid Greenman  */
279397f9dfeSDavid Greenman #define FXP_MAX_RX_IDLE	15
280397f9dfeSDavid Greenman 
281397f9dfeSDavid Greenman /*
282dfe61cf1SDavid Greenman  * Wait for the previous command to be accepted (but not necessarily
283dfe61cf1SDavid Greenman  * completed).
284dfe61cf1SDavid Greenman  */
285c1087c13SBruce Evans static __inline void
286ba8c6fd5SDavid Greenman fxp_scb_wait(sc)
287ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
288a17c678eSDavid Greenman {
289a17c678eSDavid Greenman 	int i = 10000;
290a17c678eSDavid Greenman 
291397f9dfeSDavid Greenman 	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i);
292a17c678eSDavid Greenman }
293a17c678eSDavid Greenman 
294ba8c6fd5SDavid Greenman /*************************************************************
295ba8c6fd5SDavid Greenman  * Operating system-specific autoconfiguration glue
296ba8c6fd5SDavid Greenman  *************************************************************/
297ba8c6fd5SDavid Greenman 
298ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
299ba8c6fd5SDavid Greenman 
300ba8c6fd5SDavid Greenman #ifdef __BROKEN_INDIRECT_CONFIG
301ba8c6fd5SDavid Greenman static int fxp_match __P((struct device *, void *, void *));
302ba8c6fd5SDavid Greenman #else
303ba8c6fd5SDavid Greenman static int fxp_match __P((struct device *, struct cfdata *, void *));
304ba8c6fd5SDavid Greenman #endif
305ba8c6fd5SDavid Greenman static void fxp_attach __P((struct device *, struct device *, void *));
306ba8c6fd5SDavid Greenman 
307ba8c6fd5SDavid Greenman static void	fxp_shutdown __P((void *));
308ba8c6fd5SDavid Greenman 
309ba8c6fd5SDavid Greenman /* Compensate for lack of a generic ether_ioctl() */
310ba8c6fd5SDavid Greenman static int	fxp_ether_ioctl __P((struct ifnet *,
311ba8c6fd5SDavid Greenman 				    FXP_IOCTLCMD_TYPE, caddr_t));
312ba8c6fd5SDavid Greenman #define	ether_ioctl	fxp_ether_ioctl
313ba8c6fd5SDavid Greenman 
314ba8c6fd5SDavid Greenman struct cfattach fxp_ca = {
315ba8c6fd5SDavid Greenman 	sizeof(struct fxp_softc), fxp_match, fxp_attach
316ba8c6fd5SDavid Greenman };
317ba8c6fd5SDavid Greenman 
318ba8c6fd5SDavid Greenman struct cfdriver fxp_cd = {
319ba8c6fd5SDavid Greenman 	NULL, "fxp", DV_IFNET
320ba8c6fd5SDavid Greenman };
321ba8c6fd5SDavid Greenman 
322ba8c6fd5SDavid Greenman /*
323ba8c6fd5SDavid Greenman  * Check if a device is an 82557.
324ba8c6fd5SDavid Greenman  */
325ba8c6fd5SDavid Greenman static int
326ba8c6fd5SDavid Greenman fxp_match(parent, match, aux)
327ba8c6fd5SDavid Greenman 	struct device *parent;
328ba8c6fd5SDavid Greenman #ifdef __BROKEN_INDIRECT_CONFIG
329ba8c6fd5SDavid Greenman 	void *match;
330ba8c6fd5SDavid Greenman #else
331ba8c6fd5SDavid Greenman 	struct cfdata *match;
332ba8c6fd5SDavid Greenman #endif
333ba8c6fd5SDavid Greenman 	void *aux;
334ba8c6fd5SDavid Greenman {
335ba8c6fd5SDavid Greenman 	struct pci_attach_args *pa = aux;
336ba8c6fd5SDavid Greenman 
337ba8c6fd5SDavid Greenman 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
338ba8c6fd5SDavid Greenman 		return (0);
339ba8c6fd5SDavid Greenman 
340ba8c6fd5SDavid Greenman 	switch (PCI_PRODUCT(pa->pa_id)) {
341ba8c6fd5SDavid Greenman 	case PCI_PRODUCT_INTEL_82557:
342ba8c6fd5SDavid Greenman 		return (1);
343ba8c6fd5SDavid Greenman 	}
344ba8c6fd5SDavid Greenman 
345ba8c6fd5SDavid Greenman 	return (0);
346ba8c6fd5SDavid Greenman }
347ba8c6fd5SDavid Greenman 
348ba8c6fd5SDavid Greenman static void
349ba8c6fd5SDavid Greenman fxp_attach(parent, self, aux)
350ba8c6fd5SDavid Greenman 	struct device *parent, *self;
351ba8c6fd5SDavid Greenman 	void *aux;
352ba8c6fd5SDavid Greenman {
353ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = (struct fxp_softc *)self;
354ba8c6fd5SDavid Greenman 	struct pci_attach_args *pa = aux;
355ba8c6fd5SDavid Greenman 	pci_chipset_tag_t pc = pa->pa_pc;
356ba8c6fd5SDavid Greenman 	pci_intr_handle_t ih;
357ba8c6fd5SDavid Greenman 	const char *intrstr = NULL;
358ba8c6fd5SDavid Greenman 	u_int8_t enaddr[6];
359ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
360ba8c6fd5SDavid Greenman 
361ba8c6fd5SDavid Greenman 	/*
362ba8c6fd5SDavid Greenman 	 * Map control/status registers.
363ba8c6fd5SDavid Greenman 	 */
364ba8c6fd5SDavid Greenman 	if (pci_mapreg_map(pa, FXP_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0,
365ba8c6fd5SDavid Greenman 	    &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
366ba8c6fd5SDavid Greenman 		printf(": can't map registers\n");
367ba8c6fd5SDavid Greenman 		return;
368ba8c6fd5SDavid Greenman 	}
369ba8c6fd5SDavid Greenman 	printf(": Intel EtherExpress Pro 10/100B Ethernet\n");
370ba8c6fd5SDavid Greenman 
371ba8c6fd5SDavid Greenman 	/*
372ba8c6fd5SDavid Greenman 	 * Allocate our interrupt.
373ba8c6fd5SDavid Greenman 	 */
374ba8c6fd5SDavid Greenman 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
375ba8c6fd5SDavid Greenman 	    pa->pa_intrline, &ih)) {
376ba8c6fd5SDavid Greenman 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
377ba8c6fd5SDavid Greenman 		return;
378ba8c6fd5SDavid Greenman 	}
379ba8c6fd5SDavid Greenman 	intrstr = pci_intr_string(pc, ih);
380ba8c6fd5SDavid Greenman 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
381ba8c6fd5SDavid Greenman 	if (sc->sc_ih == NULL) {
382ba8c6fd5SDavid Greenman 		printf("%s: couldn't establish interrupt",
383ba8c6fd5SDavid Greenman 		    sc->sc_dev.dv_xname);
384ba8c6fd5SDavid Greenman 		if (intrstr != NULL)
385ba8c6fd5SDavid Greenman 			printf(" at %s", intrstr);
386ba8c6fd5SDavid Greenman 		printf("\n");
387ba8c6fd5SDavid Greenman 		return;
388ba8c6fd5SDavid Greenman 	}
389ba8c6fd5SDavid Greenman 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
390ba8c6fd5SDavid Greenman 
391ba8c6fd5SDavid Greenman 	/* Do generic parts of attach. */
392ba8c6fd5SDavid Greenman 	if (fxp_attach_common(sc, enaddr)) {
393ba8c6fd5SDavid Greenman 		/* Failed! */
394ba8c6fd5SDavid Greenman 		return;
395ba8c6fd5SDavid Greenman 	}
396ba8c6fd5SDavid Greenman 
397ba8c6fd5SDavid Greenman 	printf("%s: Ethernet address %s%s\n", sc->sc_dev.dv_xname,
398ba8c6fd5SDavid Greenman 	    ether_sprintf(enaddr), sc->phy_10Mbps_only ? ", 10Mbps" : "");
399ba8c6fd5SDavid Greenman 
400ba8c6fd5SDavid Greenman 	ifp = &sc->sc_ethercom.ec_if;
401ba8c6fd5SDavid Greenman 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
402ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
403ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
404ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
405ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
406ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
407ba8c6fd5SDavid Greenman 
408ba8c6fd5SDavid Greenman 	/*
409ba8c6fd5SDavid Greenman 	 * Attach the interface.
410ba8c6fd5SDavid Greenman 	 */
411ba8c6fd5SDavid Greenman 	if_attach(ifp);
412483b9871SDavid Greenman 	/*
4133114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
4143114fdb4SDavid Greenman 	 * TX descriptors.
415483b9871SDavid Greenman 	 */
4163114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
417ba8c6fd5SDavid Greenman 	ether_ifattach(ifp, enaddr);
418ba8c6fd5SDavid Greenman 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
419ba8c6fd5SDavid Greenman 	    sizeof(struct ether_header));
420ba8c6fd5SDavid Greenman 
421ba8c6fd5SDavid Greenman 	/*
422ba8c6fd5SDavid Greenman 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
423ba8c6fd5SDavid Greenman 	 * doing do could allow DMA to corrupt kernel memory during the
424ba8c6fd5SDavid Greenman 	 * reboot before the driver initializes.
425ba8c6fd5SDavid Greenman 	 */
426ba8c6fd5SDavid Greenman 	shutdownhook_establish(fxp_shutdown, sc);
427ba8c6fd5SDavid Greenman }
428ba8c6fd5SDavid Greenman 
429ba8c6fd5SDavid Greenman /*
430ba8c6fd5SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
431ba8c6fd5SDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
432ba8c6fd5SDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
433ba8c6fd5SDavid Greenman  */
434ba8c6fd5SDavid Greenman static void
435ba8c6fd5SDavid Greenman fxp_shutdown(sc)
436ba8c6fd5SDavid Greenman 	void *sc;
437ba8c6fd5SDavid Greenman {
438ba8c6fd5SDavid Greenman 	fxp_stop((struct fxp_softc *) sc);
439ba8c6fd5SDavid Greenman }
440ba8c6fd5SDavid Greenman 
441ba8c6fd5SDavid Greenman static int
442ba8c6fd5SDavid Greenman fxp_ether_ioctl(ifp, cmd, data)
443ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
444ba8c6fd5SDavid Greenman 	FXP_IOCTLCMD_TYPE cmd;
445ba8c6fd5SDavid Greenman 	caddr_t data;
446ba8c6fd5SDavid Greenman {
447ba8c6fd5SDavid Greenman 	struct ifaddr *ifa = (struct ifaddr *) data;
448ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
449ba8c6fd5SDavid Greenman 
450ba8c6fd5SDavid Greenman 	switch (cmd) {
451ba8c6fd5SDavid Greenman 	case SIOCSIFADDR:
452ba8c6fd5SDavid Greenman 		ifp->if_flags |= IFF_UP;
453ba8c6fd5SDavid Greenman 
454ba8c6fd5SDavid Greenman 		switch (ifa->ifa_addr->sa_family) {
455ba8c6fd5SDavid Greenman #ifdef INET
456ba8c6fd5SDavid Greenman 		case AF_INET:
457ba8c6fd5SDavid Greenman 			fxp_init(sc);
458ba8c6fd5SDavid Greenman 			arp_ifinit(ifp, ifa);
459ba8c6fd5SDavid Greenman 			break;
460ba8c6fd5SDavid Greenman #endif
461ba8c6fd5SDavid Greenman #ifdef NS
462ba8c6fd5SDavid Greenman 		case AF_NS:
463ba8c6fd5SDavid Greenman 		    {
464ba8c6fd5SDavid Greenman 			 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
465ba8c6fd5SDavid Greenman 
466ba8c6fd5SDavid Greenman 			 if (ns_nullhost(*ina))
467ba8c6fd5SDavid Greenman 				ina->x_host = *(union ns_host *)
468ba8c6fd5SDavid Greenman 				    LLADDR(ifp->if_sadl);
469ba8c6fd5SDavid Greenman 			 else
470ba8c6fd5SDavid Greenman 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
471ba8c6fd5SDavid Greenman 				    ifp->if_addrlen);
472ba8c6fd5SDavid Greenman 			 /* Set new address. */
473ba8c6fd5SDavid Greenman 			 fxp_init(sc);
474ba8c6fd5SDavid Greenman 			 break;
475ba8c6fd5SDavid Greenman 		    }
476ba8c6fd5SDavid Greenman #endif
477ba8c6fd5SDavid Greenman 		default:
478ba8c6fd5SDavid Greenman 			fxp_init(sc);
479ba8c6fd5SDavid Greenman 			break;
480ba8c6fd5SDavid Greenman 		}
481ba8c6fd5SDavid Greenman 		break;
482ba8c6fd5SDavid Greenman 
483ba8c6fd5SDavid Greenman 	default:
484ba8c6fd5SDavid Greenman 		return (EINVAL);
485ba8c6fd5SDavid Greenman 	}
486ba8c6fd5SDavid Greenman 
487ba8c6fd5SDavid Greenman 	return (0);
488ba8c6fd5SDavid Greenman }
489ba8c6fd5SDavid Greenman 
490ba8c6fd5SDavid Greenman #else /* __FreeBSD__ */
491ba8c6fd5SDavid Greenman 
492dfe61cf1SDavid Greenman /*
493dfe61cf1SDavid Greenman  * Return identification string if this is device is ours.
494dfe61cf1SDavid Greenman  */
4956182fdbdSPeter Wemm static int
4966182fdbdSPeter Wemm fxp_probe(device_t dev)
497a17c678eSDavid Greenman {
49855ce7b51SDavid Greenman 	if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
49955ce7b51SDavid Greenman 		switch (pci_get_device(dev)) {
50055ce7b51SDavid Greenman 
50155ce7b51SDavid Greenman 		case FXP_DEVICEID_i82557:
50255ce7b51SDavid Greenman 			device_set_desc(dev, "Intel Pro 10/100B/100+ Ethernet");
5036182fdbdSPeter Wemm 			return 0;
50455ce7b51SDavid Greenman 		case FXP_DEVICEID_i82559:
505dd68ef16SPeter Wemm 			device_set_desc(dev, "Intel InBusiness 10/100 Ethernet");
506dd68ef16SPeter Wemm 			return 0;
50755ce7b51SDavid Greenman 		case FXP_DEVICEID_i82559ER:
50855ce7b51SDavid Greenman 			device_set_desc(dev, "Intel Embedded 10/100 Ethernet");
50955ce7b51SDavid Greenman 			return 0;
51055ce7b51SDavid Greenman 		default:
51155ce7b51SDavid Greenman 			break;
51255ce7b51SDavid Greenman 		}
513dd68ef16SPeter Wemm 	}
514a17c678eSDavid Greenman 
5156182fdbdSPeter Wemm 	return ENXIO;
5166182fdbdSPeter Wemm }
5176182fdbdSPeter Wemm 
5186182fdbdSPeter Wemm static int
5196182fdbdSPeter Wemm fxp_attach(device_t dev)
520a17c678eSDavid Greenman {
5216182fdbdSPeter Wemm 	int error = 0;
5226182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
523ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
524ba8c6fd5SDavid Greenman 	int s;
525df373873SWes Peters 	u_long val;
5266182fdbdSPeter Wemm 	int rid;
527a17c678eSDavid Greenman 
5286c951b44SJustin T. Gibbs 	callout_handle_init(&sc->stat_ch);
529a17c678eSDavid Greenman 
530a17c678eSDavid Greenman 	s = splimp();
531a17c678eSDavid Greenman 
532dfe61cf1SDavid Greenman 	/*
533df373873SWes Peters 	 * Enable bus mastering.
534df373873SWes Peters 	 */
5356182fdbdSPeter Wemm 	val = pci_read_config(dev, PCIR_COMMAND, 2);
536df373873SWes Peters 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
5376182fdbdSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, val, 2);
538df373873SWes Peters 
539df373873SWes Peters 	/*
540dfe61cf1SDavid Greenman 	 * Map control/status registers.
541dfe61cf1SDavid Greenman 	 */
5426182fdbdSPeter Wemm 	rid = FXP_PCI_MMBA;
5436182fdbdSPeter Wemm 	sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
5446182fdbdSPeter Wemm 				     0, ~0, 1, RF_ACTIVE);
5456182fdbdSPeter Wemm 	if (!sc->mem) {
5466182fdbdSPeter Wemm 		device_printf(dev, "could not map memory\n");
5476182fdbdSPeter Wemm 		error = ENXIO;
548a17c678eSDavid Greenman 		goto fail;
549a17c678eSDavid Greenman         }
5504fc1dda9SAndrew Gallatin 
5514fc1dda9SAndrew Gallatin 	sc->sc_st = rman_get_bustag(sc->mem);
5524fc1dda9SAndrew Gallatin 	sc->sc_sh = rman_get_bushandle(sc->mem);
553a17c678eSDavid Greenman 
554a17c678eSDavid Greenman 	/*
555dfe61cf1SDavid Greenman 	 * Allocate our interrupt.
556dfe61cf1SDavid Greenman 	 */
5576182fdbdSPeter Wemm 	rid = 0;
5586182fdbdSPeter Wemm 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
5596182fdbdSPeter Wemm 				 RF_SHAREABLE | RF_ACTIVE);
5606182fdbdSPeter Wemm 	if (sc->irq == NULL) {
5616182fdbdSPeter Wemm 		device_printf(dev, "could not map interrupt\n");
5626182fdbdSPeter Wemm 		error = ENXIO;
5636182fdbdSPeter Wemm 		goto fail;
5646182fdbdSPeter Wemm 	}
5656182fdbdSPeter Wemm 
566566643e3SDoug Rabson 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
567566643e3SDoug Rabson 			       fxp_intr, sc, &sc->ih);
5686182fdbdSPeter Wemm 	if (error) {
5696182fdbdSPeter Wemm 		device_printf(dev, "could not setup irq\n");
570a17c678eSDavid Greenman 		goto fail;
571a17c678eSDavid Greenman 	}
572a17c678eSDavid Greenman 
573ba8c6fd5SDavid Greenman 	/* Do generic parts of attach. */
574ba8c6fd5SDavid Greenman 	if (fxp_attach_common(sc, sc->arpcom.ac_enaddr)) {
575ba8c6fd5SDavid Greenman 		/* Failed! */
5766182fdbdSPeter Wemm 		bus_teardown_intr(dev, sc->irq, sc->ih);
5776182fdbdSPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
5786182fdbdSPeter Wemm 		bus_release_resource(dev, SYS_RES_MEMORY, FXP_PCI_MMBA, sc->mem);
5796182fdbdSPeter Wemm 		error = ENXIO;
580ba8c6fd5SDavid Greenman 		goto fail;
581a17c678eSDavid Greenman 	}
582a17c678eSDavid Greenman 
5836182fdbdSPeter Wemm 	device_printf(dev, "Ethernet address %6D%s\n",
584ba8c6fd5SDavid Greenman 	    sc->arpcom.ac_enaddr, ":", sc->phy_10Mbps_only ? ", 10Mbps" : "");
585dccee1a1SDavid Greenman 
586a17c678eSDavid Greenman 	ifp = &sc->arpcom.ac_if;
5876182fdbdSPeter Wemm 	ifp->if_unit = device_get_unit(dev);
588a17c678eSDavid Greenman 	ifp->if_name = "fxp";
589a17c678eSDavid Greenman 	ifp->if_output = ether_output;
590a330e1f1SGary Palmer 	ifp->if_baudrate = 100000000;
591fb583156SDavid Greenman 	ifp->if_init = fxp_init;
592ba8c6fd5SDavid Greenman 	ifp->if_softc = sc;
593ba8c6fd5SDavid Greenman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
594ba8c6fd5SDavid Greenman 	ifp->if_ioctl = fxp_ioctl;
595ba8c6fd5SDavid Greenman 	ifp->if_start = fxp_start;
596ba8c6fd5SDavid Greenman 	ifp->if_watchdog = fxp_watchdog;
597a17c678eSDavid Greenman 
598dfe61cf1SDavid Greenman 	/*
599dfe61cf1SDavid Greenman 	 * Attach the interface.
600dfe61cf1SDavid Greenman 	 */
601a17c678eSDavid Greenman 	if_attach(ifp);
602483b9871SDavid Greenman 	/*
6033114fdb4SDavid Greenman 	 * Let the system queue as many packets as we have available
6043114fdb4SDavid Greenman 	 * TX descriptors.
605483b9871SDavid Greenman 	 */
6063114fdb4SDavid Greenman 	ifp->if_snd.ifq_maxlen = FXP_NTXCB - 1;
6079b44ff22SGarrett Wollman 	ether_ifattach(ifp);
6089b44ff22SGarrett Wollman 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
6094a684684SDavid Greenman 
610a17c678eSDavid Greenman 	splx(s);
6116182fdbdSPeter Wemm 	return 0;
612a17c678eSDavid Greenman 
613a17c678eSDavid Greenman  fail:
614a17c678eSDavid Greenman 	splx(s);
6156182fdbdSPeter Wemm 	return error;
6166182fdbdSPeter Wemm }
6176182fdbdSPeter Wemm 
6186182fdbdSPeter Wemm /*
6196182fdbdSPeter Wemm  * Detach interface.
6206182fdbdSPeter Wemm  */
6216182fdbdSPeter Wemm static int
6226182fdbdSPeter Wemm fxp_detach(device_t dev)
6236182fdbdSPeter Wemm {
6246182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
6256182fdbdSPeter Wemm 	int s;
6266182fdbdSPeter Wemm 
6276182fdbdSPeter Wemm 	s = splimp();
6286182fdbdSPeter Wemm 
6296182fdbdSPeter Wemm 	/*
6306182fdbdSPeter Wemm 	 * Close down routes etc.
6316182fdbdSPeter Wemm 	 */
6326182fdbdSPeter Wemm 	if_detach(&sc->arpcom.ac_if);
6336182fdbdSPeter Wemm 
6346182fdbdSPeter Wemm 	/*
6356182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
6366182fdbdSPeter Wemm 	 */
6376182fdbdSPeter Wemm 	fxp_stop(sc);
6386182fdbdSPeter Wemm 
6396182fdbdSPeter Wemm 	/*
6406182fdbdSPeter Wemm 	 * Deallocate resources.
6416182fdbdSPeter Wemm 	 */
6426182fdbdSPeter Wemm 	bus_teardown_intr(dev, sc->irq, sc->ih);
6436182fdbdSPeter Wemm 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
6446182fdbdSPeter Wemm 	bus_release_resource(dev, SYS_RES_MEMORY, FXP_PCI_MMBA, sc->mem);
6456182fdbdSPeter Wemm 
6466182fdbdSPeter Wemm 	/*
6476182fdbdSPeter Wemm 	 * Free all the receive buffers.
6486182fdbdSPeter Wemm 	 */
6496182fdbdSPeter Wemm 	if (sc->rfa_headm != NULL)
6506182fdbdSPeter Wemm 		m_freem(sc->rfa_headm);
6516182fdbdSPeter Wemm 
6526182fdbdSPeter Wemm 	/*
6536182fdbdSPeter Wemm 	 * Free all media structures.
6546182fdbdSPeter Wemm 	 */
6556182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
6566182fdbdSPeter Wemm 
6576182fdbdSPeter Wemm 	/*
6586182fdbdSPeter Wemm 	 * Free anciliary structures.
6596182fdbdSPeter Wemm 	 */
6606182fdbdSPeter Wemm 	free(sc->cbl_base, M_DEVBUF);
6616182fdbdSPeter Wemm 	free(sc->fxp_stats, M_DEVBUF);
6626182fdbdSPeter Wemm 	free(sc->mcsp, M_DEVBUF);
6636182fdbdSPeter Wemm 
6646182fdbdSPeter Wemm 	splx(s);
6656182fdbdSPeter Wemm 
6666182fdbdSPeter Wemm 	return 0;
667a17c678eSDavid Greenman }
668a17c678eSDavid Greenman 
669a17c678eSDavid Greenman /*
6704a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
671a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
672a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
673a17c678eSDavid Greenman  */
6746182fdbdSPeter Wemm static int
6756182fdbdSPeter Wemm fxp_shutdown(device_t dev)
676a17c678eSDavid Greenman {
6776182fdbdSPeter Wemm 	/*
6786182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
6796182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
6806182fdbdSPeter Wemm 	 * reboot before the driver initializes.
6816182fdbdSPeter Wemm 	 */
6826182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
6836182fdbdSPeter Wemm 	return 0;
684a17c678eSDavid Greenman }
685a17c678eSDavid Greenman 
6866182fdbdSPeter Wemm static device_method_t fxp_methods[] = {
6876182fdbdSPeter Wemm 	/* Device interface */
6886182fdbdSPeter Wemm 	DEVMETHOD(device_probe,		fxp_probe),
6896182fdbdSPeter Wemm 	DEVMETHOD(device_attach,	fxp_attach),
6906182fdbdSPeter Wemm 	DEVMETHOD(device_detach,	fxp_detach),
6916182fdbdSPeter Wemm 	DEVMETHOD(device_shutdown,	fxp_shutdown),
6926182fdbdSPeter Wemm 
6936182fdbdSPeter Wemm 	{ 0, 0 }
6946182fdbdSPeter Wemm };
6956182fdbdSPeter Wemm 
6966182fdbdSPeter Wemm static driver_t fxp_driver = {
6976182fdbdSPeter Wemm 	"fxp",
6986182fdbdSPeter Wemm 	fxp_methods,
6996182fdbdSPeter Wemm 	sizeof(struct fxp_softc),
7006182fdbdSPeter Wemm };
7016182fdbdSPeter Wemm 
7026182fdbdSPeter Wemm static devclass_t fxp_devclass;
7036182fdbdSPeter Wemm 
7049e4c647cSBill Paul DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
7056182fdbdSPeter Wemm 
706ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
707ba8c6fd5SDavid Greenman 
708ba8c6fd5SDavid Greenman /*************************************************************
709ba8c6fd5SDavid Greenman  * End of operating system-specific autoconfiguration glue
710ba8c6fd5SDavid Greenman  *************************************************************/
711ba8c6fd5SDavid Greenman 
712ba8c6fd5SDavid Greenman /*
713ba8c6fd5SDavid Greenman  * Do generic parts of attach.
714ba8c6fd5SDavid Greenman  */
715ba8c6fd5SDavid Greenman static int
716ba8c6fd5SDavid Greenman fxp_attach_common(sc, enaddr)
717ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
718ba8c6fd5SDavid Greenman 	u_int8_t *enaddr;
719ba8c6fd5SDavid Greenman {
720ba8c6fd5SDavid Greenman 	u_int16_t data;
721ba8c6fd5SDavid Greenman 	int i, nmedia, defmedia;
722ba8c6fd5SDavid Greenman 	const int *media;
723ba8c6fd5SDavid Greenman 
724ba8c6fd5SDavid Greenman 	/*
725ba8c6fd5SDavid Greenman 	 * Reset to a stable state.
726ba8c6fd5SDavid Greenman 	 */
727ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
728ba8c6fd5SDavid Greenman 	DELAY(10);
729ba8c6fd5SDavid Greenman 
730ba8c6fd5SDavid Greenman 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
731ba8c6fd5SDavid Greenman 	    M_DEVBUF, M_NOWAIT);
732ba8c6fd5SDavid Greenman 	if (sc->cbl_base == NULL)
733ba8c6fd5SDavid Greenman 		goto fail;
73491aa9f90SDavid Greenman 	bzero(sc->cbl_base, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
735ba8c6fd5SDavid Greenman 
736ba8c6fd5SDavid Greenman 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT);
737ba8c6fd5SDavid Greenman 	if (sc->fxp_stats == NULL)
738ba8c6fd5SDavid Greenman 		goto fail;
739ba8c6fd5SDavid Greenman 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
740ba8c6fd5SDavid Greenman 
741397f9dfeSDavid Greenman 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
742397f9dfeSDavid Greenman 	if (sc->mcsp == NULL)
743397f9dfeSDavid Greenman 		goto fail;
744397f9dfeSDavid Greenman 
745ba8c6fd5SDavid Greenman 	/*
746ba8c6fd5SDavid Greenman 	 * Pre-allocate our receive buffers.
747ba8c6fd5SDavid Greenman 	 */
748ba8c6fd5SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
749ba8c6fd5SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
750ba8c6fd5SDavid Greenman 			goto fail;
751ba8c6fd5SDavid Greenman 		}
752ba8c6fd5SDavid Greenman 	}
753ba8c6fd5SDavid Greenman 
754ba8c6fd5SDavid Greenman 	/*
755e9bf2fa7SDavid Greenman 	 * Find out how large of an SEEPROM we have.
756e9bf2fa7SDavid Greenman 	 */
757e9bf2fa7SDavid Greenman 	fxp_autosize_eeprom(sc);
758e9bf2fa7SDavid Greenman 
759e9bf2fa7SDavid Greenman 	/*
760ba8c6fd5SDavid Greenman 	 * Get info about the primary PHY
761ba8c6fd5SDavid Greenman 	 */
762ba8c6fd5SDavid Greenman 	fxp_read_eeprom(sc, (u_int16_t *)&data, 6, 1);
763ba8c6fd5SDavid Greenman 	sc->phy_primary_addr = data & 0xff;
764ba8c6fd5SDavid Greenman 	sc->phy_primary_device = (data >> 8) & 0x3f;
765ba8c6fd5SDavid Greenman 	sc->phy_10Mbps_only = data >> 15;
766ba8c6fd5SDavid Greenman 
767ba8c6fd5SDavid Greenman 	/*
768ba8c6fd5SDavid Greenman 	 * Read MAC address.
769ba8c6fd5SDavid Greenman 	 */
770ba8c6fd5SDavid Greenman 	fxp_read_eeprom(sc, (u_int16_t *)enaddr, 0, 3);
771ba8c6fd5SDavid Greenman 
772ba8c6fd5SDavid Greenman 	/*
773ba8c6fd5SDavid Greenman 	 * Initialize the media structures.
774ba8c6fd5SDavid Greenman 	 */
775ba8c6fd5SDavid Greenman 
776ba8c6fd5SDavid Greenman 	media = fxp_media_default;
777ba8c6fd5SDavid Greenman 	nmedia = sizeof(fxp_media_default) / sizeof(fxp_media_default[0]);
778ba8c6fd5SDavid Greenman 	defmedia = FXP_MEDIA_DEFAULT_DEFMEDIA;
779ba8c6fd5SDavid Greenman 
780ba8c6fd5SDavid Greenman 	for (i = 0; i < NFXPMEDIA; i++) {
781ba8c6fd5SDavid Greenman 		if (sc->phy_primary_device == fxp_media[i].fsm_phy) {
782ba8c6fd5SDavid Greenman 			media = fxp_media[i].fsm_media;
783ba8c6fd5SDavid Greenman 			nmedia = fxp_media[i].fsm_nmedia;
784ba8c6fd5SDavid Greenman 			defmedia = fxp_media[i].fsm_defmedia;
785ba8c6fd5SDavid Greenman 		}
786ba8c6fd5SDavid Greenman 	}
787ba8c6fd5SDavid Greenman 
788ba8c6fd5SDavid Greenman 	ifmedia_init(&sc->sc_media, 0, fxp_mediachange, fxp_mediastatus);
789ba8c6fd5SDavid Greenman 	for (i = 0; i < nmedia; i++) {
790ba8c6fd5SDavid Greenman 		if (IFM_SUBTYPE(media[i]) == IFM_100_TX && sc->phy_10Mbps_only)
791ba8c6fd5SDavid Greenman 			continue;
792ba8c6fd5SDavid Greenman 		ifmedia_add(&sc->sc_media, media[i], 0, NULL);
793ba8c6fd5SDavid Greenman 	}
794ba8c6fd5SDavid Greenman 	ifmedia_set(&sc->sc_media, defmedia);
795ba8c6fd5SDavid Greenman 
796ba8c6fd5SDavid Greenman 	return (0);
797ba8c6fd5SDavid Greenman 
798ba8c6fd5SDavid Greenman  fail:
799ba8c6fd5SDavid Greenman 	printf(FXP_FORMAT ": Failed to malloc memory\n", FXP_ARGS(sc));
800ba8c6fd5SDavid Greenman 	if (sc->cbl_base)
801ba8c6fd5SDavid Greenman 		free(sc->cbl_base, M_DEVBUF);
802ba8c6fd5SDavid Greenman 	if (sc->fxp_stats)
803ba8c6fd5SDavid Greenman 		free(sc->fxp_stats, M_DEVBUF);
804397f9dfeSDavid Greenman 	if (sc->mcsp)
805397f9dfeSDavid Greenman 		free(sc->mcsp, M_DEVBUF);
806ba8c6fd5SDavid Greenman 	/* frees entire chain */
807ba8c6fd5SDavid Greenman 	if (sc->rfa_headm)
808ba8c6fd5SDavid Greenman 		m_freem(sc->rfa_headm);
809ba8c6fd5SDavid Greenman 
810ba8c6fd5SDavid Greenman 	return (ENOMEM);
811ba8c6fd5SDavid Greenman }
812ba8c6fd5SDavid Greenman 
813ba8c6fd5SDavid Greenman /*
814e9bf2fa7SDavid Greenman  * From NetBSD:
815e9bf2fa7SDavid Greenman  *
816e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
817e9bf2fa7SDavid Greenman  *
818e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
819e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
820e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
821e9bf2fa7SDavid Greenman  *
822e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
823e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
824e9bf2fa7SDavid Greenman  *
825e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
826e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
827e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
828e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
829e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
830e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
831e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
832e9bf2fa7SDavid Greenman  *
833e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
834e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
835e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
836e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
837e9bf2fa7SDavid Greenman  *
838e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
839e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
840e9bf2fa7SDavid Greenman  */
841e9bf2fa7SDavid Greenman static void
842e9bf2fa7SDavid Greenman fxp_autosize_eeprom(sc)
843e9bf2fa7SDavid Greenman 	struct fxp_softc *sc;
844e9bf2fa7SDavid Greenman {
845e9bf2fa7SDavid Greenman 	u_int16_t reg;
846e9bf2fa7SDavid Greenman 	int x;
847e9bf2fa7SDavid Greenman 
848e9bf2fa7SDavid Greenman 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
849e9bf2fa7SDavid Greenman 	/*
850e9bf2fa7SDavid Greenman 	 * Shift in read opcode.
851e9bf2fa7SDavid Greenman 	 */
852e9bf2fa7SDavid Greenman 	for (x = 3; x > 0; x--) {
853e9bf2fa7SDavid Greenman 		if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
854e9bf2fa7SDavid Greenman 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
855e9bf2fa7SDavid Greenman 		} else {
856e9bf2fa7SDavid Greenman 			reg = FXP_EEPROM_EECS;
857e9bf2fa7SDavid Greenman 		}
858e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
859e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
860e9bf2fa7SDavid Greenman 		    reg | FXP_EEPROM_EESK);
861e9bf2fa7SDavid Greenman 		DELAY(1);
862e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
863e9bf2fa7SDavid Greenman 		DELAY(1);
864e9bf2fa7SDavid Greenman 	}
865e9bf2fa7SDavid Greenman 	/*
866e9bf2fa7SDavid Greenman 	 * Shift in address.
867e9bf2fa7SDavid Greenman 	 * Wait for the dummy zero following a correct address shift.
868e9bf2fa7SDavid Greenman 	 */
869e9bf2fa7SDavid Greenman 	for (x = 1; x <= 8; x++) {
870e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
871e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
872e9bf2fa7SDavid Greenman 			FXP_EEPROM_EECS | FXP_EEPROM_EESK);
873e9bf2fa7SDavid Greenman 		DELAY(1);
874e9bf2fa7SDavid Greenman 		if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) == 0)
875e9bf2fa7SDavid Greenman 			break;
876e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
877e9bf2fa7SDavid Greenman 		DELAY(1);
878e9bf2fa7SDavid Greenman 	}
879e9bf2fa7SDavid Greenman 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
880e9bf2fa7SDavid Greenman 	DELAY(1);
881e9bf2fa7SDavid Greenman 	sc->eeprom_size = x;
882e9bf2fa7SDavid Greenman }
883e9bf2fa7SDavid Greenman /*
884ba8c6fd5SDavid Greenman  * Read from the serial EEPROM. Basically, you manually shift in
885ba8c6fd5SDavid Greenman  * the read opcode (one bit at a time) and then shift in the address,
886ba8c6fd5SDavid Greenman  * and then you shift out the data (all of this one bit at a time).
887ba8c6fd5SDavid Greenman  * The word size is 16 bits, so you have to provide the address for
888ba8c6fd5SDavid Greenman  * every 16 bits of data.
889ba8c6fd5SDavid Greenman  */
890ba8c6fd5SDavid Greenman static void
891ba8c6fd5SDavid Greenman fxp_read_eeprom(sc, data, offset, words)
892ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
893ba8c6fd5SDavid Greenman 	u_short *data;
894ba8c6fd5SDavid Greenman 	int offset;
895ba8c6fd5SDavid Greenman 	int words;
896ba8c6fd5SDavid Greenman {
897ba8c6fd5SDavid Greenman 	u_int16_t reg;
898ba8c6fd5SDavid Greenman 	int i, x;
899ba8c6fd5SDavid Greenman 
900ba8c6fd5SDavid Greenman 	for (i = 0; i < words; i++) {
901ba8c6fd5SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
902ba8c6fd5SDavid Greenman 		/*
903ba8c6fd5SDavid Greenman 		 * Shift in read opcode.
904ba8c6fd5SDavid Greenman 		 */
905ba8c6fd5SDavid Greenman 		for (x = 3; x > 0; x--) {
906ba8c6fd5SDavid Greenman 			if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
907ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
908ba8c6fd5SDavid Greenman 			} else {
909ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS;
910ba8c6fd5SDavid Greenman 			}
911ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
912ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
913ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
914ba8c6fd5SDavid Greenman 			DELAY(1);
915ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
916ba8c6fd5SDavid Greenman 			DELAY(1);
917ba8c6fd5SDavid Greenman 		}
918ba8c6fd5SDavid Greenman 		/*
919ba8c6fd5SDavid Greenman 		 * Shift in address.
920ba8c6fd5SDavid Greenman 		 */
921e9bf2fa7SDavid Greenman 		for (x = sc->eeprom_size; x > 0; x--) {
922ba8c6fd5SDavid Greenman 			if ((i + offset) & (1 << (x - 1))) {
923ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
924ba8c6fd5SDavid Greenman 			} else {
925ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS;
926ba8c6fd5SDavid Greenman 			}
927ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
928ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
929ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
930ba8c6fd5SDavid Greenman 			DELAY(1);
931ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
932ba8c6fd5SDavid Greenman 			DELAY(1);
933ba8c6fd5SDavid Greenman 		}
934ba8c6fd5SDavid Greenman 		reg = FXP_EEPROM_EECS;
935ba8c6fd5SDavid Greenman 		data[i] = 0;
936ba8c6fd5SDavid Greenman 		/*
937ba8c6fd5SDavid Greenman 		 * Shift out data.
938ba8c6fd5SDavid Greenman 		 */
939ba8c6fd5SDavid Greenman 		for (x = 16; x > 0; x--) {
940ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
941ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
942ba8c6fd5SDavid Greenman 			DELAY(1);
943ba8c6fd5SDavid Greenman 			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
944ba8c6fd5SDavid Greenman 			    FXP_EEPROM_EEDO)
945ba8c6fd5SDavid Greenman 				data[i] |= (1 << (x - 1));
946ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
947ba8c6fd5SDavid Greenman 			DELAY(1);
948ba8c6fd5SDavid Greenman 		}
949ba8c6fd5SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
950ba8c6fd5SDavid Greenman 		DELAY(1);
951ba8c6fd5SDavid Greenman 	}
952ba8c6fd5SDavid Greenman }
953ba8c6fd5SDavid Greenman 
954a17c678eSDavid Greenman /*
955a17c678eSDavid Greenman  * Start packet transmission on the interface.
956a17c678eSDavid Greenman  */
957a17c678eSDavid Greenman static void
958a17c678eSDavid Greenman fxp_start(ifp)
959a17c678eSDavid Greenman 	struct ifnet *ifp;
960a17c678eSDavid Greenman {
9619b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
962a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
963a17c678eSDavid Greenman 
964a17c678eSDavid Greenman 	/*
965483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
966483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
967483b9871SDavid Greenman 	 * of the command chain).
968a17c678eSDavid Greenman 	 */
969483b9871SDavid Greenman 	if (sc->need_mcsetup)
970a17c678eSDavid Greenman 		return;
9711cd443acSDavid Greenman 
972483b9871SDavid Greenman 	txp = NULL;
973483b9871SDavid Greenman 
974483b9871SDavid Greenman 	/*
975483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
976483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
9773114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
9783114fdb4SDavid Greenman 	 *       a NOP command when needed.
979483b9871SDavid Greenman 	 */
9803114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
981483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
982483b9871SDavid Greenman 		int segment;
983483b9871SDavid Greenman 
984dfe61cf1SDavid Greenman 		/*
985dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
986dfe61cf1SDavid Greenman 		 */
9876318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
988a17c678eSDavid Greenman 
989dfe61cf1SDavid Greenman 		/*
990483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
991dfe61cf1SDavid Greenman 		 */
992a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
993a17c678eSDavid Greenman 
994a17c678eSDavid Greenman 		/*
995a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
996483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
997a17c678eSDavid Greenman 		 * and size of the mbuf.
998a17c678eSDavid Greenman 		 */
99923a0ed7cSDavid Greenman tbdinit:
1000a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
1001a17c678eSDavid Greenman 			if (m->m_len != 0) {
1002a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1003a17c678eSDavid Greenman 					break;
1004a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1005a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1006a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1007a17c678eSDavid Greenman 				segment++;
1008a17c678eSDavid Greenman 			}
1009a17c678eSDavid Greenman 		}
1010fb583156SDavid Greenman 		if (m != NULL) {
101123a0ed7cSDavid Greenman 			struct mbuf *mn;
101223a0ed7cSDavid Greenman 
1013a17c678eSDavid Greenman 			/*
1014a17c678eSDavid Greenman 			 * We ran out of segments. We have to recopy this mbuf
1015483b9871SDavid Greenman 			 * chain first. Bail out if we can't get the new buffers.
1016a17c678eSDavid Greenman 			 */
101723a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
101823a0ed7cSDavid Greenman 			if (mn == NULL) {
101923a0ed7cSDavid Greenman 				m_freem(mb_head);
1020483b9871SDavid Greenman 				break;
1021a17c678eSDavid Greenman 			}
102223a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
102323a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
102423a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
102523a0ed7cSDavid Greenman 					m_freem(mn);
102623a0ed7cSDavid Greenman 					m_freem(mb_head);
1027483b9871SDavid Greenman 					break;
102823a0ed7cSDavid Greenman 				}
102923a0ed7cSDavid Greenman 			}
1030ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1031ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
103223a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
103323a0ed7cSDavid Greenman 			m_freem(mb_head);
103423a0ed7cSDavid Greenman 			mb_head = mn;
103523a0ed7cSDavid Greenman 			goto tbdinit;
103623a0ed7cSDavid Greenman 		}
103723a0ed7cSDavid Greenman 
103823a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10391cd443acSDavid Greenman 		txp->mb_head = mb_head;
1040a17c678eSDavid Greenman 		txp->cb_status = 0;
10413114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1042a17c678eSDavid Greenman 			txp->cb_command =
1043a17c678eSDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S;
10443114fdb4SDavid Greenman 		} else {
10453114fdb4SDavid Greenman 			txp->cb_command =
10463114fdb4SDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
10473114fdb4SDavid Greenman 			/*
10483114fdb4SDavid Greenman 			 * Set a 5 second timer just in case we don't hear from the
10493114fdb4SDavid Greenman 			 * card again.
10503114fdb4SDavid Greenman 			 */
10513114fdb4SDavid Greenman 			ifp->if_timer = 5;
10523114fdb4SDavid Greenman 		}
1053f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1054a17c678eSDavid Greenman 
1055a17c678eSDavid Greenman 		/*
1056483b9871SDavid Greenman 		 * Advance the end of list forward.
1057a17c678eSDavid Greenman 		 */
1058a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
1059a17c678eSDavid Greenman 		sc->cbl_last = txp;
1060a17c678eSDavid Greenman 
1061a17c678eSDavid Greenman 		/*
10621cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
10631cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1064483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1065a17c678eSDavid Greenman 		 */
10661cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1067a17c678eSDavid Greenman 			sc->cbl_first = txp;
1068a17c678eSDavid Greenman 
10691cd443acSDavid Greenman 		sc->tx_queued++;
10701cd443acSDavid Greenman 
1071a17c678eSDavid Greenman 		/*
1072a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1073a17c678eSDavid Greenman 		 */
1074fb583156SDavid Greenman 		if (ifp->if_bpf)
1075ba8c6fd5SDavid Greenman 			bpf_mtap(FXP_BPFTAP_ARG(ifp), mb_head);
1076483b9871SDavid Greenman 	}
1077483b9871SDavid Greenman 
1078483b9871SDavid Greenman 	/*
1079483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1080483b9871SDavid Greenman 	 * going again if suspended.
1081483b9871SDavid Greenman 	 */
1082483b9871SDavid Greenman 	if (txp != NULL) {
1083483b9871SDavid Greenman 		fxp_scb_wait(sc);
1084483b9871SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
1085483b9871SDavid Greenman 	}
1086a17c678eSDavid Greenman }
1087a17c678eSDavid Greenman 
1088a17c678eSDavid Greenman /*
10899c7d2607SDavid Greenman  * Process interface interrupts.
1090a17c678eSDavid Greenman  */
1091ba8c6fd5SDavid Greenman static FXP_INTR_TYPE
1092a17c678eSDavid Greenman fxp_intr(arg)
1093a17c678eSDavid Greenman 	void *arg;
1094a17c678eSDavid Greenman {
1095a17c678eSDavid Greenman 	struct fxp_softc *sc = arg;
1096ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
10971cd443acSDavid Greenman 	u_int8_t statack;
1098ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1099ba8c6fd5SDavid Greenman 	int claimed = 0;
1100ba8c6fd5SDavid Greenman #endif
1101a17c678eSDavid Greenman 
1102ba8c6fd5SDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1103ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1104ba8c6fd5SDavid Greenman 		claimed = 1;
1105ba8c6fd5SDavid Greenman #endif
1106a17c678eSDavid Greenman 		/*
1107a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1108a17c678eSDavid Greenman 		 */
1109ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1110a17c678eSDavid Greenman 
1111a17c678eSDavid Greenman 		/*
11123114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
11133114fdb4SDavid Greenman 		 */
11143114fdb4SDavid Greenman 		if (statack & FXP_SCB_STATACK_CXTNO) {
11153114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
11163114fdb4SDavid Greenman 
11173114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
11183114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
11193114fdb4SDavid Greenman 			    txp = txp->next) {
11203114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
11213114fdb4SDavid Greenman 					m_freem(txp->mb_head);
11223114fdb4SDavid Greenman 					txp->mb_head = NULL;
11233114fdb4SDavid Greenman 				}
11243114fdb4SDavid Greenman 				sc->tx_queued--;
11253114fdb4SDavid Greenman 			}
11263114fdb4SDavid Greenman 			sc->cbl_first = txp;
11273114fdb4SDavid Greenman 			ifp->if_timer = 0;
11283114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
11293114fdb4SDavid Greenman 				if (sc->need_mcsetup)
11303114fdb4SDavid Greenman 					fxp_mc_setup(sc);
11313114fdb4SDavid Greenman 			}
11323114fdb4SDavid Greenman 			/*
11333114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
11343114fdb4SDavid Greenman 			 */
11353114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
11363114fdb4SDavid Greenman 				fxp_start(ifp);
11373114fdb4SDavid Greenman 		}
11383114fdb4SDavid Greenman 		/*
1139a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1140a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1141a17c678eSDavid Greenman 		 * re-start the receiver.
1142a17c678eSDavid Greenman 		 */
1143a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1144a17c678eSDavid Greenman 			struct mbuf *m;
1145a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1146a17c678eSDavid Greenman rcvloop:
1147a17c678eSDavid Greenman 			m = sc->rfa_headm;
1148ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1149ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1150a17c678eSDavid Greenman 
1151a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1152dfe61cf1SDavid Greenman 				/*
1153dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1154dfe61cf1SDavid Greenman 				 */
1155a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1156a17c678eSDavid Greenman 				m->m_next = NULL;
1157a17c678eSDavid Greenman 
1158dfe61cf1SDavid Greenman 				/*
1159ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1160ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1161ba8c6fd5SDavid Greenman 				 * instead.
1162dfe61cf1SDavid Greenman 				 */
1163a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1164a17c678eSDavid Greenman 					struct ether_header *eh;
1165aed53495SDavid Greenman 					int total_len;
1166a17c678eSDavid Greenman 
1167ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1168ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1169ba8c6fd5SDavid Greenman 					if (total_len <
1170ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
117106339180SDavid Greenman 						m_freem(m);
117206339180SDavid Greenman 						goto rcvloop;
117306339180SDavid Greenman 					}
1174a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
11752e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1176a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1177ba8c6fd5SDavid Greenman 					m->m_data +=
1178ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1179ab090e5bSLuigi Rizzo 					m->m_len -=
1180ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1181ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1182a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1183a17c678eSDavid Greenman 				}
1184a17c678eSDavid Greenman 				goto rcvloop;
1185a17c678eSDavid Greenman 			}
1186a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1187ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1188ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1189ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1190ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
1191ba8c6fd5SDavid Greenman 				CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1192ba8c6fd5SDavid Greenman 				    FXP_SCB_COMMAND_RU_START);
1193a17c678eSDavid Greenman 			}
1194a17c678eSDavid Greenman 		}
1195a17c678eSDavid Greenman 	}
1196ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1197ba8c6fd5SDavid Greenman 	return (claimed);
1198ba8c6fd5SDavid Greenman #endif
1199a17c678eSDavid Greenman }
1200a17c678eSDavid Greenman 
1201dfe61cf1SDavid Greenman /*
1202dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1203dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1204dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1205dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1206dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1207dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1208dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1209dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1210dfe61cf1SDavid Greenman  * them again next time.
1211dfe61cf1SDavid Greenman  */
1212303b270bSEivind Eklund static void
1213a17c678eSDavid Greenman fxp_stats_update(arg)
1214a17c678eSDavid Greenman 	void *arg;
1215a17c678eSDavid Greenman {
1216a17c678eSDavid Greenman 	struct fxp_softc *sc = arg;
1217ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1218a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1219c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1220397f9dfeSDavid Greenman 	int s;
1221a17c678eSDavid Greenman 
1222a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1223a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1224397f9dfeSDavid Greenman 	if (sp->rx_good) {
1225397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1226397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1227397f9dfeSDavid Greenman 	} else {
1228c8cc6fcaSDavid Greenman 		/*
1229c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1230c8cc6fcaSDavid Greenman 		 */
1231397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1232397f9dfeSDavid Greenman 	}
12333ba65732SDavid Greenman 	ifp->if_ierrors +=
12343ba65732SDavid Greenman 	    sp->rx_crc_errors +
12353ba65732SDavid Greenman 	    sp->rx_alignment_errors +
12363ba65732SDavid Greenman 	    sp->rx_rnr_errors +
12376e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1238a17c678eSDavid Greenman 	/*
1239f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1240f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1241f9be9005SDavid Greenman 	 */
1242f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1243f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1244f9be9005SDavid Greenman 		if (tx_threshold < 192)
1245f9be9005SDavid Greenman 			tx_threshold += 64;
1246f9be9005SDavid Greenman 	}
1247397f9dfeSDavid Greenman 	s = splimp();
1248397f9dfeSDavid Greenman 	/*
1249c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1250c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1251c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1252c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1253c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1254c8cc6fcaSDavid Greenman 	 */
1255c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1256c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1257c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1258c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1259c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1260c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1261c8cc6fcaSDavid Greenman 		}
1262c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1263c8cc6fcaSDavid Greenman 	}
1264c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1265c8cc6fcaSDavid Greenman 	/*
1266397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1267397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1268397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1269397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1270397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1271397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1272397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1273397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1274397f9dfeSDavid Greenman 	 */
1275397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1276397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1277397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1278397f9dfeSDavid Greenman 	}
1279f9be9005SDavid Greenman 	/*
12803ba65732SDavid Greenman 	 * If there is no pending command, start another stats
12813ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1282a17c678eSDavid Greenman 	 */
1283397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1284a17c678eSDavid Greenman 		/*
1285397f9dfeSDavid Greenman 		 * Start another stats dump.
1286a17c678eSDavid Greenman 		 */
1287ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1288ba8c6fd5SDavid Greenman 		    FXP_SCB_COMMAND_CU_DUMPRESET);
1289dfe61cf1SDavid Greenman 	} else {
1290dfe61cf1SDavid Greenman 		/*
1291dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1292dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
12933ba65732SDavid Greenman 		 * next timer event to update them.
1294dfe61cf1SDavid Greenman 		 */
1295dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1296f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1297dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
12983ba65732SDavid Greenman 
1299dfe61cf1SDavid Greenman 		sp->rx_good = 0;
13003ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
13013ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
13023ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
13033ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1304dfe61cf1SDavid Greenman 	}
1305397f9dfeSDavid Greenman 	splx(s);
1306a17c678eSDavid Greenman 	/*
1307a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1308a17c678eSDavid Greenman 	 */
1309397f9dfeSDavid Greenman 	sc->stat_ch = timeout(fxp_stats_update, sc, hz);
1310a17c678eSDavid Greenman }
1311a17c678eSDavid Greenman 
1312a17c678eSDavid Greenman /*
1313a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1314a17c678eSDavid Greenman  * the interface.
1315a17c678eSDavid Greenman  */
1316a17c678eSDavid Greenman static void
13174a5f1499SDavid Greenman fxp_stop(sc)
13184a5f1499SDavid Greenman 	struct fxp_softc *sc;
1319a17c678eSDavid Greenman {
1320ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
13213ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
13223ba65732SDavid Greenman 	int i;
1323a17c678eSDavid Greenman 
1324a17c678eSDavid Greenman 	/*
1325a17c678eSDavid Greenman 	 * Cancel stats updater.
1326a17c678eSDavid Greenman 	 */
13276c951b44SJustin T. Gibbs 	untimeout(fxp_stats_update, sc, sc->stat_ch);
13283ba65732SDavid Greenman 
13293ba65732SDavid Greenman 	/*
13303ba65732SDavid Greenman 	 * Issue software reset
13313ba65732SDavid Greenman 	 */
1332ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1333a17c678eSDavid Greenman 	DELAY(10);
1334a17c678eSDavid Greenman 
13353ba65732SDavid Greenman 	/*
13363ba65732SDavid Greenman 	 * Release any xmit buffers.
13373ba65732SDavid Greenman 	 */
1338da91462dSDavid Greenman 	txp = sc->cbl_base;
1339da91462dSDavid Greenman 	if (txp != NULL) {
1340da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1341da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1342da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1343da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1344da91462dSDavid Greenman 			}
1345da91462dSDavid Greenman 		}
13463ba65732SDavid Greenman 	}
13473ba65732SDavid Greenman 	sc->tx_queued = 0;
13483ba65732SDavid Greenman 
13493ba65732SDavid Greenman 	/*
13503ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
13513ba65732SDavid Greenman 	 */
13523ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
13533ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
13543ba65732SDavid Greenman 	sc->rfa_headm = NULL;
13553ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
13563ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
13573ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
13583ba65732SDavid Greenman 			/*
13593ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
13603ba65732SDavid Greenman 			 * and we just freed all the buffers we need
13613ba65732SDavid Greenman 			 * above.
13623ba65732SDavid Greenman 			 */
13633ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
13643ba65732SDavid Greenman 		}
13653ba65732SDavid Greenman 	}
13663ba65732SDavid Greenman 
13673ba65732SDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
13683ba65732SDavid Greenman 	ifp->if_timer = 0;
1369a17c678eSDavid Greenman }
1370a17c678eSDavid Greenman 
1371a17c678eSDavid Greenman /*
1372a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1373a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1374a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1375a17c678eSDavid Greenman  * card has wedged for some reason.
1376a17c678eSDavid Greenman  */
1377a17c678eSDavid Greenman static void
13784a5f1499SDavid Greenman fxp_watchdog(ifp)
13794a5f1499SDavid Greenman 	struct ifnet *ifp;
1380a17c678eSDavid Greenman {
1381ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1382ba8c6fd5SDavid Greenman 
1383397f9dfeSDavid Greenman 	printf(FXP_FORMAT ": device timeout\n", FXP_ARGS(sc));
13844a5f1499SDavid Greenman 	ifp->if_oerrors++;
1385a17c678eSDavid Greenman 
1386ba8c6fd5SDavid Greenman 	fxp_init(sc);
1387a17c678eSDavid Greenman }
1388a17c678eSDavid Greenman 
1389a17c678eSDavid Greenman static void
1390fb583156SDavid Greenman fxp_init(xsc)
1391fb583156SDavid Greenman 	void *xsc;
1392a17c678eSDavid Greenman {
1393fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1394ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1395a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1396a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1397a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1398397f9dfeSDavid Greenman 	int i, s, prm;
1399a17c678eSDavid Greenman 
1400a17c678eSDavid Greenman 	s = splimp();
1401a17c678eSDavid Greenman 	/*
14023ba65732SDavid Greenman 	 * Cancel any pending I/O
1403a17c678eSDavid Greenman 	 */
14043ba65732SDavid Greenman 	fxp_stop(sc);
1405a17c678eSDavid Greenman 
1406a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1407a17c678eSDavid Greenman 
1408a17c678eSDavid Greenman 	/*
1409a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1410a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1411a17c678eSDavid Greenman 	 */
1412ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1413ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1414a17c678eSDavid Greenman 
1415ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1416ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1417a17c678eSDavid Greenman 
1418a17c678eSDavid Greenman 	/*
1419a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1420a17c678eSDavid Greenman 	 */
1421ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1422ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
1423ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1424a17c678eSDavid Greenman 
1425a17c678eSDavid Greenman 	/*
1426a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1427a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1428a17c678eSDavid Greenman 	 * later.
1429a17c678eSDavid Greenman 	 */
1430a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1431a17c678eSDavid Greenman 
1432a17c678eSDavid Greenman 	/*
1433a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1434a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1435a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1436a17c678eSDavid Greenman 	 */
1437697457a1SMatthew Dillon 	bcopy(fxp_cb_config_template, (volatile void *)&cbp->cb_status,
1438397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1439a17c678eSDavid Greenman 
1440a17c678eSDavid Greenman 	cbp->cb_status =	0;
1441a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1442a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1443a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1444001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1445001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1446a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1447001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1448001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1449001696daSDavid Greenman 	cbp->dma_bce =		0;	/* (disable) dma max counters */
1450a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1451a17c678eSDavid Greenman 	cbp->tno_int =		0;	/* (disable) tx not okay interrupt */
14523114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1453a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1454a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1455a17c678eSDavid Greenman 	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
1456dccee1a1SDavid Greenman 	cbp->mediatype =	!sc->phy_10Mbps_only; /* interface mode */
1457a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1458a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1459a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1460a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1461a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1462a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1463a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1464a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1465001696daSDavid Greenman 	cbp->crscdt =		0;	/* (CRS only) */
1466a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1467a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1468a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1469a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
14703ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1471a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1472397f9dfeSDavid Greenman 	cbp->mc_all =		sc->all_mcasts;/* accept all multicasts */
1473a17c678eSDavid Greenman 
1474a17c678eSDavid Greenman 	/*
1475a17c678eSDavid Greenman 	 * Start the config command/DMA.
1476a17c678eSDavid Greenman 	 */
1477ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1478397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
1479ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1480a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
1481a17c678eSDavid Greenman 	while (!(cbp->cb_status & FXP_CB_STATUS_C));
1482a17c678eSDavid Greenman 
1483a17c678eSDavid Greenman 	/*
1484a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1485a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1486a17c678eSDavid Greenman 	 */
1487a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1488a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1489a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1490a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1491ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1492ba8c6fd5SDavid Greenman 	bcopy(LLADDR(ifp->if_sadl), (void *)cb_ias->macaddr, 6);
1493ba8c6fd5SDavid Greenman #else
14948aef1712SMatthew Dillon 	bcopy(sc->arpcom.ac_enaddr, (volatile void *)cb_ias->macaddr,
1495a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1496ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
1497a17c678eSDavid Greenman 
1498a17c678eSDavid Greenman 	/*
1499a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1500a17c678eSDavid Greenman 	 */
1501ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1502ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1503a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
1504a17c678eSDavid Greenman 	while (!(cb_ias->cb_status & FXP_CB_STATUS_C));
1505a17c678eSDavid Greenman 
1506a17c678eSDavid Greenman 	/*
1507a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1508a17c678eSDavid Greenman 	 */
1509a17c678eSDavid Greenman 
1510a17c678eSDavid Greenman 	txp = sc->cbl_base;
1511a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1512a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1513a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1514a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
1515397f9dfeSDavid Greenman 		txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
1516a17c678eSDavid Greenman 		txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1517a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1518a17c678eSDavid Greenman 	}
1519a17c678eSDavid Greenman 	/*
1520397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1521a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1522a17c678eSDavid Greenman 	 */
1523a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1524a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1525397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1526a17c678eSDavid Greenman 
1527ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1528ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1529a17c678eSDavid Greenman 
1530a17c678eSDavid Greenman 	/*
1531a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1532a17c678eSDavid Greenman 	 */
1533ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1534ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1535ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
1536ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1537a17c678eSDavid Greenman 
1538dccee1a1SDavid Greenman 	/*
1539ba8c6fd5SDavid Greenman 	 * Set current media.
1540dccee1a1SDavid Greenman 	 */
1541ba8c6fd5SDavid Greenman 	fxp_set_media(sc, sc->sc_media.ifm_cur->ifm_media);
1542dccee1a1SDavid Greenman 
1543a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1544a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1545a17c678eSDavid Greenman 	splx(s);
1546a17c678eSDavid Greenman 
1547a17c678eSDavid Greenman 	/*
1548a17c678eSDavid Greenman 	 * Start stats updater.
1549a17c678eSDavid Greenman 	 */
15506c951b44SJustin T. Gibbs 	sc->stat_ch = timeout(fxp_stats_update, sc, hz);
1551a17c678eSDavid Greenman }
1552a17c678eSDavid Greenman 
1553303b270bSEivind Eklund static void
1554ba8c6fd5SDavid Greenman fxp_set_media(sc, media)
1555ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1556ba8c6fd5SDavid Greenman 	int media;
1557ba8c6fd5SDavid Greenman {
1558ba8c6fd5SDavid Greenman 
1559ba8c6fd5SDavid Greenman 	switch (sc->phy_primary_device) {
1560ba8c6fd5SDavid Greenman 	case FXP_PHY_DP83840:
1561ba8c6fd5SDavid Greenman 	case FXP_PHY_DP83840A:
1562ba8c6fd5SDavid Greenman 		fxp_mdi_write(sc, sc->phy_primary_addr, FXP_DP83840_PCR,
1563ba8c6fd5SDavid Greenman 		    fxp_mdi_read(sc, sc->phy_primary_addr, FXP_DP83840_PCR) |
1564ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_LED4_MODE |	/* LED4 always indicates duplex */
1565ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_F_CONNECT |	/* force link disconnect bypass */
1566ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_BIT10);	/* XXX I have no idea */
1567ba8c6fd5SDavid Greenman 		/* fall through */
156892924291SDavid Greenman 	case FXP_PHY_82553A:
156992924291SDavid Greenman 	case FXP_PHY_82553C: /* untested */
1570ba8c6fd5SDavid Greenman 	case FXP_PHY_82555:
157192924291SDavid Greenman 	case FXP_PHY_82555B:
1572ba8c6fd5SDavid Greenman 		if (IFM_SUBTYPE(media) != IFM_AUTO) {
1573ba8c6fd5SDavid Greenman 			int flags;
1574ba8c6fd5SDavid Greenman 
1575ba8c6fd5SDavid Greenman 			flags = (IFM_SUBTYPE(media) == IFM_100_TX) ?
1576ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR_SPEED_100M : 0;
1577ba8c6fd5SDavid Greenman 			flags |= (media & IFM_FDX) ?
1578ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR_FULLDUPLEX : 0;
1579ba8c6fd5SDavid Greenman 			fxp_mdi_write(sc, sc->phy_primary_addr,
1580ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR,
1581ba8c6fd5SDavid Greenman 			    (fxp_mdi_read(sc, sc->phy_primary_addr,
1582ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR) &
1583ba8c6fd5SDavid Greenman 			    ~(FXP_PHY_BMCR_AUTOEN | FXP_PHY_BMCR_SPEED_100M |
1584ba8c6fd5SDavid Greenman 			     FXP_PHY_BMCR_FULLDUPLEX)) | flags);
1585ba8c6fd5SDavid Greenman 		} else {
1586ba8c6fd5SDavid Greenman 			fxp_mdi_write(sc, sc->phy_primary_addr,
1587ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR,
1588ba8c6fd5SDavid Greenman 			    (fxp_mdi_read(sc, sc->phy_primary_addr,
1589ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR) | FXP_PHY_BMCR_AUTOEN));
1590ba8c6fd5SDavid Greenman 		}
1591ba8c6fd5SDavid Greenman 		break;
1592ba8c6fd5SDavid Greenman 	/*
1593ba8c6fd5SDavid Greenman 	 * The Seeq 80c24 doesn't have a PHY programming interface, so do
1594ba8c6fd5SDavid Greenman 	 * nothing.
1595ba8c6fd5SDavid Greenman 	 */
1596ba8c6fd5SDavid Greenman 	case FXP_PHY_80C24:
1597ba8c6fd5SDavid Greenman 		break;
1598ba8c6fd5SDavid Greenman 	default:
1599ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT
1600ba8c6fd5SDavid Greenman 		    ": warning: unsupported PHY, type = %d, addr = %d\n",
1601ba8c6fd5SDavid Greenman 		     FXP_ARGS(sc), sc->phy_primary_device,
1602ba8c6fd5SDavid Greenman 		     sc->phy_primary_addr);
1603ba8c6fd5SDavid Greenman 	}
1604ba8c6fd5SDavid Greenman }
1605ba8c6fd5SDavid Greenman 
1606ba8c6fd5SDavid Greenman /*
1607ba8c6fd5SDavid Greenman  * Change media according to request.
1608ba8c6fd5SDavid Greenman  */
1609ba8c6fd5SDavid Greenman int
1610ba8c6fd5SDavid Greenman fxp_mediachange(ifp)
1611ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
1612ba8c6fd5SDavid Greenman {
1613ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1614ba8c6fd5SDavid Greenman 	struct ifmedia *ifm = &sc->sc_media;
1615ba8c6fd5SDavid Greenman 
1616ba8c6fd5SDavid Greenman 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1617ba8c6fd5SDavid Greenman 		return (EINVAL);
1618ba8c6fd5SDavid Greenman 
1619ba8c6fd5SDavid Greenman 	fxp_set_media(sc, ifm->ifm_media);
1620ba8c6fd5SDavid Greenman 	return (0);
1621ba8c6fd5SDavid Greenman }
1622ba8c6fd5SDavid Greenman 
1623ba8c6fd5SDavid Greenman /*
1624ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1625ba8c6fd5SDavid Greenman  */
1626ba8c6fd5SDavid Greenman void
1627ba8c6fd5SDavid Greenman fxp_mediastatus(ifp, ifmr)
1628ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
1629ba8c6fd5SDavid Greenman 	struct ifmediareq *ifmr;
1630ba8c6fd5SDavid Greenman {
1631ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1632a7280784SJulian Elischer 	int flags, stsflags;
1633ba8c6fd5SDavid Greenman 
1634ba8c6fd5SDavid Greenman 	switch (sc->phy_primary_device) {
1635ba8c6fd5SDavid Greenman 	case FXP_PHY_82555:
163635517ab7SDavid Greenman 	case FXP_PHY_82555B:
1637a7280784SJulian Elischer 	case FXP_PHY_DP83840:
1638a7280784SJulian Elischer 	case FXP_PHY_DP83840A:
1639a7280784SJulian Elischer 		ifmr->ifm_status = IFM_AVALID; /* IFM_ACTIVE will be valid */
1640ba8c6fd5SDavid Greenman 		ifmr->ifm_active = IFM_ETHER;
1641a7280784SJulian Elischer 		/*
1642a7280784SJulian Elischer 		 * the following is not an error.
1643a7280784SJulian Elischer 		 * You need to read this register twice to get current
1644a7280784SJulian Elischer 		 * status. This is correct documented behaviour, the
1645a7280784SJulian Elischer 		 * first read gets latched values.
1646a7280784SJulian Elischer 		 */
1647a7280784SJulian Elischer 		stsflags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_STS);
1648a7280784SJulian Elischer 		stsflags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_STS);
1649a7280784SJulian Elischer 		if (stsflags & FXP_PHY_STS_LINK_STS)
1650a7280784SJulian Elischer 				ifmr->ifm_status |= IFM_ACTIVE;
1651a7280784SJulian Elischer 
1652a7280784SJulian Elischer 		/*
1653a7280784SJulian Elischer 		 * If we are in auto mode, then try report the result.
1654a7280784SJulian Elischer 		 */
1655a7280784SJulian Elischer 		flags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_BMCR);
1656f1bf08c2SJulian Elischer 		if (flags & FXP_PHY_BMCR_AUTOEN) {
1657f1bf08c2SJulian Elischer 			ifmr->ifm_active |= IFM_AUTO; /* XXX presently 0 */
1658a7280784SJulian Elischer 			if (stsflags & FXP_PHY_STS_AUTO_DONE) {
1659f1bf08c2SJulian Elischer 				/*
1660a7280784SJulian Elischer 				 * Intel and National parts report
1661a7280784SJulian Elischer 				 * differently on what they found.
1662f1bf08c2SJulian Elischer 				 */
1663f1bf08c2SJulian Elischer 				if ((sc->phy_primary_device == FXP_PHY_82555)
1664f1bf08c2SJulian Elischer 				|| (sc->phy_primary_device == FXP_PHY_82555B)) {
1665f1bf08c2SJulian Elischer 					flags = fxp_mdi_read(sc,
1666a7280784SJulian Elischer 						sc->phy_primary_addr,
1667a7280784SJulian Elischer 						FXP_PHY_USC);
1668f1bf08c2SJulian Elischer 
1669f1bf08c2SJulian Elischer 					if (flags & FXP_PHY_USC_SPEED)
1670f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_100_TX;
1671f1bf08c2SJulian Elischer 					else
1672f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_10_T;
1673f1bf08c2SJulian Elischer 
1674f1bf08c2SJulian Elischer 					if (flags & FXP_PHY_USC_DUPLEX)
1675f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_FDX;
1676a7280784SJulian Elischer 				} else { /* it's National. only know speed  */
1677a7280784SJulian Elischer 					flags = fxp_mdi_read(sc,
1678a7280784SJulian Elischer 						sc->phy_primary_addr,
1679a7280784SJulian Elischer 						FXP_DP83840_PAR);
1680a7280784SJulian Elischer 
1681a7280784SJulian Elischer 					if (flags & FXP_DP83840_PAR_SPEED_10)
1682a7280784SJulian Elischer 						ifmr->ifm_active |= IFM_10_T;
1683a7280784SJulian Elischer 					else
1684a7280784SJulian Elischer 						ifmr->ifm_active |= IFM_100_TX;
1685f1bf08c2SJulian Elischer 				}
1686a7280784SJulian Elischer 			}
1687a7280784SJulian Elischer 		} else { /* in manual mode.. just report what we were set to */
1688ba8c6fd5SDavid Greenman 			if (flags & FXP_PHY_BMCR_SPEED_100M)
1689ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_100_TX;
1690ba8c6fd5SDavid Greenman 			else
1691ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_10_T;
1692ba8c6fd5SDavid Greenman 
1693ba8c6fd5SDavid Greenman 			if (flags & FXP_PHY_BMCR_FULLDUPLEX)
1694ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_FDX;
1695ba8c6fd5SDavid Greenman 		}
1696ba8c6fd5SDavid Greenman 		break;
1697ba8c6fd5SDavid Greenman 
1698ba8c6fd5SDavid Greenman 	case FXP_PHY_80C24:
1699ba8c6fd5SDavid Greenman 	default:
1700ba8c6fd5SDavid Greenman 		ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; /* XXX IFM_AUTO ? */
1701ba8c6fd5SDavid Greenman 	}
1702ba8c6fd5SDavid Greenman }
1703ba8c6fd5SDavid Greenman 
1704a17c678eSDavid Greenman /*
1705a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1706a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1707a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1708dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1709a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1710a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1711a17c678eSDavid Greenman  */
1712a17c678eSDavid Greenman static int
1713a17c678eSDavid Greenman fxp_add_rfabuf(sc, oldm)
1714a17c678eSDavid Greenman 	struct fxp_softc *sc;
1715a17c678eSDavid Greenman 	struct mbuf *oldm;
1716a17c678eSDavid Greenman {
1717ba8c6fd5SDavid Greenman 	u_int32_t v;
1718a17c678eSDavid Greenman 	struct mbuf *m;
1719a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1720a17c678eSDavid Greenman 
1721a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1722a17c678eSDavid Greenman 	if (m != NULL) {
1723a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1724a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1725a17c678eSDavid Greenman 			m_freem(m);
1726eadd5e3aSDavid Greenman 			if (oldm == NULL)
1727eadd5e3aSDavid Greenman 				return 1;
1728a17c678eSDavid Greenman 			m = oldm;
1729eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1730a17c678eSDavid Greenman 		}
1731a17c678eSDavid Greenman 	} else {
1732eadd5e3aSDavid Greenman 		if (oldm == NULL)
1733a17c678eSDavid Greenman 			return 1;
1734eadd5e3aSDavid Greenman 		m = oldm;
1735eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1736eadd5e3aSDavid Greenman 	}
1737ba8c6fd5SDavid Greenman 
1738ba8c6fd5SDavid Greenman 	/*
1739ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1740ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1741ba8c6fd5SDavid Greenman 	 */
1742ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1743ba8c6fd5SDavid Greenman 
1744eadd5e3aSDavid Greenman 	/*
1745eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1746eadd5e3aSDavid Greenman 	 * data start past it.
1747eadd5e3aSDavid Greenman 	 */
1748a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1749eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
17504fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1751eadd5e3aSDavid Greenman 
1752ba8c6fd5SDavid Greenman 	/*
1753ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1754ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1755ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1756ba8c6fd5SDavid Greenman 	 */
17574fc1dda9SAndrew Gallatin 
1758a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1759a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1760a17c678eSDavid Greenman 	rfa->actual_size = 0;
1761ba8c6fd5SDavid Greenman 
1762ba8c6fd5SDavid Greenman 	v = -1;
17634fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
17644fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1765ba8c6fd5SDavid Greenman 
1766dfe61cf1SDavid Greenman 	/*
1767dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1768dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1769dfe61cf1SDavid Greenman 	 */
1770a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1771ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1772ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1773a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1774ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
17754fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1776aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1777a17c678eSDavid Greenman 	} else {
1778a17c678eSDavid Greenman 		sc->rfa_headm = m;
1779a17c678eSDavid Greenman 	}
1780a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1781a17c678eSDavid Greenman 
1782dfe61cf1SDavid Greenman 	return (m == oldm);
1783a17c678eSDavid Greenman }
1784a17c678eSDavid Greenman 
17856ebc3153SDavid Greenman static volatile int
1786ba8c6fd5SDavid Greenman fxp_mdi_read(sc, phy, reg)
1787ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1788dccee1a1SDavid Greenman 	int phy;
1789dccee1a1SDavid Greenman 	int reg;
1790dccee1a1SDavid Greenman {
1791dccee1a1SDavid Greenman 	int count = 10000;
17926ebc3153SDavid Greenman 	int value;
1793dccee1a1SDavid Greenman 
1794ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1795ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1796dccee1a1SDavid Greenman 
1797ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1798ba8c6fd5SDavid Greenman 	    && count--)
17996ebc3153SDavid Greenman 		DELAY(10);
1800dccee1a1SDavid Greenman 
1801dccee1a1SDavid Greenman 	if (count <= 0)
1802ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT ": fxp_mdi_read: timed out\n",
1803ba8c6fd5SDavid Greenman 		    FXP_ARGS(sc));
1804dccee1a1SDavid Greenman 
18056ebc3153SDavid Greenman 	return (value & 0xffff);
1806dccee1a1SDavid Greenman }
1807dccee1a1SDavid Greenman 
1808dccee1a1SDavid Greenman static void
1809ba8c6fd5SDavid Greenman fxp_mdi_write(sc, phy, reg, value)
1810ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1811dccee1a1SDavid Greenman 	int phy;
1812dccee1a1SDavid Greenman 	int reg;
1813dccee1a1SDavid Greenman 	int value;
1814dccee1a1SDavid Greenman {
1815dccee1a1SDavid Greenman 	int count = 10000;
1816dccee1a1SDavid Greenman 
1817ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1818ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1819ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1820dccee1a1SDavid Greenman 
1821ba8c6fd5SDavid Greenman 	while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1822ba8c6fd5SDavid Greenman 	    count--)
18236ebc3153SDavid Greenman 		DELAY(10);
1824dccee1a1SDavid Greenman 
1825dccee1a1SDavid Greenman 	if (count <= 0)
1826ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT ": fxp_mdi_write: timed out\n",
1827ba8c6fd5SDavid Greenman 		    FXP_ARGS(sc));
1828dccee1a1SDavid Greenman }
1829dccee1a1SDavid Greenman 
1830dccee1a1SDavid Greenman static int
1831a17c678eSDavid Greenman fxp_ioctl(ifp, command, data)
1832a17c678eSDavid Greenman 	struct ifnet *ifp;
1833ba8c6fd5SDavid Greenman 	FXP_IOCTLCMD_TYPE command;
1834a17c678eSDavid Greenman 	caddr_t data;
1835a17c678eSDavid Greenman {
18369b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1837a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1838a17c678eSDavid Greenman 	int s, error = 0;
1839a17c678eSDavid Greenman 
1840a17c678eSDavid Greenman 	s = splimp();
1841a17c678eSDavid Greenman 
1842a17c678eSDavid Greenman 	switch (command) {
1843a17c678eSDavid Greenman 
1844a17c678eSDavid Greenman 	case SIOCSIFADDR:
1845ba8c6fd5SDavid Greenman #if !defined(__NetBSD__)
1846a17c678eSDavid Greenman 	case SIOCGIFADDR:
1847fb583156SDavid Greenman 	case SIOCSIFMTU:
1848ba8c6fd5SDavid Greenman #endif
1849fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1850a17c678eSDavid Greenman 		break;
1851a17c678eSDavid Greenman 
1852a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1853397f9dfeSDavid Greenman 		sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1854a17c678eSDavid Greenman 
1855a17c678eSDavid Greenman 		/*
1856a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1857a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1858a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1859a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1860a17c678eSDavid Greenman 		 */
1861a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1862fb583156SDavid Greenman 			fxp_init(sc);
1863a17c678eSDavid Greenman 		} else {
1864a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
18654a5f1499SDavid Greenman 				fxp_stop(sc);
1866a17c678eSDavid Greenman 		}
1867a17c678eSDavid Greenman 		break;
1868a17c678eSDavid Greenman 
1869a17c678eSDavid Greenman 	case SIOCADDMULTI:
1870a17c678eSDavid Greenman 	case SIOCDELMULTI:
1871397f9dfeSDavid Greenman 		sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1872ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1873ba8c6fd5SDavid Greenman 		error = (command == SIOCADDMULTI) ?
1874ba8c6fd5SDavid Greenman 		    ether_addmulti(ifr, &sc->sc_ethercom) :
1875ba8c6fd5SDavid Greenman 		    ether_delmulti(ifr, &sc->sc_ethercom);
1876ba8c6fd5SDavid Greenman 
1877ba8c6fd5SDavid Greenman 		if (error == ENETRESET) {
1878ba8c6fd5SDavid Greenman 			/*
1879ba8c6fd5SDavid Greenman 			 * Multicast list has changed; set the hardware
1880ba8c6fd5SDavid Greenman 			 * filter accordingly.
1881ba8c6fd5SDavid Greenman 			 */
1882397f9dfeSDavid Greenman 			if (!sc->all_mcasts)
1883397f9dfeSDavid Greenman 				fxp_mc_setup(sc);
1884397f9dfeSDavid Greenman 			/*
1885397f9dfeSDavid Greenman 			 * fxp_mc_setup() can turn on all_mcasts if we run
1886397f9dfeSDavid Greenman 			 * out of space, so check it again rather than else {}.
1887397f9dfeSDavid Greenman 			 */
1888397f9dfeSDavid Greenman 			if (sc->all_mcasts)
1889ba8c6fd5SDavid Greenman 				fxp_init(sc);
1890ba8c6fd5SDavid Greenman 			error = 0;
1891ba8c6fd5SDavid Greenman 		}
1892ba8c6fd5SDavid Greenman #else /* __FreeBSD__ */
1893a17c678eSDavid Greenman 		/*
1894a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1895a17c678eSDavid Greenman 		 * accordingly.
1896a17c678eSDavid Greenman 		 */
1897397f9dfeSDavid Greenman 		if (!sc->all_mcasts)
1898397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1899397f9dfeSDavid Greenman 		/*
1900397f9dfeSDavid Greenman 		 * fxp_mc_setup() can turn on sc->all_mcasts, so check it
1901397f9dfeSDavid Greenman 		 * again rather than else {}.
1902397f9dfeSDavid Greenman 		 */
1903397f9dfeSDavid Greenman 		if (sc->all_mcasts)
1904fb583156SDavid Greenman 			fxp_init(sc);
1905a17c678eSDavid Greenman 		error = 0;
1906ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
1907ba8c6fd5SDavid Greenman 		break;
1908ba8c6fd5SDavid Greenman 
1909ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1910ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1911ba8c6fd5SDavid Greenman 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1912a17c678eSDavid Greenman 		break;
1913a17c678eSDavid Greenman 
1914a17c678eSDavid Greenman 	default:
1915a17c678eSDavid Greenman 		error = EINVAL;
1916a17c678eSDavid Greenman 	}
1917a17c678eSDavid Greenman 	(void) splx(s);
1918a17c678eSDavid Greenman 	return (error);
1919a17c678eSDavid Greenman }
1920397f9dfeSDavid Greenman 
1921397f9dfeSDavid Greenman /*
1922397f9dfeSDavid Greenman  * Program the multicast filter.
1923397f9dfeSDavid Greenman  *
1924397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1925397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
19263114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1927397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1928dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1929397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1930397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1931397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1932397f9dfeSDavid Greenman  *
1933397f9dfeSDavid Greenman  * This function must be called at splimp.
1934397f9dfeSDavid Greenman  */
1935397f9dfeSDavid Greenman static void
1936397f9dfeSDavid Greenman fxp_mc_setup(sc)
1937397f9dfeSDavid Greenman 	struct fxp_softc *sc;
1938397f9dfeSDavid Greenman {
1939397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1940397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1941397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1942397f9dfeSDavid Greenman 	int nmcasts;
1943397f9dfeSDavid Greenman 
19443114fdb4SDavid Greenman 	/*
19453114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
19463114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
19473114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
19483114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
19493114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
19503114fdb4SDavid Greenman 	 */
1951397f9dfeSDavid Greenman 	if (sc->tx_queued) {
19523114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
19533114fdb4SDavid Greenman 
19543114fdb4SDavid Greenman 		/*
19553114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
19563114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
19573114fdb4SDavid Greenman 		 */
19583114fdb4SDavid Greenman 		if (sc->need_mcsetup)
19593114fdb4SDavid Greenman 			return;
1960397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
19613114fdb4SDavid Greenman 
19623114fdb4SDavid Greenman 		/*
19633114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
19643114fdb4SDavid Greenman 		 * TX commands have been processed.
19653114fdb4SDavid Greenman 		 */
19663114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
19673114fdb4SDavid Greenman 		txp->mb_head = NULL;
19683114fdb4SDavid Greenman 		txp->cb_status = 0;
19693114fdb4SDavid Greenman 		txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
19703114fdb4SDavid Greenman 		/*
19713114fdb4SDavid Greenman 		 * Advance the end of list forward.
19723114fdb4SDavid Greenman 		 */
19733114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
19743114fdb4SDavid Greenman 		sc->cbl_last = txp;
19753114fdb4SDavid Greenman 		sc->tx_queued++;
19763114fdb4SDavid Greenman 		/*
19773114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
19783114fdb4SDavid Greenman 		 */
19793114fdb4SDavid Greenman 		fxp_scb_wait(sc);
19803114fdb4SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
19813114fdb4SDavid Greenman 		/*
19823114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
19833114fdb4SDavid Greenman 		 * card again.
19843114fdb4SDavid Greenman 		 */
19853114fdb4SDavid Greenman 		ifp->if_timer = 5;
19863114fdb4SDavid Greenman 
1987397f9dfeSDavid Greenman 		return;
1988397f9dfeSDavid Greenman 	}
1989397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
1990397f9dfeSDavid Greenman 
1991397f9dfeSDavid Greenman 	/*
1992397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
1993397f9dfeSDavid Greenman 	 */
1994397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
1995397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
1996397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
19973114fdb4SDavid Greenman 	mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
1998397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
1999397f9dfeSDavid Greenman 
2000397f9dfeSDavid Greenman 	nmcasts = 0;
2001397f9dfeSDavid Greenman 	if (!sc->all_mcasts) {
2002397f9dfeSDavid Greenman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
2003397f9dfeSDavid Greenman 		    ifma = ifma->ifma_link.le_next) {
2004397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
2005397f9dfeSDavid Greenman 				continue;
2006397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
2007397f9dfeSDavid Greenman 				sc->all_mcasts = 1;
2008397f9dfeSDavid Greenman 				nmcasts = 0;
2009397f9dfeSDavid Greenman 				break;
2010397f9dfeSDavid Greenman 			}
2011397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
20128aef1712SMatthew Dillon 			    (volatile void *) &sc->mcsp->mc_addr[nmcasts][0], 6);
2013397f9dfeSDavid Greenman 			nmcasts++;
2014397f9dfeSDavid Greenman 		}
2015397f9dfeSDavid Greenman 	}
2016397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
2017397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2018397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2019397f9dfeSDavid Greenman 
2020397f9dfeSDavid Greenman 	/*
2021397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2022397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2023397f9dfeSDavid Greenman 	 */
2024397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
2025397f9dfeSDavid Greenman 	    FXP_SCB_CUS_ACTIVE) ;
2026397f9dfeSDavid Greenman 
2027397f9dfeSDavid Greenman 	/*
2028397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2029397f9dfeSDavid Greenman 	 */
2030397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2031397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
2032397f9dfeSDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
2033397f9dfeSDavid Greenman 
20343114fdb4SDavid Greenman 	ifp->if_timer = 2;
2035397f9dfeSDavid Greenman 	return;
2036397f9dfeSDavid Greenman }
2037