smsc95xx.c 55.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
 /***************************************************************************
 *
 * Copyright (C) 2007-2008 SMSC
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26
 *
 *****************************************************************************/

#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/usb.h>
27 28
#include <linux/bitrev.h>
#include <linux/crc16.h>
29 30
#include <linux/crc32.h>
#include <linux/usb/usbnet.h>
31
#include <linux/slab.h>
32
#include <linux/of_net.h>
33 34 35
#include "smsc95xx.h"

#define SMSC_CHIPNAME			"smsc95xx"
36
#define SMSC_DRIVER_VERSION		"1.0.6"
37 38 39 40 41 42 43 44
#define HS_USB_PKT_SIZE			(512)
#define FS_USB_PKT_SIZE			(64)
#define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
#define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
#define DEFAULT_BULK_IN_DELAY		(0x00002000)
#define MAX_SINGLE_PACKET_SIZE		(2048)
#define LAN95XX_EEPROM_MAGIC		(0x9500)
#define EEPROM_MAC_OFFSET		(0x01)
45
#define DEFAULT_TX_CSUM_ENABLE		(true)
46 47 48
#define DEFAULT_RX_CSUM_ENABLE		(true)
#define SMSC95XX_INTERNAL_PHY_ID	(1)
#define SMSC95XX_TX_OVERHEAD		(8)
49
#define SMSC95XX_TX_OVERHEAD_CSUM	(12)
50
#define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
51
					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
52

53 54
#define FEATURE_8_WAKEUP_FILTERS	(0x01)
#define FEATURE_PHY_NLP_CROSSOVER	(0x02)
55
#define FEATURE_REMOTE_WAKEUP		(0x04)
56

57 58 59 60 61 62 63
#define SUSPEND_SUSPEND0		(0x01)
#define SUSPEND_SUSPEND1		(0x02)
#define SUSPEND_SUSPEND2		(0x04)
#define SUSPEND_SUSPEND3		(0x08)
#define SUSPEND_ALLMODES		(SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
					 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)

64 65
#define CARRIER_CHECK_DELAY (2 * HZ)

66
struct smsc95xx_priv {
67
	u32 chip_id;
68
	u32 mac_cr;
69 70
	u32 hash_hi;
	u32 hash_lo;
71
	u32 wolopts;
72
	spinlock_t mac_cr_lock;
73
	u8 features;
74
	u8 suspend_flags;
75
	u8 mdix_ctrl;
76 77 78
	bool link_ok;
	struct delayed_work carrier_check;
	struct usbnet *dev;
79 80
};

81
static bool turbo_mode = true;
82 83 84
module_param(turbo_mode, bool, 0644);
MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");

85 86
static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
					    u32 *data, int in_pm)
87
{
88
	u32 buf;
89
	int ret;
90
	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
91 92 93

	BUG_ON(!dev);

94 95 96 97 98 99 100 101
	if (!in_pm)
		fn = usbnet_read_cmd;
	else
		fn = usbnet_read_cmd_nopm;

	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		 0, index, &buf, 4);
102
	if (unlikely(ret < 0)) {
J
Joe Perches 已提交
103 104
		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
			    index, ret);
105 106
		return ret;
	}
107

108 109
	le32_to_cpus(&buf);
	*data = buf;
110 111 112 113

	return ret;
}

114 115
static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
					     u32 data, int in_pm)
116
{
117
	u32 buf;
118
	int ret;
119
	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
120 121 122

	BUG_ON(!dev);

123 124 125 126 127
	if (!in_pm)
		fn = usbnet_write_cmd;
	else
		fn = usbnet_write_cmd_nopm;

128 129
	buf = data;
	cpu_to_le32s(&buf);
130

131 132 133
	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		 0, index, &buf, 4);
134
	if (unlikely(ret < 0))
J
Joe Perches 已提交
135 136
		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
			    index, ret);
137 138 139 140

	return ret;
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
					       u32 *data)
{
	return __smsc95xx_read_reg(dev, index, data, 1);
}

static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
						u32 data)
{
	return __smsc95xx_write_reg(dev, index, data, 1);
}

static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
					  u32 *data)
{
	return __smsc95xx_read_reg(dev, index, data, 0);
}

static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
					   u32 data)
{
	return __smsc95xx_write_reg(dev, index, data, 0);
}
164

165 166
/* Loop until the read is completed with timeout
 * called with phy_mutex held */
167 168
static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
						     int in_pm)
169 170 171
{
	unsigned long start_time = jiffies;
	u32 val;
172
	int ret;
173 174

	do {
175
		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
S
Steve Glendinning 已提交
176 177 178 179 180
		if (ret < 0) {
			netdev_warn(dev->net, "Error reading MII_ACCESS\n");
			return ret;
		}

181 182 183 184 185 186 187
		if (!(val & MII_BUSY_))
			return 0;
	} while (!time_after(jiffies, start_time + HZ));

	return -EIO;
}

188 189
static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
				int in_pm)
190 191 192
{
	struct usbnet *dev = netdev_priv(netdev);
	u32 val, addr;
193
	int ret;
194 195 196 197

	mutex_lock(&dev->phy_mutex);

	/* confirm MII not busy */
198
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
S
Steve Glendinning 已提交
199 200 201 202
	if (ret < 0) {
		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
		goto done;
	}
203 204 205 206

	/* set the address, index & direction (read from PHY) */
	phy_id &= dev->mii.phy_id_mask;
	idx &= dev->mii.reg_num_mask;
207
	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
208
	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
S
Steve Glendinning 已提交
209 210 211 212
	if (ret < 0) {
		netdev_warn(dev->net, "Error writing MII_ADDR\n");
		goto done;
	}
213

214
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
S
Steve Glendinning 已提交
215 216 217 218
	if (ret < 0) {
		netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
		goto done;
	}
219

220
	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
S
Steve Glendinning 已提交
221 222 223 224
	if (ret < 0) {
		netdev_warn(dev->net, "Error reading MII_DATA\n");
		goto done;
	}
225

226
	ret = (u16)(val & 0xFFFF);
227

228 229 230
done:
	mutex_unlock(&dev->phy_mutex);
	return ret;
231 232
}

233 234
static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
				  int idx, int regval, int in_pm)
235 236 237
{
	struct usbnet *dev = netdev_priv(netdev);
	u32 val, addr;
238
	int ret;
239 240 241 242

	mutex_lock(&dev->phy_mutex);

	/* confirm MII not busy */
243
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
S
Steve Glendinning 已提交
244 245 246 247
	if (ret < 0) {
		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
		goto done;
	}
248 249

	val = regval;
250
	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
S
Steve Glendinning 已提交
251 252 253 254
	if (ret < 0) {
		netdev_warn(dev->net, "Error writing MII_DATA\n");
		goto done;
	}
255 256 257 258

	/* set the address, index & direction (write to PHY) */
	phy_id &= dev->mii.phy_id_mask;
	idx &= dev->mii.reg_num_mask;
259
	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
260
	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
S
Steve Glendinning 已提交
261 262 263 264
	if (ret < 0) {
		netdev_warn(dev->net, "Error writing MII_ADDR\n");
		goto done;
	}
265

266
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
S
Steve Glendinning 已提交
267 268 269 270
	if (ret < 0) {
		netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
		goto done;
	}
271

272
done:
273 274 275
	mutex_unlock(&dev->phy_mutex);
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
				   int idx)
{
	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
}

