xref: /freebsd/sys/dev/fxp/if_fxp.c (revision 069363018f2496323a5efb546a5e7ee0e673ebb1)
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 	 */
60121b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
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;
6074a684684SDavid Greenman 
608a17c678eSDavid Greenman 	splx(s);
6096182fdbdSPeter Wemm 	return 0;
610a17c678eSDavid Greenman 
611a17c678eSDavid Greenman  fail:
612a17c678eSDavid Greenman 	splx(s);
6136182fdbdSPeter Wemm 	return error;
6146182fdbdSPeter Wemm }
6156182fdbdSPeter Wemm 
6166182fdbdSPeter Wemm /*
6176182fdbdSPeter Wemm  * Detach interface.
6186182fdbdSPeter Wemm  */
6196182fdbdSPeter Wemm static int
6206182fdbdSPeter Wemm fxp_detach(device_t dev)
6216182fdbdSPeter Wemm {
6226182fdbdSPeter Wemm 	struct fxp_softc *sc = device_get_softc(dev);
6236182fdbdSPeter Wemm 	int s;
6246182fdbdSPeter Wemm 
6256182fdbdSPeter Wemm 	s = splimp();
6266182fdbdSPeter Wemm 
6276182fdbdSPeter Wemm 	/*
6286182fdbdSPeter Wemm 	 * Close down routes etc.
6296182fdbdSPeter Wemm 	 */
63021b8ebd9SArchie Cobbs 	ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
6316182fdbdSPeter Wemm 
6326182fdbdSPeter Wemm 	/*
6336182fdbdSPeter Wemm 	 * Stop DMA and drop transmit queue.
6346182fdbdSPeter Wemm 	 */
6356182fdbdSPeter Wemm 	fxp_stop(sc);
6366182fdbdSPeter Wemm 
6376182fdbdSPeter Wemm 	/*
6386182fdbdSPeter Wemm 	 * Deallocate resources.
6396182fdbdSPeter Wemm 	 */
6406182fdbdSPeter Wemm 	bus_teardown_intr(dev, sc->irq, sc->ih);
6416182fdbdSPeter Wemm 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
6426182fdbdSPeter Wemm 	bus_release_resource(dev, SYS_RES_MEMORY, FXP_PCI_MMBA, sc->mem);
6436182fdbdSPeter Wemm 
6446182fdbdSPeter Wemm 	/*
6456182fdbdSPeter Wemm 	 * Free all the receive buffers.
6466182fdbdSPeter Wemm 	 */
6476182fdbdSPeter Wemm 	if (sc->rfa_headm != NULL)
6486182fdbdSPeter Wemm 		m_freem(sc->rfa_headm);
6496182fdbdSPeter Wemm 
6506182fdbdSPeter Wemm 	/*
6516182fdbdSPeter Wemm 	 * Free all media structures.
6526182fdbdSPeter Wemm 	 */
6536182fdbdSPeter Wemm 	ifmedia_removeall(&sc->sc_media);
6546182fdbdSPeter Wemm 
6556182fdbdSPeter Wemm 	/*
6566182fdbdSPeter Wemm 	 * Free anciliary structures.
6576182fdbdSPeter Wemm 	 */
6586182fdbdSPeter Wemm 	free(sc->cbl_base, M_DEVBUF);
6596182fdbdSPeter Wemm 	free(sc->fxp_stats, M_DEVBUF);
6606182fdbdSPeter Wemm 	free(sc->mcsp, M_DEVBUF);
6616182fdbdSPeter Wemm 
6626182fdbdSPeter Wemm 	splx(s);
6636182fdbdSPeter Wemm 
6646182fdbdSPeter Wemm 	return 0;
665a17c678eSDavid Greenman }
666a17c678eSDavid Greenman 
667a17c678eSDavid Greenman /*
6684a684684SDavid Greenman  * Device shutdown routine. Called at system shutdown after sync. The
669a17c678eSDavid Greenman  * main purpose of this routine is to shut off receiver DMA so that
670a17c678eSDavid Greenman  * kernel memory doesn't get clobbered during warmboot.
671a17c678eSDavid Greenman  */
6726182fdbdSPeter Wemm static int
6736182fdbdSPeter Wemm fxp_shutdown(device_t dev)
674a17c678eSDavid Greenman {
6756182fdbdSPeter Wemm 	/*
6766182fdbdSPeter Wemm 	 * Make sure that DMA is disabled prior to reboot. Not doing
6776182fdbdSPeter Wemm 	 * do could allow DMA to corrupt kernel memory during the
6786182fdbdSPeter Wemm 	 * reboot before the driver initializes.
6796182fdbdSPeter Wemm 	 */
6806182fdbdSPeter Wemm 	fxp_stop((struct fxp_softc *) device_get_softc(dev));
6816182fdbdSPeter Wemm 	return 0;
682a17c678eSDavid Greenman }
683a17c678eSDavid Greenman 
6846182fdbdSPeter Wemm static device_method_t fxp_methods[] = {
6856182fdbdSPeter Wemm 	/* Device interface */
6866182fdbdSPeter Wemm 	DEVMETHOD(device_probe,		fxp_probe),
6876182fdbdSPeter Wemm 	DEVMETHOD(device_attach,	fxp_attach),
6886182fdbdSPeter Wemm 	DEVMETHOD(device_detach,	fxp_detach),
6896182fdbdSPeter Wemm 	DEVMETHOD(device_shutdown,	fxp_shutdown),
6906182fdbdSPeter Wemm 
6916182fdbdSPeter Wemm 	{ 0, 0 }
6926182fdbdSPeter Wemm };
6936182fdbdSPeter Wemm 
6946182fdbdSPeter Wemm static driver_t fxp_driver = {
6956182fdbdSPeter Wemm 	"fxp",
6966182fdbdSPeter Wemm 	fxp_methods,
6976182fdbdSPeter Wemm 	sizeof(struct fxp_softc),
6986182fdbdSPeter Wemm };
6996182fdbdSPeter Wemm 
7006182fdbdSPeter Wemm static devclass_t fxp_devclass;
7016182fdbdSPeter Wemm 
7029e4c647cSBill Paul DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, 0, 0);
7036182fdbdSPeter Wemm 
704ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
705ba8c6fd5SDavid Greenman 
706ba8c6fd5SDavid Greenman /*************************************************************
707ba8c6fd5SDavid Greenman  * End of operating system-specific autoconfiguration glue
708ba8c6fd5SDavid Greenman  *************************************************************/
709ba8c6fd5SDavid Greenman 
710ba8c6fd5SDavid Greenman /*
711ba8c6fd5SDavid Greenman  * Do generic parts of attach.
712ba8c6fd5SDavid Greenman  */
713ba8c6fd5SDavid Greenman static int
714ba8c6fd5SDavid Greenman fxp_attach_common(sc, enaddr)
715ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
716ba8c6fd5SDavid Greenman 	u_int8_t *enaddr;
717ba8c6fd5SDavid Greenman {
718ba8c6fd5SDavid Greenman 	u_int16_t data;
719ba8c6fd5SDavid Greenman 	int i, nmedia, defmedia;
720ba8c6fd5SDavid Greenman 	const int *media;
721ba8c6fd5SDavid Greenman 
722ba8c6fd5SDavid Greenman 	/*
723ba8c6fd5SDavid Greenman 	 * Reset to a stable state.
724ba8c6fd5SDavid Greenman 	 */
725ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
726ba8c6fd5SDavid Greenman 	DELAY(10);
727ba8c6fd5SDavid Greenman 
728ba8c6fd5SDavid Greenman 	sc->cbl_base = malloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
729ba8c6fd5SDavid Greenman 	    M_DEVBUF, M_NOWAIT);
730ba8c6fd5SDavid Greenman 	if (sc->cbl_base == NULL)
731ba8c6fd5SDavid Greenman 		goto fail;
73291aa9f90SDavid Greenman 	bzero(sc->cbl_base, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
733ba8c6fd5SDavid Greenman 
734ba8c6fd5SDavid Greenman 	sc->fxp_stats = malloc(sizeof(struct fxp_stats), M_DEVBUF, M_NOWAIT);
735ba8c6fd5SDavid Greenman 	if (sc->fxp_stats == NULL)
736ba8c6fd5SDavid Greenman 		goto fail;
737ba8c6fd5SDavid Greenman 	bzero(sc->fxp_stats, sizeof(struct fxp_stats));
738ba8c6fd5SDavid Greenman 
739397f9dfeSDavid Greenman 	sc->mcsp = malloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_NOWAIT);
740397f9dfeSDavid Greenman 	if (sc->mcsp == NULL)
741397f9dfeSDavid Greenman 		goto fail;
742397f9dfeSDavid Greenman 
743ba8c6fd5SDavid Greenman 	/*
744ba8c6fd5SDavid Greenman 	 * Pre-allocate our receive buffers.
745ba8c6fd5SDavid Greenman 	 */
746ba8c6fd5SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
747ba8c6fd5SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
748ba8c6fd5SDavid Greenman 			goto fail;
749ba8c6fd5SDavid Greenman 		}
750ba8c6fd5SDavid Greenman 	}
751ba8c6fd5SDavid Greenman 
752ba8c6fd5SDavid Greenman 	/*
753e9bf2fa7SDavid Greenman 	 * Find out how large of an SEEPROM we have.
754e9bf2fa7SDavid Greenman 	 */
755e9bf2fa7SDavid Greenman 	fxp_autosize_eeprom(sc);
756e9bf2fa7SDavid Greenman 
757e9bf2fa7SDavid Greenman 	/*
758ba8c6fd5SDavid Greenman 	 * Get info about the primary PHY
759ba8c6fd5SDavid Greenman 	 */
760ba8c6fd5SDavid Greenman 	fxp_read_eeprom(sc, (u_int16_t *)&data, 6, 1);
761ba8c6fd5SDavid Greenman 	sc->phy_primary_addr = data & 0xff;
762ba8c6fd5SDavid Greenman 	sc->phy_primary_device = (data >> 8) & 0x3f;
763ba8c6fd5SDavid Greenman 	sc->phy_10Mbps_only = data >> 15;
764ba8c6fd5SDavid Greenman 
765ba8c6fd5SDavid Greenman 	/*
766ba8c6fd5SDavid Greenman 	 * Read MAC address.
767ba8c6fd5SDavid Greenman 	 */
768ba8c6fd5SDavid Greenman 	fxp_read_eeprom(sc, (u_int16_t *)enaddr, 0, 3);
769ba8c6fd5SDavid Greenman 
770ba8c6fd5SDavid Greenman 	/*
771ba8c6fd5SDavid Greenman 	 * Initialize the media structures.
772ba8c6fd5SDavid Greenman 	 */
773ba8c6fd5SDavid Greenman 
774ba8c6fd5SDavid Greenman 	media = fxp_media_default;
775ba8c6fd5SDavid Greenman 	nmedia = sizeof(fxp_media_default) / sizeof(fxp_media_default[0]);
776ba8c6fd5SDavid Greenman 	defmedia = FXP_MEDIA_DEFAULT_DEFMEDIA;
777ba8c6fd5SDavid Greenman 
778ba8c6fd5SDavid Greenman 	for (i = 0; i < NFXPMEDIA; i++) {
779ba8c6fd5SDavid Greenman 		if (sc->phy_primary_device == fxp_media[i].fsm_phy) {
780ba8c6fd5SDavid Greenman 			media = fxp_media[i].fsm_media;
781ba8c6fd5SDavid Greenman 			nmedia = fxp_media[i].fsm_nmedia;
782ba8c6fd5SDavid Greenman 			defmedia = fxp_media[i].fsm_defmedia;
783ba8c6fd5SDavid Greenman 		}
784ba8c6fd5SDavid Greenman 	}
785ba8c6fd5SDavid Greenman 
786ba8c6fd5SDavid Greenman 	ifmedia_init(&sc->sc_media, 0, fxp_mediachange, fxp_mediastatus);
787ba8c6fd5SDavid Greenman 	for (i = 0; i < nmedia; i++) {
788ba8c6fd5SDavid Greenman 		if (IFM_SUBTYPE(media[i]) == IFM_100_TX && sc->phy_10Mbps_only)
789ba8c6fd5SDavid Greenman 			continue;
790ba8c6fd5SDavid Greenman 		ifmedia_add(&sc->sc_media, media[i], 0, NULL);
791ba8c6fd5SDavid Greenman 	}
792ba8c6fd5SDavid Greenman 	ifmedia_set(&sc->sc_media, defmedia);
793ba8c6fd5SDavid Greenman 
794ba8c6fd5SDavid Greenman 	return (0);
795ba8c6fd5SDavid Greenman 
796ba8c6fd5SDavid Greenman  fail:
797ba8c6fd5SDavid Greenman 	printf(FXP_FORMAT ": Failed to malloc memory\n", FXP_ARGS(sc));
798ba8c6fd5SDavid Greenman 	if (sc->cbl_base)
799ba8c6fd5SDavid Greenman 		free(sc->cbl_base, M_DEVBUF);
800ba8c6fd5SDavid Greenman 	if (sc->fxp_stats)
801ba8c6fd5SDavid Greenman 		free(sc->fxp_stats, M_DEVBUF);
802397f9dfeSDavid Greenman 	if (sc->mcsp)
803397f9dfeSDavid Greenman 		free(sc->mcsp, M_DEVBUF);
804ba8c6fd5SDavid Greenman 	/* frees entire chain */
805ba8c6fd5SDavid Greenman 	if (sc->rfa_headm)
806ba8c6fd5SDavid Greenman 		m_freem(sc->rfa_headm);
807ba8c6fd5SDavid Greenman 
808ba8c6fd5SDavid Greenman 	return (ENOMEM);
809ba8c6fd5SDavid Greenman }
810ba8c6fd5SDavid Greenman 
811ba8c6fd5SDavid Greenman /*
812e9bf2fa7SDavid Greenman  * From NetBSD:
813e9bf2fa7SDavid Greenman  *
814e9bf2fa7SDavid Greenman  * Figure out EEPROM size.
815e9bf2fa7SDavid Greenman  *
816e9bf2fa7SDavid Greenman  * 559's can have either 64-word or 256-word EEPROMs, the 558
817e9bf2fa7SDavid Greenman  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
818e9bf2fa7SDavid Greenman  * talks about the existance of 16 to 256 word EEPROMs.
819e9bf2fa7SDavid Greenman  *
820e9bf2fa7SDavid Greenman  * The only known sizes are 64 and 256, where the 256 version is used
821e9bf2fa7SDavid Greenman  * by CardBus cards to store CIS information.
822e9bf2fa7SDavid Greenman  *
823e9bf2fa7SDavid Greenman  * The address is shifted in msb-to-lsb, and after the last
824e9bf2fa7SDavid Greenman  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
825e9bf2fa7SDavid Greenman  * after which follows the actual data. We try to detect this zero, by
826e9bf2fa7SDavid Greenman  * probing the data-out bit in the EEPROM control register just after
827e9bf2fa7SDavid Greenman  * having shifted in a bit. If the bit is zero, we assume we've
828e9bf2fa7SDavid Greenman  * shifted enough address bits. The data-out should be tri-state,
829e9bf2fa7SDavid Greenman  * before this, which should translate to a logical one.
830e9bf2fa7SDavid Greenman  *
831e9bf2fa7SDavid Greenman  * Other ways to do this would be to try to read a register with known
832e9bf2fa7SDavid Greenman  * contents with a varying number of address bits, but no such
833e9bf2fa7SDavid Greenman  * register seem to be available. The high bits of register 10 are 01
834e9bf2fa7SDavid Greenman  * on the 558 and 559, but apparently not on the 557.
835e9bf2fa7SDavid Greenman  *
836e9bf2fa7SDavid Greenman  * The Linux driver computes a checksum on the EEPROM data, but the
837e9bf2fa7SDavid Greenman  * value of this checksum is not very well documented.
838e9bf2fa7SDavid Greenman  */
839e9bf2fa7SDavid Greenman static void
840e9bf2fa7SDavid Greenman fxp_autosize_eeprom(sc)
841e9bf2fa7SDavid Greenman 	struct fxp_softc *sc;
842e9bf2fa7SDavid Greenman {
843e9bf2fa7SDavid Greenman 	u_int16_t reg;
844e9bf2fa7SDavid Greenman 	int x;
845e9bf2fa7SDavid Greenman 
846e9bf2fa7SDavid Greenman 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
847e9bf2fa7SDavid Greenman 	/*
848e9bf2fa7SDavid Greenman 	 * Shift in read opcode.
849e9bf2fa7SDavid Greenman 	 */
850e9bf2fa7SDavid Greenman 	for (x = 3; x > 0; x--) {
851e9bf2fa7SDavid Greenman 		if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
852e9bf2fa7SDavid Greenman 			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
853e9bf2fa7SDavid Greenman 		} else {
854e9bf2fa7SDavid Greenman 			reg = FXP_EEPROM_EECS;
855e9bf2fa7SDavid Greenman 		}
856e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
857e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
858e9bf2fa7SDavid Greenman 		    reg | FXP_EEPROM_EESK);
859e9bf2fa7SDavid Greenman 		DELAY(1);
860e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
861e9bf2fa7SDavid Greenman 		DELAY(1);
862e9bf2fa7SDavid Greenman 	}
863e9bf2fa7SDavid Greenman 	/*
864e9bf2fa7SDavid Greenman 	 * Shift in address.
865e9bf2fa7SDavid Greenman 	 * Wait for the dummy zero following a correct address shift.
866e9bf2fa7SDavid Greenman 	 */
867e9bf2fa7SDavid Greenman 	for (x = 1; x <= 8; x++) {
868e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
869e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
870e9bf2fa7SDavid Greenman 			FXP_EEPROM_EECS | FXP_EEPROM_EESK);
871e9bf2fa7SDavid Greenman 		DELAY(1);
872e9bf2fa7SDavid Greenman 		if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO) == 0)
873e9bf2fa7SDavid Greenman 			break;
874e9bf2fa7SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
875e9bf2fa7SDavid Greenman 		DELAY(1);
876e9bf2fa7SDavid Greenman 	}
877e9bf2fa7SDavid Greenman 	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
878e9bf2fa7SDavid Greenman 	DELAY(1);
879e9bf2fa7SDavid Greenman 	sc->eeprom_size = x;
880e9bf2fa7SDavid Greenman }
881e9bf2fa7SDavid Greenman /*
882ba8c6fd5SDavid Greenman  * Read from the serial EEPROM. Basically, you manually shift in
883ba8c6fd5SDavid Greenman  * the read opcode (one bit at a time) and then shift in the address,
884ba8c6fd5SDavid Greenman  * and then you shift out the data (all of this one bit at a time).
885ba8c6fd5SDavid Greenman  * The word size is 16 bits, so you have to provide the address for
886ba8c6fd5SDavid Greenman  * every 16 bits of data.
887ba8c6fd5SDavid Greenman  */
888ba8c6fd5SDavid Greenman static void
889ba8c6fd5SDavid Greenman fxp_read_eeprom(sc, data, offset, words)
890ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
891ba8c6fd5SDavid Greenman 	u_short *data;
892ba8c6fd5SDavid Greenman 	int offset;
893ba8c6fd5SDavid Greenman 	int words;
894ba8c6fd5SDavid Greenman {
895ba8c6fd5SDavid Greenman 	u_int16_t reg;
896ba8c6fd5SDavid Greenman 	int i, x;
897ba8c6fd5SDavid Greenman 
898ba8c6fd5SDavid Greenman 	for (i = 0; i < words; i++) {
899ba8c6fd5SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
900ba8c6fd5SDavid Greenman 		/*
901ba8c6fd5SDavid Greenman 		 * Shift in read opcode.
902ba8c6fd5SDavid Greenman 		 */
903ba8c6fd5SDavid Greenman 		for (x = 3; x > 0; x--) {
904ba8c6fd5SDavid Greenman 			if (FXP_EEPROM_OPC_READ & (1 << (x - 1))) {
905ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
906ba8c6fd5SDavid Greenman 			} else {
907ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS;
908ba8c6fd5SDavid Greenman 			}
909ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
910ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
911ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
912ba8c6fd5SDavid Greenman 			DELAY(1);
913ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
914ba8c6fd5SDavid Greenman 			DELAY(1);
915ba8c6fd5SDavid Greenman 		}
916ba8c6fd5SDavid Greenman 		/*
917ba8c6fd5SDavid Greenman 		 * Shift in address.
918ba8c6fd5SDavid Greenman 		 */
919e9bf2fa7SDavid Greenman 		for (x = sc->eeprom_size; x > 0; x--) {
920ba8c6fd5SDavid Greenman 			if ((i + offset) & (1 << (x - 1))) {
921ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
922ba8c6fd5SDavid Greenman 			} else {
923ba8c6fd5SDavid Greenman 				reg = FXP_EEPROM_EECS;
924ba8c6fd5SDavid Greenman 			}
925ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
926ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
927ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
928ba8c6fd5SDavid Greenman 			DELAY(1);
929ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
930ba8c6fd5SDavid Greenman 			DELAY(1);
931ba8c6fd5SDavid Greenman 		}
932ba8c6fd5SDavid Greenman 		reg = FXP_EEPROM_EECS;
933ba8c6fd5SDavid Greenman 		data[i] = 0;
934ba8c6fd5SDavid Greenman 		/*
935ba8c6fd5SDavid Greenman 		 * Shift out data.
936ba8c6fd5SDavid Greenman 		 */
937ba8c6fd5SDavid Greenman 		for (x = 16; x > 0; x--) {
938ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
939ba8c6fd5SDavid Greenman 			    reg | FXP_EEPROM_EESK);
940ba8c6fd5SDavid Greenman 			DELAY(1);
941ba8c6fd5SDavid Greenman 			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
942ba8c6fd5SDavid Greenman 			    FXP_EEPROM_EEDO)
943ba8c6fd5SDavid Greenman 				data[i] |= (1 << (x - 1));
944ba8c6fd5SDavid Greenman 			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
945ba8c6fd5SDavid Greenman 			DELAY(1);
946ba8c6fd5SDavid Greenman 		}
947ba8c6fd5SDavid Greenman 		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
948ba8c6fd5SDavid Greenman 		DELAY(1);
949ba8c6fd5SDavid Greenman 	}
950ba8c6fd5SDavid Greenman }
951ba8c6fd5SDavid Greenman 
952a17c678eSDavid Greenman /*
953a17c678eSDavid Greenman  * Start packet transmission on the interface.
954a17c678eSDavid Greenman  */
955a17c678eSDavid Greenman static void
956a17c678eSDavid Greenman fxp_start(ifp)
957a17c678eSDavid Greenman 	struct ifnet *ifp;
958a17c678eSDavid Greenman {
9599b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
960a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
961a17c678eSDavid Greenman 
962a17c678eSDavid Greenman 	/*
963483b9871SDavid Greenman 	 * See if we need to suspend xmit until the multicast filter
964483b9871SDavid Greenman 	 * has been reprogrammed (which can only be done at the head
965483b9871SDavid Greenman 	 * of the command chain).
966a17c678eSDavid Greenman 	 */
967483b9871SDavid Greenman 	if (sc->need_mcsetup)
968a17c678eSDavid Greenman 		return;
9691cd443acSDavid Greenman 
970483b9871SDavid Greenman 	txp = NULL;
971483b9871SDavid Greenman 
972483b9871SDavid Greenman 	/*
973483b9871SDavid Greenman 	 * We're finished if there is nothing more to add to the list or if
974483b9871SDavid Greenman 	 * we're all filled up with buffers to transmit.
9753114fdb4SDavid Greenman 	 * NOTE: One TxCB is reserved to guarantee that fxp_mc_setup() can add
9763114fdb4SDavid Greenman 	 *       a NOP command when needed.
977483b9871SDavid Greenman 	 */
9783114fdb4SDavid Greenman 	while (ifp->if_snd.ifq_head != NULL && sc->tx_queued < FXP_NTXCB - 1) {
979483b9871SDavid Greenman 		struct mbuf *m, *mb_head;
980483b9871SDavid Greenman 		int segment;
981483b9871SDavid Greenman 
982dfe61cf1SDavid Greenman 		/*
983dfe61cf1SDavid Greenman 		 * Grab a packet to transmit.
984dfe61cf1SDavid Greenman 		 */
9856318197eSDavid Greenman 		IF_DEQUEUE(&ifp->if_snd, mb_head);
986a17c678eSDavid Greenman 
987dfe61cf1SDavid Greenman 		/*
988483b9871SDavid Greenman 		 * Get pointer to next available tx desc.
989dfe61cf1SDavid Greenman 		 */
990a17c678eSDavid Greenman 		txp = sc->cbl_last->next;
991a17c678eSDavid Greenman 
992a17c678eSDavid Greenman 		/*
993a17c678eSDavid Greenman 		 * Go through each of the mbufs in the chain and initialize
994483b9871SDavid Greenman 		 * the transmit buffer descriptors with the physical address
995a17c678eSDavid Greenman 		 * and size of the mbuf.
996a17c678eSDavid Greenman 		 */
99723a0ed7cSDavid Greenman tbdinit:
998a17c678eSDavid Greenman 		for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
999a17c678eSDavid Greenman 			if (m->m_len != 0) {
1000a17c678eSDavid Greenman 				if (segment == FXP_NTXSEG)
1001a17c678eSDavid Greenman 					break;
1002a17c678eSDavid Greenman 				txp->tbd[segment].tb_addr =
1003a17c678eSDavid Greenman 				    vtophys(mtod(m, vm_offset_t));
1004a17c678eSDavid Greenman 				txp->tbd[segment].tb_size = m->m_len;
1005a17c678eSDavid Greenman 				segment++;
1006a17c678eSDavid Greenman 			}
1007a17c678eSDavid Greenman 		}
1008fb583156SDavid Greenman 		if (m != NULL) {
100923a0ed7cSDavid Greenman 			struct mbuf *mn;
101023a0ed7cSDavid Greenman 
1011a17c678eSDavid Greenman 			/*
1012a17c678eSDavid Greenman 			 * We ran out of segments. We have to recopy this mbuf
1013483b9871SDavid Greenman 			 * chain first. Bail out if we can't get the new buffers.
1014a17c678eSDavid Greenman 			 */
101523a0ed7cSDavid Greenman 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
101623a0ed7cSDavid Greenman 			if (mn == NULL) {
101723a0ed7cSDavid Greenman 				m_freem(mb_head);
1018483b9871SDavid Greenman 				break;
1019a17c678eSDavid Greenman 			}
102023a0ed7cSDavid Greenman 			if (mb_head->m_pkthdr.len > MHLEN) {
102123a0ed7cSDavid Greenman 				MCLGET(mn, M_DONTWAIT);
102223a0ed7cSDavid Greenman 				if ((mn->m_flags & M_EXT) == 0) {
102323a0ed7cSDavid Greenman 					m_freem(mn);
102423a0ed7cSDavid Greenman 					m_freem(mb_head);
1025483b9871SDavid Greenman 					break;
102623a0ed7cSDavid Greenman 				}
102723a0ed7cSDavid Greenman 			}
1028ba8c6fd5SDavid Greenman 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1029ba8c6fd5SDavid Greenman 			    mtod(mn, caddr_t));
103023a0ed7cSDavid Greenman 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
103123a0ed7cSDavid Greenman 			m_freem(mb_head);
103223a0ed7cSDavid Greenman 			mb_head = mn;
103323a0ed7cSDavid Greenman 			goto tbdinit;
103423a0ed7cSDavid Greenman 		}
103523a0ed7cSDavid Greenman 
103623a0ed7cSDavid Greenman 		txp->tbd_number = segment;
10371cd443acSDavid Greenman 		txp->mb_head = mb_head;
1038a17c678eSDavid Greenman 		txp->cb_status = 0;
10393114fdb4SDavid Greenman 		if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
1040a17c678eSDavid Greenman 			txp->cb_command =
1041a17c678eSDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S;
10423114fdb4SDavid Greenman 		} else {
10433114fdb4SDavid Greenman 			txp->cb_command =
10443114fdb4SDavid Greenman 			    FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
10453114fdb4SDavid Greenman 			/*
10463114fdb4SDavid Greenman 			 * Set a 5 second timer just in case we don't hear from the
10473114fdb4SDavid Greenman 			 * card again.
10483114fdb4SDavid Greenman 			 */
10493114fdb4SDavid Greenman 			ifp->if_timer = 5;
10503114fdb4SDavid Greenman 		}
1051f9be9005SDavid Greenman 		txp->tx_threshold = tx_threshold;
1052a17c678eSDavid Greenman 
1053a17c678eSDavid Greenman 		/*
1054483b9871SDavid Greenman 		 * Advance the end of list forward.
1055a17c678eSDavid Greenman 		 */
105606175228SAndrew Gallatin 
105706175228SAndrew Gallatin #ifdef __alpha__
105806175228SAndrew Gallatin 		/*
105906175228SAndrew Gallatin 		 * On platforms which can't access memory in 16-bit
106006175228SAndrew Gallatin 		 * granularities, we must prevent the card from DMA'ing
106106175228SAndrew Gallatin 		 * up the status while we update the command field.
106206175228SAndrew Gallatin 		 * This could cause us to overwrite the completion status.
106306175228SAndrew Gallatin 		 */
106406175228SAndrew Gallatin 		atomic_clear_short(&sc->cbl_last->cb_command,
106506175228SAndrew Gallatin 		    FXP_CB_COMMAND_S);
106606175228SAndrew Gallatin #else
1067a17c678eSDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
106806175228SAndrew Gallatin #endif /*__alpha__*/
1069a17c678eSDavid Greenman 		sc->cbl_last = txp;
1070a17c678eSDavid Greenman 
1071a17c678eSDavid Greenman 		/*
10721cd443acSDavid Greenman 		 * Advance the beginning of the list forward if there are
10731cd443acSDavid Greenman 		 * no other packets queued (when nothing is queued, cbl_first
1074483b9871SDavid Greenman 		 * sits on the last TxCB that was sent out).
1075a17c678eSDavid Greenman 		 */
10761cd443acSDavid Greenman 		if (sc->tx_queued == 0)
1077a17c678eSDavid Greenman 			sc->cbl_first = txp;
1078a17c678eSDavid Greenman 
10791cd443acSDavid Greenman 		sc->tx_queued++;
10801cd443acSDavid Greenman 
1081a17c678eSDavid Greenman 		/*
1082a17c678eSDavid Greenman 		 * Pass packet to bpf if there is a listener.
1083a17c678eSDavid Greenman 		 */
1084fb583156SDavid Greenman 		if (ifp->if_bpf)
1085ba8c6fd5SDavid Greenman 			bpf_mtap(FXP_BPFTAP_ARG(ifp), mb_head);
1086483b9871SDavid Greenman 	}
1087483b9871SDavid Greenman 
1088483b9871SDavid Greenman 	/*
1089483b9871SDavid Greenman 	 * We're finished. If we added to the list, issue a RESUME to get DMA
1090483b9871SDavid Greenman 	 * going again if suspended.
1091483b9871SDavid Greenman 	 */
1092483b9871SDavid Greenman 	if (txp != NULL) {
1093483b9871SDavid Greenman 		fxp_scb_wait(sc);
1094483b9871SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
1095483b9871SDavid Greenman 	}
1096a17c678eSDavid Greenman }
1097a17c678eSDavid Greenman 
1098a17c678eSDavid Greenman /*
10999c7d2607SDavid Greenman  * Process interface interrupts.
1100a17c678eSDavid Greenman  */
1101ba8c6fd5SDavid Greenman static FXP_INTR_TYPE
1102a17c678eSDavid Greenman fxp_intr(arg)
1103a17c678eSDavid Greenman 	void *arg;
1104a17c678eSDavid Greenman {
1105a17c678eSDavid Greenman 	struct fxp_softc *sc = arg;
1106ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
11071cd443acSDavid Greenman 	u_int8_t statack;
1108ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1109ba8c6fd5SDavid Greenman 	int claimed = 0;
1110ba8c6fd5SDavid Greenman #endif
1111a17c678eSDavid Greenman 
1112ba8c6fd5SDavid Greenman 	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1113ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1114ba8c6fd5SDavid Greenman 		claimed = 1;
1115ba8c6fd5SDavid Greenman #endif
1116a17c678eSDavid Greenman 		/*
1117a17c678eSDavid Greenman 		 * First ACK all the interrupts in this pass.
1118a17c678eSDavid Greenman 		 */
1119ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1120a17c678eSDavid Greenman 
1121a17c678eSDavid Greenman 		/*
11223114fdb4SDavid Greenman 		 * Free any finished transmit mbuf chains.
112306936301SBill Paul 		 *
112406936301SBill Paul 		 * Handle the CNA event likt a CXTNO event. It used to
112506936301SBill Paul 		 * be that this event (control unit not ready) was not
112606936301SBill Paul 		 * encountered, but it is now with the SMPng modifications.
112706936301SBill Paul 		 * The exact sequence of events that occur when the interface
112806936301SBill Paul 		 * is brought up are different now, and if this event
112906936301SBill Paul 		 * goes unhandled, the configuration/rxfilter setup sequence
113006936301SBill Paul 		 * can stall for several seconds. The result is that no
113106936301SBill Paul 		 * packets go out onto the wire for about 5 to 10 seconds
113206936301SBill Paul 		 * after the interface is ifconfig'ed for the first time.
11333114fdb4SDavid Greenman 		 */
113406936301SBill Paul 		if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
11353114fdb4SDavid Greenman 			struct fxp_cb_tx *txp;
11363114fdb4SDavid Greenman 
11373114fdb4SDavid Greenman 			for (txp = sc->cbl_first; sc->tx_queued &&
11383114fdb4SDavid Greenman 			    (txp->cb_status & FXP_CB_STATUS_C) != 0;
11393114fdb4SDavid Greenman 			    txp = txp->next) {
11403114fdb4SDavid Greenman 				if (txp->mb_head != NULL) {
11413114fdb4SDavid Greenman 					m_freem(txp->mb_head);
11423114fdb4SDavid Greenman 					txp->mb_head = NULL;
11433114fdb4SDavid Greenman 				}
11443114fdb4SDavid Greenman 				sc->tx_queued--;
11453114fdb4SDavid Greenman 			}
11463114fdb4SDavid Greenman 			sc->cbl_first = txp;
11473114fdb4SDavid Greenman 			ifp->if_timer = 0;
11483114fdb4SDavid Greenman 			if (sc->tx_queued == 0) {
11493114fdb4SDavid Greenman 				if (sc->need_mcsetup)
11503114fdb4SDavid Greenman 					fxp_mc_setup(sc);
11513114fdb4SDavid Greenman 			}
11523114fdb4SDavid Greenman 			/*
11533114fdb4SDavid Greenman 			 * Try to start more packets transmitting.
11543114fdb4SDavid Greenman 			 */
11553114fdb4SDavid Greenman 			if (ifp->if_snd.ifq_head != NULL)
11563114fdb4SDavid Greenman 				fxp_start(ifp);
11573114fdb4SDavid Greenman 		}
11583114fdb4SDavid Greenman 		/*
1159a17c678eSDavid Greenman 		 * Process receiver interrupts. If a no-resource (RNR)
1160a17c678eSDavid Greenman 		 * condition exists, get whatever packets we can and
1161a17c678eSDavid Greenman 		 * re-start the receiver.
1162a17c678eSDavid Greenman 		 */
1163a17c678eSDavid Greenman 		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR)) {
1164a17c678eSDavid Greenman 			struct mbuf *m;
1165a17c678eSDavid Greenman 			struct fxp_rfa *rfa;
1166a17c678eSDavid Greenman rcvloop:
1167a17c678eSDavid Greenman 			m = sc->rfa_headm;
1168ba8c6fd5SDavid Greenman 			rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
1169ba8c6fd5SDavid Greenman 			    RFA_ALIGNMENT_FUDGE);
1170a17c678eSDavid Greenman 
1171a17c678eSDavid Greenman 			if (rfa->rfa_status & FXP_RFA_STATUS_C) {
1172dfe61cf1SDavid Greenman 				/*
1173dfe61cf1SDavid Greenman 				 * Remove first packet from the chain.
1174dfe61cf1SDavid Greenman 				 */
1175a17c678eSDavid Greenman 				sc->rfa_headm = m->m_next;
1176a17c678eSDavid Greenman 				m->m_next = NULL;
1177a17c678eSDavid Greenman 
1178dfe61cf1SDavid Greenman 				/*
1179ba8c6fd5SDavid Greenman 				 * Add a new buffer to the receive chain.
1180ba8c6fd5SDavid Greenman 				 * If this fails, the old buffer is recycled
1181ba8c6fd5SDavid Greenman 				 * instead.
1182dfe61cf1SDavid Greenman 				 */
1183a17c678eSDavid Greenman 				if (fxp_add_rfabuf(sc, m) == 0) {
1184a17c678eSDavid Greenman 					struct ether_header *eh;
1185aed53495SDavid Greenman 					int total_len;
1186a17c678eSDavid Greenman 
1187ba8c6fd5SDavid Greenman 					total_len = rfa->actual_size &
1188ba8c6fd5SDavid Greenman 					    (MCLBYTES - 1);
1189ba8c6fd5SDavid Greenman 					if (total_len <
1190ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header)) {
119106339180SDavid Greenman 						m_freem(m);
119206339180SDavid Greenman 						goto rcvloop;
119306339180SDavid Greenman 					}
1194a17c678eSDavid Greenman 					m->m_pkthdr.rcvif = ifp;
11952e2de7f2SArchie Cobbs 					m->m_pkthdr.len = m->m_len = total_len;
1196a17c678eSDavid Greenman 					eh = mtod(m, struct ether_header *);
1197ba8c6fd5SDavid Greenman 					m->m_data +=
1198ba8c6fd5SDavid Greenman 					    sizeof(struct ether_header);
1199ab090e5bSLuigi Rizzo 					m->m_len -=
1200ab090e5bSLuigi Rizzo 					    sizeof(struct ether_header);
1201ab090e5bSLuigi Rizzo 					m->m_pkthdr.len = m->m_len;
1202a17c678eSDavid Greenman 					ether_input(ifp, eh, m);
1203a17c678eSDavid Greenman 				}
1204a17c678eSDavid Greenman 				goto rcvloop;
1205a17c678eSDavid Greenman 			}
1206a17c678eSDavid Greenman 			if (statack & FXP_SCB_STATACK_RNR) {
1207ba8c6fd5SDavid Greenman 				fxp_scb_wait(sc);
1208ba8c6fd5SDavid Greenman 				CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1209ba8c6fd5SDavid Greenman 				    vtophys(sc->rfa_headm->m_ext.ext_buf) +
1210ba8c6fd5SDavid Greenman 					RFA_ALIGNMENT_FUDGE);
1211ba8c6fd5SDavid Greenman 				CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1212ba8c6fd5SDavid Greenman 				    FXP_SCB_COMMAND_RU_START);
1213a17c678eSDavid Greenman 			}
1214a17c678eSDavid Greenman 		}
1215a17c678eSDavid Greenman 	}
1216ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1217ba8c6fd5SDavid Greenman 	return (claimed);
1218ba8c6fd5SDavid Greenman #endif
1219a17c678eSDavid Greenman }
1220a17c678eSDavid Greenman 
1221dfe61cf1SDavid Greenman /*
1222dfe61cf1SDavid Greenman  * Update packet in/out/collision statistics. The i82557 doesn't
1223dfe61cf1SDavid Greenman  * allow you to access these counters without doing a fairly
1224dfe61cf1SDavid Greenman  * expensive DMA to get _all_ of the statistics it maintains, so
1225dfe61cf1SDavid Greenman  * we do this operation here only once per second. The statistics
1226dfe61cf1SDavid Greenman  * counters in the kernel are updated from the previous dump-stats
1227dfe61cf1SDavid Greenman  * DMA and then a new dump-stats DMA is started. The on-chip
1228dfe61cf1SDavid Greenman  * counters are zeroed when the DMA completes. If we can't start
1229dfe61cf1SDavid Greenman  * the DMA immediately, we don't wait - we just prepare to read
1230dfe61cf1SDavid Greenman  * them again next time.
1231dfe61cf1SDavid Greenman  */
1232303b270bSEivind Eklund static void
1233a17c678eSDavid Greenman fxp_stats_update(arg)
1234a17c678eSDavid Greenman 	void *arg;
1235a17c678eSDavid Greenman {
1236a17c678eSDavid Greenman 	struct fxp_softc *sc = arg;
1237ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1238a17c678eSDavid Greenman 	struct fxp_stats *sp = sc->fxp_stats;
1239c8cc6fcaSDavid Greenman 	struct fxp_cb_tx *txp;
1240397f9dfeSDavid Greenman 	int s;
1241a17c678eSDavid Greenman 
1242a17c678eSDavid Greenman 	ifp->if_opackets += sp->tx_good;
1243a17c678eSDavid Greenman 	ifp->if_collisions += sp->tx_total_collisions;
1244397f9dfeSDavid Greenman 	if (sp->rx_good) {
1245397f9dfeSDavid Greenman 		ifp->if_ipackets += sp->rx_good;
1246397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1247397f9dfeSDavid Greenman 	} else {
1248c8cc6fcaSDavid Greenman 		/*
1249c8cc6fcaSDavid Greenman 		 * Receiver's been idle for another second.
1250c8cc6fcaSDavid Greenman 		 */
1251397f9dfeSDavid Greenman 		sc->rx_idle_secs++;
1252397f9dfeSDavid Greenman 	}
12533ba65732SDavid Greenman 	ifp->if_ierrors +=
12543ba65732SDavid Greenman 	    sp->rx_crc_errors +
12553ba65732SDavid Greenman 	    sp->rx_alignment_errors +
12563ba65732SDavid Greenman 	    sp->rx_rnr_errors +
12576e39e599SDavid Greenman 	    sp->rx_overrun_errors;
1258a17c678eSDavid Greenman 	/*
1259f9be9005SDavid Greenman 	 * If any transmit underruns occured, bump up the transmit
1260f9be9005SDavid Greenman 	 * threshold by another 512 bytes (64 * 8).
1261f9be9005SDavid Greenman 	 */
1262f9be9005SDavid Greenman 	if (sp->tx_underruns) {
1263f9be9005SDavid Greenman 		ifp->if_oerrors += sp->tx_underruns;
1264f9be9005SDavid Greenman 		if (tx_threshold < 192)
1265f9be9005SDavid Greenman 			tx_threshold += 64;
1266f9be9005SDavid Greenman 	}
1267397f9dfeSDavid Greenman 	s = splimp();
1268397f9dfeSDavid Greenman 	/*
1269c8cc6fcaSDavid Greenman 	 * Release any xmit buffers that have completed DMA. This isn't
1270c8cc6fcaSDavid Greenman 	 * strictly necessary to do here, but it's advantagous for mbufs
1271c8cc6fcaSDavid Greenman 	 * with external storage to be released in a timely manner rather
1272c8cc6fcaSDavid Greenman 	 * than being defered for a potentially long time. This limits
1273c8cc6fcaSDavid Greenman 	 * the delay to a maximum of one second.
1274c8cc6fcaSDavid Greenman 	 */
1275c8cc6fcaSDavid Greenman 	for (txp = sc->cbl_first; sc->tx_queued &&
1276c8cc6fcaSDavid Greenman 	    (txp->cb_status & FXP_CB_STATUS_C) != 0;
1277c8cc6fcaSDavid Greenman 	    txp = txp->next) {
1278c8cc6fcaSDavid Greenman 		if (txp->mb_head != NULL) {
1279c8cc6fcaSDavid Greenman 			m_freem(txp->mb_head);
1280c8cc6fcaSDavid Greenman 			txp->mb_head = NULL;
1281c8cc6fcaSDavid Greenman 		}
1282c8cc6fcaSDavid Greenman 		sc->tx_queued--;
1283c8cc6fcaSDavid Greenman 	}
1284c8cc6fcaSDavid Greenman 	sc->cbl_first = txp;
1285c8cc6fcaSDavid Greenman 	/*
1286397f9dfeSDavid Greenman 	 * If we haven't received any packets in FXP_MAC_RX_IDLE seconds,
1287397f9dfeSDavid Greenman 	 * then assume the receiver has locked up and attempt to clear
1288397f9dfeSDavid Greenman 	 * the condition by reprogramming the multicast filter. This is
1289397f9dfeSDavid Greenman 	 * a work-around for a bug in the 82557 where the receiver locks
1290397f9dfeSDavid Greenman 	 * up if it gets certain types of garbage in the syncronization
1291397f9dfeSDavid Greenman 	 * bits prior to the packet header. This bug is supposed to only
1292397f9dfeSDavid Greenman 	 * occur in 10Mbps mode, but has been seen to occur in 100Mbps
1293397f9dfeSDavid Greenman 	 * mode as well (perhaps due to a 10/100 speed transition).
1294397f9dfeSDavid Greenman 	 */
1295397f9dfeSDavid Greenman 	if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
1296397f9dfeSDavid Greenman 		sc->rx_idle_secs = 0;
1297397f9dfeSDavid Greenman 		fxp_mc_setup(sc);
1298397f9dfeSDavid Greenman 	}
1299f9be9005SDavid Greenman 	/*
13003ba65732SDavid Greenman 	 * If there is no pending command, start another stats
13013ba65732SDavid Greenman 	 * dump. Otherwise punt for now.
1302a17c678eSDavid Greenman 	 */
1303397f9dfeSDavid Greenman 	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1304a17c678eSDavid Greenman 		/*
1305397f9dfeSDavid Greenman 		 * Start another stats dump.
1306a17c678eSDavid Greenman 		 */
1307ba8c6fd5SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND,
1308ba8c6fd5SDavid Greenman 		    FXP_SCB_COMMAND_CU_DUMPRESET);
1309dfe61cf1SDavid Greenman 	} else {
1310dfe61cf1SDavid Greenman 		/*
1311dfe61cf1SDavid Greenman 		 * A previous command is still waiting to be accepted.
1312dfe61cf1SDavid Greenman 		 * Just zero our copy of the stats and wait for the
13133ba65732SDavid Greenman 		 * next timer event to update them.
1314dfe61cf1SDavid Greenman 		 */
1315dfe61cf1SDavid Greenman 		sp->tx_good = 0;
1316f9be9005SDavid Greenman 		sp->tx_underruns = 0;
1317dfe61cf1SDavid Greenman 		sp->tx_total_collisions = 0;
13183ba65732SDavid Greenman 
1319dfe61cf1SDavid Greenman 		sp->rx_good = 0;
13203ba65732SDavid Greenman 		sp->rx_crc_errors = 0;
13213ba65732SDavid Greenman 		sp->rx_alignment_errors = 0;
13223ba65732SDavid Greenman 		sp->rx_rnr_errors = 0;
13233ba65732SDavid Greenman 		sp->rx_overrun_errors = 0;
1324dfe61cf1SDavid Greenman 	}
1325397f9dfeSDavid Greenman 	splx(s);
1326a17c678eSDavid Greenman 	/*
1327a17c678eSDavid Greenman 	 * Schedule another timeout one second from now.
1328a17c678eSDavid Greenman 	 */
1329397f9dfeSDavid Greenman 	sc->stat_ch = timeout(fxp_stats_update, sc, hz);
1330a17c678eSDavid Greenman }
1331a17c678eSDavid Greenman 
1332a17c678eSDavid Greenman /*
1333a17c678eSDavid Greenman  * Stop the interface. Cancels the statistics updater and resets
1334a17c678eSDavid Greenman  * the interface.
1335a17c678eSDavid Greenman  */
1336a17c678eSDavid Greenman static void
13374a5f1499SDavid Greenman fxp_stop(sc)
13384a5f1499SDavid Greenman 	struct fxp_softc *sc;
1339a17c678eSDavid Greenman {
1340ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
13413ba65732SDavid Greenman 	struct fxp_cb_tx *txp;
13423ba65732SDavid Greenman 	int i;
1343a17c678eSDavid Greenman 
1344a17c678eSDavid Greenman 	/*
1345a17c678eSDavid Greenman 	 * Cancel stats updater.
1346a17c678eSDavid Greenman 	 */
13476c951b44SJustin T. Gibbs 	untimeout(fxp_stats_update, sc, sc->stat_ch);
13483ba65732SDavid Greenman 
13493ba65732SDavid Greenman 	/*
13503ba65732SDavid Greenman 	 * Issue software reset
13513ba65732SDavid Greenman 	 */
1352ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
1353a17c678eSDavid Greenman 	DELAY(10);
1354a17c678eSDavid Greenman 
13553ba65732SDavid Greenman 	/*
13563ba65732SDavid Greenman 	 * Release any xmit buffers.
13573ba65732SDavid Greenman 	 */
1358da91462dSDavid Greenman 	txp = sc->cbl_base;
1359da91462dSDavid Greenman 	if (txp != NULL) {
1360da91462dSDavid Greenman 		for (i = 0; i < FXP_NTXCB; i++) {
1361da91462dSDavid Greenman 			if (txp[i].mb_head != NULL) {
1362da91462dSDavid Greenman 				m_freem(txp[i].mb_head);
1363da91462dSDavid Greenman 				txp[i].mb_head = NULL;
1364da91462dSDavid Greenman 			}
1365da91462dSDavid Greenman 		}
13663ba65732SDavid Greenman 	}
13673ba65732SDavid Greenman 	sc->tx_queued = 0;
13683ba65732SDavid Greenman 
13693ba65732SDavid Greenman 	/*
13703ba65732SDavid Greenman 	 * Free all the receive buffers then reallocate/reinitialize
13713ba65732SDavid Greenman 	 */
13723ba65732SDavid Greenman 	if (sc->rfa_headm != NULL)
13733ba65732SDavid Greenman 		m_freem(sc->rfa_headm);
13743ba65732SDavid Greenman 	sc->rfa_headm = NULL;
13753ba65732SDavid Greenman 	sc->rfa_tailm = NULL;
13763ba65732SDavid Greenman 	for (i = 0; i < FXP_NRFABUFS; i++) {
13773ba65732SDavid Greenman 		if (fxp_add_rfabuf(sc, NULL) != 0) {
13783ba65732SDavid Greenman 			/*
13793ba65732SDavid Greenman 			 * This "can't happen" - we're at splimp()
13803ba65732SDavid Greenman 			 * and we just freed all the buffers we need
13813ba65732SDavid Greenman 			 * above.
13823ba65732SDavid Greenman 			 */
13833ba65732SDavid Greenman 			panic("fxp_stop: no buffers!");
13843ba65732SDavid Greenman 		}
13853ba65732SDavid Greenman 	}
13863ba65732SDavid Greenman 
13873ba65732SDavid Greenman 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
13883ba65732SDavid Greenman 	ifp->if_timer = 0;
1389a17c678eSDavid Greenman }
1390a17c678eSDavid Greenman 
1391a17c678eSDavid Greenman /*
1392a17c678eSDavid Greenman  * Watchdog/transmission transmit timeout handler. Called when a
1393a17c678eSDavid Greenman  * transmission is started on the interface, but no interrupt is
1394a17c678eSDavid Greenman  * received before the timeout. This usually indicates that the
1395a17c678eSDavid Greenman  * card has wedged for some reason.
1396a17c678eSDavid Greenman  */
1397a17c678eSDavid Greenman static void
13984a5f1499SDavid Greenman fxp_watchdog(ifp)
13994a5f1499SDavid Greenman 	struct ifnet *ifp;
1400a17c678eSDavid Greenman {
1401ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1402ba8c6fd5SDavid Greenman 
1403397f9dfeSDavid Greenman 	printf(FXP_FORMAT ": device timeout\n", FXP_ARGS(sc));
14044a5f1499SDavid Greenman 	ifp->if_oerrors++;
1405a17c678eSDavid Greenman 
1406ba8c6fd5SDavid Greenman 	fxp_init(sc);
1407a17c678eSDavid Greenman }
1408a17c678eSDavid Greenman 
1409a17c678eSDavid Greenman static void
1410fb583156SDavid Greenman fxp_init(xsc)
1411fb583156SDavid Greenman 	void *xsc;
1412a17c678eSDavid Greenman {
1413fb583156SDavid Greenman 	struct fxp_softc *sc = xsc;
1414ba8c6fd5SDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1415a17c678eSDavid Greenman 	struct fxp_cb_config *cbp;
1416a17c678eSDavid Greenman 	struct fxp_cb_ias *cb_ias;
1417a17c678eSDavid Greenman 	struct fxp_cb_tx *txp;
1418397f9dfeSDavid Greenman 	int i, s, prm;
1419a17c678eSDavid Greenman 
1420a17c678eSDavid Greenman 	s = splimp();
1421a17c678eSDavid Greenman 	/*
14223ba65732SDavid Greenman 	 * Cancel any pending I/O
1423a17c678eSDavid Greenman 	 */
14243ba65732SDavid Greenman 	fxp_stop(sc);
1425a17c678eSDavid Greenman 
1426a17c678eSDavid Greenman 	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1427a17c678eSDavid Greenman 
1428a17c678eSDavid Greenman 	/*
1429a17c678eSDavid Greenman 	 * Initialize base of CBL and RFA memory. Loading with zero
1430a17c678eSDavid Greenman 	 * sets it up for regular linear addressing.
1431a17c678eSDavid Greenman 	 */
1432ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1433ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_BASE);
1434a17c678eSDavid Greenman 
1435ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1436ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_BASE);
1437a17c678eSDavid Greenman 
1438a17c678eSDavid Greenman 	/*
1439a17c678eSDavid Greenman 	 * Initialize base of dump-stats buffer.
1440a17c678eSDavid Greenman 	 */
1441ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1442ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
1443ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_DUMP_ADR);
1444a17c678eSDavid Greenman 
1445a17c678eSDavid Greenman 	/*
1446a17c678eSDavid Greenman 	 * We temporarily use memory that contains the TxCB list to
1447a17c678eSDavid Greenman 	 * construct the config CB. The TxCB list memory is rebuilt
1448a17c678eSDavid Greenman 	 * later.
1449a17c678eSDavid Greenman 	 */
1450a17c678eSDavid Greenman 	cbp = (struct fxp_cb_config *) sc->cbl_base;
1451a17c678eSDavid Greenman 
1452a17c678eSDavid Greenman 	/*
1453a17c678eSDavid Greenman 	 * This bcopy is kind of disgusting, but there are a bunch of must be
1454a17c678eSDavid Greenman 	 * zero and must be one bits in this structure and this is the easiest
1455a17c678eSDavid Greenman 	 * way to initialize them all to proper values.
1456a17c678eSDavid Greenman 	 */
1457d244b0e9SPeter Wemm 	bcopy(fxp_cb_config_template,
1458d244b0e9SPeter Wemm 		(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
1459397f9dfeSDavid Greenman 		sizeof(fxp_cb_config_template));
1460a17c678eSDavid Greenman 
1461a17c678eSDavid Greenman 	cbp->cb_status =	0;
1462a17c678eSDavid Greenman 	cbp->cb_command =	FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
1463a17c678eSDavid Greenman 	cbp->link_addr =	-1;	/* (no) next command */
1464a17c678eSDavid Greenman 	cbp->byte_count =	22;	/* (22) bytes to config */
1465001696daSDavid Greenman 	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1466001696daSDavid Greenman 	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1467a17c678eSDavid Greenman 	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1468001696daSDavid Greenman 	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1469001696daSDavid Greenman 	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1470001696daSDavid Greenman 	cbp->dma_bce =		0;	/* (disable) dma max counters */
1471a17c678eSDavid Greenman 	cbp->late_scb =		0;	/* (don't) defer SCB update */
1472a17c678eSDavid Greenman 	cbp->tno_int =		0;	/* (disable) tx not okay interrupt */
14733114fdb4SDavid Greenman 	cbp->ci_int =		1;	/* interrupt on CU idle */
1474a17c678eSDavid Greenman 	cbp->save_bf =		prm;	/* save bad frames */
1475a17c678eSDavid Greenman 	cbp->disc_short_rx =	!prm;	/* discard short packets */
1476a17c678eSDavid Greenman 	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
1477dccee1a1SDavid Greenman 	cbp->mediatype =	!sc->phy_10Mbps_only; /* interface mode */
1478a17c678eSDavid Greenman 	cbp->nsai =		1;	/* (don't) disable source addr insert */
1479a17c678eSDavid Greenman 	cbp->preamble_length =	2;	/* (7 byte) preamble */
1480a17c678eSDavid Greenman 	cbp->loopback =		0;	/* (don't) loopback */
1481a17c678eSDavid Greenman 	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1482a17c678eSDavid Greenman 	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1483a17c678eSDavid Greenman 	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1484a17c678eSDavid Greenman 	cbp->promiscuous =	prm;	/* promiscuous mode */
1485a17c678eSDavid Greenman 	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1486001696daSDavid Greenman 	cbp->crscdt =		0;	/* (CRS only) */
1487a17c678eSDavid Greenman 	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1488a17c678eSDavid Greenman 	cbp->padding =		1;	/* (do) pad short tx packets */
1489a17c678eSDavid Greenman 	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1490a17c678eSDavid Greenman 	cbp->force_fdx =	0;	/* (don't) force full duplex */
14913ba65732SDavid Greenman 	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1492a17c678eSDavid Greenman 	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1493397f9dfeSDavid Greenman 	cbp->mc_all =		sc->all_mcasts;/* accept all multicasts */
1494a17c678eSDavid Greenman 
1495a17c678eSDavid Greenman 	/*
1496a17c678eSDavid Greenman 	 * Start the config command/DMA.
1497a17c678eSDavid Greenman 	 */
1498ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1499397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
1500ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1501a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
1502a17c678eSDavid Greenman 	while (!(cbp->cb_status & FXP_CB_STATUS_C));
1503a17c678eSDavid Greenman 
1504a17c678eSDavid Greenman 	/*
1505a17c678eSDavid Greenman 	 * Now initialize the station address. Temporarily use the TxCB
1506a17c678eSDavid Greenman 	 * memory area like we did above for the config CB.
1507a17c678eSDavid Greenman 	 */
1508a17c678eSDavid Greenman 	cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
1509a17c678eSDavid Greenman 	cb_ias->cb_status = 0;
1510a17c678eSDavid Greenman 	cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
1511a17c678eSDavid Greenman 	cb_ias->link_addr = -1;
1512ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1513ba8c6fd5SDavid Greenman 	bcopy(LLADDR(ifp->if_sadl), (void *)cb_ias->macaddr, 6);
1514ba8c6fd5SDavid Greenman #else
1515d244b0e9SPeter Wemm 	bcopy(sc->arpcom.ac_enaddr,
1516d244b0e9SPeter Wemm 	    (void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
1517a17c678eSDavid Greenman 	    sizeof(sc->arpcom.ac_enaddr));
1518ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
1519a17c678eSDavid Greenman 
1520a17c678eSDavid Greenman 	/*
1521a17c678eSDavid Greenman 	 * Start the IAS (Individual Address Setup) command/DMA.
1522a17c678eSDavid Greenman 	 */
1523ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1524ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1525a17c678eSDavid Greenman 	/* ...and wait for it to complete. */
1526a17c678eSDavid Greenman 	while (!(cb_ias->cb_status & FXP_CB_STATUS_C));
1527a17c678eSDavid Greenman 
1528a17c678eSDavid Greenman 	/*
1529a17c678eSDavid Greenman 	 * Initialize transmit control block (TxCB) list.
1530a17c678eSDavid Greenman 	 */
1531a17c678eSDavid Greenman 
1532a17c678eSDavid Greenman 	txp = sc->cbl_base;
1533a17c678eSDavid Greenman 	bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
1534a17c678eSDavid Greenman 	for (i = 0; i < FXP_NTXCB; i++) {
1535a17c678eSDavid Greenman 		txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
1536a17c678eSDavid Greenman 		txp[i].cb_command = FXP_CB_COMMAND_NOP;
1537397f9dfeSDavid Greenman 		txp[i].link_addr = vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
1538a17c678eSDavid Greenman 		txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
1539a17c678eSDavid Greenman 		txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
1540a17c678eSDavid Greenman 	}
1541a17c678eSDavid Greenman 	/*
1542397f9dfeSDavid Greenman 	 * Set the suspend flag on the first TxCB and start the control
1543a17c678eSDavid Greenman 	 * unit. It will execute the NOP and then suspend.
1544a17c678eSDavid Greenman 	 */
1545a17c678eSDavid Greenman 	txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
1546a17c678eSDavid Greenman 	sc->cbl_first = sc->cbl_last = txp;
1547397f9dfeSDavid Greenman 	sc->tx_queued = 1;
1548a17c678eSDavid Greenman 
1549ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1550ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
1551a17c678eSDavid Greenman 
1552a17c678eSDavid Greenman 	/*
1553a17c678eSDavid Greenman 	 * Initialize receiver buffer area - RFA.
1554a17c678eSDavid Greenman 	 */
1555ba8c6fd5SDavid Greenman 	fxp_scb_wait(sc);
1556ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1557ba8c6fd5SDavid Greenman 	    vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
1558ba8c6fd5SDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_RU_START);
1559a17c678eSDavid Greenman 
1560dccee1a1SDavid Greenman 	/*
1561ba8c6fd5SDavid Greenman 	 * Set current media.
1562dccee1a1SDavid Greenman 	 */
1563ba8c6fd5SDavid Greenman 	fxp_set_media(sc, sc->sc_media.ifm_cur->ifm_media);
1564dccee1a1SDavid Greenman 
1565a17c678eSDavid Greenman 	ifp->if_flags |= IFF_RUNNING;
1566a17c678eSDavid Greenman 	ifp->if_flags &= ~IFF_OACTIVE;
1567a17c678eSDavid Greenman 	splx(s);
1568a17c678eSDavid Greenman 
1569a17c678eSDavid Greenman 	/*
1570a17c678eSDavid Greenman 	 * Start stats updater.
1571a17c678eSDavid Greenman 	 */
15726c951b44SJustin T. Gibbs 	sc->stat_ch = timeout(fxp_stats_update, sc, hz);
1573a17c678eSDavid Greenman }
1574a17c678eSDavid Greenman 
1575303b270bSEivind Eklund static void
1576ba8c6fd5SDavid Greenman fxp_set_media(sc, media)
1577ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1578ba8c6fd5SDavid Greenman 	int media;
1579ba8c6fd5SDavid Greenman {
1580ba8c6fd5SDavid Greenman 
1581ba8c6fd5SDavid Greenman 	switch (sc->phy_primary_device) {
1582ba8c6fd5SDavid Greenman 	case FXP_PHY_DP83840:
1583ba8c6fd5SDavid Greenman 	case FXP_PHY_DP83840A:
1584ba8c6fd5SDavid Greenman 		fxp_mdi_write(sc, sc->phy_primary_addr, FXP_DP83840_PCR,
1585ba8c6fd5SDavid Greenman 		    fxp_mdi_read(sc, sc->phy_primary_addr, FXP_DP83840_PCR) |
1586ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_LED4_MODE |	/* LED4 always indicates duplex */
1587ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_F_CONNECT |	/* force link disconnect bypass */
1588ba8c6fd5SDavid Greenman 		    FXP_DP83840_PCR_BIT10);	/* XXX I have no idea */
1589ba8c6fd5SDavid Greenman 		/* fall through */
159092924291SDavid Greenman 	case FXP_PHY_82553A:
159192924291SDavid Greenman 	case FXP_PHY_82553C: /* untested */
1592ba8c6fd5SDavid Greenman 	case FXP_PHY_82555:
159392924291SDavid Greenman 	case FXP_PHY_82555B:
1594ba8c6fd5SDavid Greenman 		if (IFM_SUBTYPE(media) != IFM_AUTO) {
1595ba8c6fd5SDavid Greenman 			int flags;
1596ba8c6fd5SDavid Greenman 
1597ba8c6fd5SDavid Greenman 			flags = (IFM_SUBTYPE(media) == IFM_100_TX) ?
1598ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR_SPEED_100M : 0;
1599ba8c6fd5SDavid Greenman 			flags |= (media & IFM_FDX) ?
1600ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR_FULLDUPLEX : 0;
1601ba8c6fd5SDavid Greenman 			fxp_mdi_write(sc, sc->phy_primary_addr,
1602ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR,
1603ba8c6fd5SDavid Greenman 			    (fxp_mdi_read(sc, sc->phy_primary_addr,
1604ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR) &
1605ba8c6fd5SDavid Greenman 			    ~(FXP_PHY_BMCR_AUTOEN | FXP_PHY_BMCR_SPEED_100M |
1606ba8c6fd5SDavid Greenman 			     FXP_PHY_BMCR_FULLDUPLEX)) | flags);
1607ba8c6fd5SDavid Greenman 		} else {
1608ba8c6fd5SDavid Greenman 			fxp_mdi_write(sc, sc->phy_primary_addr,
1609ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR,
1610ba8c6fd5SDavid Greenman 			    (fxp_mdi_read(sc, sc->phy_primary_addr,
1611ba8c6fd5SDavid Greenman 			    FXP_PHY_BMCR) | FXP_PHY_BMCR_AUTOEN));
1612ba8c6fd5SDavid Greenman 		}
1613ba8c6fd5SDavid Greenman 		break;
1614ba8c6fd5SDavid Greenman 	/*
1615ba8c6fd5SDavid Greenman 	 * The Seeq 80c24 doesn't have a PHY programming interface, so do
1616ba8c6fd5SDavid Greenman 	 * nothing.
1617ba8c6fd5SDavid Greenman 	 */
1618ba8c6fd5SDavid Greenman 	case FXP_PHY_80C24:
1619ba8c6fd5SDavid Greenman 		break;
1620ba8c6fd5SDavid Greenman 	default:
1621ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT
1622ba8c6fd5SDavid Greenman 		    ": warning: unsupported PHY, type = %d, addr = %d\n",
1623ba8c6fd5SDavid Greenman 		     FXP_ARGS(sc), sc->phy_primary_device,
1624ba8c6fd5SDavid Greenman 		     sc->phy_primary_addr);
1625ba8c6fd5SDavid Greenman 	}
1626ba8c6fd5SDavid Greenman }
1627ba8c6fd5SDavid Greenman 
1628ba8c6fd5SDavid Greenman /*
1629ba8c6fd5SDavid Greenman  * Change media according to request.
1630ba8c6fd5SDavid Greenman  */
1631ba8c6fd5SDavid Greenman int
1632ba8c6fd5SDavid Greenman fxp_mediachange(ifp)
1633ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
1634ba8c6fd5SDavid Greenman {
1635ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1636ba8c6fd5SDavid Greenman 	struct ifmedia *ifm = &sc->sc_media;
1637ba8c6fd5SDavid Greenman 
1638ba8c6fd5SDavid Greenman 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1639ba8c6fd5SDavid Greenman 		return (EINVAL);
1640ba8c6fd5SDavid Greenman 
1641ba8c6fd5SDavid Greenman 	fxp_set_media(sc, ifm->ifm_media);
1642ba8c6fd5SDavid Greenman 	return (0);
1643ba8c6fd5SDavid Greenman }
1644ba8c6fd5SDavid Greenman 
1645ba8c6fd5SDavid Greenman /*
1646ba8c6fd5SDavid Greenman  * Notify the world which media we're using.
1647ba8c6fd5SDavid Greenman  */
1648ba8c6fd5SDavid Greenman void
1649ba8c6fd5SDavid Greenman fxp_mediastatus(ifp, ifmr)
1650ba8c6fd5SDavid Greenman 	struct ifnet *ifp;
1651ba8c6fd5SDavid Greenman 	struct ifmediareq *ifmr;
1652ba8c6fd5SDavid Greenman {
1653ba8c6fd5SDavid Greenman 	struct fxp_softc *sc = ifp->if_softc;
1654a7280784SJulian Elischer 	int flags, stsflags;
1655ba8c6fd5SDavid Greenman 
1656ba8c6fd5SDavid Greenman 	switch (sc->phy_primary_device) {
1657ba8c6fd5SDavid Greenman 	case FXP_PHY_82555:
165835517ab7SDavid Greenman 	case FXP_PHY_82555B:
1659a7280784SJulian Elischer 	case FXP_PHY_DP83840:
1660a7280784SJulian Elischer 	case FXP_PHY_DP83840A:
1661a7280784SJulian Elischer 		ifmr->ifm_status = IFM_AVALID; /* IFM_ACTIVE will be valid */
1662ba8c6fd5SDavid Greenman 		ifmr->ifm_active = IFM_ETHER;
1663a7280784SJulian Elischer 		/*
1664a7280784SJulian Elischer 		 * the following is not an error.
1665a7280784SJulian Elischer 		 * You need to read this register twice to get current
1666a7280784SJulian Elischer 		 * status. This is correct documented behaviour, the
1667a7280784SJulian Elischer 		 * first read gets latched values.
1668a7280784SJulian Elischer 		 */
1669a7280784SJulian Elischer 		stsflags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_STS);
1670a7280784SJulian Elischer 		stsflags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_STS);
1671a7280784SJulian Elischer 		if (stsflags & FXP_PHY_STS_LINK_STS)
1672a7280784SJulian Elischer 				ifmr->ifm_status |= IFM_ACTIVE;
1673a7280784SJulian Elischer 
1674a7280784SJulian Elischer 		/*
1675a7280784SJulian Elischer 		 * If we are in auto mode, then try report the result.
1676a7280784SJulian Elischer 		 */
1677a7280784SJulian Elischer 		flags = fxp_mdi_read(sc, sc->phy_primary_addr, FXP_PHY_BMCR);
1678f1bf08c2SJulian Elischer 		if (flags & FXP_PHY_BMCR_AUTOEN) {
1679f1bf08c2SJulian Elischer 			ifmr->ifm_active |= IFM_AUTO; /* XXX presently 0 */
1680a7280784SJulian Elischer 			if (stsflags & FXP_PHY_STS_AUTO_DONE) {
1681f1bf08c2SJulian Elischer 				/*
1682a7280784SJulian Elischer 				 * Intel and National parts report
1683a7280784SJulian Elischer 				 * differently on what they found.
1684f1bf08c2SJulian Elischer 				 */
1685f1bf08c2SJulian Elischer 				if ((sc->phy_primary_device == FXP_PHY_82555)
1686f1bf08c2SJulian Elischer 				|| (sc->phy_primary_device == FXP_PHY_82555B)) {
1687f1bf08c2SJulian Elischer 					flags = fxp_mdi_read(sc,
1688a7280784SJulian Elischer 						sc->phy_primary_addr,
1689a7280784SJulian Elischer 						FXP_PHY_USC);
1690f1bf08c2SJulian Elischer 
1691f1bf08c2SJulian Elischer 					if (flags & FXP_PHY_USC_SPEED)
1692f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_100_TX;
1693f1bf08c2SJulian Elischer 					else
1694f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_10_T;
1695f1bf08c2SJulian Elischer 
1696f1bf08c2SJulian Elischer 					if (flags & FXP_PHY_USC_DUPLEX)
1697f1bf08c2SJulian Elischer 						ifmr->ifm_active |= IFM_FDX;
1698a7280784SJulian Elischer 				} else { /* it's National. only know speed  */
1699a7280784SJulian Elischer 					flags = fxp_mdi_read(sc,
1700a7280784SJulian Elischer 						sc->phy_primary_addr,
1701a7280784SJulian Elischer 						FXP_DP83840_PAR);
1702a7280784SJulian Elischer 
1703a7280784SJulian Elischer 					if (flags & FXP_DP83840_PAR_SPEED_10)
1704a7280784SJulian Elischer 						ifmr->ifm_active |= IFM_10_T;
1705a7280784SJulian Elischer 					else
1706a7280784SJulian Elischer 						ifmr->ifm_active |= IFM_100_TX;
1707f1bf08c2SJulian Elischer 				}
1708a7280784SJulian Elischer 			}
1709a7280784SJulian Elischer 		} else { /* in manual mode.. just report what we were set to */
1710ba8c6fd5SDavid Greenman 			if (flags & FXP_PHY_BMCR_SPEED_100M)
1711ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_100_TX;
1712ba8c6fd5SDavid Greenman 			else
1713ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_10_T;
1714ba8c6fd5SDavid Greenman 
1715ba8c6fd5SDavid Greenman 			if (flags & FXP_PHY_BMCR_FULLDUPLEX)
1716ba8c6fd5SDavid Greenman 				ifmr->ifm_active |= IFM_FDX;
1717ba8c6fd5SDavid Greenman 		}
1718ba8c6fd5SDavid Greenman 		break;
1719ba8c6fd5SDavid Greenman 
1720ba8c6fd5SDavid Greenman 	case FXP_PHY_80C24:
1721ba8c6fd5SDavid Greenman 	default:
1722ba8c6fd5SDavid Greenman 		ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; /* XXX IFM_AUTO ? */
1723ba8c6fd5SDavid Greenman 	}
1724ba8c6fd5SDavid Greenman }
1725ba8c6fd5SDavid Greenman 
1726a17c678eSDavid Greenman /*
1727a17c678eSDavid Greenman  * Add a buffer to the end of the RFA buffer list.
1728a17c678eSDavid Greenman  * Return 0 if successful, 1 for failure. A failure results in
1729a17c678eSDavid Greenman  * adding the 'oldm' (if non-NULL) on to the end of the list -
1730dc733423SDag-Erling Smørgrav  * tossing out its old contents and recycling it.
1731a17c678eSDavid Greenman  * The RFA struct is stuck at the beginning of mbuf cluster and the
1732a17c678eSDavid Greenman  * data pointer is fixed up to point just past it.
1733a17c678eSDavid Greenman  */
1734a17c678eSDavid Greenman static int
1735a17c678eSDavid Greenman fxp_add_rfabuf(sc, oldm)
1736a17c678eSDavid Greenman 	struct fxp_softc *sc;
1737a17c678eSDavid Greenman 	struct mbuf *oldm;
1738a17c678eSDavid Greenman {
1739ba8c6fd5SDavid Greenman 	u_int32_t v;
1740a17c678eSDavid Greenman 	struct mbuf *m;
1741a17c678eSDavid Greenman 	struct fxp_rfa *rfa, *p_rfa;
1742a17c678eSDavid Greenman 
1743a17c678eSDavid Greenman 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1744a17c678eSDavid Greenman 	if (m != NULL) {
1745a17c678eSDavid Greenman 		MCLGET(m, M_DONTWAIT);
1746a17c678eSDavid Greenman 		if ((m->m_flags & M_EXT) == 0) {
1747a17c678eSDavid Greenman 			m_freem(m);
1748eadd5e3aSDavid Greenman 			if (oldm == NULL)
1749eadd5e3aSDavid Greenman 				return 1;
1750a17c678eSDavid Greenman 			m = oldm;
1751eadd5e3aSDavid Greenman 			m->m_data = m->m_ext.ext_buf;
1752a17c678eSDavid Greenman 		}
1753a17c678eSDavid Greenman 	} else {
1754eadd5e3aSDavid Greenman 		if (oldm == NULL)
1755a17c678eSDavid Greenman 			return 1;
1756eadd5e3aSDavid Greenman 		m = oldm;
1757eadd5e3aSDavid Greenman 		m->m_data = m->m_ext.ext_buf;
1758eadd5e3aSDavid Greenman 	}
1759ba8c6fd5SDavid Greenman 
1760ba8c6fd5SDavid Greenman 	/*
1761ba8c6fd5SDavid Greenman 	 * Move the data pointer up so that the incoming data packet
1762ba8c6fd5SDavid Greenman 	 * will be 32-bit aligned.
1763ba8c6fd5SDavid Greenman 	 */
1764ba8c6fd5SDavid Greenman 	m->m_data += RFA_ALIGNMENT_FUDGE;
1765ba8c6fd5SDavid Greenman 
1766eadd5e3aSDavid Greenman 	/*
1767eadd5e3aSDavid Greenman 	 * Get a pointer to the base of the mbuf cluster and move
1768eadd5e3aSDavid Greenman 	 * data start past it.
1769eadd5e3aSDavid Greenman 	 */
1770a17c678eSDavid Greenman 	rfa = mtod(m, struct fxp_rfa *);
1771eadd5e3aSDavid Greenman 	m->m_data += sizeof(struct fxp_rfa);
17724fc1dda9SAndrew Gallatin 	rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) - RFA_ALIGNMENT_FUDGE);
1773eadd5e3aSDavid Greenman 
1774ba8c6fd5SDavid Greenman 	/*
1775ba8c6fd5SDavid Greenman 	 * Initialize the rest of the RFA.  Note that since the RFA
1776ba8c6fd5SDavid Greenman 	 * is misaligned, we cannot store values directly.  Instead,
1777ba8c6fd5SDavid Greenman 	 * we use an optimized, inline copy.
1778ba8c6fd5SDavid Greenman 	 */
17794fc1dda9SAndrew Gallatin 
1780a17c678eSDavid Greenman 	rfa->rfa_status = 0;
1781a17c678eSDavid Greenman 	rfa->rfa_control = FXP_RFA_CONTROL_EL;
1782a17c678eSDavid Greenman 	rfa->actual_size = 0;
1783ba8c6fd5SDavid Greenman 
1784ba8c6fd5SDavid Greenman 	v = -1;
17854fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
17864fc1dda9SAndrew Gallatin 	fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
1787ba8c6fd5SDavid Greenman 
1788dfe61cf1SDavid Greenman 	/*
1789dfe61cf1SDavid Greenman 	 * If there are other buffers already on the list, attach this
1790dfe61cf1SDavid Greenman 	 * one to the end by fixing up the tail to point to this one.
1791dfe61cf1SDavid Greenman 	 */
1792a17c678eSDavid Greenman 	if (sc->rfa_headm != NULL) {
1793ba8c6fd5SDavid Greenman 		p_rfa = (struct fxp_rfa *) (sc->rfa_tailm->m_ext.ext_buf +
1794ba8c6fd5SDavid Greenman 		    RFA_ALIGNMENT_FUDGE);
1795a17c678eSDavid Greenman 		sc->rfa_tailm->m_next = m;
1796ba8c6fd5SDavid Greenman 		v = vtophys(rfa);
17974fc1dda9SAndrew Gallatin 		fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
1798aed53495SDavid Greenman 		p_rfa->rfa_control = 0;
1799a17c678eSDavid Greenman 	} else {
1800a17c678eSDavid Greenman 		sc->rfa_headm = m;
1801a17c678eSDavid Greenman 	}
1802a17c678eSDavid Greenman 	sc->rfa_tailm = m;
1803a17c678eSDavid Greenman 
1804dfe61cf1SDavid Greenman 	return (m == oldm);
1805a17c678eSDavid Greenman }
1806a17c678eSDavid Greenman 
18076ebc3153SDavid Greenman static volatile int
1808ba8c6fd5SDavid Greenman fxp_mdi_read(sc, phy, reg)
1809ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1810dccee1a1SDavid Greenman 	int phy;
1811dccee1a1SDavid Greenman 	int reg;
1812dccee1a1SDavid Greenman {
1813dccee1a1SDavid Greenman 	int count = 10000;
18146ebc3153SDavid Greenman 	int value;
1815dccee1a1SDavid Greenman 
1816ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1817ba8c6fd5SDavid Greenman 	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
1818dccee1a1SDavid Greenman 
1819ba8c6fd5SDavid Greenman 	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
1820ba8c6fd5SDavid Greenman 	    && count--)
18216ebc3153SDavid Greenman 		DELAY(10);
1822dccee1a1SDavid Greenman 
1823dccee1a1SDavid Greenman 	if (count <= 0)
1824ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT ": fxp_mdi_read: timed out\n",
1825ba8c6fd5SDavid Greenman 		    FXP_ARGS(sc));
1826dccee1a1SDavid Greenman 
18276ebc3153SDavid Greenman 	return (value & 0xffff);
1828dccee1a1SDavid Greenman }
1829dccee1a1SDavid Greenman 
1830dccee1a1SDavid Greenman static void
1831ba8c6fd5SDavid Greenman fxp_mdi_write(sc, phy, reg, value)
1832ba8c6fd5SDavid Greenman 	struct fxp_softc *sc;
1833dccee1a1SDavid Greenman 	int phy;
1834dccee1a1SDavid Greenman 	int reg;
1835dccee1a1SDavid Greenman 	int value;
1836dccee1a1SDavid Greenman {
1837dccee1a1SDavid Greenman 	int count = 10000;
1838dccee1a1SDavid Greenman 
1839ba8c6fd5SDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
1840ba8c6fd5SDavid Greenman 	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
1841ba8c6fd5SDavid Greenman 	    (value & 0xffff));
1842dccee1a1SDavid Greenman 
1843ba8c6fd5SDavid Greenman 	while((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
1844ba8c6fd5SDavid Greenman 	    count--)
18456ebc3153SDavid Greenman 		DELAY(10);
1846dccee1a1SDavid Greenman 
1847dccee1a1SDavid Greenman 	if (count <= 0)
1848ba8c6fd5SDavid Greenman 		printf(FXP_FORMAT ": fxp_mdi_write: timed out\n",
1849ba8c6fd5SDavid Greenman 		    FXP_ARGS(sc));
1850dccee1a1SDavid Greenman }
1851dccee1a1SDavid Greenman 
1852dccee1a1SDavid Greenman static int
1853a17c678eSDavid Greenman fxp_ioctl(ifp, command, data)
1854a17c678eSDavid Greenman 	struct ifnet *ifp;
1855ba8c6fd5SDavid Greenman 	FXP_IOCTLCMD_TYPE command;
1856a17c678eSDavid Greenman 	caddr_t data;
1857a17c678eSDavid Greenman {
18589b44ff22SGarrett Wollman 	struct fxp_softc *sc = ifp->if_softc;
1859a17c678eSDavid Greenman 	struct ifreq *ifr = (struct ifreq *)data;
1860a17c678eSDavid Greenman 	int s, error = 0;
1861a17c678eSDavid Greenman 
1862a17c678eSDavid Greenman 	s = splimp();
1863a17c678eSDavid Greenman 
1864a17c678eSDavid Greenman 	switch (command) {
1865a17c678eSDavid Greenman 
1866a17c678eSDavid Greenman 	case SIOCSIFADDR:
1867ba8c6fd5SDavid Greenman #if !defined(__NetBSD__)
1868a17c678eSDavid Greenman 	case SIOCGIFADDR:
1869fb583156SDavid Greenman 	case SIOCSIFMTU:
1870ba8c6fd5SDavid Greenman #endif
1871fb583156SDavid Greenman 		error = ether_ioctl(ifp, command, data);
1872a17c678eSDavid Greenman 		break;
1873a17c678eSDavid Greenman 
1874a17c678eSDavid Greenman 	case SIOCSIFFLAGS:
1875397f9dfeSDavid Greenman 		sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1876a17c678eSDavid Greenman 
1877a17c678eSDavid Greenman 		/*
1878a17c678eSDavid Greenman 		 * If interface is marked up and not running, then start it.
1879a17c678eSDavid Greenman 		 * If it is marked down and running, stop it.
1880a17c678eSDavid Greenman 		 * XXX If it's up then re-initialize it. This is so flags
1881a17c678eSDavid Greenman 		 * such as IFF_PROMISC are handled.
1882a17c678eSDavid Greenman 		 */
1883a17c678eSDavid Greenman 		if (ifp->if_flags & IFF_UP) {
1884fb583156SDavid Greenman 			fxp_init(sc);
1885a17c678eSDavid Greenman 		} else {
1886a17c678eSDavid Greenman 			if (ifp->if_flags & IFF_RUNNING)
18874a5f1499SDavid Greenman 				fxp_stop(sc);
1888a17c678eSDavid Greenman 		}
1889a17c678eSDavid Greenman 		break;
1890a17c678eSDavid Greenman 
1891a17c678eSDavid Greenman 	case SIOCADDMULTI:
1892a17c678eSDavid Greenman 	case SIOCDELMULTI:
1893397f9dfeSDavid Greenman 		sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1894ba8c6fd5SDavid Greenman #if defined(__NetBSD__)
1895ba8c6fd5SDavid Greenman 		error = (command == SIOCADDMULTI) ?
1896ba8c6fd5SDavid Greenman 		    ether_addmulti(ifr, &sc->sc_ethercom) :
1897ba8c6fd5SDavid Greenman 		    ether_delmulti(ifr, &sc->sc_ethercom);
1898ba8c6fd5SDavid Greenman 
1899ba8c6fd5SDavid Greenman 		if (error == ENETRESET) {
1900ba8c6fd5SDavid Greenman 			/*
1901ba8c6fd5SDavid Greenman 			 * Multicast list has changed; set the hardware
1902ba8c6fd5SDavid Greenman 			 * filter accordingly.
1903ba8c6fd5SDavid Greenman 			 */
1904397f9dfeSDavid Greenman 			if (!sc->all_mcasts)
1905397f9dfeSDavid Greenman 				fxp_mc_setup(sc);
1906397f9dfeSDavid Greenman 			/*
1907397f9dfeSDavid Greenman 			 * fxp_mc_setup() can turn on all_mcasts if we run
1908397f9dfeSDavid Greenman 			 * out of space, so check it again rather than else {}.
1909397f9dfeSDavid Greenman 			 */
1910397f9dfeSDavid Greenman 			if (sc->all_mcasts)
1911ba8c6fd5SDavid Greenman 				fxp_init(sc);
1912ba8c6fd5SDavid Greenman 			error = 0;
1913ba8c6fd5SDavid Greenman 		}
1914ba8c6fd5SDavid Greenman #else /* __FreeBSD__ */
1915a17c678eSDavid Greenman 		/*
1916a17c678eSDavid Greenman 		 * Multicast list has changed; set the hardware filter
1917a17c678eSDavid Greenman 		 * accordingly.
1918a17c678eSDavid Greenman 		 */
1919397f9dfeSDavid Greenman 		if (!sc->all_mcasts)
1920397f9dfeSDavid Greenman 			fxp_mc_setup(sc);
1921397f9dfeSDavid Greenman 		/*
1922397f9dfeSDavid Greenman 		 * fxp_mc_setup() can turn on sc->all_mcasts, so check it
1923397f9dfeSDavid Greenman 		 * again rather than else {}.
1924397f9dfeSDavid Greenman 		 */
1925397f9dfeSDavid Greenman 		if (sc->all_mcasts)
1926fb583156SDavid Greenman 			fxp_init(sc);
1927a17c678eSDavid Greenman 		error = 0;
1928ba8c6fd5SDavid Greenman #endif /* __NetBSD__ */
1929ba8c6fd5SDavid Greenman 		break;
1930ba8c6fd5SDavid Greenman 
1931ba8c6fd5SDavid Greenman 	case SIOCSIFMEDIA:
1932ba8c6fd5SDavid Greenman 	case SIOCGIFMEDIA:
1933ba8c6fd5SDavid Greenman 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1934a17c678eSDavid Greenman 		break;
1935a17c678eSDavid Greenman 
1936a17c678eSDavid Greenman 	default:
1937a17c678eSDavid Greenman 		error = EINVAL;
1938a17c678eSDavid Greenman 	}
1939a17c678eSDavid Greenman 	(void) splx(s);
1940a17c678eSDavid Greenman 	return (error);
1941a17c678eSDavid Greenman }
1942397f9dfeSDavid Greenman 
1943397f9dfeSDavid Greenman /*
1944397f9dfeSDavid Greenman  * Program the multicast filter.
1945397f9dfeSDavid Greenman  *
1946397f9dfeSDavid Greenman  * We have an artificial restriction that the multicast setup command
1947397f9dfeSDavid Greenman  * must be the first command in the chain, so we take steps to ensure
19483114fdb4SDavid Greenman  * this. By requiring this, it allows us to keep up the performance of
1949397f9dfeSDavid Greenman  * the pre-initialized command ring (esp. link pointers) by not actually
1950dc733423SDag-Erling Smørgrav  * inserting the mcsetup command in the ring - i.e. its link pointer
1951397f9dfeSDavid Greenman  * points to the TxCB ring, but the mcsetup descriptor itself is not part
1952397f9dfeSDavid Greenman  * of it. We then can do 'CU_START' on the mcsetup descriptor and have it
1953397f9dfeSDavid Greenman  * lead into the regular TxCB ring when it completes.
1954397f9dfeSDavid Greenman  *
1955397f9dfeSDavid Greenman  * This function must be called at splimp.
1956397f9dfeSDavid Greenman  */
1957397f9dfeSDavid Greenman static void
1958397f9dfeSDavid Greenman fxp_mc_setup(sc)
1959397f9dfeSDavid Greenman 	struct fxp_softc *sc;
1960397f9dfeSDavid Greenman {
1961397f9dfeSDavid Greenman 	struct fxp_cb_mcs *mcsp = sc->mcsp;
1962397f9dfeSDavid Greenman 	struct ifnet *ifp = &sc->sc_if;
1963397f9dfeSDavid Greenman 	struct ifmultiaddr *ifma;
1964397f9dfeSDavid Greenman 	int nmcasts;
1965397f9dfeSDavid Greenman 
19663114fdb4SDavid Greenman 	/*
19673114fdb4SDavid Greenman 	 * If there are queued commands, we must wait until they are all
19683114fdb4SDavid Greenman 	 * completed. If we are already waiting, then add a NOP command
19693114fdb4SDavid Greenman 	 * with interrupt option so that we're notified when all commands
19703114fdb4SDavid Greenman 	 * have been completed - fxp_start() ensures that no additional
19713114fdb4SDavid Greenman 	 * TX commands will be added when need_mcsetup is true.
19723114fdb4SDavid Greenman 	 */
1973397f9dfeSDavid Greenman 	if (sc->tx_queued) {
19743114fdb4SDavid Greenman 		struct fxp_cb_tx *txp;
19753114fdb4SDavid Greenman 
19763114fdb4SDavid Greenman 		/*
19773114fdb4SDavid Greenman 		 * need_mcsetup will be true if we are already waiting for the
19783114fdb4SDavid Greenman 		 * NOP command to be completed (see below). In this case, bail.
19793114fdb4SDavid Greenman 		 */
19803114fdb4SDavid Greenman 		if (sc->need_mcsetup)
19813114fdb4SDavid Greenman 			return;
1982397f9dfeSDavid Greenman 		sc->need_mcsetup = 1;
19833114fdb4SDavid Greenman 
19843114fdb4SDavid Greenman 		/*
19853114fdb4SDavid Greenman 		 * Add a NOP command with interrupt so that we are notified when all
19863114fdb4SDavid Greenman 		 * TX commands have been processed.
19873114fdb4SDavid Greenman 		 */
19883114fdb4SDavid Greenman 		txp = sc->cbl_last->next;
19893114fdb4SDavid Greenman 		txp->mb_head = NULL;
19903114fdb4SDavid Greenman 		txp->cb_status = 0;
19913114fdb4SDavid Greenman 		txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
19923114fdb4SDavid Greenman 		/*
19933114fdb4SDavid Greenman 		 * Advance the end of list forward.
19943114fdb4SDavid Greenman 		 */
19953114fdb4SDavid Greenman 		sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
19963114fdb4SDavid Greenman 		sc->cbl_last = txp;
19973114fdb4SDavid Greenman 		sc->tx_queued++;
19983114fdb4SDavid Greenman 		/*
19993114fdb4SDavid Greenman 		 * Issue a resume in case the CU has just suspended.
20003114fdb4SDavid Greenman 		 */
20013114fdb4SDavid Greenman 		fxp_scb_wait(sc);
20023114fdb4SDavid Greenman 		CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_RESUME);
20033114fdb4SDavid Greenman 		/*
20043114fdb4SDavid Greenman 		 * Set a 5 second timer just in case we don't hear from the
20053114fdb4SDavid Greenman 		 * card again.
20063114fdb4SDavid Greenman 		 */
20073114fdb4SDavid Greenman 		ifp->if_timer = 5;
20083114fdb4SDavid Greenman 
2009397f9dfeSDavid Greenman 		return;
2010397f9dfeSDavid Greenman 	}
2011397f9dfeSDavid Greenman 	sc->need_mcsetup = 0;
2012397f9dfeSDavid Greenman 
2013397f9dfeSDavid Greenman 	/*
2014397f9dfeSDavid Greenman 	 * Initialize multicast setup descriptor.
2015397f9dfeSDavid Greenman 	 */
2016397f9dfeSDavid Greenman 	mcsp->next = sc->cbl_base;
2017397f9dfeSDavid Greenman 	mcsp->mb_head = NULL;
2018397f9dfeSDavid Greenman 	mcsp->cb_status = 0;
20193114fdb4SDavid Greenman 	mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
2020397f9dfeSDavid Greenman 	mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
2021397f9dfeSDavid Greenman 
2022397f9dfeSDavid Greenman 	nmcasts = 0;
2023397f9dfeSDavid Greenman 	if (!sc->all_mcasts) {
2024397f9dfeSDavid Greenman 		for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
2025397f9dfeSDavid Greenman 		    ifma = ifma->ifma_link.le_next) {
2026397f9dfeSDavid Greenman 			if (ifma->ifma_addr->sa_family != AF_LINK)
2027397f9dfeSDavid Greenman 				continue;
2028397f9dfeSDavid Greenman 			if (nmcasts >= MAXMCADDR) {
2029397f9dfeSDavid Greenman 				sc->all_mcasts = 1;
2030397f9dfeSDavid Greenman 				nmcasts = 0;
2031397f9dfeSDavid Greenman 				break;
2032397f9dfeSDavid Greenman 			}
2033397f9dfeSDavid Greenman 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2034d244b0e9SPeter Wemm 			    (void *)(uintptr_t)(volatile void *)
2035d244b0e9SPeter Wemm 				&sc->mcsp->mc_addr[nmcasts][0], 6);
2036397f9dfeSDavid Greenman 			nmcasts++;
2037397f9dfeSDavid Greenman 		}
2038397f9dfeSDavid Greenman 	}
2039397f9dfeSDavid Greenman 	mcsp->mc_cnt = nmcasts * 6;
2040397f9dfeSDavid Greenman 	sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
2041397f9dfeSDavid Greenman 	sc->tx_queued = 1;
2042397f9dfeSDavid Greenman 
2043397f9dfeSDavid Greenman 	/*
2044397f9dfeSDavid Greenman 	 * Wait until command unit is not active. This should never
2045397f9dfeSDavid Greenman 	 * be the case when nothing is queued, but make sure anyway.
2046397f9dfeSDavid Greenman 	 */
2047397f9dfeSDavid Greenman 	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
2048397f9dfeSDavid Greenman 	    FXP_SCB_CUS_ACTIVE) ;
2049397f9dfeSDavid Greenman 
2050397f9dfeSDavid Greenman 	/*
2051397f9dfeSDavid Greenman 	 * Start the multicast setup command.
2052397f9dfeSDavid Greenman 	 */
2053397f9dfeSDavid Greenman 	fxp_scb_wait(sc);
2054397f9dfeSDavid Greenman 	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
2055397f9dfeSDavid Greenman 	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_SCB_COMMAND_CU_START);
2056397f9dfeSDavid Greenman 
20573114fdb4SDavid Greenman 	ifp->if_timer = 2;
2058397f9dfeSDavid Greenman 	return;
2059397f9dfeSDavid Greenman }
2060