sis190.c 46.5 KB
Newer Older
F
Francois Romieu 已提交
1 2 3 4 5 6 7
/*
   sis190.c: Silicon Integrated Systems SiS190 ethernet driver

   Copyright (c) 2003 K.M. Liu <kmliu@sis.com>
   Copyright (c) 2003, 2004 Jeff Garzik <jgarzik@pobox.com>
   Copyright (c) 2003, 2004, 2005 Francois Romieu <romieu@fr.zoreil.com>

8 9
   Based on r8169.c, tg3.c, 8139cp.c, skge.c, epic100.c and SiS 190/191
   genuine driver.
F
Francois Romieu 已提交
10 11 12 13 14 15 16 17 18 19

   This software may be used and distributed according to the terms of
   the GNU General Public License (GPL), incorporated herein by reference.
   Drivers based on or derived from this code fall under the GPL and must
   retain the authorship, copyright and license notice.  This file is not
   a complete program and may only be used when the entire operating
   system is licensed under the GPL.

   See the file COPYING in this distribution for more information.

20 21 22
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
F
Francois Romieu 已提交
23

24
#include <linux/interrupt.h>
F
Francois Romieu 已提交
25 26 27
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/netdevice.h>
28
#include <linux/rtnetlink.h>
F
Francois Romieu 已提交
29 30 31 32 33 34 35
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/pci.h>
#include <linux/mii.h>
#include <linux/delay.h>
#include <linux/crc32.h>
#include <linux/dma-mapping.h>
36
#include <linux/slab.h>
F
Francois Romieu 已提交
37 38
#include <asm/irq.h>

39 40 41 42
#define PHY_MAX_ADDR		32
#define PHY_ID_ANY		0x1f
#define MII_REG_ANY		0x1f

43
#define DRV_VERSION		"1.4"
F
Francois Romieu 已提交
44 45 46 47 48 49 50 51
#define DRV_NAME		"sis190"
#define SIS190_DRIVER_NAME	DRV_NAME " Gigabit Ethernet driver " DRV_VERSION

#define sis190_rx_skb			netif_rx
#define sis190_rx_quota(count, quota)	count

#define MAC_ADDR_LEN		6

52 53
#define NUM_TX_DESC		64	/* [8..1024] */
#define NUM_RX_DESC		64	/* [8..8192] */
F
Francois Romieu 已提交
54 55 56
#define TX_RING_BYTES		(NUM_TX_DESC * sizeof(struct TxDesc))
#define RX_RING_BYTES		(NUM_RX_DESC * sizeof(struct RxDesc))
#define RX_BUF_SIZE		1536
57
#define RX_BUF_MASK		0xfff8
F
Francois Romieu 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#define SIS190_REGS_SIZE	0x80
#define SIS190_TX_TIMEOUT	(6*HZ)
#define SIS190_PHY_TIMEOUT	(10*HZ)
#define SIS190_MSG_DEFAULT	(NETIF_MSG_DRV | NETIF_MSG_PROBE | \
				 NETIF_MSG_LINK | NETIF_MSG_IFUP | \
				 NETIF_MSG_IFDOWN)

/* Enhanced PHY access register bit definitions */
#define EhnMIIread		0x0000
#define EhnMIIwrite		0x0020
#define EhnMIIdataShift		16
#define EhnMIIpmdShift		6	/* 7016 only */
#define EhnMIIregShift		11
#define EhnMIIreq		0x0010
#define EhnMIInotDone		0x0010

/* Write/read MMIO register */
#define SIS_W8(reg, val)	writeb ((val), ioaddr + (reg))
#define SIS_W16(reg, val)	writew ((val), ioaddr + (reg))
#define SIS_W32(reg, val)	writel ((val), ioaddr + (reg))
#define SIS_R8(reg)		readb (ioaddr + (reg))
#define SIS_R16(reg)		readw (ioaddr + (reg))
#define SIS_R32(reg)		readl (ioaddr + (reg))

#define SIS_PCI_COMMIT()	SIS_R32(IntrControl)

enum sis190_registers {
	TxControl		= 0x00,
	TxDescStartAddr		= 0x04,
88 89
	rsv0			= 0x08,	// reserved
	TxSts			= 0x0c,	// unused (Control/Status)
F
Francois Romieu 已提交
90 91
	RxControl		= 0x10,
	RxDescStartAddr		= 0x14,
92 93
	rsv1			= 0x18,	// reserved
	RxSts			= 0x1c,	// unused
F
Francois Romieu 已提交
94 95 96
	IntrStatus		= 0x20,
	IntrMask		= 0x24,
	IntrControl		= 0x28,
L
Lucas De Marchi 已提交
97
	IntrTimer		= 0x2c,	// unused (Interrupt Timer)
98 99
	PMControl		= 0x30,	// unused (Power Mgmt Control/Status)
	rsv2			= 0x34,	// reserved
F
Francois Romieu 已提交
100 101 102 103
	ROMControl		= 0x38,
	ROMInterface		= 0x3c,
	StationControl		= 0x40,
	GMIIControl		= 0x44,
104 105
	GIoCR			= 0x48, // unused (GMAC IO Compensation)
	GIoCtrl			= 0x4c, // unused (GMAC IO Control)
F
Francois Romieu 已提交
106
	TxMacControl		= 0x50,
107 108 109
	TxLimit			= 0x54, // unused (Tx MAC Timer/TryLimit)
	RGDelay			= 0x58, // unused (RGMII Tx Internal Delay)
	rsv3			= 0x5c, // reserved
F
Francois Romieu 已提交
110 111 112 113
	RxMacControl		= 0x60,
	RxMacAddr		= 0x62,
	RxHashTable		= 0x68,
	// Undocumented		= 0x6c,
114 115 116 117
	RxWolCtrl		= 0x70,
	RxWolData		= 0x74, // unused (Rx WOL Data Access)
	RxMPSControl		= 0x78,	// unused (Rx MPS Control)
	rsv4			= 0x7c, // reserved
F
Francois Romieu 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
};

enum sis190_register_content {
	/* IntrStatus */
	SoftInt			= 0x40000000,	// unused
	Timeup			= 0x20000000,	// unused
	PauseFrame		= 0x00080000,	// unused
	MagicPacket		= 0x00040000,	// unused
	WakeupFrame		= 0x00020000,	// unused
	LinkChange		= 0x00010000,
	RxQEmpty		= 0x00000080,
	RxQInt			= 0x00000040,
	TxQ1Empty		= 0x00000020,	// unused
	TxQ1Int			= 0x00000010,
	TxQ0Empty		= 0x00000008,	// unused
	TxQ0Int			= 0x00000004,
	RxHalt			= 0x00000002,
	TxHalt			= 0x00000001,

	/* {Rx/Tx}CmdBits */
	CmdReset		= 0x10,
	CmdRxEnb		= 0x08,		// unused
	CmdTxEnb		= 0x01,
	RxBufEmpty		= 0x01,		// unused

	/* Cfg9346Bits */
	Cfg9346_Lock		= 0x00,		// unused
	Cfg9346_Unlock		= 0xc0,		// unused

	/* RxMacControl */
	AcceptErr		= 0x20,		// unused
	AcceptRunt		= 0x10,		// unused
	AcceptBroadcast		= 0x0800,
	AcceptMulticast		= 0x0400,
	AcceptMyPhys		= 0x0200,
	AcceptAllPhys		= 0x0100,

	/* RxConfigBits */
	RxCfgFIFOShift		= 13,
	RxCfgDMAShift		= 8,		// 0x1a in RxControl ?

	/* TxConfigBits */
	TxInterFrameGapShift	= 24,
	TxDMAShift		= 8, /* DMA burst value (0-7) is shift this many bits */

	LinkStatus		= 0x02,		// unused
	FullDup			= 0x01,		// unused

	/* TBICSRBit */
	TBILinkOK		= 0x02000000,	// unused
};

struct TxDesc {
171 172 173 174
	__le32 PSize;
	__le32 status;
	__le32 addr;
	__le32 size;
F
Francois Romieu 已提交
175 176 177
};

struct RxDesc {
178 179 180 181
	__le32 PSize;
	__le32 status;
	__le32 addr;
	__le32 size;
F
Francois Romieu 已提交
182 183 184 185
};

enum _DescStatusBit {
	/* _Desc.status */
186 187 188 189
	OWNbit		= 0x80000000, // RXOWN/TXOWN
	INTbit		= 0x40000000, // RXINT/TXINT
	CRCbit		= 0x00020000, // CRCOFF/CRCEN
	PADbit		= 0x00010000, // PREADD/PADEN
F
Francois Romieu 已提交
190
	/* _Desc.size */
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	RingEnd		= 0x80000000,
	/* TxDesc.status */
	LSEN		= 0x08000000, // TSO ? -- FR
	IPCS		= 0x04000000,
	TCPCS		= 0x02000000,
	UDPCS		= 0x01000000,
	BSTEN		= 0x00800000,
	EXTEN		= 0x00400000,
	DEFEN		= 0x00200000,
	BKFEN		= 0x00100000,
	CRSEN		= 0x00080000,
	COLEN		= 0x00040000,
	THOL3		= 0x30000000,
	THOL2		= 0x20000000,
	THOL1		= 0x10000000,
	THOL0		= 0x00000000,
F
Francois Romieu 已提交
207 208 209 210 211 212