static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
				     int idx, int regval)
{
	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
}

static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
{
	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
}

static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
				int regval)
{
	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
}

299
static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
300 301 302
{
	unsigned long start_time = jiffies;
	u32 val;
303
	int ret;
304 305

	do {
306
		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
S
Steve Glendinning 已提交
307 308 309 310 311
		if (ret < 0) {
			netdev_warn(dev->net, "Error reading E2P_CMD\n");
			return ret;
		}

312 313 314 315 316 317
		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
			break;
		udelay(40);
	} while (!time_after(jiffies, start_time + HZ));

	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
318
		netdev_warn(dev->net, "EEPROM read operation timeout\n");
319 320 321 322 323 324
		return -EIO;
	}

	return 0;
}

325
static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
326 327 328
{
	unsigned long start_time = jiffies;
	u32 val;
329
	int ret;
330 331

	do {
332
		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
S
Steve Glendinning 已提交
333 334 335 336
		if (ret < 0) {
			netdev_warn(dev->net, "Error reading E2P_CMD\n");
			return ret;
		}
337 338 339 340 341 342 343

		if (!(val & E2P_CMD_BUSY_))
			return 0;

		udelay(40);
	} while (!time_after(jiffies, start_time + HZ));

344
	netdev_warn(dev->net, "EEPROM is busy\n");
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	return -EIO;
}

static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
				u8 *data)
{
	u32 val;
	int i, ret;

	BUG_ON(!dev);
	BUG_ON(!data);

	ret = smsc95xx_eeprom_confirm_not_busy(dev);
	if (ret)
		return ret;

	for (i = 0; i < length; i++) {
		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
363
		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
S
Steve Glendinning 已提交
364 365 366 367
		if (ret < 0) {
			netdev_warn(dev->net, "Error writing E2P_CMD\n");
			return ret;
		}
368 369 370 371 372

		ret = smsc95xx_wait_eeprom(dev);
		if (ret < 0)
			return ret;

373
		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
S
Steve Glendinning 已提交
374 375 376 377
		if (ret < 0) {
			netdev_warn(dev->net, "Error reading E2P_DATA\n");
			return ret;
		}
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

		data[i] = val & 0xFF;
		offset++;
	}

	return 0;
}

static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
				 u8 *data)
{
	u32 val;
	int i, ret;

	BUG_ON(!dev);
	BUG_ON(!data);

	ret = smsc95xx_eeprom_confirm_not_busy(dev);
	if (ret)
		return ret;

	/* Issue write/erase enable command */
	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
401
	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
S
Steve Glendinning 已提交
402 403 404 405
	if (ret < 0) {
		netdev_warn(dev->net, "Error writing E2P_DATA\n");
		return ret;
	}
406 407 408 409 410 411 412 413 414

	ret = smsc95xx_wait_eeprom(dev);
	if (ret < 0)
		return ret;

	for (i = 0; i < length; i++) {

		/* Fill data register */
		val = data[i];
415
		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
S
Steve Glendinning 已提交
416 417 418 419
		if (ret < 0) {
			netdev_warn(dev->net, "Error writing E2P_DATA\n");
			return ret;
		}
420 421 422

		/* Send "write" command */
		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
423
		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
S
Steve Glendinning 已提交
424 425 426 427
		if (ret < 0) {
			netdev_warn(dev->net, "Error writing E2P_CMD\n");
			return ret;
		}
428 429 430 431 432 433 434 435 436 437 438

		ret = smsc95xx_wait_eeprom(dev);
		if (ret < 0)
			return ret;

		offset++;
	}

	return 0;
}

439
static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
440
						 u32 data)
441
{
442
	const u16 size = 4;
443
	u32 buf;
444
	int ret;
445

446 447 448
	buf = data;
	cpu_to_le32s(&buf);

449 450 451
	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
				     USB_DIR_OUT | USB_TYPE_VENDOR |
				     USB_RECIP_DEVICE,
452
				     0, index, &buf, size);
453 454 455 456
	if (ret < 0)
		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
			    ret);
	return ret;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
}

/* returns hash bit number for given MAC address
 * example:
 * 01 00 5E 00 00 01 -> returns bit number 31 */
static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
{
	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
}

static void smsc95xx_set_multicast(struct net_device *netdev)
{
	struct usbnet *dev = netdev_priv(netdev);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
472
	int ret;
473

474 475 476
	pdata->hash_hi = 0;
	pdata->hash_lo = 0;

477 478 479
	spin_lock_irqsave(&pdata->mac_cr_lock, flags);

	if (dev->net->flags & IFF_PROMISC) {
480
		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
481 482 483
		pdata->mac_cr |= MAC_CR_PRMS_;
		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
	} else if (dev->net->flags & IFF_ALLMULTI) {
484
		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
485 486
		pdata->mac_cr |= MAC_CR_MCPAS_;
		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
487
	} else if (!netdev_mc_empty(dev->net)) {
488
		struct netdev_hw_addr *ha;
489 490 491 492

		pdata->mac_cr |= MAC_CR_HPFILT_;
		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);

493 494
		netdev_for_each_mc_addr(ha, netdev) {
			u32 bitnum = smsc95xx_hash(ha->addr);
495 496
			u32 mask = 0x01 << (bitnum & 0x1F);
			if (bitnum & 0x20)
497
				pdata->hash_hi |= mask;
498
			else
499
				pdata->hash_lo |= mask;
500 501
		}

502
		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
503
				   pdata->hash_hi, pdata->hash_lo);
504
	} else {
505
		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
506 507 508 509 510 511 512
		pdata->mac_cr &=
			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
	}

	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

	/* Initiate async writes, as we can't wait for completion here */
513
	ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
S
Steve Glendinning 已提交
514 515
	if (ret < 0)
		netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
516

517
	ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
S
Steve Glendinning 已提交
518 519
	if (ret < 0)
		netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
520

521
	ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
S
Steve Glendinning 已提交
522 523
	if (ret < 0)
		netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
524 525
}

526 527
static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
					   u16 lcladv, u16 rmtadv)
528 529 530 531
{
	u32 flow, afc_cfg = 0;

	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
532
	if (ret < 0)
S
Steve Glendinning 已提交
533
		return ret;
534 535

	if (duplex == DUPLEX_FULL) {
536
		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
537 538 539 540 541 542 543 544 545 546 547

		if (cap & FLOW_CTRL_RX)
			flow = 0xFFFF0002;
		else
			flow = 0;

		if (cap & FLOW_CTRL_TX)
			afc_cfg |= 0xF;
		else
			afc_cfg &= ~0xF;

548
		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
549 550
				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
551
	} else {
552
		netif_dbg(dev, link, dev->net, "half duplex\n");
553 554 555 556
		flow = 0;
		afc_cfg |= 0xF;
	}

557
	ret = smsc95xx_write_reg(dev, FLOW, flow);
S
Steve Glendinning 已提交
558
	if (ret < 0)
559
		return ret;
560

561
	return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
562 563 564 565 566 567
}

static int smsc95xx_link_reset(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	struct mii_if_info *mii = &dev->mii;
568
	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
569 570
	unsigned long flags;
	u16 lcladv, rmtadv;
571
	int ret;
572 573

	/* clear interrupt status */
574
	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
575
	if (ret < 0)
S
Steve Glendinning 已提交
576
		return ret;
577 578

	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
579
	if (ret < 0)
S
Steve Glendinning 已提交
580
		return ret;
581 582 583 584 585 586

	mii_check_media(mii, 1, 1);
	mii_ethtool_gset(&dev->mii, &ecmd);
	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);

