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