	WND		= 0x00080000,
	TABRT		= 0x00040000,
	FIFO		= 0x00020000,
	LINK		= 0x00010000,
	ColCountMask	= 0x0000ffff,
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	/* RxDesc.status */
	IPON		= 0x20000000,
	TCPON		= 0x10000000,
	UDPON		= 0x08000000,
	Wakup		= 0x00400000,
	Magic		= 0x00200000,
	Pause		= 0x00100000,
	DEFbit		= 0x00200000,
	BCAST		= 0x000c0000,
	MCAST		= 0x00080000,
	UCAST		= 0x00040000,
	/* RxDesc.PSize */
	TAGON		= 0x80000000,
	RxDescCountMask	= 0x7f000000, // multi-desc pkt when > 1 ? -- FR
	ABORT		= 0x00800000,
	SHORT		= 0x00400000,
	LIMIT		= 0x00200000,
	MIIER		= 0x00100000,
	OVRUN		= 0x00080000,
	NIBON		= 0x00040000,
	COLON		= 0x00020000,
	CRCOK		= 0x00010000,
F
Francois Romieu 已提交
235
	RxSizeMask	= 0x0000ffff
236 237
	/*
	 * The asic could apparently do vlan, TSO, jumbo (sis191 only) and
L
Lucas De Marchi 已提交
238
	 * provide two (unused with Linux) Tx queues. No publicly
239 240
	 * available documentation alas.
	 */
F
Francois Romieu 已提交
241 242
};

243 244 245 246 247 248 249 250 251 252
enum sis190_eeprom_access_register_bits {
	EECS	= 0x00000001,	// unused
	EECLK	= 0x00000002,	// unused
	EEDO	= 0x00000008,	// unused
	EEDI	= 0x00000004,	// unused
	EEREQ	= 0x00000080,
	EEROP	= 0x00000200,
	EEWOP	= 0x00000100	// unused
};

253 254 255 256 257 258 259 260
/* EEPROM Addresses */
enum sis190_eeprom_address {
	EEPROMSignature	= 0x00,
	EEPROMCLK	= 0x01,	// unused
	EEPROMInfo	= 0x02,
	EEPROMMACAddr	= 0x03
};

261 262
enum sis190_feature {
	F_HAS_RGMII	= 1,
263 264
	F_PHY_88E1111	= 2,
	F_PHY_BCM5461	= 4
265 266
};

F
Francois Romieu 已提交
267 268 269
struct sis190_private {
	void __iomem *mmio_addr;
	struct pci_dev *pci_dev;
D
David Howells 已提交
270
	struct net_device *dev;
F
Francois Romieu 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	spinlock_t lock;
	u32 rx_buf_sz;
	u32 cur_rx;
	u32 cur_tx;
	u32 dirty_rx;
	u32 dirty_tx;
	dma_addr_t rx_dma;
	dma_addr_t tx_dma;
	struct RxDesc *RxDescRing;
	struct TxDesc *TxDescRing;
	struct sk_buff *Rx_skbuff[NUM_RX_DESC];
	struct sk_buff *Tx_skbuff[NUM_TX_DESC];
	struct work_struct phy_task;
	struct timer_list timer;
	u32 msg_enable;
286
	struct mii_if_info mii_if;
287
	struct list_head first_phy;
288
	u32 features;
289
	u32 negotiated_lpa;
290 291 292 293 294
	enum {
		LNK_OFF,
		LNK_ON,
		LNK_AUTONEG,
	} link_status;
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
};

struct sis190_phy {
	struct list_head list;
	int phy_id;
	u16 id[2];
	u16 status;
	u8  type;
};

enum sis190_phy_type {
	UNKNOWN	= 0x00,
	HOME	= 0x01,
	LAN	= 0x02,
	MIX	= 0x03
};

static struct mii_chip_info {
        const char *name;
        u16 id[2];
        unsigned int type;
316
	u32 feature;
317
} mii_chip_table[] = {
318
	{ "Atheros PHY",          { 0x004d, 0xd010 }, LAN, 0 },
319
	{ "Atheros PHY AR8012",   { 0x004d, 0xd020 }, LAN, 0 },
320
	{ "Broadcom PHY BCM5461", { 0x0020, 0x60c0 }, LAN, F_PHY_BCM5461 },
F
Francois Romieu 已提交
321
	{ "Broadcom PHY AC131",   { 0x0143, 0xbc70 }, LAN, 0 },
322 323 324
	{ "Agere PHY ET1101B",    { 0x0282, 0xf010 }, LAN, 0 },
	{ "Marvell PHY 88E1111",  { 0x0141, 0x0cc0 }, LAN, F_PHY_88E1111 },
	{ "Realtek PHY RTL8201",  { 0x0000, 0x8200 }, LAN, 0 },
325
	{ NULL, }
F
Francois Romieu 已提交
326 327
};

328
static const struct {
F
Francois Romieu 已提交
329 330
	const char *name;
} sis_chip_info[] = {
331 332
	{ "SiS 190 PCI Fast Ethernet adapter" },
	{ "SiS 191 PCI Gigabit Ethernet adapter" },
F
Francois Romieu 已提交
333 334
};

335
static DEFINE_PCI_DEVICE_TABLE(sis190_pci_tbl) = {
F
Francois Romieu 已提交
336
	{ PCI_DEVICE(PCI_VENDOR_ID_SI, 0x0190), 0, 0, 0 },
337
	{ PCI_DEVICE(PCI_VENDOR_ID_SI, 0x0191), 0, 0, 1 },
F
Francois Romieu 已提交
338 339 340 341 342 343 344 345 346 347 348
	{ 0, },
};

MODULE_DEVICE_TABLE(pci, sis190_pci_tbl);

static int rx_copybreak = 200;

static struct {
	u32 msg_enable;
} debug = { -1 };

349
MODULE_DESCRIPTION("SiS sis190/191 Gigabit Ethernet driver");
F
Francois Romieu 已提交
350 351 352 353 354 355 356 357 358
module_param(rx_copybreak, int, 0);
MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
module_param_named(debug, debug.msg_enable, int, 0);
MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
MODULE_AUTHOR("K.M. Liu <kmliu@sis.com>, Ueimor <romieu@fr.zoreil.com>");
MODULE_VERSION(DRV_VERSION);
MODULE_LICENSE("GPL");

static const u32 sis190_intr_mask =
359
	RxQEmpty | RxQInt | TxQ1Int | TxQ0Int | RxHalt | TxHalt | LinkChange;
F
Francois Romieu 已提交
360 361 362 363 364

/*
 * Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
 * The chips use a 64 element hash table based on the Ethernet CRC.
 */
365
static const int multicast_filter_limit = 32;
F
Francois Romieu 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

static void __mdio_cmd(void __iomem *ioaddr, u32 ctl)
{
	unsigned int i;

	SIS_W32(GMIIControl, ctl);

	msleep(1);

	for (i = 0; i < 100; i++) {
		if (!(SIS_R32(GMIIControl) & EhnMIInotDone))
			break;
		msleep(1);
	}

381
	if (i > 99)
382
		pr_err("PHY command failed !\n");
F
Francois Romieu 已提交
383 384
}

385
static void mdio_write(void __iomem *ioaddr, int phy_id, int reg, int val)
F
Francois Romieu 已提交
386 387
{
	__mdio_cmd(ioaddr, EhnMIIreq | EhnMIIwrite |
388
		(((u32) reg) << EhnMIIregShift) | (phy_id << EhnMIIpmdShift) |
F
Francois Romieu 已提交
389 390 391
		(((u32) val) << EhnMIIdataShift));
}

392
static int mdio_read(void __iomem *ioaddr, int phy_id, int reg)
F
Francois Romieu 已提交
393 394
{
	__mdio_cmd(ioaddr, EhnMIIreq | EhnMIIread |
395
		(((u32) reg) << EhnMIIregShift) | (phy_id << EhnMIIpmdShift));
F
Francois Romieu 已提交
396 397 398 399

	return (u16) (SIS_R32(GMIIControl) >> EhnMIIdataShift);
}

400 401 402 403
static void __mdio_write(struct net_device *dev, int phy_id, int reg, int val)
{
	struct sis190_private *tp = netdev_priv(dev);

404
	mdio_write(tp->mmio_addr, phy_id, reg, val);
405 406 407 408 409 410
}

static int __mdio_read(struct net_device *dev, int phy_id, int reg)
{
	struct sis190_private *tp = netdev_priv(dev);

411
	return mdio_read(tp->mmio_addr, phy_id, reg);
412 413
}

414 415 416 417 418 419
static u16 mdio_read_latched(void __iomem *ioaddr, int phy_id, int reg)
{
	mdio_read(ioaddr, phy_id, reg);
	return mdio_read(ioaddr, phy_id, reg);
}

420
static u16 __devinit sis190_read_eeprom(void __iomem *ioaddr, u32 reg)
F
Francois Romieu 已提交
421
{
422
	u16 data = 0xffff;
F
Francois Romieu 已提交
423 424 425 426 427
	unsigned int i;

	if (!(SIS_R32(ROMControl) & 0x0002))
		return 0;

428
	SIS_W32(ROMInterface, EEREQ | EEROP | (reg << 10));
F
Francois Romieu 已提交
429 430

	for (i = 0; i < 200; i++) {
431 432
		if (!(SIS_R32(ROMInterface) & EEREQ)) {
			data = (SIS_R32(ROMInterface) & 0xffff0000) >> 16;
F
Francois Romieu 已提交
433
			break;
434
		}
F
Francois Romieu 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		msleep(1);
	}

	return data;
}

