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