qlcnic_sysfs.c 36.1 KB
Newer Older
1 2 3 4 5 6 7
/*
 * QLogic qlcnic NIC Driver
 * Copyright (c) 2009-2013 QLogic Corporation
 *
 * See LICENSE.qlcnic for copyright and licensing details.
 */

8 9 10 11 12 13 14 15 16 17
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/swab.h>
#include <linux/dma-mapping.h>
#include <net/ip.h>
#include <linux/ipv6.h>
#include <linux/inetdevice.h>
#include <linux/sysfs.h>
#include <linux/aer.h>
#include <linux/log2.h>
18 19 20 21
#ifdef CONFIG_QLCNIC_HWMON
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#endif
22

23 24 25
#include "qlcnic.h"
#include "qlcnic_hw.h"

26 27 28 29 30 31 32 33 34 35
int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
{
	return -EOPNOTSUPP;
}

int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
{
	return -EOPNOTSUPP;
}

36 37 38
static ssize_t qlcnic_store_bridged_mode(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf, size_t len)
39 40 41 42 43
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	unsigned long new;
	int ret = -EINVAL;

44
	if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
45 46 47 48 49
		goto err_out;

	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
		goto err_out;

50
	if (kstrtoul(buf, 2, &new))
51 52
		goto err_out;

S
Sony Chacko 已提交
53
	if (!qlcnic_config_bridged_mode(adapter, !!new))
54 55 56 57 58 59
		ret = len;

err_out:
	return ret;
}

60 61 62
static ssize_t qlcnic_show_bridged_mode(struct device *dev,
					struct device_attribute *attr,
					char *buf)
63 64 65 66
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	int bridged_mode = 0;

67
	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
68 69 70 71 72
		bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);

	return sprintf(buf, "%d\n", bridged_mode);
}

73 74 75
static ssize_t qlcnic_store_diag_mode(struct device *dev,
				      struct device_attribute *attr,
				      const char *buf, size_t len)
76 77 78 79
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	unsigned long new;

80
	if (kstrtoul(buf, 2, &new))
81 82 83 84 85 86 87 88
		return -EINVAL;

	if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
		adapter->flags ^= QLCNIC_DIAG_ENABLED;

	return len;
}

89 90
static ssize_t qlcnic_show_diag_mode(struct device *dev,
				     struct device_attribute *attr, char *buf)
91 92
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
S
Sony Chacko 已提交
93
	return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
94 95
}

96 97
static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
				  u8 *state, u8 *rate)
98 99 100 101 102 103 104 105 106
{
	*rate = LSB(beacon);
	*state = MSB(beacon);

	QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);

	if (!*state) {
		*rate = __QLCNIC_MAX_LED_RATE;
		return 0;
107
	} else if (*state > __QLCNIC_MAX_LED_STATE) {
108
		return -EINVAL;
109
	}
110 111 112 113 114 115 116

	if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
		return -EINVAL;

	return 0;
}

117 118
static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
				    const char *buf, size_t len)
119
{
S
Sony Chacko 已提交
120 121
	struct qlcnic_hardware_context *ahw = adapter->ahw;
	unsigned long h_beacon;
122
	int err;
123

124 125
	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
		return -EIO;
126

127 128
	if (kstrtoul(buf, 2, &h_beacon))
		return -EINVAL;
S
Sony Chacko 已提交
129

130 131
	qlcnic_get_beacon_state(adapter);

132
	if (ahw->beacon_state == h_beacon)
S
Sony Chacko 已提交
133
		return len;
134 135 136 137 138 139 140

	rtnl_lock();
	if (!ahw->beacon_state) {
		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
			rtnl_unlock();
			return -EBUSY;
		}
S
Sony Chacko 已提交
141 142
	}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	if (h_beacon)
		err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
	else
		err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
	if (!err)
		ahw->beacon_state = h_beacon;

	if (!ahw->beacon_state)
		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);

	rtnl_unlock();
	return len;
}

static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
				    const char *buf, size_t len)
{
	struct qlcnic_hardware_context *ahw = adapter->ahw;
161
	int err, drv_sds_rings = adapter->drv_sds_rings;
162
	u16 beacon;
163
	u8 b_state, b_rate;
164

165
	if (len != sizeof(u16))
166
		return -EINVAL;
167 168 169 170 171 172

	memcpy(&beacon, buf, sizeof(u16));
	err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
	if (err)
		return err;

173
	qlcnic_get_beacon_state(adapter);
174 175

	if (ahw->beacon_state == b_state)
176 177 178
		return len;

	rtnl_lock();
179
	if (!ahw->beacon_state) {
180 181 182 183
		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
			rtnl_unlock();
			return -EBUSY;
		}
184
	}
185 186 187 188 189 190 191 192 193 194 195 196 197 198

	if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
		err = -EIO;
		goto out;
	}

	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
		err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
		if (err)
			goto out;
		set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
	}

	err = qlcnic_config_led(adapter, b_state, b_rate);