static void sis190_irq_mask_and_ack(void __iomem *ioaddr)
{
	SIS_W32(IntrMask, 0x00);
	SIS_W32(IntrStatus, 0xffffffff);
	SIS_PCI_COMMIT();
}

static void sis190_asic_down(void __iomem *ioaddr)
{
	/* Stop the chip's Tx and Rx DMA processes. */

	SIS_W32(TxControl, 0x1a00);
	SIS_W32(RxControl, 0x1a00);

	sis190_irq_mask_and_ack(ioaddr);
}

static void sis190_mark_as_last_descriptor(struct RxDesc *desc)
{
	desc->size |= cpu_to_le32(RingEnd);
}

static inline void sis190_give_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
{
	u32 eor = le32_to_cpu(desc->size) & RingEnd;

	desc->PSize = 0x0;
468
	desc->size = cpu_to_le32((rx_buf_sz & RX_BUF_MASK) | eor);
F
Francois Romieu 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482
	wmb();
	desc->status = cpu_to_le32(OWNbit | INTbit);
}

static inline void sis190_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
				      u32 rx_buf_sz)
{
	desc->addr = cpu_to_le32(mapping);
	sis190_give_to_asic(desc, rx_buf_sz);
}

static inline void sis190_make_unusable_by_asic(struct RxDesc *desc)
{
	desc->PSize = 0x0;
A
Al Viro 已提交
483
	desc->addr = cpu_to_le32(0xdeadbeef);
F
Francois Romieu 已提交
484 485 486 487 488
	desc->size &= cpu_to_le32(RingEnd);
	wmb();
	desc->status = 0x0;
}

S
Stephen Hemminger 已提交
489 490
static struct sk_buff *sis190_alloc_rx_skb(struct sis190_private *tp,
					   struct RxDesc *desc)
F
Francois Romieu 已提交
491
{
S
Stephen Hemminger 已提交
492
	u32 rx_buf_sz = tp->rx_buf_sz;
F
Francois Romieu 已提交
493
	struct sk_buff *skb;
494
	dma_addr_t mapping;
F
Francois Romieu 已提交
495

S
Stephen Hemminger 已提交
496
	skb = netdev_alloc_skb(tp->dev, rx_buf_sz);
497 498 499 500 501 502 503
	if (unlikely(!skb))
		goto skb_alloc_failed;
	mapping = pci_map_single(tp->pci_dev, skb->data, tp->rx_buf_sz,
			PCI_DMA_FROMDEVICE);
	if (pci_dma_mapping_error(tp->pci_dev, mapping))
		goto out;
	sis190_map_to_asic(desc, mapping, rx_buf_sz);
F
Francois Romieu 已提交
504

505
	return skb;
506 507 508 509 510 511

out:
	dev_kfree_skb_any(skb);
skb_alloc_failed:
	sis190_make_unusable_by_asic(desc);
	return NULL;
F
Francois Romieu 已提交
512 513 514 515 516 517 518 519
}

static u32 sis190_rx_fill(struct sis190_private *tp, struct net_device *dev,
			  u32 start, u32 end)
{
	u32 cur;

	for (cur = start; cur < end; cur++) {
520
		unsigned int i = cur % NUM_RX_DESC;
F
Francois Romieu 已提交
521 522 523 524

		if (tp->Rx_skbuff[i])
			continue;

S
Stephen Hemminger 已提交
525 526
		tp->Rx_skbuff[i] = sis190_alloc_rx_skb(tp, tp->RxDescRing + i);

527
		if (!tp->Rx_skbuff[i])
F
Francois Romieu 已提交
528 529 530 531 532
			break;
	}
	return cur - start;
}

F
Francois Romieu 已提交
533 534 535
static bool sis190_try_rx_copy(struct sis190_private *tp,
			       struct sk_buff **sk_buff, int pkt_size,
			       dma_addr_t addr)
