qlcnic_ethtool.c 37.6 KB
Newer Older
1
/*
S
Sritej Velaga 已提交
2 3
 * QLogic qlcnic NIC Driver
 * Copyright (c)  2009-2010 QLogic Corporation
4
 *
S
Sritej Velaga 已提交
5
 * See LICENSE.qlcnic for copyright and licensing details.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 */

#include <linux/types.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/io.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>

#include "qlcnic.h"

struct qlcnic_stats {
	char stat_string[ETH_GSTRING_LEN];
	int sizeof_stat;
	int stat_offset;
};

#define QLC_SIZEOF(m) FIELD_SIZEOF(struct qlcnic_adapter, m)
#define QLC_OFF(m) offsetof(struct qlcnic_adapter, m)

static const struct qlcnic_stats qlcnic_gstrings_stats[] = {
	{"xmit_called",
		QLC_SIZEOF(stats.xmitcalled), QLC_OFF(stats.xmitcalled)},
	{"xmit_finished",
		QLC_SIZEOF(stats.xmitfinished), QLC_OFF(stats.xmitfinished)},
	{"rx_dropped",
		QLC_SIZEOF(stats.rxdropped), QLC_OFF(stats.rxdropped)},
	{"tx_dropped",
		QLC_SIZEOF(stats.txdropped), QLC_OFF(stats.txdropped)},
	{"csummed",
		QLC_SIZEOF(stats.csummed), QLC_OFF(stats.csummed)},
	{"rx_pkts",
		QLC_SIZEOF(stats.rx_pkts), QLC_OFF(stats.rx_pkts)},
	{"lro_pkts",
		QLC_SIZEOF(stats.lro_pkts), QLC_OFF(stats.lro_pkts)},
	{"rx_bytes",
		QLC_SIZEOF(stats.rxbytes), QLC_OFF(stats.rxbytes)},
	{"tx_bytes",
		QLC_SIZEOF(stats.txbytes), QLC_OFF(stats.txbytes)},
45 46 47 48 49 50 51 52 53 54
	{"lrobytes",
		QLC_SIZEOF(stats.lrobytes), QLC_OFF(stats.lrobytes)},
	{"lso_frames",
		QLC_SIZEOF(stats.lso_frames), QLC_OFF(stats.lso_frames)},
	{"xmit_on",
		QLC_SIZEOF(stats.xmit_on), QLC_OFF(stats.xmit_on)},
	{"xmit_off",
		QLC_SIZEOF(stats.xmit_off), QLC_OFF(stats.xmit_off)},
	{"skb_alloc_failure", QLC_SIZEOF(stats.skb_alloc_failure),
		QLC_OFF(stats.skb_alloc_failure)},
55 56 57 58 59 60
	{"null rxbuf",
		QLC_SIZEOF(stats.null_rxbuf), QLC_OFF(stats.null_rxbuf)},
	{"rx dma map error", QLC_SIZEOF(stats.rx_dma_map_error),
					 QLC_OFF(stats.rx_dma_map_error)},
	{"tx dma map error", QLC_SIZEOF(stats.tx_dma_map_error),
					 QLC_OFF(stats.tx_dma_map_error)},
61

62 63
};

A
amit salecha 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static const char qlcnic_device_gstrings_stats[][ETH_GSTRING_LEN] = {
	"rx unicast frames",
	"rx multicast frames",
	"rx broadcast frames",
	"rx dropped frames",
	"rx errors",
	"rx local frames",
	"rx numbytes",
	"tx unicast frames",
	"tx multicast frames",
	"tx broadcast frames",
	"tx dropped frames",
	"tx errors",
	"tx local frames",
	"tx numbytes",
};

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static const char qlcnic_mac_stats_strings [][ETH_GSTRING_LEN] = {
	"mac_tx_frames",
	"mac_tx_bytes",
	"mac_tx_mcast_pkts",
	"mac_tx_bcast_pkts",
	"mac_tx_pause_cnt",
	"mac_tx_ctrl_pkt",
	"mac_tx_lt_64b_pkts",
	"mac_tx_lt_127b_pkts",
	"mac_tx_lt_255b_pkts",
	"mac_tx_lt_511b_pkts",
	"mac_tx_lt_1023b_pkts",
	"mac_tx_lt_1518b_pkts",
	"mac_tx_gt_1518b_pkts",
	"mac_rx_frames",
	"mac_rx_bytes",
	"mac_rx_mcast_pkts",
	"mac_rx_bcast_pkts",
	"mac_rx_pause_cnt",
	"mac_rx_ctrl_pkt",
	"mac_rx_lt_64b_pkts",
	"mac_rx_lt_127b_pkts",
	"mac_rx_lt_255b_pkts",
	"mac_rx_lt_511b_pkts",
	"mac_rx_lt_1023b_pkts",
	"mac_rx_lt_1518b_pkts",
	"mac_rx_gt_1518b_pkts",
	"mac_rx_length_error",
	"mac_rx_length_small",
	"mac_rx_length_large",
	"mac_rx_jabber",
	"mac_rx_dropped",
	"mac_rx_crc_error",
	"mac_align_error",
};

#define QLCNIC_STATS_LEN ARRAY_SIZE(qlcnic_gstrings_stats)
#define QLCNIC_MAC_STATS_LEN ARRAY_SIZE(qlcnic_mac_stats_strings)
A
amit salecha 已提交
119
#define QLCNIC_DEVICE_STATS_LEN	ARRAY_SIZE(qlcnic_device_gstrings_stats)
120
#define QLCNIC_TOTAL_STATS_LEN QLCNIC_STATS_LEN + QLCNIC_MAC_STATS_LEN
121 122 123

static const char qlcnic_gstrings_test[][ETH_GSTRING_LEN] = {
	"Register_Test_on_offline",
124
	"Link_Test_on_offline",
125
	"Interrupt_Test_offline",
126 127
	"Internal_Loopback_offline",
	"External_Loopback_offline"
128 129 130 131 132 133 134 135 136 137 138
};

#define QLCNIC_TEST_LEN	ARRAY_SIZE(qlcnic_gstrings_test)

#define QLCNIC_RING_REGS_COUNT	20
#define QLCNIC_RING_REGS_LEN	(QLCNIC_RING_REGS_COUNT * sizeof(u32))
#define QLCNIC_MAX_EEPROM_LEN   1024

static const u32 diag_registers[] = {
	CRB_CMDPEG_STATE,
	CRB_RCVPEG_STATE,
139
	CRB_XG_STATE_P3P,
140 141
	CRB_FW_CAPABILITIES_1,
	ISR_INT_STATE_REG,
A
Amit Kumar Salecha 已提交
142
	QLCNIC_CRB_DRV_ACTIVE,
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	QLCNIC_CRB_DEV_STATE,
	QLCNIC_CRB_DRV_STATE,
	QLCNIC_CRB_DRV_SCRATCH,
	QLCNIC_CRB_DEV_PARTITION_INFO,
	QLCNIC_CRB_DRV_IDC_VER,
	QLCNIC_PEG_ALIVE_COUNTER,
	QLCNIC_PEG_HALT_STATUS1,
	QLCNIC_PEG_HALT_STATUS2,
	QLCNIC_CRB_PEG_NET_0+0x3c,
	QLCNIC_CRB_PEG_NET_1+0x3c,
	QLCNIC_CRB_PEG_NET_2+0x3c,
	QLCNIC_CRB_PEG_NET_4+0x3c,
	-1
};