199
	if (!err) {
200
		err = len;
S
Sony Chacko 已提交
201
		ahw->beacon_state = b_state;
202
	}
203 204

	if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
205
		qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
206

207 208
out:
	if (!ahw->beacon_state)
209 210 211 212 213 214
		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
	rtnl_unlock();

	return err;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static ssize_t qlcnic_store_beacon(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t len)
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	int err = 0;

	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
		dev_warn(dev,
			 "LED test not supported in non privileged mode\n");
		return -EOPNOTSUPP;
	}

	if (qlcnic_82xx_check(adapter))
		err = qlcnic_82xx_store_beacon(adapter, buf, len);
	else if (qlcnic_83xx_check(adapter))
		err = qlcnic_83xx_store_beacon(adapter, buf, len);
	else
		return -EIO;

	return err;
}

238 239
static ssize_t qlcnic_show_beacon(struct device *dev,
				  struct device_attribute *attr, char *buf)
240 241 242 243 244 245
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
}

246 247
static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
				     loff_t offset, size_t size)
248 249 250 251 252 253 254 255
{
	size_t crb_size = 4;

	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
		return -EIO;

	if (offset < QLCNIC_PCI_CRBSPACE) {
		if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
256
				  QLCNIC_PCI_CAMQM_END))
257 258 259 260 261 262 263 264 265 266 267
			crb_size = 8;
		else
			return -EINVAL;
	}

	if ((size != crb_size) || (offset & (crb_size-1)))
		return  -EINVAL;

	return 0;
}

268 269 270
static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
				     struct bin_attribute *attr, char *buf,
				     loff_t offset, size_t size)
271 272 273 274 275 276 277 278
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	int ret;

	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
	if (ret != 0)
		return ret;
S
Sony Chacko 已提交
279
	qlcnic_read_crb(adapter, buf, offset, size);
280
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
281 282 283 284

	return size;
}

285 286 287
static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
				      struct bin_attribute *attr, char *buf,
				      loff_t offset, size_t size)
288 289 290 291 292 293 294 295 296
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	int ret;

	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
	if (ret != 0)
		return ret;

297
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
S
Sony Chacko 已提交
298
	qlcnic_write_crb(adapter, buf, offset, size);
299 300 301
	return size;
}

302 303
static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
				     loff_t offset, size_t size)
304 305 306 307 308 309 310 311 312 313
{
	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
		return -EIO;

	if ((size != 8) || (offset & 0x7))
		return  -EIO;

	return 0;
}

314 315 316
static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
				     struct bin_attribute *attr, char *buf,
				     loff_t offset, size_t size)
317 318 319 320 321 322 323 324 325 326 327 328 329 330
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	u64 data;
	int ret;

	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
	if (ret != 0)
		return ret;

	if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
		return -EIO;

	memcpy(buf, &data, size);
331
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
332 333 334 335

	return size;
}

336 337 338
static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
				      struct bin_attribute *attr, char *buf,
				      loff_t offset, size_t size)
339 340 341 342 343 344 345 346 347 348
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	u64 data;
	int ret;

	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
	if (ret != 0)
		return ret;

349
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
350 351 352 353 354 355 356 357
	memcpy(&data, buf, size);

	if (qlcnic_pci_mem_write_2M(adapter, offset, data))
		return -EIO;

	return size;
}

358 359
int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
{
S
Sony Chacko 已提交
360
	int i;
361

362
	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
S
Sony Chacko 已提交
363 364 365
		if (adapter->npars[i].pci_func == pci_func)
			return i;
	}
366 367

	dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
368
	return -EINVAL;
S
Sony Chacko 已提交
369 370
}

371 372
static int validate_pm_config(struct qlcnic_adapter *adapter,
			      struct qlcnic_pm_func_cfg *pm_cfg, int count)
373
{
S
Sony Chacko 已提交
374 375 376
	u8 src_pci_func, s_esw_id, d_esw_id;
	u8 dest_pci_func;
	int i, src_index, dest_index;
377 378 379 380

	for (i = 0; i < count; i++) {
		src_pci_func = pm_cfg[i].pci_func;
		dest_pci_func = pm_cfg[i].dest_npar;
S
Sony Chacko 已提交
381 382
		src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
		if (src_index < 0)
383
			return -EINVAL;
384

S
Sony Chacko 已提交
385 386
		dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
		if (dest_index < 0)
387
			return -EINVAL;
388

S
Sony Chacko 已提交
389 390
		s_esw_id = adapter->npars[src_index].phy_port;
		d_esw_id = adapter->npars[dest_index].phy_port;
391 392

		if (s_esw_id != d_esw_id)
393
			return -EINVAL;
394 395
	}

S
Sony Chacko 已提交
396
	return 0;
397 398
}

399 400 401 402 403
static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
					    struct kobject *kobj,
					    struct bin_attribute *attr,
					    char *buf, loff_t offset,
					    size_t size)
404 405 406 407 408
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	struct qlcnic_pm_func_cfg *pm_cfg;
	u32 id, action, pci_func;