F
Francois Romieu 已提交
536
{
F
Francois Romieu 已提交
537 538 539 540 541 542
	struct sk_buff *skb;
	bool done = false;

	if (pkt_size >= rx_copybreak)
		goto out;

543
	skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
F
Francois Romieu 已提交
544 545 546
	if (!skb)
		goto out;

547 548
	pci_dma_sync_single_for_cpu(tp->pci_dev, addr, tp->rx_buf_sz,
				PCI_DMA_FROMDEVICE);
F
Francois Romieu 已提交
549 550 551 552 553
	skb_copy_to_linear_data(skb, sk_buff[0]->data, pkt_size);
	*sk_buff = skb;
	done = true;
out:
	return done;
F
Francois Romieu 已提交
554 555
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
static inline int sis190_rx_pkt_err(u32 status, struct net_device_stats *stats)
{
#define ErrMask	(OVRUN | SHORT | LIMIT | MIIER | NIBON | COLON | ABORT)

	if ((status & CRCOK) && !(status & ErrMask))
		return 0;

	if (!(status & CRCOK))
		stats->rx_crc_errors++;
	else if (status & OVRUN)
		stats->rx_over_errors++;
	else if (status & (SHORT | LIMIT))
		stats->rx_length_errors++;
	else if (status & (MIIER | NIBON | COLON))
		stats->rx_frame_errors++;

	stats->rx_errors++;
	return -1;
}

F
Francois Romieu 已提交
576 577 578
static int sis190_rx_interrupt(struct net_device *dev,
			       struct sis190_private *tp, void __iomem *ioaddr)
{
579
	struct net_device_stats *stats = &dev->stats;
F
Francois Romieu 已提交
580 581 582 583 584 585 586 587 588 589 590
	u32 rx_left, cur_rx = tp->cur_rx;
	u32 delta, count;

	rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
	rx_left = sis190_rx_quota(rx_left, (u32) dev->quota);

	for (; rx_left > 0; rx_left--, cur_rx++) {
		unsigned int entry = cur_rx % NUM_RX_DESC;
		struct RxDesc *desc = tp->RxDescRing + entry;
		u32 status;

A
Al Viro 已提交
591
		if (le32_to_cpu(desc->status) & OWNbit)
F
Francois Romieu 已提交
592 593 594 595
			break;

		status = le32_to_cpu(desc->PSize);

596
		//netif_info(tp, intr, dev, "Rx PSize = %08x\n", status);
F
Francois Romieu 已提交
597

598
		if (sis190_rx_pkt_err(status, stats) < 0)
F
Francois Romieu 已提交
599
			sis190_give_to_asic(desc, tp->rx_buf_sz);
600
		else {
F
Francois Romieu 已提交
601
			struct sk_buff *skb = tp->Rx_skbuff[entry];
F
Francois Romieu 已提交
602
			dma_addr_t addr = le32_to_cpu(desc->addr);
F
Francois Romieu 已提交
603
			int pkt_size = (status & RxSizeMask) - 4;
F
Francois Romieu 已提交
604
			struct pci_dev *pdev = tp->pci_dev;
F
Francois Romieu 已提交
605 606

			if (unlikely(pkt_size > tp->rx_buf_sz)) {
607 608
				netif_info(tp, intr, dev,
					   "(frag) status = %08x\n", status);
F
Francois Romieu 已提交
609 610 611 612 613 614 615
				stats->rx_dropped++;
				stats->rx_length_errors++;
				sis190_give_to_asic(desc, tp->rx_buf_sz);
				continue;
			}


F
Francois Romieu 已提交
616 617 618 619 620 621 622
			if (sis190_try_rx_copy(tp, &skb, pkt_size, addr)) {
				pci_dma_sync_single_for_device(pdev, addr,
					tp->rx_buf_sz, PCI_DMA_FROMDEVICE);
				sis190_give_to_asic(desc, tp->rx_buf_sz);
			} else {
				pci_unmap_single(pdev, addr, tp->rx_buf_sz,
						 PCI_DMA_FROMDEVICE);
F
Francois Romieu 已提交
623 624 625 626 627 628 629 630 631 632
				tp->Rx_skbuff[entry] = NULL;
				sis190_make_unusable_by_asic(desc);
			}

			skb_put(skb, pkt_size);
			skb->protocol = eth_type_trans(skb, dev);

			sis190_rx_skb(skb);

			stats->rx_packets++;
633 634 635
			stats->rx_bytes += pkt_size;
			if ((status & BCAST) == MCAST)
				stats->multicast++;
F
Francois Romieu 已提交
636 637 638 639 640 641
		}
	}
	count = cur_rx - tp->cur_rx;
	tp->cur_rx = cur_rx;

	delta = sis190_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
642 643
	if (!delta && count)
		netif_info(tp, intr, dev, "no Rx buffer allocated\n");
F
Francois Romieu 已提交
644 645
	tp->dirty_rx += delta;

646 647
	if ((tp->dirty_rx + NUM_RX_DESC) == tp->cur_rx)
		netif_emerg(tp, intr, dev, "Rx buffers exhausted\n");
F
Francois Romieu 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

	return count;
}

static void sis190_unmap_tx_skb(struct pci_dev *pdev, struct sk_buff *skb,
				struct TxDesc *desc)
{
	unsigned int len;

	len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;

	pci_unmap_single(pdev, le32_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);

	memset(desc, 0x00, sizeof(*desc));
}

F
Francois Romieu 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
static inline int sis190_tx_pkt_err(u32 status, struct net_device_stats *stats)
{
#define TxErrMask	(WND | TABRT | FIFO | LINK)

	if (!unlikely(status & TxErrMask))
		return 0;

	if (status & WND)
		stats->tx_window_errors++;
	if (status & TABRT)
		stats->tx_aborted_errors++;
	if (status & FIFO)
		stats->tx_fifo_errors++;
	if (status & LINK)
		stats->tx_carrier_errors++;

	stats->tx_errors++;

	return -1;
}

F
Francois Romieu 已提交
685 686 687
static void sis190_tx_interrupt(struct net_device *dev,
				struct sis190_private *tp, void __iomem *ioaddr)
{
F
Francois Romieu 已提交
688
	struct net_device_stats *stats = &dev->stats;
F
Francois Romieu 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702
	u32 pending, dirty_tx = tp->dirty_tx;
	/*
	 * It would not be needed if queueing was allowed to be enabled
	 * again too early (hint: think preempt and unclocked smp systems).
	 */
	unsigned int queue_stopped;

	smp_rmb();
	pending = tp->cur_tx - dirty_tx;
	queue_stopped = (pending == NUM_TX_DESC);

	for (; pending; pending--, dirty_tx++) {
		unsigned int entry = dirty_tx % NUM_TX_DESC;
		struct TxDesc *txd = tp->TxDescRing + entry;
F
Francois Romieu 已提交
703
		u32 status = le32_to_cpu(txd->status);
F
Francois Romieu 已提交
704 705
		struct sk_buff *skb;

F
Francois Romieu 已提交
706
		if (status & OWNbit)
F
Francois Romieu 已提交
707 708 709 710
			break;

		skb = tp->Tx_skbuff[entry];

F
Francois Romieu 已提交
711 712 713 714 715
		if (likely(sis190_tx_pkt_err(status, stats) == 0)) {
			stats->tx_packets++;
			stats->tx_bytes += skb->len;
			stats->collisions += ((status & ColCountMask) - 1);
		}
F
Francois Romieu 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

		sis190_unmap_tx_skb(tp->pci_dev, skb, txd);
		tp->Tx_skbuff[entry] = NULL;
		dev_kfree_skb_irq(skb);
	}

	if (tp->dirty_tx != dirty_tx) {
		tp->dirty_tx = dirty_tx;
		smp_wmb();
		if (queue_stopped)
			netif_wake_queue(dev);
	}
}

/*
 * The interrupt handler does all of the Rx thread work and cleans up after
 * the Tx thread.
 */
734
static irqreturn_t sis190_interrupt(int irq, void *__dev)
F
Francois Romieu 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
{
	struct net_device *dev = __dev;
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	unsigned int handled = 0;
	u32 status;

	status = SIS_R32(IntrStatus);

	if ((status == 0xffffffff) || !status)
		goto out;

	handled = 1;

	if (unlikely(!netif_running(dev))) {
		sis190_asic_down(ioaddr);
		goto out;
	}

	SIS_W32(IntrStatus, status);

756
//	netif_info(tp, intr, dev, "status = %08x\n", status);
F
Francois Romieu 已提交
757 758

	if (status & LinkChange) {
759
		netif_info(tp, intr, dev, "link change\n");
760
		del_timer(&tp->timer);
F
Francois Romieu 已提交
761 762 763 764 765 766 767 768 769 770 771 772
		schedule_work(&tp->phy_task);
	}

	if (status & RxQInt)
		sis190_rx_interrupt(dev, tp, ioaddr);

	if (status & TxQ0Int)
		sis190_tx_interrupt(dev, tp, ioaddr);
out:
	return IRQ_RETVAL(handled);
}

773 774 775 776 777 778 779
#ifdef CONFIG_NET_POLL_CONTROLLER
static void sis190_netpoll(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	struct pci_dev *pdev = tp->pci_dev;

	disable_irq(pdev->irq);
780
	sis190_interrupt(pdev->irq, dev);
781 782 783 784
	enable_irq(pdev->irq);
}
#endif

F
Francois Romieu 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
static void sis190_free_rx_skb(struct sis190_private *tp,
			       struct sk_buff **sk_buff, struct RxDesc *desc)
{
	struct pci_dev *pdev = tp->pci_dev;

	pci_unmap_single(pdev, le32_to_cpu(desc->addr), tp->rx_buf_sz,
			 PCI_DMA_FROMDEVICE);
	dev_kfree_skb(*sk_buff);
	*sk_buff = NULL;
	sis190_make_unusable_by_asic(desc);
}

static void sis190_rx_clear(struct sis190_private *tp)
{
	unsigned int i;

	for (i = 0; i < NUM_RX_DESC; i++) {
		if (!tp->Rx_skbuff[i])
			continue;
		sis190_free_rx_skb(tp, tp->Rx_skbuff + i, tp->RxDescRing + i);
	}
}

static void sis190_init_ring_indexes(struct sis190_private *tp)
{
	tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
}

static int sis190_init_ring(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);

	sis190_init_ring_indexes(tp);

	memset(tp->Tx_skbuff, 0x0, NUM_TX_DESC * sizeof(struct sk_buff *));
	memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));

	if (sis190_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
		goto err_rx_clear;

	sis190_mark_as_last_descriptor(tp->RxDescRing + NUM_RX_DESC - 1);

	return 0;

err_rx_clear:
	sis190_rx_clear(tp);
	return -ENOMEM;
}

static void sis190_set_rx_mode(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	unsigned long flags;
	u32 mc_filter[2];	/* Multicast hash filter */
	u16 rx_mode;

	if (dev->flags & IFF_PROMISC) {
		rx_mode =
			AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
			AcceptAllPhys;
		mc_filter[1] = mc_filter[0] = 0xffffffff;
847
	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
F
Francois Romieu 已提交
848 849 850 851 852
		   (dev->flags & IFF_ALLMULTI)) {
		/* Too many to filter perfectly -- accept all multicasts. */
		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
		mc_filter[1] = mc_filter[0] = 0xffffffff;
	} else {
853
		struct netdev_hw_addr *ha;
F
Francois Romieu 已提交
854 855 856

		rx_mode = AcceptBroadcast | AcceptMyPhys;
		mc_filter[1] = mc_filter[0] = 0;
857
		netdev_for_each_mc_addr(ha, dev) {
F
Francois Romieu 已提交
858
			int bit_nr =
859
				ether_crc(ETH_ALEN, ha->addr) & 0x3f;
F
Francois Romieu 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
			rx_mode |= AcceptMulticast;
		}
	}

	spin_lock_irqsave(&tp->lock, flags);

	SIS_W16(RxMacControl, rx_mode | 0x2);
	SIS_W32(RxHashTable, mc_filter[0]);
	SIS_W32(RxHashTable + 4, mc_filter[1]);

	spin_unlock_irqrestore(&tp->lock, flags);
}

static void sis190_soft_reset(void __iomem *ioaddr)
{
	SIS_W32(IntrControl, 0x8000);
	SIS_PCI_COMMIT();
	SIS_W32(IntrControl, 0x0);
	sis190_asic_down(ioaddr);
}

static void sis190_hw_start(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;

	sis190_soft_reset(ioaddr);

	SIS_W32(TxDescStartAddr, tp->tx_dma);
	SIS_W32(RxDescStartAddr, tp->rx_dma);

	SIS_W32(IntrStatus, 0xffffffff);
	SIS_W32(IntrMask, 0x0);
	SIS_W32(GMIIControl, 0x0);
	SIS_W32(TxMacControl, 0x60);
	SIS_W16(RxMacControl, 0x02);
	SIS_W32(RxHashTable, 0x0);
	SIS_W32(0x6c, 0x0);
899 900
	SIS_W32(RxWolCtrl, 0x0);
	SIS_W32(RxWolData, 0x0);
F
Francois Romieu 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914

	SIS_PCI_COMMIT();

	sis190_set_rx_mode(dev);

	/* Enable all known interrupts by setting the interrupt mask. */
	SIS_W32(IntrMask, sis190_intr_mask);

	SIS_W32(TxControl, 0x1a00 | CmdTxEnb);
	SIS_W32(RxControl, 0x1a1d);

	netif_start_queue(dev);
}

