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