158 159 160
#define QLCNIC_MGMT_API_VERSION	2
#define QLCNIC_DEV_INFO_SIZE	1
#define QLCNIC_ETHTOOL_REGS_VER	2
161 162
static int qlcnic_get_regs_len(struct net_device *dev)
{
163 164
	return sizeof(diag_registers) + QLCNIC_RING_REGS_LEN +
				QLCNIC_DEV_INFO_SIZE + 1;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
}

static int qlcnic_get_eeprom_len(struct net_device *dev)
{
	return QLCNIC_FLASH_TOTAL_SIZE;
}

static void
qlcnic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	u32 fw_major, fw_minor, fw_build;

	fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
	fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
	fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
181 182 183 184 185 186 187 188
	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
		"%d.%d.%d", fw_major, fw_minor, fw_build);

	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
		sizeof(drvinfo->bus_info));
	strlcpy(drvinfo->driver, qlcnic_driver_name, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, QLCNIC_LINUX_VERSIONID,
		sizeof(drvinfo->version));
189 190 191 192 193 194 195 196 197
}

static int
qlcnic_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	int check_sfp_module = 0;

	/* read which mode */
A
Anirban Chakraborty 已提交
198
	if (adapter->ahw->port_type == QLCNIC_GBE) {
199 200 201 202 203 204 205 206 207 208 209 210
		ecmd->supported = (SUPPORTED_10baseT_Half |
				   SUPPORTED_10baseT_Full |
				   SUPPORTED_100baseT_Half |
				   SUPPORTED_100baseT_Full |
				   SUPPORTED_1000baseT_Half |
				   SUPPORTED_1000baseT_Full);

		ecmd->advertising = (ADVERTISED_100baseT_Half |
				     ADVERTISED_100baseT_Full |
				     ADVERTISED_1000baseT_Half |
				     ADVERTISED_1000baseT_Full);

211 212 213
		ethtool_cmd_speed_set(ecmd, adapter->ahw->link_speed);
		ecmd->duplex = adapter->ahw->link_duplex;
		ecmd->autoneg = adapter->ahw->link_autoneg;
214

A
Anirban Chakraborty 已提交
215
	} else if (adapter->ahw->port_type == QLCNIC_XGBE) {
216 217 218 219 220 221 222 223 224 225 226
		u32 val;

		val = QLCRD32(adapter, QLCNIC_PORT_MODE_ADDR);
		if (val == QLCNIC_PORT_MODE_802_3_AP) {
			ecmd->supported = SUPPORTED_1000baseT_Full;
			ecmd->advertising = ADVERTISED_1000baseT_Full;
		} else {
			ecmd->supported = SUPPORTED_10000baseT_Full;
			ecmd->advertising = ADVERTISED_10000baseT_Full;
		}

227 228 229 230
		if (netif_running(dev) && adapter->ahw->has_link_events) {
			ethtool_cmd_speed_set(ecmd, adapter->ahw->link_speed);
			ecmd->autoneg = adapter->ahw->link_autoneg;
			ecmd->duplex = adapter->ahw->link_duplex;
231 232 233
			goto skip;
		}

234 235
		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
		ecmd->duplex = DUPLEX_UNKNOWN;
236 237 238 239 240
		ecmd->autoneg = AUTONEG_DISABLE;
	} else
		return -EIO;

skip:
241
	ecmd->phy_address = adapter->ahw->physical_port;
242 243
	ecmd->transceiver = XCVR_EXTERNAL;

A
Anirban Chakraborty 已提交
244
	switch (adapter->ahw->board_type) {
245 246 247
	case QLCNIC_BRDTYPE_P3P_REF_QG:
	case QLCNIC_BRDTYPE_P3P_4_GB:
	case QLCNIC_BRDTYPE_P3P_4_GB_MM:
248 249 250

		ecmd->supported |= SUPPORTED_Autoneg;
		ecmd->advertising |= ADVERTISED_Autoneg;
251 252 253
	case QLCNIC_BRDTYPE_P3P_10G_CX4:
	case QLCNIC_BRDTYPE_P3P_10G_CX4_LP:
	case QLCNIC_BRDTYPE_P3P_10000_BASE_T:
254 255 256
		ecmd->supported |= SUPPORTED_TP;
		ecmd->advertising |= ADVERTISED_TP;
		ecmd->port = PORT_TP;
257
		ecmd->autoneg =  adapter->ahw->link_autoneg;
258
		break;
259 260 261
	case QLCNIC_BRDTYPE_P3P_IMEZ:
	case QLCNIC_BRDTYPE_P3P_XG_LOM:
	case QLCNIC_BRDTYPE_P3P_HMEZ:
262 263 264 265 266
		ecmd->supported |= SUPPORTED_MII;
		ecmd->advertising |= ADVERTISED_MII;
		ecmd->port = PORT_MII;
		ecmd->autoneg = AUTONEG_DISABLE;
		break;
267 268 269
	case QLCNIC_BRDTYPE_P3P_10G_SFP_PLUS:
	case QLCNIC_BRDTYPE_P3P_10G_SFP_CT:
	case QLCNIC_BRDTYPE_P3P_10G_SFP_QT:
270 271 272
		ecmd->advertising |= ADVERTISED_TP;
		ecmd->supported |= SUPPORTED_TP;
		check_sfp_module = netif_running(dev) &&
273
				   adapter->ahw->has_link_events;
274
	case QLCNIC_BRDTYPE_P3P_10G_XFP:
275 276 277 278 279
		ecmd->supported |= SUPPORTED_FIBRE;
		ecmd->advertising |= ADVERTISED_FIBRE;
		ecmd->port = PORT_FIBRE;
		ecmd->autoneg = AUTONEG_DISABLE;
		break;
280
	case QLCNIC_BRDTYPE_P3P_10G_TP:
A
Anirban Chakraborty 已提交
281
		if (adapter->ahw->port_type == QLCNIC_XGBE) {
282 283 284 285 286 287
			ecmd->autoneg = AUTONEG_DISABLE;
			ecmd->supported |= (SUPPORTED_FIBRE | SUPPORTED_TP);
			ecmd->advertising |=
				(ADVERTISED_FIBRE | ADVERTISED_TP);
			ecmd->port = PORT_FIBRE;
			check_sfp_module = netif_running(dev) &&
288
					   adapter->ahw->has_link_events;
289 290 291 292 293 294 295 296 297 298
		} else {
			ecmd->autoneg = AUTONEG_ENABLE;
			ecmd->supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
			ecmd->advertising |=
				(ADVERTISED_TP | ADVERTISED_Autoneg);
			ecmd->port = PORT_TP;
		}
		break;
	default:
		dev_err(&adapter->pdev->dev, "Unsupported board model %d\n",
A
Anirban Chakraborty 已提交
299
			adapter->ahw->board_type);
300 301 302 303
		return -EIO;
	}

	if (check_sfp_module) {
304
		switch (adapter->ahw->module_type) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
		case LINKEVENT_MODULE_OPTICAL_UNKNOWN:
		case LINKEVENT_MODULE_OPTICAL_SRLR:
		case LINKEVENT_MODULE_OPTICAL_LRM:
		case LINKEVENT_MODULE_OPTICAL_SFP_1G:
			ecmd->port = PORT_FIBRE;
			break;
		case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE:
		case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN:
		case LINKEVENT_MODULE_TWINAX:
			ecmd->port = PORT_TP;
			break;
		default:
			ecmd->port = PORT_OTHER;
		}
	}

	return 0;
}

