drivers/8139too.c
branchkernel2.6
changeset 22 e8c2cd004e0b
child 23 39364fbcd069
equal deleted inserted replaced
21:ec912b010528 22:e8c2cd004e0b
       
     1 /******************************************************************************
       
     2  *
       
     3  *  drv_8139too.c
       
     4  *
       
     5  *  EtherCAT-Treiber für RTL8139-kompatible Netzwerkkarten.
       
     6  *
       
     7  *  Autoren: Wilhelm Hagemeister, Florian Pose
       
     8  *
       
     9  *  $Date: 2005-11-18 11:30:01 +0100 (Fre, 18 Nov 2005) $
       
    10  *  $Author: fp $
       
    11  *
       
    12  *  (C) Copyright IgH 2005
       
    13  *  Ingenieurgemeinschaft IgH
       
    14  *  Heinz-Bäcker Str. 34
       
    15  *  D-45356 Essen
       
    16  *  Tel.: +49 201/61 99 31
       
    17  *  Fax.: +49 201/61 98 36
       
    18  *  E-mail: sp@igh-essen.com
       
    19  *
       
    20  ******************************************************************************/
       
    21 
       
    22 /*
       
    23 	8139too.c: A RealTek RTL-8139 Fast Ethernet driver for Linux.
       
    24 
       
    25 	Maintained by Jeff Garzik <jgarzik@pobox.com>
       
    26 	Copyright 2000-2002 Jeff Garzik
       
    27 
       
    28 	Much code comes from Donald Becker's rtl8139.c driver,
       
    29 	versions 1.13 and older.  This driver was originally based
       
    30 	on rtl8139.c version 1.07.  Header of rtl8139.c version 1.13:
       
    31 
       
    32 	-----<snip>-----
       
    33 
       
    34         	Written 1997-2001 by Donald Becker.
       
    35 		This software may be used and distributed according to the
       
    36 		terms of the GNU General Public License (GPL), incorporated
       
    37 		herein by reference.  Drivers based on or derived from this
       
    38 		code fall under the GPL and must retain the authorship,
       
    39 		copyright and license notice.  This file is not a complete
       
    40 		program and may only be used when the entire operating
       
    41 		system is licensed under the GPL.
       
    42 
       
    43 		This driver is for boards based on the RTL8129 and RTL8139
       
    44 		PCI ethernet chips.
       
    45 
       
    46 		The author may be reached as becker@scyld.com, or C/O Scyld
       
    47 		Computing Corporation 410 Severn Ave., Suite 210 Annapolis
       
    48 		MD 21403
       
    49 
       
    50 		Support and updates available at
       
    51 		http://www.scyld.com/network/rtl8139.html
       
    52 
       
    53 		Twister-tuning table provided by Kinston
       
    54 		<shangh@realtek.com.tw>.
       
    55 
       
    56 	-----<snip>-----
       
    57 
       
    58 	This software may be used and distributed according to the terms
       
    59 	of the GNU General Public License, incorporated herein by reference.
       
    60 
       
    61 	Contributors:
       
    62 
       
    63 		Donald Becker - he wrote the original driver, kudos to him!
       
    64 		(but please don't e-mail him for support, this isn't his driver)
       
    65 
       
    66 		Tigran Aivazian - bug fixes, skbuff free cleanup
       
    67 
       
    68 		Martin Mares - suggestions for PCI cleanup
       
    69 
       
    70 		David S. Miller - PCI DMA and softnet updates
       
    71 
       
    72 		Ernst Gill - fixes ported from BSD driver
       
    73 
       
    74 		Daniel Kobras - identified specific locations of
       
    75 			posted MMIO write bugginess
       
    76 
       
    77 		Gerard Sharp - bug fix, testing and feedback
       
    78 
       
    79 		David Ford - Rx ring wrap fix
       
    80 
       
    81 		Dan DeMaggio - swapped RTL8139 cards with me, and allowed me
       
    82 		to find and fix a crucial bug on older chipsets.
       
    83 
       
    84 		Donald Becker/Chris Butterworth/Marcus Westergren -
       
    85 		Noticed various Rx packet size-related buglets.
       
    86 
       
    87 		Santiago Garcia Mantinan - testing and feedback
       
    88 
       
    89 		Jens David - 2.2.x kernel backports
       
    90 
       
    91 		Martin Dennett - incredibly helpful insight on undocumented
       
    92 		features of the 8139 chips
       
    93 
       
    94 		Jean-Jacques Michel - bug fix
       
    95 
       
    96 		Tobias Ringström - Rx interrupt status checking suggestion
       
    97 
       
    98 		Andrew Morton - Clear blocked signals, avoid
       
    99 		buffer overrun setting current->comm.
       
   100 
       
   101 		Kalle Olavi Niemitalo - Wake-on-LAN ioctls
       
   102 
       
   103 		Robert Kuebel - Save kernel thread from dying on any signal.
       
   104 
       
   105 	Submitting bug reports:
       
   106 
       
   107 		"rtl8139-diag -mmmaaavvveefN" output
       
   108 		enable RTL8139_DEBUG below, and look at 'dmesg' or kernel log
       
   109 
       
   110 */
       
   111 
       
   112 #define DRV_NAME	"8139too_ecat"
       
   113 #define DRV_VERSION	"0.9.27"
       
   114 
       
   115 
       
   116 #include <linux/config.h>
       
   117 #include <linux/module.h>
       
   118 #include <linux/kernel.h>
       
   119 #include <linux/compiler.h>
       
   120 #include <linux/pci.h>
       
   121 #include <linux/init.h>
       
   122 #include <linux/ioport.h>
       
   123 #include <linux/netdevice.h>
       
   124 #include <linux/etherdevice.h>
       
   125 #include <linux/rtnetlink.h>
       
   126 #include <linux/delay.h>
       
   127 #include <linux/ethtool.h>
       
   128 #include <linux/mii.h>
       
   129 #include <linux/completion.h>
       
   130 #include <linux/crc32.h>
       
   131 #include <asm/io.h>
       
   132 #include <asm/uaccess.h>
       
   133 #include <asm/irq.h>
       
   134 
       
   135 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   136 
       
   137 #include "ec_device.h"
       
   138 //#include "ec_dbg.h"
       
   139 
       
   140 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   141 
       
   142 #define RTL8139_DRIVER_NAME   DRV_NAME " Fast Ethernet driver " DRV_VERSION
       
   143 #define PFX DRV_NAME ": "
       
   144 
       
   145 /* Default Message level */
       
   146 #define RTL8139_DEF_MSG_ENABLE   (NETIF_MSG_DRV   | \
       
   147                                  NETIF_MSG_PROBE  | \
       
   148                                  NETIF_MSG_LINK)
       
   149 
       
   150 
       
   151 /* enable PIO instead of MMIO, if CONFIG_8139TOO_PIO is selected */
       
   152 #ifdef CONFIG_8139TOO_PIO
       
   153 #define USE_IO_OPS 1
       
   154 #endif
       
   155 
       
   156 /* define to 1, 2 or 3 to enable copious debugging info */
       
   157 #define RTL8139_DEBUG 0
       
   158 
       
   159 /* define to 1 to disable lightweight runtime debugging checks */
       
   160 #undef RTL8139_NDEBUG
       
   161 
       
   162 
       
   163 #if RTL8139_DEBUG
       
   164 /* note: prints function name for you */
       
   165 #  define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
       
   166 #else
       
   167 #  define DPRINTK(fmt, args...)
       
   168 #endif
       
   169 
       
   170 #ifdef RTL8139_NDEBUG
       
   171 #  define assert(expr) do {} while (0)
       
   172 #else
       
   173 #  define assert(expr) \
       
   174         if(unlikely(!(expr))) {				        \
       
   175         printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n",	\
       
   176         #expr,__FILE__,__FUNCTION__,__LINE__);		        \
       
   177         }
       
   178 #endif
       
   179 
       
   180 
       
   181 /* A few user-configurable values. */
       
   182 /* media options */
       
   183 #define MAX_UNITS 8
       
   184 static int media[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
       
   185 static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
       
   186 
       
   187 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
       
   188    The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
       
   189 static int multicast_filter_limit = 32;
       
   190 
       
   191 /* bitmapped message enable number */
       
   192 static int debug = -1;
       
   193 
       
   194 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   195 
       
   196 // Device index for EtherCAT device selection
       
   197 static int ec_device_index = -1;
       
   198 
       
   199 //#define ECAT_DEBUG
       
   200 
       
   201 EtherCAT_device_t rtl_ecat_dev;
       
   202 
       
   203 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   204 
       
   205 /*
       
   206  * Receive ring size 
       
   207  * Warning: 64K ring has hardware issues and may lock up.
       
   208  */
       
   209 #if defined(CONFIG_SH_DREAMCAST)
       
   210 #define RX_BUF_IDX	1	/* 16K ring */
       
   211 #else
       
   212 #define RX_BUF_IDX	2	/* 32K ring */
       
   213 #endif
       
   214 #define RX_BUF_LEN	(8192 << RX_BUF_IDX)
       
   215 #define RX_BUF_PAD	16
       
   216 #define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */
       
   217 
       
   218 #if RX_BUF_LEN == 65536
       
   219 #define RX_BUF_TOT_LEN	RX_BUF_LEN
       
   220 #else
       
   221 #define RX_BUF_TOT_LEN	(RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)
       
   222 #endif
       
   223 
       
   224 /* Number of Tx descriptor registers. */
       
   225 #define NUM_TX_DESC	4
       
   226 
       
   227 /* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/
       
   228 #define MAX_ETH_FRAME_SIZE	1536
       
   229 
       
   230 /* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
       
   231 #define TX_BUF_SIZE	MAX_ETH_FRAME_SIZE
       
   232 #define TX_BUF_TOT_LEN	(TX_BUF_SIZE * NUM_TX_DESC)
       
   233 
       
   234 /* PCI Tuning Parameters
       
   235    Threshold is bytes transferred to chip before transmission starts. */
       
   236 #define TX_FIFO_THRESH 256	/* In bytes, rounded down to 32 byte units. */
       
   237 
       
   238 /* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024, 7==end of packet. */
       
   239 #define RX_FIFO_THRESH	7	/* Rx buffer level before first PCI xfer.  */
       
   240 #define RX_DMA_BURST	7	/* Maximum PCI burst, '6' is 1024 */
       
   241 #define TX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */
       
   242 #define TX_RETRY	8	/* 0-15.  retries = 16 + (TX_RETRY * 16) */
       
   243 
       
   244 /* Operational parameters that usually are not changed. */
       
   245 /* Time in jiffies before concluding the transmitter is hung. */
       
   246 #define TX_TIMEOUT  (6*HZ)
       
   247 
       
   248 
       
   249 enum {
       
   250 	HAS_MII_XCVR = 0x010000,
       
   251 	HAS_CHIP_XCVR = 0x020000,
       
   252 	HAS_LNK_CHNG = 0x040000,
       
   253 };
       
   254 
       
   255 #define RTL_NUM_STATS 4		/* number of ETHTOOL_GSTATS u64's */
       
   256 #define RTL_REGS_VER 1		/* version of reg. data in ETHTOOL_GREGS */
       
   257 #define RTL_MIN_IO_SIZE 0x80
       
   258 #define RTL8139B_IO_SIZE 256
       
   259 
       
   260 #define RTL8129_CAPS	HAS_MII_XCVR
       
   261 #define RTL8139_CAPS	HAS_CHIP_XCVR|HAS_LNK_CHNG
       
   262 
       
   263 typedef enum {
       
   264 	RTL8139 = 0,
       
   265 	RTL8129,
       
   266 } board_t;
       
   267 
       
   268 
       
   269 /* indexed by board_t, above */
       
   270 static struct {
       
   271 	const char *name;
       
   272 	u32 hw_flags;
       
   273 } board_info[] __devinitdata = {
       
   274 	{ "RealTek RTL8139", RTL8139_CAPS },
       
   275 	{ "RealTek RTL8129", RTL8129_CAPS },
       
   276 };
       
   277 
       
   278 
       
   279 static struct pci_device_id rtl8139_pci_tbl[] = {
       
   280 	{0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   281 	{0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   282 	{0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   283 	{0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   284 	{0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   285 	{0x1186, 0x1300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   286 	{0x1186, 0x1340, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   287 	{0x13d1, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   288 	{0x1259, 0xa117, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   289 	{0x1259, 0xa11e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   290 	{0x14ea, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   291 	{0x14ea, 0xab07, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   292 	{0x11db, 0x1234, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   293 	{0x1432, 0x9130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   294 	{0x02ac, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   295 	{0x018a, 0x0106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   296 	{0x126c, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   297 	{0x1743, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   298 	{0x021b, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 }, 
       
   299 
       
   300 #ifdef CONFIG_SH_SECUREEDGE5410
       
   301 	/* Bogus 8139 silicon reports 8129 without external PROM :-( */
       
   302 	{0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
       
   303 #endif
       
   304 #ifdef CONFIG_8139TOO_8129
       
   305 	{0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8129 },
       
   306 #endif
       
   307 
       
   308 	/* some crazy cards report invalid vendor ids like
       
   309 	 * 0x0001 here.  The other ids are valid and constant,
       
   310 	 * so we simply don't match on the main vendor id.
       
   311 	 */
       
   312 	{PCI_ANY_ID, 0x8139, 0x10ec, 0x8139, 0, 0, RTL8139 },
       
   313 	{PCI_ANY_ID, 0x8139, 0x1186, 0x1300, 0, 0, RTL8139 },
       
   314 	{PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
       
   315 
       
   316 	{0,}
       
   317 };
       
   318 MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);
       
   319 
       
   320 static struct {
       
   321 	const char str[ETH_GSTRING_LEN];
       
   322 } ethtool_stats_keys[] = {
       
   323 	{ "early_rx" },
       
   324 	{ "tx_buf_mapped" },
       
   325 	{ "tx_timeouts" },
       
   326 	{ "rx_lost_in_ring" },
       
   327 };
       
   328 
       
   329 /* The rest of these values should never change. */
       
   330 
       
   331 /* Symbolic offsets to registers. */
       
   332 enum RTL8139_registers {
       
   333 	MAC0 = 0,		/* Ethernet hardware address. */
       
   334 	MAR0 = 8,		/* Multicast filter. */
       
   335 	TxStatus0 = 0x10,	/* Transmit status (Four 32bit registers). */
       
   336 	TxAddr0 = 0x20,		/* Tx descriptors (also four 32bit). */
       
   337 	RxBuf = 0x30,
       
   338 	ChipCmd = 0x37,
       
   339 	RxBufPtr = 0x38,
       
   340 	RxBufAddr = 0x3A,
       
   341 	IntrMask = 0x3C,
       
   342 	IntrStatus = 0x3E,
       
   343 	TxConfig = 0x40,
       
   344 	RxConfig = 0x44,
       
   345 	Timer = 0x48,		/* A general-purpose counter. */
       
   346 	RxMissed = 0x4C,	/* 24 bits valid, write clears. */
       
   347 	Cfg9346 = 0x50,
       
   348 	Config0 = 0x51,
       
   349 	Config1 = 0x52,
       
   350 	FlashReg = 0x54,
       
   351 	MediaStatus = 0x58,
       
   352 	Config3 = 0x59,
       
   353 	Config4 = 0x5A,		/* absent on RTL-8139A */
       
   354 	HltClk = 0x5B,
       
   355 	MultiIntr = 0x5C,
       
   356 	TxSummary = 0x60,
       
   357 	BasicModeCtrl = 0x62,
       
   358 	BasicModeStatus = 0x64,
       
   359 	NWayAdvert = 0x66,
       
   360 	NWayLPAR = 0x68,
       
   361 	NWayExpansion = 0x6A,
       
   362 	/* Undocumented registers, but required for proper operation. */
       
   363 	FIFOTMS = 0x70,		/* FIFO Control and test. */
       
   364 	CSCR = 0x74,		/* Chip Status and Configuration Register. */
       
   365 	PARA78 = 0x78,
       
   366 	PARA7c = 0x7c,		/* Magic transceiver parameter register. */
       
   367 	Config5 = 0xD8,		/* absent on RTL-8139A */
       
   368 };
       
   369 
       
   370 enum ClearBitMasks {
       
   371 	MultiIntrClear = 0xF000,
       
   372 	ChipCmdClear = 0xE2,
       
   373 	Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
       
   374 };
       
   375 
       
   376 enum ChipCmdBits {
       
   377 	CmdReset = 0x10,
       
   378 	CmdRxEnb = 0x08,
       
   379 	CmdTxEnb = 0x04,
       
   380 	RxBufEmpty = 0x01,
       
   381 };
       
   382 
       
   383 /* Interrupt register bits, using my own meaningful names. */
       
   384 enum IntrStatusBits {
       
   385 	PCIErr = 0x8000,
       
   386 	PCSTimeout = 0x4000,
       
   387 	RxFIFOOver = 0x40,
       
   388 	RxUnderrun = 0x20,
       
   389 	RxOverflow = 0x10,
       
   390 	TxErr = 0x08,
       
   391 	TxOK = 0x04,
       
   392 	RxErr = 0x02,
       
   393 	RxOK = 0x01,
       
   394 
       
   395 	RxAckBits = RxFIFOOver | RxOverflow | RxOK,
       
   396 };
       
   397 
       
   398 enum TxStatusBits {
       
   399 	TxHostOwns = 0x2000,
       
   400 	TxUnderrun = 0x4000,
       
   401 	TxStatOK = 0x8000,
       
   402 	TxOutOfWindow = 0x20000000,
       
   403 	TxAborted = 0x40000000,
       
   404 	TxCarrierLost = 0x80000000,
       
   405 };
       
   406 enum RxStatusBits {
       
   407 	RxMulticast = 0x8000,
       
   408 	RxPhysical = 0x4000,
       
   409 	RxBroadcast = 0x2000,
       
   410 	RxBadSymbol = 0x0020,
       
   411 	RxRunt = 0x0010,
       
   412 	RxTooLong = 0x0008,
       
   413 	RxCRCErr = 0x0004,
       
   414 	RxBadAlign = 0x0002,
       
   415 	RxStatusOK = 0x0001,
       
   416 };
       
   417 
       
   418 /* Bits in RxConfig. */
       
   419 enum rx_mode_bits {
       
   420 	AcceptErr = 0x20,
       
   421 	AcceptRunt = 0x10,
       
   422 	AcceptBroadcast = 0x08,
       
   423 	AcceptMulticast = 0x04,
       
   424 	AcceptMyPhys = 0x02,
       
   425 	AcceptAllPhys = 0x01,
       
   426 };
       
   427 
       
   428 /* Bits in TxConfig. */
       
   429 enum tx_config_bits {
       
   430 
       
   431         /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
       
   432         TxIFGShift = 24,
       
   433         TxIFG84 = (0 << TxIFGShift),    /* 8.4us / 840ns (10 / 100Mbps) */
       
   434         TxIFG88 = (1 << TxIFGShift),    /* 8.8us / 880ns (10 / 100Mbps) */
       
   435         TxIFG92 = (2 << TxIFGShift),    /* 9.2us / 920ns (10 / 100Mbps) */
       
   436         TxIFG96 = (3 << TxIFGShift),    /* 9.6us / 960ns (10 / 100Mbps) */
       
   437 
       
   438 	TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
       
   439 	TxCRC = (1 << 16),	/* DISABLE appending CRC to end of Tx packets */
       
   440 	TxClearAbt = (1 << 0),	/* Clear abort (WO) */
       
   441 	TxDMAShift = 8,		/* DMA burst value (0-7) is shifted this many bits */
       
   442 	TxRetryShift = 4,	/* TXRR value (0-15) is shifted this many bits */
       
   443 
       
   444 	TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
       
   445 };
       
   446 
       
   447 /* Bits in Config1 */
       
   448 enum Config1Bits {
       
   449 	Cfg1_PM_Enable = 0x01,
       
   450 	Cfg1_VPD_Enable = 0x02,
       
   451 	Cfg1_PIO = 0x04,
       
   452 	Cfg1_MMIO = 0x08,
       
   453 	LWAKE = 0x10,		/* not on 8139, 8139A */
       
   454 	Cfg1_Driver_Load = 0x20,
       
   455 	Cfg1_LED0 = 0x40,
       
   456 	Cfg1_LED1 = 0x80,
       
   457 	SLEEP = (1 << 1),	/* only on 8139, 8139A */
       
   458 	PWRDN = (1 << 0),	/* only on 8139, 8139A */
       
   459 };
       
   460 
       
   461 /* Bits in Config3 */
       
   462 enum Config3Bits {
       
   463 	Cfg3_FBtBEn    = (1 << 0), /* 1 = Fast Back to Back */
       
   464 	Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */
       
   465 	Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */
       
   466 	Cfg3_CardB_En  = (1 << 3), /* 1 = enable CardBus registers */
       
   467 	Cfg3_LinkUp    = (1 << 4), /* 1 = wake up on link up */
       
   468 	Cfg3_Magic     = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
       
   469 	Cfg3_PARM_En   = (1 << 6), /* 0 = software can set twister parameters */
       
   470 	Cfg3_GNTSel    = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
       
   471 };
       
   472 
       
   473 /* Bits in Config4 */
       
   474 enum Config4Bits {
       
   475 	LWPTN = (1 << 2),	/* not on 8139, 8139A */
       
   476 };
       
   477 
       
   478 /* Bits in Config5 */
       
   479 enum Config5Bits {
       
   480 	Cfg5_PME_STS     = (1 << 0), /* 1 = PCI reset resets PME_Status */
       
   481 	Cfg5_LANWake     = (1 << 1), /* 1 = enable LANWake signal */
       
   482 	Cfg5_LDPS        = (1 << 2), /* 0 = save power when link is down */
       
   483 	Cfg5_FIFOAddrPtr = (1 << 3), /* Realtek internal SRAM testing */
       
   484 	Cfg5_UWF         = (1 << 4), /* 1 = accept unicast wakeup frame */
       
   485 	Cfg5_MWF         = (1 << 5), /* 1 = accept multicast wakeup frame */
       
   486 	Cfg5_BWF         = (1 << 6), /* 1 = accept broadcast wakeup frame */
       
   487 };
       
   488 
       
   489 enum RxConfigBits {
       
   490 	/* rx fifo threshold */
       
   491 	RxCfgFIFOShift = 13,
       
   492 	RxCfgFIFONone = (7 << RxCfgFIFOShift),
       
   493 
       
   494 	/* Max DMA burst */
       
   495 	RxCfgDMAShift = 8,
       
   496 	RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
       
   497 
       
   498 	/* rx ring buffer length */
       
   499 	RxCfgRcv8K = 0,
       
   500 	RxCfgRcv16K = (1 << 11),
       
   501 	RxCfgRcv32K = (1 << 12),
       
   502 	RxCfgRcv64K = (1 << 11) | (1 << 12),
       
   503 
       
   504 	/* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
       
   505 	RxNoWrap = (1 << 7),
       
   506 };
       
   507 
       
   508 /* Twister tuning parameters from RealTek.
       
   509    Completely undocumented, but required to tune bad links on some boards. */
       
   510 enum CSCRBits {
       
   511 	CSCR_LinkOKBit = 0x0400,
       
   512 	CSCR_LinkChangeBit = 0x0800,
       
   513 	CSCR_LinkStatusBits = 0x0f000,
       
   514 	CSCR_LinkDownOffCmd = 0x003c0,
       
   515 	CSCR_LinkDownCmd = 0x0f3c0,
       
   516 };
       
   517 
       
   518 enum Cfg9346Bits {
       
   519 	Cfg9346_Lock = 0x00,
       
   520 	Cfg9346_Unlock = 0xC0,
       
   521 };
       
   522 
       
   523 typedef enum {
       
   524 	CH_8139 = 0,
       
   525 	CH_8139_K,
       
   526 	CH_8139A,
       
   527 	CH_8139A_G,
       
   528 	CH_8139B,
       
   529 	CH_8130,
       
   530 	CH_8139C,
       
   531 	CH_8100,
       
   532 	CH_8100B_8139D,
       
   533 	CH_8101,
       
   534 } chip_t;
       
   535 
       
   536 enum chip_flags {
       
   537 	HasHltClk = (1 << 0),
       
   538 	HasLWake = (1 << 1),
       
   539 };
       
   540 
       
   541 #define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
       
   542 	(b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
       
   543 #define HW_REVID_MASK	HW_REVID(1, 1, 1, 1, 1, 1, 1)
       
   544 
       
   545 /* directly indexed by chip_t, above */
       
   546 const static struct {
       
   547 	const char *name;
       
   548 	u32 version; /* from RTL8139C/RTL8139D docs */
       
   549 	u32 flags;
       
   550 } rtl_chip_info[] = {
       
   551 	{ "RTL-8139",
       
   552 	  HW_REVID(1, 0, 0, 0, 0, 0, 0),
       
   553 	  HasHltClk,
       
   554 	},
       
   555 
       
   556 	{ "RTL-8139 rev K",
       
   557 	  HW_REVID(1, 1, 0, 0, 0, 0, 0),
       
   558 	  HasHltClk,
       
   559 	},
       
   560 
       
   561 	{ "RTL-8139A",
       
   562 	  HW_REVID(1, 1, 1, 0, 0, 0, 0),
       
   563 	  HasHltClk, /* XXX undocumented? */
       
   564 	},
       
   565 
       
   566 	{ "RTL-8139A rev G",
       
   567 	  HW_REVID(1, 1, 1, 0, 0, 1, 0),
       
   568 	  HasHltClk, /* XXX undocumented? */
       
   569 	},
       
   570 
       
   571 	{ "RTL-8139B",
       
   572 	  HW_REVID(1, 1, 1, 1, 0, 0, 0),
       
   573 	  HasLWake,
       
   574 	},
       
   575 
       
   576 	{ "RTL-8130",
       
   577 	  HW_REVID(1, 1, 1, 1, 1, 0, 0),
       
   578 	  HasLWake,
       
   579 	},
       
   580 
       
   581 	{ "RTL-8139C",
       
   582 	  HW_REVID(1, 1, 1, 0, 1, 0, 0),
       
   583 	  HasLWake,
       
   584 	},
       
   585 
       
   586 	{ "RTL-8100",
       
   587 	  HW_REVID(1, 1, 1, 1, 0, 1, 0),
       
   588  	  HasLWake,
       
   589  	},
       
   590 
       
   591 	{ "RTL-8100B/8139D",
       
   592 	  HW_REVID(1, 1, 1, 0, 1, 0, 1),
       
   593 	  HasLWake,
       
   594 	},
       
   595 
       
   596 	{ "RTL-8101",
       
   597 	  HW_REVID(1, 1, 1, 0, 1, 1, 1),
       
   598 	  HasLWake,
       
   599 	},
       
   600 };
       
   601 
       
   602 struct rtl_extra_stats {
       
   603 	unsigned long early_rx;
       
   604 	unsigned long tx_buf_mapped;
       
   605 	unsigned long tx_timeouts;
       
   606 	unsigned long rx_lost_in_ring;
       
   607 };
       
   608 
       
   609 struct rtl8139_private {
       
   610 	void __iomem *mmio_addr;
       
   611 	int drv_flags;
       
   612 	struct pci_dev *pci_dev;
       
   613 	u32 msg_enable;
       
   614 	struct net_device_stats stats;
       
   615 	unsigned char *rx_ring;
       
   616 	unsigned int cur_rx;	/* Index into the Rx buffer of next Rx pkt. */
       
   617 	unsigned int tx_flag;
       
   618 	unsigned long cur_tx;
       
   619 	unsigned long dirty_tx;
       
   620 	unsigned char *tx_buf[NUM_TX_DESC];	/* Tx bounce buffers */
       
   621 	unsigned char *tx_bufs;	/* Tx bounce buffer region. */
       
   622 	dma_addr_t rx_ring_dma;
       
   623 	dma_addr_t tx_bufs_dma;
       
   624 	signed char phys[4];		/* MII device addresses. */
       
   625 	char twistie, twist_row, twist_col;	/* Twister tune state. */
       
   626 	unsigned int default_port:4;	/* Last dev->if_port value. */
       
   627 	spinlock_t lock;
       
   628 	spinlock_t rx_lock;
       
   629 	chip_t chipset;
       
   630 	pid_t thr_pid;
       
   631 	wait_queue_head_t thr_wait;
       
   632 	struct completion thr_exited;
       
   633 	u32 rx_config;
       
   634 	struct rtl_extra_stats xstats;
       
   635 	int time_to_die;
       
   636 	struct mii_if_info mii;
       
   637 	unsigned int regs_len;
       
   638 	unsigned long fifo_copy_timeout;
       
   639 };
       
   640 
       
   641 MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
       
   642 MODULE_DESCRIPTION ("RealTek RTL-8139 Fast Ethernet driver");
       
   643 MODULE_LICENSE("GPL");
       
   644 MODULE_VERSION(DRV_VERSION);
       
   645 
       
   646 module_param(multicast_filter_limit, int, 0);
       
   647 module_param_array(media, int, NULL, 0);
       
   648 module_param_array(full_duplex, int, NULL, 0);
       
   649 module_param(debug, int, 0);
       
   650 MODULE_PARM_DESC (debug, "8139too bitmapped message enable number");
       
   651 MODULE_PARM_DESC (multicast_filter_limit, "8139too maximum number of filtered multicast addresses");
       
   652 MODULE_PARM_DESC (media, "8139too: Bits 4+9: force full duplex, bit 5: 100Mbps");
       
   653 MODULE_PARM_DESC (full_duplex, "8139too: Force full duplex for board(s) (1)");
       
   654 
       
   655 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   656 
       
   657 MODULE_PARM(ec_device_index, "i");
       
   658 MODULE_PARM_DESC(ec_device_index, "Index of the device reserved for EtherCAT.");
       
   659 
       
   660 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   661 
       
   662 static int read_eeprom (void __iomem *ioaddr, int location, int addr_len);
       
   663 static int rtl8139_open (struct net_device *dev);
       
   664 static int mdio_read (struct net_device *dev, int phy_id, int location);
       
   665 static void mdio_write (struct net_device *dev, int phy_id, int location,
       
   666 			int val);
       
   667 static void rtl8139_start_thread(struct net_device *dev);
       
   668 static void rtl8139_tx_timeout (struct net_device *dev);
       
   669 static void rtl8139_init_ring (struct net_device *dev);
       
   670 static int rtl8139_start_xmit (struct sk_buff *skb,
       
   671 			       struct net_device *dev);
       
   672 int rtl8139_poll(struct net_device *dev, int *budget);
       
   673 #ifdef CONFIG_NET_POLL_CONTROLLER
       
   674 static void rtl8139_poll_controller(struct net_device *dev);
       
   675 #endif
       
   676 irqreturn_t rtl8139_interrupt (int irq, void *dev_instance,
       
   677                                struct pt_regs *regs);
       
   678 static int rtl8139_close (struct net_device *dev);
       
   679 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
       
   680 static struct net_device_stats *rtl8139_get_stats (struct net_device *dev);
       
   681 static void rtl8139_set_rx_mode (struct net_device *dev);
       
   682 static void __set_rx_mode (struct net_device *dev);
       
   683 static void rtl8139_hw_start (struct net_device *dev);
       
   684 static struct ethtool_ops rtl8139_ethtool_ops;
       
   685 
       
   686 /* write MMIO register, with flush */
       
   687 /* Flush avoids rtl8139 bug w/ posted MMIO writes */
       
   688 #define RTL_W8_F(reg, val8)	do { iowrite8 ((val8), ioaddr + (reg)); ioread8 (ioaddr + (reg)); } while (0)
       
   689 #define RTL_W16_F(reg, val16)	do { iowrite16 ((val16), ioaddr + (reg)); ioread16 (ioaddr + (reg)); } while (0)
       
   690 #define RTL_W32_F(reg, val32)	do { iowrite32 ((val32), ioaddr + (reg)); ioread32 (ioaddr + (reg)); } while (0)
       
   691 
       
   692 
       
   693 #define MMIO_FLUSH_AUDIT_COMPLETE 1
       
   694 #if MMIO_FLUSH_AUDIT_COMPLETE
       
   695 
       
   696 /* write MMIO register */
       
   697 #define RTL_W8(reg, val8)	iowrite8 ((val8), ioaddr + (reg))
       
   698 #define RTL_W16(reg, val16)	iowrite16 ((val16), ioaddr + (reg))
       
   699 #define RTL_W32(reg, val32)	iowrite32 ((val32), ioaddr + (reg))
       
   700 
       
   701 #else
       
   702 
       
   703 /* write MMIO register, then flush */
       
   704 #define RTL_W8		RTL_W8_F
       
   705 #define RTL_W16		RTL_W16_F
       
   706 #define RTL_W32		RTL_W32_F
       
   707 
       
   708 #endif /* MMIO_FLUSH_AUDIT_COMPLETE */
       
   709 
       
   710 /* read MMIO register */
       
   711 #define RTL_R8(reg)		ioread8 (ioaddr + (reg))
       
   712 #define RTL_R16(reg)		ioread16 (ioaddr + (reg))
       
   713 #define RTL_R32(reg)		((unsigned long) ioread32 (ioaddr + (reg)))
       
   714 
       
   715 
       
   716 static const u16 rtl8139_intr_mask =
       
   717 	PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver |
       
   718 	TxErr | TxOK | RxErr | RxOK;
       
   719 
       
   720 static const u16 rtl8139_norx_intr_mask =
       
   721 	PCIErr | PCSTimeout | RxUnderrun |
       
   722 	TxErr | TxOK | RxErr ;
       
   723 
       
   724 #if RX_BUF_IDX == 0
       
   725 static const unsigned int rtl8139_rx_config =
       
   726 	RxCfgRcv8K | RxNoWrap |
       
   727 	(RX_FIFO_THRESH << RxCfgFIFOShift) |
       
   728 	(RX_DMA_BURST << RxCfgDMAShift);
       
   729 #elif RX_BUF_IDX == 1
       
   730 static const unsigned int rtl8139_rx_config =
       
   731 	RxCfgRcv16K | RxNoWrap |
       
   732 	(RX_FIFO_THRESH << RxCfgFIFOShift) |
       
   733 	(RX_DMA_BURST << RxCfgDMAShift);
       
   734 #elif RX_BUF_IDX == 2
       
   735 static const unsigned int rtl8139_rx_config =
       
   736 	RxCfgRcv32K | RxNoWrap |
       
   737 	(RX_FIFO_THRESH << RxCfgFIFOShift) |
       
   738 	(RX_DMA_BURST << RxCfgDMAShift);
       
   739 #elif RX_BUF_IDX == 3
       
   740 static const unsigned int rtl8139_rx_config =
       
   741 	RxCfgRcv64K |
       
   742 	(RX_FIFO_THRESH << RxCfgFIFOShift) |
       
   743 	(RX_DMA_BURST << RxCfgDMAShift);
       
   744 #else
       
   745 #error "Invalid configuration for 8139_RXBUF_IDX"
       
   746 #endif
       
   747 
       
   748 static const unsigned int rtl8139_tx_config =
       
   749 	TxIFG96 | (TX_DMA_BURST << TxDMAShift) | (TX_RETRY << TxRetryShift);
       
   750 
       
   751 static void __rtl8139_cleanup_dev (struct net_device *dev)
       
   752 {
       
   753 	struct rtl8139_private *tp = netdev_priv(dev);
       
   754 	struct pci_dev *pdev;
       
   755 
       
   756 	assert (dev != NULL);
       
   757 	assert (tp->pci_dev != NULL);
       
   758 	pdev = tp->pci_dev;
       
   759 
       
   760 #ifdef USE_IO_OPS
       
   761 	if (tp->mmio_addr)
       
   762 		ioport_unmap (tp->mmio_addr);
       
   763 #else
       
   764 	if (tp->mmio_addr)
       
   765 		pci_iounmap (pdev, tp->mmio_addr);
       
   766 #endif /* USE_IO_OPS */
       
   767 
       
   768 	/* it's ok to call this even if we have no regions to free */
       
   769 	pci_release_regions (pdev);
       
   770 
       
   771 	free_netdev(dev);
       
   772 	pci_set_drvdata (pdev, NULL);
       
   773 }
       
   774 
       
   775 
       
   776 static void rtl8139_chip_reset (void __iomem *ioaddr)
       
   777 {
       
   778 	int i;
       
   779 
       
   780 	/* Soft reset the chip. */
       
   781 	RTL_W8 (ChipCmd, CmdReset);
       
   782 
       
   783 	/* Check that the chip has finished the reset. */
       
   784 	for (i = 1000; i > 0; i--) {
       
   785 		barrier();
       
   786 		if ((RTL_R8 (ChipCmd) & CmdReset) == 0)
       
   787 		    break;
       
   788 		udelay (10);
       
   789 	}
       
   790 }
       
   791 
       
   792 
       
   793 static int __devinit rtl8139_init_board (struct pci_dev *pdev,
       
   794 					 struct net_device **dev_out)
       
   795 {
       
   796 	void __iomem *ioaddr;
       
   797 	struct net_device *dev;
       
   798 	struct rtl8139_private *tp;
       
   799 	u8 tmp8;
       
   800 	int rc, disable_dev_on_err = 0;
       
   801 	unsigned int i;
       
   802 	unsigned long pio_start, pio_end, pio_flags, pio_len;
       
   803 	unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
       
   804 	u32 version;
       
   805 
       
   806 	assert (pdev != NULL);
       
   807 
       
   808 	*dev_out = NULL;
       
   809 
       
   810 	/* dev and priv zeroed in alloc_etherdev */
       
   811 	dev = alloc_etherdev (sizeof (*tp));
       
   812 	if (dev == NULL) {
       
   813 		printk (KERN_ERR PFX "%s: Unable to alloc new net device\n", pci_name(pdev));
       
   814 		return -ENOMEM;
       
   815 	}
       
   816 	SET_MODULE_OWNER(dev);
       
   817 	SET_NETDEV_DEV(dev, &pdev->dev);
       
   818 
       
   819 	tp = netdev_priv(dev);
       
   820 	tp->pci_dev = pdev;
       
   821 
       
   822 	/* enable device (incl. PCI PM wakeup and hotplug setup) */
       
   823 	rc = pci_enable_device (pdev);
       
   824 	if (rc)
       
   825 		goto err_out;
       
   826 
       
   827 	pio_start = pci_resource_start (pdev, 0);
       
   828 	pio_end = pci_resource_end (pdev, 0);
       
   829 	pio_flags = pci_resource_flags (pdev, 0);
       
   830 	pio_len = pci_resource_len (pdev, 0);
       
   831 
       
   832 	mmio_start = pci_resource_start (pdev, 1);
       
   833 	mmio_end = pci_resource_end (pdev, 1);
       
   834 	mmio_flags = pci_resource_flags (pdev, 1);
       
   835 	mmio_len = pci_resource_len (pdev, 1);
       
   836 
       
   837 	/* set this immediately, we need to know before
       
   838 	 * we talk to the chip directly */
       
   839 	DPRINTK("PIO region size == 0x%02X\n", pio_len);
       
   840 	DPRINTK("MMIO region size == 0x%02lX\n", mmio_len);
       
   841 
       
   842 #ifdef USE_IO_OPS
       
   843 	/* make sure PCI base addr 0 is PIO */
       
   844 	if (!(pio_flags & IORESOURCE_IO)) {
       
   845 		printk (KERN_ERR PFX "%s: region #0 not a PIO resource, aborting\n", pci_name(pdev));
       
   846 		rc = -ENODEV;
       
   847 		goto err_out;
       
   848 	}
       
   849 	/* check for weird/broken PCI region reporting */
       
   850 	if (pio_len < RTL_MIN_IO_SIZE) {
       
   851 		printk (KERN_ERR PFX "%s: Invalid PCI I/O region size(s), aborting\n", pci_name(pdev));
       
   852 		rc = -ENODEV;
       
   853 		goto err_out;
       
   854 	}
       
   855 #else
       
   856 	/* make sure PCI base addr 1 is MMIO */
       
   857 	if (!(mmio_flags & IORESOURCE_MEM)) {
       
   858 		printk (KERN_ERR PFX "%s: region #1 not an MMIO resource, aborting\n", pci_name(pdev));
       
   859 		rc = -ENODEV;
       
   860 		goto err_out;
       
   861 	}
       
   862 	if (mmio_len < RTL_MIN_IO_SIZE) {
       
   863 		printk (KERN_ERR PFX "%s: Invalid PCI mem region size(s), aborting\n", pci_name(pdev));
       
   864 		rc = -ENODEV;
       
   865 		goto err_out;
       
   866 	}
       
   867 #endif
       
   868 
       
   869 	rc = pci_request_regions (pdev, "8139too");
       
   870 	if (rc)
       
   871 		goto err_out;
       
   872 	disable_dev_on_err = 1;
       
   873 
       
   874 	/* enable PCI bus-mastering */
       
   875 	pci_set_master (pdev);
       
   876 
       
   877 #ifdef USE_IO_OPS
       
   878 	ioaddr = ioport_map(pio_start, pio_len);
       
   879 	if (!ioaddr) {
       
   880 		printk (KERN_ERR PFX "%s: cannot map PIO, aborting\n", pci_name(pdev));
       
   881 		rc = -EIO;
       
   882 		goto err_out;
       
   883 	}
       
   884 	dev->base_addr = pio_start;
       
   885 	tp->mmio_addr = ioaddr;
       
   886 	tp->regs_len = pio_len;
       
   887 #else
       
   888 	/* ioremap MMIO region */
       
   889 	ioaddr = pci_iomap(pdev, 1, 0);
       
   890 	if (ioaddr == NULL) {
       
   891 		printk (KERN_ERR PFX "%s: cannot remap MMIO, aborting\n", pci_name(pdev));
       
   892 		rc = -EIO;
       
   893 		goto err_out;
       
   894 	}
       
   895 	dev->base_addr = (long) ioaddr;
       
   896 	tp->mmio_addr = ioaddr;
       
   897 	tp->regs_len = mmio_len;
       
   898 #endif /* USE_IO_OPS */
       
   899 
       
   900 	/* Bring old chips out of low-power mode. */
       
   901 	RTL_W8 (HltClk, 'R');
       
   902 
       
   903 	/* check for missing/broken hardware */
       
   904 	if (RTL_R32 (TxConfig) == 0xFFFFFFFF) {
       
   905 		printk (KERN_ERR PFX "%s: Chip not responding, ignoring board\n",
       
   906 			pci_name(pdev));
       
   907 		rc = -EIO;
       
   908 		goto err_out;
       
   909 	}
       
   910 
       
   911 	/* identify chip attached to board */
       
   912 	version = RTL_R32 (TxConfig) & HW_REVID_MASK;
       
   913 	for (i = 0; i < ARRAY_SIZE (rtl_chip_info); i++)
       
   914 		if (version == rtl_chip_info[i].version) {
       
   915 			tp->chipset = i;
       
   916 			goto match;
       
   917 		}
       
   918 
       
   919 	/* if unknown chip, assume array element #0, original RTL-8139 in this case */
       
   920 	printk (KERN_DEBUG PFX "%s: unknown chip version, assuming RTL-8139\n",
       
   921 		pci_name(pdev));
       
   922         printk (KERN_DEBUG PFX "%s: TxConfig = 0x%lx\n", pci_name(pdev), RTL_R32 (TxConfig));
       
   923 	tp->chipset = 0;
       
   924 
       
   925 match:
       
   926 	DPRINTK ("chipset id (%d) == index %d, '%s'\n",
       
   927 		 version, i, rtl_chip_info[i].name);
       
   928 
       
   929 	if (tp->chipset >= CH_8139B) {
       
   930 		u8 new_tmp8 = tmp8 = RTL_R8 (Config1);
       
   931 		DPRINTK("PCI PM wakeup\n");
       
   932 		if ((rtl_chip_info[tp->chipset].flags & HasLWake) &&
       
   933 		    (tmp8 & LWAKE))
       
   934 			new_tmp8 &= ~LWAKE;
       
   935 		new_tmp8 |= Cfg1_PM_Enable;
       
   936 		if (new_tmp8 != tmp8) {
       
   937 			RTL_W8 (Cfg9346, Cfg9346_Unlock);
       
   938 			RTL_W8 (Config1, tmp8);
       
   939 			RTL_W8 (Cfg9346, Cfg9346_Lock);
       
   940 		}
       
   941 		if (rtl_chip_info[tp->chipset].flags & HasLWake) {
       
   942 			tmp8 = RTL_R8 (Config4);
       
   943 			if (tmp8 & LWPTN) {
       
   944 				RTL_W8 (Cfg9346, Cfg9346_Unlock);
       
   945 				RTL_W8 (Config4, tmp8 & ~LWPTN);
       
   946 				RTL_W8 (Cfg9346, Cfg9346_Lock);
       
   947 			}
       
   948 		}
       
   949 	} else {
       
   950 		DPRINTK("Old chip wakeup\n");
       
   951 		tmp8 = RTL_R8 (Config1);
       
   952 		tmp8 &= ~(SLEEP | PWRDN);
       
   953 		RTL_W8 (Config1, tmp8);
       
   954 	}
       
   955 
       
   956 	rtl8139_chip_reset (ioaddr);
       
   957 
       
   958 	*dev_out = dev;
       
   959 	return 0;
       
   960 
       
   961 err_out:
       
   962 	__rtl8139_cleanup_dev (dev);
       
   963 	if (disable_dev_on_err)
       
   964 		pci_disable_device (pdev);
       
   965 	return rc;
       
   966 }
       
   967 
       
   968 
       
   969 static int __devinit rtl8139_init_one (struct pci_dev *pdev,
       
   970 				       const struct pci_device_id *ent)
       
   971 {
       
   972 	struct net_device *dev = NULL;
       
   973 	struct rtl8139_private *tp;
       
   974 	int i, addr_len, option;
       
   975 	void __iomem *ioaddr;
       
   976 	static int board_idx = -1;
       
   977 	u8 pci_rev;
       
   978 
       
   979 	assert (pdev != NULL);
       
   980 	assert (ent != NULL);
       
   981 
       
   982 	board_idx++;
       
   983 
       
   984 	/* when we're built into the kernel, the driver version message
       
   985 	 * is only printed if at least one 8139 board has been found
       
   986 	 */
       
   987 #ifndef MODULE
       
   988 	{
       
   989 		static int printed_version;
       
   990 		if (!printed_version++)
       
   991 			printk (KERN_INFO RTL8139_DRIVER_NAME "\n");
       
   992 	}
       
   993 #endif
       
   994 
       
   995 	pci_read_config_byte(pdev, PCI_REVISION_ID, &pci_rev);
       
   996 
       
   997 	if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
       
   998 	    pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pci_rev >= 0x20) {
       
   999 		printk(KERN_INFO PFX "pci dev %s (id %04x:%04x rev %02x) is an enhanced 8139C+ chip\n",
       
  1000 		       pci_name(pdev), pdev->vendor, pdev->device, pci_rev);
       
  1001 		printk(KERN_INFO PFX "Use the \"8139cp\" driver for improved performance and stability.\n");
       
  1002 	}
       
  1003 
       
  1004 	i = rtl8139_init_board (pdev, &dev);
       
  1005 	if (i < 0)
       
  1006 		return i;
       
  1007 
       
  1008 	assert (dev != NULL);
       
  1009 	tp = netdev_priv(dev);
       
  1010 
       
  1011 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1012 
       
  1013 	if (board_idx == ec_device_index)
       
  1014         {
       
  1015           printk("EtherCAT registering board %d.\n", board_idx);
       
  1016 
       
  1017           if (EtherCAT_device_assign(&rtl_ecat_dev, dev) < 0)
       
  1018             goto err_out;
       
  1019 
       
  1020 	  strcpy(dev->name,"ECAT");  //device name überschreiben
       
  1021 	}
       
  1022 
       
  1023 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1024 
       
  1025 	ioaddr = tp->mmio_addr;
       
  1026 	assert (ioaddr != NULL);
       
  1027 
       
  1028 	addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
       
  1029 	for (i = 0; i < 3; i++)
       
  1030 		((u16 *) (dev->dev_addr))[i] =
       
  1031 		    le16_to_cpu (read_eeprom (ioaddr, i + 7, addr_len));
       
  1032 
       
  1033 	/* The Rtl8139-specific entries in the device structure. */
       
  1034 	dev->open = rtl8139_open;
       
  1035 	dev->hard_start_xmit = rtl8139_start_xmit;
       
  1036 	dev->poll = rtl8139_poll;
       
  1037 	dev->weight = 64;
       
  1038 	dev->stop = rtl8139_close;
       
  1039 	dev->get_stats = rtl8139_get_stats;
       
  1040 	dev->set_multicast_list = rtl8139_set_rx_mode;
       
  1041 	dev->do_ioctl = netdev_ioctl;
       
  1042 	dev->ethtool_ops = &rtl8139_ethtool_ops;
       
  1043 	dev->tx_timeout = rtl8139_tx_timeout;
       
  1044 	dev->watchdog_timeo = TX_TIMEOUT;
       
  1045 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  1046 	dev->poll_controller = rtl8139_poll_controller;
       
  1047 #endif
       
  1048 
       
  1049 	/* note: the hardware is not capable of sg/csum/highdma, however
       
  1050 	 * through the use of skb_copy_and_csum_dev we enable these
       
  1051 	 * features
       
  1052 	 */
       
  1053 	dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA;
       
  1054 
       
  1055 	dev->irq = pdev->irq;
       
  1056 
       
  1057 	/* tp zeroed and aligned in alloc_etherdev */
       
  1058 	tp = netdev_priv(dev);
       
  1059 
       
  1060 	/* note: tp->chipset set in rtl8139_init_board */
       
  1061 	tp->drv_flags = board_info[ent->driver_data].hw_flags;
       
  1062 	tp->mmio_addr = ioaddr;
       
  1063 	tp->msg_enable =
       
  1064 		(debug < 0 ? RTL8139_DEF_MSG_ENABLE : ((1 << debug) - 1));
       
  1065 	spin_lock_init (&tp->lock);
       
  1066 	spin_lock_init (&tp->rx_lock);
       
  1067 
       
  1068 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1069 
       
  1070 	if (board_idx == ec_device_index)
       
  1071         {
       
  1072           rtl_ecat_dev.lock = &tp->lock;
       
  1073 	}
       
  1074 
       
  1075 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1076 
       
  1077 	init_waitqueue_head (&tp->thr_wait);
       
  1078 	init_completion (&tp->thr_exited);
       
  1079 	tp->mii.dev = dev;
       
  1080 	tp->mii.mdio_read = mdio_read;
       
  1081 	tp->mii.mdio_write = mdio_write;
       
  1082 	tp->mii.phy_id_mask = 0x3f;
       
  1083 	tp->mii.reg_num_mask = 0x1f;
       
  1084 
       
  1085 	/* dev is fully set up and ready to use now */
       
  1086 
       
  1087 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1088 
       
  1089 	/* EtherCAT-Karten nicht beim Stack anmelden. */
       
  1090         if (dev != rtl_ecat_dev.dev)
       
  1091 	{
       
  1092           DPRINTK("About to register device named %s (%p)...\n", dev->name, dev);
       
  1093           i = register_netdev (dev);
       
  1094           if (i) goto err_out;
       
  1095 	}
       
  1096 
       
  1097 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1098 
       
  1099 	pci_set_drvdata (pdev, dev);
       
  1100 
       
  1101 	printk (KERN_INFO "%s: %s at 0x%lx, "
       
  1102 		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
       
  1103 		"IRQ %d\n",
       
  1104 		dev->name,
       
  1105 		board_info[ent->driver_data].name,
       
  1106 		dev->base_addr,
       
  1107 		dev->dev_addr[0], dev->dev_addr[1],
       
  1108 		dev->dev_addr[2], dev->dev_addr[3],
       
  1109 		dev->dev_addr[4], dev->dev_addr[5],
       
  1110 		dev->irq);
       
  1111 
       
  1112 	printk (KERN_DEBUG "%s:  Identified 8139 chip type '%s'\n",
       
  1113 		dev->name, rtl_chip_info[tp->chipset].name);
       
  1114 
       
  1115 	/* Find the connected MII xcvrs.
       
  1116 	   Doing this in open() would allow detecting external xcvrs later, but
       
  1117 	   takes too much time. */
       
  1118 #ifdef CONFIG_8139TOO_8129
       
  1119 	if (tp->drv_flags & HAS_MII_XCVR) {
       
  1120 		int phy, phy_idx = 0;
       
  1121 		for (phy = 0; phy < 32 && phy_idx < sizeof(tp->phys); phy++) {
       
  1122 			int mii_status = mdio_read(dev, phy, 1);
       
  1123 			if (mii_status != 0xffff  &&  mii_status != 0x0000) {
       
  1124 				u16 advertising = mdio_read(dev, phy, 4);
       
  1125 				tp->phys[phy_idx++] = phy;
       
  1126 				printk(KERN_INFO "%s: MII transceiver %d status 0x%4.4x "
       
  1127 					   "advertising %4.4x.\n",
       
  1128 					   dev->name, phy, mii_status, advertising);
       
  1129 			}
       
  1130 		}
       
  1131 		if (phy_idx == 0) {
       
  1132 			printk(KERN_INFO "%s: No MII transceivers found!  Assuming SYM "
       
  1133 				   "transceiver.\n",
       
  1134 				   dev->name);
       
  1135 			tp->phys[0] = 32;
       
  1136 		}
       
  1137 	} else
       
  1138 #endif
       
  1139 		tp->phys[0] = 32;
       
  1140 	tp->mii.phy_id = tp->phys[0];
       
  1141 
       
  1142 	/* The lower four bits are the media type. */
       
  1143 	option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
       
  1144 	if (option > 0) {
       
  1145 		tp->mii.full_duplex = (option & 0x210) ? 1 : 0;
       
  1146 		tp->default_port = option & 0xFF;
       
  1147 		if (tp->default_port)
       
  1148 			tp->mii.force_media = 1;
       
  1149 	}
       
  1150 	if (board_idx < MAX_UNITS  &&  full_duplex[board_idx] > 0)
       
  1151 		tp->mii.full_duplex = full_duplex[board_idx];
       
  1152 	if (tp->mii.full_duplex) {
       
  1153 		printk(KERN_INFO "%s: Media type forced to Full Duplex.\n", dev->name);
       
  1154 		/* Changing the MII-advertised media because might prevent
       
  1155 		   re-connection. */
       
  1156 		tp->mii.force_media = 1;
       
  1157 	}
       
  1158 	if (tp->default_port) {
       
  1159 		printk(KERN_INFO "  Forcing %dMbps %s-duplex operation.\n",
       
  1160 			   (option & 0x20 ? 100 : 10),
       
  1161 			   (option & 0x10 ? "full" : "half"));
       
  1162 		mdio_write(dev, tp->phys[0], 0,
       
  1163 				   ((option & 0x20) ? 0x2000 : 0) | 	/* 100Mbps? */
       
  1164 				   ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
       
  1165 	}
       
  1166 
       
  1167 	/* Put the chip into low-power mode. */
       
  1168 	if (rtl_chip_info[tp->chipset].flags & HasHltClk)
       
  1169 		RTL_W8 (HltClk, 'H');	/* 'R' would leave the clock running. */
       
  1170 
       
  1171 	return 0;
       
  1172 
       
  1173 err_out:
       
  1174 	__rtl8139_cleanup_dev (dev);
       
  1175 	pci_disable_device (pdev);
       
  1176 	return i;
       
  1177 }
       
  1178 
       
  1179 
       
  1180 static void __devexit rtl8139_remove_one (struct pci_dev *pdev)
       
  1181 {
       
  1182 	struct net_device *dev = pci_get_drvdata (pdev);
       
  1183 
       
  1184 	assert (dev != NULL);
       
  1185 
       
  1186         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1187 
       
  1188         if (dev != rtl_ecat_dev.dev)
       
  1189         {
       
  1190           unregister_netdev (dev);
       
  1191 	}
       
  1192 
       
  1193 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1194 
       
  1195 	__rtl8139_cleanup_dev (dev);
       
  1196 	pci_disable_device (pdev);
       
  1197 }
       
  1198 
       
  1199 
       
  1200 /* Serial EEPROM section. */
       
  1201 
       
  1202 /*  EEPROM_Ctrl bits. */
       
  1203 #define EE_SHIFT_CLK	0x04	/* EEPROM shift clock. */
       
  1204 #define EE_CS			0x08	/* EEPROM chip select. */
       
  1205 #define EE_DATA_WRITE	0x02	/* EEPROM chip data in. */
       
  1206 #define EE_WRITE_0		0x00
       
  1207 #define EE_WRITE_1		0x02
       
  1208 #define EE_DATA_READ	0x01	/* EEPROM chip data out. */
       
  1209 #define EE_ENB			(0x80 | EE_CS)
       
  1210 
       
  1211 /* Delay between EEPROM clock transitions.
       
  1212    No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
       
  1213  */
       
  1214 
       
  1215 #define eeprom_delay()	RTL_R32(Cfg9346)
       
  1216 
       
  1217 /* The EEPROM commands include the alway-set leading bit. */
       
  1218 #define EE_WRITE_CMD	(5)
       
  1219 #define EE_READ_CMD		(6)
       
  1220 #define EE_ERASE_CMD	(7)
       
  1221 
       
  1222 static int __devinit read_eeprom (void __iomem *ioaddr, int location, int addr_len)
       
  1223 {
       
  1224 	int i;
       
  1225 	unsigned retval = 0;
       
  1226 	int read_cmd = location | (EE_READ_CMD << addr_len);
       
  1227 
       
  1228 	RTL_W8 (Cfg9346, EE_ENB & ~EE_CS);
       
  1229 	RTL_W8 (Cfg9346, EE_ENB);
       
  1230 	eeprom_delay ();
       
  1231 
       
  1232 	/* Shift the read command bits out. */
       
  1233 	for (i = 4 + addr_len; i >= 0; i--) {
       
  1234 		int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
       
  1235 		RTL_W8 (Cfg9346, EE_ENB | dataval);
       
  1236 		eeprom_delay ();
       
  1237 		RTL_W8 (Cfg9346, EE_ENB | dataval | EE_SHIFT_CLK);
       
  1238 		eeprom_delay ();
       
  1239 	}
       
  1240 	RTL_W8 (Cfg9346, EE_ENB);
       
  1241 	eeprom_delay ();
       
  1242 
       
  1243 	for (i = 16; i > 0; i--) {
       
  1244 		RTL_W8 (Cfg9346, EE_ENB | EE_SHIFT_CLK);
       
  1245 		eeprom_delay ();
       
  1246 		retval =
       
  1247 		    (retval << 1) | ((RTL_R8 (Cfg9346) & EE_DATA_READ) ? 1 :
       
  1248 				     0);
       
  1249 		RTL_W8 (Cfg9346, EE_ENB);
       
  1250 		eeprom_delay ();
       
  1251 	}
       
  1252 
       
  1253 	/* Terminate the EEPROM access. */
       
  1254 	RTL_W8 (Cfg9346, ~EE_CS);
       
  1255 	eeprom_delay ();
       
  1256 
       
  1257 	return retval;
       
  1258 }
       
  1259 
       
  1260 /* MII serial management: mostly bogus for now. */
       
  1261 /* Read and write the MII management registers using software-generated
       
  1262    serial MDIO protocol.
       
  1263    The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
       
  1264    met by back-to-back PCI I/O cycles, but we insert a delay to avoid
       
  1265    "overclocking" issues. */
       
  1266 #define MDIO_DIR		0x80
       
  1267 #define MDIO_DATA_OUT	0x04
       
  1268 #define MDIO_DATA_IN	0x02
       
  1269 #define MDIO_CLK		0x01
       
  1270 #define MDIO_WRITE0 (MDIO_DIR)
       
  1271 #define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
       
  1272 
       
  1273 #define mdio_delay()	RTL_R8(Config4)
       
  1274 
       
  1275 
       
  1276 static char mii_2_8139_map[8] = {
       
  1277 	BasicModeCtrl,
       
  1278 	BasicModeStatus,
       
  1279 	0,
       
  1280 	0,
       
  1281 	NWayAdvert,
       
  1282 	NWayLPAR,
       
  1283 	NWayExpansion,
       
  1284 	0
       
  1285 };
       
  1286 
       
  1287 
       
  1288 #ifdef CONFIG_8139TOO_8129
       
  1289 /* Syncronize the MII management interface by shifting 32 one bits out. */
       
  1290 static void mdio_sync (void __iomem *ioaddr)
       
  1291 {
       
  1292 	int i;
       
  1293 
       
  1294 	for (i = 32; i >= 0; i--) {
       
  1295 		RTL_W8 (Config4, MDIO_WRITE1);
       
  1296 		mdio_delay ();
       
  1297 		RTL_W8 (Config4, MDIO_WRITE1 | MDIO_CLK);
       
  1298 		mdio_delay ();
       
  1299 	}
       
  1300 }
       
  1301 #endif
       
  1302 
       
  1303 static int mdio_read (struct net_device *dev, int phy_id, int location)
       
  1304 {
       
  1305 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1306 	int retval = 0;
       
  1307 #ifdef CONFIG_8139TOO_8129
       
  1308 	void __iomem *ioaddr = tp->mmio_addr;
       
  1309 	int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
       
  1310 	int i;
       
  1311 #endif
       
  1312 
       
  1313 	if (phy_id > 31) {	/* Really a 8139.  Use internal registers. */
       
  1314 		void __iomem *ioaddr = tp->mmio_addr;
       
  1315 		return location < 8 && mii_2_8139_map[location] ?
       
  1316 		    RTL_R16 (mii_2_8139_map[location]) : 0;
       
  1317 	}
       
  1318 
       
  1319 #ifdef CONFIG_8139TOO_8129
       
  1320 	mdio_sync (ioaddr);
       
  1321 	/* Shift the read command bits out. */
       
  1322 	for (i = 15; i >= 0; i--) {
       
  1323 		int dataval = (mii_cmd & (1 << i)) ? MDIO_DATA_OUT : 0;
       
  1324 
       
  1325 		RTL_W8 (Config4, MDIO_DIR | dataval);
       
  1326 		mdio_delay ();
       
  1327 		RTL_W8 (Config4, MDIO_DIR | dataval | MDIO_CLK);
       
  1328 		mdio_delay ();
       
  1329 	}
       
  1330 
       
  1331 	/* Read the two transition, 16 data, and wire-idle bits. */
       
  1332 	for (i = 19; i > 0; i--) {
       
  1333 		RTL_W8 (Config4, 0);
       
  1334 		mdio_delay ();
       
  1335 		retval = (retval << 1) | ((RTL_R8 (Config4) & MDIO_DATA_IN) ? 1 : 0);
       
  1336 		RTL_W8 (Config4, MDIO_CLK);
       
  1337 		mdio_delay ();
       
  1338 	}
       
  1339 #endif
       
  1340 
       
  1341 	return (retval >> 1) & 0xffff;
       
  1342 }
       
  1343 
       
  1344 
       
  1345 static void mdio_write (struct net_device *dev, int phy_id, int location,
       
  1346 			int value)
       
  1347 {
       
  1348 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1349 #ifdef CONFIG_8139TOO_8129
       
  1350 	void __iomem *ioaddr = tp->mmio_addr;
       
  1351 	int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
       
  1352 	int i;
       
  1353 #endif
       
  1354 
       
  1355 	if (phy_id > 31) {	/* Really a 8139.  Use internal registers. */
       
  1356 		void __iomem *ioaddr = tp->mmio_addr;
       
  1357 		if (location == 0) {
       
  1358 			RTL_W8 (Cfg9346, Cfg9346_Unlock);
       
  1359 			RTL_W16 (BasicModeCtrl, value);
       
  1360 			RTL_W8 (Cfg9346, Cfg9346_Lock);
       
  1361 		} else if (location < 8 && mii_2_8139_map[location])
       
  1362 			RTL_W16 (mii_2_8139_map[location], value);
       
  1363 		return;
       
  1364 	}
       
  1365 
       
  1366 #ifdef CONFIG_8139TOO_8129
       
  1367 	mdio_sync (ioaddr);
       
  1368 
       
  1369 	/* Shift the command bits out. */
       
  1370 	for (i = 31; i >= 0; i--) {
       
  1371 		int dataval =
       
  1372 		    (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
       
  1373 		RTL_W8 (Config4, dataval);
       
  1374 		mdio_delay ();
       
  1375 		RTL_W8 (Config4, dataval | MDIO_CLK);
       
  1376 		mdio_delay ();
       
  1377 	}
       
  1378 	/* Clear out extra bits. */
       
  1379 	for (i = 2; i > 0; i--) {
       
  1380 		RTL_W8 (Config4, 0);
       
  1381 		mdio_delay ();
       
  1382 		RTL_W8 (Config4, MDIO_CLK);
       
  1383 		mdio_delay ();
       
  1384 	}
       
  1385 #endif
       
  1386 }
       
  1387 
       
  1388 
       
  1389 static int rtl8139_open (struct net_device *dev)
       
  1390 {
       
  1391 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1392 	int retval;
       
  1393 	void __iomem *ioaddr = tp->mmio_addr;
       
  1394 
       
  1395 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1396 
       
  1397 #ifdef ECAT_DEBUG
       
  1398         printk(KERN_DEBUG "%s: open\n", dev->name);
       
  1399 #endif
       
  1400 
       
  1401         if (dev != rtl_ecat_dev.dev)
       
  1402         {
       
  1403           retval = request_irq(dev->irq, rtl8139_interrupt, SA_SHIRQ, dev->name, dev);
       
  1404           if (retval)
       
  1405             return retval;
       
  1406         }
       
  1407 
       
  1408 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1409 
       
  1410 	tp->tx_bufs = pci_alloc_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
       
  1411 					   &tp->tx_bufs_dma);
       
  1412 	tp->rx_ring = pci_alloc_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
       
  1413 					   &tp->rx_ring_dma);
       
  1414 	if (tp->tx_bufs == NULL || tp->rx_ring == NULL)
       
  1415         {
       
  1416           /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1417 
       
  1418           if (dev != rtl_ecat_dev.dev)
       
  1419           {
       
  1420             free_irq(dev->irq, dev);
       
  1421           }
       
  1422 
       
  1423           /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1424 
       
  1425           if (tp->tx_bufs)
       
  1426             pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
       
  1427                                 tp->tx_bufs, tp->tx_bufs_dma);
       
  1428           if (tp->rx_ring)
       
  1429             pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
       
  1430                                 tp->rx_ring, tp->rx_ring_dma);
       
  1431 
       
  1432           return -ENOMEM;
       
  1433 
       
  1434 	}
       
  1435 
       
  1436 	tp->mii.full_duplex = tp->mii.force_media;
       
  1437 	tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
       
  1438 
       
  1439 	rtl8139_init_ring (dev);
       
  1440 	rtl8139_hw_start (dev);
       
  1441 
       
  1442         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1443 
       
  1444         if (dev != rtl_ecat_dev.dev)
       
  1445         {
       
  1446           netif_start_queue (dev);
       
  1447         }
       
  1448 
       
  1449         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1450 
       
  1451 	if (netif_msg_ifup(tp))
       
  1452 		printk(KERN_DEBUG "%s: rtl8139_open() ioaddr %#lx IRQ %d"
       
  1453 			" GP Pins %2.2x %s-duplex.\n",
       
  1454 			dev->name, pci_resource_start (tp->pci_dev, 1),
       
  1455 			dev->irq, RTL_R8 (MediaStatus),
       
  1456 			tp->mii.full_duplex ? "full" : "half");
       
  1457 
       
  1458 
       
  1459 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1460 
       
  1461         if (dev != rtl_ecat_dev.dev)
       
  1462         {
       
  1463           rtl8139_start_thread(dev);
       
  1464 	}
       
  1465 
       
  1466 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1467 
       
  1468 	return 0;
       
  1469 }
       
  1470 
       
  1471 
       
  1472 static void rtl_check_media (struct net_device *dev, unsigned int init_media)
       
  1473 {
       
  1474 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1475 
       
  1476 	if (tp->phys[0] >= 0) {
       
  1477 		mii_check_media(&tp->mii, netif_msg_link(tp), init_media);
       
  1478 	}
       
  1479 }
       
  1480 
       
  1481 /* Start the hardware at open or resume. */
       
  1482 static void rtl8139_hw_start (struct net_device *dev)
       
  1483 {
       
  1484 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1485 	void __iomem *ioaddr = tp->mmio_addr;
       
  1486 	u32 i;
       
  1487 	u8 tmp;
       
  1488 
       
  1489 	/* Bring old chips out of low-power mode. */
       
  1490 	if (rtl_chip_info[tp->chipset].flags & HasHltClk)
       
  1491 		RTL_W8 (HltClk, 'R');
       
  1492 
       
  1493 	rtl8139_chip_reset (ioaddr);
       
  1494 
       
  1495 	/* unlock Config[01234] and BMCR register writes */
       
  1496 	RTL_W8_F (Cfg9346, Cfg9346_Unlock);
       
  1497 	/* Restore our idea of the MAC address. */
       
  1498 	RTL_W32_F (MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
       
  1499 	RTL_W32_F (MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
       
  1500 
       
  1501 	/* Must enable Tx/Rx before setting transfer thresholds! */
       
  1502 	RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
       
  1503 
       
  1504 	tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
       
  1505 	RTL_W32 (RxConfig, tp->rx_config);
       
  1506 	RTL_W32 (TxConfig, rtl8139_tx_config);
       
  1507 
       
  1508 	tp->cur_rx = 0;
       
  1509 
       
  1510 	rtl_check_media (dev, 1);
       
  1511 
       
  1512 	if (tp->chipset >= CH_8139B) {
       
  1513 		/* Disable magic packet scanning, which is enabled
       
  1514 		 * when PM is enabled in Config1.  It can be reenabled
       
  1515 		 * via ETHTOOL_SWOL if desired.  */
       
  1516 		RTL_W8 (Config3, RTL_R8 (Config3) & ~Cfg3_Magic);
       
  1517 	}
       
  1518 
       
  1519 	DPRINTK("init buffer addresses\n");
       
  1520 
       
  1521 	/* Lock Config[01234] and BMCR register writes */
       
  1522 	RTL_W8 (Cfg9346, Cfg9346_Lock);
       
  1523 
       
  1524 	/* init Rx ring buffer DMA address */
       
  1525 	RTL_W32_F (RxBuf, tp->rx_ring_dma);
       
  1526 
       
  1527 	/* init Tx buffer DMA addresses */
       
  1528 	for (i = 0; i < NUM_TX_DESC; i++)
       
  1529 		RTL_W32_F (TxAddr0 + (i * 4), tp->tx_bufs_dma + (tp->tx_buf[i] - tp->tx_bufs));
       
  1530 
       
  1531 	RTL_W32 (RxMissed, 0);
       
  1532 
       
  1533 	rtl8139_set_rx_mode (dev);
       
  1534 
       
  1535 	/* no early-rx interrupts */
       
  1536 	RTL_W16 (MultiIntr, RTL_R16 (MultiIntr) & MultiIntrClear);
       
  1537 
       
  1538 	/* make sure RxTx has started */
       
  1539 	tmp = RTL_R8 (ChipCmd);
       
  1540 	if ((!(tmp & CmdRxEnb)) || (!(tmp & CmdTxEnb)))
       
  1541 		RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
       
  1542 
       
  1543 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1544 
       
  1545         if (dev != rtl_ecat_dev.dev)
       
  1546         {
       
  1547           /* Enable all known interrupts by setting the interrupt mask. */
       
  1548           RTL_W16 (IntrMask, rtl8139_intr_mask);
       
  1549 	}
       
  1550 
       
  1551 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1552 }
       
  1553 
       
  1554 
       
  1555 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
       
  1556 static void rtl8139_init_ring (struct net_device *dev)
       
  1557 {
       
  1558 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1559 	int i;
       
  1560 
       
  1561 	tp->cur_rx = 0;
       
  1562 	tp->cur_tx = 0;
       
  1563 	tp->dirty_tx = 0;
       
  1564 
       
  1565 	for (i = 0; i < NUM_TX_DESC; i++)
       
  1566 		tp->tx_buf[i] = &tp->tx_bufs[i * TX_BUF_SIZE];
       
  1567 }
       
  1568 
       
  1569 
       
  1570 /* This must be global for CONFIG_8139TOO_TUNE_TWISTER case */
       
  1571 static int next_tick = 3 * HZ;
       
  1572 
       
  1573 #ifndef CONFIG_8139TOO_TUNE_TWISTER
       
  1574 static inline void rtl8139_tune_twister (struct net_device *dev,
       
  1575 				  struct rtl8139_private *tp) {}
       
  1576 #else
       
  1577 enum TwisterParamVals {
       
  1578 	PARA78_default	= 0x78fa8388,
       
  1579 	PARA7c_default	= 0xcb38de43,	/* param[0][3] */
       
  1580 	PARA7c_xxx	= 0xcb38de43,
       
  1581 };
       
  1582 
       
  1583 static const unsigned long param[4][4] = {
       
  1584 	{0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
       
  1585 	{0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
       
  1586 	{0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
       
  1587 	{0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
       
  1588 };
       
  1589 
       
  1590 static void rtl8139_tune_twister (struct net_device *dev,
       
  1591 				  struct rtl8139_private *tp)
       
  1592 {
       
  1593 	int linkcase;
       
  1594 	void __iomem *ioaddr = tp->mmio_addr;
       
  1595 
       
  1596 	/* This is a complicated state machine to configure the "twister" for
       
  1597 	   impedance/echos based on the cable length.
       
  1598 	   All of this is magic and undocumented.
       
  1599 	 */
       
  1600 	switch (tp->twistie) {
       
  1601 	case 1:
       
  1602 		if (RTL_R16 (CSCR) & CSCR_LinkOKBit) {
       
  1603 			/* We have link beat, let us tune the twister. */
       
  1604 			RTL_W16 (CSCR, CSCR_LinkDownOffCmd);
       
  1605 			tp->twistie = 2;	/* Change to state 2. */
       
  1606 			next_tick = HZ / 10;
       
  1607 		} else {
       
  1608 			/* Just put in some reasonable defaults for when beat returns. */
       
  1609 			RTL_W16 (CSCR, CSCR_LinkDownCmd);
       
  1610 			RTL_W32 (FIFOTMS, 0x20);	/* Turn on cable test mode. */
       
  1611 			RTL_W32 (PARA78, PARA78_default);
       
  1612 			RTL_W32 (PARA7c, PARA7c_default);
       
  1613 			tp->twistie = 0;	/* Bail from future actions. */
       
  1614 		}
       
  1615 		break;
       
  1616 	case 2:
       
  1617 		/* Read how long it took to hear the echo. */
       
  1618 		linkcase = RTL_R16 (CSCR) & CSCR_LinkStatusBits;
       
  1619 		if (linkcase == 0x7000)
       
  1620 			tp->twist_row = 3;
       
  1621 		else if (linkcase == 0x3000)
       
  1622 			tp->twist_row = 2;
       
  1623 		else if (linkcase == 0x1000)
       
  1624 			tp->twist_row = 1;
       
  1625 		else
       
  1626 			tp->twist_row = 0;
       
  1627 		tp->twist_col = 0;
       
  1628 		tp->twistie = 3;	/* Change to state 2. */
       
  1629 		next_tick = HZ / 10;
       
  1630 		break;
       
  1631 	case 3:
       
  1632 		/* Put out four tuning parameters, one per 100msec. */
       
  1633 		if (tp->twist_col == 0)
       
  1634 			RTL_W16 (FIFOTMS, 0);
       
  1635 		RTL_W32 (PARA7c, param[(int) tp->twist_row]
       
  1636 			 [(int) tp->twist_col]);
       
  1637 		next_tick = HZ / 10;
       
  1638 		if (++tp->twist_col >= 4) {
       
  1639 			/* For short cables we are done.
       
  1640 			   For long cables (row == 3) check for mistune. */
       
  1641 			tp->twistie =
       
  1642 			    (tp->twist_row == 3) ? 4 : 0;
       
  1643 		}
       
  1644 		break;
       
  1645 	case 4:
       
  1646 		/* Special case for long cables: check for mistune. */
       
  1647 		if ((RTL_R16 (CSCR) &
       
  1648 		     CSCR_LinkStatusBits) == 0x7000) {
       
  1649 			tp->twistie = 0;
       
  1650 			break;
       
  1651 		} else {
       
  1652 			RTL_W32 (PARA7c, 0xfb38de03);
       
  1653 			tp->twistie = 5;
       
  1654 			next_tick = HZ / 10;
       
  1655 		}
       
  1656 		break;
       
  1657 	case 5:
       
  1658 		/* Retune for shorter cable (column 2). */
       
  1659 		RTL_W32 (FIFOTMS, 0x20);
       
  1660 		RTL_W32 (PARA78, PARA78_default);
       
  1661 		RTL_W32 (PARA7c, PARA7c_default);
       
  1662 		RTL_W32 (FIFOTMS, 0x00);
       
  1663 		tp->twist_row = 2;
       
  1664 		tp->twist_col = 0;
       
  1665 		tp->twistie = 3;
       
  1666 		next_tick = HZ / 10;
       
  1667 		break;
       
  1668 
       
  1669 	default:
       
  1670 		/* do nothing */
       
  1671 		break;
       
  1672 	}
       
  1673 }
       
  1674 #endif /* CONFIG_8139TOO_TUNE_TWISTER */
       
  1675 
       
  1676 static inline void rtl8139_thread_iter (struct net_device *dev,
       
  1677 				 struct rtl8139_private *tp,
       
  1678 				 void __iomem *ioaddr)
       
  1679 {
       
  1680 	int mii_lpa;
       
  1681 
       
  1682 	mii_lpa = mdio_read (dev, tp->phys[0], MII_LPA);
       
  1683 
       
  1684 	if (!tp->mii.force_media && mii_lpa != 0xffff) {
       
  1685 		int duplex = (mii_lpa & LPA_100FULL)
       
  1686 		    || (mii_lpa & 0x01C0) == 0x0040;
       
  1687 		if (tp->mii.full_duplex != duplex) {
       
  1688 			tp->mii.full_duplex = duplex;
       
  1689 
       
  1690 			if (mii_lpa) {
       
  1691 				printk (KERN_INFO
       
  1692 					"%s: Setting %s-duplex based on MII #%d link"
       
  1693 					" partner ability of %4.4x.\n",
       
  1694 					dev->name,
       
  1695 					tp->mii.full_duplex ? "full" : "half",
       
  1696 					tp->phys[0], mii_lpa);
       
  1697 			} else {
       
  1698 				printk(KERN_INFO"%s: media is unconnected, link down, or incompatible connection\n",
       
  1699 				       dev->name);
       
  1700 			}
       
  1701 #if 0
       
  1702 			RTL_W8 (Cfg9346, Cfg9346_Unlock);
       
  1703 			RTL_W8 (Config1, tp->mii.full_duplex ? 0x60 : 0x20);
       
  1704 			RTL_W8 (Cfg9346, Cfg9346_Lock);
       
  1705 #endif
       
  1706 		}
       
  1707 	}
       
  1708 
       
  1709 	next_tick = HZ * 60;
       
  1710 
       
  1711 	rtl8139_tune_twister (dev, tp);
       
  1712 
       
  1713 	DPRINTK ("%s: Media selection tick, Link partner %4.4x.\n",
       
  1714 		 dev->name, RTL_R16 (NWayLPAR));
       
  1715 	DPRINTK ("%s:  Other registers are IntMask %4.4x IntStatus %4.4x\n",
       
  1716 		 dev->name, RTL_R16 (IntrMask), RTL_R16 (IntrStatus));
       
  1717 	DPRINTK ("%s:  Chip config %2.2x %2.2x.\n",
       
  1718 		 dev->name, RTL_R8 (Config0),
       
  1719 		 RTL_R8 (Config1));
       
  1720 }
       
  1721 
       
  1722 static int rtl8139_thread (void *data)
       
  1723 {
       
  1724 	struct net_device *dev = data;
       
  1725 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1726 	unsigned long timeout;
       
  1727 
       
  1728 	daemonize("%s", dev->name);
       
  1729 	allow_signal(SIGTERM);
       
  1730 
       
  1731 	while (1) {
       
  1732 		timeout = next_tick;
       
  1733 		do {
       
  1734 			timeout = interruptible_sleep_on_timeout (&tp->thr_wait, timeout);
       
  1735 			/* make swsusp happy with our thread */
       
  1736 			try_to_freeze();
       
  1737 		} while (!signal_pending (current) && (timeout > 0));
       
  1738 
       
  1739 		if (signal_pending (current)) {
       
  1740 			flush_signals(current);
       
  1741 		}
       
  1742 
       
  1743 		if (tp->time_to_die)
       
  1744 			break;
       
  1745 
       
  1746 		if (rtnl_lock_interruptible ())
       
  1747 			break;
       
  1748 		rtl8139_thread_iter (dev, tp, tp->mmio_addr);
       
  1749 		rtnl_unlock ();
       
  1750 	}
       
  1751 
       
  1752 	complete_and_exit (&tp->thr_exited, 0);
       
  1753 }
       
  1754 
       
  1755 static void rtl8139_start_thread(struct net_device *dev)
       
  1756 {
       
  1757 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1758 
       
  1759 	tp->thr_pid = -1;
       
  1760 	tp->twistie = 0;
       
  1761 	tp->time_to_die = 0;
       
  1762 	if (tp->chipset == CH_8139_K)
       
  1763 		tp->twistie = 1;
       
  1764 	else if (tp->drv_flags & HAS_LNK_CHNG)
       
  1765 		return;
       
  1766 
       
  1767 	tp->thr_pid = kernel_thread(rtl8139_thread, dev, CLONE_FS|CLONE_FILES);
       
  1768 	if (tp->thr_pid < 0) {
       
  1769 		printk (KERN_WARNING "%s: unable to start kernel thread\n",
       
  1770 			dev->name);
       
  1771 	}
       
  1772 }
       
  1773 
       
  1774 static inline void rtl8139_tx_clear (struct rtl8139_private *tp)
       
  1775 {
       
  1776 	tp->cur_tx = 0;
       
  1777 	tp->dirty_tx = 0;
       
  1778 
       
  1779 	/* XXX account for unsent Tx packets in tp->stats.tx_dropped */
       
  1780 }
       
  1781 
       
  1782 
       
  1783 static void rtl8139_tx_timeout (struct net_device *dev)
       
  1784 {
       
  1785 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1786 	void __iomem *ioaddr = tp->mmio_addr;
       
  1787 	int i;
       
  1788 	u8 tmp8;
       
  1789 	unsigned long flags;
       
  1790 
       
  1791 	printk (KERN_DEBUG "%s: Transmit timeout, status %2.2x %4.4x %4.4x "
       
  1792 		"media %2.2x.\n", dev->name, RTL_R8 (ChipCmd),
       
  1793 		RTL_R16(IntrStatus), RTL_R16(IntrMask), RTL_R8(MediaStatus));
       
  1794 	/* Emit info to figure out what went wrong. */
       
  1795 	printk (KERN_DEBUG "%s: Tx queue start entry %ld  dirty entry %ld.\n",
       
  1796 		dev->name, tp->cur_tx, tp->dirty_tx);
       
  1797 	for (i = 0; i < NUM_TX_DESC; i++)
       
  1798 		printk (KERN_DEBUG "%s:  Tx descriptor %d is %8.8lx.%s\n",
       
  1799 			dev->name, i, RTL_R32 (TxStatus0 + (i * 4)),
       
  1800 			i == tp->dirty_tx % NUM_TX_DESC ?
       
  1801 				" (queue head)" : "");
       
  1802 
       
  1803 	tp->xstats.tx_timeouts++;
       
  1804 
       
  1805         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1806 
       
  1807         printk(KERN_DEBUG "%s: tx_timeout\n", dev->name);
       
  1808 
       
  1809         if (dev == rtl_ecat_dev.dev)
       
  1810         {
       
  1811           if (rtl_ecat_dev.state != ECAT_DS_SENT)
       
  1812           {
       
  1813             printk(KERN_WARNING "EtherCAT: Wrong status at timeout: %i\n",
       
  1814                    rtl_ecat_dev.state);
       
  1815           }
       
  1816 
       
  1817           rtl_ecat_dev.state = ECAT_DS_TIMEOUT;
       
  1818         }
       
  1819 
       
  1820         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1821 
       
  1822 	/* disable Tx ASAP, if not already */
       
  1823 	tmp8 = RTL_R8 (ChipCmd);
       
  1824 	if (tmp8 & CmdTxEnb)
       
  1825 		RTL_W8 (ChipCmd, CmdRxEnb);
       
  1826 
       
  1827 	spin_lock(&tp->rx_lock);
       
  1828 	/* Disable interrupts by clearing the interrupt mask. */
       
  1829 	RTL_W16 (IntrMask, 0x0000);
       
  1830 
       
  1831 	/* Stop a shared interrupt from scavenging while we are. */
       
  1832         spin_lock_irqsave (&tp->lock, flags);
       
  1833         rtl8139_tx_clear (tp);
       
  1834         spin_unlock_irqrestore (&tp->lock, flags);
       
  1835 
       
  1836 	/* ...and finally, reset everything */
       
  1837 
       
  1838 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1839 
       
  1840 	if (dev != rtl_ecat_dev.dev)
       
  1841         {
       
  1842           if (netif_running(dev))
       
  1843           {
       
  1844             rtl8139_hw_start (dev);
       
  1845             netif_wake_queue (dev);
       
  1846           }
       
  1847 	}
       
  1848         else
       
  1849         {
       
  1850           rtl8139_hw_start (dev);
       
  1851         }
       
  1852 
       
  1853 	spin_unlock(&tp->rx_lock);
       
  1854 
       
  1855 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1856 }
       
  1857 
       
  1858 static int rtl8139_start_xmit (struct sk_buff *skb, struct net_device *dev)
       
  1859 {
       
  1860 	struct rtl8139_private *tp = netdev_priv(dev);
       
  1861 	void __iomem *ioaddr = tp->mmio_addr;
       
  1862 	unsigned int entry;
       
  1863 	unsigned int len = skb->len;
       
  1864 
       
  1865 	/* Calculate the next Tx descriptor entry. */
       
  1866 	entry = tp->cur_tx % NUM_TX_DESC;
       
  1867 
       
  1868         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1869 
       
  1870         /* Note: the chip doesn't have auto-pad! */
       
  1871 	if (likely(len < TX_BUF_SIZE))
       
  1872         {
       
  1873           if (len < ETH_ZLEN)
       
  1874             memset(tp->tx_buf[entry], 0, ETH_ZLEN);
       
  1875 
       
  1876           skb_copy_and_csum_dev(skb, tp->tx_buf[entry]);
       
  1877           if (dev != rtl_ecat_dev.dev) dev_kfree_skb(skb);
       
  1878 	}
       
  1879         else
       
  1880         {
       
  1881           if (dev != rtl_ecat_dev.dev) dev_kfree_skb(skb);
       
  1882           tp->stats.tx_dropped++;
       
  1883           return 0;
       
  1884 	}
       
  1885 
       
  1886 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1887 
       
  1888         spin_lock_irq(&tp->lock);
       
  1889 	RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
       
  1890 		   tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
       
  1891 
       
  1892 	dev->trans_start = jiffies;
       
  1893 
       
  1894 	tp->cur_tx++;
       
  1895 	wmb();
       
  1896 
       
  1897 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1898 
       
  1899 	if (dev != rtl_ecat_dev.dev)
       
  1900         {
       
  1901           if ((tp->cur_tx - NUM_TX_DESC) == tp->dirty_tx)
       
  1902             netif_stop_queue (dev);
       
  1903           spin_unlock_irq(&tp->lock);
       
  1904 
       
  1905           if (netif_msg_tx_queued(tp))
       
  1906             printk (KERN_DEBUG "%s: Queued Tx packet size %u to slot %d.\n",
       
  1907                     dev->name, len, entry);
       
  1908         }
       
  1909 
       
  1910 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1911 
       
  1912 	return 0;
       
  1913 }
       
  1914 
       
  1915 
       
  1916 static void rtl8139_tx_interrupt (struct net_device *dev,
       
  1917 				  struct rtl8139_private *tp,
       
  1918 				  void __iomem *ioaddr)
       
  1919 {
       
  1920 	unsigned long dirty_tx, tx_left;
       
  1921 
       
  1922 	assert (dev != NULL);
       
  1923 	assert (ioaddr != NULL);
       
  1924 
       
  1925 	dirty_tx = tp->dirty_tx;
       
  1926 	tx_left = tp->cur_tx - dirty_tx;
       
  1927 
       
  1928 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1929 
       
  1930 	if (dev == rtl_ecat_dev.dev)
       
  1931         {
       
  1932           rtl_ecat_dev.tx_intr_cnt++;
       
  1933           rdtscl(rtl_ecat_dev.tx_time); // Get CPU cycles
       
  1934 	}
       
  1935 
       
  1936         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1937 
       
  1938 	while (tx_left > 0) {
       
  1939 		int entry = dirty_tx % NUM_TX_DESC;
       
  1940 		int txstatus;
       
  1941 
       
  1942 		txstatus = RTL_R32 (TxStatus0 + (entry * sizeof (u32)));
       
  1943 
       
  1944 		if (!(txstatus & (TxStatOK | TxUnderrun | TxAborted)))
       
  1945 			break;	/* It still hasn't been Txed */
       
  1946 
       
  1947 		/* Note: TxCarrierLost is always asserted at 100mbps. */
       
  1948 		if (txstatus & (TxOutOfWindow | TxAborted)) {
       
  1949 			/* There was an major error, log it. */
       
  1950 			if (netif_msg_tx_err(tp))
       
  1951 				printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
       
  1952 					dev->name, txstatus);
       
  1953 			tp->stats.tx_errors++;
       
  1954 			if (txstatus & TxAborted) {
       
  1955 				tp->stats.tx_aborted_errors++;
       
  1956 				RTL_W32 (TxConfig, TxClearAbt);
       
  1957 				RTL_W16 (IntrStatus, TxErr);
       
  1958 				wmb();
       
  1959 			}
       
  1960 			if (txstatus & TxCarrierLost)
       
  1961 				tp->stats.tx_carrier_errors++;
       
  1962 			if (txstatus & TxOutOfWindow)
       
  1963 				tp->stats.tx_window_errors++;
       
  1964 
       
  1965                         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1966 
       
  1967                         if (dev == rtl_ecat_dev.dev)
       
  1968                         {
       
  1969                           rtl_ecat_dev.state = ECAT_DS_ERROR;
       
  1970                         }
       
  1971 
       
  1972                         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1973 
       
  1974 		} else {
       
  1975 			if (txstatus & TxUnderrun) {
       
  1976 				/* Add 64 to the Tx FIFO threshold. */
       
  1977 				if (tp->tx_flag < 0x00300000)
       
  1978 					tp->tx_flag += 0x00020000;
       
  1979 				tp->stats.tx_fifo_errors++;
       
  1980 			}
       
  1981 			tp->stats.collisions += (txstatus >> 24) & 15;
       
  1982 			tp->stats.tx_bytes += txstatus & 0x7ff;
       
  1983 			tp->stats.tx_packets++;
       
  1984 		}
       
  1985 
       
  1986 		dirty_tx++;
       
  1987 		tx_left--;
       
  1988 	}
       
  1989 
       
  1990 #ifndef RTL8139_NDEBUG
       
  1991 	if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
       
  1992 		printk (KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
       
  1993 		        dev->name, dirty_tx, tp->cur_tx);
       
  1994 		dirty_tx += NUM_TX_DESC;
       
  1995 	}
       
  1996 #endif /* RTL8139_NDEBUG */
       
  1997 
       
  1998 	/* only wake the queue if we did work, and the queue is stopped */
       
  1999 	if (tp->dirty_tx != dirty_tx) {
       
  2000 		tp->dirty_tx = dirty_tx;
       
  2001 		mb();
       
  2002 
       
  2003                 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2004 
       
  2005 		if (dev != rtl_ecat_dev.dev)
       
  2006                 {
       
  2007                   netif_wake_queue (dev);
       
  2008                 }
       
  2009 
       
  2010                 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2011 	}
       
  2012 }
       
  2013 
       
  2014 
       
  2015 /* TODO: clean this up!  Rx reset need not be this intensive */
       
  2016 static void rtl8139_rx_err (u32 rx_status, struct net_device *dev,
       
  2017 			    struct rtl8139_private *tp, void __iomem *ioaddr)
       
  2018 {
       
  2019 	u8 tmp8;
       
  2020 #ifdef CONFIG_8139_OLD_RX_RESET
       
  2021 	int tmp_work;
       
  2022 #endif
       
  2023 
       
  2024 	if (netif_msg_rx_err (tp)) 
       
  2025 		printk(KERN_DEBUG "%s: Ethernet frame had errors, status %8.8x.\n",
       
  2026 			dev->name, rx_status);
       
  2027 	tp->stats.rx_errors++;
       
  2028 	if (!(rx_status & RxStatusOK)) {
       
  2029 		if (rx_status & RxTooLong) {
       
  2030 			DPRINTK ("%s: Oversized Ethernet frame, status %4.4x!\n",
       
  2031 			 	dev->name, rx_status);
       
  2032 			/* A.C.: The chip hangs here. */
       
  2033 		}
       
  2034 		if (rx_status & (RxBadSymbol | RxBadAlign))
       
  2035 			tp->stats.rx_frame_errors++;
       
  2036 		if (rx_status & (RxRunt | RxTooLong))
       
  2037 			tp->stats.rx_length_errors++;
       
  2038 		if (rx_status & RxCRCErr)
       
  2039 			tp->stats.rx_crc_errors++;
       
  2040 	} else {
       
  2041 		tp->xstats.rx_lost_in_ring++;
       
  2042 	}
       
  2043 
       
  2044         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2045 
       
  2046         if (dev == rtl_ecat_dev.dev)
       
  2047         {
       
  2048           rtl_ecat_dev.state = ECAT_DS_ERROR;
       
  2049         }
       
  2050 
       
  2051         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2052 
       
  2053 #ifndef CONFIG_8139_OLD_RX_RESET
       
  2054 	tmp8 = RTL_R8 (ChipCmd);
       
  2055 	RTL_W8 (ChipCmd, tmp8 & ~CmdRxEnb);
       
  2056 	RTL_W8 (ChipCmd, tmp8);
       
  2057 	RTL_W32 (RxConfig, tp->rx_config);
       
  2058 	tp->cur_rx = 0;
       
  2059 #else
       
  2060 	/* Reset the receiver, based on RealTek recommendation. (Bug?) */
       
  2061 
       
  2062 	/* disable receive */
       
  2063 	RTL_W8_F (ChipCmd, CmdTxEnb);
       
  2064 	tmp_work = 200;
       
  2065 	while (--tmp_work > 0) {
       
  2066 		udelay(1);
       
  2067 		tmp8 = RTL_R8 (ChipCmd);
       
  2068 		if (!(tmp8 & CmdRxEnb))
       
  2069 			break;
       
  2070 	}
       
  2071 	if (tmp_work <= 0)
       
  2072 		printk (KERN_WARNING PFX "rx stop wait too long\n");
       
  2073 	/* restart receive */
       
  2074 	tmp_work = 200;
       
  2075 	while (--tmp_work > 0) {
       
  2076 		RTL_W8_F (ChipCmd, CmdRxEnb | CmdTxEnb);
       
  2077 		udelay(1);
       
  2078 		tmp8 = RTL_R8 (ChipCmd);
       
  2079 		if ((tmp8 & CmdRxEnb) && (tmp8 & CmdTxEnb))
       
  2080 			break;
       
  2081 	}
       
  2082 	if (tmp_work <= 0)
       
  2083 		printk (KERN_WARNING PFX "tx/rx enable wait too long\n");
       
  2084 
       
  2085 	/* and reinitialize all rx related registers */
       
  2086 	RTL_W8_F (Cfg9346, Cfg9346_Unlock);
       
  2087 	/* Must enable Tx/Rx before setting transfer thresholds! */
       
  2088 	RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
       
  2089 
       
  2090 	tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
       
  2091 	RTL_W32 (RxConfig, tp->rx_config);
       
  2092 	tp->cur_rx = 0;
       
  2093 
       
  2094 	DPRINTK("init buffer addresses\n");
       
  2095 
       
  2096 	/* Lock Config[01234] and BMCR register writes */
       
  2097 	RTL_W8 (Cfg9346, Cfg9346_Lock);
       
  2098 
       
  2099 	/* init Rx ring buffer DMA address */
       
  2100 	RTL_W32_F (RxBuf, tp->rx_ring_dma);
       
  2101 
       
  2102 	/* A.C.: Reset the multicast list. */
       
  2103 	__set_rx_mode (dev);
       
  2104 #endif
       
  2105 }
       
  2106 
       
  2107 #if RX_BUF_IDX == 3
       
  2108 static __inline__ void wrap_copy(struct sk_buff *skb, const unsigned char *ring,
       
  2109 				 u32 offset, unsigned int size)
       
  2110 {
       
  2111 	u32 left = RX_BUF_LEN - offset;
       
  2112 
       
  2113 	if (size > left) {
       
  2114 		memcpy(skb->data, ring + offset, left);
       
  2115 		memcpy(skb->data+left, ring, size - left);
       
  2116 	} else
       
  2117 		memcpy(skb->data, ring + offset, size);
       
  2118 }
       
  2119 #endif
       
  2120 
       
  2121 static void rtl8139_isr_ack(struct rtl8139_private *tp)
       
  2122 {
       
  2123 	void __iomem *ioaddr = tp->mmio_addr;
       
  2124 	u16 status;
       
  2125 
       
  2126 	status = RTL_R16 (IntrStatus) & RxAckBits;
       
  2127 
       
  2128 	/* Clear out errors and receive interrupts */
       
  2129 	if (likely(status != 0)) {
       
  2130 		if (unlikely(status & (RxFIFOOver | RxOverflow))) {
       
  2131 			tp->stats.rx_errors++;
       
  2132 			if (status & RxFIFOOver)
       
  2133 				tp->stats.rx_fifo_errors++;
       
  2134 		}
       
  2135 		RTL_W16_F (IntrStatus, RxAckBits);
       
  2136 	}
       
  2137 }
       
  2138 
       
  2139 static int rtl8139_rx(struct net_device *dev, struct rtl8139_private *tp,
       
  2140 		      int budget)
       
  2141 {
       
  2142 	void __iomem *ioaddr = tp->mmio_addr;
       
  2143 	int received = 0;
       
  2144 	unsigned char *rx_ring = tp->rx_ring;
       
  2145 	unsigned int cur_rx = tp->cur_rx;
       
  2146 	unsigned int rx_size = 0;
       
  2147 
       
  2148 	DPRINTK ("%s: In rtl8139_rx(), current %4.4x BufAddr %4.4x,"
       
  2149 		 " free to %4.4x, Cmd %2.2x.\n", dev->name, (u16)cur_rx,
       
  2150 		 RTL_R16 (RxBufAddr),
       
  2151 		 RTL_R16 (RxBufPtr), RTL_R8 (ChipCmd));
       
  2152 
       
  2153     /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2154 
       
  2155 	if (dev == rtl_ecat_dev.dev)
       
  2156     {
       
  2157       rtl_ecat_dev.rx_intr_cnt++;
       
  2158       rdtscl(rtl_ecat_dev.rx_time); // Get CPU cycles
       
  2159 	}
       
  2160 
       
  2161 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2162 
       
  2163 	while (netif_running(dev) && received < budget
       
  2164 	       && (RTL_R8 (ChipCmd) & RxBufEmpty) == 0) {
       
  2165 		u32 ring_offset = cur_rx % RX_BUF_LEN;
       
  2166 		u32 rx_status;
       
  2167 		unsigned int pkt_size;
       
  2168 		struct sk_buff *skb;
       
  2169 
       
  2170 		rmb();
       
  2171 
       
  2172 		/* read size+status of next frame from DMA ring buffer */
       
  2173 		rx_status = le32_to_cpu (*(u32 *) (rx_ring + ring_offset));
       
  2174 		rx_size = rx_status >> 16;
       
  2175 		pkt_size = rx_size - 4;
       
  2176 
       
  2177         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2178 
       
  2179 		if (dev != rtl_ecat_dev.dev && netif_msg_rx_status(tp))
       
  2180           printk(KERN_DEBUG "%s:  rtl8139_rx() status %4.4x, size %4.4x,"
       
  2181                  " cur %4.4x.\n", dev->name, rx_status,
       
  2182                  rx_size, cur_rx);
       
  2183 
       
  2184         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2185 
       
  2186 #if RTL8139_DEBUG > 2
       
  2187 		{
       
  2188                   int i;
       
  2189                   DPRINTK ("%s: Frame contents ", dev->name);
       
  2190                   for (i = 0; i < 70; i++)
       
  2191                     printk (" %2.2x",
       
  2192                             rx_ring[ring_offset + i]);
       
  2193                   printk (".\n");
       
  2194 		}
       
  2195 #endif
       
  2196 
       
  2197 		/* Packet copy from FIFO still in progress.
       
  2198 		 * Theoretically, this should never happen
       
  2199 		 * since EarlyRx is disabled.
       
  2200 		 */
       
  2201 		if (unlikely(rx_size == 0xfff0)) {
       
  2202 			if (!tp->fifo_copy_timeout)
       
  2203 				tp->fifo_copy_timeout = jiffies + 2;
       
  2204 			else if (time_after(jiffies, tp->fifo_copy_timeout)) {
       
  2205 				DPRINTK ("%s: hung FIFO. Reset.", dev->name);
       
  2206 				rx_size = 0;
       
  2207 				goto no_early_rx;
       
  2208 			}
       
  2209 			if (netif_msg_intr(tp)) {
       
  2210 				printk(KERN_DEBUG "%s: fifo copy in progress.",
       
  2211 				       dev->name);
       
  2212 			}
       
  2213 			tp->xstats.early_rx++;
       
  2214 			break;
       
  2215 		}
       
  2216 
       
  2217 no_early_rx:
       
  2218 		tp->fifo_copy_timeout = 0;
       
  2219 
       
  2220 		/* If Rx err or invalid rx_size/rx_status received
       
  2221 		 * (which happens if we get lost in the ring),
       
  2222 		 * Rx process gets reset, so we abort any further
       
  2223 		 * Rx processing.
       
  2224 		 */
       
  2225 		if (unlikely((rx_size > (MAX_ETH_FRAME_SIZE+4)) ||
       
  2226 			     (rx_size < 8) ||
       
  2227 			     (!(rx_status & RxStatusOK)))) {
       
  2228 			rtl8139_rx_err (rx_status, dev, tp, ioaddr);
       
  2229 			received = -1;
       
  2230 			goto out;
       
  2231 		}
       
  2232 
       
  2233         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2234 
       
  2235         if (dev != rtl_ecat_dev.dev)
       
  2236         {
       
  2237           /* Malloc up new buffer, compatible with net-2e. */
       
  2238           /* Omit the four octet CRC from the length. */
       
  2239           skb = dev_alloc_skb(pkt_size + 2);
       
  2240         }
       
  2241 
       
  2242         if (dev != rtl_ecat_dev.dev)
       
  2243         {
       
  2244           if (likely(skb)) {
       
  2245 			skb->dev = dev;
       
  2246 			skb_reserve (skb, 2);	/* 16 byte align the IP fields. */
       
  2247 #if RX_BUF_IDX == 3
       
  2248 			wrap_copy(skb, rx_ring, ring_offset+4, pkt_size);
       
  2249 #else
       
  2250 			eth_copy_and_sum (skb, &rx_ring[ring_offset + 4], pkt_size, 0);
       
  2251 #endif
       
  2252 			skb_put (skb, pkt_size);
       
  2253 
       
  2254 			skb->protocol = eth_type_trans (skb, dev);
       
  2255 
       
  2256 			dev->last_rx = jiffies;
       
  2257 			tp->stats.rx_bytes += pkt_size;
       
  2258 			tp->stats.rx_packets++;
       
  2259 
       
  2260             netif_receive_skb (skb);
       
  2261 
       
  2262           } else {
       
  2263 			if (net_ratelimit())
       
  2264 				printk (KERN_WARNING
       
  2265 					"%s: Memory squeeze, dropping packet.\n",
       
  2266 					dev->name);
       
  2267 			tp->stats.rx_dropped++;
       
  2268           }
       
  2269         }
       
  2270         else
       
  2271         {
       
  2272           if (rtl_ecat_dev.state != ECAT_DS_SENT)
       
  2273           {
       
  2274             printk(KERN_WARNING "EtherCAT: Received frame while not in SENT state!\n");
       
  2275           }
       
  2276           else
       
  2277           {
       
  2278             // Copy received data to ethercat-device buffer, skip Ethernet-II header
       
  2279             memcpy(rtl_ecat_dev.rx_data, &rx_ring[ring_offset + 4] + ETH_HLEN,
       
  2280                    pkt_size - ETH_HLEN);
       
  2281             rtl_ecat_dev.rx_data_length = pkt_size - ETH_HLEN;
       
  2282 
       
  2283             rtl_ecat_dev.state = ECAT_DS_RECEIVED;
       
  2284 
       
  2285             dev->last_rx = jiffies;
       
  2286             tp->stats.rx_bytes += pkt_size;
       
  2287             tp->stats.rx_packets++;
       
  2288           }
       
  2289         }
       
  2290 
       
  2291         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2292 
       
  2293 		received++;
       
  2294 
       
  2295 		cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
       
  2296 		RTL_W16 (RxBufPtr, (u16) (cur_rx - 16));
       
  2297 
       
  2298 		rtl8139_isr_ack(tp);
       
  2299 	}
       
  2300 
       
  2301 	if (unlikely(!received || rx_size == 0xfff0))
       
  2302 		rtl8139_isr_ack(tp);
       
  2303 
       
  2304 #if RTL8139_DEBUG > 1
       
  2305 	DPRINTK ("%s: Done rtl8139_rx(), current %4.4x BufAddr %4.4x,"
       
  2306 		 " free to %4.4x, Cmd %2.2x.\n", dev->name, cur_rx,
       
  2307 		 RTL_R16 (RxBufAddr),
       
  2308 		 RTL_R16 (RxBufPtr), RTL_R8 (ChipCmd));
       
  2309 #endif
       
  2310 
       
  2311 	tp->cur_rx = cur_rx;
       
  2312 
       
  2313 	/*
       
  2314 	 * The receive buffer should be mostly empty.
       
  2315 	 * Tell NAPI to reenable the Rx irq.
       
  2316 	 */
       
  2317 	if (tp->fifo_copy_timeout)
       
  2318 		received = budget;
       
  2319 
       
  2320 out:
       
  2321 	return received;
       
  2322 }
       
  2323 
       
  2324 
       
  2325 static void rtl8139_weird_interrupt (struct net_device *dev,
       
  2326 				     struct rtl8139_private *tp,
       
  2327 				     void __iomem *ioaddr,
       
  2328 				     int status, int link_changed)
       
  2329 {
       
  2330 	DPRINTK ("%s: Abnormal interrupt, status %8.8x.\n",
       
  2331 		 dev->name, status);
       
  2332 
       
  2333 	assert (dev != NULL);
       
  2334 	assert (tp != NULL);
       
  2335 	assert (ioaddr != NULL);
       
  2336 
       
  2337 	/* Update the error count. */
       
  2338 	tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
       
  2339 	RTL_W32 (RxMissed, 0);
       
  2340 
       
  2341 	if ((status & RxUnderrun) && link_changed &&
       
  2342 	    (tp->drv_flags & HAS_LNK_CHNG)) {
       
  2343 		rtl_check_media(dev, 0);
       
  2344 		status &= ~RxUnderrun;
       
  2345 	}
       
  2346 
       
  2347 	if (status & (RxUnderrun | RxErr))
       
  2348 		tp->stats.rx_errors++;
       
  2349 
       
  2350 	if (status & PCSTimeout)
       
  2351 		tp->stats.rx_length_errors++;
       
  2352 	if (status & RxUnderrun)
       
  2353 		tp->stats.rx_fifo_errors++;
       
  2354 	if (status & PCIErr) {
       
  2355 		u16 pci_cmd_status;
       
  2356 		pci_read_config_word (tp->pci_dev, PCI_STATUS, &pci_cmd_status);
       
  2357 		pci_write_config_word (tp->pci_dev, PCI_STATUS, pci_cmd_status);
       
  2358 
       
  2359 		printk (KERN_ERR "%s: PCI Bus error %4.4x.\n",
       
  2360 			dev->name, pci_cmd_status);
       
  2361 	}
       
  2362 }
       
  2363 
       
  2364 int rtl8139_poll(struct net_device *dev, int *budget)
       
  2365 {
       
  2366 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2367 	void __iomem *ioaddr = tp->mmio_addr;
       
  2368 	int orig_budget = min(*budget, dev->quota);
       
  2369 	int done = 1;
       
  2370 
       
  2371 	spin_lock(&tp->rx_lock);
       
  2372 	if (likely(RTL_R16(IntrStatus) & RxAckBits)) {
       
  2373 		int work_done;
       
  2374 
       
  2375 		work_done = rtl8139_rx(dev, tp, orig_budget);
       
  2376 		if (likely(work_done > 0)) {
       
  2377 			*budget -= work_done;
       
  2378 			dev->quota -= work_done;
       
  2379 			done = (work_done < orig_budget);
       
  2380 		}
       
  2381 	}
       
  2382 
       
  2383 	if (done) {
       
  2384 		/*
       
  2385 		 * Order is important since data can get interrupted
       
  2386 		 * again when we think we are done.
       
  2387 		 */
       
  2388 		local_irq_disable();
       
  2389 		RTL_W16_F(IntrMask, rtl8139_intr_mask);
       
  2390 		__netif_rx_complete(dev);
       
  2391 		local_irq_enable();
       
  2392 	}
       
  2393 	spin_unlock(&tp->rx_lock);
       
  2394 
       
  2395 	return !done;
       
  2396 }
       
  2397 
       
  2398 /* The interrupt handler does all of the Rx thread work and cleans up
       
  2399    after the Tx thread. */
       
  2400 irqreturn_t rtl8139_interrupt (int irq, void *dev_instance,
       
  2401                                struct pt_regs *regs)
       
  2402 {
       
  2403 	struct net_device *dev = (struct net_device *) dev_instance;
       
  2404 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2405 	void __iomem *ioaddr = tp->mmio_addr;
       
  2406 	u16 status, ackstat;
       
  2407 	int link_changed = 0; /* avoid bogus "uninit" warning */
       
  2408 	int handled = 0;
       
  2409 
       
  2410         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2411 
       
  2412 	if (dev == rtl_ecat_dev.dev)
       
  2413         {
       
  2414           rtl_ecat_dev.intr_cnt++;
       
  2415 	}
       
  2416 	else
       
  2417         {
       
  2418           spin_lock(&tp->lock);
       
  2419         }
       
  2420 
       
  2421         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2422 
       
  2423 	status = RTL_R16 (IntrStatus);
       
  2424 
       
  2425         /* shared irq? */
       
  2426 	if (unlikely((status & rtl8139_intr_mask) == 0))
       
  2427 		goto out;
       
  2428 
       
  2429 	handled = 1;
       
  2430 
       
  2431 	/* h/w no longer present (hotplug?) or major error, bail */
       
  2432 	if (unlikely(status == 0xFFFF))
       
  2433 		goto out;
       
  2434 
       
  2435 	/* close possible race's with dev_close */
       
  2436 	if (unlikely(!netif_running(dev))) {
       
  2437 		RTL_W16 (IntrMask, 0);
       
  2438 		goto out;
       
  2439 	}
       
  2440 
       
  2441 	/* Acknowledge all of the current interrupt sources ASAP, but
       
  2442 	   an first get an additional status bit from CSCR. */
       
  2443 	if (unlikely(status & RxUnderrun))
       
  2444 		link_changed = RTL_R16 (CSCR) & CSCR_LinkChangeBit;
       
  2445 
       
  2446 	ackstat = status & ~(RxAckBits | TxErr);
       
  2447 	if (ackstat)
       
  2448 		RTL_W16 (IntrStatus, ackstat);
       
  2449 
       
  2450 	/* Receive packets are processed by poll routine.
       
  2451 	   If not running start it now. */
       
  2452 	if (status & RxAckBits){
       
  2453 		if (netif_rx_schedule_prep(dev)) {
       
  2454 			RTL_W16_F (IntrMask, rtl8139_norx_intr_mask);
       
  2455 			__netif_rx_schedule (dev);
       
  2456 		}
       
  2457 	}
       
  2458 
       
  2459 	/* Check uncommon events with one test. */
       
  2460 	if (unlikely(status & (PCIErr | PCSTimeout | RxUnderrun | RxErr)))
       
  2461 		rtl8139_weird_interrupt (dev, tp, ioaddr,
       
  2462 					 status, link_changed);
       
  2463 
       
  2464 	if (status & (TxOK | TxErr)) {
       
  2465 		rtl8139_tx_interrupt (dev, tp, ioaddr);
       
  2466 		if (status & TxErr)
       
  2467 			RTL_W16 (IntrStatus, TxErr);
       
  2468 	}
       
  2469  out:
       
  2470         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2471 
       
  2472 	if (dev != rtl_ecat_dev.dev)
       
  2473         {
       
  2474           spin_unlock (&tp->lock);
       
  2475         }
       
  2476 
       
  2477         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2478 
       
  2479 	DPRINTK ("%s: exiting interrupt, intr_status=%#4.4x.\n",
       
  2480 		 dev->name, RTL_R16 (IntrStatus));
       
  2481 	return IRQ_RETVAL(handled);
       
  2482 }
       
  2483 
       
  2484 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  2485 /*
       
  2486  * Polling receive - used by netconsole and other diagnostic tools
       
  2487  * to allow network i/o with interrupts disabled.
       
  2488  */
       
  2489 static void rtl8139_poll_controller(struct net_device *dev)
       
  2490 {
       
  2491 	disable_irq(dev->irq);
       
  2492 	rtl8139_interrupt(dev->irq, dev, NULL);
       
  2493 	enable_irq(dev->irq);
       
  2494 }
       
  2495 #endif
       
  2496 
       
  2497 static int rtl8139_close (struct net_device *dev)
       
  2498 {
       
  2499 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2500 	void __iomem *ioaddr = tp->mmio_addr;
       
  2501 	int ret = 0;
       
  2502 	unsigned long flags;
       
  2503 
       
  2504         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2505 
       
  2506         if (dev != rtl_ecat_dev.dev)
       
  2507         {
       
  2508 	    netif_stop_queue(dev);
       
  2509         }
       
  2510 
       
  2511         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2512 
       
  2513         if (tp->thr_pid >= 0) {
       
  2514 		tp->time_to_die = 1;
       
  2515 		wmb();
       
  2516 		ret = kill_proc (tp->thr_pid, SIGTERM, 1);
       
  2517 		if (ret) {
       
  2518 			printk (KERN_ERR "%s: unable to signal thread\n", dev->name);
       
  2519 			return ret;
       
  2520 		}
       
  2521 		wait_for_completion (&tp->thr_exited);
       
  2522         }
       
  2523 
       
  2524         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2525 
       
  2526         if (dev != rtl_ecat_dev.dev)
       
  2527         {
       
  2528           if (netif_msg_ifdown(tp))
       
  2529             printk(KERN_DEBUG "%s: Shutting down ethercard, status was 0x%4.4x.\n",
       
  2530                    dev->name, RTL_R16 (IntrStatus));
       
  2531         }
       
  2532 
       
  2533         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2534 
       
  2535         spin_lock_irqsave (&tp->lock, flags);
       
  2536 
       
  2537 	/* Stop the chip's Tx and Rx DMA processes. */
       
  2538 	RTL_W8 (ChipCmd, 0);
       
  2539 
       
  2540 	/* Disable interrupts by clearing the interrupt mask. */
       
  2541 	RTL_W16 (IntrMask, 0);
       
  2542 
       
  2543 	/* Update the error counts. */
       
  2544 	tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
       
  2545 	RTL_W32 (RxMissed, 0);
       
  2546 
       
  2547         spin_unlock_irqrestore (&tp->lock, flags);
       
  2548 
       
  2549         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2550 
       
  2551         if (dev != rtl_ecat_dev.dev)
       
  2552         {
       
  2553           synchronize_irq (dev->irq);	/* racy, but that's ok here */
       
  2554           free_irq (dev->irq, dev);
       
  2555         }
       
  2556 
       
  2557         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2558 
       
  2559 	rtl8139_tx_clear (tp);
       
  2560 
       
  2561 	pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
       
  2562 			    tp->rx_ring, tp->rx_ring_dma);
       
  2563 	pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
       
  2564 			    tp->tx_bufs, tp->tx_bufs_dma);
       
  2565 	tp->rx_ring = NULL;
       
  2566 	tp->tx_bufs = NULL;
       
  2567 
       
  2568 	/* Green! Put the chip in low-power mode. */
       
  2569 	RTL_W8 (Cfg9346, Cfg9346_Unlock);
       
  2570 
       
  2571 	if (rtl_chip_info[tp->chipset].flags & HasHltClk)
       
  2572 		RTL_W8 (HltClk, 'H');	/* 'R' would leave the clock running. */
       
  2573 
       
  2574 	return 0;
       
  2575 }
       
  2576 
       
  2577 
       
  2578 /* Get the ethtool Wake-on-LAN settings.  Assumes that wol points to
       
  2579    kernel memory, *wol has been initialized as {ETHTOOL_GWOL}, and
       
  2580    other threads or interrupts aren't messing with the 8139.  */
       
  2581 static void rtl8139_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
       
  2582 {
       
  2583 	struct rtl8139_private *np = netdev_priv(dev);
       
  2584 	void __iomem *ioaddr = np->mmio_addr;
       
  2585 
       
  2586 	spin_lock_irq(&np->lock);
       
  2587 	if (rtl_chip_info[np->chipset].flags & HasLWake) {
       
  2588 		u8 cfg3 = RTL_R8 (Config3);
       
  2589 		u8 cfg5 = RTL_R8 (Config5);
       
  2590 
       
  2591 		wol->supported = WAKE_PHY | WAKE_MAGIC
       
  2592 			| WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;
       
  2593 
       
  2594 		wol->wolopts = 0;
       
  2595 		if (cfg3 & Cfg3_LinkUp)
       
  2596 			wol->wolopts |= WAKE_PHY;
       
  2597 		if (cfg3 & Cfg3_Magic)
       
  2598 			wol->wolopts |= WAKE_MAGIC;
       
  2599 		/* (KON)FIXME: See how netdev_set_wol() handles the
       
  2600 		   following constants.  */
       
  2601 		if (cfg5 & Cfg5_UWF)
       
  2602 			wol->wolopts |= WAKE_UCAST;
       
  2603 		if (cfg5 & Cfg5_MWF)
       
  2604 			wol->wolopts |= WAKE_MCAST;
       
  2605 		if (cfg5 & Cfg5_BWF)
       
  2606 			wol->wolopts |= WAKE_BCAST;
       
  2607 	}
       
  2608 	spin_unlock_irq(&np->lock);
       
  2609 }
       
  2610 
       
  2611 
       
  2612 /* Set the ethtool Wake-on-LAN settings.  Return 0 or -errno.  Assumes
       
  2613    that wol points to kernel memory and other threads or interrupts
       
  2614    aren't messing with the 8139.  */
       
  2615 static int rtl8139_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
       
  2616 {
       
  2617 	struct rtl8139_private *np = netdev_priv(dev);
       
  2618 	void __iomem *ioaddr = np->mmio_addr;
       
  2619 	u32 support;
       
  2620 	u8 cfg3, cfg5;
       
  2621 
       
  2622 	support = ((rtl_chip_info[np->chipset].flags & HasLWake)
       
  2623 		   ? (WAKE_PHY | WAKE_MAGIC
       
  2624 		      | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST)
       
  2625 		   : 0);
       
  2626 	if (wol->wolopts & ~support)
       
  2627 		return -EINVAL;
       
  2628 
       
  2629 	spin_lock_irq(&np->lock);
       
  2630 	cfg3 = RTL_R8 (Config3) & ~(Cfg3_LinkUp | Cfg3_Magic);
       
  2631 	if (wol->wolopts & WAKE_PHY)
       
  2632 		cfg3 |= Cfg3_LinkUp;
       
  2633 	if (wol->wolopts & WAKE_MAGIC)
       
  2634 		cfg3 |= Cfg3_Magic;
       
  2635 	RTL_W8 (Cfg9346, Cfg9346_Unlock);
       
  2636 	RTL_W8 (Config3, cfg3);
       
  2637 	RTL_W8 (Cfg9346, Cfg9346_Lock);
       
  2638 
       
  2639 	cfg5 = RTL_R8 (Config5) & ~(Cfg5_UWF | Cfg5_MWF | Cfg5_BWF);
       
  2640 	/* (KON)FIXME: These are untested.  We may have to set the
       
  2641 	   CRC0, Wakeup0 and LSBCRC0 registers too, but I have no
       
  2642 	   documentation.  */
       
  2643 	if (wol->wolopts & WAKE_UCAST)
       
  2644 		cfg5 |= Cfg5_UWF;
       
  2645 	if (wol->wolopts & WAKE_MCAST)
       
  2646 		cfg5 |= Cfg5_MWF;
       
  2647 	if (wol->wolopts & WAKE_BCAST)
       
  2648 		cfg5 |= Cfg5_BWF;
       
  2649 	RTL_W8 (Config5, cfg5);	/* need not unlock via Cfg9346 */
       
  2650 	spin_unlock_irq(&np->lock);
       
  2651 
       
  2652 	return 0;
       
  2653 }
       
  2654 
       
  2655 static void rtl8139_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
       
  2656 {
       
  2657 	struct rtl8139_private *np = netdev_priv(dev);
       
  2658 	strcpy(info->driver, DRV_NAME);
       
  2659 	strcpy(info->version, DRV_VERSION);
       
  2660 	strcpy(info->bus_info, pci_name(np->pci_dev));
       
  2661 	info->regdump_len = np->regs_len;
       
  2662 }
       
  2663 
       
  2664 static int rtl8139_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
       
  2665 {
       
  2666 	struct rtl8139_private *np = netdev_priv(dev);
       
  2667 	spin_lock_irq(&np->lock);
       
  2668 	mii_ethtool_gset(&np->mii, cmd);
       
  2669 	spin_unlock_irq(&np->lock);
       
  2670 	return 0;
       
  2671 }
       
  2672 
       
  2673 static int rtl8139_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
       
  2674 {
       
  2675 	struct rtl8139_private *np = netdev_priv(dev);
       
  2676 	int rc;
       
  2677 	spin_lock_irq(&np->lock);
       
  2678 	rc = mii_ethtool_sset(&np->mii, cmd);
       
  2679 	spin_unlock_irq(&np->lock);
       
  2680 	return rc;
       
  2681 }
       
  2682 
       
  2683 static int rtl8139_nway_reset(struct net_device *dev)
       
  2684 {
       
  2685 	struct rtl8139_private *np = netdev_priv(dev);
       
  2686 	return mii_nway_restart(&np->mii);
       
  2687 }
       
  2688 
       
  2689 static u32 rtl8139_get_link(struct net_device *dev)
       
  2690 {
       
  2691 	struct rtl8139_private *np = netdev_priv(dev);
       
  2692 	return mii_link_ok(&np->mii);
       
  2693 }
       
  2694 
       
  2695 static u32 rtl8139_get_msglevel(struct net_device *dev)
       
  2696 {
       
  2697 	struct rtl8139_private *np = netdev_priv(dev);
       
  2698 	return np->msg_enable;
       
  2699 }
       
  2700 
       
  2701 static void rtl8139_set_msglevel(struct net_device *dev, u32 datum)
       
  2702 {
       
  2703 	struct rtl8139_private *np = netdev_priv(dev);
       
  2704 	np->msg_enable = datum;
       
  2705 }
       
  2706 
       
  2707 /* TODO: we are too slack to do reg dumping for pio, for now */
       
  2708 #ifdef CONFIG_8139TOO_PIO
       
  2709 #define rtl8139_get_regs_len	NULL
       
  2710 #define rtl8139_get_regs	NULL
       
  2711 #else
       
  2712 static int rtl8139_get_regs_len(struct net_device *dev)
       
  2713 {
       
  2714 	struct rtl8139_private *np = netdev_priv(dev);
       
  2715 	return np->regs_len;
       
  2716 }
       
  2717 
       
  2718 static void rtl8139_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
       
  2719 {
       
  2720 	struct rtl8139_private *np = netdev_priv(dev);
       
  2721 
       
  2722 	regs->version = RTL_REGS_VER;
       
  2723 
       
  2724 	spin_lock_irq(&np->lock);
       
  2725 	memcpy_fromio(regbuf, np->mmio_addr, regs->len);
       
  2726 	spin_unlock_irq(&np->lock);
       
  2727 }
       
  2728 #endif /* CONFIG_8139TOO_MMIO */
       
  2729 
       
  2730 static int rtl8139_get_stats_count(struct net_device *dev)
       
  2731 {
       
  2732 	return RTL_NUM_STATS;
       
  2733 }
       
  2734 
       
  2735 static void rtl8139_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
       
  2736 {
       
  2737 	struct rtl8139_private *np = netdev_priv(dev);
       
  2738 
       
  2739 	data[0] = np->xstats.early_rx;
       
  2740 	data[1] = np->xstats.tx_buf_mapped;
       
  2741 	data[2] = np->xstats.tx_timeouts;
       
  2742 	data[3] = np->xstats.rx_lost_in_ring;
       
  2743 }
       
  2744 
       
  2745 static void rtl8139_get_strings(struct net_device *dev, u32 stringset, u8 *data)
       
  2746 {
       
  2747 	memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
       
  2748 }
       
  2749 
       
  2750 static struct ethtool_ops rtl8139_ethtool_ops = {
       
  2751 	.get_drvinfo		= rtl8139_get_drvinfo,
       
  2752 	.get_settings		= rtl8139_get_settings,
       
  2753 	.set_settings		= rtl8139_set_settings,
       
  2754 	.get_regs_len		= rtl8139_get_regs_len,
       
  2755 	.get_regs		= rtl8139_get_regs,
       
  2756 	.nway_reset		= rtl8139_nway_reset,
       
  2757 	.get_link		= rtl8139_get_link,
       
  2758 	.get_msglevel		= rtl8139_get_msglevel,
       
  2759 	.set_msglevel		= rtl8139_set_msglevel,
       
  2760 	.get_wol		= rtl8139_get_wol,
       
  2761 	.set_wol		= rtl8139_set_wol,
       
  2762 	.get_strings		= rtl8139_get_strings,
       
  2763 	.get_stats_count	= rtl8139_get_stats_count,
       
  2764 	.get_ethtool_stats	= rtl8139_get_ethtool_stats,
       
  2765 };
       
  2766 
       
  2767 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
       
  2768 {
       
  2769 	struct rtl8139_private *np = netdev_priv(dev);
       
  2770 	int rc;
       
  2771 
       
  2772         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2773 
       
  2774 	if (dev == rtl_ecat_dev.dev || !netif_running(dev))
       
  2775 		return -EINVAL;
       
  2776 
       
  2777         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2778 
       
  2779 	spin_lock_irq(&np->lock);
       
  2780 	rc = generic_mii_ioctl(&np->mii, if_mii(rq), cmd, NULL);
       
  2781 	spin_unlock_irq(&np->lock);
       
  2782 
       
  2783 	return rc;
       
  2784 }
       
  2785 
       
  2786 
       
  2787 static struct net_device_stats *rtl8139_get_stats (struct net_device *dev)
       
  2788 {
       
  2789 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2790 	void __iomem *ioaddr = tp->mmio_addr;
       
  2791 	unsigned long flags;
       
  2792 
       
  2793         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2794 
       
  2795 	if (dev == rtl_ecat_dev.dev || netif_running(dev))
       
  2796         {
       
  2797           spin_lock_irqsave (&tp->lock, flags);
       
  2798           tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
       
  2799           RTL_W32 (RxMissed, 0);
       
  2800           spin_unlock_irqrestore (&tp->lock, flags);
       
  2801 	}
       
  2802 
       
  2803         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2804 
       
  2805 	return &tp->stats;
       
  2806 }
       
  2807 
       
  2808 /* Set or clear the multicast filter for this adaptor.
       
  2809    This routine is not state sensitive and need not be SMP locked. */
       
  2810 
       
  2811 static void __set_rx_mode (struct net_device *dev)
       
  2812 {
       
  2813 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2814 	void __iomem *ioaddr = tp->mmio_addr;
       
  2815 	u32 mc_filter[2];	/* Multicast hash filter */
       
  2816 	int i, rx_mode;
       
  2817 	u32 tmp;
       
  2818 
       
  2819 	DPRINTK ("%s:   rtl8139_set_rx_mode(%4.4x) done -- Rx config %8.8lx.\n",
       
  2820 			dev->name, dev->flags, RTL_R32 (RxConfig));
       
  2821 
       
  2822 	/* Note: do not reorder, GCC is clever about common statements. */
       
  2823 	if (dev->flags & IFF_PROMISC) {
       
  2824 		/* Unconditionally log net taps. */
       
  2825 		printk (KERN_NOTICE "%s: Promiscuous mode enabled.\n",
       
  2826 			dev->name);
       
  2827 		rx_mode =
       
  2828 		    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
       
  2829 		    AcceptAllPhys;
       
  2830 		mc_filter[1] = mc_filter[0] = 0xffffffff;
       
  2831 	} else if ((dev->mc_count > multicast_filter_limit)
       
  2832 		   || (dev->flags & IFF_ALLMULTI)) {
       
  2833 		/* Too many to filter perfectly -- accept all multicasts. */
       
  2834 		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
       
  2835 		mc_filter[1] = mc_filter[0] = 0xffffffff;
       
  2836 	} else {
       
  2837 		struct dev_mc_list *mclist;
       
  2838 		rx_mode = AcceptBroadcast | AcceptMyPhys;
       
  2839 		mc_filter[1] = mc_filter[0] = 0;
       
  2840 		for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
       
  2841 		     i++, mclist = mclist->next) {
       
  2842 			int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
       
  2843 
       
  2844 			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
       
  2845 			rx_mode |= AcceptMulticast;
       
  2846 		}
       
  2847 	}
       
  2848 
       
  2849 	/* We can safely update without stopping the chip. */
       
  2850 	tmp = rtl8139_rx_config | rx_mode;
       
  2851 	if (tp->rx_config != tmp) {
       
  2852 		RTL_W32_F (RxConfig, tmp);
       
  2853 		tp->rx_config = tmp;
       
  2854 	}
       
  2855 	RTL_W32_F (MAR0 + 0, mc_filter[0]);
       
  2856 	RTL_W32_F (MAR0 + 4, mc_filter[1]);
       
  2857 }
       
  2858 
       
  2859 static void rtl8139_set_rx_mode (struct net_device *dev)
       
  2860 {
       
  2861 	unsigned long flags;
       
  2862 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2863 
       
  2864         spin_lock_irqsave (&tp->lock, flags);
       
  2865         __set_rx_mode(dev);
       
  2866         spin_unlock_irqrestore (&tp->lock, flags);
       
  2867 }
       
  2868 
       
  2869 #ifdef CONFIG_PM
       
  2870 
       
  2871 static int rtl8139_suspend (struct pci_dev *pdev, pm_message_t state)
       
  2872 {
       
  2873 	struct net_device *dev = pci_get_drvdata (pdev);
       
  2874 	struct rtl8139_private *tp = netdev_priv(dev);
       
  2875 	void __iomem *ioaddr = tp->mmio_addr;
       
  2876 	unsigned long flags;
       
  2877 
       
  2878 	pci_save_state (pdev);
       
  2879 
       
  2880         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2881 
       
  2882 	if (dev == rtl_ecat_dev.dev || !netif_running (dev))
       
  2883           return 0;
       
  2884 
       
  2885         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2886 
       
  2887 	netif_device_detach (dev);
       
  2888 
       
  2889 	spin_lock_irqsave (&tp->lock, flags);
       
  2890 
       
  2891 	/* Disable interrupts, stop Tx and Rx. */
       
  2892 	RTL_W16 (IntrMask, 0);
       
  2893 	RTL_W8 (ChipCmd, 0);
       
  2894 
       
  2895 	/* Update the error counts. */
       
  2896 	tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
       
  2897 	RTL_W32 (RxMissed, 0);
       
  2898 
       
  2899 	spin_unlock_irqrestore (&tp->lock, flags);
       
  2900 
       
  2901 	pci_set_power_state (pdev, PCI_D3hot);
       
  2902 
       
  2903 	return 0;
       
  2904 }
       
  2905 
       
  2906 
       
  2907 static int rtl8139_resume (struct pci_dev *pdev)
       
  2908 {
       
  2909 	struct net_device *dev = pci_get_drvdata (pdev);
       
  2910 
       
  2911 	pci_restore_state (pdev);
       
  2912 
       
  2913         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2914 
       
  2915 	if (dev == rtl_ecat_dev.dev || !netif_running (dev))
       
  2916           return 0;
       
  2917 
       
  2918         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2919 
       
  2920 	pci_set_power_state (pdev, PCI_D0);
       
  2921 	rtl8139_init_ring (dev);
       
  2922 	rtl8139_hw_start (dev);
       
  2923 	netif_device_attach (dev);
       
  2924 	return 0;
       
  2925 }
       
  2926 
       
  2927 #endif /* CONFIG_PM */
       
  2928 
       
  2929 
       
  2930 static struct pci_driver rtl8139_pci_driver = {
       
  2931 	.name		= DRV_NAME,
       
  2932 	.id_table	= rtl8139_pci_tbl,
       
  2933 	.probe		= rtl8139_init_one,
       
  2934 	.remove		= __devexit_p(rtl8139_remove_one),
       
  2935 #ifdef CONFIG_PM
       
  2936 	.suspend	= rtl8139_suspend,
       
  2937 	.resume		= rtl8139_resume,
       
  2938 #endif /* CONFIG_PM */
       
  2939 };
       
  2940 
       
  2941 
       
  2942 static int __init rtl8139_init_module (void)
       
  2943 {
       
  2944 	/* when we're a module, we always print a version message,
       
  2945 	 * even if no 8139 board is found.
       
  2946 	 */
       
  2947 #ifdef MODULE
       
  2948 	printk (KERN_INFO RTL8139_DRIVER_NAME "\n");
       
  2949 #endif
       
  2950 
       
  2951         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2952 
       
  2953         EtherCAT_device_init(&rtl_ecat_dev);
       
  2954 
       
  2955         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2956 
       
  2957 	return pci_module_init (&rtl8139_pci_driver);
       
  2958 }
       
  2959 
       
  2960 
       
  2961 static void __exit rtl8139_cleanup_module (void)
       
  2962 {
       
  2963 	pci_unregister_driver (&rtl8139_pci_driver);
       
  2964 
       
  2965         /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2966 
       
  2967         EtherCAT_device_clear(&rtl_ecat_dev);
       
  2968 
       
  2969         /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2970 }
       
  2971 
       
  2972 
       
  2973 module_init(rtl8139_init_module);
       
  2974 module_exit(rtl8139_cleanup_module);