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