587 588 589
	netif_dbg(dev, link, dev->net,
		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
590 591 592 593 594 595 596 597 598 599 600

	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
	if (ecmd.duplex != DUPLEX_FULL) {
		pdata->mac_cr &= ~MAC_CR_FDPX_;
		pdata->mac_cr |= MAC_CR_RCVOWN_;
	} else {
		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
		pdata->mac_cr |= MAC_CR_FDPX_;
	}
	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

601
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
602
	if (ret < 0)
S
Steve Glendinning 已提交
603
		return ret;
604

605
	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
S
Steve Glendinning 已提交
606 607
	if (ret < 0)
		netdev_warn(dev->net, "Error updating PHY flow control\n");
608

S
Steve Glendinning 已提交
609
	return ret;
610 611 612 613 614 615 616
}

static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
{
	u32 intdata;

	if (urb->actual_length != 4) {
617 618
		netdev_warn(dev->net, "unexpected urb length %d\n",
			    urb->actual_length);
619 620 621 622
		return;
	}

	memcpy(&intdata, urb->transfer_buffer, 4);
623
	le32_to_cpus(&intdata);
624

625
	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
626 627 628 629

	if (intdata & INT_ENP_PHY_INT_)
		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
	else
630 631
		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
			    intdata);
632 633
}

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
static void set_carrier(struct usbnet *dev, bool link)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);

	if (pdata->link_ok == link)
		return;

	pdata->link_ok = link;

	if (link)
		usbnet_link_change(dev, 1, 0);
	else
		usbnet_link_change(dev, 0, 0);
}

static void check_carrier(struct work_struct *work)
{
	struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
						carrier_check.work);
	struct usbnet *dev = pdata->dev;
	int ret;

	if (pdata->suspend_flags != 0)
		return;

	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
	if (ret < 0) {
		netdev_warn(dev->net, "Failed to read MII_BMSR\n");
		return;
	}
	if (ret & BMSR_LSTATUS)
		set_carrier(dev, 1);
	else
		set_carrier(dev, 0);

	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
}

672
/* Enable or disable Tx & Rx checksum offload engines */
673 674
static int smsc95xx_set_features(struct net_device *netdev,
	netdev_features_t features)
675
{
676
	struct usbnet *dev = netdev_priv(netdev);
677
	u32 read_buf;
678 679 680
	int ret;

	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
681
	if (ret < 0)
S
Steve Glendinning 已提交
682
		return ret;
683

684
	if (features & NETIF_F_IP_CSUM)
685 686 687 688
		read_buf |= Tx_COE_EN_;
	else
		read_buf &= ~Tx_COE_EN_;

689
	if (features & NETIF_F_RXCSUM)
690 691 692 693 694
		read_buf |= Rx_COE_EN_;
	else
		read_buf &= ~Rx_COE_EN_;

	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
695
	if (ret < 0)
S
Steve Glendinning 已提交
696
		return ret;
697

698
	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	return 0;
}

static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
{
	return MAX_EEPROM_SIZE;
}

static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
				       struct ethtool_eeprom *ee, u8 *data)
{
	struct usbnet *dev = netdev_priv(netdev);

	ee->magic = LAN95XX_EEPROM_MAGIC;

	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
}

static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
				       struct ethtool_eeprom *ee, u8 *data)
{
	struct usbnet *dev = netdev_priv(netdev);

	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
723 724
		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
			    ee->magic);
725 726 727 728 729 730
		return -EINVAL;
	}

	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
}

731 732 733
static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
{
	/* all smsc95xx registers */
734
	return COE_CR - ID_REV + sizeof(u32);
735 736 737 738 739 740 741
}

static void
smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
			 void *buf)
{
	struct usbnet *dev = netdev_priv(netdev);
742 743
	unsigned int i, j;
	int retval;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	u32 *data = buf;

	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
	if (retval < 0) {
		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
		return;
	}

	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
		retval = smsc95xx_read_reg(dev, i, &data[j]);
		if (retval < 0) {
			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
			return;
		}
	}
}

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
static void smsc95xx_ethtool_get_wol(struct net_device *net,
				     struct ethtool_wolinfo *wolinfo)
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);

	wolinfo->supported = SUPPORTED_WAKE;
	wolinfo->wolopts = pdata->wolopts;
}

static int smsc95xx_ethtool_set_wol(struct net_device *net,
				    struct ethtool_wolinfo *wolinfo)
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
776
	int ret;
777 778

	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
779 780

	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
S
Steve Glendinning 已提交
781 782
	if (ret < 0)
		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
783

S
Steve Glendinning 已提交
784
	return ret;
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 847 848 849 850 851 852 853 854 855
static int get_mdix_status(struct net_device *net)
{
	struct usbnet *dev = netdev_priv(net);
	u32 val;
	int buf;

	buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
	if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
		if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
			return ETH_TP_MDI_AUTO;
		else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
			return ETH_TP_MDI_X;
	} else {
		buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
		if (val & STRAP_STATUS_AMDIX_EN_)
			return ETH_TP_MDI_AUTO;
	}

	return ETH_TP_MDI;
}

static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	int buf;

	if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
	    (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
	    (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
	    (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
		/* Extend Manual AutoMDIX timer for 9500A/9500Ai */
		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
					 PHY_EDPD_CONFIG);
		buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
				    PHY_EDPD_CONFIG, buf);
	}

	if (mdix_ctrl == ETH_TP_MDI) {
		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
					 SPECIAL_CTRL_STS);
		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
			 SPECIAL_CTRL_STS_AMDIX_STATE_);
		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
				    SPECIAL_CTRL_STS, buf);
	} else if (mdix_ctrl == ETH_TP_MDI_X) {
		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
					 SPECIAL_CTRL_STS);
		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
			 SPECIAL_CTRL_STS_AMDIX_STATE_);
		buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
				    SPECIAL_CTRL_STS, buf);
	} else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
					 SPECIAL_CTRL_STS);
		buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
			 SPECIAL_CTRL_STS_AMDIX_STATE_);
		buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
				    SPECIAL_CTRL_STS, buf);
	}
	pdata->mdix_ctrl = mdix_ctrl;
}

856 857
static int smsc95xx_get_link_ksettings(struct net_device *net,
				       struct ethtool_link_ksettings *cmd)
858 859 860 861 862
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	int retval;

863
	retval = usbnet_get_link_ksettings(net, cmd);
864

865 866
	cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
	cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
867 868 869 870

	return retval;
}

871 872
static int smsc95xx_set_link_ksettings(struct net_device *net,
				       const struct ethtool_link_ksettings *cmd)
873 874 875 876 877
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	int retval;

878 879
	if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
		set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
880

881
	retval = usbnet_set_link_ksettings(net, cmd);
882 883 884 885

	return retval;
}