S
Sony Chacko 已提交
409
	int count, rem, i, ret, index;
410 411 412 413

	count	= size / sizeof(struct qlcnic_pm_func_cfg);
	rem	= size % sizeof(struct qlcnic_pm_func_cfg);
	if (rem)
414
		return -EINVAL;
415

416
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
417
	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
418
	ret = validate_pm_config(adapter, pm_cfg, count);
S
Sony Chacko 已提交
419

420 421 422 423 424
	if (ret)
		return ret;
	for (i = 0; i < count; i++) {
		pci_func = pm_cfg[i].pci_func;
		action = !!pm_cfg[i].action;
S
Sony Chacko 已提交
425 426
		index = qlcnic_is_valid_nic_func(adapter, pci_func);
		if (index < 0)
427
			return -EINVAL;
S
Sony Chacko 已提交
428 429 430 431

		id = adapter->npars[index].phy_port;
		ret = qlcnic_config_port_mirroring(adapter, id,
						   action, pci_func);
432 433 434 435 436 437
		if (ret)
			return ret;
	}

	for (i = 0; i < count; i++) {
		pci_func = pm_cfg[i].pci_func;
S
Sony Chacko 已提交
438
		index = qlcnic_is_valid_nic_func(adapter, pci_func);
439
		if (index < 0)
440
			return -EINVAL;
S
Sony Chacko 已提交
441 442 443
		id = adapter->npars[index].phy_port;
		adapter->npars[index].enable_pm = !!pm_cfg[i].action;
		adapter->npars[index].dest_npar = id;
444
	}
S
Sony Chacko 已提交
445

446 447 448
	return size;
}

449 450 451 452 453
static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
					   struct kobject *kobj,
					   struct bin_attribute *attr,
					   char *buf, loff_t offset,
					   size_t size)
454 455 456
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
457
	struct qlcnic_pm_func_cfg *pm_cfg;
S
Sony Chacko 已提交
458
	u8 pci_func;
459 460
	u32 count;
	int i;
461

462
	memset(buf, 0, size);
463
	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
464 465
	count = size / sizeof(struct qlcnic_pm_func_cfg);
	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
S
Sony Chacko 已提交
466
		pci_func = adapter->npars[i].pci_func;
467 468 469
		if (pci_func >= count) {
			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
				__func__, adapter->ahw->total_nic_func, count);
470
			continue;
471
		}
472 473 474
		if (!adapter->npars[i].eswitch_status)
			continue;

S
Sony Chacko 已提交
475 476 477
		pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
		pm_cfg[pci_func].dest_npar = 0;
		pm_cfg[pci_func].pci_func = i;
478
	}
479
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
480 481 482
	return size;
}

483 484
static int validate_esw_config(struct qlcnic_adapter *adapter,
			       struct qlcnic_esw_func_cfg *esw_cfg, int count)
485
{
486 487
	struct qlcnic_hardware_context *ahw = adapter->ahw;
	int i, ret;
488 489 490
	u32 op_mode;
	u8 pci_func;

S
Sony Chacko 已提交
491
	if (qlcnic_82xx_check(adapter))
492
		op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
S
Sony Chacko 已提交
493
	else
494
		op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
495 496 497

	for (i = 0; i < count; i++) {
		pci_func = esw_cfg[i].pci_func;
498
		if (pci_func >= ahw->max_vnic_func)
499
			return -EINVAL;
500

S
Sony Chacko 已提交
501 502
		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
			if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
503
				return -EINVAL;
504 505 506

		switch (esw_cfg[i].op_mode) {
		case QLCNIC_PORT_DEFAULTS:
S
Sony Chacko 已提交
507 508 509 510 511 512 513 514 515
			if (qlcnic_82xx_check(adapter)) {
				ret = QLC_DEV_GET_DRV(op_mode, pci_func);
			} else {
				ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
								  pci_func);
				esw_cfg[i].offload_flags = 0;
			}

			if (ret != QLCNIC_NON_PRIV_FUNC) {
516
				if (esw_cfg[i].mac_anti_spoof != 0)
517
					return -EINVAL;
518
				if (esw_cfg[i].mac_override != 1)
519
					return -EINVAL;
520
				if (esw_cfg[i].promisc_mode != 1)
521
					return -EINVAL;
522 523 524 525
			}
			break;
		case QLCNIC_ADD_VLAN:
			if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
526
				return -EINVAL;
527
			if (!esw_cfg[i].op_type)
528
				return -EINVAL;
529 530 531
			break;
		case QLCNIC_DEL_VLAN:
			if (!esw_cfg[i].op_type)
532
				return -EINVAL;
533 534
			break;
		default:
535
			return -EINVAL;
536 537
		}
	}
S
Sony Chacko 已提交
538

539 540 541
	return 0;
}

542 543 544 545 546
static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
					     struct kobject *kobj,
					     struct bin_attribute *attr,
					     char *buf, loff_t offset,
					     size_t size)