static int
qlcnic_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
{
327 328
	u32 config = 0;
	u32 ret = 0;
329
	struct qlcnic_adapter *adapter = netdev_priv(dev);
330 331 332

	if (adapter->ahw->port_type != QLCNIC_GBE)
		return -EOPNOTSUPP;
333 334

	/* read which mode */
335 336
	if (ecmd->duplex)
		config |= 0x1;
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	if (ecmd->autoneg)
		config |= 0x2;

	switch (ethtool_cmd_speed(ecmd)) {
	case SPEED_10:
		config |= (0 << 8);
		break;
	case SPEED_100:
		config |= (1 << 8);
		break;
	case SPEED_1000:
		config |= (10 << 8);
		break;
	default:
		return -EIO;
	}

	ret = qlcnic_fw_cmd_set_port(adapter, config);

	if (ret == QLCNIC_RCODE_NOT_SUPPORTED)
358
		return -EOPNOTSUPP;
359 360 361
	else if (ret)
		return -EIO;

362 363 364
	adapter->ahw->link_speed = ethtool_cmd_speed(ecmd);
	adapter->ahw->link_duplex = ecmd->duplex;
	adapter->ahw->link_autoneg = ecmd->autoneg;
365 366 367 368 369 370 371 372 373 374 375 376

	if (!netif_running(dev))
		return 0;

	dev->netdev_ops->ndo_stop(dev);
	return dev->netdev_ops->ndo_open(dev);
}

static void
qlcnic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
A
Anirban Chakraborty 已提交
377
	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
378 379
	struct qlcnic_host_sds_ring *sds_ring;
	u32 *regs_buff = p;
A
Amit Kumar Salecha 已提交
380
	int ring, i = 0, j = 0;
381 382

	memset(p, 0, qlcnic_get_regs_len(dev));
383
	regs->version = (QLCNIC_ETHTOOL_REGS_VER << 24) |
A
Anirban Chakraborty 已提交
384
		(adapter->ahw->revision_id << 16) | (adapter->pdev)->device;
385

386 387 388
	regs_buff[0] = (0xcafe0000 | (QLCNIC_DEV_INFO_SIZE & 0xffff));
	regs_buff[1] = QLCNIC_MGMT_API_VERSION;

A
Amit Kumar Salecha 已提交
389 390
	for (i = QLCNIC_DEV_INFO_SIZE + 1; diag_registers[j] != -1; j++, i++)
		regs_buff[i] = QLCRD32(adapter, diag_registers[j]);
391

392
	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
393 394
		return;

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	regs_buff[i++] = 0xFFEFCDAB; /* Marker btw regs and ring count*/

	regs_buff[i++] = 1; /* No. of tx ring */
	regs_buff[i++] = le32_to_cpu(*(adapter->tx_ring->hw_consumer));
	regs_buff[i++] = readl(adapter->tx_ring->crb_cmd_producer);

	regs_buff[i++] = 2; /* No. of rx ring */
	regs_buff[i++] = readl(recv_ctx->rds_rings[0].crb_rcv_producer);
	regs_buff[i++] = readl(recv_ctx->rds_rings[1].crb_rcv_producer);

	regs_buff[i++] = adapter->max_sds_rings;

	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
		sds_ring = &(recv_ctx->sds_rings[ring]);
		regs_buff[i++] = readl(sds_ring->crb_sts_consumer);
	}
}

static u32 qlcnic_test_link(struct net_device *dev)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	u32 val;

418
	val = QLCRD32(adapter, CRB_XG_STATE_P3P);
A
Anirban Chakraborty 已提交
419
	val = XG_LINK_STATE_P3P(adapter->ahw->pci_func, val);
420
	return (val == XG_LINK_UP_P3P) ? 0 : 1;
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
}

static int
qlcnic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
		      u8 *bytes)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	int offset;
	int ret;

	if (eeprom->len == 0)
		return -EINVAL;

	eeprom->magic = (adapter->pdev)->vendor |
			((adapter->pdev)->device << 16);
	offset = eeprom->offset;

	ret = qlcnic_rom_fast_read_words(adapter, offset, bytes,
						eeprom->len);
	if (ret < 0)
		return ret;

	return 0;
}

static void
qlcnic_get_ringparam(struct net_device *dev,
		struct ethtool_ringparam *ring)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);

	ring->rx_pending = adapter->num_rxd;
	ring->rx_jumbo_pending = adapter->num_jumbo_rxd;
	ring->tx_pending = adapter->num_txd;

S
Sony Chacko 已提交
456 457
	ring->rx_max_pending = adapter->max_rxd;
	ring->rx_jumbo_max_pending = adapter->max_jumbo_rxd;
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	ring->tx_max_pending = MAX_CMD_DESCRIPTORS;
}

static u32
qlcnic_validate_ringparam(u32 val, u32 min, u32 max, char *r_name)
{
	u32 num_desc;
	num_desc = max(val, min);
	num_desc = min(num_desc, max);
	num_desc = roundup_pow_of_two(num_desc);

	if (val != num_desc) {
		printk(KERN_INFO "%s: setting %s ring size %d instead of %d\n",
		       qlcnic_driver_name, r_name, num_desc, val);
	}

	return num_desc;
}

static int
qlcnic_set_ringparam(struct net_device *dev,
		struct ethtool_ringparam *ring)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	u16 num_rxd, num_jumbo_rxd, num_txd;

	if (ring->rx_mini_pending)
		return -EOPNOTSUPP;

	num_rxd = qlcnic_validate_ringparam(ring->rx_pending,
S
Sony Chacko 已提交
488
			MIN_RCV_DESCRIPTORS, adapter->max_rxd, "rx");