886
static const struct ethtool_ops smsc95xx_ethtool_ops = {
887 888 889 890 891 892 893 894
	.get_link	= usbnet_get_link,
	.nway_reset	= usbnet_nway_reset,
	.get_drvinfo	= usbnet_get_drvinfo,
	.get_msglevel	= usbnet_get_msglevel,
	.set_msglevel	= usbnet_set_msglevel,
	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
895 896
	.get_regs_len	= smsc95xx_ethtool_getregslen,
	.get_regs	= smsc95xx_ethtool_getregs,
897 898
	.get_wol	= smsc95xx_ethtool_get_wol,
	.set_wol	= smsc95xx_ethtool_set_wol,
899 900
	.get_link_ksettings	= smsc95xx_get_link_ksettings,
	.set_link_ksettings	= smsc95xx_set_link_ksettings,
901 902 903 904 905 906 907 908 909 910 911 912 913 914
};

static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
{
	struct usbnet *dev = netdev_priv(netdev);

	if (!netif_running(netdev))
		return -EINVAL;

	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
}

static void smsc95xx_init_mac_address(struct usbnet *dev)
{
915 916 917 918 919 920 921 922 923
	const u8 *mac_addr;

	/* maybe the boot loader passed the MAC address in devicetree */
	mac_addr = of_get_mac_address(dev->udev->dev.of_node);
	if (mac_addr) {
		memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
		return;
	}

924 925 926 927 928
	/* try reading mac address from EEPROM */
	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
			dev->net->dev_addr) == 0) {
		if (is_valid_ether_addr(dev->net->dev_addr)) {
			/* eeprom values are valid so use them */
929
			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
930 931 932 933
			return;
		}
	}

934
	/* no useful static MAC address found. generate a random one */
935
	eth_hw_addr_random(dev->net);
J
Joe Perches 已提交
936
	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
937 938 939 940 941 942 943 944 945 946
}

static int smsc95xx_set_mac_address(struct usbnet *dev)
{
	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
	int ret;

	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
S
Steve Glendinning 已提交
947
	if (ret < 0)
948
		return ret;
949

950
	return smsc95xx_write_reg(dev, ADDRH, addr_hi);
951 952 953
}

/* starts the TX path */
954
static int smsc95xx_start_tx_path(struct usbnet *dev)
955 956 957
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
958
	int ret;
959 960 961 962 963 964

	/* Enable Tx at MAC */
	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
	pdata->mac_cr |= MAC_CR_TXEN_;
	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

965
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
966
	if (ret < 0)
S
Steve Glendinning 已提交
967
		return ret;
968 969

	/* Enable Tx at SCSRs */
970
	return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
971 972 973
}

/* Starts the Receive path */
974
static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
975 976 977 978 979 980 981 982
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;

	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
	pdata->mac_cr |= MAC_CR_RXEN_;
	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

983
	return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
984 985 986 987
}

static int smsc95xx_phy_initialize(struct usbnet *dev)
{
988
	int bmcr, ret, timeout = 0;
989

990 991 992 993 994 995 996 997
	/* Initialize MII structure */
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = smsc95xx_mdio_read;
	dev->mii.mdio_write = smsc95xx_mdio_write;
	dev->mii.phy_id_mask = 0x1f;
	dev->mii.reg_num_mask = 0x1f;
	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;

998
	/* reset phy and wait for reset to complete */
999
	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1000 1001 1002 1003 1004

	do {
		msleep(10);
		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
		timeout++;
R
Rabin Vincent 已提交
1005
	} while ((bmcr & BMCR_RESET) && (timeout < 100));
1006 1007 1008 1009 1010 1011

	if (timeout >= 100) {
		netdev_warn(dev->net, "timeout on PHY Reset");
		return -EIO;
	}

1012 1013 1014 1015 1016
	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
		ADVERTISE_PAUSE_ASYM);

	/* read to clear */
1017
	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
S
Steve Glendinning 已提交
1018 1019 1020 1021
	if (ret < 0) {
		netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
		return ret;
	}
1022 1023 1024 1025 1026

	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
		PHY_INT_MASK_DEFAULT_);
	mii_nway_restart(&dev->mii);

1027
	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
1028 1029 1030 1031 1032 1033 1034 1035 1036
	return 0;
}

static int smsc95xx_reset(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	u32 read_buf, write_buf, burst_cap;
	int ret = 0, timeout;

1037
	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
1038

1039
	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
1040
	if (ret < 0)
S
Steve Glendinning 已提交
1041
		return ret;
1042 1043 1044

	timeout = 0;
	do {
1045
		msleep(10);
1046
		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1047
		if (ret < 0)
S
Steve Glendinning 已提交
1048
			return ret;
1049 1050 1051 1052
		timeout++;
	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));

	if (timeout >= 100) {
1053
		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
1054 1055 1056
		return ret;
	}

1057
	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
1058
	if (ret < 0)
S
Steve Glendinning 已提交
1059
		return ret;
1060 1061 1062

	timeout = 0;
	do {
1063
		msleep(10);
1064
		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
1065
		if (ret < 0)
S
Steve Glendinning 已提交
1066
			return ret;
1067 1068 1069 1070
		timeout++;
	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));

	if (timeout >= 100) {
1071
		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1072 1073 1074 1075 1076 1077 1078
		return ret;
	}

	ret = smsc95xx_set_mac_address(dev);
	if (ret < 0)
		return ret;

J
Joe Perches 已提交
1079 1080
	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
		  dev->net->dev_addr);
1081 1082

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1083
	if (ret < 0)
S
Steve Glendinning 已提交
1084
		return ret;
1085

J
Joe Perches 已提交
1086 1087
	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
		  read_buf);
1088 1089 1090 1091

	read_buf |= HW_CFG_BIR_;

	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1092
	if (ret < 0)
S
Steve Glendinning 已提交
1093
		return ret;
1094 1095

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1096
	if (ret < 0)
S
Steve Glendinning 已提交
1097 1098
		return ret;

1099 1100 1101
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
		  read_buf);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113

	if (!turbo_mode) {
		burst_cap = 0;
		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
	} else if (dev->udev->speed == USB_SPEED_HIGH) {
		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
	} else {
		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
	}

J
Joe Perches 已提交
1114 1115
	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
		  (ulong)dev->rx_urb_size);
1116 1117

	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1118
	if (ret < 0)
S
Steve Glendinning 已提交
1119
		return ret;
1120 1121

	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1122
	if (ret < 0)
S
Steve Glendinning 已提交
1123
		return ret;
1124

1125 1126 1127
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from BURST_CAP after writing: 0x%08x\n",
		  read_buf);
1128

1129
	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1130
	if (ret < 0)
S
Steve Glendinning 已提交
1131
		return ret;
1132 1133

	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1134
	if (ret < 0)
S
Steve Glendinning 已提交
1135
		return ret;
1136

1137 1138 1139
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
		  read_buf);
1140 1141

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1142
	if (ret < 0)
S
Steve Glendinning 已提交
1143
		return ret;
1144

J
Joe Perches 已提交
1145 1146
	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
		  read_buf);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

	if (turbo_mode)
		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);

	read_buf &= ~HW_CFG_RXDOFF_;

	/* set Rx data offset=2, Make IP header aligns on word boundary. */
	read_buf |= NET_IP_ALIGN << 9;

	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1157
	if (ret < 0)
S
Steve Glendinning 已提交
1158
		return ret;
1159 1160

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1161
	if (ret < 0)
S
Steve Glendinning 已提交
1162
		return ret;
1163

1164 1165
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
1166

1167
	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1168
	if (ret < 0)
S
Steve Glendinning 已提交
1169
		return ret;
1170 1171

	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1172
	if (ret < 0)
S
Steve Glendinning 已提交
1173
		return ret;
1174
	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
1175