547 548 549 550 551 552
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	struct qlcnic_esw_func_cfg *esw_cfg;
	struct qlcnic_npar_info *npar;
	int count, rem, i, ret;
S
Sony Chacko 已提交
553 554
	int index;
	u8 op_mode = 0, pci_func;
555 556 557 558

	count	= size / sizeof(struct qlcnic_esw_func_cfg);
	rem	= size % sizeof(struct qlcnic_esw_func_cfg);
	if (rem)
559
		return -EINVAL;
560

561
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
562
	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
563 564 565 566 567
	ret = validate_esw_config(adapter, esw_cfg, count);
	if (ret)
		return ret;

	for (i = 0; i < count; i++) {
S
Sony Chacko 已提交
568
		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
569
			if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
570
				return -EINVAL;
571 572 573 574 575 576 577 578 579 580 581 582

		if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
			continue;

		op_mode = esw_cfg[i].op_mode;
		qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
		esw_cfg[i].op_mode = op_mode;
		esw_cfg[i].pci_func = adapter->ahw->pci_func;

		switch (esw_cfg[i].op_mode) {
		case QLCNIC_PORT_DEFAULTS:
			qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
583 584 585
			rtnl_lock();
			qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
			rtnl_unlock();
586 587 588 589 590 591 592 593 594 595 596
			break;
		case QLCNIC_ADD_VLAN:
			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
			break;
		case QLCNIC_DEL_VLAN:
			esw_cfg[i].vlan_id = 0;
			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
			break;
		}
	}

597
	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
598 599 600 601
		goto out;

	for (i = 0; i < count; i++) {
		pci_func = esw_cfg[i].pci_func;
S
Sony Chacko 已提交
602
		index = qlcnic_is_valid_nic_func(adapter, pci_func);
603
		if (index < 0)
604
			return -EINVAL;
S
Sony Chacko 已提交
605
		npar = &adapter->npars[index];
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		switch (esw_cfg[i].op_mode) {
		case QLCNIC_PORT_DEFAULTS:
			npar->promisc_mode = esw_cfg[i].promisc_mode;
			npar->mac_override = esw_cfg[i].mac_override;
			npar->offload_flags = esw_cfg[i].offload_flags;
			npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
			npar->discard_tagged = esw_cfg[i].discard_tagged;
			break;
		case QLCNIC_ADD_VLAN:
			npar->pvid = esw_cfg[i].vlan_id;
			break;
		case QLCNIC_DEL_VLAN:
			npar->pvid = 0;
			break;
		}
	}
out:
	return size;
}

626 627 628 629 630
static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
					    struct kobject *kobj,
					    struct bin_attribute *attr,
					    char *buf, loff_t offset,
					    size_t size)
631 632 633
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
634
	struct qlcnic_esw_func_cfg *esw_cfg;
635 636 637
	u8 pci_func;
	u32 count;
	int i;
638

639
	memset(buf, 0, size);
640
	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
641 642
	count = size / sizeof(struct qlcnic_esw_func_cfg);
	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
S
Sony Chacko 已提交
643
		pci_func = adapter->npars[i].pci_func;
644 645 646
		if (pci_func >= count) {
			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
				__func__, adapter->ahw->total_nic_func, count);
647
			continue;
648
		}
649 650 651
		if (!adapter->npars[i].eswitch_status)
			continue;

S
Sony Chacko 已提交
652 653
		esw_cfg[pci_func].pci_func = pci_func;
		if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
654
			return -EINVAL;
655
	}
656
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
657 658 659
	return size;
}

660 661 662
static int validate_npar_config(struct qlcnic_adapter *adapter,
				struct qlcnic_npar_func_cfg *np_cfg,
				int count)
663 664 665 666 667
{
	u8 pci_func, i;

	for (i = 0; i < count; i++) {
		pci_func = np_cfg[i].pci_func;
S
Sony Chacko 已提交
668
		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
669
			return -EINVAL;
670 671 672

		if (!IS_VALID_BW(np_cfg[i].min_bw) ||
		    !IS_VALID_BW(np_cfg[i].max_bw))
673
			return -EINVAL;
674 675 676 677
	}
	return 0;
}

678 679 680 681 682
static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
					      struct kobject *kobj,
					      struct bin_attribute *attr,
					      char *buf, loff_t offset,
					      size_t size)
683 684 685 686 687
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	struct qlcnic_info nic_info;
	struct qlcnic_npar_func_cfg *np_cfg;
S
Sony Chacko 已提交
688
	int i, count, rem, ret, index;
689 690 691 692 693
	u8 pci_func;

	count	= size / sizeof(struct qlcnic_npar_func_cfg);
	rem	= size % sizeof(struct qlcnic_npar_func_cfg);
	if (rem)
694
		return -EINVAL;
695

696
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
697
	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
698 699 700 701
	ret = validate_npar_config(adapter, np_cfg, count);
	if (ret)
		return ret;