489 490

	num_jumbo_rxd = qlcnic_validate_ringparam(ring->rx_jumbo_pending,
S
Sony Chacko 已提交
491 492
			MIN_JUMBO_DESCRIPTORS, adapter->max_jumbo_rxd,
						"rx jumbo");
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

	num_txd = qlcnic_validate_ringparam(ring->tx_pending,
			MIN_CMD_DESCRIPTORS, MAX_CMD_DESCRIPTORS, "tx");

	if (num_rxd == adapter->num_rxd && num_txd == adapter->num_txd &&
			num_jumbo_rxd == adapter->num_jumbo_rxd)
		return 0;

	adapter->num_rxd = num_rxd;
	adapter->num_jumbo_rxd = num_jumbo_rxd;
	adapter->num_txd = num_txd;

	return qlcnic_reset_context(adapter);
}

508 509 510
static void qlcnic_get_channels(struct net_device *dev,
		struct ethtool_channels *channel)
{
511
	int min;
512 513
	struct qlcnic_adapter *adapter = netdev_priv(dev);

514 515 516
	min = min_t(int, adapter->ahw->max_rx_ques, num_online_cpus());
	channel->max_rx = rounddown_pow_of_two(min);
	channel->max_tx = adapter->ahw->max_tx_ques;
517 518

	channel->rx_count = adapter->max_sds_rings;
519
	channel->tx_count = adapter->ahw->max_tx_ques;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
}

static int qlcnic_set_channels(struct net_device *dev,
		struct ethtool_channels *channel)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	int err;

	if (channel->other_count || channel->combined_count ||
	    channel->tx_count != channel->max_tx)
		return -EINVAL;

	err = qlcnic_validate_max_rss(dev, channel->max_rx, channel->rx_count);
	if (err)
		return err;

	err = qlcnic_set_max_rss(adapter, channel->rx_count);
	netdev_info(dev, "allocated 0x%x sds rings\n",
				 adapter->max_sds_rings);
	return err;
}

542 543 544 545 546
static void
qlcnic_get_pauseparam(struct net_device *netdev,
			  struct ethtool_pauseparam *pause)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
547
	int port = adapter->ahw->physical_port;
548 549
	__u32 val;

A
Anirban Chakraborty 已提交
550
	if (adapter->ahw->port_type == QLCNIC_GBE) {
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
		if ((port < 0) || (port > QLCNIC_NIU_MAX_GBE_PORTS))
			return;
		/* get flow control settings */
		val = QLCRD32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port));
		pause->rx_pause = qlcnic_gb_get_rx_flowctl(val);
		val = QLCRD32(adapter, QLCNIC_NIU_GB_PAUSE_CTL);
		switch (port) {
		case 0:
			pause->tx_pause = !(qlcnic_gb_get_gb0_mask(val));
			break;
		case 1:
			pause->tx_pause = !(qlcnic_gb_get_gb1_mask(val));
			break;
		case 2:
			pause->tx_pause = !(qlcnic_gb_get_gb2_mask(val));
			break;
		case 3:
		default:
			pause->tx_pause = !(qlcnic_gb_get_gb3_mask(val));
			break;
		}
A
Anirban Chakraborty 已提交
572
	} else if (adapter->ahw->port_type == QLCNIC_XGBE) {
573 574 575 576 577 578 579 580 581 582
		if ((port < 0) || (port > QLCNIC_NIU_MAX_XG_PORTS))
			return;
		pause->rx_pause = 1;
		val = QLCRD32(adapter, QLCNIC_NIU_XG_PAUSE_CTL);
		if (port == 0)
			pause->tx_pause = !(qlcnic_xg_get_xg0_mask(val));
		else
			pause->tx_pause = !(qlcnic_xg_get_xg1_mask(val));
	} else {
		dev_err(&netdev->dev, "Unknown board type: %x\n",
A
Anirban Chakraborty 已提交
583
					adapter->ahw->port_type);
584 585 586 587 588 589 590 591
	}
}

static int
qlcnic_set_pauseparam(struct net_device *netdev,
			  struct ethtool_pauseparam *pause)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
592
	int port = adapter->ahw->physical_port;
593 594 595
	__u32 val;

	/* read mode */
A
Anirban Chakraborty 已提交
596
	if (adapter->ahw->port_type == QLCNIC_GBE) {
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
		if ((port < 0) || (port > QLCNIC_NIU_MAX_GBE_PORTS))
			return -EIO;
		/* set flow control */
		val = QLCRD32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port));

		if (pause->rx_pause)
			qlcnic_gb_rx_flowctl(val);
		else
			qlcnic_gb_unset_rx_flowctl(val);

		QLCWR32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port),
				val);
		/* set autoneg */
		val = QLCRD32(adapter, QLCNIC_NIU_GB_PAUSE_CTL);
		switch (port) {
		case 0:
			if (pause->tx_pause)
				qlcnic_gb_unset_gb0_mask(val);
			else
				qlcnic_gb_set_gb0_mask(val);
			break;
		case 1:
			if (pause->tx_pause)
				qlcnic_gb_unset_gb1_mask(val);
			else
				qlcnic_gb_set_gb1_mask(val);
			break;
		case 2:
			if (pause->tx_pause)
				qlcnic_gb_unset_gb2_mask(val);
			else
				qlcnic_gb_set_gb2_mask(val);
			break;
		case 3:
		default:
			if (pause->tx_pause)
				qlcnic_gb_unset_gb3_mask(val);
			else
				qlcnic_gb_set_gb3_mask(val);
			break;
		}
		QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, val);
A
Anirban Chakraborty 已提交
639
	} else if (adapter->ahw->port_type == QLCNIC_XGBE) {
640 641 642
		if (!pause->rx_pause || pause->autoneg)
			return -EOPNOTSUPP;

643 644
		if ((port < 0) || (port > QLCNIC_NIU_MAX_XG_PORTS))
			return -EIO;
645

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
		val = QLCRD32(adapter, QLCNIC_NIU_XG_PAUSE_CTL);
		if (port == 0) {
			if (pause->tx_pause)
				qlcnic_xg_unset_xg0_mask(val);
			else
				qlcnic_xg_set_xg0_mask(val);
		} else {
			if (pause->tx_pause)
				qlcnic_xg_unset_xg1_mask(val);
			else
				qlcnic_xg_set_xg1_mask(val);
		}
		QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, val);
	} else {
		dev_err(&netdev->dev, "Unknown board type: %x\n",
A
Anirban Chakraborty 已提交
661
				adapter->ahw->port_type);
662 663 664 665 666 667 668
	}
	return 0;
}

static int qlcnic_reg_test(struct net_device *dev)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
669
	u32 data_read;
670 671 672 673 674 675 676 677 678 679

	data_read = QLCRD32(adapter, QLCNIC_PCIX_PH_REG(0));
	if ((data_read & 0xffff) != adapter->pdev->vendor)
		return 1;

	return 0;
}