D
David Howells 已提交
915
static void sis190_phy_task(struct work_struct *work)
F
Francois Romieu 已提交
916
{
D
David Howells 已提交
917 918 919
	struct sis190_private *tp =
		container_of(work, struct sis190_private, phy_task);
	struct net_device *dev = tp->dev;
F
Francois Romieu 已提交
920
	void __iomem *ioaddr = tp->mmio_addr;
921
	int phy_id = tp->mii_if.phy_id;
F
Francois Romieu 已提交
922 923
	u16 val;

924 925
	rtnl_lock();

926 927 928
	if (!netif_running(dev))
		goto out_unlock;

929
	val = mdio_read(ioaddr, phy_id, MII_BMCR);
F
Francois Romieu 已提交
930 931 932
	if (val & BMCR_RESET) {
		// FIXME: needlessly high ?  -- FR 02/07/2005
		mod_timer(&tp->timer, jiffies + HZ/10);
933 934 935 936 937
		goto out_unlock;
	}

	val = mdio_read_latched(ioaddr, phy_id, MII_BMSR);
	if (!(val & BMSR_ANEGCOMPLETE) && tp->link_status != LNK_AUTONEG) {
938
		netif_carrier_off(dev);
939
		netif_warn(tp, link, dev, "auto-negotiating...\n");
940 941
		tp->link_status = LNK_AUTONEG;
	} else if ((val & BMSR_LSTATUS) && tp->link_status != LNK_ON) {
F
Francois Romieu 已提交
942 943 944
		/* Rejoice ! */
		struct {
			int val;
945
			u32 ctl;
F
Francois Romieu 已提交
946 947
			const char *msg;
		} reg31[] = {
948
			{ LPA_1000FULL, 0x07000c00 | 0x00001000,
949
				"1000 Mbps Full Duplex" },
950
			{ LPA_1000HALF, 0x07000c00,
951 952 953 954 955 956 957 958 959 960
				"1000 Mbps Half Duplex" },
			{ LPA_100FULL, 0x04000800 | 0x00001000,
				"100 Mbps Full Duplex" },
			{ LPA_100HALF, 0x04000800,
				"100 Mbps Half Duplex" },
			{ LPA_10FULL, 0x04000400 | 0x00001000,
				"10 Mbps Full Duplex" },
			{ LPA_10HALF, 0x04000400,
				"10 Mbps Half Duplex" },
			{ 0, 0x04000400, "unknown" }
961 962
		}, *p = NULL;
		u16 adv, autoexp, gigadv, gigrec;
F
Francois Romieu 已提交
963

964
		val = mdio_read(ioaddr, phy_id, 0x1f);
965
		netif_info(tp, link, dev, "mii ext = %04x\n", val);
F
Francois Romieu 已提交
966

967
		val = mdio_read(ioaddr, phy_id, MII_LPA);
968
		adv = mdio_read(ioaddr, phy_id, MII_ADVERTISE);
969
		autoexp = mdio_read(ioaddr, phy_id, MII_EXPANSION);
970 971
		netif_info(tp, link, dev, "mii lpa=%04x adv=%04x exp=%04x\n",
			   val, adv, autoexp);
972 973 974 975 976 977 978 979 980 981 982 983 984

		if (val & LPA_NPAGE && autoexp & EXPANSION_NWAY) {
			/* check for gigabit speed */
			gigadv = mdio_read(ioaddr, phy_id, MII_CTRL1000);
			gigrec = mdio_read(ioaddr, phy_id, MII_STAT1000);
			val = (gigadv & (gigrec >> 2));
			if (val & ADVERTISE_1000FULL)
				p = reg31;
			else if (val & ADVERTISE_1000HALF)
				p = reg31 + 1;
		}
		if (!p) {
			val &= adv;
F
Francois Romieu 已提交
985

986 987 988 989
			for (p = reg31; p->val; p++) {
				if ((val & p->val) == p->val)
					break;
			}
F
Francois Romieu 已提交
990
		}
991 992 993

		p->ctl |= SIS_R32(StationControl) & ~0x0f001c00;

994 995 996 997 998 999 1000 1001 1002
		if ((tp->features & F_HAS_RGMII) &&
		    (tp->features & F_PHY_BCM5461)) {
			// Set Tx Delay in RGMII mode.
			mdio_write(ioaddr, phy_id, 0x18, 0xf1c7);
			udelay(200);
			mdio_write(ioaddr, phy_id, 0x1c, 0x8c00);
			p->ctl |= 0x03000000;
		}

1003 1004
		SIS_W32(StationControl, p->ctl);

1005 1006 1007 1008 1009
		if (tp->features & F_HAS_RGMII) {
			SIS_W32(RGDelay, 0x0441);
			SIS_W32(RGDelay, 0x0440);
		}

1010 1011
		tp->negotiated_lpa = p->val;

1012
		netif_info(tp, link, dev, "link on %s mode\n", p->msg);
F
Francois Romieu 已提交
1013
		netif_carrier_on(dev);
1014 1015 1016 1017
		tp->link_status = LNK_ON;
	} else if (!(val & BMSR_LSTATUS) && tp->link_status != LNK_AUTONEG)
		tp->link_status = LNK_OFF;
	mod_timer(&tp->timer, jiffies + SIS190_PHY_TIMEOUT);
1018

1019
out_unlock:
1020
	rtnl_unlock();
F
Francois Romieu 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
}

static void sis190_phy_timer(unsigned long __opaque)
{
	struct net_device *dev = (struct net_device *)__opaque;
	struct sis190_private *tp = netdev_priv(dev);

	if (likely(netif_running(dev)))
		schedule_work(&tp->phy_task);
}

static inline void sis190_delete_timer(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);

	del_timer_sync(&tp->timer);
}

static inline void sis190_request_timer(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	struct timer_list *timer = &tp->timer;

	init_timer(timer);
	timer->expires = jiffies + SIS190_PHY_TIMEOUT;
	timer->data = (unsigned long)dev;
	timer->function = sis190_phy_timer;
	add_timer(timer);
}

static void sis190_set_rxbufsize(struct sis190_private *tp,
				 struct net_device *dev)
{
	unsigned int mtu = dev->mtu;

	tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
1057 1058 1059 1060 1061
	/* RxDesc->size has a licence to kill the lower bits */
	if (tp->rx_buf_sz & 0x07) {
		tp->rx_buf_sz += 8;
		tp->rx_buf_sz &= RX_BUF_MASK;
	}
F
Francois Romieu 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
}

static int sis190_open(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	struct pci_dev *pdev = tp->pci_dev;
	int rc = -ENOMEM;

	sis190_set_rxbufsize(tp, dev);

	/*
	 * Rx and Tx descriptors need 256 bytes alignment.
	 * pci_alloc_consistent() guarantees a stronger alignment.
	 */
	tp->TxDescRing = pci_alloc_consistent(pdev, TX_RING_BYTES, &tp->tx_dma);
	if (!tp->TxDescRing)
		goto out;

	tp->RxDescRing = pci_alloc_consistent(pdev, RX_RING_BYTES, &tp->rx_dma);
	if (!tp->RxDescRing)
		goto err_free_tx_0;

	rc = sis190_init_ring(dev);
	if (rc < 0)
		goto err_free_rx_1;

	sis190_request_timer(dev);

1090
	rc = request_irq(dev->irq, sis190_interrupt, IRQF_SHARED, dev->name, dev);
F
Francois Romieu 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	if (rc < 0)
		goto err_release_timer_2;

	sis190_hw_start(dev);
out:
	return rc;

err_release_timer_2:
	sis190_delete_timer(dev);
	sis190_rx_clear(tp);
err_free_rx_1:
	pci_free_consistent(tp->pci_dev, RX_RING_BYTES, tp->RxDescRing,
		tp->rx_dma);
err_free_tx_0:
	pci_free_consistent(tp->pci_dev, TX_RING_BYTES, tp->TxDescRing,
		tp->tx_dma);
	goto out;
}

static void sis190_tx_clear(struct sis190_private *tp)
{
	unsigned int i;

	for (i = 0; i < NUM_TX_DESC; i++) {
		struct sk_buff *skb = tp->Tx_skbuff[i];

		if (!skb)
			continue;

		sis190_unmap_tx_skb(tp->pci_dev, skb, tp->TxDescRing + i);
		tp->Tx_skbuff[i] = NULL;
		dev_kfree_skb(skb);

1124
		tp->dev->stats.tx_dropped++;
F
Francois Romieu 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	}
	tp->cur_tx = tp->dirty_tx = 0;
}

static void sis190_down(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	unsigned int poll_locked = 0;

	sis190_delete_timer(dev);

	netif_stop_queue(dev);

	do {
		spin_lock_irq(&tp->lock);

		sis190_asic_down(ioaddr);

		spin_unlock_irq(&tp->lock);

		synchronize_irq(dev->irq);

1148
		if (!poll_locked)
F
Francois Romieu 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
			poll_locked++;

		synchronize_sched();

	} while (SIS_R32(IntrMask));

	sis190_tx_clear(tp);
	sis190_rx_clear(tp);
}

static int sis190_close(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	struct pci_dev *pdev = tp->pci_dev;

	sis190_down(dev);

	free_irq(dev->irq, dev);

	pci_free_consistent(pdev, TX_RING_BYTES, tp->TxDescRing, tp->tx_dma);
	pci_free_consistent(pdev, RX_RING_BYTES, tp->RxDescRing, tp->rx_dma);

	tp->TxDescRing = NULL;
	tp->RxDescRing = NULL;

	return 0;
}

1177 1178
static netdev_tx_t sis190_start_xmit(struct sk_buff *skb,
				     struct net_device *dev)
F
Francois Romieu 已提交
1179 1180 1181 1182 1183 1184 1185 1186
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	u32 len, entry, dirty_tx;
	struct TxDesc *desc;
	dma_addr_t mapping;

	if (unlikely(skb->len < ETH_ZLEN)) {
1187
		if (skb_padto(skb, ETH_ZLEN)) {
1188
			dev->stats.tx_dropped++;
F
Francois Romieu 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
			goto out;
		}
		len = ETH_ZLEN;
	} else {
		len = skb->len;
	}

	entry = tp->cur_tx % NUM_TX_DESC;
	desc = tp->TxDescRing + entry;

	if (unlikely(le32_to_cpu(desc->status) & OWNbit)) {
		netif_stop_queue(dev);
1201 1202
		netif_err(tp, tx_err, dev,
			  "BUG! Tx Ring full when queue awake!\n");
F
Francois Romieu 已提交
1203 1204 1205 1206
		return NETDEV_TX_BUSY;
	}

	mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