S
Sony Chacko 已提交
702
	for (i = 0; i < count; i++) {
703
		pci_func = np_cfg[i].pci_func;
S
Sony Chacko 已提交
704 705

		memset(&nic_info, 0, sizeof(struct qlcnic_info));
706 707 708 709 710 711 712 713 714
		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
		if (ret)
			return ret;
		nic_info.pci_func = pci_func;
		nic_info.min_tx_bw = np_cfg[i].min_bw;
		nic_info.max_tx_bw = np_cfg[i].max_bw;
		ret = qlcnic_set_nic_info(adapter, &nic_info);
		if (ret)
			return ret;
S
Sony Chacko 已提交
715
		index = qlcnic_is_valid_nic_func(adapter, pci_func);
716
		if (index < 0)
717
			return -EINVAL;
S
Sony Chacko 已提交
718 719
		adapter->npars[index].min_bw = nic_info.min_tx_bw;
		adapter->npars[index].max_bw = nic_info.max_tx_bw;
720 721 722 723
	}

	return size;
}
724 725 726 727 728 729

static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
					     struct kobject *kobj,
					     struct bin_attribute *attr,
					     char *buf, loff_t offset,
					     size_t size)
730 731 732
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
733
	struct qlcnic_npar_func_cfg *np_cfg;
734
	struct qlcnic_info nic_info;
735
	u8 pci_func;
736
	int i, ret;
737
	u32 count;
738

S
Sony Chacko 已提交
739
	memset(&nic_info, 0, sizeof(struct qlcnic_info));
740
	memset(buf, 0, size);
741
	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
S
Sony Chacko 已提交
742

743 744 745 746 747 748 749
	count = size / sizeof(struct qlcnic_npar_func_cfg);
	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
		if (adapter->npars[i].pci_func >= count) {
			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
				__func__, adapter->ahw->total_nic_func, count);
			continue;
		}
750 751
		if (!adapter->npars[i].eswitch_status)
			continue;
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
		pci_func = adapter->npars[i].pci_func;
		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
			continue;
		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
		if (ret)
			return ret;

		np_cfg[pci_func].pci_func = pci_func;
		np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
		np_cfg[pci_func].port_num = nic_info.phys_port;
		np_cfg[pci_func].fw_capab = nic_info.capabilities;
		np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
		np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
		np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
		np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
767
	}
768
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
769 770 771
	return size;
}

772 773 774 775 776
static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
					   struct kobject *kobj,
					   struct bin_attribute *attr,
					   char *buf, loff_t offset,
					   size_t size)
777 778 779 780 781 782
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	struct qlcnic_esw_statistics port_stats;
	int ret;

S
Sony Chacko 已提交
783
	if (qlcnic_83xx_check(adapter))
784
		return -EOPNOTSUPP;
S
Sony Chacko 已提交
785

786
	if (size != sizeof(struct qlcnic_esw_statistics))
787
		return -EINVAL;
788

789
	if (offset >= adapter->ahw->max_vnic_func)
790
		return -EINVAL;
791 792 793

	memset(&port_stats, 0, size);
	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
794
				    &port_stats.rx);
795 796 797 798
	if (ret)
		return ret;

	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
799
				    &port_stats.tx);
800 801 802 803 804 805 806
	if (ret)
		return ret;

	memcpy(buf, &port_stats, size);
	return size;
}

807 808 809 810 811
static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
					  struct kobject *kobj,
					  struct bin_attribute *attr,
					  char *buf, loff_t offset,
					  size_t size)
812 813 814 815 816 817
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	struct qlcnic_esw_statistics esw_stats;
	int ret;

S
Sony Chacko 已提交
818
	if (qlcnic_83xx_check(adapter))
819
		return -EOPNOTSUPP;
S
Sony Chacko 已提交
820

821
	if (size != sizeof(struct qlcnic_esw_statistics))
822
		return -EINVAL;
823 824

	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
825
		return -EINVAL;
826 827 828

	memset(&esw_stats, 0, size);
	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
829
				       &esw_stats.rx);
830 831 832 833
	if (ret)
		return ret;

	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
834
				       &esw_stats.tx);
835 836 837 838 839 840 841
	if (ret)
		return ret;

	memcpy(buf, &esw_stats, size);
	return size;
}

842 843 844 845 846
static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
					    struct kobject *kobj,
					    struct bin_attribute *attr,
					    char *buf, loff_t offset,
					    size_t size)
847 848 849 850 851
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	int ret;

S
Sony Chacko 已提交
852
	if (qlcnic_83xx_check(adapter))
853
		return -EOPNOTSUPP;
S
Sony Chacko 已提交
854

855
	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
856
		return -EINVAL;
857 858

	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
859
				     QLCNIC_QUERY_RX_COUNTER);
860 861 862 863
	if (ret)
		return ret;

	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
864
				     QLCNIC_QUERY_TX_COUNTER);
865 866 867 868 869 870
	if (ret)
		return ret;

	return size;
}

871 872 873 874 875
static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
					     struct kobject *kobj,
					     struct bin_attribute *attr,
					     char *buf, loff_t offset,
					     size_t size)
