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