18c2ecf20Sopenharmony_ci/* yellowfin.c: A Packet Engines G-NIC ethernet driver for linux. */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci	Written 1997-2001 by Donald Becker.
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci	This software may be used and distributed according to the terms of
68c2ecf20Sopenharmony_ci	the GNU General Public License (GPL), incorporated herein by reference.
78c2ecf20Sopenharmony_ci	Drivers based on or derived from this code fall under the GPL and must
88c2ecf20Sopenharmony_ci	retain the authorship, copyright and license notice.  This file is not
98c2ecf20Sopenharmony_ci	a complete program and may only be used when the entire operating
108c2ecf20Sopenharmony_ci	system is licensed under the GPL.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci	This driver is for the Packet Engines G-NIC PCI Gigabit Ethernet adapter.
138c2ecf20Sopenharmony_ci	It also supports the Symbios Logic version of the same chip core.
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	The author may be reached as becker@scyld.com, or C/O
168c2ecf20Sopenharmony_ci	Scyld Computing Corporation
178c2ecf20Sopenharmony_ci	410 Severn Ave., Suite 210
188c2ecf20Sopenharmony_ci	Annapolis MD 21403
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	Support and updates available at
218c2ecf20Sopenharmony_ci	http://www.scyld.com/network/yellowfin.html
228c2ecf20Sopenharmony_ci	[link no longer provides useful info -jgarzik]
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci*/
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define DRV_NAME	"yellowfin"
298c2ecf20Sopenharmony_ci#define DRV_VERSION	"2.1"
308c2ecf20Sopenharmony_ci#define DRV_RELDATE	"Sep 11, 2006"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* The user-configurable values.
338c2ecf20Sopenharmony_ci   These may be modified when a driver module is loaded.*/
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int debug = 1;			/* 1 normal messages, 0 quiet .. 7 verbose. */
368c2ecf20Sopenharmony_ci/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
378c2ecf20Sopenharmony_cistatic int max_interrupt_work = 20;
388c2ecf20Sopenharmony_cistatic int mtu;
398c2ecf20Sopenharmony_ci#ifdef YF_PROTOTYPE			/* Support for prototype hardware errata. */
408c2ecf20Sopenharmony_ci/* System-wide count of bogus-rx frames. */
418c2ecf20Sopenharmony_cistatic int bogus_rx;
428c2ecf20Sopenharmony_cistatic int dma_ctrl = 0x004A0263; 			/* Constrained by errata */
438c2ecf20Sopenharmony_cistatic int fifo_cfg = 0x0020;				/* Bypass external Tx FIFO. */
448c2ecf20Sopenharmony_ci#elif defined(YF_NEW)					/* A future perfect board :->.  */
458c2ecf20Sopenharmony_cistatic int dma_ctrl = 0x00CAC277;			/* Override when loading module! */
468c2ecf20Sopenharmony_cistatic int fifo_cfg = 0x0028;
478c2ecf20Sopenharmony_ci#else
488c2ecf20Sopenharmony_cistatic const int dma_ctrl = 0x004A0263; 			/* Constrained by errata */
498c2ecf20Sopenharmony_cistatic const int fifo_cfg = 0x0020;				/* Bypass external Tx FIFO. */
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
538c2ecf20Sopenharmony_ci   Setting to > 1514 effectively disables this feature. */
548c2ecf20Sopenharmony_cistatic int rx_copybreak;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Used to pass the media type, etc.
578c2ecf20Sopenharmony_ci   No media types are currently defined.  These exist for driver
588c2ecf20Sopenharmony_ci   interoperability.
598c2ecf20Sopenharmony_ci*/
608c2ecf20Sopenharmony_ci#define MAX_UNITS 8				/* More are supported, limit only on options */
618c2ecf20Sopenharmony_cistatic int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
628c2ecf20Sopenharmony_cistatic int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* Do ugly workaround for GX server chipset errata. */
658c2ecf20Sopenharmony_cistatic int gx_fix;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* Operational parameters that are set at compile time. */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* Keep the ring sizes a power of two for efficiency.
708c2ecf20Sopenharmony_ci   Making the Tx ring too long decreases the effectiveness of channel
718c2ecf20Sopenharmony_ci   bonding and packet priority.
728c2ecf20Sopenharmony_ci   There are no ill effects from too-large receive rings. */
738c2ecf20Sopenharmony_ci#define TX_RING_SIZE	16
748c2ecf20Sopenharmony_ci#define TX_QUEUE_SIZE	12		/* Must be > 4 && <= TX_RING_SIZE */
758c2ecf20Sopenharmony_ci#define RX_RING_SIZE	64
768c2ecf20Sopenharmony_ci#define STATUS_TOTAL_SIZE	TX_RING_SIZE*sizeof(struct tx_status_words)
778c2ecf20Sopenharmony_ci#define TX_TOTAL_SIZE		2*TX_RING_SIZE*sizeof(struct yellowfin_desc)
788c2ecf20Sopenharmony_ci#define RX_TOTAL_SIZE		RX_RING_SIZE*sizeof(struct yellowfin_desc)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* Operational parameters that usually are not changed. */
818c2ecf20Sopenharmony_ci/* Time in jiffies before concluding the transmitter is hung. */
828c2ecf20Sopenharmony_ci#define TX_TIMEOUT  (2*HZ)
838c2ecf20Sopenharmony_ci#define PKT_BUF_SZ		1536			/* Size of each temporary Rx buffer.*/
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define yellowfin_debug debug
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#include <linux/module.h>
888c2ecf20Sopenharmony_ci#include <linux/kernel.h>
898c2ecf20Sopenharmony_ci#include <linux/string.h>
908c2ecf20Sopenharmony_ci#include <linux/timer.h>
918c2ecf20Sopenharmony_ci#include <linux/errno.h>
928c2ecf20Sopenharmony_ci#include <linux/ioport.h>
938c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
948c2ecf20Sopenharmony_ci#include <linux/pci.h>
958c2ecf20Sopenharmony_ci#include <linux/init.h>
968c2ecf20Sopenharmony_ci#include <linux/mii.h>
978c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
988c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
998c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
1008c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
1018c2ecf20Sopenharmony_ci#include <linux/crc32.h>
1028c2ecf20Sopenharmony_ci#include <linux/bitops.h>
1038c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
1048c2ecf20Sopenharmony_ci#include <asm/processor.h>		/* Processor type for cache alignment. */
1058c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
1068c2ecf20Sopenharmony_ci#include <asm/io.h>
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* These identify the driver base version and may not be removed. */
1098c2ecf20Sopenharmony_cistatic const char version[] =
1108c2ecf20Sopenharmony_ci  KERN_INFO DRV_NAME ".c:v1.05  1/09/2001  Written by Donald Becker <becker@scyld.com>\n"
1118c2ecf20Sopenharmony_ci  "  (unofficial 2.4.x port, " DRV_VERSION ", " DRV_RELDATE ")\n";
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciMODULE_AUTHOR("Donald Becker <becker@scyld.com>");
1148c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Packet Engines Yellowfin G-NIC Gigabit Ethernet driver");
1158c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cimodule_param(max_interrupt_work, int, 0);
1188c2ecf20Sopenharmony_cimodule_param(mtu, int, 0);
1198c2ecf20Sopenharmony_cimodule_param(debug, int, 0);
1208c2ecf20Sopenharmony_cimodule_param(rx_copybreak, int, 0);
1218c2ecf20Sopenharmony_cimodule_param_array(options, int, NULL, 0);
1228c2ecf20Sopenharmony_cimodule_param_array(full_duplex, int, NULL, 0);
1238c2ecf20Sopenharmony_cimodule_param(gx_fix, int, 0);
1248c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_interrupt_work, "G-NIC maximum events handled per interrupt");
1258c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mtu, "G-NIC MTU (all boards)");
1268c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "G-NIC debug level (0-7)");
1278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(rx_copybreak, "G-NIC copy breakpoint for copy-only-tiny-frames");
1288c2ecf20Sopenharmony_ciMODULE_PARM_DESC(options, "G-NIC: Bits 0-3: media type, bit 17: full duplex");
1298c2ecf20Sopenharmony_ciMODULE_PARM_DESC(full_duplex, "G-NIC full duplex setting(s) (1)");
1308c2ecf20Sopenharmony_ciMODULE_PARM_DESC(gx_fix, "G-NIC: enable GX server chipset bug workaround (0-1)");
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*
1338c2ecf20Sopenharmony_ci				Theory of Operation
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciI. Board Compatibility
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciThis device driver is designed for the Packet Engines "Yellowfin" Gigabit
1388c2ecf20Sopenharmony_ciEthernet adapter.  The G-NIC 64-bit PCI card is supported, as well as the
1398c2ecf20Sopenharmony_ciSymbios 53C885E dual function chip.
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciII. Board-specific settings
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciPCI bus devices are configured by the system at boot time, so no jumpers
1448c2ecf20Sopenharmony_cineed to be set on the board.  The system BIOS preferably should assign the
1458c2ecf20Sopenharmony_ciPCI INTA signal to an otherwise unused system IRQ line.
1468c2ecf20Sopenharmony_ciNote: Kernel versions earlier than 1.3.73 do not support shared PCI
1478c2ecf20Sopenharmony_ciinterrupt lines.
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciIII. Driver operation
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ciIIIa. Ring buffers
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciThe Yellowfin uses the Descriptor Based DMA Architecture specified by Apple.
1548c2ecf20Sopenharmony_ciThis is a descriptor list scheme similar to that used by the EEPro100 and
1558c2ecf20Sopenharmony_ciTulip.  This driver uses two statically allocated fixed-size descriptor lists
1568c2ecf20Sopenharmony_ciformed into rings by a branch from the final descriptor to the beginning of
1578c2ecf20Sopenharmony_cithe list.  The ring sizes are set at compile time by RX/TX_RING_SIZE.
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciThe driver allocates full frame size skbuffs for the Rx ring buffers at
1608c2ecf20Sopenharmony_ciopen() time and passes the skb->data field to the Yellowfin as receive data
1618c2ecf20Sopenharmony_cibuffers.  When an incoming frame is less than RX_COPYBREAK bytes long,
1628c2ecf20Sopenharmony_cia fresh skbuff is allocated and the frame is copied to the new skbuff.
1638c2ecf20Sopenharmony_ciWhen the incoming frame is larger, the skbuff is passed directly up the
1648c2ecf20Sopenharmony_ciprotocol stack and replaced by a newly allocated skbuff.
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ciThe RX_COPYBREAK value is chosen to trade-off the memory wasted by
1678c2ecf20Sopenharmony_ciusing a full-sized skbuff for small frames vs. the copying costs of larger
1688c2ecf20Sopenharmony_ciframes.  For small frames the copying cost is negligible (esp. considering
1698c2ecf20Sopenharmony_cithat we are pre-loading the cache with immediately useful header
1708c2ecf20Sopenharmony_ciinformation).  For large frames the copying cost is non-trivial, and the
1718c2ecf20Sopenharmony_cilarger copy might flush the cache of useful data.
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ciIIIC. Synchronization
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciThe driver runs as two independent, single-threaded flows of control.  One
1768c2ecf20Sopenharmony_ciis the send-packet routine, which enforces single-threaded use by the
1778c2ecf20Sopenharmony_cidev->tbusy flag.  The other thread is the interrupt handler, which is single
1788c2ecf20Sopenharmony_cithreaded by the hardware and other software.
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciThe send packet thread has partial control over the Tx ring and 'dev->tbusy'
1818c2ecf20Sopenharmony_ciflag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
1828c2ecf20Sopenharmony_ciqueue slot is empty, it clears the tbusy flag when finished otherwise it sets
1838c2ecf20Sopenharmony_cithe 'yp->tx_full' flag.
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciThe interrupt handler has exclusive control over the Rx ring and records stats
1868c2ecf20Sopenharmony_cifrom the Tx ring.  After reaping the stats, it marks the Tx queue entry as
1878c2ecf20Sopenharmony_ciempty by incrementing the dirty_tx mark. Iff the 'yp->tx_full' flag is set, it
1888c2ecf20Sopenharmony_ciclears both the tx_full and tbusy flags.
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciIV. Notes
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciThanks to Kim Stearns of Packet Engines for providing a pair of G-NIC boards.
1938c2ecf20Sopenharmony_ciThanks to Bruce Faust of Digitalscape for providing both their SYM53C885 board
1948c2ecf20Sopenharmony_ciand an AlphaStation to verifty the Alpha port!
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciIVb. References
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciYellowfin Engineering Design Specification, 4/23/97 Preliminary/Confidential
1998c2ecf20Sopenharmony_ciSymbios SYM53C885 PCI-SCSI/Fast Ethernet Multifunction Controller Preliminary
2008c2ecf20Sopenharmony_ci   Data Manual v3.0
2018c2ecf20Sopenharmony_cihttp://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
2028c2ecf20Sopenharmony_cihttp://cesdis.gsfc.nasa.gov/linux/misc/100mbps.html
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciIVc. Errata
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciSee Packet Engines confidential appendix (prototype chips only).
2078c2ecf20Sopenharmony_ci*/
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cienum capability_flags {
2128c2ecf20Sopenharmony_ci	HasMII=1, FullTxStatus=2, IsGigabit=4, HasMulticastBug=8, FullRxStatus=16,
2138c2ecf20Sopenharmony_ci	HasMACAddrBug=32, /* Only on early revs.  */
2148c2ecf20Sopenharmony_ci	DontUseEeprom=64, /* Don't read the MAC from the EEPROm. */
2158c2ecf20Sopenharmony_ci};
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* The PCI I/O space extent. */
2188c2ecf20Sopenharmony_cienum {
2198c2ecf20Sopenharmony_ci	YELLOWFIN_SIZE	= 0x100,
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistruct pci_id_info {
2238c2ecf20Sopenharmony_ci        const char *name;
2248c2ecf20Sopenharmony_ci        struct match_info {
2258c2ecf20Sopenharmony_ci                int     pci, pci_mask, subsystem, subsystem_mask;
2268c2ecf20Sopenharmony_ci                int revision, revision_mask;                            /* Only 8 bits. */
2278c2ecf20Sopenharmony_ci        } id;
2288c2ecf20Sopenharmony_ci        int drv_flags;                          /* Driver use, intended as capability flags. */
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic const struct pci_id_info pci_id_tbl[] = {
2328c2ecf20Sopenharmony_ci	{"Yellowfin G-NIC Gigabit Ethernet", { 0x07021000, 0xffffffff},
2338c2ecf20Sopenharmony_ci	 FullTxStatus | IsGigabit | HasMulticastBug | HasMACAddrBug | DontUseEeprom},
2348c2ecf20Sopenharmony_ci	{"Symbios SYM83C885", { 0x07011000, 0xffffffff},
2358c2ecf20Sopenharmony_ci	  HasMII | DontUseEeprom },
2368c2ecf20Sopenharmony_ci	{ }
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic const struct pci_device_id yellowfin_pci_tbl[] = {
2408c2ecf20Sopenharmony_ci	{ 0x1000, 0x0702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
2418c2ecf20Sopenharmony_ci	{ 0x1000, 0x0701, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
2428c2ecf20Sopenharmony_ci	{ }
2438c2ecf20Sopenharmony_ci};
2448c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE (pci, yellowfin_pci_tbl);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* Offsets to the Yellowfin registers.  Various sizes and alignments. */
2488c2ecf20Sopenharmony_cienum yellowfin_offsets {
2498c2ecf20Sopenharmony_ci	TxCtrl=0x00, TxStatus=0x04, TxPtr=0x0C,
2508c2ecf20Sopenharmony_ci	TxIntrSel=0x10, TxBranchSel=0x14, TxWaitSel=0x18,
2518c2ecf20Sopenharmony_ci	RxCtrl=0x40, RxStatus=0x44, RxPtr=0x4C,
2528c2ecf20Sopenharmony_ci	RxIntrSel=0x50, RxBranchSel=0x54, RxWaitSel=0x58,
2538c2ecf20Sopenharmony_ci	EventStatus=0x80, IntrEnb=0x82, IntrClear=0x84, IntrStatus=0x86,
2548c2ecf20Sopenharmony_ci	ChipRev=0x8C, DMACtrl=0x90, TxThreshold=0x94,
2558c2ecf20Sopenharmony_ci	Cnfg=0xA0, FrameGap0=0xA2, FrameGap1=0xA4,
2568c2ecf20Sopenharmony_ci	MII_Cmd=0xA6, MII_Addr=0xA8, MII_Wr_Data=0xAA, MII_Rd_Data=0xAC,
2578c2ecf20Sopenharmony_ci	MII_Status=0xAE,
2588c2ecf20Sopenharmony_ci	RxDepth=0xB8, FlowCtrl=0xBC,
2598c2ecf20Sopenharmony_ci	AddrMode=0xD0, StnAddr=0xD2, HashTbl=0xD8, FIFOcfg=0xF8,
2608c2ecf20Sopenharmony_ci	EEStatus=0xF0, EECtrl=0xF1, EEAddr=0xF2, EERead=0xF3, EEWrite=0xF4,
2618c2ecf20Sopenharmony_ci	EEFeature=0xF5,
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/* The Yellowfin Rx and Tx buffer descriptors.
2658c2ecf20Sopenharmony_ci   Elements are written as 32 bit for endian portability. */
2668c2ecf20Sopenharmony_cistruct yellowfin_desc {
2678c2ecf20Sopenharmony_ci	__le32 dbdma_cmd;
2688c2ecf20Sopenharmony_ci	__le32 addr;
2698c2ecf20Sopenharmony_ci	__le32 branch_addr;
2708c2ecf20Sopenharmony_ci	__le32 result_status;
2718c2ecf20Sopenharmony_ci};
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistruct tx_status_words {
2748c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
2758c2ecf20Sopenharmony_ci	u16 tx_errs;
2768c2ecf20Sopenharmony_ci	u16 tx_cnt;
2778c2ecf20Sopenharmony_ci	u16 paused;
2788c2ecf20Sopenharmony_ci	u16 total_tx_cnt;
2798c2ecf20Sopenharmony_ci#else  /* Little endian chips. */
2808c2ecf20Sopenharmony_ci	u16 tx_cnt;
2818c2ecf20Sopenharmony_ci	u16 tx_errs;
2828c2ecf20Sopenharmony_ci	u16 total_tx_cnt;
2838c2ecf20Sopenharmony_ci	u16 paused;
2848c2ecf20Sopenharmony_ci#endif /* __BIG_ENDIAN */
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci/* Bits in yellowfin_desc.cmd */
2888c2ecf20Sopenharmony_cienum desc_cmd_bits {
2898c2ecf20Sopenharmony_ci	CMD_TX_PKT=0x10000000, CMD_RX_BUF=0x20000000, CMD_TXSTATUS=0x30000000,
2908c2ecf20Sopenharmony_ci	CMD_NOP=0x60000000, CMD_STOP=0x70000000,
2918c2ecf20Sopenharmony_ci	BRANCH_ALWAYS=0x0C0000, INTR_ALWAYS=0x300000, WAIT_ALWAYS=0x030000,
2928c2ecf20Sopenharmony_ci	BRANCH_IFTRUE=0x040000,
2938c2ecf20Sopenharmony_ci};
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci/* Bits in yellowfin_desc.status */
2968c2ecf20Sopenharmony_cienum desc_status_bits { RX_EOP=0x0040, };
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/* Bits in the interrupt status/mask registers. */
2998c2ecf20Sopenharmony_cienum intr_status_bits {
3008c2ecf20Sopenharmony_ci	IntrRxDone=0x01, IntrRxInvalid=0x02, IntrRxPCIFault=0x04,IntrRxPCIErr=0x08,
3018c2ecf20Sopenharmony_ci	IntrTxDone=0x10, IntrTxInvalid=0x20, IntrTxPCIFault=0x40,IntrTxPCIErr=0x80,
3028c2ecf20Sopenharmony_ci	IntrEarlyRx=0x100, IntrWakeup=0x200, };
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci#define PRIV_ALIGN	31 	/* Required alignment mask */
3058c2ecf20Sopenharmony_ci#define MII_CNT		4
3068c2ecf20Sopenharmony_cistruct yellowfin_private {
3078c2ecf20Sopenharmony_ci	/* Descriptor rings first for alignment.
3088c2ecf20Sopenharmony_ci	   Tx requires a second descriptor for status. */
3098c2ecf20Sopenharmony_ci	struct yellowfin_desc *rx_ring;
3108c2ecf20Sopenharmony_ci	struct yellowfin_desc *tx_ring;
3118c2ecf20Sopenharmony_ci	struct sk_buff* rx_skbuff[RX_RING_SIZE];
3128c2ecf20Sopenharmony_ci	struct sk_buff* tx_skbuff[TX_RING_SIZE];
3138c2ecf20Sopenharmony_ci	dma_addr_t rx_ring_dma;
3148c2ecf20Sopenharmony_ci	dma_addr_t tx_ring_dma;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	struct tx_status_words *tx_status;
3178c2ecf20Sopenharmony_ci	dma_addr_t tx_status_dma;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	struct timer_list timer;	/* Media selection timer. */
3208c2ecf20Sopenharmony_ci	/* Frequently used and paired value: keep adjacent for cache effect. */
3218c2ecf20Sopenharmony_ci	int chip_id, drv_flags;
3228c2ecf20Sopenharmony_ci	struct pci_dev *pci_dev;
3238c2ecf20Sopenharmony_ci	unsigned int cur_rx, dirty_rx;		/* Producer/consumer ring indices */
3248c2ecf20Sopenharmony_ci	unsigned int rx_buf_sz;				/* Based on MTU+slack. */
3258c2ecf20Sopenharmony_ci	struct tx_status_words *tx_tail_desc;
3268c2ecf20Sopenharmony_ci	unsigned int cur_tx, dirty_tx;
3278c2ecf20Sopenharmony_ci	int tx_threshold;
3288c2ecf20Sopenharmony_ci	unsigned int tx_full:1;				/* The Tx queue is full. */
3298c2ecf20Sopenharmony_ci	unsigned int full_duplex:1;			/* Full-duplex operation requested. */
3308c2ecf20Sopenharmony_ci	unsigned int duplex_lock:1;
3318c2ecf20Sopenharmony_ci	unsigned int medialock:1;			/* Do not sense media. */
3328c2ecf20Sopenharmony_ci	unsigned int default_port:4;		/* Last dev->if_port value. */
3338c2ecf20Sopenharmony_ci	/* MII transceiver section. */
3348c2ecf20Sopenharmony_ci	int mii_cnt;						/* MII device addresses. */
3358c2ecf20Sopenharmony_ci	u16 advertising;					/* NWay media advertisement */
3368c2ecf20Sopenharmony_ci	unsigned char phys[MII_CNT];		/* MII device addresses, only first one used */
3378c2ecf20Sopenharmony_ci	spinlock_t lock;
3388c2ecf20Sopenharmony_ci	void __iomem *base;
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic int read_eeprom(void __iomem *ioaddr, int location);
3428c2ecf20Sopenharmony_cistatic int mdio_read(void __iomem *ioaddr, int phy_id, int location);
3438c2ecf20Sopenharmony_cistatic void mdio_write(void __iomem *ioaddr, int phy_id, int location, int value);
3448c2ecf20Sopenharmony_cistatic int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
3458c2ecf20Sopenharmony_cistatic int yellowfin_open(struct net_device *dev);
3468c2ecf20Sopenharmony_cistatic void yellowfin_timer(struct timer_list *t);
3478c2ecf20Sopenharmony_cistatic void yellowfin_tx_timeout(struct net_device *dev, unsigned int txqueue);
3488c2ecf20Sopenharmony_cistatic int yellowfin_init_ring(struct net_device *dev);
3498c2ecf20Sopenharmony_cistatic netdev_tx_t yellowfin_start_xmit(struct sk_buff *skb,
3508c2ecf20Sopenharmony_ci					struct net_device *dev);
3518c2ecf20Sopenharmony_cistatic irqreturn_t yellowfin_interrupt(int irq, void *dev_instance);
3528c2ecf20Sopenharmony_cistatic int yellowfin_rx(struct net_device *dev);
3538c2ecf20Sopenharmony_cistatic void yellowfin_error(struct net_device *dev, int intr_status);
3548c2ecf20Sopenharmony_cistatic int yellowfin_close(struct net_device *dev);
3558c2ecf20Sopenharmony_cistatic void set_rx_mode(struct net_device *dev);
3568c2ecf20Sopenharmony_cistatic const struct ethtool_ops ethtool_ops;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic const struct net_device_ops netdev_ops = {
3598c2ecf20Sopenharmony_ci	.ndo_open 		= yellowfin_open,
3608c2ecf20Sopenharmony_ci	.ndo_stop 		= yellowfin_close,
3618c2ecf20Sopenharmony_ci	.ndo_start_xmit 	= yellowfin_start_xmit,
3628c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= set_rx_mode,
3638c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
3648c2ecf20Sopenharmony_ci	.ndo_set_mac_address 	= eth_mac_addr,
3658c2ecf20Sopenharmony_ci	.ndo_do_ioctl 		= netdev_ioctl,
3668c2ecf20Sopenharmony_ci	.ndo_tx_timeout 	= yellowfin_tx_timeout,
3678c2ecf20Sopenharmony_ci};
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic int yellowfin_init_one(struct pci_dev *pdev,
3708c2ecf20Sopenharmony_ci			      const struct pci_device_id *ent)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct net_device *dev;
3738c2ecf20Sopenharmony_ci	struct yellowfin_private *np;
3748c2ecf20Sopenharmony_ci	int irq;
3758c2ecf20Sopenharmony_ci	int chip_idx = ent->driver_data;
3768c2ecf20Sopenharmony_ci	static int find_cnt;
3778c2ecf20Sopenharmony_ci	void __iomem *ioaddr;
3788c2ecf20Sopenharmony_ci	int i, option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
3798c2ecf20Sopenharmony_ci	int drv_flags = pci_id_tbl[chip_idx].drv_flags;
3808c2ecf20Sopenharmony_ci        void *ring_space;
3818c2ecf20Sopenharmony_ci        dma_addr_t ring_dma;
3828c2ecf20Sopenharmony_ci#ifdef USE_IO_OPS
3838c2ecf20Sopenharmony_ci	int bar = 0;
3848c2ecf20Sopenharmony_ci#else
3858c2ecf20Sopenharmony_ci	int bar = 1;
3868c2ecf20Sopenharmony_ci#endif
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/* when built into the kernel, we only print version if device is found */
3898c2ecf20Sopenharmony_ci#ifndef MODULE
3908c2ecf20Sopenharmony_ci	static int printed_version;
3918c2ecf20Sopenharmony_ci	if (!printed_version++)
3928c2ecf20Sopenharmony_ci		printk(version);
3938c2ecf20Sopenharmony_ci#endif
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	i = pci_enable_device(pdev);
3968c2ecf20Sopenharmony_ci	if (i) return i;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	dev = alloc_etherdev(sizeof(*np));
3998c2ecf20Sopenharmony_ci	if (!dev)
4008c2ecf20Sopenharmony_ci		return -ENOMEM;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(dev, &pdev->dev);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	np = netdev_priv(dev);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	if (pci_request_regions(pdev, DRV_NAME))
4078c2ecf20Sopenharmony_ci		goto err_out_free_netdev;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	pci_set_master (pdev);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	ioaddr = pci_iomap(pdev, bar, YELLOWFIN_SIZE);
4128c2ecf20Sopenharmony_ci	if (!ioaddr)
4138c2ecf20Sopenharmony_ci		goto err_out_free_res;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	irq = pdev->irq;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (drv_flags & DontUseEeprom)
4188c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++)
4198c2ecf20Sopenharmony_ci			dev->dev_addr[i] = ioread8(ioaddr + StnAddr + i);
4208c2ecf20Sopenharmony_ci	else {
4218c2ecf20Sopenharmony_ci		int ee_offset = (read_eeprom(ioaddr, 6) == 0xff ? 0x100 : 0);
4228c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++)
4238c2ecf20Sopenharmony_ci			dev->dev_addr[i] = read_eeprom(ioaddr, ee_offset + i);
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/* Reset the chip. */
4278c2ecf20Sopenharmony_ci	iowrite32(0x80000000, ioaddr + DMACtrl);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, dev);
4308c2ecf20Sopenharmony_ci	spin_lock_init(&np->lock);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	np->pci_dev = pdev;
4338c2ecf20Sopenharmony_ci	np->chip_id = chip_idx;
4348c2ecf20Sopenharmony_ci	np->drv_flags = drv_flags;
4358c2ecf20Sopenharmony_ci	np->base = ioaddr;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	ring_space = dma_alloc_coherent(&pdev->dev, TX_TOTAL_SIZE, &ring_dma,
4388c2ecf20Sopenharmony_ci					GFP_KERNEL);
4398c2ecf20Sopenharmony_ci	if (!ring_space)
4408c2ecf20Sopenharmony_ci		goto err_out_cleardev;
4418c2ecf20Sopenharmony_ci	np->tx_ring = ring_space;
4428c2ecf20Sopenharmony_ci	np->tx_ring_dma = ring_dma;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	ring_space = dma_alloc_coherent(&pdev->dev, RX_TOTAL_SIZE, &ring_dma,
4458c2ecf20Sopenharmony_ci					GFP_KERNEL);
4468c2ecf20Sopenharmony_ci	if (!ring_space)
4478c2ecf20Sopenharmony_ci		goto err_out_unmap_tx;
4488c2ecf20Sopenharmony_ci	np->rx_ring = ring_space;
4498c2ecf20Sopenharmony_ci	np->rx_ring_dma = ring_dma;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	ring_space = dma_alloc_coherent(&pdev->dev, STATUS_TOTAL_SIZE,
4528c2ecf20Sopenharmony_ci					&ring_dma, GFP_KERNEL);
4538c2ecf20Sopenharmony_ci	if (!ring_space)
4548c2ecf20Sopenharmony_ci		goto err_out_unmap_rx;
4558c2ecf20Sopenharmony_ci	np->tx_status = ring_space;
4568c2ecf20Sopenharmony_ci	np->tx_status_dma = ring_dma;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if (dev->mem_start)
4598c2ecf20Sopenharmony_ci		option = dev->mem_start;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	/* The lower four bits are the media type. */
4628c2ecf20Sopenharmony_ci	if (option > 0) {
4638c2ecf20Sopenharmony_ci		if (option & 0x200)
4648c2ecf20Sopenharmony_ci			np->full_duplex = 1;
4658c2ecf20Sopenharmony_ci		np->default_port = option & 15;
4668c2ecf20Sopenharmony_ci		if (np->default_port)
4678c2ecf20Sopenharmony_ci			np->medialock = 1;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci	if (find_cnt < MAX_UNITS  &&  full_duplex[find_cnt] > 0)
4708c2ecf20Sopenharmony_ci		np->full_duplex = 1;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	if (np->full_duplex)
4738c2ecf20Sopenharmony_ci		np->duplex_lock = 1;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* The Yellowfin-specific entries in the device structure. */
4768c2ecf20Sopenharmony_ci	dev->netdev_ops = &netdev_ops;
4778c2ecf20Sopenharmony_ci	dev->ethtool_ops = &ethtool_ops;
4788c2ecf20Sopenharmony_ci	dev->watchdog_timeo = TX_TIMEOUT;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	if (mtu)
4818c2ecf20Sopenharmony_ci		dev->mtu = mtu;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	i = register_netdev(dev);
4848c2ecf20Sopenharmony_ci	if (i)
4858c2ecf20Sopenharmony_ci		goto err_out_unmap_status;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	netdev_info(dev, "%s type %8x at %p, %pM, IRQ %d\n",
4888c2ecf20Sopenharmony_ci		    pci_id_tbl[chip_idx].name,
4898c2ecf20Sopenharmony_ci		    ioread32(ioaddr + ChipRev), ioaddr,
4908c2ecf20Sopenharmony_ci		    dev->dev_addr, irq);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	if (np->drv_flags & HasMII) {
4938c2ecf20Sopenharmony_ci		int phy, phy_idx = 0;
4948c2ecf20Sopenharmony_ci		for (phy = 0; phy < 32 && phy_idx < MII_CNT; phy++) {
4958c2ecf20Sopenharmony_ci			int mii_status = mdio_read(ioaddr, phy, 1);
4968c2ecf20Sopenharmony_ci			if (mii_status != 0xffff  &&  mii_status != 0x0000) {
4978c2ecf20Sopenharmony_ci				np->phys[phy_idx++] = phy;
4988c2ecf20Sopenharmony_ci				np->advertising = mdio_read(ioaddr, phy, 4);
4998c2ecf20Sopenharmony_ci				netdev_info(dev, "MII PHY found at address %d, status 0x%04x advertising %04x\n",
5008c2ecf20Sopenharmony_ci					    phy, mii_status, np->advertising);
5018c2ecf20Sopenharmony_ci			}
5028c2ecf20Sopenharmony_ci		}
5038c2ecf20Sopenharmony_ci		np->mii_cnt = phy_idx;
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	find_cnt++;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	return 0;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cierr_out_unmap_status:
5118c2ecf20Sopenharmony_ci	dma_free_coherent(&pdev->dev, STATUS_TOTAL_SIZE, np->tx_status,
5128c2ecf20Sopenharmony_ci			  np->tx_status_dma);
5138c2ecf20Sopenharmony_cierr_out_unmap_rx:
5148c2ecf20Sopenharmony_ci	dma_free_coherent(&pdev->dev, RX_TOTAL_SIZE, np->rx_ring,
5158c2ecf20Sopenharmony_ci			  np->rx_ring_dma);
5168c2ecf20Sopenharmony_cierr_out_unmap_tx:
5178c2ecf20Sopenharmony_ci	dma_free_coherent(&pdev->dev, TX_TOTAL_SIZE, np->tx_ring,
5188c2ecf20Sopenharmony_ci			  np->tx_ring_dma);
5198c2ecf20Sopenharmony_cierr_out_cleardev:
5208c2ecf20Sopenharmony_ci	pci_iounmap(pdev, ioaddr);
5218c2ecf20Sopenharmony_cierr_out_free_res:
5228c2ecf20Sopenharmony_ci	pci_release_regions(pdev);
5238c2ecf20Sopenharmony_cierr_out_free_netdev:
5248c2ecf20Sopenharmony_ci	free_netdev (dev);
5258c2ecf20Sopenharmony_ci	return -ENODEV;
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic int read_eeprom(void __iomem *ioaddr, int location)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	int bogus_cnt = 10000;		/* Typical 33Mhz: 1050 ticks */
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	iowrite8(location, ioaddr + EEAddr);
5338c2ecf20Sopenharmony_ci	iowrite8(0x30 | ((location >> 8) & 7), ioaddr + EECtrl);
5348c2ecf20Sopenharmony_ci	while ((ioread8(ioaddr + EEStatus) & 0x80)  &&  --bogus_cnt > 0)
5358c2ecf20Sopenharmony_ci		;
5368c2ecf20Sopenharmony_ci	return ioread8(ioaddr + EERead);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci/* MII Managemen Data I/O accesses.
5408c2ecf20Sopenharmony_ci   These routines assume the MDIO controller is idle, and do not exit until
5418c2ecf20Sopenharmony_ci   the command is finished. */
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic int mdio_read(void __iomem *ioaddr, int phy_id, int location)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	int i;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	iowrite16((phy_id<<8) + location, ioaddr + MII_Addr);
5488c2ecf20Sopenharmony_ci	iowrite16(1, ioaddr + MII_Cmd);
5498c2ecf20Sopenharmony_ci	for (i = 10000; i >= 0; i--)
5508c2ecf20Sopenharmony_ci		if ((ioread16(ioaddr + MII_Status) & 1) == 0)
5518c2ecf20Sopenharmony_ci			break;
5528c2ecf20Sopenharmony_ci	return ioread16(ioaddr + MII_Rd_Data);
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic void mdio_write(void __iomem *ioaddr, int phy_id, int location, int value)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	int i;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	iowrite16((phy_id<<8) + location, ioaddr + MII_Addr);
5608c2ecf20Sopenharmony_ci	iowrite16(value, ioaddr + MII_Wr_Data);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/* Wait for the command to finish. */
5638c2ecf20Sopenharmony_ci	for (i = 10000; i >= 0; i--)
5648c2ecf20Sopenharmony_ci		if ((ioread16(ioaddr + MII_Status) & 1) == 0)
5658c2ecf20Sopenharmony_ci			break;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic int yellowfin_open(struct net_device *dev)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
5728c2ecf20Sopenharmony_ci	const int irq = yp->pci_dev->irq;
5738c2ecf20Sopenharmony_ci	void __iomem *ioaddr = yp->base;
5748c2ecf20Sopenharmony_ci	int i, rc;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	/* Reset the chip. */
5778c2ecf20Sopenharmony_ci	iowrite32(0x80000000, ioaddr + DMACtrl);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	rc = request_irq(irq, yellowfin_interrupt, IRQF_SHARED, dev->name, dev);
5808c2ecf20Sopenharmony_ci	if (rc)
5818c2ecf20Sopenharmony_ci		return rc;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	rc = yellowfin_init_ring(dev);
5848c2ecf20Sopenharmony_ci	if (rc < 0)
5858c2ecf20Sopenharmony_ci		goto err_free_irq;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	iowrite32(yp->rx_ring_dma, ioaddr + RxPtr);
5888c2ecf20Sopenharmony_ci	iowrite32(yp->tx_ring_dma, ioaddr + TxPtr);
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++)
5918c2ecf20Sopenharmony_ci		iowrite8(dev->dev_addr[i], ioaddr + StnAddr + i);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/* Set up various condition 'select' registers.
5948c2ecf20Sopenharmony_ci	   There are no options here. */
5958c2ecf20Sopenharmony_ci	iowrite32(0x00800080, ioaddr + TxIntrSel); 	/* Interrupt on Tx abort */
5968c2ecf20Sopenharmony_ci	iowrite32(0x00800080, ioaddr + TxBranchSel);	/* Branch on Tx abort */
5978c2ecf20Sopenharmony_ci	iowrite32(0x00400040, ioaddr + TxWaitSel); 	/* Wait on Tx status */
5988c2ecf20Sopenharmony_ci	iowrite32(0x00400040, ioaddr + RxIntrSel);	/* Interrupt on Rx done */
5998c2ecf20Sopenharmony_ci	iowrite32(0x00400040, ioaddr + RxBranchSel);	/* Branch on Rx error */
6008c2ecf20Sopenharmony_ci	iowrite32(0x00400040, ioaddr + RxWaitSel);	/* Wait on Rx done */
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/* Initialize other registers: with so many this eventually this will
6038c2ecf20Sopenharmony_ci	   converted to an offset/value list. */
6048c2ecf20Sopenharmony_ci	iowrite32(dma_ctrl, ioaddr + DMACtrl);
6058c2ecf20Sopenharmony_ci	iowrite16(fifo_cfg, ioaddr + FIFOcfg);
6068c2ecf20Sopenharmony_ci	/* Enable automatic generation of flow control frames, period 0xffff. */
6078c2ecf20Sopenharmony_ci	iowrite32(0x0030FFFF, ioaddr + FlowCtrl);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	yp->tx_threshold = 32;
6108c2ecf20Sopenharmony_ci	iowrite32(yp->tx_threshold, ioaddr + TxThreshold);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (dev->if_port == 0)
6138c2ecf20Sopenharmony_ci		dev->if_port = yp->default_port;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	netif_start_queue(dev);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* Setting the Rx mode will start the Rx process. */
6188c2ecf20Sopenharmony_ci	if (yp->drv_flags & IsGigabit) {
6198c2ecf20Sopenharmony_ci		/* We are always in full-duplex mode with gigabit! */
6208c2ecf20Sopenharmony_ci		yp->full_duplex = 1;
6218c2ecf20Sopenharmony_ci		iowrite16(0x01CF, ioaddr + Cnfg);
6228c2ecf20Sopenharmony_ci	} else {
6238c2ecf20Sopenharmony_ci		iowrite16(0x0018, ioaddr + FrameGap0); /* 0060/4060 for non-MII 10baseT */
6248c2ecf20Sopenharmony_ci		iowrite16(0x1018, ioaddr + FrameGap1);
6258c2ecf20Sopenharmony_ci		iowrite16(0x101C | (yp->full_duplex ? 2 : 0), ioaddr + Cnfg);
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci	set_rx_mode(dev);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	/* Enable interrupts by setting the interrupt mask. */
6308c2ecf20Sopenharmony_ci	iowrite16(0x81ff, ioaddr + IntrEnb);			/* See enum intr_status_bits */
6318c2ecf20Sopenharmony_ci	iowrite16(0x0000, ioaddr + EventStatus);		/* Clear non-interrupting events */
6328c2ecf20Sopenharmony_ci	iowrite32(0x80008000, ioaddr + RxCtrl);		/* Start Rx and Tx channels. */
6338c2ecf20Sopenharmony_ci	iowrite32(0x80008000, ioaddr + TxCtrl);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (yellowfin_debug > 2) {
6368c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "Done %s()\n", __func__);
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* Set the timer to check for link beat. */
6408c2ecf20Sopenharmony_ci	timer_setup(&yp->timer, yellowfin_timer, 0);
6418c2ecf20Sopenharmony_ci	yp->timer.expires = jiffies + 3*HZ;
6428c2ecf20Sopenharmony_ci	add_timer(&yp->timer);
6438c2ecf20Sopenharmony_ciout:
6448c2ecf20Sopenharmony_ci	return rc;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cierr_free_irq:
6478c2ecf20Sopenharmony_ci	free_irq(irq, dev);
6488c2ecf20Sopenharmony_ci	goto out;
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic void yellowfin_timer(struct timer_list *t)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = from_timer(yp, t, timer);
6548c2ecf20Sopenharmony_ci	struct net_device *dev = pci_get_drvdata(yp->pci_dev);
6558c2ecf20Sopenharmony_ci	void __iomem *ioaddr = yp->base;
6568c2ecf20Sopenharmony_ci	int next_tick = 60*HZ;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	if (yellowfin_debug > 3) {
6598c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "Yellowfin timer tick, status %08x\n",
6608c2ecf20Sopenharmony_ci			      ioread16(ioaddr + IntrStatus));
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	if (yp->mii_cnt) {
6648c2ecf20Sopenharmony_ci		int bmsr = mdio_read(ioaddr, yp->phys[0], MII_BMSR);
6658c2ecf20Sopenharmony_ci		int lpa = mdio_read(ioaddr, yp->phys[0], MII_LPA);
6668c2ecf20Sopenharmony_ci		int negotiated = lpa & yp->advertising;
6678c2ecf20Sopenharmony_ci		if (yellowfin_debug > 1)
6688c2ecf20Sopenharmony_ci			netdev_printk(KERN_DEBUG, dev, "MII #%d status register is %04x, link partner capability %04x\n",
6698c2ecf20Sopenharmony_ci				      yp->phys[0], bmsr, lpa);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci		yp->full_duplex = mii_duplex(yp->duplex_lock, negotiated);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci		iowrite16(0x101C | (yp->full_duplex ? 2 : 0), ioaddr + Cnfg);
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci		if (bmsr & BMSR_LSTATUS)
6768c2ecf20Sopenharmony_ci			next_tick = 60*HZ;
6778c2ecf20Sopenharmony_ci		else
6788c2ecf20Sopenharmony_ci			next_tick = 3*HZ;
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	yp->timer.expires = jiffies + next_tick;
6828c2ecf20Sopenharmony_ci	add_timer(&yp->timer);
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic void yellowfin_tx_timeout(struct net_device *dev, unsigned int txqueue)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
6888c2ecf20Sopenharmony_ci	void __iomem *ioaddr = yp->base;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	netdev_warn(dev, "Yellowfin transmit timed out at %d/%d Tx status %04x, Rx status %04x, resetting...\n",
6918c2ecf20Sopenharmony_ci		    yp->cur_tx, yp->dirty_tx,
6928c2ecf20Sopenharmony_ci		    ioread32(ioaddr + TxStatus),
6938c2ecf20Sopenharmony_ci		    ioread32(ioaddr + RxStatus));
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	/* Note: these should be KERN_DEBUG. */
6968c2ecf20Sopenharmony_ci	if (yellowfin_debug) {
6978c2ecf20Sopenharmony_ci		int i;
6988c2ecf20Sopenharmony_ci		pr_warn("  Rx ring %p: ", yp->rx_ring);
6998c2ecf20Sopenharmony_ci		for (i = 0; i < RX_RING_SIZE; i++)
7008c2ecf20Sopenharmony_ci			pr_cont(" %08x", yp->rx_ring[i].result_status);
7018c2ecf20Sopenharmony_ci		pr_cont("\n");
7028c2ecf20Sopenharmony_ci		pr_warn("  Tx ring %p: ", yp->tx_ring);
7038c2ecf20Sopenharmony_ci		for (i = 0; i < TX_RING_SIZE; i++)
7048c2ecf20Sopenharmony_ci			pr_cont(" %04x /%08x",
7058c2ecf20Sopenharmony_ci			       yp->tx_status[i].tx_errs,
7068c2ecf20Sopenharmony_ci			       yp->tx_ring[i].result_status);
7078c2ecf20Sopenharmony_ci		pr_cont("\n");
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* If the hardware is found to hang regularly, we will update the code
7118c2ecf20Sopenharmony_ci	   to reinitialize the chip here. */
7128c2ecf20Sopenharmony_ci	dev->if_port = 0;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	/* Wake the potentially-idle transmit channel. */
7158c2ecf20Sopenharmony_ci	iowrite32(0x10001000, yp->base + TxCtrl);
7168c2ecf20Sopenharmony_ci	if (yp->cur_tx - yp->dirty_tx < TX_QUEUE_SIZE)
7178c2ecf20Sopenharmony_ci		netif_wake_queue (dev);		/* Typical path */
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	netif_trans_update(dev); /* prevent tx timeout */
7208c2ecf20Sopenharmony_ci	dev->stats.tx_errors++;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
7248c2ecf20Sopenharmony_cistatic int yellowfin_init_ring(struct net_device *dev)
7258c2ecf20Sopenharmony_ci{
7268c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
7278c2ecf20Sopenharmony_ci	int i, j;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	yp->tx_full = 0;
7308c2ecf20Sopenharmony_ci	yp->cur_rx = yp->cur_tx = 0;
7318c2ecf20Sopenharmony_ci	yp->dirty_tx = 0;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	yp->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
7368c2ecf20Sopenharmony_ci		yp->rx_ring[i].dbdma_cmd =
7378c2ecf20Sopenharmony_ci			cpu_to_le32(CMD_RX_BUF | INTR_ALWAYS | yp->rx_buf_sz);
7388c2ecf20Sopenharmony_ci		yp->rx_ring[i].branch_addr = cpu_to_le32(yp->rx_ring_dma +
7398c2ecf20Sopenharmony_ci			((i+1)%RX_RING_SIZE)*sizeof(struct yellowfin_desc));
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
7438c2ecf20Sopenharmony_ci		struct sk_buff *skb = netdev_alloc_skb(dev, yp->rx_buf_sz + 2);
7448c2ecf20Sopenharmony_ci		yp->rx_skbuff[i] = skb;
7458c2ecf20Sopenharmony_ci		if (skb == NULL)
7468c2ecf20Sopenharmony_ci			break;
7478c2ecf20Sopenharmony_ci		skb_reserve(skb, 2);	/* 16 byte align the IP header. */
7488c2ecf20Sopenharmony_ci		yp->rx_ring[i].addr = cpu_to_le32(dma_map_single(&yp->pci_dev->dev,
7498c2ecf20Sopenharmony_ci								 skb->data,
7508c2ecf20Sopenharmony_ci								 yp->rx_buf_sz,
7518c2ecf20Sopenharmony_ci								 DMA_FROM_DEVICE));
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ci	if (i != RX_RING_SIZE) {
7548c2ecf20Sopenharmony_ci		for (j = 0; j < i; j++)
7558c2ecf20Sopenharmony_ci			dev_kfree_skb(yp->rx_skbuff[j]);
7568c2ecf20Sopenharmony_ci		return -ENOMEM;
7578c2ecf20Sopenharmony_ci	}
7588c2ecf20Sopenharmony_ci	yp->rx_ring[i-1].dbdma_cmd = cpu_to_le32(CMD_STOP);
7598c2ecf20Sopenharmony_ci	yp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci#define NO_TXSTATS
7628c2ecf20Sopenharmony_ci#ifdef NO_TXSTATS
7638c2ecf20Sopenharmony_ci	/* In this mode the Tx ring needs only a single descriptor. */
7648c2ecf20Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++) {
7658c2ecf20Sopenharmony_ci		yp->tx_skbuff[i] = NULL;
7668c2ecf20Sopenharmony_ci		yp->tx_ring[i].dbdma_cmd = cpu_to_le32(CMD_STOP);
7678c2ecf20Sopenharmony_ci		yp->tx_ring[i].branch_addr = cpu_to_le32(yp->tx_ring_dma +
7688c2ecf20Sopenharmony_ci			((i+1)%TX_RING_SIZE)*sizeof(struct yellowfin_desc));
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci	/* Wrap ring */
7718c2ecf20Sopenharmony_ci	yp->tx_ring[--i].dbdma_cmd = cpu_to_le32(CMD_STOP | BRANCH_ALWAYS);
7728c2ecf20Sopenharmony_ci#else
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	/* Tx ring needs a pair of descriptors, the second for the status. */
7758c2ecf20Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++) {
7768c2ecf20Sopenharmony_ci		j = 2*i;
7778c2ecf20Sopenharmony_ci		yp->tx_skbuff[i] = 0;
7788c2ecf20Sopenharmony_ci		/* Branch on Tx error. */
7798c2ecf20Sopenharmony_ci		yp->tx_ring[j].dbdma_cmd = cpu_to_le32(CMD_STOP);
7808c2ecf20Sopenharmony_ci		yp->tx_ring[j].branch_addr = cpu_to_le32(yp->tx_ring_dma +
7818c2ecf20Sopenharmony_ci			(j+1)*sizeof(struct yellowfin_desc));
7828c2ecf20Sopenharmony_ci		j++;
7838c2ecf20Sopenharmony_ci		if (yp->flags & FullTxStatus) {
7848c2ecf20Sopenharmony_ci			yp->tx_ring[j].dbdma_cmd =
7858c2ecf20Sopenharmony_ci				cpu_to_le32(CMD_TXSTATUS | sizeof(*yp->tx_status));
7868c2ecf20Sopenharmony_ci			yp->tx_ring[j].request_cnt = sizeof(*yp->tx_status);
7878c2ecf20Sopenharmony_ci			yp->tx_ring[j].addr = cpu_to_le32(yp->tx_status_dma +
7888c2ecf20Sopenharmony_ci				i*sizeof(struct tx_status_words));
7898c2ecf20Sopenharmony_ci		} else {
7908c2ecf20Sopenharmony_ci			/* Symbios chips write only tx_errs word. */
7918c2ecf20Sopenharmony_ci			yp->tx_ring[j].dbdma_cmd =
7928c2ecf20Sopenharmony_ci				cpu_to_le32(CMD_TXSTATUS | INTR_ALWAYS | 2);
7938c2ecf20Sopenharmony_ci			yp->tx_ring[j].request_cnt = 2;
7948c2ecf20Sopenharmony_ci			/* Om pade ummmmm... */
7958c2ecf20Sopenharmony_ci			yp->tx_ring[j].addr = cpu_to_le32(yp->tx_status_dma +
7968c2ecf20Sopenharmony_ci				i*sizeof(struct tx_status_words) +
7978c2ecf20Sopenharmony_ci				&(yp->tx_status[0].tx_errs) -
7988c2ecf20Sopenharmony_ci				&(yp->tx_status[0]));
7998c2ecf20Sopenharmony_ci		}
8008c2ecf20Sopenharmony_ci		yp->tx_ring[j].branch_addr = cpu_to_le32(yp->tx_ring_dma +
8018c2ecf20Sopenharmony_ci			((j+1)%(2*TX_RING_SIZE))*sizeof(struct yellowfin_desc));
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci	/* Wrap ring */
8048c2ecf20Sopenharmony_ci	yp->tx_ring[++j].dbdma_cmd |= cpu_to_le32(BRANCH_ALWAYS | INTR_ALWAYS);
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci#endif
8078c2ecf20Sopenharmony_ci	yp->tx_tail_desc = &yp->tx_status[0];
8088c2ecf20Sopenharmony_ci	return 0;
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic netdev_tx_t yellowfin_start_xmit(struct sk_buff *skb,
8128c2ecf20Sopenharmony_ci					struct net_device *dev)
8138c2ecf20Sopenharmony_ci{
8148c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
8158c2ecf20Sopenharmony_ci	unsigned entry;
8168c2ecf20Sopenharmony_ci	int len = skb->len;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	netif_stop_queue (dev);
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	/* Note: Ordering is important here, set the field with the
8218c2ecf20Sopenharmony_ci	   "ownership" bit last, and only then increment cur_tx. */
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	/* Calculate the next Tx descriptor entry. */
8248c2ecf20Sopenharmony_ci	entry = yp->cur_tx % TX_RING_SIZE;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	if (gx_fix) {	/* Note: only works for paddable protocols e.g.  IP. */
8278c2ecf20Sopenharmony_ci		int cacheline_end = ((unsigned long)skb->data + skb->len) % 32;
8288c2ecf20Sopenharmony_ci		/* Fix GX chipset errata. */
8298c2ecf20Sopenharmony_ci		if (cacheline_end > 24  || cacheline_end == 0) {
8308c2ecf20Sopenharmony_ci			len = skb->len + 32 - cacheline_end + 1;
8318c2ecf20Sopenharmony_ci			if (skb_padto(skb, len)) {
8328c2ecf20Sopenharmony_ci				yp->tx_skbuff[entry] = NULL;
8338c2ecf20Sopenharmony_ci				netif_wake_queue(dev);
8348c2ecf20Sopenharmony_ci				return NETDEV_TX_OK;
8358c2ecf20Sopenharmony_ci			}
8368c2ecf20Sopenharmony_ci		}
8378c2ecf20Sopenharmony_ci	}
8388c2ecf20Sopenharmony_ci	yp->tx_skbuff[entry] = skb;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci#ifdef NO_TXSTATS
8418c2ecf20Sopenharmony_ci	yp->tx_ring[entry].addr = cpu_to_le32(dma_map_single(&yp->pci_dev->dev,
8428c2ecf20Sopenharmony_ci							     skb->data,
8438c2ecf20Sopenharmony_ci							     len, DMA_TO_DEVICE));
8448c2ecf20Sopenharmony_ci	yp->tx_ring[entry].result_status = 0;
8458c2ecf20Sopenharmony_ci	if (entry >= TX_RING_SIZE-1) {
8468c2ecf20Sopenharmony_ci		/* New stop command. */
8478c2ecf20Sopenharmony_ci		yp->tx_ring[0].dbdma_cmd = cpu_to_le32(CMD_STOP);
8488c2ecf20Sopenharmony_ci		yp->tx_ring[TX_RING_SIZE-1].dbdma_cmd =
8498c2ecf20Sopenharmony_ci			cpu_to_le32(CMD_TX_PKT|BRANCH_ALWAYS | len);
8508c2ecf20Sopenharmony_ci	} else {
8518c2ecf20Sopenharmony_ci		yp->tx_ring[entry+1].dbdma_cmd = cpu_to_le32(CMD_STOP);
8528c2ecf20Sopenharmony_ci		yp->tx_ring[entry].dbdma_cmd =
8538c2ecf20Sopenharmony_ci			cpu_to_le32(CMD_TX_PKT | BRANCH_IFTRUE | len);
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci	yp->cur_tx++;
8568c2ecf20Sopenharmony_ci#else
8578c2ecf20Sopenharmony_ci	yp->tx_ring[entry<<1].request_cnt = len;
8588c2ecf20Sopenharmony_ci	yp->tx_ring[entry<<1].addr = cpu_to_le32(dma_map_single(&yp->pci_dev->dev,
8598c2ecf20Sopenharmony_ci								skb->data,
8608c2ecf20Sopenharmony_ci								len, DMA_TO_DEVICE));
8618c2ecf20Sopenharmony_ci	/* The input_last (status-write) command is constant, but we must
8628c2ecf20Sopenharmony_ci	   rewrite the subsequent 'stop' command. */
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	yp->cur_tx++;
8658c2ecf20Sopenharmony_ci	{
8668c2ecf20Sopenharmony_ci		unsigned next_entry = yp->cur_tx % TX_RING_SIZE;
8678c2ecf20Sopenharmony_ci		yp->tx_ring[next_entry<<1].dbdma_cmd = cpu_to_le32(CMD_STOP);
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci	/* Final step -- overwrite the old 'stop' command. */
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	yp->tx_ring[entry<<1].dbdma_cmd =
8728c2ecf20Sopenharmony_ci		cpu_to_le32( ((entry % 6) == 0 ? CMD_TX_PKT|INTR_ALWAYS|BRANCH_IFTRUE :
8738c2ecf20Sopenharmony_ci					  CMD_TX_PKT | BRANCH_IFTRUE) | len);
8748c2ecf20Sopenharmony_ci#endif
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	/* Non-x86 Todo: explicitly flush cache lines here. */
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	/* Wake the potentially-idle transmit channel. */
8798c2ecf20Sopenharmony_ci	iowrite32(0x10001000, yp->base + TxCtrl);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (yp->cur_tx - yp->dirty_tx < TX_QUEUE_SIZE)
8828c2ecf20Sopenharmony_ci		netif_start_queue (dev);		/* Typical path */
8838c2ecf20Sopenharmony_ci	else
8848c2ecf20Sopenharmony_ci		yp->tx_full = 1;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (yellowfin_debug > 4) {
8878c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "Yellowfin transmit frame #%d queued in slot %d\n",
8888c2ecf20Sopenharmony_ci			      yp->cur_tx, entry);
8898c2ecf20Sopenharmony_ci	}
8908c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci/* The interrupt handler does all of the Rx thread work and cleans up
8948c2ecf20Sopenharmony_ci   after the Tx thread. */
8958c2ecf20Sopenharmony_cistatic irqreturn_t yellowfin_interrupt(int irq, void *dev_instance)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	struct net_device *dev = dev_instance;
8988c2ecf20Sopenharmony_ci	struct yellowfin_private *yp;
8998c2ecf20Sopenharmony_ci	void __iomem *ioaddr;
9008c2ecf20Sopenharmony_ci	int boguscnt = max_interrupt_work;
9018c2ecf20Sopenharmony_ci	unsigned int handled = 0;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	yp = netdev_priv(dev);
9048c2ecf20Sopenharmony_ci	ioaddr = yp->base;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	spin_lock (&yp->lock);
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	do {
9098c2ecf20Sopenharmony_ci		u16 intr_status = ioread16(ioaddr + IntrClear);
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci		if (yellowfin_debug > 4)
9128c2ecf20Sopenharmony_ci			netdev_printk(KERN_DEBUG, dev, "Yellowfin interrupt, status %04x\n",
9138c2ecf20Sopenharmony_ci				      intr_status);
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci		if (intr_status == 0)
9168c2ecf20Sopenharmony_ci			break;
9178c2ecf20Sopenharmony_ci		handled = 1;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci		if (intr_status & (IntrRxDone | IntrEarlyRx)) {
9208c2ecf20Sopenharmony_ci			yellowfin_rx(dev);
9218c2ecf20Sopenharmony_ci			iowrite32(0x10001000, ioaddr + RxCtrl);		/* Wake Rx engine. */
9228c2ecf20Sopenharmony_ci		}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci#ifdef NO_TXSTATS
9258c2ecf20Sopenharmony_ci		for (; yp->cur_tx - yp->dirty_tx > 0; yp->dirty_tx++) {
9268c2ecf20Sopenharmony_ci			int entry = yp->dirty_tx % TX_RING_SIZE;
9278c2ecf20Sopenharmony_ci			struct sk_buff *skb;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci			if (yp->tx_ring[entry].result_status == 0)
9308c2ecf20Sopenharmony_ci				break;
9318c2ecf20Sopenharmony_ci			skb = yp->tx_skbuff[entry];
9328c2ecf20Sopenharmony_ci			dev->stats.tx_packets++;
9338c2ecf20Sopenharmony_ci			dev->stats.tx_bytes += skb->len;
9348c2ecf20Sopenharmony_ci			/* Free the original skb. */
9358c2ecf20Sopenharmony_ci			dma_unmap_single(&yp->pci_dev->dev,
9368c2ecf20Sopenharmony_ci					 le32_to_cpu(yp->tx_ring[entry].addr),
9378c2ecf20Sopenharmony_ci					 skb->len, DMA_TO_DEVICE);
9388c2ecf20Sopenharmony_ci			dev_consume_skb_irq(skb);
9398c2ecf20Sopenharmony_ci			yp->tx_skbuff[entry] = NULL;
9408c2ecf20Sopenharmony_ci		}
9418c2ecf20Sopenharmony_ci		if (yp->tx_full &&
9428c2ecf20Sopenharmony_ci		    yp->cur_tx - yp->dirty_tx < TX_QUEUE_SIZE - 4) {
9438c2ecf20Sopenharmony_ci			/* The ring is no longer full, clear tbusy. */
9448c2ecf20Sopenharmony_ci			yp->tx_full = 0;
9458c2ecf20Sopenharmony_ci			netif_wake_queue(dev);
9468c2ecf20Sopenharmony_ci		}
9478c2ecf20Sopenharmony_ci#else
9488c2ecf20Sopenharmony_ci		if ((intr_status & IntrTxDone) || (yp->tx_tail_desc->tx_errs)) {
9498c2ecf20Sopenharmony_ci			unsigned dirty_tx = yp->dirty_tx;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci			for (dirty_tx = yp->dirty_tx; yp->cur_tx - dirty_tx > 0;
9528c2ecf20Sopenharmony_ci				 dirty_tx++) {
9538c2ecf20Sopenharmony_ci				/* Todo: optimize this. */
9548c2ecf20Sopenharmony_ci				int entry = dirty_tx % TX_RING_SIZE;
9558c2ecf20Sopenharmony_ci				u16 tx_errs = yp->tx_status[entry].tx_errs;
9568c2ecf20Sopenharmony_ci				struct sk_buff *skb;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci#ifndef final_version
9598c2ecf20Sopenharmony_ci				if (yellowfin_debug > 5)
9608c2ecf20Sopenharmony_ci					netdev_printk(KERN_DEBUG, dev, "Tx queue %d check, Tx status %04x %04x %04x %04x\n",
9618c2ecf20Sopenharmony_ci						      entry,
9628c2ecf20Sopenharmony_ci						      yp->tx_status[entry].tx_cnt,
9638c2ecf20Sopenharmony_ci						      yp->tx_status[entry].tx_errs,
9648c2ecf20Sopenharmony_ci						      yp->tx_status[entry].total_tx_cnt,
9658c2ecf20Sopenharmony_ci						      yp->tx_status[entry].paused);
9668c2ecf20Sopenharmony_ci#endif
9678c2ecf20Sopenharmony_ci				if (tx_errs == 0)
9688c2ecf20Sopenharmony_ci					break;	/* It still hasn't been Txed */
9698c2ecf20Sopenharmony_ci				skb = yp->tx_skbuff[entry];
9708c2ecf20Sopenharmony_ci				if (tx_errs & 0xF810) {
9718c2ecf20Sopenharmony_ci					/* There was an major error, log it. */
9728c2ecf20Sopenharmony_ci#ifndef final_version
9738c2ecf20Sopenharmony_ci					if (yellowfin_debug > 1)
9748c2ecf20Sopenharmony_ci						netdev_printk(KERN_DEBUG, dev, "Transmit error, Tx status %04x\n",
9758c2ecf20Sopenharmony_ci							      tx_errs);
9768c2ecf20Sopenharmony_ci#endif
9778c2ecf20Sopenharmony_ci					dev->stats.tx_errors++;
9788c2ecf20Sopenharmony_ci					if (tx_errs & 0xF800) dev->stats.tx_aborted_errors++;
9798c2ecf20Sopenharmony_ci					if (tx_errs & 0x0800) dev->stats.tx_carrier_errors++;
9808c2ecf20Sopenharmony_ci					if (tx_errs & 0x2000) dev->stats.tx_window_errors++;
9818c2ecf20Sopenharmony_ci					if (tx_errs & 0x8000) dev->stats.tx_fifo_errors++;
9828c2ecf20Sopenharmony_ci				} else {
9838c2ecf20Sopenharmony_ci#ifndef final_version
9848c2ecf20Sopenharmony_ci					if (yellowfin_debug > 4)
9858c2ecf20Sopenharmony_ci						netdev_printk(KERN_DEBUG, dev, "Normal transmit, Tx status %04x\n",
9868c2ecf20Sopenharmony_ci							      tx_errs);
9878c2ecf20Sopenharmony_ci#endif
9888c2ecf20Sopenharmony_ci					dev->stats.tx_bytes += skb->len;
9898c2ecf20Sopenharmony_ci					dev->stats.collisions += tx_errs & 15;
9908c2ecf20Sopenharmony_ci					dev->stats.tx_packets++;
9918c2ecf20Sopenharmony_ci				}
9928c2ecf20Sopenharmony_ci				/* Free the original skb. */
9938c2ecf20Sopenharmony_ci				dma_unmap_single(&yp->pci_dev->dev,
9948c2ecf20Sopenharmony_ci						 yp->tx_ring[entry << 1].addr,
9958c2ecf20Sopenharmony_ci						 skb->len, DMA_TO_DEVICE);
9968c2ecf20Sopenharmony_ci				dev_consume_skb_irq(skb);
9978c2ecf20Sopenharmony_ci				yp->tx_skbuff[entry] = 0;
9988c2ecf20Sopenharmony_ci				/* Mark status as empty. */
9998c2ecf20Sopenharmony_ci				yp->tx_status[entry].tx_errs = 0;
10008c2ecf20Sopenharmony_ci			}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci#ifndef final_version
10038c2ecf20Sopenharmony_ci			if (yp->cur_tx - dirty_tx > TX_RING_SIZE) {
10048c2ecf20Sopenharmony_ci				netdev_err(dev, "Out-of-sync dirty pointer, %d vs. %d, full=%d\n",
10058c2ecf20Sopenharmony_ci					   dirty_tx, yp->cur_tx, yp->tx_full);
10068c2ecf20Sopenharmony_ci				dirty_tx += TX_RING_SIZE;
10078c2ecf20Sopenharmony_ci			}
10088c2ecf20Sopenharmony_ci#endif
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci			if (yp->tx_full &&
10118c2ecf20Sopenharmony_ci			    yp->cur_tx - dirty_tx < TX_QUEUE_SIZE - 2) {
10128c2ecf20Sopenharmony_ci				/* The ring is no longer full, clear tbusy. */
10138c2ecf20Sopenharmony_ci				yp->tx_full = 0;
10148c2ecf20Sopenharmony_ci				netif_wake_queue(dev);
10158c2ecf20Sopenharmony_ci			}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci			yp->dirty_tx = dirty_tx;
10188c2ecf20Sopenharmony_ci			yp->tx_tail_desc = &yp->tx_status[dirty_tx % TX_RING_SIZE];
10198c2ecf20Sopenharmony_ci		}
10208c2ecf20Sopenharmony_ci#endif
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci		/* Log errors and other uncommon events. */
10238c2ecf20Sopenharmony_ci		if (intr_status & 0x2ee)	/* Abnormal error summary. */
10248c2ecf20Sopenharmony_ci			yellowfin_error(dev, intr_status);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci		if (--boguscnt < 0) {
10278c2ecf20Sopenharmony_ci			netdev_warn(dev, "Too much work at interrupt, status=%#04x\n",
10288c2ecf20Sopenharmony_ci				    intr_status);
10298c2ecf20Sopenharmony_ci			break;
10308c2ecf20Sopenharmony_ci		}
10318c2ecf20Sopenharmony_ci	} while (1);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	if (yellowfin_debug > 3)
10348c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "exiting interrupt, status=%#04x\n",
10358c2ecf20Sopenharmony_ci			      ioread16(ioaddr + IntrStatus));
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	spin_unlock (&yp->lock);
10388c2ecf20Sopenharmony_ci	return IRQ_RETVAL(handled);
10398c2ecf20Sopenharmony_ci}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci/* This routine is logically part of the interrupt handler, but separated
10428c2ecf20Sopenharmony_ci   for clarity and better register allocation. */
10438c2ecf20Sopenharmony_cistatic int yellowfin_rx(struct net_device *dev)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
10468c2ecf20Sopenharmony_ci	int entry = yp->cur_rx % RX_RING_SIZE;
10478c2ecf20Sopenharmony_ci	int boguscnt = yp->dirty_rx + RX_RING_SIZE - yp->cur_rx;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	if (yellowfin_debug > 4) {
10508c2ecf20Sopenharmony_ci		printk(KERN_DEBUG " In yellowfin_rx(), entry %d status %08x\n",
10518c2ecf20Sopenharmony_ci			   entry, yp->rx_ring[entry].result_status);
10528c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "   #%d desc. %08x %08x %08x\n",
10538c2ecf20Sopenharmony_ci			   entry, yp->rx_ring[entry].dbdma_cmd, yp->rx_ring[entry].addr,
10548c2ecf20Sopenharmony_ci			   yp->rx_ring[entry].result_status);
10558c2ecf20Sopenharmony_ci	}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	/* If EOP is set on the next entry, it's a new packet. Send it up. */
10588c2ecf20Sopenharmony_ci	while (1) {
10598c2ecf20Sopenharmony_ci		struct yellowfin_desc *desc = &yp->rx_ring[entry];
10608c2ecf20Sopenharmony_ci		struct sk_buff *rx_skb = yp->rx_skbuff[entry];
10618c2ecf20Sopenharmony_ci		s16 frame_status;
10628c2ecf20Sopenharmony_ci		u16 desc_status;
10638c2ecf20Sopenharmony_ci		int data_size, __maybe_unused yf_size;
10648c2ecf20Sopenharmony_ci		u8 *buf_addr;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci		if(!desc->result_status)
10678c2ecf20Sopenharmony_ci			break;
10688c2ecf20Sopenharmony_ci		dma_sync_single_for_cpu(&yp->pci_dev->dev,
10698c2ecf20Sopenharmony_ci					le32_to_cpu(desc->addr),
10708c2ecf20Sopenharmony_ci					yp->rx_buf_sz, DMA_FROM_DEVICE);
10718c2ecf20Sopenharmony_ci		desc_status = le32_to_cpu(desc->result_status) >> 16;
10728c2ecf20Sopenharmony_ci		buf_addr = rx_skb->data;
10738c2ecf20Sopenharmony_ci		data_size = (le32_to_cpu(desc->dbdma_cmd) -
10748c2ecf20Sopenharmony_ci			le32_to_cpu(desc->result_status)) & 0xffff;
10758c2ecf20Sopenharmony_ci		frame_status = get_unaligned_le16(&(buf_addr[data_size - 2]));
10768c2ecf20Sopenharmony_ci		if (yellowfin_debug > 4)
10778c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "  %s() status was %04x\n",
10788c2ecf20Sopenharmony_ci			       __func__, frame_status);
10798c2ecf20Sopenharmony_ci		if (--boguscnt < 0)
10808c2ecf20Sopenharmony_ci			break;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci		yf_size = sizeof(struct yellowfin_desc);
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci		if ( ! (desc_status & RX_EOP)) {
10858c2ecf20Sopenharmony_ci			if (data_size != 0)
10868c2ecf20Sopenharmony_ci				netdev_warn(dev, "Oversized Ethernet frame spanned multiple buffers, status %04x, data_size %d!\n",
10878c2ecf20Sopenharmony_ci					    desc_status, data_size);
10888c2ecf20Sopenharmony_ci			dev->stats.rx_length_errors++;
10898c2ecf20Sopenharmony_ci		} else if ((yp->drv_flags & IsGigabit)  &&  (frame_status & 0x0038)) {
10908c2ecf20Sopenharmony_ci			/* There was a error. */
10918c2ecf20Sopenharmony_ci			if (yellowfin_debug > 3)
10928c2ecf20Sopenharmony_ci				printk(KERN_DEBUG "  %s() Rx error was %04x\n",
10938c2ecf20Sopenharmony_ci				       __func__, frame_status);
10948c2ecf20Sopenharmony_ci			dev->stats.rx_errors++;
10958c2ecf20Sopenharmony_ci			if (frame_status & 0x0060) dev->stats.rx_length_errors++;
10968c2ecf20Sopenharmony_ci			if (frame_status & 0x0008) dev->stats.rx_frame_errors++;
10978c2ecf20Sopenharmony_ci			if (frame_status & 0x0010) dev->stats.rx_crc_errors++;
10988c2ecf20Sopenharmony_ci			if (frame_status < 0) dev->stats.rx_dropped++;
10998c2ecf20Sopenharmony_ci		} else if ( !(yp->drv_flags & IsGigabit)  &&
11008c2ecf20Sopenharmony_ci				   ((buf_addr[data_size-1] & 0x85) || buf_addr[data_size-2] & 0xC0)) {
11018c2ecf20Sopenharmony_ci			u8 status1 = buf_addr[data_size-2];
11028c2ecf20Sopenharmony_ci			u8 status2 = buf_addr[data_size-1];
11038c2ecf20Sopenharmony_ci			dev->stats.rx_errors++;
11048c2ecf20Sopenharmony_ci			if (status1 & 0xC0) dev->stats.rx_length_errors++;
11058c2ecf20Sopenharmony_ci			if (status2 & 0x03) dev->stats.rx_frame_errors++;
11068c2ecf20Sopenharmony_ci			if (status2 & 0x04) dev->stats.rx_crc_errors++;
11078c2ecf20Sopenharmony_ci			if (status2 & 0x80) dev->stats.rx_dropped++;
11088c2ecf20Sopenharmony_ci#ifdef YF_PROTOTYPE		/* Support for prototype hardware errata. */
11098c2ecf20Sopenharmony_ci		} else if ((yp->flags & HasMACAddrBug)  &&
11108c2ecf20Sopenharmony_ci			!ether_addr_equal(le32_to_cpu(yp->rx_ring_dma +
11118c2ecf20Sopenharmony_ci						      entry * yf_size),
11128c2ecf20Sopenharmony_ci					  dev->dev_addr) &&
11138c2ecf20Sopenharmony_ci			!ether_addr_equal(le32_to_cpu(yp->rx_ring_dma +
11148c2ecf20Sopenharmony_ci						      entry * yf_size),
11158c2ecf20Sopenharmony_ci					  "\377\377\377\377\377\377")) {
11168c2ecf20Sopenharmony_ci			if (bogus_rx++ == 0)
11178c2ecf20Sopenharmony_ci				netdev_warn(dev, "Bad frame to %pM\n",
11188c2ecf20Sopenharmony_ci					    buf_addr);
11198c2ecf20Sopenharmony_ci#endif
11208c2ecf20Sopenharmony_ci		} else {
11218c2ecf20Sopenharmony_ci			struct sk_buff *skb;
11228c2ecf20Sopenharmony_ci			int pkt_len = data_size -
11238c2ecf20Sopenharmony_ci				(yp->chip_id ? 7 : 8 + buf_addr[data_size - 8]);
11248c2ecf20Sopenharmony_ci			/* To verify: Yellowfin Length should omit the CRC! */
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci#ifndef final_version
11278c2ecf20Sopenharmony_ci			if (yellowfin_debug > 4)
11288c2ecf20Sopenharmony_ci				printk(KERN_DEBUG "  %s() normal Rx pkt length %d of %d, bogus_cnt %d\n",
11298c2ecf20Sopenharmony_ci				       __func__, pkt_len, data_size, boguscnt);
11308c2ecf20Sopenharmony_ci#endif
11318c2ecf20Sopenharmony_ci			/* Check if the packet is long enough to just pass up the skbuff
11328c2ecf20Sopenharmony_ci			   without copying to a properly sized skbuff. */
11338c2ecf20Sopenharmony_ci			if (pkt_len > rx_copybreak) {
11348c2ecf20Sopenharmony_ci				skb_put(skb = rx_skb, pkt_len);
11358c2ecf20Sopenharmony_ci				dma_unmap_single(&yp->pci_dev->dev,
11368c2ecf20Sopenharmony_ci						 le32_to_cpu(yp->rx_ring[entry].addr),
11378c2ecf20Sopenharmony_ci						 yp->rx_buf_sz,
11388c2ecf20Sopenharmony_ci						 DMA_FROM_DEVICE);
11398c2ecf20Sopenharmony_ci				yp->rx_skbuff[entry] = NULL;
11408c2ecf20Sopenharmony_ci			} else {
11418c2ecf20Sopenharmony_ci				skb = netdev_alloc_skb(dev, pkt_len + 2);
11428c2ecf20Sopenharmony_ci				if (skb == NULL)
11438c2ecf20Sopenharmony_ci					break;
11448c2ecf20Sopenharmony_ci				skb_reserve(skb, 2);	/* 16 byte align the IP header */
11458c2ecf20Sopenharmony_ci				skb_copy_to_linear_data(skb, rx_skb->data, pkt_len);
11468c2ecf20Sopenharmony_ci				skb_put(skb, pkt_len);
11478c2ecf20Sopenharmony_ci				dma_sync_single_for_device(&yp->pci_dev->dev,
11488c2ecf20Sopenharmony_ci							   le32_to_cpu(desc->addr),
11498c2ecf20Sopenharmony_ci							   yp->rx_buf_sz,
11508c2ecf20Sopenharmony_ci							   DMA_FROM_DEVICE);
11518c2ecf20Sopenharmony_ci			}
11528c2ecf20Sopenharmony_ci			skb->protocol = eth_type_trans(skb, dev);
11538c2ecf20Sopenharmony_ci			netif_rx(skb);
11548c2ecf20Sopenharmony_ci			dev->stats.rx_packets++;
11558c2ecf20Sopenharmony_ci			dev->stats.rx_bytes += pkt_len;
11568c2ecf20Sopenharmony_ci		}
11578c2ecf20Sopenharmony_ci		entry = (++yp->cur_rx) % RX_RING_SIZE;
11588c2ecf20Sopenharmony_ci	}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	/* Refill the Rx ring buffers. */
11618c2ecf20Sopenharmony_ci	for (; yp->cur_rx - yp->dirty_rx > 0; yp->dirty_rx++) {
11628c2ecf20Sopenharmony_ci		entry = yp->dirty_rx % RX_RING_SIZE;
11638c2ecf20Sopenharmony_ci		if (yp->rx_skbuff[entry] == NULL) {
11648c2ecf20Sopenharmony_ci			struct sk_buff *skb = netdev_alloc_skb(dev, yp->rx_buf_sz + 2);
11658c2ecf20Sopenharmony_ci			if (skb == NULL)
11668c2ecf20Sopenharmony_ci				break;				/* Better luck next round. */
11678c2ecf20Sopenharmony_ci			yp->rx_skbuff[entry] = skb;
11688c2ecf20Sopenharmony_ci			skb_reserve(skb, 2);	/* Align IP on 16 byte boundaries */
11698c2ecf20Sopenharmony_ci			yp->rx_ring[entry].addr = cpu_to_le32(dma_map_single(&yp->pci_dev->dev,
11708c2ecf20Sopenharmony_ci									     skb->data,
11718c2ecf20Sopenharmony_ci									     yp->rx_buf_sz,
11728c2ecf20Sopenharmony_ci									     DMA_FROM_DEVICE));
11738c2ecf20Sopenharmony_ci		}
11748c2ecf20Sopenharmony_ci		yp->rx_ring[entry].dbdma_cmd = cpu_to_le32(CMD_STOP);
11758c2ecf20Sopenharmony_ci		yp->rx_ring[entry].result_status = 0;	/* Clear complete bit. */
11768c2ecf20Sopenharmony_ci		if (entry != 0)
11778c2ecf20Sopenharmony_ci			yp->rx_ring[entry - 1].dbdma_cmd =
11788c2ecf20Sopenharmony_ci				cpu_to_le32(CMD_RX_BUF | INTR_ALWAYS | yp->rx_buf_sz);
11798c2ecf20Sopenharmony_ci		else
11808c2ecf20Sopenharmony_ci			yp->rx_ring[RX_RING_SIZE - 1].dbdma_cmd =
11818c2ecf20Sopenharmony_ci				cpu_to_le32(CMD_RX_BUF | INTR_ALWAYS | BRANCH_ALWAYS
11828c2ecf20Sopenharmony_ci							| yp->rx_buf_sz);
11838c2ecf20Sopenharmony_ci	}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	return 0;
11868c2ecf20Sopenharmony_ci}
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_cistatic void yellowfin_error(struct net_device *dev, int intr_status)
11898c2ecf20Sopenharmony_ci{
11908c2ecf20Sopenharmony_ci	netdev_err(dev, "Something Wicked happened! %04x\n", intr_status);
11918c2ecf20Sopenharmony_ci	/* Hmmmmm, it's not clear what to do here. */
11928c2ecf20Sopenharmony_ci	if (intr_status & (IntrTxPCIErr | IntrTxPCIFault))
11938c2ecf20Sopenharmony_ci		dev->stats.tx_errors++;
11948c2ecf20Sopenharmony_ci	if (intr_status & (IntrRxPCIErr | IntrRxPCIFault))
11958c2ecf20Sopenharmony_ci		dev->stats.rx_errors++;
11968c2ecf20Sopenharmony_ci}
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_cistatic int yellowfin_close(struct net_device *dev)
11998c2ecf20Sopenharmony_ci{
12008c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
12018c2ecf20Sopenharmony_ci	void __iomem *ioaddr = yp->base;
12028c2ecf20Sopenharmony_ci	int i;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	netif_stop_queue (dev);
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	if (yellowfin_debug > 1) {
12078c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "Shutting down ethercard, status was Tx %04x Rx %04x Int %02x\n",
12088c2ecf20Sopenharmony_ci			      ioread16(ioaddr + TxStatus),
12098c2ecf20Sopenharmony_ci			      ioread16(ioaddr + RxStatus),
12108c2ecf20Sopenharmony_ci			      ioread16(ioaddr + IntrStatus));
12118c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "Queue pointers were Tx %d / %d,  Rx %d / %d\n",
12128c2ecf20Sopenharmony_ci			      yp->cur_tx, yp->dirty_tx,
12138c2ecf20Sopenharmony_ci			      yp->cur_rx, yp->dirty_rx);
12148c2ecf20Sopenharmony_ci	}
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	/* Disable interrupts by clearing the interrupt mask. */
12178c2ecf20Sopenharmony_ci	iowrite16(0x0000, ioaddr + IntrEnb);
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	/* Stop the chip's Tx and Rx processes. */
12208c2ecf20Sopenharmony_ci	iowrite32(0x80000000, ioaddr + RxCtrl);
12218c2ecf20Sopenharmony_ci	iowrite32(0x80000000, ioaddr + TxCtrl);
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	del_timer(&yp->timer);
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci#if defined(__i386__)
12268c2ecf20Sopenharmony_ci	if (yellowfin_debug > 2) {
12278c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "  Tx ring at %08llx:\n",
12288c2ecf20Sopenharmony_ci				(unsigned long long)yp->tx_ring_dma);
12298c2ecf20Sopenharmony_ci		for (i = 0; i < TX_RING_SIZE*2; i++)
12308c2ecf20Sopenharmony_ci			printk(KERN_DEBUG " %c #%d desc. %08x %08x %08x %08x\n",
12318c2ecf20Sopenharmony_ci				   ioread32(ioaddr + TxPtr) == (long)&yp->tx_ring[i] ? '>' : ' ',
12328c2ecf20Sopenharmony_ci				   i, yp->tx_ring[i].dbdma_cmd, yp->tx_ring[i].addr,
12338c2ecf20Sopenharmony_ci				   yp->tx_ring[i].branch_addr, yp->tx_ring[i].result_status);
12348c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "  Tx status %p:\n", yp->tx_status);
12358c2ecf20Sopenharmony_ci		for (i = 0; i < TX_RING_SIZE; i++)
12368c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "   #%d status %04x %04x %04x %04x\n",
12378c2ecf20Sopenharmony_ci				   i, yp->tx_status[i].tx_cnt, yp->tx_status[i].tx_errs,
12388c2ecf20Sopenharmony_ci				   yp->tx_status[i].total_tx_cnt, yp->tx_status[i].paused);
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "  Rx ring %08llx:\n",
12418c2ecf20Sopenharmony_ci				(unsigned long long)yp->rx_ring_dma);
12428c2ecf20Sopenharmony_ci		for (i = 0; i < RX_RING_SIZE; i++) {
12438c2ecf20Sopenharmony_ci			printk(KERN_DEBUG " %c #%d desc. %08x %08x %08x\n",
12448c2ecf20Sopenharmony_ci				   ioread32(ioaddr + RxPtr) == (long)&yp->rx_ring[i] ? '>' : ' ',
12458c2ecf20Sopenharmony_ci				   i, yp->rx_ring[i].dbdma_cmd, yp->rx_ring[i].addr,
12468c2ecf20Sopenharmony_ci				   yp->rx_ring[i].result_status);
12478c2ecf20Sopenharmony_ci			if (yellowfin_debug > 6) {
12488c2ecf20Sopenharmony_ci				if (get_unaligned((u8*)yp->rx_ring[i].addr) != 0x69) {
12498c2ecf20Sopenharmony_ci					int j;
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci					printk(KERN_DEBUG);
12528c2ecf20Sopenharmony_ci					for (j = 0; j < 0x50; j++)
12538c2ecf20Sopenharmony_ci						pr_cont(" %04x",
12548c2ecf20Sopenharmony_ci							get_unaligned(((u16*)yp->rx_ring[i].addr) + j));
12558c2ecf20Sopenharmony_ci					pr_cont("\n");
12568c2ecf20Sopenharmony_ci				}
12578c2ecf20Sopenharmony_ci			}
12588c2ecf20Sopenharmony_ci		}
12598c2ecf20Sopenharmony_ci	}
12608c2ecf20Sopenharmony_ci#endif /* __i386__ debugging only */
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	free_irq(yp->pci_dev->irq, dev);
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	/* Free all the skbuffs in the Rx queue. */
12658c2ecf20Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
12668c2ecf20Sopenharmony_ci		yp->rx_ring[i].dbdma_cmd = cpu_to_le32(CMD_STOP);
12678c2ecf20Sopenharmony_ci		yp->rx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
12688c2ecf20Sopenharmony_ci		if (yp->rx_skbuff[i]) {
12698c2ecf20Sopenharmony_ci			dev_kfree_skb(yp->rx_skbuff[i]);
12708c2ecf20Sopenharmony_ci		}
12718c2ecf20Sopenharmony_ci		yp->rx_skbuff[i] = NULL;
12728c2ecf20Sopenharmony_ci	}
12738c2ecf20Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++) {
12748c2ecf20Sopenharmony_ci		dev_kfree_skb(yp->tx_skbuff[i]);
12758c2ecf20Sopenharmony_ci		yp->tx_skbuff[i] = NULL;
12768c2ecf20Sopenharmony_ci	}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci#ifdef YF_PROTOTYPE			/* Support for prototype hardware errata. */
12798c2ecf20Sopenharmony_ci	if (yellowfin_debug > 0) {
12808c2ecf20Sopenharmony_ci		netdev_printk(KERN_DEBUG, dev, "Received %d frames that we should not have\n",
12818c2ecf20Sopenharmony_ci			      bogus_rx);
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci#endif
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	return 0;
12868c2ecf20Sopenharmony_ci}
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci/* Set or clear the multicast filter for this adaptor. */
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_cistatic void set_rx_mode(struct net_device *dev)
12918c2ecf20Sopenharmony_ci{
12928c2ecf20Sopenharmony_ci	struct yellowfin_private *yp = netdev_priv(dev);
12938c2ecf20Sopenharmony_ci	void __iomem *ioaddr = yp->base;
12948c2ecf20Sopenharmony_ci	u16 cfg_value = ioread16(ioaddr + Cnfg);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	/* Stop the Rx process to change any value. */
12978c2ecf20Sopenharmony_ci	iowrite16(cfg_value & ~0x1000, ioaddr + Cnfg);
12988c2ecf20Sopenharmony_ci	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous. */
12998c2ecf20Sopenharmony_ci		iowrite16(0x000F, ioaddr + AddrMode);
13008c2ecf20Sopenharmony_ci	} else if ((netdev_mc_count(dev) > 64) ||
13018c2ecf20Sopenharmony_ci		   (dev->flags & IFF_ALLMULTI)) {
13028c2ecf20Sopenharmony_ci		/* Too many to filter well, or accept all multicasts. */
13038c2ecf20Sopenharmony_ci		iowrite16(0x000B, ioaddr + AddrMode);
13048c2ecf20Sopenharmony_ci	} else if (!netdev_mc_empty(dev)) { /* Must use the multicast hash table. */
13058c2ecf20Sopenharmony_ci		struct netdev_hw_addr *ha;
13068c2ecf20Sopenharmony_ci		u16 hash_table[4];
13078c2ecf20Sopenharmony_ci		int i;
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci		memset(hash_table, 0, sizeof(hash_table));
13108c2ecf20Sopenharmony_ci		netdev_for_each_mc_addr(ha, dev) {
13118c2ecf20Sopenharmony_ci			unsigned int bit;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci			/* Due to a bug in the early chip versions, multiple filter
13148c2ecf20Sopenharmony_ci			   slots must be set for each address. */
13158c2ecf20Sopenharmony_ci			if (yp->drv_flags & HasMulticastBug) {
13168c2ecf20Sopenharmony_ci				bit = (ether_crc_le(3, ha->addr) >> 3) & 0x3f;
13178c2ecf20Sopenharmony_ci				hash_table[bit >> 4] |= (1 << bit);
13188c2ecf20Sopenharmony_ci				bit = (ether_crc_le(4, ha->addr) >> 3) & 0x3f;
13198c2ecf20Sopenharmony_ci				hash_table[bit >> 4] |= (1 << bit);
13208c2ecf20Sopenharmony_ci				bit = (ether_crc_le(5, ha->addr) >> 3) & 0x3f;
13218c2ecf20Sopenharmony_ci				hash_table[bit >> 4] |= (1 << bit);
13228c2ecf20Sopenharmony_ci			}
13238c2ecf20Sopenharmony_ci			bit = (ether_crc_le(6, ha->addr) >> 3) & 0x3f;
13248c2ecf20Sopenharmony_ci			hash_table[bit >> 4] |= (1 << bit);
13258c2ecf20Sopenharmony_ci		}
13268c2ecf20Sopenharmony_ci		/* Copy the hash table to the chip. */
13278c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++)
13288c2ecf20Sopenharmony_ci			iowrite16(hash_table[i], ioaddr + HashTbl + i*2);
13298c2ecf20Sopenharmony_ci		iowrite16(0x0003, ioaddr + AddrMode);
13308c2ecf20Sopenharmony_ci	} else {					/* Normal, unicast/broadcast-only mode. */
13318c2ecf20Sopenharmony_ci		iowrite16(0x0001, ioaddr + AddrMode);
13328c2ecf20Sopenharmony_ci	}
13338c2ecf20Sopenharmony_ci	/* Restart the Rx process. */
13348c2ecf20Sopenharmony_ci	iowrite16(cfg_value | 0x1000, ioaddr + Cnfg);
13358c2ecf20Sopenharmony_ci}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_cistatic void yellowfin_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	struct yellowfin_private *np = netdev_priv(dev);
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
13428c2ecf20Sopenharmony_ci	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
13438c2ecf20Sopenharmony_ci	strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
13448c2ecf20Sopenharmony_ci}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_cistatic const struct ethtool_ops ethtool_ops = {
13478c2ecf20Sopenharmony_ci	.get_drvinfo = yellowfin_get_drvinfo
13488c2ecf20Sopenharmony_ci};
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_cistatic int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
13518c2ecf20Sopenharmony_ci{
13528c2ecf20Sopenharmony_ci	struct yellowfin_private *np = netdev_priv(dev);
13538c2ecf20Sopenharmony_ci	void __iomem *ioaddr = np->base;
13548c2ecf20Sopenharmony_ci	struct mii_ioctl_data *data = if_mii(rq);
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	switch(cmd) {
13578c2ecf20Sopenharmony_ci	case SIOCGMIIPHY:		/* Get address of MII PHY in use. */
13588c2ecf20Sopenharmony_ci		data->phy_id = np->phys[0] & 0x1f;
13598c2ecf20Sopenharmony_ci		fallthrough;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	case SIOCGMIIREG:		/* Read MII PHY register. */
13628c2ecf20Sopenharmony_ci		data->val_out = mdio_read(ioaddr, data->phy_id & 0x1f, data->reg_num & 0x1f);
13638c2ecf20Sopenharmony_ci		return 0;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	case SIOCSMIIREG:		/* Write MII PHY register. */
13668c2ecf20Sopenharmony_ci		if (data->phy_id == np->phys[0]) {
13678c2ecf20Sopenharmony_ci			u16 value = data->val_in;
13688c2ecf20Sopenharmony_ci			switch (data->reg_num) {
13698c2ecf20Sopenharmony_ci			case 0:
13708c2ecf20Sopenharmony_ci				/* Check for autonegotiation on or reset. */
13718c2ecf20Sopenharmony_ci				np->medialock = (value & 0x9000) ? 0 : 1;
13728c2ecf20Sopenharmony_ci				if (np->medialock)
13738c2ecf20Sopenharmony_ci					np->full_duplex = (value & 0x0100) ? 1 : 0;
13748c2ecf20Sopenharmony_ci				break;
13758c2ecf20Sopenharmony_ci			case 4: np->advertising = value; break;
13768c2ecf20Sopenharmony_ci			}
13778c2ecf20Sopenharmony_ci			/* Perhaps check_duplex(dev), depending on chip semantics. */
13788c2ecf20Sopenharmony_ci		}
13798c2ecf20Sopenharmony_ci		mdio_write(ioaddr, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
13808c2ecf20Sopenharmony_ci		return 0;
13818c2ecf20Sopenharmony_ci	default:
13828c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
13838c2ecf20Sopenharmony_ci	}
13848c2ecf20Sopenharmony_ci}
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_cistatic void yellowfin_remove_one(struct pci_dev *pdev)
13888c2ecf20Sopenharmony_ci{
13898c2ecf20Sopenharmony_ci	struct net_device *dev = pci_get_drvdata(pdev);
13908c2ecf20Sopenharmony_ci	struct yellowfin_private *np;
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci	BUG_ON(!dev);
13938c2ecf20Sopenharmony_ci	np = netdev_priv(dev);
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	dma_free_coherent(&pdev->dev, STATUS_TOTAL_SIZE, np->tx_status,
13968c2ecf20Sopenharmony_ci			  np->tx_status_dma);
13978c2ecf20Sopenharmony_ci	dma_free_coherent(&pdev->dev, RX_TOTAL_SIZE, np->rx_ring,
13988c2ecf20Sopenharmony_ci			  np->rx_ring_dma);
13998c2ecf20Sopenharmony_ci	dma_free_coherent(&pdev->dev, TX_TOTAL_SIZE, np->tx_ring,
14008c2ecf20Sopenharmony_ci			  np->tx_ring_dma);
14018c2ecf20Sopenharmony_ci	unregister_netdev (dev);
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	pci_iounmap(pdev, np->base);
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	pci_release_regions (pdev);
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci	free_netdev (dev);
14088c2ecf20Sopenharmony_ci}
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_cistatic struct pci_driver yellowfin_driver = {
14128c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
14138c2ecf20Sopenharmony_ci	.id_table	= yellowfin_pci_tbl,
14148c2ecf20Sopenharmony_ci	.probe		= yellowfin_init_one,
14158c2ecf20Sopenharmony_ci	.remove		= yellowfin_remove_one,
14168c2ecf20Sopenharmony_ci};
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_cistatic int __init yellowfin_init (void)
14208c2ecf20Sopenharmony_ci{
14218c2ecf20Sopenharmony_ci/* when a module, this is printed whether or not devices are found in probe */
14228c2ecf20Sopenharmony_ci#ifdef MODULE
14238c2ecf20Sopenharmony_ci	printk(version);
14248c2ecf20Sopenharmony_ci#endif
14258c2ecf20Sopenharmony_ci	return pci_register_driver(&yellowfin_driver);
14268c2ecf20Sopenharmony_ci}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_cistatic void __exit yellowfin_cleanup (void)
14308c2ecf20Sopenharmony_ci{
14318c2ecf20Sopenharmony_ci	pci_unregister_driver (&yellowfin_driver);
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_cimodule_init(yellowfin_init);
14368c2ecf20Sopenharmony_cimodule_exit(yellowfin_cleanup);
1437