1176 1177 1178 1179
	/* Configure GPIO pins as LED outputs */
	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
		LED_GPIO_CFG_FDX_LED;
	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1180
	if (ret < 0)
S
Steve Glendinning 已提交
1181
		return ret;
1182

1183
	/* Init Tx */
1184
	ret = smsc95xx_write_reg(dev, FLOW, 0);
1185
	if (ret < 0)
S
Steve Glendinning 已提交
1186
		return ret;
1187

1188
	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1189
	if (ret < 0)
S
Steve Glendinning 已提交
1190
		return ret;
1191 1192 1193

	/* Don't need mac_cr_lock during initialisation */
	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1194
	if (ret < 0)
S
Steve Glendinning 已提交
1195
		return ret;
1196 1197 1198

	/* Init Rx */
	/* Set Vlan */
1199
	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1200
	if (ret < 0)
S
Steve Glendinning 已提交
1201
		return ret;
1202

1203
	/* Enable or disable checksum offload engines */
1204
	ret = smsc95xx_set_features(dev->net, dev->net->features);
S
Steve Glendinning 已提交
1205 1206 1207 1208
	if (ret < 0) {
		netdev_warn(dev->net, "Failed to set checksum offload features\n");
		return ret;
	}
1209 1210 1211

	smsc95xx_set_multicast(dev->net);

1212
	ret = smsc95xx_phy_initialize(dev);
S
Steve Glendinning 已提交
1213 1214 1215 1216
	if (ret < 0) {
		netdev_warn(dev->net, "Failed to init PHY\n");
		return ret;
	}
1217 1218

	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1219
	if (ret < 0)
S
Steve Glendinning 已提交
1220
		return ret;
1221 1222 1223 1224 1225

	/* enable PHY interrupts */
	read_buf |= INT_EP_CTL_PHY_INT_;

	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1226
	if (ret < 0)
S
Steve Glendinning 已提交
1227
		return ret;
1228

1229
	ret = smsc95xx_start_tx_path(dev);
S
Steve Glendinning 已提交
1230 1231 1232 1233
	if (ret < 0) {
		netdev_warn(dev->net, "Failed to start TX path\n");
		return ret;
	}
1234

1235
	ret = smsc95xx_start_rx_path(dev, 0);
S
Steve Glendinning 已提交
1236 1237 1238 1239
	if (ret < 0) {
		netdev_warn(dev->net, "Failed to start RX path\n");
		return ret;
	}
1240

1241
	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1242 1243 1244
	return 0;
}

1245 1246 1247 1248 1249 1250
static const struct net_device_ops smsc95xx_netdev_ops = {
	.ndo_open		= usbnet_open,
	.ndo_stop		= usbnet_stop,
	.ndo_start_xmit		= usbnet_start_xmit,
	.ndo_tx_timeout		= usbnet_tx_timeout,
	.ndo_change_mtu		= usbnet_change_mtu,
G
Greg Ungerer 已提交
1251
	.ndo_get_stats64	= usbnet_get_stats64,
1252 1253 1254
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_do_ioctl 		= smsc95xx_ioctl,
1255
	.ndo_set_rx_mode	= smsc95xx_set_multicast,
1256
	.ndo_set_features	= smsc95xx_set_features,
1257 1258
};

1259 1260 1261
static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
{
	struct smsc95xx_priv *pdata = NULL;
1262
	u32 val;
1263 1264 1265 1266 1267
	int ret;

	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");

	ret = usbnet_get_endpoints(dev, intf);
S
Steve Glendinning 已提交
1268 1269 1270 1271
	if (ret < 0) {
		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
		return ret;
	}
1272 1273

	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1274
					      GFP_KERNEL);
1275 1276

	pdata = (struct smsc95xx_priv *)(dev->data[0]);
1277
	if (!pdata)
1278 1279 1280 1281
		return -ENOMEM;

	spin_lock_init(&pdata->mac_cr_lock);

1282 1283 1284 1285 1286 1287 1288
	/* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
	 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
	 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
	 * it is transmitted as all ones. The zero transmitted checksum means
	 * transmitter generated no checksum. Hence, enable csum offload only
	 * for ipv4 packets.
	 */
1289
	if (DEFAULT_TX_CSUM_ENABLE)
1290
		dev->net->features |= NETIF_F_IP_CSUM;
1291 1292 1293
	if (DEFAULT_RX_CSUM_ENABLE)
		dev->net->features |= NETIF_F_RXCSUM;

1294
	dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1295

1296 1297
	smsc95xx_init_mac_address(dev);

1298 1299 1300
	/* Init all registers */
	ret = smsc95xx_reset(dev);

1301 1302
	/* detect device revision as different features may be available */
	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1303
	if (ret < 0)
S
Steve Glendinning 已提交
1304
		return ret;
1305
	val >>= 16;
1306 1307
	pdata->chip_id = val;
	pdata->mdix_ctrl = get_mdix_status(dev->net);
1308 1309 1310 1311 1312

	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
			FEATURE_PHY_NLP_CROSSOVER |
1313
			FEATURE_REMOTE_WAKEUP);
1314 1315
	else if (val == ID_REV_CHIP_ID_9512_)
		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1316

1317
	dev->net->netdev_ops = &smsc95xx_netdev_ops;
1318 1319
	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
	dev->net->flags |= IFF_MULTICAST;
1320
	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
S
Stephane Fillod 已提交
1321
	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1322 1323 1324 1325 1326

	pdata->dev = dev;
	INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);

1327 1328 1329 1330 1331 1332
	return 0;
}

static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1333

1334
	if (pdata) {
1335
		cancel_delayed_work(&pdata->carrier_check);
1336
		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1337 1338 1339 1340 1341 1342
		kfree(pdata);
		pdata = NULL;
		dev->data[0] = 0;
	}
}

1343
static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1344
{
1345 1346
	u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
	return crc << ((filter % 2) * 16);
1347 1348
}

1349 1350 1351 1352 1353
static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
{
	struct mii_if_info *mii = &dev->mii;
	int ret;

J
Joe Perches 已提交
1354
	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1355 1356 1357

	/* read to clear */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1358
	if (ret < 0)
S
Steve Glendinning 已提交
1359
		return ret;
1360 1361 1362

	/* enable interrupt source */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1363
	if (ret < 0)
S
Steve Glendinning 已提交
1364
		return ret;
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379

	ret |= mask;

	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);

	return 0;
}

static int smsc95xx_link_ok_nopm(struct usbnet *dev)
{
	struct mii_if_info *mii = &dev->mii;
	int ret;

	/* first, a dummy read, needed to latch some MII phys */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1380
	if (ret < 0)
S
Steve Glendinning 已提交
1381
		return ret;
1382 1383

	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1384
	if (ret < 0)
S
Steve Glendinning 已提交
1385
		return ret;
1386 1387 1388 1389

	return !!(ret & BMSR_LSTATUS);
}

