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