876
{
S
Sony Chacko 已提交
877

878 879 880 881
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	int ret;

S
Sony Chacko 已提交
882
	if (qlcnic_83xx_check(adapter))
883
		return -EOPNOTSUPP;
S
Sony Chacko 已提交
884

885
	if (offset >= adapter->ahw->max_vnic_func)
886
		return -EINVAL;
887 888

	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
889
				     QLCNIC_QUERY_RX_COUNTER);
890 891 892 893
	if (ret)
		return ret;

	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
894
				     QLCNIC_QUERY_TX_COUNTER);
895 896 897 898 899 900
	if (ret)
		return ret;

	return size;
}

901 902 903 904 905
static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
					    struct kobject *kobj,
					    struct bin_attribute *attr,
					    char *buf, loff_t offset,
					    size_t size)
906 907 908
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
909
	struct qlcnic_pci_func_cfg *pci_cfg;
910 911
	struct qlcnic_pci_info *pci_info;
	int i, ret;
912
	u32 count;
913

914
	pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
915 916 917 918 919
	if (!pci_info)
		return -ENOMEM;

	ret = qlcnic_get_pci_info(adapter, pci_info);
	if (ret) {
920
		kfree(pci_info);
921 922 923
		return ret;
	}

924
	pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
925
	count = size / sizeof(struct qlcnic_pci_func_cfg);
926
	qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
927
	for (i = 0; i < count; i++) {
928 929
		pci_cfg[i].pci_func = pci_info[i].id;
		pci_cfg[i].func_type = pci_info[i].type;
930
		pci_cfg[i].func_state = 0;
931 932 933 934 935
		pci_cfg[i].port_num = pci_info[i].default_port;
		pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
		pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
		memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
	}
S
Sony Chacko 已提交
936

937
	kfree(pci_info);
938 939 940
	return size;
}

941 942 943 944 945 946 947 948 949 950 951 952
static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
						    struct kobject *kobj,
						    struct bin_attribute *attr,
						    char *buf, loff_t offset,
						    size_t size)
{
	unsigned char *p_read_buf;
	int  ret, count;
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);

	if (!size)
953
		return -EINVAL;
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

	count = size / sizeof(u32);

	if (size % sizeof(u32))
		count++;

	p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
	if (!p_read_buf)
		return -ENOMEM;
	if (qlcnic_83xx_lock_flash(adapter) != 0) {
		kfree(p_read_buf);
		return -EIO;
	}

	ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
						count);

	if (ret) {
		qlcnic_83xx_unlock_flash(adapter);
		kfree(p_read_buf);
		return ret;
	}

	qlcnic_83xx_unlock_flash(adapter);
978
	qlcnic_swap32_buffer((u32 *)p_read_buf, count);
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	memcpy(buf, p_read_buf, size);
	kfree(p_read_buf);

	return size;
}

static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
					      char *buf, loff_t offset,
					      size_t size)
{
	int  i, ret, count;
	unsigned char *p_cache, *p_src;

	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
	if (!p_cache)
		return -ENOMEM;

996 997
	count = size / sizeof(u32);
	qlcnic_swap32_buffer((u32 *)buf, count);
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	memcpy(p_cache, buf, size);
	p_src = p_cache;

	if (qlcnic_83xx_lock_flash(adapter) != 0) {
		kfree(p_cache);
		return -EIO;
	}

	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
		ret = qlcnic_83xx_enable_flash_write(adapter);
		if (ret) {
			kfree(p_cache);
			qlcnic_83xx_unlock_flash(adapter);
			return -EIO;
		}
	}

	for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
		ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
						   (u32 *)p_src,
						   QLC_83XX_FLASH_WRITE_MAX);

		if (ret) {
			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
				ret = qlcnic_83xx_disable_flash_write(adapter);
				if (ret) {
					kfree(p_cache);
					qlcnic_83xx_unlock_flash(adapter);
					return -EIO;
				}
			}

			kfree(p_cache);
			qlcnic_83xx_unlock_flash(adapter);
			return -EIO;
		}

		p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
		offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
	}

	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
		ret = qlcnic_83xx_disable_flash_write(adapter);
		if (ret) {
			kfree(p_cache);
			qlcnic_83xx_unlock_flash(adapter);
			return -EIO;
		}
	}

	kfree(p_cache);
	qlcnic_83xx_unlock_flash(adapter);

	return 0;
}