1390 1391 1392 1393 1394 1395 1396
static int smsc95xx_enter_suspend0(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	u32 val;
	int ret;

	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1397
	if (ret < 0)
S
Steve Glendinning 已提交
1398
		return ret;
1399 1400 1401 1402 1403

	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
	val |= PM_CTL_SUS_MODE_0;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1404
	if (ret < 0)
S
Steve Glendinning 已提交
1405
		return ret;
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415

	/* clear wol status */
	val &= ~PM_CTL_WUPS_;
	val |= PM_CTL_WUPS_WOL_;

	/* enable energy detection */
	if (pdata->wolopts & WAKE_PHY)
		val |= PM_CTL_WUPS_ED_;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1416
	if (ret < 0)
S
Steve Glendinning 已提交
1417
		return ret;
1418 1419 1420

	/* read back PM_CTRL */
	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
M
Ming Lei 已提交
1421 1422
	if (ret < 0)
		return ret;
1423

1424 1425
	pdata->suspend_flags |= SUSPEND_SUSPEND0;

M
Ming Lei 已提交
1426
	return 0;
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
}

static int smsc95xx_enter_suspend1(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	struct mii_if_info *mii = &dev->mii;
	u32 val;
	int ret;

	/* reconfigure link pulse detection timing for
	 * compatibility with non-standard link partners
	 */
	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
			PHY_EDPD_CONFIG_DEFAULT);

	/* enable energy detect power-down mode */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1445
	if (ret < 0)
S
Steve Glendinning 已提交
1446
		return ret;
1447 1448 1449 1450 1451 1452 1453

	ret |= MODE_CTRL_STS_EDPWRDOWN_;

	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);

	/* enter SUSPEND1 mode */
	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1454
	if (ret < 0)
S
Steve Glendinning 已提交
1455
		return ret;
1456 1457 1458 1459 1460

	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
	val |= PM_CTL_SUS_MODE_1;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1461
	if (ret < 0)
S
Steve Glendinning 已提交
1462
		return ret;
1463 1464 1465 1466 1467 1468

	/* clear wol status, enable energy detection */
	val &= ~PM_CTL_WUPS_;
	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
M
Ming Lei 已提交
1469 1470
	if (ret < 0)
		return ret;
1471

1472 1473
	pdata->suspend_flags |= SUSPEND_SUSPEND1;

M
Ming Lei 已提交
1474
	return 0;
1475 1476 1477 1478
}

static int smsc95xx_enter_suspend2(struct usbnet *dev)
{
1479
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1480 1481 1482 1483
	u32 val;
	int ret;

	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1484
	if (ret < 0)
S
Steve Glendinning 已提交
1485
		return ret;
1486 1487 1488 1489 1490

	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
	val |= PM_CTL_SUS_MODE_2;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
M
Ming Lei 已提交
1491 1492
	if (ret < 0)
		return ret;
1493

1494 1495
	pdata->suspend_flags |= SUSPEND_SUSPEND2;

M
Ming Lei 已提交
1496
	return 0;
1497 1498
}

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
static int smsc95xx_enter_suspend3(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	u32 val;
	int ret;

	ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
	if (ret < 0)
		return ret;

1509
	if (val & RX_FIFO_INF_USED_) {
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
		netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
		return -EBUSY;
	}

	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
	if (ret < 0)
		return ret;

	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
	val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
	if (ret < 0)
		return ret;

	/* clear wol status */
	val &= ~PM_CTL_WUPS_;
	val |= PM_CTL_WUPS_WOL_;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
	if (ret < 0)
		return ret;

	pdata->suspend_flags |= SUSPEND_SUSPEND3;

	return 0;
}

static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	int ret;

	if (!netif_running(dev->net)) {
		/* interface is ifconfig down so fully power down hw */
		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
		return smsc95xx_enter_suspend2(dev);
	}

	if (!link_up) {
		/* link is down so enter EDPD mode, but only if device can
		 * reliably resume from it.  This check should be redundant
1552
		 * as current FEATURE_REMOTE_WAKEUP parts also support
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
		 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
		if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
			netdev_warn(dev->net, "EDPD not supported\n");
			return -EBUSY;
		}

		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");

		/* enable PHY wakeup events for if cable is attached */
		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
			PHY_INT_MASK_ANEG_COMP_);
		if (ret < 0) {
			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
			return ret;
		}

		netdev_info(dev->net, "entering SUSPEND1 mode\n");
		return smsc95xx_enter_suspend1(dev);
	}

	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
	ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
		PHY_INT_MASK_LINK_DOWN_);
	if (ret < 0) {
		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
		return ret;
	}

	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
	return smsc95xx_enter_suspend3(dev);
}

1585 1586 1587
static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct usbnet *dev = usb_get_intfdata(intf);
1588
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1589
	u32 val, link_up;
1590 1591 1592
	int ret;

	ret = usbnet_suspend(intf, message);
S
Steve Glendinning 已提交
1593 1594 1595 1596
	if (ret < 0) {
		netdev_warn(dev->net, "usbnet_suspend error\n");
		return ret;
	}
1597

1598 1599 1600 1601 1602
	if (pdata->suspend_flags) {
		netdev_warn(dev->net, "error during last resume\n");
		pdata->suspend_flags = 0;
	}

1603 1604 1605
	/* determine if link is up using only _nopm functions */
	link_up = smsc95xx_link_ok_nopm(dev);

1606
	if (message.event == PM_EVENT_AUTO_SUSPEND &&
1607
	    (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1608 1609 1610 1611 1612
		ret = smsc95xx_autosuspend(dev, link_up);
		goto done;
	}

	/* if we get this far we're not autosuspending */
1613 1614 1615 1616 1617
	/* if no wol options set, or if link is down and we're not waking on
	 * PHY activity, enter lowest power SUSPEND2 mode
	 */
	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
		!(link_up || (pdata->wolopts & WAKE_PHY))) {
J
Joe Perches 已提交
1618
		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1619 1620

		/* disable energy detect (link up) & wake up events */
1621
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1622
		if (ret < 0)
S
Steve Glendinning 已提交
1623
			goto done;
1624 1625 1626

		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);

1627
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1628
		if (ret < 0)
S
Steve Glendinning 已提交
1629
			goto done;
1630

1631
		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1632
		if (ret < 0)
S
Steve Glendinning 已提交
1633
			goto done;
1634 1635 1636

		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);

1637
		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1638
		if (ret < 0)
S
Steve Glendinning 已提交
1639
			goto done;
1640

1641 1642
		ret = smsc95xx_enter_suspend2(dev);
		goto done;
1643 1644
	}

1645 1646 1647
	if (pdata->wolopts & WAKE_PHY) {
		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
S
Steve Glendinning 已提交
1648 1649 1650 1651
		if (ret < 0) {
			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
			goto done;
		}
1652 1653 1654 1655 1656

		/* if link is down then configure EDPD and enter SUSPEND1,
		 * otherwise enter SUSPEND0 below
		 */
		if (!link_up) {
J
Joe Perches 已提交
1657
			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1658 1659
			ret = smsc95xx_enter_suspend1(dev);
			goto done;
1660 1661 1662
		}
	}

1663
	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1664
		u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
1665 1666 1667
		u32 command[2];
		u32 offset[2];
		u32 crc[4];
1668 1669 1670
		int wuff_filter_count =
			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1671 1672
		int i, filter = 0;

1673 1674
		if (!filter_mask) {
			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1675 1676
			ret = -ENOMEM;
			goto done;
1677 1678
		}

1679 1680 1681 1682
		memset(command, 0, sizeof(command));
		memset(offset, 0, sizeof(offset));
		memset(crc, 0, sizeof(crc));

