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