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