static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
					 char *buf, loff_t offset, size_t size)
{
	int  i, ret, count;
	unsigned char *p_cache, *p_src;

	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
	if (!p_cache)
		return -ENOMEM;

1064
	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 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 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	memcpy(p_cache, buf, size);
	p_src = p_cache;
	count = size / sizeof(u32);

	if (qlcnic_83xx_lock_flash(adapter) != 0) {
		kfree(p_cache);
		return -EIO;
	}

	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
		ret = qlcnic_83xx_enable_flash_write(adapter);
		if (ret) {
			kfree(p_cache);
			qlcnic_83xx_unlock_flash(adapter);
			return -EIO;
		}
	}

	for (i = 0; i < count; i++) {
		ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
		if (ret) {
			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
				ret = qlcnic_83xx_disable_flash_write(adapter);
				if (ret) {
					kfree(p_cache);
					qlcnic_83xx_unlock_flash(adapter);
					return -EIO;
				}
			}
			kfree(p_cache);
			qlcnic_83xx_unlock_flash(adapter);
			return -EIO;
		}

		p_src = p_src + sizeof(u32);
		offset = offset + sizeof(u32);
	}

	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
		ret = qlcnic_83xx_disable_flash_write(adapter);
		if (ret) {
			kfree(p_cache);
			qlcnic_83xx_unlock_flash(adapter);
			return -EIO;
		}
	}

	kfree(p_cache);
	qlcnic_83xx_unlock_flash(adapter);

	return 0;
}

static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
						     struct kobject *kobj,
						     struct bin_attribute *attr,
						     char *buf, loff_t offset,
						     size_t size)
{
	int  ret;
	static int flash_mode;
	unsigned long data;
	struct device *dev = container_of(kobj, struct device, kobj);
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);

	ret = kstrtoul(buf, 16, &data);

	switch (data) {
	case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
		flash_mode = QLC_83XX_ERASE_MODE;
		ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
		if (ret) {
			dev_err(&adapter->pdev->dev,
				"%s failed at %d\n", __func__, __LINE__);
			return -EIO;
		}
		break;

	case QLC_83XX_FLASH_BULK_WRITE_CMD:
		flash_mode = QLC_83XX_BULK_WRITE_MODE;
		break;

	case QLC_83XX_FLASH_WRITE_CMD:
		flash_mode = QLC_83XX_WRITE_MODE;
		break;
	default:
		if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
			ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
								 offset, size);
			if (ret) {
				dev_err(&adapter->pdev->dev,
					"%s failed at %d\n",
					__func__, __LINE__);
				return -EIO;
			}
		}

		if (flash_mode == QLC_83XX_WRITE_MODE) {
			ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
							    offset, size);
			if (ret) {
				dev_err(&adapter->pdev->dev,
					"%s failed at %d\n", __func__,
					__LINE__);
				return -EIO;
			}
		}
	}

	return size;
}

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
static struct device_attribute dev_attr_bridged_mode = {
       .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
       .show = qlcnic_show_bridged_mode,
       .store = qlcnic_store_bridged_mode,
};

static struct device_attribute dev_attr_diag_mode = {
	.attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
	.show = qlcnic_show_diag_mode,
	.store = qlcnic_store_diag_mode,
};

static struct device_attribute dev_attr_beacon = {
	.attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
	.show = qlcnic_show_beacon,
	.store = qlcnic_store_beacon,
};

static struct bin_attribute bin_attr_crb = {
	.attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_read_crb,
	.write = qlcnic_sysfs_write_crb,
};

static struct bin_attribute bin_attr_mem = {
	.attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_read_mem,
	.write = qlcnic_sysfs_write_mem,
};

static struct bin_attribute bin_attr_npar_config = {
	.attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_read_npar_config,
	.write = qlcnic_sysfs_write_npar_config,
};

static struct bin_attribute bin_attr_pci_config = {
	.attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_read_pci_config,
	.write = NULL,
};

static struct bin_attribute bin_attr_port_stats = {
	.attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_get_port_stats,
	.write = qlcnic_sysfs_clear_port_stats,
};

static struct bin_attribute bin_attr_esw_stats = {
	.attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_get_esw_stats,
	.write = qlcnic_sysfs_clear_esw_stats,
};

static struct bin_attribute bin_attr_esw_config = {
	.attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_read_esw_config,
	.write = qlcnic_sysfs_write_esw_config,
};

static struct bin_attribute bin_attr_pm_config = {
	.attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_sysfs_read_pm_config,
	.write = qlcnic_sysfs_write_pm_config,
};

1251 1252 1253 1254 1255 1256 1257
static struct bin_attribute bin_attr_flash = {
	.attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
	.size = 0,
	.read = qlcnic_83xx_sysfs_flash_read_handler,
	.write = qlcnic_83xx_sysfs_flash_write_handler,
};

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
#ifdef CONFIG_QLCNIC_HWMON

static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
				      struct device_attribute *dev_attr,
				      char *buf)
{
	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
	unsigned int temperature = 0, value = 0;

	if (qlcnic_83xx_check(adapter))
		value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
	else if (qlcnic_82xx_check(adapter))
		value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);

	temperature = qlcnic_get_temp_val(value);
	/* display millidegree celcius */
	temperature *= 1000;
	return sprintf(buf, "%u\n", temperature);
}

/* hwmon-sysfs attributes */
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
			  qlcnic_hwmon_show_temp, NULL, 1);

static struct attribute *qlcnic_hwmon_attrs[] = {
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	NULL
};

