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