i82557.c revision 1.131
1/*	$NetBSD: i82557.c,v 1.131 2010/01/19 22:06:24 pooka Exp $	*/
2
3/*-
4 * Copyright (c) 1997, 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1995, David Greenman
35 * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice unmodified, this list of conditions, and the following
43 *    disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	Id: if_fxp.c,v 1.113 2001/05/17 23:50:24 jlemon
61 */
62
63/*
64 * Device driver for the Intel i82557 fast Ethernet controller,
65 * and its successors, the i82558 and i82559.
66 */
67
68#include <sys/cdefs.h>
69__KERNEL_RCSID(0, "$NetBSD: i82557.c,v 1.131 2010/01/19 22:06:24 pooka Exp $");
70
71#include "rnd.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/callout.h>
76#include <sys/mbuf.h>
77#include <sys/malloc.h>
78#include <sys/kernel.h>
79#include <sys/socket.h>
80#include <sys/ioctl.h>
81#include <sys/errno.h>
82#include <sys/device.h>
83#include <sys/syslog.h>
84
85#include <machine/endian.h>
86
87#include <uvm/uvm_extern.h>
88
89#if NRND > 0
90#include <sys/rnd.h>
91#endif
92
93#include <net/if.h>
94#include <net/if_dl.h>
95#include <net/if_media.h>
96#include <net/if_ether.h>
97
98#include <netinet/in.h>
99#include <netinet/in_systm.h>
100#include <netinet/ip.h>
101#include <netinet/tcp.h>
102#include <netinet/udp.h>
103
104#include <net/bpf.h>
105
106#include <sys/bus.h>
107#include <sys/intr.h>
108
109#include <dev/mii/miivar.h>
110
111#include <dev/ic/i82557reg.h>
112#include <dev/ic/i82557var.h>
113
114#include <dev/microcode/i8255x/rcvbundl.h>
115
116/*
117 * NOTE!  On the Alpha, we have an alignment constraint.  The
118 * card DMAs the packet immediately following the RFA.  However,
119 * the first thing in the packet is a 14-byte Ethernet header.
120 * This means that the packet is misaligned.  To compensate,
121 * we actually offset the RFA 2 bytes into the cluster.  This
122 * alignes the packet after the Ethernet header at a 32-bit
123 * boundary.  HOWEVER!  This means that the RFA is misaligned!
124 */
125#define	RFA_ALIGNMENT_FUDGE	2
126
127/*
128 * The configuration byte map has several undefined fields which
129 * must be one or must be zero.  Set up a template for these bits
130 * only (assuming an i82557 chip), leaving the actual configuration
131 * for fxp_init().
132 *
133 * See the definition of struct fxp_cb_config for the bit definitions.
134 */
135const uint8_t fxp_cb_config_template[] = {
136	0x0, 0x0,		/* cb_status */
137	0x0, 0x0,		/* cb_command */
138	0x0, 0x0, 0x0, 0x0,	/* link_addr */
139	0x0,	/*  0 */
140	0x0,	/*  1 */
141	0x0,	/*  2 */
142	0x0,	/*  3 */
143	0x0,	/*  4 */
144	0x0,	/*  5 */
145	0x32,	/*  6 */
146	0x0,	/*  7 */
147	0x0,	/*  8 */
148	0x0,	/*  9 */
149	0x6,	/* 10 */
150	0x0,	/* 11 */
151	0x0,	/* 12 */
152	0x0,	/* 13 */
153	0xf2,	/* 14 */
154	0x48,	/* 15 */
155	0x0,	/* 16 */
156	0x40,	/* 17 */
157	0xf0,	/* 18 */
158	0x0,	/* 19 */
159	0x3f,	/* 20 */
160	0x5,	/* 21 */
161	0x0,	/* 22 */
162	0x0,	/* 23 */
163	0x0,	/* 24 */
164	0x0,	/* 25 */
165	0x0,	/* 26 */
166	0x0,	/* 27 */
167	0x0,	/* 28 */
168	0x0,	/* 29 */
169	0x0,	/* 30 */
170	0x0,	/* 31 */
171};
172
173void	fxp_mii_initmedia(struct fxp_softc *);
174void	fxp_mii_mediastatus(struct ifnet *, struct ifmediareq *);
175
176void	fxp_80c24_initmedia(struct fxp_softc *);
177int	fxp_80c24_mediachange(struct ifnet *);
178void	fxp_80c24_mediastatus(struct ifnet *, struct ifmediareq *);
179
180void	fxp_start(struct ifnet *);
181int	fxp_ioctl(struct ifnet *, u_long, void *);
182void	fxp_watchdog(struct ifnet *);
183int	fxp_init(struct ifnet *);
184void	fxp_stop(struct ifnet *, int);
185
186void	fxp_txintr(struct fxp_softc *);
187int	fxp_rxintr(struct fxp_softc *);
188
189void	fxp_rx_hwcksum(struct fxp_softc *,struct mbuf *,
190	    const struct fxp_rfa *, u_int);
191
192void	fxp_rxdrain(struct fxp_softc *);
193int	fxp_add_rfabuf(struct fxp_softc *, bus_dmamap_t, int);
194int	fxp_mdi_read(device_t, int, int);
195void	fxp_statchg(device_t);
196void	fxp_mdi_write(device_t, int, int, int);
197void	fxp_autosize_eeprom(struct fxp_softc*);
198void	fxp_read_eeprom(struct fxp_softc *, uint16_t *, int, int);
199void	fxp_write_eeprom(struct fxp_softc *, uint16_t *, int, int);
200void	fxp_eeprom_update_cksum(struct fxp_softc *);
201void	fxp_get_info(struct fxp_softc *, uint8_t *);
202void	fxp_tick(void *);
203void	fxp_mc_setup(struct fxp_softc *);
204void	fxp_load_ucode(struct fxp_softc *);
205
206int	fxp_copy_small = 0;
207
208/*
209 * Variables for interrupt mitigating microcode.
210 */
211int	fxp_int_delay = 1000;		/* usec */
212int	fxp_bundle_max = 6;		/* packets */
213
214struct fxp_phytype {
215	int	fp_phy;		/* type of PHY, -1 for MII at the end. */
216	void	(*fp_init)(struct fxp_softc *);
217} fxp_phytype_table[] = {
218	{ FXP_PHY_80C24,		fxp_80c24_initmedia },
219	{ -1,				fxp_mii_initmedia },
220};
221
222/*
223 * Set initial transmit threshold at 64 (512 bytes). This is
224 * increased by 64 (512 bytes) at a time, to maximum of 192
225 * (1536 bytes), if an underrun occurs.
226 */
227static int tx_threshold = 64;
228
229/*
230 * Wait for the previous command to be accepted (but not necessarily
231 * completed).
232 */
233static inline void
234fxp_scb_wait(struct fxp_softc *sc)
235{
236	int i = 10000;
237
238	while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
239		delay(2);
240	if (i == 0)
241		log(LOG_WARNING,
242		    "%s: WARNING: SCB timed out!\n", device_xname(sc->sc_dev));
243}
244
245/*
246 * Submit a command to the i82557.
247 */
248static inline void
249fxp_scb_cmd(struct fxp_softc *sc, uint8_t cmd)
250{
251
252	CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
253}
254
255/*
256 * Finish attaching an i82557 interface.  Called by bus-specific front-end.
257 */
258void
259fxp_attach(struct fxp_softc *sc)
260{
261	uint8_t enaddr[ETHER_ADDR_LEN];
262	struct ifnet *ifp;
263	bus_dma_segment_t seg;
264	int rseg, i, error;
265	struct fxp_phytype *fp;
266
267	callout_init(&sc->sc_callout, 0);
268
269        /*
270	 * Enable use of extended RFDs and IPCBs for 82550 and later chips.
271	 * Note: to use IPCB we need extended TXCB support too, and
272	 *       these feature flags should be set in each bus attachment.
273	 */
274	if (sc->sc_flags & FXPF_EXT_RFA) {
275		sc->sc_txcmd = htole16(FXP_CB_COMMAND_IPCBXMIT);
276		sc->sc_rfa_size = RFA_EXT_SIZE;
277	} else {
278		sc->sc_txcmd = htole16(FXP_CB_COMMAND_XMIT);
279		sc->sc_rfa_size = RFA_SIZE;
280	}
281
282	/*
283	 * Allocate the control data structures, and create and load the
284	 * DMA map for it.
285	 */
286	if ((error = bus_dmamem_alloc(sc->sc_dmat,
287	    sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
288	    0)) != 0) {
289		aprint_error_dev(sc->sc_dev,
290		    "unable to allocate control data, error = %d\n",
291		    error);
292		goto fail_0;
293	}
294
295	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
296	    sizeof(struct fxp_control_data), (void **)&sc->sc_control_data,
297	    BUS_DMA_COHERENT)) != 0) {
298		aprint_error_dev(sc->sc_dev,
299		    "unable to map control data, error = %d\n", error);
300		goto fail_1;
301	}
302	sc->sc_cdseg = seg;
303	sc->sc_cdnseg = rseg;
304
305	memset(sc->sc_control_data, 0, sizeof(struct fxp_control_data));
306
307	if ((error = bus_dmamap_create(sc->sc_dmat,
308	    sizeof(struct fxp_control_data), 1,
309	    sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
310		aprint_error_dev(sc->sc_dev,
311		    "unable to create control data DMA map, error = %d\n",
312		    error);
313		goto fail_2;
314	}
315
316	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
317	    sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
318	    0)) != 0) {
319		aprint_error_dev(sc->sc_dev,
320		    "can't load control data DMA map, error = %d\n",
321		    error);
322		goto fail_3;
323	}
324
325	/*
326	 * Create the transmit buffer DMA maps.
327	 */
328	for (i = 0; i < FXP_NTXCB; i++) {
329		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
330		    (sc->sc_flags & FXPF_EXT_RFA) ?
331		    FXP_IPCB_NTXSEG : FXP_NTXSEG,
332		    MCLBYTES, 0, 0, &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
333			aprint_error_dev(sc->sc_dev,
334			    "unable to create tx DMA map %d, error = %d\n",
335			    i, error);
336			goto fail_4;
337		}
338	}
339
340	/*
341	 * Create the receive buffer DMA maps.
342	 */
343	for (i = 0; i < FXP_NRFABUFS; i++) {
344		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
345		    MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
346			aprint_error_dev(sc->sc_dev,
347			    "unable to create rx DMA map %d, error = %d\n",
348			    i, error);
349			goto fail_5;
350		}
351	}
352
353	/* Initialize MAC address and media structures. */
354	fxp_get_info(sc, enaddr);
355
356	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
357	    ether_sprintf(enaddr));
358
359	ifp = &sc->sc_ethercom.ec_if;
360
361	/*
362	 * Get info about our media interface, and initialize it.  Note
363	 * the table terminates itself with a phy of -1, indicating
364	 * that we're using MII.
365	 */
366	for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
367		if (fp->fp_phy == sc->phy_primary_device)
368			break;
369	(*fp->fp_init)(sc);
370
371	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
372	ifp->if_softc = sc;
373	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
374	ifp->if_ioctl = fxp_ioctl;
375	ifp->if_start = fxp_start;
376	ifp->if_watchdog = fxp_watchdog;
377	ifp->if_init = fxp_init;
378	ifp->if_stop = fxp_stop;
379	IFQ_SET_READY(&ifp->if_snd);
380
381	if (sc->sc_flags & FXPF_EXT_RFA) {
382		/*
383		 * Enable hardware cksum support by EXT_RFA and IPCB.
384		 *
385		 * IFCAP_CSUM_IPv4_Tx seems to have a problem,
386		 * at least, on i82550 rev.12.
387		 * specifically, it doesn't set ipv4 checksum properly
388		 * when sending UDP (and probably TCP) packets with
389		 * 20 byte ipv4 header + 1 or 2 byte data,
390		 * though ICMP packets seem working.
391		 * FreeBSD driver has related comments.
392		 * We've added a workaround to handle the bug by padding
393		 * such packets manually.
394		 */
395		ifp->if_capabilities =
396		    IFCAP_CSUM_IPv4_Tx  | IFCAP_CSUM_IPv4_Rx  |
397		    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
398		    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
399		sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
400	} else if (sc->sc_flags & FXPF_82559_RXCSUM) {
401		ifp->if_capabilities =
402		    IFCAP_CSUM_TCPv4_Rx |
403		    IFCAP_CSUM_UDPv4_Rx;
404	}
405
406	/*
407	 * We can support 802.1Q VLAN-sized frames.
408	 */
409	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
410
411	/*
412	 * Attach the interface.
413	 */
414	if_attach(ifp);
415	ether_ifattach(ifp, enaddr);
416#if NRND > 0
417	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
418	    RND_TYPE_NET, 0);
419#endif
420
421#ifdef FXP_EVENT_COUNTERS
422	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
423	    NULL, device_xname(sc->sc_dev), "txstall");
424	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
425	    NULL, device_xname(sc->sc_dev), "txintr");
426	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
427	    NULL, device_xname(sc->sc_dev), "rxintr");
428	if (sc->sc_flags & FXPF_FC) {
429		evcnt_attach_dynamic(&sc->sc_ev_txpause, EVCNT_TYPE_MISC,
430		    NULL, device_xname(sc->sc_dev), "txpause");
431		evcnt_attach_dynamic(&sc->sc_ev_rxpause, EVCNT_TYPE_MISC,
432		    NULL, device_xname(sc->sc_dev), "rxpause");
433	}
434#endif /* FXP_EVENT_COUNTERS */
435
436	/* The attach is successful. */
437	sc->sc_flags |= FXPF_ATTACHED;
438
439	return;
440
441	/*
442	 * Free any resources we've allocated during the failed attach
443	 * attempt.  Do this in reverse order and fall though.
444	 */
445 fail_5:
446	for (i = 0; i < FXP_NRFABUFS; i++) {
447		if (sc->sc_rxmaps[i] != NULL)
448			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
449	}
450 fail_4:
451	for (i = 0; i < FXP_NTXCB; i++) {
452		if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
453			bus_dmamap_destroy(sc->sc_dmat,
454			    FXP_DSTX(sc, i)->txs_dmamap);
455	}
456	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
457 fail_3:
458	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
459 fail_2:
460	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
461	    sizeof(struct fxp_control_data));
462 fail_1:
463	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
464 fail_0:
465	return;
466}
467
468void
469fxp_mii_initmedia(struct fxp_softc *sc)
470{
471	int flags;
472
473	sc->sc_flags |= FXPF_MII;
474
475	sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
476	sc->sc_mii.mii_readreg = fxp_mdi_read;
477	sc->sc_mii.mii_writereg = fxp_mdi_write;
478	sc->sc_mii.mii_statchg = fxp_statchg;
479
480	sc->sc_ethercom.ec_mii = &sc->sc_mii;
481	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ether_mediachange,
482	    fxp_mii_mediastatus);
483
484	flags = MIIF_NOISOLATE;
485	if (sc->sc_flags & FXPF_FC)
486		flags |= MIIF_FORCEANEG|MIIF_DOPAUSE;
487	/*
488	 * The i82557 wedges if all of its PHYs are isolated!
489	 */
490	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
491	    MII_OFFSET_ANY, flags);
492	if (LIST_EMPTY(&sc->sc_mii.mii_phys)) {
493		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
494		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
495	} else
496		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
497}
498
499void
500fxp_80c24_initmedia(struct fxp_softc *sc)
501{
502
503	/*
504	 * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
505	 * doesn't have a programming interface of any sort.  The
506	 * media is sensed automatically based on how the link partner
507	 * is configured.  This is, in essence, manual configuration.
508	 */
509	aprint_normal_dev(sc->sc_dev,
510	    "Seeq 80c24 AutoDUPLEX media interface present\n");
511	ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
512	    fxp_80c24_mediastatus);
513	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
514	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
515}
516
517/*
518 * Initialize the interface media.
519 */
520void
521fxp_get_info(struct fxp_softc *sc, uint8_t *enaddr)
522{
523	uint16_t data, myea[ETHER_ADDR_LEN / 2];
524
525	/*
526	 * Reset to a stable state.
527	 */
528	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
529	DELAY(100);
530
531	sc->sc_eeprom_size = 0;
532	fxp_autosize_eeprom(sc);
533	if (sc->sc_eeprom_size == 0) {
534		aprint_error_dev(sc->sc_dev, "failed to detect EEPROM size\n");
535		sc->sc_eeprom_size = 6; /* XXX panic here? */
536	}
537#ifdef DEBUG
538	aprint_debug_dev(sc->sc_dev, "detected %d word EEPROM\n",
539	    1 << sc->sc_eeprom_size);
540#endif
541
542	/*
543	 * Get info about the primary PHY
544	 */
545	fxp_read_eeprom(sc, &data, 6, 1);
546	sc->phy_primary_device =
547	    (data & FXP_PHY_DEVICE_MASK) >> FXP_PHY_DEVICE_SHIFT;
548
549	/*
550	 * Read MAC address.
551	 */
552	fxp_read_eeprom(sc, myea, 0, 3);
553	enaddr[0] = myea[0] & 0xff;
554	enaddr[1] = myea[0] >> 8;
555	enaddr[2] = myea[1] & 0xff;
556	enaddr[3] = myea[1] >> 8;
557	enaddr[4] = myea[2] & 0xff;
558	enaddr[5] = myea[2] >> 8;
559
560	/*
561	 * Systems based on the ICH2/ICH2-M chip from Intel, as well
562	 * as some i82559 designs, have a defect where the chip can
563	 * cause a PCI protocol violation if it receives a CU_RESUME
564	 * command when it is entering the IDLE state.
565	 *
566	 * The work-around is to disable Dynamic Standby Mode, so that
567	 * the chip never deasserts #CLKRUN, and always remains in the
568	 * active state.
569	 *
570	 * Unfortunately, the only way to disable Dynamic Standby is
571	 * to frob an EEPROM setting and reboot (the EEPROM setting
572	 * is only consulted when the PCI bus comes out of reset).
573	 *
574	 * See Intel 82801BA/82801BAM Specification Update, Errata #30.
575	 */
576	if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
577		fxp_read_eeprom(sc, &data, 10, 1);
578		if (data & 0x02) {		/* STB enable */
579			aprint_error_dev(sc->sc_dev, "WARNING: "
580			    "Disabling dynamic standby mode in EEPROM "
581			    "to work around a\n");
582			aprint_normal_dev(sc->sc_dev,
583			    "WARNING: hardware bug.  You must reset "
584			    "the system before using this\n");
585			aprint_normal_dev(sc->sc_dev, "WARNING: interface.\n");
586			data &= ~0x02;
587			fxp_write_eeprom(sc, &data, 10, 1);
588			aprint_normal_dev(sc->sc_dev, "new EEPROM ID: 0x%04x\n",
589			    data);
590			fxp_eeprom_update_cksum(sc);
591		}
592	}
593
594	/* Receiver lock-up workaround detection. (FXPF_RECV_WORKAROUND) */
595	/* Due to false positives we make it conditional on setting link1 */
596	fxp_read_eeprom(sc, &data, 3, 1);
597	if ((data & 0x03) != 0x03) {
598		aprint_verbose_dev(sc->sc_dev,
599		    "May need receiver lock-up workaround\n");
600	}
601}
602
603static void
604fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int len)
605{
606	uint16_t reg;
607	int x;
608
609	for (x = 1 << (len - 1); x != 0; x >>= 1) {
610		DELAY(40);
611		if (data & x)
612			reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
613		else
614			reg = FXP_EEPROM_EECS;
615		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
616		DELAY(40);
617		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
618		    reg | FXP_EEPROM_EESK);
619		DELAY(40);
620		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
621	}
622	DELAY(40);
623}
624
625/*
626 * Figure out EEPROM size.
627 *
628 * 559's can have either 64-word or 256-word EEPROMs, the 558
629 * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
630 * talks about the existence of 16 to 256 word EEPROMs.
631 *
632 * The only known sizes are 64 and 256, where the 256 version is used
633 * by CardBus cards to store CIS information.
634 *
635 * The address is shifted in msb-to-lsb, and after the last
636 * address-bit the EEPROM is supposed to output a `dummy zero' bit,
637 * after which follows the actual data. We try to detect this zero, by
638 * probing the data-out bit in the EEPROM control register just after
639 * having shifted in a bit. If the bit is zero, we assume we've
640 * shifted enough address bits. The data-out should be tri-state,
641 * before this, which should translate to a logical one.
642 *
643 * Other ways to do this would be to try to read a register with known
644 * contents with a varying number of address bits, but no such
645 * register seem to be available. The high bits of register 10 are 01
646 * on the 558 and 559, but apparently not on the 557.
647 *
648 * The Linux driver computes a checksum on the EEPROM data, but the
649 * value of this checksum is not very well documented.
650 */
651
652void
653fxp_autosize_eeprom(struct fxp_softc *sc)
654{
655	int x;
656
657	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
658	DELAY(40);
659
660	/* Shift in read opcode. */
661	fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
662
663	/*
664	 * Shift in address, wait for the dummy zero following a correct
665	 * address shift.
666	 */
667	for (x = 1; x <= 8; x++) {
668		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
669		DELAY(40);
670		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
671		    FXP_EEPROM_EECS | FXP_EEPROM_EESK);
672		DELAY(40);
673		if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
674		    FXP_EEPROM_EEDO) == 0)
675			break;
676		DELAY(40);
677		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
678		DELAY(40);
679	}
680	CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
681	DELAY(40);
682	if (x != 6 && x != 8) {
683#ifdef DEBUG
684		printf("%s: strange EEPROM size (%d)\n",
685		    device_xname(sc->sc_dev), 1 << x);
686#endif
687	} else
688		sc->sc_eeprom_size = x;
689}
690
691/*
692 * Read from the serial EEPROM. Basically, you manually shift in
693 * the read opcode (one bit at a time) and then shift in the address,
694 * and then you shift out the data (all of this one bit at a time).
695 * The word size is 16 bits, so you have to provide the address for
696 * every 16 bits of data.
697 */
698void
699fxp_read_eeprom(struct fxp_softc *sc, uint16_t *data, int offset, int words)
700{
701	uint16_t reg;
702	int i, x;
703
704	for (i = 0; i < words; i++) {
705		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
706
707		/* Shift in read opcode. */
708		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
709
710		/* Shift in address. */
711		fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
712
713		reg = FXP_EEPROM_EECS;
714		data[i] = 0;
715
716		/* Shift out data. */
717		for (x = 16; x > 0; x--) {
718			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
719			    reg | FXP_EEPROM_EESK);
720			DELAY(40);
721			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
722			    FXP_EEPROM_EEDO)
723				data[i] |= (1 << (x - 1));
724			CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
725			DELAY(40);
726		}
727		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
728		DELAY(40);
729	}
730}
731
732/*
733 * Write data to the serial EEPROM.
734 */
735void
736fxp_write_eeprom(struct fxp_softc *sc, uint16_t *data, int offset, int words)
737{
738	int i, j;
739
740	for (i = 0; i < words; i++) {
741		/* Erase/write enable. */
742		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
743		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
744		fxp_eeprom_shiftin(sc, 0x3 << (sc->sc_eeprom_size - 2),
745		    sc->sc_eeprom_size);
746		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
747		DELAY(4);
748
749		/* Shift in write opcode, address, data. */
750		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
751		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
752		fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
753		fxp_eeprom_shiftin(sc, data[i], 16);
754		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
755		DELAY(4);
756
757		/* Wait for the EEPROM to finish up. */
758		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
759		DELAY(4);
760		for (j = 0; j < 1000; j++) {
761			if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
762			    FXP_EEPROM_EEDO)
763				break;
764			DELAY(50);
765		}
766		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
767		DELAY(4);
768
769		/* Erase/write disable. */
770		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
771		fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
772		fxp_eeprom_shiftin(sc, 0, sc->sc_eeprom_size);
773		CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
774		DELAY(4);
775	}
776}
777
778/*
779 * Update the checksum of the EEPROM.
780 */
781void
782fxp_eeprom_update_cksum(struct fxp_softc *sc)
783{
784	int i;
785	uint16_t data, cksum;
786
787	cksum = 0;
788	for (i = 0; i < (1 << sc->sc_eeprom_size) - 1; i++) {
789		fxp_read_eeprom(sc, &data, i, 1);
790		cksum += data;
791	}
792	i = (1 << sc->sc_eeprom_size) - 1;
793	cksum = 0xbaba - cksum;
794	fxp_read_eeprom(sc, &data, i, 1);
795	fxp_write_eeprom(sc, &cksum, i, 1);
796	log(LOG_INFO, "%s: EEPROM checksum @ 0x%x: 0x%04x -> 0x%04x\n",
797	    device_xname(sc->sc_dev), i, data, cksum);
798}
799
800/*
801 * Start packet transmission on the interface.
802 */
803void
804fxp_start(struct ifnet *ifp)
805{
806	struct fxp_softc *sc = ifp->if_softc;
807	struct mbuf *m0, *m;
808	struct fxp_txdesc *txd;
809	struct fxp_txsoft *txs;
810	bus_dmamap_t dmamap;
811	int error, lasttx, nexttx, opending, seg, nsegs, len;
812
813	/*
814	 * If we want a re-init, bail out now.
815	 */
816	if (sc->sc_flags & FXPF_WANTINIT) {
817		ifp->if_flags |= IFF_OACTIVE;
818		return;
819	}
820
821	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
822		return;
823
824	/*
825	 * Remember the previous txpending and the current lasttx.
826	 */
827	opending = sc->sc_txpending;
828	lasttx = sc->sc_txlast;
829
830	/*
831	 * Loop through the send queue, setting up transmit descriptors
832	 * until we drain the queue, or use up all available transmit
833	 * descriptors.
834	 */
835	for (;;) {
836		struct fxp_tbd *tbdp;
837		int csum_flags;
838
839		/*
840		 * Grab a packet off the queue.
841		 */
842		IFQ_POLL(&ifp->if_snd, m0);
843		if (m0 == NULL)
844			break;
845		m = NULL;
846
847		if (sc->sc_txpending == FXP_NTXCB - 1) {
848			FXP_EVCNT_INCR(&sc->sc_ev_txstall);
849			break;
850		}
851
852		/*
853		 * Get the next available transmit descriptor.
854		 */
855		nexttx = FXP_NEXTTX(sc->sc_txlast);
856		txd = FXP_CDTX(sc, nexttx);
857		txs = FXP_DSTX(sc, nexttx);
858		dmamap = txs->txs_dmamap;
859
860		/*
861		 * Load the DMA map.  If this fails, the packet either
862		 * didn't fit in the allotted number of frags, or we were
863		 * short on resources.  In this case, we'll copy and try
864		 * again.
865		 */
866		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
867		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
868			MGETHDR(m, M_DONTWAIT, MT_DATA);
869			if (m == NULL) {
870				log(LOG_ERR, "%s: unable to allocate Tx mbuf\n",
871				    device_xname(sc->sc_dev));
872				break;
873			}
874			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
875			if (m0->m_pkthdr.len > MHLEN) {
876				MCLGET(m, M_DONTWAIT);
877				if ((m->m_flags & M_EXT) == 0) {
878					log(LOG_ERR, "%s: unable to allocate "
879					    "Tx cluster\n",
880					    device_xname(sc->sc_dev));
881					m_freem(m);
882					break;
883				}
884			}
885			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
886			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
887			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
888			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
889			if (error) {
890				log(LOG_ERR, "%s: unable to load Tx buffer, "
891				    "error = %d\n",
892				    device_xname(sc->sc_dev), error);
893				break;
894			}
895		}
896
897		IFQ_DEQUEUE(&ifp->if_snd, m0);
898		csum_flags = m0->m_pkthdr.csum_flags;
899		if (m != NULL) {
900			m_freem(m0);
901			m0 = m;
902		}
903
904		/* Initialize the fraglist. */
905		tbdp = txd->txd_tbd;
906		len = m0->m_pkthdr.len;
907		nsegs = dmamap->dm_nsegs;
908		if (sc->sc_flags & FXPF_EXT_RFA)
909			tbdp++;
910		for (seg = 0; seg < nsegs; seg++) {
911			tbdp[seg].tb_addr =
912			    htole32(dmamap->dm_segs[seg].ds_addr);
913			tbdp[seg].tb_size =
914			    htole32(dmamap->dm_segs[seg].ds_len);
915		}
916		if (__predict_false(len <= FXP_IP4CSUMTX_PADLEN &&
917		    (csum_flags & M_CSUM_IPv4) != 0)) {
918			/*
919			 * Pad short packets to avoid ip4csum-tx bug.
920			 *
921			 * XXX Should we still consider if such short
922			 *     (36 bytes or less) packets might already
923			 *     occupy FXP_IPCB_NTXSEG (15) fragments here?
924			 */
925			KASSERT(nsegs < FXP_IPCB_NTXSEG);
926			nsegs++;
927			tbdp[seg].tb_addr = htole32(FXP_CDTXPADADDR(sc));
928			tbdp[seg].tb_size =
929			    htole32(FXP_IP4CSUMTX_PADLEN + 1 - len);
930		}
931
932		/* Sync the DMA map. */
933		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
934		    BUS_DMASYNC_PREWRITE);
935
936		/*
937		 * Store a pointer to the packet so we can free it later.
938		 */
939		txs->txs_mbuf = m0;
940
941		/*
942		 * Initialize the transmit descriptor.
943		 */
944		/* BIG_ENDIAN: no need to swap to store 0 */
945		txd->txd_txcb.cb_status = 0;
946		txd->txd_txcb.cb_command =
947		    sc->sc_txcmd | htole16(FXP_CB_COMMAND_SF);
948		txd->txd_txcb.tx_threshold = tx_threshold;
949		txd->txd_txcb.tbd_number = nsegs;
950
951		KASSERT((csum_flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) == 0);
952		if (sc->sc_flags & FXPF_EXT_RFA) {
953			struct m_tag *vtag;
954			struct fxp_ipcb *ipcb;
955			/*
956			 * Deal with TCP/IP checksum offload. Note that
957			 * in order for TCP checksum offload to work,
958			 * the pseudo header checksum must have already
959			 * been computed and stored in the checksum field
960			 * in the TCP header. The stack should have
961			 * already done this for us.
962			 */
963			ipcb = &txd->txd_u.txdu_ipcb;
964			memset(ipcb, 0, sizeof(*ipcb));
965			/*
966			 * always do hardware parsing.
967			 */
968			ipcb->ipcb_ip_activation_high =
969			    FXP_IPCB_HARDWAREPARSING_ENABLE;
970			/*
971			 * ip checksum offloading.
972			 */
973			if (csum_flags & M_CSUM_IPv4) {
974				ipcb->ipcb_ip_schedule |=
975				    FXP_IPCB_IP_CHECKSUM_ENABLE;
976			}
977			/*
978			 * TCP/UDP checksum offloading.
979			 */
980			if (csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
981				ipcb->ipcb_ip_schedule |=
982				    FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
983			}
984
985			/*
986			 * request VLAN tag insertion if needed.
987			 */
988			vtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0);
989			if (vtag) {
990				ipcb->ipcb_vlan_id =
991				    htobe16(*(u_int *)(vtag + 1));
992				ipcb->ipcb_ip_activation_high |=
993				    FXP_IPCB_INSERTVLAN_ENABLE;
994			}
995		} else {
996			KASSERT((csum_flags &
997			    (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) == 0);
998		}
999
1000		FXP_CDTXSYNC(sc, nexttx,
1001		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1002
1003		/* Advance the tx pointer. */
1004		sc->sc_txpending++;
1005		sc->sc_txlast = nexttx;
1006
1007		/*
1008		 * Pass packet to bpf if there is a listener.
1009		 */
1010		if (ifp->if_bpf)
1011			bpf_ops->bpf_mtap(ifp->if_bpf, m0);
1012	}
1013
1014	if (sc->sc_txpending == FXP_NTXCB - 1) {
1015		/* No more slots; notify upper layer. */
1016		ifp->if_flags |= IFF_OACTIVE;
1017	}
1018
1019	if (sc->sc_txpending != opending) {
1020		/*
1021		 * We enqueued packets.  If the transmitter was idle,
1022		 * reset the txdirty pointer.
1023		 */
1024		if (opending == 0)
1025			sc->sc_txdirty = FXP_NEXTTX(lasttx);
1026
1027		/*
1028		 * Cause the chip to interrupt and suspend command
1029		 * processing once the last packet we've enqueued
1030		 * has been transmitted.
1031		 *
1032		 * To avoid a race between updating status bits
1033		 * by the fxp chip and clearing command bits
1034		 * by this function on machines which don't have
1035		 * atomic methods to clear/set bits in memory
1036		 * smaller than 32bits (both cb_status and cb_command
1037		 * members are uint16_t and in the same 32bit word),
1038		 * we have to prepare a dummy TX descriptor which has
1039		 * NOP command and just causes a TX completion interrupt.
1040		 */
1041		sc->sc_txpending++;
1042		sc->sc_txlast = FXP_NEXTTX(sc->sc_txlast);
1043		txd = FXP_CDTX(sc, sc->sc_txlast);
1044		/* BIG_ENDIAN: no need to swap to store 0 */
1045		txd->txd_txcb.cb_status = 0;
1046		txd->txd_txcb.cb_command = htole16(FXP_CB_COMMAND_NOP |
1047		    FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
1048		FXP_CDTXSYNC(sc, sc->sc_txlast,
1049		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1050
1051		/*
1052		 * The entire packet chain is set up.  Clear the suspend bit
1053		 * on the command prior to the first packet we set up.
1054		 */
1055		FXP_CDTXSYNC(sc, lasttx,
1056		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1057		FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
1058		    htole16(~FXP_CB_COMMAND_S);
1059		FXP_CDTXSYNC(sc, lasttx,
1060		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1061
1062		/*
1063		 * Issue a Resume command in case the chip was suspended.
1064		 */
1065		fxp_scb_wait(sc);
1066		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
1067
1068		/* Set a watchdog timer in case the chip flakes out. */
1069		ifp->if_timer = 5;
1070	}
1071}
1072
1073/*
1074 * Process interface interrupts.
1075 */
1076int
1077fxp_intr(void *arg)
1078{
1079	struct fxp_softc *sc = arg;
1080	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1081	bus_dmamap_t rxmap;
1082	int claimed = 0, rnr;
1083	uint8_t statack;
1084
1085	if (!device_is_active(sc->sc_dev) || sc->sc_enabled == 0)
1086		return (0);
1087	/*
1088	 * If the interface isn't running, don't try to
1089	 * service the interrupt.. just ack it and bail.
1090	 */
1091	if ((ifp->if_flags & IFF_RUNNING) == 0) {
1092		statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
1093		if (statack) {
1094			claimed = 1;
1095			CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1096		}
1097		return (claimed);
1098	}
1099
1100	while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
1101		claimed = 1;
1102
1103		/*
1104		 * First ACK all the interrupts in this pass.
1105		 */
1106		CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
1107
1108		/*
1109		 * Process receiver interrupts. If a no-resource (RNR)
1110		 * condition exists, get whatever packets we can and
1111		 * re-start the receiver.
1112		 */
1113		rnr = (statack & (FXP_SCB_STATACK_RNR | FXP_SCB_STATACK_SWI)) ?
1114		    1 : 0;
1115		if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR |
1116		    FXP_SCB_STATACK_SWI)) {
1117			FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
1118			rnr |= fxp_rxintr(sc);
1119		}
1120
1121		/*
1122		 * Free any finished transmit mbuf chains.
1123		 */
1124		if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
1125			FXP_EVCNT_INCR(&sc->sc_ev_txintr);
1126			fxp_txintr(sc);
1127
1128			/*
1129			 * Try to get more packets going.
1130			 */
1131			fxp_start(ifp);
1132
1133			if (sc->sc_txpending == 0) {
1134				/*
1135				 * Tell them that they can re-init now.
1136				 */
1137				if (sc->sc_flags & FXPF_WANTINIT)
1138					wakeup(sc);
1139			}
1140		}
1141
1142		if (rnr) {
1143			fxp_scb_wait(sc);
1144			fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_ABORT);
1145			rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1146			fxp_scb_wait(sc);
1147			CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1148			    rxmap->dm_segs[0].ds_addr +
1149			    RFA_ALIGNMENT_FUDGE);
1150			fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
1151		}
1152	}
1153
1154#if NRND > 0
1155	if (claimed)
1156		rnd_add_uint32(&sc->rnd_source, statack);
1157#endif
1158	return (claimed);
1159}
1160
1161/*
1162 * Handle transmit completion interrupts.
1163 */
1164void
1165fxp_txintr(struct fxp_softc *sc)
1166{
1167	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1168	struct fxp_txdesc *txd;
1169	struct fxp_txsoft *txs;
1170	int i;
1171	uint16_t txstat;
1172
1173	ifp->if_flags &= ~IFF_OACTIVE;
1174	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1175	    i = FXP_NEXTTX(i), sc->sc_txpending--) {
1176		txd = FXP_CDTX(sc, i);
1177		txs = FXP_DSTX(sc, i);
1178
1179		FXP_CDTXSYNC(sc, i,
1180		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1181
1182		/* skip dummy NOP TX descriptor */
1183		if ((le16toh(txd->txd_txcb.cb_command) & FXP_CB_COMMAND_CMD)
1184		    == FXP_CB_COMMAND_NOP)
1185			continue;
1186
1187		txstat = le16toh(txd->txd_txcb.cb_status);
1188
1189		if ((txstat & FXP_CB_STATUS_C) == 0)
1190			break;
1191
1192		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1193		    0, txs->txs_dmamap->dm_mapsize,
1194		    BUS_DMASYNC_POSTWRITE);
1195		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1196		m_freem(txs->txs_mbuf);
1197		txs->txs_mbuf = NULL;
1198	}
1199
1200	/* Update the dirty transmit buffer pointer. */
1201	sc->sc_txdirty = i;
1202
1203	/*
1204	 * Cancel the watchdog timer if there are no pending
1205	 * transmissions.
1206	 */
1207	if (sc->sc_txpending == 0)
1208		ifp->if_timer = 0;
1209}
1210
1211/*
1212 * fxp_rx_hwcksum: check status of H/W offloading for received packets.
1213 */
1214
1215void
1216fxp_rx_hwcksum(struct fxp_softc *sc, struct mbuf *m, const struct fxp_rfa *rfa,
1217    u_int len)
1218{
1219	uint32_t csum_data;
1220	int csum_flags;
1221
1222	/*
1223	 * check H/W Checksumming.
1224	 */
1225
1226	csum_flags = 0;
1227	csum_data = 0;
1228
1229	if ((sc->sc_flags & FXPF_EXT_RFA) != 0) {
1230		uint8_t rxparsestat;
1231		uint8_t csum_stat;
1232
1233		csum_stat = rfa->cksum_stat;
1234		rxparsestat = rfa->rx_parse_stat;
1235		if ((rfa->rfa_status & htole16(FXP_RFA_STATUS_PARSE)) == 0)
1236			goto out;
1237
1238		if (csum_stat & FXP_RFDX_CS_IP_CSUM_BIT_VALID) {
1239			csum_flags = M_CSUM_IPv4;
1240			if ((csum_stat & FXP_RFDX_CS_IP_CSUM_VALID) == 0)
1241				csum_flags |= M_CSUM_IPv4_BAD;
1242		}
1243
1244		if (csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) {
1245			csum_flags |= (M_CSUM_TCPv4|M_CSUM_UDPv4); /* XXX */
1246			if ((csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_VALID) == 0)
1247				csum_flags |= M_CSUM_TCP_UDP_BAD;
1248		}
1249
1250	} else if ((sc->sc_flags & FXPF_82559_RXCSUM) != 0) {
1251		struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1252		struct ether_header *eh;
1253		struct ip *ip;
1254		struct udphdr *uh;
1255		u_int hlen, pktlen;
1256
1257		if (len < ETHER_HDR_LEN + sizeof(struct ip))
1258			goto out;
1259		pktlen = len - ETHER_HDR_LEN;
1260		eh = mtod(m, struct ether_header *);
1261		if (ntohs(eh->ether_type) != ETHERTYPE_IP)
1262			goto out;
1263		ip = (struct ip *)((uint8_t *)eh + ETHER_HDR_LEN);
1264		if (ip->ip_v != IPVERSION)
1265			goto out;
1266
1267		hlen = ip->ip_hl << 2;
1268		if (hlen < sizeof(struct ip))
1269			goto out;
1270
1271		/*
1272		 * Bail if too short, has random trailing garbage, truncated,
1273		 * fragment, or has ethernet pad.
1274		 */
1275		if (ntohs(ip->ip_len) < hlen ||
1276		    ntohs(ip->ip_len) != pktlen ||
1277		    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0)
1278			goto out;
1279
1280		switch (ip->ip_p) {
1281		case IPPROTO_TCP:
1282			if ((ifp->if_csum_flags_rx & M_CSUM_TCPv4) == 0 ||
1283			    pktlen < (hlen + sizeof(struct tcphdr)))
1284				goto out;
1285			csum_flags =
1286			    M_CSUM_TCPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
1287			break;
1288		case IPPROTO_UDP:
1289			if ((ifp->if_csum_flags_rx & M_CSUM_UDPv4) == 0 ||
1290			    pktlen < (hlen + sizeof(struct udphdr)))
1291				goto out;
1292			uh = (struct udphdr *)((uint8_t *)ip + hlen);
1293			if (uh->uh_sum == 0)
1294				goto out;	/* no checksum */
1295			csum_flags =
1296			    M_CSUM_UDPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
1297			break;
1298		default:
1299			goto out;
1300		}
1301
1302		/* Extract computed checksum. */
1303		csum_data = be16dec(mtod(m, uint8_t *) + len);
1304
1305		/*
1306		 * The computed checksum includes IP headers,
1307		 * so we have to deduct them.
1308		 */
1309#if 0
1310		/*
1311		 * But in TCP/UDP layer we can assume the IP header is valid,
1312		 * i.e. a sum of the whole IP header should be 0xffff,
1313		 * so we don't have to bother to deduct it.
1314		 */
1315		if (hlen > 0) {
1316			uint32_t hsum;
1317			const uint16_t *iphdr;
1318			hsum = 0;
1319			iphdr = (uint16_t *)ip;
1320
1321			while (hlen > 1) {
1322				hsum += ntohs(*iphdr++);
1323				hlen -= sizeof(uint16_t);
1324			}
1325			while (hsum >> 16)
1326				hsum = (hsum >> 16) + (hsum & 0xffff);
1327
1328			csum_data += (uint16_t)~hsum;
1329
1330			while (csum_data >> 16)
1331				csum_data =
1332				    (csum_data >> 16) + (csum_data & 0xffff);
1333		}
1334#endif
1335	}
1336 out:
1337	m->m_pkthdr.csum_flags = csum_flags;
1338	m->m_pkthdr.csum_data = csum_data;
1339}
1340
1341/*
1342 * Handle receive interrupts.
1343 */
1344int
1345fxp_rxintr(struct fxp_softc *sc)
1346{
1347	struct ethercom *ec = &sc->sc_ethercom;
1348	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1349	struct mbuf *m, *m0;
1350	bus_dmamap_t rxmap;
1351	struct fxp_rfa *rfa;
1352	int rnr;
1353	uint16_t len, rxstat;
1354
1355	rnr = 0;
1356
1357	for (;;) {
1358		m = sc->sc_rxq.ifq_head;
1359		rfa = FXP_MTORFA(m);
1360		rxmap = M_GETCTX(m, bus_dmamap_t);
1361
1362		FXP_RFASYNC(sc, m,
1363		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1364
1365		rxstat = le16toh(rfa->rfa_status);
1366
1367		if ((rxstat & FXP_RFA_STATUS_RNR) != 0)
1368			rnr = 1;
1369
1370		if ((rxstat & FXP_RFA_STATUS_C) == 0) {
1371			/*
1372			 * We have processed all of the
1373			 * receive buffers.
1374			 */
1375			FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
1376			return rnr;
1377		}
1378
1379		IF_DEQUEUE(&sc->sc_rxq, m);
1380
1381		FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
1382
1383		len = le16toh(rfa->actual_size) &
1384		    (m->m_ext.ext_size - 1);
1385		if ((sc->sc_flags & FXPF_82559_RXCSUM) != 0) {
1386			/* Adjust for appended checksum bytes. */
1387			len -= sizeof(uint16_t);
1388		}
1389
1390		if (len < sizeof(struct ether_header)) {
1391			/*
1392			 * Runt packet; drop it now.
1393			 */
1394			FXP_INIT_RFABUF(sc, m);
1395			continue;
1396		}
1397
1398		/*
1399		 * If support for 802.1Q VLAN sized frames is
1400		 * enabled, we need to do some additional error
1401		 * checking (as we are saving bad frames, in
1402		 * order to receive the larger ones).
1403		 */
1404		if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
1405		    (rxstat & (FXP_RFA_STATUS_OVERRUN|
1406			       FXP_RFA_STATUS_RNR|
1407			       FXP_RFA_STATUS_ALIGN|
1408			       FXP_RFA_STATUS_CRC)) != 0) {
1409			FXP_INIT_RFABUF(sc, m);
1410			continue;
1411		}
1412
1413		/*
1414		 * check VLAN tag stripping.
1415		 */
1416		if ((sc->sc_flags & FXPF_EXT_RFA) != 0 &&
1417		    (rfa->rfa_status & htole16(FXP_RFA_STATUS_VLAN)) != 0) {
1418			struct m_tag *vtag;
1419
1420			vtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
1421			    M_NOWAIT);
1422			if (vtag == NULL)
1423				goto dropit;
1424			*(u_int *)(vtag + 1) = be16toh(rfa->vlan_id);
1425			m_tag_prepend(m, vtag);
1426		}
1427
1428		/* Do checksum checking. */
1429		if ((ifp->if_csum_flags_rx & (M_CSUM_TCPv4|M_CSUM_UDPv4)) != 0)
1430			fxp_rx_hwcksum(sc, m, rfa, len);
1431
1432		/*
1433		 * If the packet is small enough to fit in a
1434		 * single header mbuf, allocate one and copy
1435		 * the data into it.  This greatly reduces
1436		 * memory consumption when we receive lots
1437		 * of small packets.
1438		 *
1439		 * Otherwise, we add a new buffer to the receive
1440		 * chain.  If this fails, we drop the packet and
1441		 * recycle the old buffer.
1442		 */
1443		if (fxp_copy_small != 0 && len <= MHLEN) {
1444			MGETHDR(m0, M_DONTWAIT, MT_DATA);
1445			if (m0 == NULL)
1446				goto dropit;
1447			MCLAIM(m0, &sc->sc_ethercom.ec_rx_mowner);
1448			memcpy(mtod(m0, void *),
1449			    mtod(m, void *), len);
1450			m0->m_pkthdr.csum_flags = m->m_pkthdr.csum_flags;
1451			m0->m_pkthdr.csum_data = m->m_pkthdr.csum_data;
1452			FXP_INIT_RFABUF(sc, m);
1453			m = m0;
1454		} else {
1455			if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
1456 dropit:
1457				ifp->if_ierrors++;
1458				FXP_INIT_RFABUF(sc, m);
1459				continue;
1460			}
1461		}
1462
1463		m->m_pkthdr.rcvif = ifp;
1464		m->m_pkthdr.len = m->m_len = len;
1465
1466		/*
1467		 * Pass this up to any BPF listeners, but only
1468		 * pass it up the stack if it's for us.
1469		 */
1470		if (ifp->if_bpf)
1471			bpf_ops->bpf_mtap(ifp->if_bpf, m);
1472
1473		/* Pass it on. */
1474		(*ifp->if_input)(ifp, m);
1475	}
1476}
1477
1478/*
1479 * Update packet in/out/collision statistics. The i82557 doesn't
1480 * allow you to access these counters without doing a fairly
1481 * expensive DMA to get _all_ of the statistics it maintains, so
1482 * we do this operation here only once per second. The statistics
1483 * counters in the kernel are updated from the previous dump-stats
1484 * DMA and then a new dump-stats DMA is started. The on-chip
1485 * counters are zeroed when the DMA completes. If we can't start
1486 * the DMA immediately, we don't wait - we just prepare to read
1487 * them again next time.
1488 */
1489void
1490fxp_tick(void *arg)
1491{
1492	struct fxp_softc *sc = arg;
1493	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1494	struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
1495	int s;
1496
1497	if (!device_is_active(sc->sc_dev))
1498		return;
1499
1500	s = splnet();
1501
1502	FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
1503
1504	ifp->if_opackets += le32toh(sp->tx_good);
1505	ifp->if_collisions += le32toh(sp->tx_total_collisions);
1506	if (sp->rx_good) {
1507		ifp->if_ipackets += le32toh(sp->rx_good);
1508		sc->sc_rxidle = 0;
1509	} else if (sc->sc_flags & FXPF_RECV_WORKAROUND) {
1510		sc->sc_rxidle++;
1511	}
1512	ifp->if_ierrors +=
1513	    le32toh(sp->rx_crc_errors) +
1514	    le32toh(sp->rx_alignment_errors) +
1515	    le32toh(sp->rx_rnr_errors) +
1516	    le32toh(sp->rx_overrun_errors);
1517	/*
1518	 * If any transmit underruns occurred, bump up the transmit
1519	 * threshold by another 512 bytes (64 * 8).
1520	 */
1521	if (sp->tx_underruns) {
1522		ifp->if_oerrors += le32toh(sp->tx_underruns);
1523		if (tx_threshold < 192)
1524			tx_threshold += 64;
1525	}
1526#ifdef FXP_EVENT_COUNTERS
1527	if (sc->sc_flags & FXPF_FC) {
1528		sc->sc_ev_txpause.ev_count += sp->tx_pauseframes;
1529		sc->sc_ev_rxpause.ev_count += sp->rx_pauseframes;
1530	}
1531#endif
1532
1533	/*
1534	 * If we haven't received any packets in FXP_MAX_RX_IDLE seconds,
1535	 * then assume the receiver has locked up and attempt to clear
1536	 * the condition by reprogramming the multicast filter (actually,
1537	 * resetting the interface). This is a work-around for a bug in
1538	 * the 82557 where the receiver locks up if it gets certain types
1539	 * of garbage in the synchronization bits prior to the packet header.
1540	 * This bug is supposed to only occur in 10Mbps mode, but has been
1541	 * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
1542	 * speed transition).
1543	 */
1544	if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
1545		(void) fxp_init(ifp);
1546		splx(s);
1547		return;
1548	}
1549	/*
1550	 * If there is no pending command, start another stats
1551	 * dump. Otherwise punt for now.
1552	 */
1553	if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
1554		/*
1555		 * Start another stats dump.
1556		 */
1557		FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1558		fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
1559	} else {
1560		/*
1561		 * A previous command is still waiting to be accepted.
1562		 * Just zero our copy of the stats and wait for the
1563		 * next timer event to update them.
1564		 */
1565		/* BIG_ENDIAN: no swap required to store 0 */
1566		sp->tx_good = 0;
1567		sp->tx_underruns = 0;
1568		sp->tx_total_collisions = 0;
1569
1570		sp->rx_good = 0;
1571		sp->rx_crc_errors = 0;
1572		sp->rx_alignment_errors = 0;
1573		sp->rx_rnr_errors = 0;
1574		sp->rx_overrun_errors = 0;
1575		if (sc->sc_flags & FXPF_FC) {
1576			sp->tx_pauseframes = 0;
1577			sp->rx_pauseframes = 0;
1578		}
1579	}
1580
1581	if (sc->sc_flags & FXPF_MII) {
1582		/* Tick the MII clock. */
1583		mii_tick(&sc->sc_mii);
1584	}
1585
1586	splx(s);
1587
1588	/*
1589	 * Schedule another timeout one second from now.
1590	 */
1591	callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
1592}
1593
1594/*
1595 * Drain the receive queue.
1596 */
1597void
1598fxp_rxdrain(struct fxp_softc *sc)
1599{
1600	bus_dmamap_t rxmap;
1601	struct mbuf *m;
1602
1603	for (;;) {
1604		IF_DEQUEUE(&sc->sc_rxq, m);
1605		if (m == NULL)
1606			break;
1607		rxmap = M_GETCTX(m, bus_dmamap_t);
1608		bus_dmamap_unload(sc->sc_dmat, rxmap);
1609		FXP_RXMAP_PUT(sc, rxmap);
1610		m_freem(m);
1611	}
1612}
1613
1614/*
1615 * Stop the interface. Cancels the statistics updater and resets
1616 * the interface.
1617 */
1618void
1619fxp_stop(struct ifnet *ifp, int disable)
1620{
1621	struct fxp_softc *sc = ifp->if_softc;
1622	struct fxp_txsoft *txs;
1623	int i;
1624
1625	/*
1626	 * Turn down interface (done early to avoid bad interactions
1627	 * between panics, shutdown hooks, and the watchdog timer)
1628	 */
1629	ifp->if_timer = 0;
1630	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1631
1632	/*
1633	 * Cancel stats updater.
1634	 */
1635	callout_stop(&sc->sc_callout);
1636	if (sc->sc_flags & FXPF_MII) {
1637		/* Down the MII. */
1638		mii_down(&sc->sc_mii);
1639	}
1640
1641	/*
1642	 * Issue software reset.  This unloads any microcode that
1643	 * might already be loaded.
1644	 */
1645	sc->sc_flags &= ~FXPF_UCODE_LOADED;
1646	CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
1647	DELAY(50);
1648
1649	/*
1650	 * Release any xmit buffers.
1651	 */
1652	for (i = 0; i < FXP_NTXCB; i++) {
1653		txs = FXP_DSTX(sc, i);
1654		if (txs->txs_mbuf != NULL) {
1655			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1656			m_freem(txs->txs_mbuf);
1657			txs->txs_mbuf = NULL;
1658		}
1659	}
1660	sc->sc_txpending = 0;
1661
1662	if (disable) {
1663		fxp_rxdrain(sc);
1664		fxp_disable(sc);
1665	}
1666
1667}
1668
1669/*
1670 * Watchdog/transmission transmit timeout handler. Called when a
1671 * transmission is started on the interface, but no interrupt is
1672 * received before the timeout. This usually indicates that the
1673 * card has wedged for some reason.
1674 */
1675void
1676fxp_watchdog(struct ifnet *ifp)
1677{
1678	struct fxp_softc *sc = ifp->if_softc;
1679
1680	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
1681	ifp->if_oerrors++;
1682
1683	(void) fxp_init(ifp);
1684}
1685
1686/*
1687 * Initialize the interface.  Must be called at splnet().
1688 */
1689int
1690fxp_init(struct ifnet *ifp)
1691{
1692	struct fxp_softc *sc = ifp->if_softc;
1693	struct fxp_cb_config *cbp;
1694	struct fxp_cb_ias *cb_ias;
1695	struct fxp_txdesc *txd;
1696	bus_dmamap_t rxmap;
1697	int i, prm, save_bf, lrxen, vlan_drop, allm, error = 0;
1698	uint16_t status;
1699
1700	if ((error = fxp_enable(sc)) != 0)
1701		goto out;
1702
1703	/*
1704	 * Cancel any pending I/O
1705	 */
1706	fxp_stop(ifp, 0);
1707
1708	/*
1709	 * XXX just setting sc_flags to 0 here clears any FXPF_MII
1710	 * flag, and this prevents the MII from detaching resulting in
1711	 * a panic. The flags field should perhaps be split in runtime
1712	 * flags and more static information. For now, just clear the
1713	 * only other flag set.
1714	 */
1715
1716	sc->sc_flags &= ~FXPF_WANTINIT;
1717
1718	/*
1719	 * Initialize base of CBL and RFA memory. Loading with zero
1720	 * sets it up for regular linear addressing.
1721	 */
1722	fxp_scb_wait(sc);
1723	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
1724	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
1725
1726	fxp_scb_wait(sc);
1727	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
1728
1729	/*
1730	 * Initialize the multicast filter.  Do this now, since we might
1731	 * have to setup the config block differently.
1732	 */
1733	fxp_mc_setup(sc);
1734
1735	prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
1736	allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1737
1738	/*
1739	 * In order to support receiving 802.1Q VLAN frames, we have to
1740	 * enable "save bad frames", since they are 4 bytes larger than
1741	 * the normal Ethernet maximum frame length.  On i82558 and later,
1742	 * we have a better mechanism for this.
1743	 */
1744	save_bf = 0;
1745	lrxen = 0;
1746	vlan_drop = 0;
1747	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
1748		if (sc->sc_rev < FXP_REV_82558_A4)
1749			save_bf = 1;
1750		else
1751			lrxen = 1;
1752		if (sc->sc_rev >= FXP_REV_82550)
1753			vlan_drop = 1;
1754	}
1755
1756	/*
1757	 * Initialize base of dump-stats buffer.
1758	 */
1759	fxp_scb_wait(sc);
1760	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1761	    sc->sc_cddma + FXP_CDSTATSOFF);
1762	FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
1763	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
1764
1765	cbp = &sc->sc_control_data->fcd_configcb;
1766	memset(cbp, 0, sizeof(struct fxp_cb_config));
1767
1768	/*
1769	 * Load microcode for this controller.
1770	 */
1771	fxp_load_ucode(sc);
1772
1773	if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK1))
1774		sc->sc_flags |= FXPF_RECV_WORKAROUND;
1775	else
1776		sc->sc_flags &= ~FXPF_RECV_WORKAROUND;
1777
1778	/*
1779	 * This copy is kind of disgusting, but there are a bunch of must be
1780	 * zero and must be one bits in this structure and this is the easiest
1781	 * way to initialize them all to proper values.
1782	 */
1783	memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
1784
1785	/* BIG_ENDIAN: no need to swap to store 0 */
1786	cbp->cb_status =	0;
1787	cbp->cb_command =	htole16(FXP_CB_COMMAND_CONFIG |
1788				    FXP_CB_COMMAND_EL);
1789	/* BIG_ENDIAN: no need to swap to store 0xffffffff */
1790	cbp->link_addr =	0xffffffff; /* (no) next command */
1791					/* bytes in config block */
1792	cbp->byte_count =	(sc->sc_flags & FXPF_EXT_RFA) ?
1793				FXP_EXT_CONFIG_LEN : FXP_CONFIG_LEN;
1794	cbp->rx_fifo_limit =	8;	/* rx fifo threshold (32 bytes) */
1795	cbp->tx_fifo_limit =	0;	/* tx fifo threshold (0 bytes) */
1796	cbp->adaptive_ifs =	0;	/* (no) adaptive interframe spacing */
1797	cbp->mwi_enable =	(sc->sc_flags & FXPF_MWI) ? 1 : 0;
1798	cbp->type_enable =	0;	/* actually reserved */
1799	cbp->read_align_en =	(sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
1800	cbp->end_wr_on_cl =	(sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
1801	cbp->rx_dma_bytecount =	0;	/* (no) rx DMA max */
1802	cbp->tx_dma_bytecount =	0;	/* (no) tx DMA max */
1803	cbp->dma_mbce =		0;	/* (disable) dma max counters */
1804	cbp->late_scb =		0;	/* (don't) defer SCB update */
1805	cbp->tno_int_or_tco_en =0;	/* (disable) tx not okay interrupt */
1806	cbp->ci_int =		1;	/* interrupt on CU idle */
1807	cbp->ext_txcb_dis =	(sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
1808	cbp->ext_stats_dis =	1;	/* disable extended counters */
1809	cbp->keep_overrun_rx =	0;	/* don't pass overrun frames to host */
1810	cbp->save_bf =		save_bf;/* save bad frames */
1811	cbp->disc_short_rx =	!prm;	/* discard short packets */
1812	cbp->underrun_retry =	1;	/* retry mode (1) on DMA underrun */
1813	cbp->ext_rfa =		(sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
1814	cbp->two_frames =	0;	/* do not limit FIFO to 2 frames */
1815	cbp->dyn_tbd =		0;	/* (no) dynamic TBD mode */
1816					/* interface mode */
1817	cbp->mediatype =	(sc->sc_flags & FXPF_MII) ? 1 : 0;
1818	cbp->csma_dis =		0;	/* (don't) disable link */
1819	cbp->tcp_udp_cksum =	(sc->sc_flags & FXPF_82559_RXCSUM) ? 1 : 0;
1820					/* (don't) enable RX checksum */
1821	cbp->vlan_tco =		0;	/* (don't) enable vlan wakeup */
1822	cbp->link_wake_en =	0;	/* (don't) assert PME# on link change */
1823	cbp->arp_wake_en =	0;	/* (don't) assert PME# on arp */
1824	cbp->mc_wake_en =	0;	/* (don't) assert PME# on mcmatch */
1825	cbp->nsai =		1;	/* (don't) disable source addr insert */
1826	cbp->preamble_length =	2;	/* (7 byte) preamble */
1827	cbp->loopback =		0;	/* (don't) loopback */
1828	cbp->linear_priority =	0;	/* (normal CSMA/CD operation) */
1829	cbp->linear_pri_mode =	0;	/* (wait after xmit only) */
1830	cbp->interfrm_spacing =	6;	/* (96 bits of) interframe spacing */
1831	cbp->promiscuous =	prm;	/* promiscuous mode */
1832	cbp->bcast_disable =	0;	/* (don't) disable broadcasts */
1833	cbp->wait_after_win =	0;	/* (don't) enable modified backoff alg*/
1834	cbp->ignore_ul =	0;	/* consider U/L bit in IA matching */
1835	cbp->crc16_en =		0;	/* (don't) enable crc-16 algorithm */
1836	cbp->crscdt =		(sc->sc_flags & FXPF_MII) ? 0 : 1;
1837	cbp->stripping =	!prm;	/* truncate rx packet to byte count */
1838	cbp->padding =		1;	/* (do) pad short tx packets */
1839	cbp->rcv_crc_xfer =	0;	/* (don't) xfer CRC to host */
1840	cbp->long_rx_en =	lrxen;	/* long packet receive enable */
1841	cbp->ia_wake_en =	0;	/* (don't) wake up on address match */
1842	cbp->magic_pkt_dis =	0;	/* (don't) disable magic packet */
1843					/* must set wake_en in PMCSR also */
1844	cbp->force_fdx =	0;	/* (don't) force full duplex */
1845	cbp->fdx_pin_en =	1;	/* (enable) FDX# pin */
1846	cbp->multi_ia =		0;	/* (don't) accept multiple IAs */
1847	cbp->mc_all =		allm;	/* accept all multicasts */
1848	cbp->ext_rx_mode =	(sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
1849	cbp->vlan_drop_en =	vlan_drop;
1850
1851	if (!(sc->sc_flags & FXPF_FC)) {
1852		/*
1853		 * The i82557 has no hardware flow control, the values
1854		 * here are the defaults for the chip.
1855		 */
1856		cbp->fc_delay_lsb =	0;
1857		cbp->fc_delay_msb =	0x40;
1858		cbp->pri_fc_thresh =	3;
1859		cbp->tx_fc_dis =	0;
1860		cbp->rx_fc_restop =	0;
1861		cbp->rx_fc_restart =	0;
1862		cbp->fc_filter =	0;
1863		cbp->pri_fc_loc =	1;
1864	} else {
1865		cbp->fc_delay_lsb =	0x1f;
1866		cbp->fc_delay_msb =	0x01;
1867		cbp->pri_fc_thresh =	3;
1868		cbp->tx_fc_dis =	0;	/* enable transmit FC */
1869		cbp->rx_fc_restop =	1;	/* enable FC restop frames */
1870		cbp->rx_fc_restart =	1;	/* enable FC restart frames */
1871		cbp->fc_filter =	!prm;	/* drop FC frames to host */
1872		cbp->pri_fc_loc =	1;	/* FC pri location (byte31) */
1873		cbp->ext_stats_dis =	0;	/* enable extended stats */
1874	}
1875
1876	FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1877
1878	/*
1879	 * Start the config command/DMA.
1880	 */
1881	fxp_scb_wait(sc);
1882	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
1883	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1884	/* ...and wait for it to complete. */
1885	for (i = 1000; i > 0; i--) {
1886		FXP_CDCONFIGSYNC(sc,
1887		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1888		status = le16toh(cbp->cb_status);
1889		FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD);
1890		if ((status & FXP_CB_STATUS_C) != 0)
1891			break;
1892		DELAY(1);
1893	}
1894	if (i == 0) {
1895		log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
1896		    device_xname(sc->sc_dev), __LINE__);
1897		return (ETIMEDOUT);
1898	}
1899
1900	/*
1901	 * Initialize the station address.
1902	 */
1903	cb_ias = &sc->sc_control_data->fcd_iascb;
1904	/* BIG_ENDIAN: no need to swap to store 0 */
1905	cb_ias->cb_status = 0;
1906	cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
1907	/* BIG_ENDIAN: no need to swap to store 0xffffffff */
1908	cb_ias->link_addr = 0xffffffff;
1909	memcpy(cb_ias->macaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1910
1911	FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1912
1913	/*
1914	 * Start the IAS (Individual Address Setup) command/DMA.
1915	 */
1916	fxp_scb_wait(sc);
1917	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
1918	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1919	/* ...and wait for it to complete. */
1920	for (i = 1000; i > 0; i++) {
1921		FXP_CDIASSYNC(sc,
1922		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1923		status = le16toh(cb_ias->cb_status);
1924		FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD);
1925		if ((status & FXP_CB_STATUS_C) != 0)
1926			break;
1927		DELAY(1);
1928	}
1929	if (i == 0) {
1930		log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
1931		    device_xname(sc->sc_dev), __LINE__);
1932		return (ETIMEDOUT);
1933	}
1934
1935	/*
1936	 * Initialize the transmit descriptor ring.  txlast is initialized
1937	 * to the end of the list so that it will wrap around to the first
1938	 * descriptor when the first packet is transmitted.
1939	 */
1940	for (i = 0; i < FXP_NTXCB; i++) {
1941		txd = FXP_CDTX(sc, i);
1942		memset(txd, 0, sizeof(*txd));
1943		txd->txd_txcb.cb_command =
1944		    htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
1945		txd->txd_txcb.link_addr =
1946		    htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
1947		if (sc->sc_flags & FXPF_EXT_TXCB)
1948			txd->txd_txcb.tbd_array_addr =
1949			    htole32(FXP_CDTBDADDR(sc, i) +
1950				    (2 * sizeof(struct fxp_tbd)));
1951		else
1952			txd->txd_txcb.tbd_array_addr =
1953			    htole32(FXP_CDTBDADDR(sc, i));
1954		FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1955	}
1956	sc->sc_txpending = 0;
1957	sc->sc_txdirty = 0;
1958	sc->sc_txlast = FXP_NTXCB - 1;
1959
1960	/*
1961	 * Initialize the receive buffer list.
1962	 */
1963	sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
1964	while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
1965		rxmap = FXP_RXMAP_GET(sc);
1966		if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
1967			log(LOG_ERR, "%s: unable to allocate or map rx "
1968			    "buffer %d, error = %d\n",
1969			    device_xname(sc->sc_dev),
1970			    sc->sc_rxq.ifq_len, error);
1971			/*
1972			 * XXX Should attempt to run with fewer receive
1973			 * XXX buffers instead of just failing.
1974			 */
1975			FXP_RXMAP_PUT(sc, rxmap);
1976			fxp_rxdrain(sc);
1977			goto out;
1978		}
1979	}
1980	sc->sc_rxidle = 0;
1981
1982	/*
1983	 * Give the transmit ring to the chip.  We do this by pointing
1984	 * the chip at the last descriptor (which is a NOP|SUSPEND), and
1985	 * issuing a start command.  It will execute the NOP and then
1986	 * suspend, pointing at the first descriptor.
1987	 */
1988	fxp_scb_wait(sc);
1989	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
1990	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
1991
1992	/*
1993	 * Initialize receiver buffer area - RFA.
1994	 */
1995#if 0	/* initialization will be done by FXP_SCB_INTRCNTL_REQUEST_SWI later */
1996	rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
1997	fxp_scb_wait(sc);
1998	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
1999	    rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
2000	fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
2001#endif
2002
2003	if (sc->sc_flags & FXPF_MII) {
2004		/*
2005		 * Set current media.
2006		 */
2007		if ((error = mii_ifmedia_change(&sc->sc_mii)) != 0)
2008			goto out;
2009	}
2010
2011	/*
2012	 * ...all done!
2013	 */
2014	ifp->if_flags |= IFF_RUNNING;
2015	ifp->if_flags &= ~IFF_OACTIVE;
2016
2017	/*
2018	 * Request a software generated interrupt that will be used to
2019	 * (re)start the RU processing.  If we direct the chip to start
2020	 * receiving from the start of queue now, instead of letting the
2021	 * interrupt handler first process all received packets, we run
2022	 * the risk of having it overwrite mbuf clusters while they are
2023	 * being processed or after they have been returned to the pool.
2024	 */
2025	CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTRCNTL_REQUEST_SWI);
2026
2027	/*
2028	 * Start the one second timer.
2029	 */
2030	callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
2031
2032	/*
2033	 * Attempt to start output on the interface.
2034	 */
2035	fxp_start(ifp);
2036
2037 out:
2038	if (error) {
2039		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2040		ifp->if_timer = 0;
2041		log(LOG_ERR, "%s: interface not running\n",
2042		    device_xname(sc->sc_dev));
2043	}
2044	return (error);
2045}
2046
2047/*
2048 * Notify the world which media we're using.
2049 */
2050void
2051fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2052{
2053	struct fxp_softc *sc = ifp->if_softc;
2054
2055	if (sc->sc_enabled == 0) {
2056		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
2057		ifmr->ifm_status = 0;
2058		return;
2059	}
2060
2061	ether_mediastatus(ifp, ifmr);
2062}
2063
2064int
2065fxp_80c24_mediachange(struct ifnet *ifp)
2066{
2067
2068	/* Nothing to do here. */
2069	return (0);
2070}
2071
2072void
2073fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2074{
2075	struct fxp_softc *sc = ifp->if_softc;
2076
2077	/*
2078	 * Media is currently-selected media.  We cannot determine
2079	 * the link status.
2080	 */
2081	ifmr->ifm_status = 0;
2082	ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
2083}
2084
2085/*
2086 * Add a buffer to the end of the RFA buffer list.
2087 * Return 0 if successful, error code on failure.
2088 *
2089 * The RFA struct is stuck at the beginning of mbuf cluster and the
2090 * data pointer is fixed up to point just past it.
2091 */
2092int
2093fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
2094{
2095	struct mbuf *m;
2096	int error;
2097
2098	MGETHDR(m, M_DONTWAIT, MT_DATA);
2099	if (m == NULL)
2100		return (ENOBUFS);
2101
2102	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
2103	MCLGET(m, M_DONTWAIT);
2104	if ((m->m_flags & M_EXT) == 0) {
2105		m_freem(m);
2106		return (ENOBUFS);
2107	}
2108
2109	if (unload)
2110		bus_dmamap_unload(sc->sc_dmat, rxmap);
2111
2112	M_SETCTX(m, rxmap);
2113
2114	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
2115	error = bus_dmamap_load_mbuf(sc->sc_dmat, rxmap, m,
2116	    BUS_DMA_READ|BUS_DMA_NOWAIT);
2117	if (error) {
2118		/* XXX XXX XXX */
2119		aprint_error_dev(sc->sc_dev,
2120		    "can't load rx DMA map %d, error = %d\n",
2121		    sc->sc_rxq.ifq_len, error);
2122		panic("fxp_add_rfabuf");
2123	}
2124
2125	FXP_INIT_RFABUF(sc, m);
2126
2127	return (0);
2128}
2129
2130int
2131fxp_mdi_read(device_t self, int phy, int reg)
2132{
2133	struct fxp_softc *sc = device_private(self);
2134	int count = 10000;
2135	int value;
2136
2137	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2138	    (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
2139
2140	while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) &
2141	    0x10000000) == 0 && count--)
2142		DELAY(10);
2143
2144	if (count <= 0)
2145		log(LOG_WARNING,
2146		    "%s: fxp_mdi_read: timed out\n", device_xname(self));
2147
2148	return (value & 0xffff);
2149}
2150
2151void
2152fxp_statchg(device_t self)
2153{
2154
2155	/* Nothing to do. */
2156}
2157
2158void
2159fxp_mdi_write(device_t self, int phy, int reg, int value)
2160{
2161	struct fxp_softc *sc = device_private(self);
2162	int count = 10000;
2163
2164	CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
2165	    (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
2166	    (value & 0xffff));
2167
2168	while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
2169	    count--)
2170		DELAY(10);
2171
2172	if (count <= 0)
2173		log(LOG_WARNING,
2174		    "%s: fxp_mdi_write: timed out\n", device_xname(self));
2175}
2176
2177int
2178fxp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2179{
2180	struct fxp_softc *sc = ifp->if_softc;
2181	struct ifreq *ifr = (struct ifreq *)data;
2182	int s, error;
2183
2184	s = splnet();
2185
2186	switch (cmd) {
2187	case SIOCSIFMEDIA:
2188	case SIOCGIFMEDIA:
2189		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
2190		break;
2191
2192	default:
2193		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
2194			break;
2195
2196		error = 0;
2197
2198		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
2199			;
2200		else if (ifp->if_flags & IFF_RUNNING) {
2201			/*
2202			 * Multicast list has changed; set the
2203			 * hardware filter accordingly.
2204			 */
2205			while (sc->sc_txpending) {
2206				sc->sc_flags |= FXPF_WANTINIT;
2207				tsleep(sc, PSOCK, "fxp_init", 0);
2208			}
2209			error = fxp_init(ifp);
2210		}
2211		break;
2212	}
2213
2214	/* Try to get more packets going. */
2215	if (sc->sc_enabled)
2216		fxp_start(ifp);
2217
2218	splx(s);
2219	return (error);
2220}
2221
2222/*
2223 * Program the multicast filter.
2224 *
2225 * This function must be called at splnet().
2226 */
2227void
2228fxp_mc_setup(struct fxp_softc *sc)
2229{
2230	struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
2231	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2232	struct ethercom *ec = &sc->sc_ethercom;
2233	struct ether_multi *enm;
2234	struct ether_multistep step;
2235	int count, nmcasts;
2236	uint16_t status;
2237
2238#ifdef DIAGNOSTIC
2239	if (sc->sc_txpending)
2240		panic("fxp_mc_setup: pending transmissions");
2241#endif
2242
2243	ifp->if_flags &= ~IFF_ALLMULTI;
2244
2245	/*
2246	 * Initialize multicast setup descriptor.
2247	 */
2248	nmcasts = 0;
2249	ETHER_FIRST_MULTI(step, ec, enm);
2250	while (enm != NULL) {
2251		/*
2252		 * Check for too many multicast addresses or if we're
2253		 * listening to a range.  Either way, we simply have
2254		 * to accept all multicasts.
2255		 */
2256		if (nmcasts >= MAXMCADDR ||
2257		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
2258		    ETHER_ADDR_LEN) != 0) {
2259			/*
2260			 * Callers of this function must do the
2261			 * right thing with this.  If we're called
2262			 * from outside fxp_init(), the caller must
2263			 * detect if the state if IFF_ALLMULTI changes.
2264			 * If it does, the caller must then call
2265			 * fxp_init(), since allmulti is handled by
2266			 * the config block.
2267			 */
2268			ifp->if_flags |= IFF_ALLMULTI;
2269			return;
2270		}
2271		memcpy(&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
2272		    ETHER_ADDR_LEN);
2273		nmcasts++;
2274		ETHER_NEXT_MULTI(step, enm);
2275	}
2276
2277	/* BIG_ENDIAN: no need to swap to store 0 */
2278	mcsp->cb_status = 0;
2279	mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
2280	mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
2281	mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
2282
2283	FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2284
2285	/*
2286	 * Wait until the command unit is not active.  This should never
2287	 * happen since nothing is queued, but make sure anyway.
2288	 */
2289	count = 100;
2290	while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
2291	    FXP_SCB_CUS_ACTIVE && --count)
2292		DELAY(1);
2293	if (count == 0) {
2294		log(LOG_WARNING, "%s: line %d: command queue timeout\n",
2295		    device_xname(sc->sc_dev), __LINE__);
2296		return;
2297	}
2298
2299	/*
2300	 * Start the multicast setup command/DMA.
2301	 */
2302	fxp_scb_wait(sc);
2303	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
2304	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2305
2306	/* ...and wait for it to complete. */
2307	for (count = 1000; count > 0; count--) {
2308		FXP_CDMCSSYNC(sc,
2309		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2310		status = le16toh(mcsp->cb_status);
2311		FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD);
2312		if ((status & FXP_CB_STATUS_C) != 0)
2313			break;
2314		DELAY(1);
2315	}
2316	if (count == 0) {
2317		log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
2318		    device_xname(sc->sc_dev), __LINE__);
2319		return;
2320	}
2321}
2322
2323static const uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
2324static const uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
2325static const uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
2326static const uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
2327static const uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
2328static const uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
2329
2330#define	UCODE(x)	x, sizeof(x)/sizeof(uint32_t)
2331
2332static const struct ucode {
2333	int32_t		revision;
2334	const uint32_t	*ucode;
2335	size_t		length;
2336	uint16_t	int_delay_offset;
2337	uint16_t	bundle_max_offset;
2338} ucode_table[] = {
2339	{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a),
2340	  D101_CPUSAVER_DWORD, 0 },
2341
2342	{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0),
2343	  D101_CPUSAVER_DWORD, 0 },
2344
2345	{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
2346	  D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
2347
2348	{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
2349	  D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
2350
2351	{ FXP_REV_82550, UCODE(fxp_ucode_d102),
2352	  D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
2353
2354	{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
2355	  D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
2356
2357	{ 0, NULL, 0, 0, 0 }
2358};
2359
2360void
2361fxp_load_ucode(struct fxp_softc *sc)
2362{
2363	const struct ucode *uc;
2364	struct fxp_cb_ucode *cbp = &sc->sc_control_data->fcd_ucode;
2365	int count, i;
2366	uint16_t status;
2367
2368	if (sc->sc_flags & FXPF_UCODE_LOADED)
2369		return;
2370
2371	/*
2372	 * Only load the uCode if the user has requested that
2373	 * we do so.
2374	 */
2375	if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK0) == 0) {
2376		sc->sc_int_delay = 0;
2377		sc->sc_bundle_max = 0;
2378		return;
2379	}
2380
2381	for (uc = ucode_table; uc->ucode != NULL; uc++) {
2382		if (sc->sc_rev == uc->revision)
2383			break;
2384	}
2385	if (uc->ucode == NULL)
2386		return;
2387
2388	/* BIG ENDIAN: no need to swap to store 0 */
2389	cbp->cb_status = 0;
2390	cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
2391	cbp->link_addr = 0xffffffff;		/* (no) next command */
2392	for (i = 0; i < uc->length; i++)
2393		cbp->ucode[i] = htole32(uc->ucode[i]);
2394
2395	if (uc->int_delay_offset)
2396		*(volatile uint16_t *) &cbp->ucode[uc->int_delay_offset] =
2397		    htole16(fxp_int_delay + (fxp_int_delay / 2));
2398
2399	if (uc->bundle_max_offset)
2400		*(volatile uint16_t *) &cbp->ucode[uc->bundle_max_offset] =
2401		    htole16(fxp_bundle_max);
2402
2403	FXP_CDUCODESYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2404
2405	/*
2406	 * Download the uCode to the chip.
2407	 */
2408	fxp_scb_wait(sc);
2409	CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDUCODEOFF);
2410	fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
2411
2412	/* ...and wait for it to complete. */
2413	for (count = 10000; count > 0; count--) {
2414		FXP_CDUCODESYNC(sc,
2415		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2416		status = le16toh(cbp->cb_status);
2417		FXP_CDUCODESYNC(sc, BUS_DMASYNC_PREREAD);
2418		if ((status & FXP_CB_STATUS_C) != 0)
2419			break;
2420		DELAY(2);
2421	}
2422	if (count == 0) {
2423		sc->sc_int_delay = 0;
2424		sc->sc_bundle_max = 0;
2425		log(LOG_WARNING, "%s: timeout loading microcode\n",
2426		    device_xname(sc->sc_dev));
2427		return;
2428	}
2429
2430	if (sc->sc_int_delay != fxp_int_delay ||
2431	    sc->sc_bundle_max != fxp_bundle_max) {
2432		sc->sc_int_delay = fxp_int_delay;
2433		sc->sc_bundle_max = fxp_bundle_max;
2434		log(LOG_INFO, "%s: Microcode loaded: int delay: %d usec, "
2435		    "max bundle: %d\n", device_xname(sc->sc_dev),
2436		    sc->sc_int_delay,
2437		    uc->bundle_max_offset == 0 ? 0 : sc->sc_bundle_max);
2438	}
2439
2440	sc->sc_flags |= FXPF_UCODE_LOADED;
2441}
2442
2443int
2444fxp_enable(struct fxp_softc *sc)
2445{
2446
2447	if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
2448		if ((*sc->sc_enable)(sc) != 0) {
2449			log(LOG_ERR, "%s: device enable failed\n",
2450			    device_xname(sc->sc_dev));
2451			return (EIO);
2452		}
2453	}
2454
2455	sc->sc_enabled = 1;
2456	return (0);
2457}
2458
2459void
2460fxp_disable(struct fxp_softc *sc)
2461{
2462
2463	if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
2464		(*sc->sc_disable)(sc);
2465		sc->sc_enabled = 0;
2466	}
2467}
2468
2469/*
2470 * fxp_activate:
2471 *
2472 *	Handle device activation/deactivation requests.
2473 */
2474int
2475fxp_activate(device_t self, enum devact act)
2476{
2477	struct fxp_softc *sc = device_private(self);
2478
2479	switch (act) {
2480	case DVACT_DEACTIVATE:
2481		if_deactivate(&sc->sc_ethercom.ec_if);
2482		return 0;
2483	default:
2484		return EOPNOTSUPP;
2485	}
2486}
2487
2488/*
2489 * fxp_detach:
2490 *
2491 *	Detach an i82557 interface.
2492 */
2493int
2494fxp_detach(struct fxp_softc *sc)
2495{
2496	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2497	int i;
2498
2499	/* Succeed now if there's no work to do. */
2500	if ((sc->sc_flags & FXPF_ATTACHED) == 0)
2501		return (0);
2502
2503	/* Unhook our tick handler. */
2504	callout_stop(&sc->sc_callout);
2505
2506	if (sc->sc_flags & FXPF_MII) {
2507		/* Detach all PHYs */
2508		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
2509	}
2510
2511	/* Delete all remaining media. */
2512	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
2513
2514#if NRND > 0
2515	rnd_detach_source(&sc->rnd_source);
2516#endif
2517	ether_ifdetach(ifp);
2518	if_detach(ifp);
2519
2520	for (i = 0; i < FXP_NRFABUFS; i++) {
2521		bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
2522		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
2523	}
2524
2525	for (i = 0; i < FXP_NTXCB; i++) {
2526		bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2527		bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
2528	}
2529
2530	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
2531	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
2532	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
2533	    sizeof(struct fxp_control_data));
2534	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
2535
2536	return (0);
2537}
2538