1683 1684
		if (pdata->wolopts & WAKE_BCAST) {
			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
J
Joe Perches 已提交
1685
			netdev_info(dev->net, "enabling broadcast detection\n");
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
			filter_mask[filter * 4] = 0x003F;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(bcast, 6, filter);
			filter++;
		}

		if (pdata->wolopts & WAKE_MCAST) {
			const u8 mcast[] = {0x01, 0x00, 0x5E};
J
Joe Perches 已提交
1698
			netdev_info(dev->net, "enabling multicast detection\n");
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
			filter_mask[filter * 4] = 0x0007;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(mcast, 3, filter);
			filter++;
		}

		if (pdata->wolopts & WAKE_ARP) {
			const u8 arp[] = {0x08, 0x06};
J
Joe Perches 已提交
1711
			netdev_info(dev->net, "enabling ARP detection\n");
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
			filter_mask[filter * 4] = 0x0003;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(arp, 2, filter);
			filter++;
		}

		if (pdata->wolopts & WAKE_UCAST) {
J
Joe Perches 已提交
1723
			netdev_info(dev->net, "enabling unicast detection\n");
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
			filter_mask[filter * 4] = 0x003F;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
			filter++;
		}

1734
		for (i = 0; i < (wuff_filter_count * 4); i++) {
1735
			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
S
Steve Glendinning 已提交
1736
			if (ret < 0) {
1737
				kfree(filter_mask);
S
Steve Glendinning 已提交
1738 1739
				goto done;
			}
1740
		}
1741
		kfree(filter_mask);
1742

1743
		for (i = 0; i < (wuff_filter_count / 4); i++) {
1744
			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1745
			if (ret < 0)
S
Steve Glendinning 已提交
1746
				goto done;
1747 1748
		}

1749
		for (i = 0; i < (wuff_filter_count / 4); i++) {
1750
			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1751
			if (ret < 0)
S
Steve Glendinning 已提交
1752
				goto done;
1753 1754
		}

1755
		for (i = 0; i < (wuff_filter_count / 2); i++) {
1756
			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1757
			if (ret < 0)
S
Steve Glendinning 已提交
1758
				goto done;
1759 1760 1761
		}

		/* clear any pending pattern match packet status */
1762
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1763
		if (ret < 0)
S
Steve Glendinning 已提交
1764
			goto done;
1765 1766 1767

		val |= WUCSR_WUFR_;

1768
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1769
		if (ret < 0)
S
Steve Glendinning 已提交
1770
			goto done;
1771 1772
	}

1773 1774
	if (pdata->wolopts & WAKE_MAGIC) {
		/* clear any pending magic packet status */
1775
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1776
		if (ret < 0)
S
Steve Glendinning 已提交
1777
			goto done;
1778 1779 1780

		val |= WUCSR_MPR_;

1781
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1782
		if (ret < 0)
S
Steve Glendinning 已提交
1783
			goto done;
1784 1785
	}

1786
	/* enable/disable wakeup sources */
1787
	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1788
	if (ret < 0)
S
Steve Glendinning 已提交
1789
		goto done;
1790

1791
	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
J
Joe Perches 已提交
1792
		netdev_info(dev->net, "enabling pattern match wakeup\n");
1793 1794
		val |= WUCSR_WAKE_EN_;
	} else {
J
Joe Perches 已提交
1795
		netdev_info(dev->net, "disabling pattern match wakeup\n");
1796 1797 1798
		val &= ~WUCSR_WAKE_EN_;
	}

1799
	if (pdata->wolopts & WAKE_MAGIC) {
J
Joe Perches 已提交
1800
		netdev_info(dev->net, "enabling magic packet wakeup\n");
1801 1802
		val |= WUCSR_MPEN_;
	} else {
J
Joe Perches 已提交
1803
		netdev_info(dev->net, "disabling magic packet wakeup\n");
1804 1805 1806
		val &= ~WUCSR_MPEN_;
	}

1807
	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1808
	if (ret < 0)
S
Steve Glendinning 已提交
1809
		goto done;
1810 1811

	/* enable wol wakeup source */
1812
	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1813
	if (ret < 0)
S
Steve Glendinning 已提交
1814
		goto done;
1815 1816 1817

	val |= PM_CTL_WOL_EN_;

1818 1819 1820 1821
	/* phy energy detect wakeup source */
	if (pdata->wolopts & WAKE_PHY)
		val |= PM_CTL_ED_EN_;

1822
	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1823
	if (ret < 0)
S
Steve Glendinning 已提交
1824
		goto done;
1825

1826
	/* enable receiver to enable frame reception */
1827
	smsc95xx_start_rx_path(dev, 1);
1828 1829

	/* some wol options are enabled, so enter SUSPEND0 */
J
Joe Perches 已提交
1830
	netdev_info(dev->net, "entering SUSPEND0 mode\n");
1831 1832 1833
	ret = smsc95xx_enter_suspend0(dev);

done:
1834 1835 1836 1837 1838
	/*
	 * TODO: resume() might need to handle the suspend failure
	 * in system sleep
	 */
	if (ret && PMSG_IS_AUTO(message))
1839 1840
		usbnet_resume(intf);
	return ret;
1841 1842 1843 1844 1845
}

static int smsc95xx_resume(struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);
1846 1847
	struct smsc95xx_priv *pdata;
	u8 suspend_flags;
1848 1849 1850 1851
	int ret;
	u32 val;

	BUG_ON(!dev);
1852 1853
	pdata = (struct smsc95xx_priv *)(dev->data[0]);
	suspend_flags = pdata->suspend_flags;
1854

1855 1856 1857 1858
	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);

	/* do this first to ensure it's cleared even in error case */
	pdata->suspend_flags = 0;
1859
	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1860 1861

	if (suspend_flags & SUSPEND_ALLMODES) {
1862
		/* clear wake-up sources */
1863
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1864
		if (ret < 0)
S
Steve Glendinning 已提交
1865
			return ret;
1866

1867
		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1868

1869
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1870
		if (ret < 0)
S
Steve Glendinning 已提交
1871
			return ret;
1872 1873

		/* clear wake-up status */
1874
		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1875
		if (ret < 0)
S
Steve Glendinning 已提交
1876
			return ret;
1877 1878 1879 1880

		val &= ~PM_CTL_WOL_EN_;
		val |= PM_CTL_WUPS_;

1881
		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1882
		if (ret < 0)
S
Steve Glendinning 已提交
1883
			return ret;
1884 1885
	}

1886
	ret = usbnet_resume(intf);
S
Steve Glendinning 已提交
1887 1888
	if (ret < 0)
		netdev_warn(dev->net, "usbnet_resume error\n");
1889

S
Steve Glendinning 已提交
1890
	return ret;
1891 1892
}

1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
static int smsc95xx_reset_resume(struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);
	int ret;

	ret = smsc95xx_reset(dev);
	if (ret < 0)
		return ret;

	return smsc95xx_resume(intf);
}

1905 1906 1907 1908 1909 1910 1911 1912 1913
static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
{
	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
	skb->ip_summed = CHECKSUM_COMPLETE;
	skb_trim(skb, skb->len - 2);
}

static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
1914 1915 1916 1917
	/* This check is no longer done by usbnet */
	if (skb->len < dev->net->hard_header_len)
		return 0;

