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