1207 1208 1209 1210 1211
	if (pci_dma_mapping_error(tp->pci_dev, mapping)) {
		netif_err(tp, tx_err, dev,
				"PCI mapping failed, dropping packet");
		return NETDEV_TX_BUSY;
	}
F
Francois Romieu 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

	tp->Tx_skbuff[entry] = skb;

	desc->PSize = cpu_to_le32(len);
	desc->addr = cpu_to_le32(mapping);

	desc->size = cpu_to_le32(len);
	if (entry == (NUM_TX_DESC - 1))
		desc->size |= cpu_to_le32(RingEnd);

	wmb();

	desc->status = cpu_to_le32(OWNbit | INTbit | DEFbit | CRCbit | PADbit);
1225 1226 1227 1228 1229 1230
	if (tp->negotiated_lpa & (LPA_1000HALF | LPA_100HALF | LPA_10HALF)) {
		/* Half Duplex */
		desc->status |= cpu_to_le32(COLEN | CRSEN | BKFEN);
		if (tp->negotiated_lpa & (LPA_1000HALF | LPA_1000FULL))
			desc->status |= cpu_to_le32(EXTEN | BSTEN); /* gigabit HD */
	}
F
Francois Romieu 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

	tp->cur_tx++;

	smp_wmb();

	SIS_W32(TxControl, 0x1a00 | CmdReset | CmdTxEnb);

	dirty_tx = tp->dirty_tx;
	if ((tp->cur_tx - NUM_TX_DESC) == dirty_tx) {
		netif_stop_queue(dev);
		smp_rmb();
		if (dirty_tx != tp->dirty_tx)
			netif_wake_queue(dev);
	}
out:
	return NETDEV_TX_OK;
}

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
static void sis190_free_phy(struct list_head *first_phy)
{
	struct sis190_phy *cur, *next;

	list_for_each_entry_safe(cur, next, first_phy, list) {
		kfree(cur);
	}
}

/**
 *	sis190_default_phy - Select default PHY for sis190 mac.
 *	@dev: the net device to probe for
 *
 *	Select first detected PHY with link as default.
 *	If no one is link on, select PHY whose types is HOME as default.
 *	If HOME doesn't exist, select LAN.
 */
static u16 sis190_default_phy(struct net_device *dev)
{
	struct sis190_phy *phy, *phy_home, *phy_default, *phy_lan;
	struct sis190_private *tp = netdev_priv(dev);
	struct mii_if_info *mii_if = &tp->mii_if;
	void __iomem *ioaddr = tp->mmio_addr;
	u16 status;

	phy_home = phy_default = phy_lan = NULL;

	list_for_each_entry(phy, &tp->first_phy, list) {
		status = mdio_read_latched(ioaddr, phy->phy_id, MII_BMSR);

		// Link ON & Not select default PHY & not ghost PHY.
		if ((status & BMSR_LSTATUS) &&
		    !phy_default &&
		    (phy->type != UNKNOWN)) {
			phy_default = phy;
		} else {
			status = mdio_read(ioaddr, phy->phy_id, MII_BMCR);
			mdio_write(ioaddr, phy->phy_id, MII_BMCR,
				   status | BMCR_ANENABLE | BMCR_ISOLATE);
			if (phy->type == HOME)
				phy_home = phy;
			else if (phy->type == LAN)
				phy_lan = phy;
		}
	}

	if (!phy_default) {
		if (phy_home)
			phy_default = phy_home;
		else if (phy_lan)
			phy_default = phy_lan;
		else
1301
			phy_default = list_first_entry(&tp->first_phy,
1302 1303 1304 1305 1306
						 struct sis190_phy, list);
	}

	if (mii_if->phy_id != phy_default->phy_id) {
		mii_if->phy_id = phy_default->phy_id;
1307 1308 1309
		if (netif_msg_probe(tp))
			pr_info("%s: Using transceiver at address %d as default\n",
				pci_name(tp->pci_dev), mii_if->phy_id);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	}

	status = mdio_read(ioaddr, mii_if->phy_id, MII_BMCR);
	status &= (~BMCR_ISOLATE);

	mdio_write(ioaddr, mii_if->phy_id, MII_BMCR, status);
	status = mdio_read_latched(ioaddr, mii_if->phy_id, MII_BMSR);

	return status;
}

static void sis190_init_phy(struct net_device *dev, struct sis190_private *tp,
			    struct sis190_phy *phy, unsigned int phy_id,
			    u16 mii_status)
{
	void __iomem *ioaddr = tp->mmio_addr;
	struct mii_chip_info *p;

	INIT_LIST_HEAD(&phy->list);
	phy->status = mii_status;
	phy->phy_id = phy_id;

	phy->id[0] = mdio_read(ioaddr, phy_id, MII_PHYSID1);
	phy->id[1] = mdio_read(ioaddr, phy_id, MII_PHYSID2);

	for (p = mii_chip_table; p->type; p++) {
		if ((p->id[0] == phy->id[0]) &&
		    (p->id[1] == (phy->id[1] & 0xfff0))) {
			break;
		}
	}

	if (p->id[1]) {
		phy->type = (p->type == MIX) ?
			((mii_status & (BMSR_100FULL | BMSR_100HALF)) ?
				LAN : HOME) : p->type;
1346
		tp->features |= p->feature;
1347 1348 1349
		if (netif_msg_probe(tp))
			pr_info("%s: %s transceiver at address %d\n",
				pci_name(tp->pci_dev), p->name, phy_id);
1350
	} else {
1351
		phy->type = UNKNOWN;
1352 1353 1354 1355
		if (netif_msg_probe(tp))
			pr_info("%s: unknown PHY 0x%x:0x%x transceiver at address %d\n",
				pci_name(tp->pci_dev),
				phy->id[0], (phy->id[1] & 0xfff0), phy_id);
1356
	}
1357 1358
}

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
static void sis190_mii_probe_88e1111_fixup(struct sis190_private *tp)
{
	if (tp->features & F_PHY_88E1111) {
		void __iomem *ioaddr = tp->mmio_addr;
		int phy_id = tp->mii_if.phy_id;
		u16 reg[2][2] = {
			{ 0x808b, 0x0ce1 },
			{ 0x808f, 0x0c60 }
		}, *p;

		p = (tp->features & F_HAS_RGMII) ? reg[0] : reg[1];

		mdio_write(ioaddr, phy_id, 0x1b, p[0]);
		udelay(200);
		mdio_write(ioaddr, phy_id, 0x14, p[1]);
		udelay(200);
	}
}

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
/**
 *	sis190_mii_probe - Probe MII PHY for sis190
 *	@dev: the net device to probe for
 *
 *	Search for total of 32 possible mii phy addresses.
 *	Identify and set current phy if found one,
 *	return error if it failed to found.
 */
static int __devinit sis190_mii_probe(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	struct mii_if_info *mii_if = &tp->mii_if;
	void __iomem *ioaddr = tp->mmio_addr;
	int phy_id;
	int rc = 0;

	INIT_LIST_HEAD(&tp->first_phy);

	for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
		struct sis190_phy *phy;
		u16 status;

		status = mdio_read_latched(ioaddr, phy_id, MII_BMSR);

		// Try next mii if the current one is not accessible.
		if (status == 0xffff || status == 0x0000)
			continue;

		phy = kmalloc(sizeof(*phy), GFP_KERNEL);
		if (!phy) {
			sis190_free_phy(&tp->first_phy);
			rc = -ENOMEM;
			goto out;
		}

		sis190_init_phy(dev, tp, phy, phy_id, status);

		list_add(&tp->first_phy, &phy->list);
	}

	if (list_empty(&tp->first_phy)) {
1419 1420 1421
		if (netif_msg_probe(tp))
			pr_info("%s: No MII transceivers found!\n",
				pci_name(tp->pci_dev));
1422 1423 1424 1425 1426 1427 1428
		rc = -EIO;
		goto out;
	}

	/* Select default PHY for mac */
	sis190_default_phy(dev);

1429 1430
	sis190_mii_probe_88e1111_fixup(tp);

1431 1432 1433 1434 1435 1436 1437 1438 1439
	mii_if->dev = dev;
	mii_if->mdio_read = __mdio_read;
	mii_if->mdio_write = __mdio_write;
	mii_if->phy_id_mask = PHY_ID_ANY;
	mii_if->reg_num_mask = MII_REG_ANY;
out:
	return rc;
}

A
Adrian Bunk 已提交
1440
static void sis190_mii_remove(struct net_device *dev)
1441 1442 1443 1444 1445 1446
{
	struct sis190_private *tp = netdev_priv(dev);

	sis190_free_phy(&tp->first_phy);
}

F
Francois Romieu 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
static void sis190_release_board(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct sis190_private *tp = netdev_priv(dev);

	iounmap(tp->mmio_addr);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	free_netdev(dev);
}

static struct net_device * __devinit sis190_init_board(struct pci_dev *pdev)
{
	struct sis190_private *tp;
	struct net_device *dev;
	void __iomem *ioaddr;
	int rc;