static int qlcnic_get_sset_count(struct net_device *dev, int sset)
{
A
amit salecha 已提交
680
	struct qlcnic_adapter *adapter = netdev_priv(dev);
681 682 683 684
	switch (sset) {
	case ETH_SS_TEST:
		return QLCNIC_TEST_LEN;
	case ETH_SS_STATS:
A
amit salecha 已提交
685
		if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
686 687
			return QLCNIC_TOTAL_STATS_LEN + QLCNIC_DEVICE_STATS_LEN;
		return QLCNIC_TOTAL_STATS_LEN;
688 689 690 691 692
	default:
		return -EOPNOTSUPP;
	}
}

693 694 695 696 697
static int qlcnic_irq_test(struct net_device *netdev)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
	int max_sds_rings = adapter->max_sds_rings;
	int ret;
698
	struct qlcnic_cmd_args cmd;
699 700 701 702 703 704 705 706

	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
		return -EIO;

	ret = qlcnic_diag_alloc_res(netdev, QLCNIC_INTERRUPT_TEST);
	if (ret)
		goto clear_it;

707
	adapter->ahw->diag_cnt = 0;
708 709 710 711 712 713
	memset(&cmd, 0, sizeof(cmd));
	cmd.req.cmd = QLCNIC_CDRP_CMD_INTRPT_TEST;
	cmd.req.arg1 = adapter->ahw->pci_func;
	qlcnic_issue_cmd(adapter, &cmd);
	ret = cmd.rsp.cmd;

714 715 716 717 718
	if (ret)
		goto done;

	msleep(10);

719
	ret = !adapter->ahw->diag_cnt;
720 721 722 723 724 725 726 727 728 729

done:
	qlcnic_diag_free_res(netdev, max_sds_rings);

clear_it:
	adapter->max_sds_rings = max_sds_rings;
	clear_bit(__QLCNIC_RESETTING, &adapter->state);
	return ret;
}

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
#define QLCNIC_ILB_PKT_SIZE 64
#define QLCNIC_NUM_ILB_PKT	16
#define QLCNIC_ILB_MAX_RCV_LOOP 10

static void qlcnic_create_loopback_buff(unsigned char *data, u8 mac[])
{
	unsigned char random_data[] = {0xa8, 0x06, 0x45, 0x00};

	memset(data, 0x4e, QLCNIC_ILB_PKT_SIZE);

	memcpy(data, mac, ETH_ALEN);
	memcpy(data + ETH_ALEN, mac, ETH_ALEN);

	memcpy(data + 2 * ETH_ALEN, random_data, sizeof(random_data));
}

int qlcnic_check_loopback_buff(unsigned char *data, u8 mac[])
{
	unsigned char buff[QLCNIC_ILB_PKT_SIZE];
	qlcnic_create_loopback_buff(buff, mac);
	return memcmp(data, buff, QLCNIC_ILB_PKT_SIZE);
}

753
static int qlcnic_do_lb_test(struct qlcnic_adapter *adapter, u8 mode)
754 755 756 757 758 759 760
{
	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
	struct qlcnic_host_sds_ring *sds_ring = &recv_ctx->sds_rings[0];
	struct sk_buff *skb;
	int i, loop, cnt = 0;

	for (i = 0; i < QLCNIC_NUM_ILB_PKT; i++) {
761
		skb = netdev_alloc_skb(adapter->netdev, QLCNIC_ILB_PKT_SIZE);
762 763 764
		qlcnic_create_loopback_buff(skb->data, adapter->mac_addr);
		skb_put(skb, QLCNIC_ILB_PKT_SIZE);

765
		adapter->ahw->diag_cnt = 0;
766 767 768 769 770 771 772 773
		qlcnic_xmit_frame(skb, adapter->netdev);

		loop = 0;
		do {
			msleep(1);
			qlcnic_process_rcv_ring_diag(sds_ring);
			if (loop++ > QLCNIC_ILB_MAX_RCV_LOOP)
				break;
774
		} while (!adapter->ahw->diag_cnt);
775 776 777

		dev_kfree_skb_any(skb);

778
		if (!adapter->ahw->diag_cnt)
779 780
			QLCDB(adapter, DRV,
			"LB Test: packet #%d was not received\n", i + 1);
781 782 783 784
		else
			cnt++;
	}
	if (cnt != i) {
785
		dev_warn(&adapter->pdev->dev, "LB Test failed\n");
786 787 788 789 790
		if (mode != QLCNIC_ILB_MODE) {
			dev_warn(&adapter->pdev->dev,
				"WARNING: Please make sure external"
				"loopback connector is plugged in\n");
		}
791 792 793 794 795
		return -1;
	}
	return 0;
}

796
static int qlcnic_loopback_test(struct net_device *netdev, u8 mode)
797 798 799 800 801 802 803
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
	int max_sds_rings = adapter->max_sds_rings;
	struct qlcnic_host_sds_ring *sds_ring;
	int loop = 0;
	int ret;

804 805
	if (!(adapter->ahw->capabilities &
	      QLCNIC_FW_CAPABILITY_MULTI_LOOPBACK)) {
806 807 808 809
		netdev_info(netdev, "Firmware is not loopback test capable\n");
		return -EOPNOTSUPP;
	}

810
	QLCDB(adapter, DRV, "%s loopback test in progress\n",
811
		   mode == QLCNIC_ILB_MODE ? "internal" : "external");
812
	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
813 814 815 816 817 818
		netdev_warn(netdev, "Loopback test not supported for non "
				"privilege function\n");
		return 0;
	}

	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
819
		return -EBUSY;
820 821 822 823 824 825 826

	ret = qlcnic_diag_alloc_res(netdev, QLCNIC_LOOPBACK_TEST);
	if (ret)
		goto clear_it;

	sds_ring = &adapter->recv_ctx->sds_rings[0];

827
	ret = qlcnic_set_lb_mode(adapter, mode);
828 829 830
	if (ret)
		goto free_res;

831
	adapter->ahw->diag_cnt = 0;
832 833 834
	do {
		msleep(500);
		qlcnic_process_rcv_ring_diag(sds_ring);
835 836 837 838 839
		if (loop++ > QLCNIC_ILB_MAX_RCV_LOOP) {
			netdev_info(netdev, "firmware didnt respond to loopback"
				" configure request\n");
			ret = -QLCNIC_FW_NOT_RESPOND;
			goto free_res;
840 841
		} else if (adapter->ahw->diag_cnt) {
			ret = adapter->ahw->diag_cnt;
842 843
			goto free_res;
		}
844 845
	} while (!QLCNIC_IS_LB_CONFIGURED(adapter->ahw->loopback_state));

846
	ret = qlcnic_do_lb_test(adapter, mode);
847 848 849 850 851 852 853 854 855 856 857 858

	qlcnic_clear_lb_mode(adapter);

 free_res:
	qlcnic_diag_free_res(netdev, max_sds_rings);

 clear_it:
	adapter->max_sds_rings = max_sds_rings;
	clear_bit(__QLCNIC_RESETTING, &adapter->state);
	return ret;
}