ATTRIBUTE_GROUPS(qlcnic_hwmon);

void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
{
	struct device *dev = &adapter->pdev->dev;
	struct device *hwmon_dev;

	/* Skip hwmon registration for a VF device */
	if (qlcnic_sriov_vf_check(adapter)) {
		adapter->ahw->hwmon_dev = NULL;
		return;
	}
	hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
						      adapter,
						      qlcnic_hwmon_groups);
	if (IS_ERR(hwmon_dev)) {
		dev_err(dev, "Cannot register with hwmon, err=%ld\n",
			PTR_ERR(hwmon_dev));
		hwmon_dev = NULL;
	}
	adapter->ahw->hwmon_dev = hwmon_dev;
}

void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
{
	struct device *hwmon_dev = adapter->ahw->hwmon_dev;
	if (hwmon_dev) {
		hwmon_device_unregister(hwmon_dev);
		adapter->ahw->hwmon_dev = NULL;
	}
}
#endif

1320 1321 1322 1323
void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
{
	struct device *dev = &adapter->pdev->dev;

1324
	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1325 1326
		if (device_create_file(dev, &dev_attr_bridged_mode))
			dev_warn(dev,
1327
				 "failed to create bridged_mode sysfs entry\n");
1328 1329 1330 1331 1332 1333
}

void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
{
	struct device *dev = &adapter->pdev->dev;

1334
	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1335 1336 1337
		device_remove_file(dev, &dev_attr_bridged_mode);
}

1338
static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1339 1340 1341 1342 1343 1344
{
	struct device *dev = &adapter->pdev->dev;

	if (device_create_bin_file(dev, &bin_attr_port_stats))
		dev_info(dev, "failed to create port stats sysfs entry");

1345
	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1346 1347 1348 1349 1350 1351 1352 1353
		return;
	if (device_create_file(dev, &dev_attr_diag_mode))
		dev_info(dev, "failed to create diag_mode sysfs entry\n");
	if (device_create_bin_file(dev, &bin_attr_crb))
		dev_info(dev, "failed to create crb sysfs entry\n");
	if (device_create_bin_file(dev, &bin_attr_mem))
		dev_info(dev, "failed to create mem sysfs entry\n");

1354
	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1355 1356
		return;

1357 1358
	if (device_create_bin_file(dev, &bin_attr_pci_config))
		dev_info(dev, "failed to create pci config sysfs entry");
1359

1360 1361 1362 1363 1364 1365 1366
	if (device_create_file(dev, &dev_attr_beacon))
		dev_info(dev, "failed to create beacon sysfs entry");

	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
		return;
	if (device_create_bin_file(dev, &bin_attr_esw_config))
		dev_info(dev, "failed to create esw config sysfs entry");
1367
	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1368 1369 1370 1371 1372 1373 1374 1375 1376
		return;
	if (device_create_bin_file(dev, &bin_attr_npar_config))
		dev_info(dev, "failed to create npar config sysfs entry");
	if (device_create_bin_file(dev, &bin_attr_pm_config))
		dev_info(dev, "failed to create pm config sysfs entry");
	if (device_create_bin_file(dev, &bin_attr_esw_stats))
		dev_info(dev, "failed to create eswitch stats sysfs entry");
}

1377
static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1378 1379 1380 1381 1382
{
	struct device *dev = &adapter->pdev->dev;

	device_remove_bin_file(dev, &bin_attr_port_stats);

1383
	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1384 1385 1386 1387
		return;
	device_remove_file(dev, &dev_attr_diag_mode);
	device_remove_bin_file(dev, &bin_attr_crb);
	device_remove_bin_file(dev, &bin_attr_mem);
1388

1389
	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1390 1391
		return;

1392 1393 1394 1395 1396
	device_remove_bin_file(dev, &bin_attr_pci_config);
	device_remove_file(dev, &dev_attr_beacon);
	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
		return;
	device_remove_bin_file(dev, &bin_attr_esw_config);
1397
	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1398 1399 1400 1401 1402
		return;
	device_remove_bin_file(dev, &bin_attr_npar_config);
	device_remove_bin_file(dev, &bin_attr_pm_config);
	device_remove_bin_file(dev, &bin_attr_esw_stats);
}
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412

void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
{
	qlcnic_create_diag_entries(adapter);
}

void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
{
	qlcnic_remove_diag_entries(adapter);
}
S
Sony Chacko 已提交
1413 1414 1415

void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
{
1416 1417
	struct device *dev = &adapter->pdev->dev;

S
Sony Chacko 已提交
1418
	qlcnic_create_diag_entries(adapter);
1419 1420 1421

	if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
		dev_info(dev, "failed to create flash sysfs entry\n");
S
Sony Chacko 已提交
1422 1423 1424 1425
}

void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
{
1426 1427
	struct device *dev = &adapter->pdev->dev;

S
Sony Chacko 已提交
1428
	qlcnic_remove_diag_entries(adapter);
1429
	sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);
S
Sony Chacko 已提交
1430
}