	dev = alloc_etherdev(sizeof(*tp));
	if (!dev) {
1467 1468
		if (netif_msg_drv(&debug))
			pr_err("unable to alloc new ethernet\n");
F
Francois Romieu 已提交
1469 1470 1471 1472 1473 1474 1475
		rc = -ENOMEM;
		goto err_out_0;
	}

	SET_NETDEV_DEV(dev, &pdev->dev);

	tp = netdev_priv(dev);
D
David Howells 已提交
1476
	tp->dev = dev;
F
Francois Romieu 已提交
1477 1478 1479 1480
	tp->msg_enable = netif_msg_init(debug.msg_enable, SIS190_MSG_DEFAULT);

	rc = pci_enable_device(pdev);
	if (rc < 0) {
1481 1482
		if (netif_msg_probe(tp))
			pr_err("%s: enable failure\n", pci_name(pdev));
F
Francois Romieu 已提交
1483 1484 1485 1486 1487 1488
		goto err_free_dev_1;
	}

	rc = -ENODEV;

	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1489 1490 1491
		if (netif_msg_probe(tp))
			pr_err("%s: region #0 is no MMIO resource\n",
			       pci_name(pdev));
F
Francois Romieu 已提交
1492 1493 1494
		goto err_pci_disable_2;
	}
	if (pci_resource_len(pdev, 0) < SIS190_REGS_SIZE) {
1495 1496 1497
		if (netif_msg_probe(tp))
			pr_err("%s: invalid PCI region size(s)\n",
			       pci_name(pdev));
F
Francois Romieu 已提交
1498 1499 1500 1501 1502
		goto err_pci_disable_2;
	}

	rc = pci_request_regions(pdev, DRV_NAME);
	if (rc < 0) {
1503 1504 1505
		if (netif_msg_probe(tp))
			pr_err("%s: could not request regions\n",
			       pci_name(pdev));
F
Francois Romieu 已提交
1506 1507 1508
		goto err_pci_disable_2;
	}

1509
	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
F
Francois Romieu 已提交
1510
	if (rc < 0) {
1511 1512 1513
		if (netif_msg_probe(tp))
			pr_err("%s: DMA configuration failed\n",
			       pci_name(pdev));
F
Francois Romieu 已提交
1514 1515 1516 1517 1518 1519 1520
		goto err_free_res_3;
	}

	pci_set_master(pdev);

	ioaddr = ioremap(pci_resource_start(pdev, 0), SIS190_REGS_SIZE);
	if (!ioaddr) {
1521 1522 1523
		if (netif_msg_probe(tp))
			pr_err("%s: cannot remap MMIO, aborting\n",
			       pci_name(pdev));
F
Francois Romieu 已提交
1524 1525 1526 1527 1528 1529
		rc = -EIO;
		goto err_free_res_3;
	}

	tp->pci_dev = pdev;
	tp->mmio_addr = ioaddr;
1530
	tp->link_status = LNK_OFF;
F
Francois Romieu 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

	sis190_irq_mask_and_ack(ioaddr);

	sis190_soft_reset(ioaddr);
out:
	return dev;

err_free_res_3:
	pci_release_regions(pdev);
err_pci_disable_2:
	pci_disable_device(pdev);
err_free_dev_1:
	free_netdev(dev);
err_out_0:
	dev = ERR_PTR(rc);
	goto out;
}

static void sis190_tx_timeout(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	u8 tmp8;

	/* Disable Tx, if not already */
	tmp8 = SIS_R8(TxControl);
	if (tmp8 & CmdTxEnb)
		SIS_W8(TxControl, tmp8 & ~CmdTxEnb);

1560 1561
	netif_info(tp, tx_err, dev, "Transmit timeout, status %08x %08x\n",
		   SIS_R32(TxControl), SIS_R32(TxSts));
1562

F
Francois Romieu 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
	/* Disable interrupts by clearing the interrupt mask. */
	SIS_W32(IntrMask, 0x0000);

	/* Stop a shared interrupt from scavenging while we are. */
	spin_lock_irq(&tp->lock);
	sis190_tx_clear(tp);
	spin_unlock_irq(&tp->lock);

	/* ...and finally, reset everything. */
	sis190_hw_start(dev);

	netif_wake_queue(dev);
}

1577 1578 1579 1580 1581
static void sis190_set_rgmii(struct sis190_private *tp, u8 reg)
{
	tp->features |= (reg & 0x80) ? F_HAS_RGMII : 0;
}

1582 1583 1584 1585 1586 1587 1588 1589
static int __devinit sis190_get_mac_addr_from_eeprom(struct pci_dev *pdev,
						     struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	u16 sig;
	int i;

1590 1591
	if (netif_msg_probe(tp))
		pr_info("%s: Read MAC address from EEPROM\n", pci_name(pdev));
1592 1593 1594 1595 1596

	/* Check to see if there is a sane EEPROM */
	sig = (u16) sis190_read_eeprom(ioaddr, EEPROMSignature);

	if ((sig == 0xffff) || (sig == 0x0000)) {
1597 1598 1599
		if (netif_msg_probe(tp))
			pr_info("%s: Error EEPROM read %x\n",
				pci_name(pdev), sig);
1600 1601 1602 1603 1604
		return -EIO;
	}

	/* Get MAC address from EEPROM */
	for (i = 0; i < MAC_ADDR_LEN / 2; i++) {
A
Al Viro 已提交
1605
		u16 w = sis190_read_eeprom(ioaddr, EEPROMMACAddr + i);
1606

A
Al Viro 已提交
1607
		((__le16 *)dev->dev_addr)[i] = cpu_to_le16(w);
1608 1609
	}

1610 1611
	sis190_set_rgmii(tp, sis190_read_eeprom(ioaddr, EEPROMInfo));

1612 1613 1614 1615
	return 0;
}

/**
1616
 *	sis190_get_mac_addr_from_apc - Get MAC address for SiS96x model
1617 1618 1619
 *	@pdev: PCI device
 *	@dev:  network device to get address for
 *
1620
 *	SiS96x model, use APC CMOS RAM to store MAC address.
1621 1622 1623 1624 1625 1626
 *	APC CMOS RAM is accessed through ISA bridge.
 *	MAC address is read into @net_dev->dev_addr.
 */
static int __devinit sis190_get_mac_addr_from_apc(struct pci_dev *pdev,
						  struct net_device *dev)
{
1627
	static const u16 __devinitdata ids[] = { 0x0965, 0x0966, 0x0968 };
1628 1629 1630
	struct sis190_private *tp = netdev_priv(dev);
	struct pci_dev *isa_bridge;
	u8 reg, tmp8;
1631
	unsigned int i;
1632

1633 1634
	if (netif_msg_probe(tp))
		pr_info("%s: Read MAC address from APC\n", pci_name(pdev));
1635

1636 1637 1638 1639 1640
	for (i = 0; i < ARRAY_SIZE(ids); i++) {
		isa_bridge = pci_get_device(PCI_VENDOR_ID_SI, ids[i], NULL);
		if (isa_bridge)
			break;
	}
1641

1642
	if (!isa_bridge) {
1643 1644 1645
		if (netif_msg_probe(tp))
			pr_info("%s: Can not find ISA bridge\n",
				pci_name(pdev));
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
		return -EIO;
	}

	/* Enable port 78h & 79h to access APC Registers. */
	pci_read_config_byte(isa_bridge, 0x48, &tmp8);
	reg = (tmp8 & ~0x02);
	pci_write_config_byte(isa_bridge, 0x48, reg);
	udelay(50);
	pci_read_config_byte(isa_bridge, 0x48, &reg);

        for (i = 0; i < MAC_ADDR_LEN; i++) {
                outb(0x9 + i, 0x78);
                dev->dev_addr[i] = inb(0x79);
        }

	outb(0x12, 0x78);
	reg = inb(0x79);

1664 1665
	sis190_set_rgmii(tp, reg);

1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
	/* Restore the value to ISA Bridge */
	pci_write_config_byte(isa_bridge, 0x48, tmp8);
	pci_dev_put(isa_bridge);

	return 0;
}

/**
 *      sis190_init_rxfilter - Initialize the Rx filter
 *      @dev: network device to initialize
 *
 *      Set receive filter address to our MAC address
 *      and enable packet filtering.
 */
static inline void sis190_init_rxfilter(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	u16 ctl;
	int i;

	ctl = SIS_R16(RxMacControl);
	/*
	 * Disable packet filtering before setting filter.
	 * Note: SiS's driver writes 32 bits but RxMacControl is 16 bits
	 * only and followed by RxMacAddr (6 bytes). Strange. -- FR
	 */
	SIS_W16(RxMacControl, ctl & ~0x0f00);

	for (i = 0; i < MAC_ADDR_LEN; i++)
		SIS_W8(RxMacAddr + i, dev->dev_addr[i]);

	SIS_W16(RxMacControl, ctl);
	SIS_PCI_COMMIT();
}

1702
static int __devinit sis190_get_mac_addr(struct pci_dev *pdev,
1703
					 struct net_device *dev)
1704
{
1705 1706 1707 1708 1709
	int rc;

	rc = sis190_get_mac_addr_from_eeprom(pdev, dev);
	if (rc < 0) {
		u8 reg;
1710

1711
		pci_read_config_byte(pdev, 0x73, &reg);
1712

1713 1714 1715 1716
		if (reg & 0x00000001)
			rc = sis190_get_mac_addr_from_apc(pdev, dev);
	}
	return rc;
1717 1718
}