859 860 861 862 863
static void
qlcnic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
		     u64 *data)
{
	memset(data, 0, sizeof(u64) * QLCNIC_TEST_LEN);
864

S
Sony Chacko 已提交
865 866 867 868 869 870 871 872
	data[0] = qlcnic_reg_test(dev);
	if (data[0])
		eth_test->flags |= ETH_TEST_FL_FAILED;

	data[1] = (u64) qlcnic_test_link(dev);
	if (data[1])
		eth_test->flags |= ETH_TEST_FL_FAILED;

873
	if (eth_test->flags & ETH_TEST_FL_OFFLINE) {
874 875 876
		data[2] = qlcnic_irq_test(dev);
		if (data[2])
			eth_test->flags |= ETH_TEST_FL_FAILED;
877

878
		data[3] = qlcnic_loopback_test(dev, QLCNIC_ILB_MODE);
879 880
		if (data[3])
			eth_test->flags |= ETH_TEST_FL_FAILED;
881 882 883 884 885 886
		if (eth_test->flags & ETH_TEST_FL_EXTERNAL_LB) {
			data[4] = qlcnic_loopback_test(dev, QLCNIC_ELB_MODE);
			if (data[4])
				eth_test->flags |= ETH_TEST_FL_FAILED;
			eth_test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
		}
887
	}
888 889 890 891 892
}

static void
qlcnic_get_strings(struct net_device *dev, u32 stringset, u8 * data)
{
A
amit salecha 已提交
893
	struct qlcnic_adapter *adapter = netdev_priv(dev);
894
	int index, i, j;
895 896 897 898 899 900 901 902 903 904 905 906

	switch (stringset) {
	case ETH_SS_TEST:
		memcpy(data, *qlcnic_gstrings_test,
		       QLCNIC_TEST_LEN * ETH_GSTRING_LEN);
		break;
	case ETH_SS_STATS:
		for (index = 0; index < QLCNIC_STATS_LEN; index++) {
			memcpy(data + index * ETH_GSTRING_LEN,
			       qlcnic_gstrings_stats[index].stat_string,
			       ETH_GSTRING_LEN);
		}
907 908 909 910 911
		for (j = 0; j < QLCNIC_MAC_STATS_LEN; index++, j++) {
			memcpy(data + index * ETH_GSTRING_LEN,
			       qlcnic_mac_stats_strings[j],
			       ETH_GSTRING_LEN);
		}
A
amit salecha 已提交
912 913 914 915 916 917 918
		if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
			return;
		for (i = 0; i < QLCNIC_DEVICE_STATS_LEN; index++, i++) {
			memcpy(data + index * ETH_GSTRING_LEN,
			       qlcnic_device_gstrings_stats[i],
			       ETH_GSTRING_LEN);
		}
919 920 921
	}
}

A
amit salecha 已提交
922
static void
923
qlcnic_fill_stats(int *index, u64 *data, void *stats, int type)
A
amit salecha 已提交
924 925 926
{
	int ind = *index;

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	if (type == QLCNIC_MAC_STATS) {
		struct qlcnic_mac_statistics *mac_stats =
					(struct qlcnic_mac_statistics *)stats;
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_frames);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_bytes);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_mcast_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_bcast_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_pause_cnt);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_ctrl_pkt);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_lt_64b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_lt_127b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_lt_255b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_tx_lt_511b_pkts);
		data[ind++] =
			QLCNIC_FILL_STATS(mac_stats->mac_tx_lt_1023b_pkts);
		data[ind++] =
			QLCNIC_FILL_STATS(mac_stats->mac_tx_lt_1518b_pkts);
		data[ind++] =
			QLCNIC_FILL_STATS(mac_stats->mac_tx_gt_1518b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_frames);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_bytes);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_mcast_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_bcast_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_pause_cnt);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_ctrl_pkt);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_lt_64b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_lt_127b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_lt_255b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_lt_511b_pkts);
		data[ind++] =
			QLCNIC_FILL_STATS(mac_stats->mac_rx_lt_1023b_pkts);
		data[ind++] =
			QLCNIC_FILL_STATS(mac_stats->mac_rx_lt_1518b_pkts);
		data[ind++] =
			QLCNIC_FILL_STATS(mac_stats->mac_rx_gt_1518b_pkts);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_length_error);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_length_small);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_length_large);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_jabber);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_dropped);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_rx_crc_error);
		data[ind++] = QLCNIC_FILL_STATS(mac_stats->mac_align_error);
	} else if (type == QLCNIC_ESW_STATS) {
		struct __qlcnic_esw_statistics *esw_stats =
				(struct __qlcnic_esw_statistics *)stats;
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->unicast_frames);
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->multicast_frames);
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->broadcast_frames);
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->dropped_frames);
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->errors);
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->local_frames);
		data[ind++] = QLCNIC_FILL_STATS(esw_stats->numbytes);
	}
A
amit salecha 已提交
980 981 982 983

	*index = ind;
}

984 985 986 987 988
static void
qlcnic_get_ethtool_stats(struct net_device *dev,
			     struct ethtool_stats *stats, u64 * data)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
A
amit salecha 已提交
989
	struct qlcnic_esw_statistics port_stats;
990
	struct qlcnic_mac_statistics mac_stats;
A
amit salecha 已提交
991
	int index, ret;
992 993 994 995 996 997 998 999 1000

	for (index = 0; index < QLCNIC_STATS_LEN; index++) {
		char *p =
		    (char *)adapter +
		    qlcnic_gstrings_stats[index].stat_offset;
		data[index] =
		    (qlcnic_gstrings_stats[index].sizeof_stat ==
		     sizeof(u64)) ? *(u64 *)p:(*(u32 *)p);
	}
A
amit salecha 已提交
1001

1002 1003 1004 1005 1006
	/* Retrieve MAC statistics from firmware */
	memset(&mac_stats, 0, sizeof(struct qlcnic_mac_statistics));
	qlcnic_get_mac_stats(adapter, &mac_stats);
	qlcnic_fill_stats(&index, data, &mac_stats, QLCNIC_MAC_STATS);

A
amit salecha 已提交
1007 1008 1009 1010
	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
		return;

	memset(&port_stats, 0, sizeof(struct qlcnic_esw_statistics));
A
Anirban Chakraborty 已提交
1011
	ret = qlcnic_get_port_stats(adapter, adapter->ahw->pci_func,
A
amit salecha 已提交
1012 1013 1014 1015
			QLCNIC_QUERY_RX_COUNTER, &port_stats.rx);
	if (ret)
		return;

1016
	qlcnic_fill_stats(&index, data, &port_stats.rx, QLCNIC_ESW_STATS);
A
amit salecha 已提交
1017

A
Anirban Chakraborty 已提交
1018
	ret = qlcnic_get_port_stats(adapter, adapter->ahw->pci_func,
A
amit salecha 已提交
1019 1020 1021 1022
			QLCNIC_QUERY_TX_COUNTER, &port_stats.tx);
	if (ret)
		return;

1023
	qlcnic_fill_stats(&index, data, &port_stats.tx, QLCNIC_ESW_STATS);
