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