F
Francois Romieu 已提交
1719 1720 1721 1722
static void sis190_set_speed_auto(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
1723
	int phy_id = tp->mii_if.phy_id;
F
Francois Romieu 已提交
1724 1725
	int val;

1726
	netif_info(tp, link, dev, "Enabling Auto-negotiation\n");
F
Francois Romieu 已提交
1727

1728
	val = mdio_read(ioaddr, phy_id, MII_ADVERTISE);
F
Francois Romieu 已提交
1729 1730 1731

	// Enable 10/100 Full/Half Mode, leave MII_ADVERTISE bit4:0
	// unchanged.
1732
	mdio_write(ioaddr, phy_id, MII_ADVERTISE, (val & ADVERTISE_SLCT) |
F
Francois Romieu 已提交
1733 1734 1735 1736
		   ADVERTISE_100FULL | ADVERTISE_10FULL |
		   ADVERTISE_100HALF | ADVERTISE_10HALF);

	// Enable 1000 Full Mode.
1737
	mdio_write(ioaddr, phy_id, MII_CTRL1000, ADVERTISE_1000FULL);
F
Francois Romieu 已提交
1738 1739

	// Enable auto-negotiation and restart auto-negotiation.
1740
	mdio_write(ioaddr, phy_id, MII_BMCR,
F
Francois Romieu 已提交
1741 1742 1743
		   BMCR_ANENABLE | BMCR_ANRESTART | BMCR_RESET);
}

1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
static int sis190_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct sis190_private *tp = netdev_priv(dev);

	return mii_ethtool_gset(&tp->mii_if, cmd);
}

static int sis190_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct sis190_private *tp = netdev_priv(dev);

	return mii_ethtool_sset(&tp->mii_if, cmd);
}

F
Francois Romieu 已提交
1758 1759 1760 1761 1762
static void sis190_get_drvinfo(struct net_device *dev,
			       struct ethtool_drvinfo *info)
{
	struct sis190_private *tp = netdev_priv(dev);

1763 1764 1765 1766
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
	strlcpy(info->bus_info, pci_name(tp->pci_dev),
		sizeof(info->bus_info));
F
Francois Romieu 已提交
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
}

static int sis190_get_regs_len(struct net_device *dev)
{
	return SIS190_REGS_SIZE;
}

static void sis190_get_regs(struct net_device *dev, struct ethtool_regs *regs,
			    void *p)
{
	struct sis190_private *tp = netdev_priv(dev);
	unsigned long flags;

	if (regs->len > SIS190_REGS_SIZE)
		regs->len = SIS190_REGS_SIZE;

	spin_lock_irqsave(&tp->lock, flags);
	memcpy_fromio(p, tp->mmio_addr, regs->len);
	spin_unlock_irqrestore(&tp->lock, flags);
}

1788 1789 1790 1791 1792 1793 1794
static int sis190_nway_reset(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);

	return mii_nway_restart(&tp->mii_if);
}

F
Francois Romieu 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
static u32 sis190_get_msglevel(struct net_device *dev)
{
	struct sis190_private *tp = netdev_priv(dev);

	return tp->msg_enable;
}

static void sis190_set_msglevel(struct net_device *dev, u32 value)
{
	struct sis190_private *tp = netdev_priv(dev);

	tp->msg_enable = value;
}

1809
static const struct ethtool_ops sis190_ethtool_ops = {
1810 1811
	.get_settings	= sis190_get_settings,
	.set_settings	= sis190_set_settings,
F
Francois Romieu 已提交
1812 1813 1814 1815 1816 1817
	.get_drvinfo	= sis190_get_drvinfo,
	.get_regs_len	= sis190_get_regs_len,
	.get_regs	= sis190_get_regs,
	.get_link	= ethtool_op_get_link,
	.get_msglevel	= sis190_get_msglevel,
	.set_msglevel	= sis190_set_msglevel,
1818
	.nway_reset	= sis190_nway_reset,
F
Francois Romieu 已提交
1819 1820
};

1821 1822 1823 1824 1825 1826 1827 1828
static int sis190_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	struct sis190_private *tp = netdev_priv(dev);

	return !netif_running(dev) ? -EINVAL :
		generic_mii_ioctl(&tp->mii_if, if_mii(ifr), cmd, NULL);
}

1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
static int sis190_mac_addr(struct net_device  *dev, void *p)
{
	int rc;

	rc = eth_mac_addr(dev, p);
	if (!rc)
		sis190_init_rxfilter(dev);
	return rc;
}

1839 1840 1841 1842 1843 1844
static const struct net_device_ops sis190_netdev_ops = {
	.ndo_open		= sis190_open,
	.ndo_stop		= sis190_close,
	.ndo_do_ioctl		= sis190_ioctl,
	.ndo_start_xmit		= sis190_start_xmit,
	.ndo_tx_timeout		= sis190_tx_timeout,
1845
	.ndo_set_rx_mode	= sis190_set_rx_mode,
1846
	.ndo_change_mtu		= eth_change_mtu,
1847
	.ndo_set_mac_address	= sis190_mac_addr,
1848 1849 1850 1851 1852 1853
	.ndo_validate_addr	= eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	 = sis190_netpoll,
#endif
};

F
Francois Romieu 已提交
1854 1855 1856 1857 1858 1859 1860
static int __devinit sis190_init_one(struct pci_dev *pdev,
				     const struct pci_device_id *ent)
{
	static int printed_version = 0;
	struct sis190_private *tp;
	struct net_device *dev;
	void __iomem *ioaddr;
1861
	int rc;
F
Francois Romieu 已提交
1862 1863

	if (!printed_version) {
1864 1865
		if (netif_msg_drv(&debug))
			pr_info(SIS190_DRIVER_NAME " loaded\n");
F
Francois Romieu 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874
		printed_version = 1;
	}

	dev = sis190_init_board(pdev);
	if (IS_ERR(dev)) {
		rc = PTR_ERR(dev);
		goto out;
	}

1875 1876
	pci_set_drvdata(pdev, dev);

F
Francois Romieu 已提交
1877 1878 1879
	tp = netdev_priv(dev);
	ioaddr = tp->mmio_addr;

1880 1881 1882
	rc = sis190_get_mac_addr(pdev, dev);
	if (rc < 0)
		goto err_release_board;
F
Francois Romieu 已提交
1883

1884
	sis190_init_rxfilter(dev);
F
Francois Romieu 已提交
1885

D
David Howells 已提交
1886
	INIT_WORK(&tp->phy_task, sis190_phy_task);
F
Francois Romieu 已提交
1887

1888 1889
	dev->netdev_ops = &sis190_netdev_ops;

F
Francois Romieu 已提交
1890 1891 1892
	SET_ETHTOOL_OPS(dev, &sis190_ethtool_ops);
	dev->irq = pdev->irq;
	dev->base_addr = (unsigned long) 0xdead;
1893
	dev->watchdog_timeo = SIS190_TX_TIMEOUT;
F
Francois Romieu 已提交
1894 1895

	spin_lock_init(&tp->lock);
1896 1897

	rc = sis190_mii_probe(dev);
1898 1899
	if (rc < 0)
		goto err_release_board;
F
Francois Romieu 已提交
1900

1901
	rc = register_netdev(dev);
1902
	if (rc < 0)
1903 1904
		goto err_remove_mii;

1905 1906 1907 1908 1909 1910 1911 1912
	if (netif_msg_probe(tp)) {
		netdev_info(dev, "%s: %s at %p (IRQ: %d), %pM\n",
			    pci_name(pdev),
			    sis_chip_info[ent->driver_data].name,
			    ioaddr, dev->irq, dev->dev_addr);
		netdev_info(dev, "%s mode.\n",
			    (tp->features & F_HAS_RGMII) ? "RGMII" : "GMII");
	}
1913

F
Francois Romieu 已提交
1914 1915 1916 1917 1918
	netif_carrier_off(dev);

	sis190_set_speed_auto(dev);
out:
	return rc;
1919

1920 1921
err_remove_mii:
	sis190_mii_remove(dev);
1922 1923 1924
err_release_board:
	sis190_release_board(pdev);
	goto out;
F
Francois Romieu 已提交
1925 1926 1927 1928 1929
}

static void __devexit sis190_remove_one(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
1930
	struct sis190_private *tp = netdev_priv(dev);
F
Francois Romieu 已提交
1931

1932
	sis190_mii_remove(dev);
1933
	cancel_work_sync(&tp->phy_task);
F
Francois Romieu 已提交
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
	unregister_netdev(dev);
	sis190_release_board(pdev);
	pci_set_drvdata(pdev, NULL);
}

static struct pci_driver sis190_pci_driver = {
	.name		= DRV_NAME,
	.id_table	= sis190_pci_tbl,
	.probe		= sis190_init_one,
	.remove		= __devexit_p(sis190_remove_one),
};

static int __init sis190_init_module(void)
{
1948
	return pci_register_driver(&sis190_pci_driver);
F
Francois Romieu 已提交
1949 1950 1951 1952 1953 1954 1955 1956 1957
}

static void __exit sis190_cleanup_module(void)
{
	pci_unregister_driver(&sis190_pci_driver);
}

module_init(sis190_init_module);
module_exit(sis190_cleanup_module);