1024 1025
}

1026 1027
static int qlcnic_set_led(struct net_device *dev,
			  enum ethtool_phys_id_state state)
1028 1029
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
1030
	int max_sds_rings = adapter->max_sds_rings;
1031 1032
	int err = -EIO, active = 1;

1033
	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
1034 1035 1036 1037
		netdev_warn(dev, "LED test not supported for non "
				"privilege function\n");
		return -EOPNOTSUPP;
	}
1038

1039 1040
	switch (state) {
	case ETHTOOL_ID_ACTIVE:
1041 1042 1043
		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state))
			return -EBUSY;

1044 1045
		if (test_bit(__QLCNIC_RESETTING, &adapter->state))
			break;
1046

1047 1048 1049
		if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
			if (qlcnic_diag_alloc_res(dev, QLCNIC_LED_TEST))
				break;
1050
			set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
1051
		}
1052

1053 1054 1055 1056
		if (adapter->nic_ops->config_led(adapter, 1, 0xf) == 0) {
			err = 0;
			break;
		}
1057

1058 1059
		dev_err(&adapter->pdev->dev,
			"Failed to set LED blink state.\n");
1060
		break;
1061

1062
	case ETHTOOL_ID_INACTIVE:
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
		active = 0;

		if (test_bit(__QLCNIC_RESETTING, &adapter->state))
			break;

		if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
			if (qlcnic_diag_alloc_res(dev, QLCNIC_LED_TEST))
				break;
			set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
		}

1074 1075 1076
		if (adapter->nic_ops->config_led(adapter, 0, 0xf))
			dev_err(&adapter->pdev->dev,
				"Failed to reset LED blink state.\n");
1077

1078 1079 1080 1081
		break;

	default:
		return -EINVAL;
1082 1083
	}

1084
	if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
1085 1086
		qlcnic_diag_free_res(dev, max_sds_rings);

1087 1088
	if (!active || err)
		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
1089

1090
	return err;
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 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
static void
qlcnic_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	u32 wol_cfg;

	wol->supported = 0;
	wol->wolopts = 0;

	wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
	if (wol_cfg & (1UL << adapter->portnum))
		wol->supported |= WAKE_MAGIC;

	wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
	if (wol_cfg & (1UL << adapter->portnum))
		wol->wolopts |= WAKE_MAGIC;
}

static int
qlcnic_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct qlcnic_adapter *adapter = netdev_priv(dev);
	u32 wol_cfg;

	if (wol->wolopts & ~WAKE_MAGIC)
		return -EOPNOTSUPP;

	wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
	if (!(wol_cfg & (1 << adapter->portnum)))
		return -EOPNOTSUPP;

	wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
	if (wol->wolopts & WAKE_MAGIC)
		wol_cfg |= 1UL << adapter->portnum;
	else
		wol_cfg &= ~(1UL << adapter->portnum);

	QLCWR32(adapter, QLCNIC_WOL_CONFIG, wol_cfg);

	return 0;
}

/*
 * Set the coalescing parameters. Currently only normal is supported.
 * If rx_coalesce_usecs == 0 or rx_max_coalesced_frames == 0 then set the
 * firmware coalescing to default.
 */
static int qlcnic_set_intr_coalesce(struct net_device *netdev,
			struct ethtool_coalesce *ethcoal)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);

1145
	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
1146 1147 1148 1149 1150 1151 1152 1153
		return -EINVAL;

	/*
	* Return Error if unsupported values or
	* unsupported parameters are set.
	*/
	if (ethcoal->rx_coalesce_usecs > 0xffff ||
		ethcoal->rx_max_coalesced_frames > 0xffff ||
A
Anirban Chakraborty 已提交
1154 1155
		ethcoal->tx_coalesce_usecs ||
		ethcoal->tx_max_coalesced_frames ||
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
		ethcoal->rx_coalesce_usecs_irq ||
		ethcoal->rx_max_coalesced_frames_irq ||
		ethcoal->tx_coalesce_usecs_irq ||
		ethcoal->tx_max_coalesced_frames_irq ||
		ethcoal->stats_block_coalesce_usecs ||
		ethcoal->use_adaptive_rx_coalesce ||
		ethcoal->use_adaptive_tx_coalesce ||
		ethcoal->pkt_rate_low ||
		ethcoal->rx_coalesce_usecs_low ||
		ethcoal->rx_max_coalesced_frames_low ||
		ethcoal->tx_coalesce_usecs_low ||
		ethcoal->tx_max_coalesced_frames_low ||
		ethcoal->pkt_rate_high ||
		ethcoal->rx_coalesce_usecs_high ||
		ethcoal->rx_max_coalesced_frames_high ||
		ethcoal->tx_coalesce_usecs_high ||
		ethcoal->tx_max_coalesced_frames_high)
		return -EINVAL;

	if (!ethcoal->rx_coalesce_usecs ||
		!ethcoal->rx_max_coalesced_frames) {
A
Anirban Chakraborty 已提交
1177 1178
		adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
		adapter->ahw->coal.rx_time_us =
1179
			QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
A
Anirban Chakraborty 已提交
1180
		adapter->ahw->coal.rx_packets =
1181 1182
			QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
	} else {
A
Anirban Chakraborty 已提交
1183 1184 1185 1186
		adapter->ahw->coal.flag = 0;
		adapter->ahw->coal.rx_time_us = ethcoal->rx_coalesce_usecs;
		adapter->ahw->coal.rx_packets =
			ethcoal->rx_max_coalesced_frames;
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	}

	qlcnic_config_intr_coalesce(adapter);

	return 0;
}

static int qlcnic_get_intr_coalesce(struct net_device *netdev,
			struct ethtool_coalesce *ethcoal)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);

	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
		return -EINVAL;

A
Anirban Chakraborty 已提交
1202 1203
	ethcoal->rx_coalesce_usecs = adapter->ahw->coal.rx_time_us;
	ethcoal->rx_max_coalesced_frames = adapter->ahw->coal.rx_packets;
1204 1205 1206 1207

	return 0;
}

1208 1209 1210 1211
static u32 qlcnic_get_msglevel(struct net_device *netdev)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);

1212
	return adapter->ahw->msg_enable;
1213 1214 1215 1216 1217 1218
}

static void qlcnic_set_msglevel(struct net_device *netdev, u32 msglvl)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);

1219
	adapter->ahw->msg_enable = msglvl;
1220 1221
}

1222 1223 1224 1225 1226 1227
static int
qlcnic_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
{
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
	struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;

1228 1229 1230 1231 1232
	if (!fw_dump->tmpl_hdr) {
		netdev_err(adapter->netdev, "FW Dump not supported\n");
		return -ENOTSUPP;
	}

1233 1234 1235 1236
	if (fw_dump->clr)
		dump->len = fw_dump->tmpl_hdr->size + fw_dump->size;
	else
		dump->len = 0;
1237 1238 1239 1240 1241 1242

	if (!fw_dump->enable)
		dump->flag = ETH_FW_DUMP_DISABLE;
	else
		dump->flag = fw_dump->tmpl_hdr->drv_cap_mask;

1243 1244 1245 1246 1247 1248 1249 1250 1251
	dump->version = adapter->fw_version;
	return 0;
}