1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
	while (skb->len > 0) {
		u32 header, align_count;
		struct sk_buff *ax_skb;
		unsigned char *packet;
		u16 size;

		memcpy(&header, skb->data, sizeof(header));
		le32_to_cpus(&header);
		skb_pull(skb, 4 + NET_IP_ALIGN);
		packet = skb->data;

		/* get the packet length */
		size = (u16)((header & RX_STS_FL_) >> 16);
		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;

		if (unlikely(header & RX_STS_ES_)) {
1934 1935
			netif_dbg(dev, rx_err, dev->net,
				  "Error header=0x%08x\n", header);
1936 1937
			dev->net->stats.rx_errors++;
			dev->net->stats.rx_dropped++;
1938 1939

			if (header & RX_STS_CRC_) {
1940
				dev->net->stats.rx_crc_errors++;
1941 1942
			} else {
				if (header & (RX_STS_TL_ | RX_STS_RF_))
1943
					dev->net->stats.rx_frame_errors++;
1944 1945 1946

				if ((header & RX_STS_LE_) &&
					(!(header & RX_STS_FT_)))
1947
					dev->net->stats.rx_length_errors++;
1948 1949 1950 1951
			}
		} else {
			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1952 1953
				netif_dbg(dev, rx_err, dev->net,
					  "size err header=0x%08x\n", header);
1954 1955 1956 1957 1958
				return 0;
			}

			/* last frame in this batch */
			if (skb->len == size) {
1959
				if (dev->net->features & NETIF_F_RXCSUM)
1960
					smsc95xx_rx_csum_offload(skb);
1961
				skb_trim(skb, skb->len - 4); /* remove fcs */
1962 1963 1964 1965 1966 1967 1968
				skb->truesize = size + sizeof(struct sk_buff);

				return 1;
			}

			ax_skb = skb_clone(skb, GFP_ATOMIC);
			if (unlikely(!ax_skb)) {
1969
				netdev_warn(dev->net, "Error allocating skb\n");
1970 1971 1972 1973 1974 1975 1976
				return 0;
			}

			ax_skb->len = size;
			ax_skb->data = packet;
			skb_set_tail_pointer(ax_skb, size);

1977
			if (dev->net->features & NETIF_F_RXCSUM)
1978
				smsc95xx_rx_csum_offload(ax_skb);
1979
			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
			ax_skb->truesize = size + sizeof(struct sk_buff);

			usbnet_skb_return(dev, ax_skb);
		}

		skb_pull(skb, size);

		/* padding bytes before the next frame starts */
		if (skb->len)
			skb_pull(skb, align_count);
	}

	return 1;
}

1995 1996
static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
{
1997 1998
	u16 low_16 = (u16)skb_checksum_start_offset(skb);
	u16 high_16 = low_16 + skb->csum_offset;
1999 2000 2001
	return (high_16 << 16) | low_16;
}

2002 2003 2004
static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
					 struct sk_buff *skb, gfp_t flags)
{
2005
	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
2006
	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
2007 2008
	u32 tx_cmd_a, tx_cmd_b;

2009 2010 2011
	/* We do not advertise SG, so skbs should be already linearized */
	BUG_ON(skb_shinfo(skb)->nr_frags);

2012 2013 2014 2015 2016
	/* Make writable and expand header space by overhead if required */
	if (skb_cow_head(skb, overhead)) {
		/* Must deallocate here as returning NULL to indicate error
		 * means the skb won't be deallocated in the caller.
		 */
2017
		dev_kfree_skb_any(skb);
2018
		return NULL;
2019 2020
	}

2021
	if (csum) {
2022 2023 2024
		if (skb->len <= 45) {
			/* workaround - hardware tx checksum does not work
			 * properly with extremely small packets */
2025
			long csstart = skb_checksum_start_offset(skb);
2026 2027 2028 2029 2030 2031 2032 2033 2034
			__wsum calc = csum_partial(skb->data + csstart,
				skb->len - csstart, 0);
			*((__sum16 *)(skb->data + csstart
				+ skb->csum_offset)) = csum_fold(calc);

			csum = false;
		} else {
			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
			skb_push(skb, 4);
2035
			cpu_to_le32s(&csum_preamble);
2036 2037
			memcpy(skb->data, &csum_preamble, 4);
		}
2038 2039
	}

2040 2041
	skb_push(skb, 4);
	tx_cmd_b = (u32)(skb->len - 4);
2042 2043
	if (csum)
		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
	cpu_to_le32s(&tx_cmd_b);
	memcpy(skb->data, &tx_cmd_b, 4);

	skb_push(skb, 4);
	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
		TX_CMD_A_LAST_SEG_;
	cpu_to_le32s(&tx_cmd_a);
	memcpy(skb->data, &tx_cmd_a, 4);

	return skb;
}

2056 2057 2058 2059 2060 2061
static int smsc95xx_manage_power(struct usbnet *dev, int on)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);

	dev->intf->needs_remote_wakeup = on;

2062
	if (pdata->features & FEATURE_REMOTE_WAKEUP)
2063 2064
		return 0;

2065 2066
	/* this chip revision isn't capable of remote wakeup */
	netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
2067 2068 2069 2070 2071 2072 2073 2074 2075

	if (on)
		usb_autopm_get_interface_no_resume(dev->intf);
	else
		usb_autopm_put_interface(dev->intf);

	return 0;
}

2076 2077 2078 2079 2080 2081 2082 2083 2084
static const struct driver_info smsc95xx_info = {
	.description	= "smsc95xx USB 2.0 Ethernet",
	.bind		= smsc95xx_bind,
	.unbind		= smsc95xx_unbind,
	.link_reset	= smsc95xx_link_reset,
	.reset		= smsc95xx_reset,
	.rx_fixup	= smsc95xx_rx_fixup,
	.tx_fixup	= smsc95xx_tx_fixup,
	.status		= smsc95xx_status,
2085
	.manage_power	= smsc95xx_manage_power,
2086
	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2087 2088 2089 2090 2091 2092 2093 2094
};

static const struct usb_device_id products[] = {
	{
		/* SMSC9500 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9500),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
	{
		/* SMSC9505 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9505),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9E00),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505A USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9E01),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
2110 2111 2112 2113 2114
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device */
		USB_DEVICE(0x0424, 0xec00),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
	{
		/* SMSC9500 USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9900),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505 USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9901),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9902),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505A USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9903),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9904),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device (HAL) */
		USB_DEVICE(0x0424, 0x9905),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505A USB Ethernet Device (HAL) */
		USB_DEVICE(0x0424, 0x9906),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500 USB Ethernet Device (Alternate ID) */
		USB_DEVICE(0x0424, 0x9907),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device (Alternate ID) */
		USB_DEVICE(0x0424, 0x9908),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
		USB_DEVICE(0x0424, 0x9909),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
	{
		/* SMSC LAN9530 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9530),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC LAN9730 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9730),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC LAN89530 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9E08),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
2180 2181 2182 2183 2184 2185 2186 2187
	{ },		/* END */
};
MODULE_DEVICE_TABLE(usb, products);

static struct usb_driver smsc95xx_driver = {
	.name		= "smsc95xx",
	.id_table	= products,
	.probe		= usbnet_probe,
2188
	.suspend	= smsc95xx_suspend,
2189
	.resume		= smsc95xx_resume,
2190
	.reset_resume	= smsc95xx_reset_resume,
2191
	.disconnect	= usbnet_disconnect,
2192
	.disable_hub_initiated_lpm = 1,
2193
	.supports_autosuspend = 1,
2194 2195
};

2196
module_usb_driver(smsc95xx_driver);
2197 2198

MODULE_AUTHOR("Nancy Lin");
2199
MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2200 2201
MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
MODULE_LICENSE("GPL");