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