static int
qlcnic_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
			void *buffer)
{
	int i, copy_sz;
1252 1253
	u32 *hdr_ptr;
	__le32 *data;
1254 1255 1256
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
	struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;

1257 1258 1259 1260 1261
	if (!fw_dump->tmpl_hdr) {
		netdev_err(netdev, "FW Dump not supported\n");
		return -ENOTSUPP;
	}

1262 1263 1264 1265 1266 1267 1268
	if (!fw_dump->clr) {
		netdev_info(netdev, "Dump not available\n");
		return -EINVAL;
	}
	/* Copy template header first */
	copy_sz = fw_dump->tmpl_hdr->size;
	hdr_ptr = (u32 *) fw_dump->tmpl_hdr;
1269
	data = buffer;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	for (i = 0; i < copy_sz/sizeof(u32); i++)
		*data++ = cpu_to_le32(*hdr_ptr++);

	/* Copy captured dump data */
	memcpy(buffer + copy_sz, fw_dump->data, fw_dump->size);
	dump->len = copy_sz + fw_dump->size;
	dump->flag = fw_dump->tmpl_hdr->drv_cap_mask;

	/* Free dump area once data has been captured */
	vfree(fw_dump->data);
	fw_dump->data = NULL;
	fw_dump->clr = 0;
M
Manish chopra 已提交
1282
	netdev_info(netdev, "extracted the FW dump Successfully\n");
1283 1284 1285 1286 1287 1288
	return 0;
}

static int
qlcnic_set_dump(struct net_device *netdev, struct ethtool_dump *val)
{
1289
	int i;
1290 1291
	struct qlcnic_adapter *adapter = netdev_priv(netdev);
	struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
1292
	u32 state;
1293

1294 1295
	switch (val->flag) {
	case QLCNIC_FORCE_FW_DUMP_KEY:
1296 1297 1298 1299
		if (!fw_dump->tmpl_hdr) {
			netdev_err(netdev, "FW dump not supported\n");
			return -ENOTSUPP;
		}
1300 1301
		if (!fw_dump->enable) {
			netdev_info(netdev, "FW dump not enabled\n");
1302
			return 0;
1303 1304
		}
		if (fw_dump->clr) {
M
Manish chopra 已提交
1305
			netdev_info(netdev,
1306
			"Previous dump not cleared, not forcing dump\n");
1307
			return 0;
1308
		}
1309 1310
		netdev_info(netdev, "Forcing a FW dump\n");
		qlcnic_dev_request_reset(adapter);
1311 1312
		break;
	case QLCNIC_DISABLE_FW_DUMP:
1313
		if (fw_dump->enable && fw_dump->tmpl_hdr) {
1314 1315 1316
			netdev_info(netdev, "Disabling FW dump\n");
			fw_dump->enable = 0;
		}
1317
		return 0;
1318
	case QLCNIC_ENABLE_FW_DUMP:
1319 1320 1321 1322 1323
		if (!fw_dump->tmpl_hdr) {
			netdev_err(netdev, "FW dump not supported\n");
			return -ENOTSUPP;
		}
		if (!fw_dump->enable) {
1324 1325 1326
			netdev_info(netdev, "Enabling FW dump\n");
			fw_dump->enable = 1;
		}
1327
		return 0;
1328 1329 1330 1331
	case QLCNIC_FORCE_FW_RESET:
		netdev_info(netdev, "Forcing a FW reset\n");
		qlcnic_dev_request_reset(adapter);
		adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
1332
		return 0;
1333 1334 1335 1336 1337
	case QLCNIC_SET_QUIESCENT:
	case QLCNIC_RESET_QUIESCENT:
		state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
		if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
			netdev_info(netdev, "Device in FAILED state\n");
1338
		return 0;
1339
	default:
1340 1341 1342 1343
		if (!fw_dump->tmpl_hdr) {
			netdev_err(netdev, "FW dump not supported\n");
			return -ENOTSUPP;
		}
1344 1345 1346 1347 1348 1349 1350 1351
		for (i = 0; i < ARRAY_SIZE(FW_DUMP_LEVELS); i++) {
			if (val->flag == FW_DUMP_LEVELS[i]) {
				fw_dump->tmpl_hdr->drv_cap_mask =
							val->flag;
				netdev_info(netdev, "Driver mask changed to: 0x%x\n",
					fw_dump->tmpl_hdr->drv_cap_mask);
				return 0;
			}
1352
		}
1353 1354
		netdev_info(netdev, "Invalid dump level: 0x%x\n", val->flag);
		return -EINVAL;
1355
	}
1356
	return 0;
1357 1358
}

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
const struct ethtool_ops qlcnic_ethtool_ops = {
	.get_settings = qlcnic_get_settings,
	.set_settings = qlcnic_set_settings,
	.get_drvinfo = qlcnic_get_drvinfo,
	.get_regs_len = qlcnic_get_regs_len,
	.get_regs = qlcnic_get_regs,
	.get_link = ethtool_op_get_link,
	.get_eeprom_len = qlcnic_get_eeprom_len,
	.get_eeprom = qlcnic_get_eeprom,
	.get_ringparam = qlcnic_get_ringparam,
	.set_ringparam = qlcnic_set_ringparam,
1370 1371
	.get_channels = qlcnic_get_channels,
	.set_channels = qlcnic_set_channels,
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
	.get_pauseparam = qlcnic_get_pauseparam,
	.set_pauseparam = qlcnic_set_pauseparam,
	.get_wol = qlcnic_get_wol,
	.set_wol = qlcnic_set_wol,
	.self_test = qlcnic_diag_test,
	.get_strings = qlcnic_get_strings,
	.get_ethtool_stats = qlcnic_get_ethtool_stats,
	.get_sset_count = qlcnic_get_sset_count,
	.get_coalesce = qlcnic_get_intr_coalesce,
	.set_coalesce = qlcnic_set_intr_coalesce,
1382
	.set_phys_id = qlcnic_set_led,
1383 1384
	.set_msglevel = qlcnic_set_msglevel,
	.get_msglevel = qlcnic_get_msglevel,
1385 1386 1387
	.get_dump_flag = qlcnic_get_dump_flag,
	.get_dump_data = qlcnic_get_dump_data,
	.set_dump = qlcnic_set_dump,
1388
};
1389 1390 1391 1392 1393 1394 1395

const struct ethtool_ops qlcnic_ethtool_failed_ops = {
	.get_settings = qlcnic_get_settings,
	.get_drvinfo = qlcnic_get_drvinfo,
	.set_msglevel = qlcnic_set_msglevel,
	.get_msglevel = qlcnic_get_msglevel,
};