Linux Audio

Check our new training course

Linux kernel drivers training

Mar 31-Apr 9, 2025, special US time zones
Register
Loading...
   1/*
   2 * QLogic qlcnic NIC Driver
   3 * Copyright (c) 2009-2013 QLogic Corporation
   4 *
   5 * See LICENSE.qlcnic for copyright and licensing details.
   6 */
   7
   8#include <linux/slab.h>
   9#include <linux/interrupt.h>
  10
  11#include "qlcnic.h"
  12#include "qlcnic_hw.h"
  13
  14#include <linux/swab.h>
  15#include <linux/dma-mapping.h>
  16#include <net/ip.h>
  17#include <linux/ipv6.h>
  18#include <linux/inetdevice.h>
  19#include <linux/sysfs.h>
  20#include <linux/aer.h>
  21#include <linux/log2.h>
 
 
 
 
  22
  23#define QLC_STATUS_UNSUPPORTED_CMD	-2
 
  24
  25int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
  26{
  27	return -EOPNOTSUPP;
  28}
  29
  30int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
  31{
  32	return -EOPNOTSUPP;
  33}
  34
  35static ssize_t qlcnic_store_bridged_mode(struct device *dev,
  36					 struct device_attribute *attr,
  37					 const char *buf, size_t len)
  38{
  39	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  40	unsigned long new;
  41	int ret = -EINVAL;
  42
  43	if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
  44		goto err_out;
  45
  46	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
  47		goto err_out;
  48
  49	if (kstrtoul(buf, 2, &new))
  50		goto err_out;
  51
  52	if (!qlcnic_config_bridged_mode(adapter, !!new))
  53		ret = len;
  54
  55err_out:
  56	return ret;
  57}
  58
  59static ssize_t qlcnic_show_bridged_mode(struct device *dev,
  60					struct device_attribute *attr,
  61					char *buf)
  62{
  63	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  64	int bridged_mode = 0;
  65
  66	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
  67		bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
  68
  69	return sprintf(buf, "%d\n", bridged_mode);
  70}
  71
  72static ssize_t qlcnic_store_diag_mode(struct device *dev,
  73				      struct device_attribute *attr,
  74				      const char *buf, size_t len)
  75{
  76	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  77	unsigned long new;
  78
  79	if (kstrtoul(buf, 2, &new))
  80		return -EINVAL;
  81
  82	if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
  83		adapter->flags ^= QLCNIC_DIAG_ENABLED;
  84
  85	return len;
  86}
  87
  88static ssize_t qlcnic_show_diag_mode(struct device *dev,
  89				     struct device_attribute *attr, char *buf)
  90{
  91	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  92	return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
  93}
  94
  95static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
  96				  u8 *state, u8 *rate)
  97{
  98	*rate = LSB(beacon);
  99	*state = MSB(beacon);
 100
 101	QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
 102
 103	if (!*state) {
 104		*rate = __QLCNIC_MAX_LED_RATE;
 105		return 0;
 106	} else if (*state > __QLCNIC_MAX_LED_STATE) {
 107		return -EINVAL;
 108	}
 109
 110	if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
 111		return -EINVAL;
 112
 113	return 0;
 114}
 115
 116static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
 117				    const char *buf, size_t len)
 118{
 119	struct qlcnic_hardware_context *ahw = adapter->ahw;
 120	unsigned long h_beacon;
 121	int err;
 122
 123	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
 124		return -EIO;
 125
 126	if (kstrtoul(buf, 2, &h_beacon))
 127		return -EINVAL;
 128
 129	qlcnic_get_beacon_state(adapter);
 130
 131	if (ahw->beacon_state == h_beacon)
 132		return len;
 133
 134	rtnl_lock();
 135	if (!ahw->beacon_state) {
 136		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
 137			rtnl_unlock();
 138			return -EBUSY;
 139		}
 140	}
 141
 142	if (h_beacon)
 143		err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
 144	else
 145		err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
 146	if (!err)
 147		ahw->beacon_state = h_beacon;
 148
 149	if (!ahw->beacon_state)
 150		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
 151
 152	rtnl_unlock();
 153	return len;
 154}
 155
 156static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
 157				    const char *buf, size_t len)
 158{
 159	struct qlcnic_hardware_context *ahw = adapter->ahw;
 160	int err, drv_sds_rings = adapter->drv_sds_rings;
 161	u16 beacon;
 162	u8 b_state, b_rate;
 163
 164	if (len != sizeof(u16))
 165		return QL_STATUS_INVALID_PARAM;
 166
 167	memcpy(&beacon, buf, sizeof(u16));
 168	err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
 169	if (err)
 170		return err;
 171
 172	qlcnic_get_beacon_state(adapter);
 173
 174	if (ahw->beacon_state == b_state)
 175		return len;
 176
 177	rtnl_lock();
 178	if (!ahw->beacon_state) {
 179		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
 180			rtnl_unlock();
 181			return -EBUSY;
 182		}
 183	}
 184
 185	if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
 186		err = -EIO;
 187		goto out;
 188	}
 189
 190	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 191		err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
 192		if (err)
 193			goto out;
 194		set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
 195	}
 196
 197	err = qlcnic_config_led(adapter, b_state, b_rate);
 198	if (!err) {
 199		err = len;
 200		ahw->beacon_state = b_state;
 201	}
 202
 203	if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
 204		qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
 205
 206out:
 207	if (!ahw->beacon_state)
 208		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
 209	rtnl_unlock();
 210
 211	return err;
 212}
 213
 214static ssize_t qlcnic_store_beacon(struct device *dev,
 215				   struct device_attribute *attr,
 216				   const char *buf, size_t len)
 217{
 218	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 219	int err = 0;
 220
 221	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
 222		dev_warn(dev,
 223			 "LED test not supported in non privileged mode\n");
 224		return -EOPNOTSUPP;
 225	}
 226
 227	if (qlcnic_82xx_check(adapter))
 228		err = qlcnic_82xx_store_beacon(adapter, buf, len);
 229	else if (qlcnic_83xx_check(adapter))
 230		err = qlcnic_83xx_store_beacon(adapter, buf, len);
 231	else
 232		return -EIO;
 233
 234	return err;
 235}
 236
 237static ssize_t qlcnic_show_beacon(struct device *dev,
 238				  struct device_attribute *attr, char *buf)
 239{
 240	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 241
 242	return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
 243}
 244
 245static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
 246				     loff_t offset, size_t size)
 247{
 248	size_t crb_size = 4;
 249
 250	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
 251		return -EIO;
 252
 253	if (offset < QLCNIC_PCI_CRBSPACE) {
 254		if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
 255				  QLCNIC_PCI_CAMQM_END))
 256			crb_size = 8;
 257		else
 258			return -EINVAL;
 259	}
 260
 261	if ((size != crb_size) || (offset & (crb_size-1)))
 262		return  -EINVAL;
 263
 264	return 0;
 265}
 266
 267static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
 268				     struct bin_attribute *attr, char *buf,
 269				     loff_t offset, size_t size)
 270{
 271	struct device *dev = container_of(kobj, struct device, kobj);
 272	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 273	int ret;
 274
 275	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
 276	if (ret != 0)
 277		return ret;
 278	qlcnic_read_crb(adapter, buf, offset, size);
 
 279
 280	return size;
 281}
 282
 283static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
 284				      struct bin_attribute *attr, char *buf,
 285				      loff_t offset, size_t size)
 286{
 287	struct device *dev = container_of(kobj, struct device, kobj);
 288	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 289	int ret;
 290
 291	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
 292	if (ret != 0)
 293		return ret;
 294
 
 295	qlcnic_write_crb(adapter, buf, offset, size);
 296	return size;
 297}
 298
 299static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
 300				     loff_t offset, size_t size)
 301{
 302	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
 303		return -EIO;
 304
 305	if ((size != 8) || (offset & 0x7))
 306		return  -EIO;
 307
 308	return 0;
 309}
 310
 311static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
 312				     struct bin_attribute *attr, char *buf,
 313				     loff_t offset, size_t size)
 314{
 315	struct device *dev = container_of(kobj, struct device, kobj);
 316	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 317	u64 data;
 318	int ret;
 319
 320	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
 321	if (ret != 0)
 322		return ret;
 323
 324	if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
 325		return -EIO;
 326
 327	memcpy(buf, &data, size);
 
 328
 329	return size;
 330}
 331
 332static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
 333				      struct bin_attribute *attr, char *buf,
 334				      loff_t offset, size_t size)
 335{
 336	struct device *dev = container_of(kobj, struct device, kobj);
 337	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 338	u64 data;
 339	int ret;
 340
 341	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
 342	if (ret != 0)
 343		return ret;
 344
 
 345	memcpy(&data, buf, size);
 346
 347	if (qlcnic_pci_mem_write_2M(adapter, offset, data))
 348		return -EIO;
 349
 350	return size;
 351}
 352
 353int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
 354{
 355	int i;
 356
 357	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 358		if (adapter->npars[i].pci_func == pci_func)
 359			return i;
 360	}
 
 
 361	return -EINVAL;
 362}
 363
 364static int validate_pm_config(struct qlcnic_adapter *adapter,
 365			      struct qlcnic_pm_func_cfg *pm_cfg, int count)
 366{
 367	u8 src_pci_func, s_esw_id, d_esw_id;
 368	u8 dest_pci_func;
 369	int i, src_index, dest_index;
 370
 371	for (i = 0; i < count; i++) {
 372		src_pci_func = pm_cfg[i].pci_func;
 373		dest_pci_func = pm_cfg[i].dest_npar;
 374		src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
 375		if (src_index < 0)
 376			return QL_STATUS_INVALID_PARAM;
 377
 378		dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
 379		if (dest_index < 0)
 380			return QL_STATUS_INVALID_PARAM;
 381
 382		s_esw_id = adapter->npars[src_index].phy_port;
 383		d_esw_id = adapter->npars[dest_index].phy_port;
 384
 385		if (s_esw_id != d_esw_id)
 386			return QL_STATUS_INVALID_PARAM;
 387	}
 388
 389	return 0;
 390}
 391
 392static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
 393					    struct kobject *kobj,
 394					    struct bin_attribute *attr,
 395					    char *buf, loff_t offset,
 396					    size_t size)
 397{
 398	struct device *dev = container_of(kobj, struct device, kobj);
 399	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 400	struct qlcnic_pm_func_cfg *pm_cfg;
 401	u32 id, action, pci_func;
 402	int count, rem, i, ret, index;
 403
 404	count	= size / sizeof(struct qlcnic_pm_func_cfg);
 405	rem	= size % sizeof(struct qlcnic_pm_func_cfg);
 406	if (rem)
 407		return QL_STATUS_INVALID_PARAM;
 408
 
 409	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
 410	ret = validate_pm_config(adapter, pm_cfg, count);
 411
 412	if (ret)
 413		return ret;
 414	for (i = 0; i < count; i++) {
 415		pci_func = pm_cfg[i].pci_func;
 416		action = !!pm_cfg[i].action;
 417		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 418		if (index < 0)
 419			return QL_STATUS_INVALID_PARAM;
 420
 421		id = adapter->npars[index].phy_port;
 422		ret = qlcnic_config_port_mirroring(adapter, id,
 423						   action, pci_func);
 424		if (ret)
 425			return ret;
 426	}
 427
 428	for (i = 0; i < count; i++) {
 429		pci_func = pm_cfg[i].pci_func;
 430		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 431		if (index < 0)
 432			return QL_STATUS_INVALID_PARAM;
 433		id = adapter->npars[index].phy_port;
 434		adapter->npars[index].enable_pm = !!pm_cfg[i].action;
 435		adapter->npars[index].dest_npar = id;
 436	}
 437
 438	return size;
 439}
 440
 441static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
 442					   struct kobject *kobj,
 443					   struct bin_attribute *attr,
 444					   char *buf, loff_t offset,
 445					   size_t size)
 446{
 447	struct device *dev = container_of(kobj, struct device, kobj);
 448	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 449	struct qlcnic_pm_func_cfg *pm_cfg;
 450	u8 pci_func;
 451	u32 count;
 452	int i;
 453
 454	memset(buf, 0, size);
 455	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
 456	count = size / sizeof(struct qlcnic_pm_func_cfg);
 457	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 458		pci_func = adapter->npars[i].pci_func;
 459		if (pci_func >= count) {
 460			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
 461				__func__, adapter->ahw->total_nic_func, count);
 462			continue;
 463		}
 464		if (!adapter->npars[i].eswitch_status)
 465			continue;
 466
 467		pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
 468		pm_cfg[pci_func].dest_npar = 0;
 469		pm_cfg[pci_func].pci_func = i;
 470	}
 
 471	return size;
 472}
 473
 474static int validate_esw_config(struct qlcnic_adapter *adapter,
 475			       struct qlcnic_esw_func_cfg *esw_cfg, int count)
 476{
 477	struct qlcnic_hardware_context *ahw = adapter->ahw;
 478	int i, ret;
 479	u32 op_mode;
 480	u8 pci_func;
 481
 482	if (qlcnic_82xx_check(adapter))
 483		op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
 484	else
 485		op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
 486
 487	for (i = 0; i < count; i++) {
 488		pci_func = esw_cfg[i].pci_func;
 489		if (pci_func >= ahw->max_vnic_func)
 490			return QL_STATUS_INVALID_PARAM;
 491
 492		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
 493			if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
 494				return QL_STATUS_INVALID_PARAM;
 495
 496		switch (esw_cfg[i].op_mode) {
 497		case QLCNIC_PORT_DEFAULTS:
 498			if (qlcnic_82xx_check(adapter)) {
 499				ret = QLC_DEV_GET_DRV(op_mode, pci_func);
 500			} else {
 501				ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
 502								  pci_func);
 503				esw_cfg[i].offload_flags = 0;
 504			}
 505
 506			if (ret != QLCNIC_NON_PRIV_FUNC) {
 507				if (esw_cfg[i].mac_anti_spoof != 0)
 508					return QL_STATUS_INVALID_PARAM;
 509				if (esw_cfg[i].mac_override != 1)
 510					return QL_STATUS_INVALID_PARAM;
 511				if (esw_cfg[i].promisc_mode != 1)
 512					return QL_STATUS_INVALID_PARAM;
 513			}
 514			break;
 515		case QLCNIC_ADD_VLAN:
 516			if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
 517				return QL_STATUS_INVALID_PARAM;
 518			if (!esw_cfg[i].op_type)
 519				return QL_STATUS_INVALID_PARAM;
 520			break;
 521		case QLCNIC_DEL_VLAN:
 522			if (!esw_cfg[i].op_type)
 523				return QL_STATUS_INVALID_PARAM;
 524			break;
 525		default:
 526			return QL_STATUS_INVALID_PARAM;
 527		}
 528	}
 529
 530	return 0;
 531}
 532
 533static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
 534					     struct kobject *kobj,
 535					     struct bin_attribute *attr,
 536					     char *buf, loff_t offset,
 537					     size_t size)
 538{
 539	struct device *dev = container_of(kobj, struct device, kobj);
 540	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 541	struct qlcnic_esw_func_cfg *esw_cfg;
 542	struct qlcnic_npar_info *npar;
 543	int count, rem, i, ret;
 544	int index;
 545	u8 op_mode = 0, pci_func;
 546
 547	count	= size / sizeof(struct qlcnic_esw_func_cfg);
 548	rem	= size % sizeof(struct qlcnic_esw_func_cfg);
 549	if (rem)
 550		return QL_STATUS_INVALID_PARAM;
 551
 
 552	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
 553	ret = validate_esw_config(adapter, esw_cfg, count);
 554	if (ret)
 555		return ret;
 556
 557	for (i = 0; i < count; i++) {
 558		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
 559			if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
 560				return QL_STATUS_INVALID_PARAM;
 561
 562		if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
 563			continue;
 564
 565		op_mode = esw_cfg[i].op_mode;
 566		qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
 567		esw_cfg[i].op_mode = op_mode;
 568		esw_cfg[i].pci_func = adapter->ahw->pci_func;
 569
 570		switch (esw_cfg[i].op_mode) {
 571		case QLCNIC_PORT_DEFAULTS:
 572			qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
 573			rtnl_lock();
 574			qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
 575			rtnl_unlock();
 576			break;
 577		case QLCNIC_ADD_VLAN:
 578			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
 579			break;
 580		case QLCNIC_DEL_VLAN:
 581			esw_cfg[i].vlan_id = 0;
 582			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
 583			break;
 584		}
 585	}
 586
 587	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
 588		goto out;
 589
 590	for (i = 0; i < count; i++) {
 591		pci_func = esw_cfg[i].pci_func;
 592		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 593		if (index < 0)
 594			return QL_STATUS_INVALID_PARAM;
 595		npar = &adapter->npars[index];
 596		switch (esw_cfg[i].op_mode) {
 597		case QLCNIC_PORT_DEFAULTS:
 598			npar->promisc_mode = esw_cfg[i].promisc_mode;
 599			npar->mac_override = esw_cfg[i].mac_override;
 600			npar->offload_flags = esw_cfg[i].offload_flags;
 601			npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
 602			npar->discard_tagged = esw_cfg[i].discard_tagged;
 603			break;
 604		case QLCNIC_ADD_VLAN:
 605			npar->pvid = esw_cfg[i].vlan_id;
 606			break;
 607		case QLCNIC_DEL_VLAN:
 608			npar->pvid = 0;
 609			break;
 610		}
 611	}
 612out:
 613	return size;
 614}
 615
 616static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
 617					    struct kobject *kobj,
 618					    struct bin_attribute *attr,
 619					    char *buf, loff_t offset,
 620					    size_t size)
 621{
 622	struct device *dev = container_of(kobj, struct device, kobj);
 623	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 624	struct qlcnic_esw_func_cfg *esw_cfg;
 625	u8 pci_func;
 626	u32 count;
 627	int i;
 628
 629	memset(buf, 0, size);
 630	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
 631	count = size / sizeof(struct qlcnic_esw_func_cfg);
 632	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 633		pci_func = adapter->npars[i].pci_func;
 634		if (pci_func >= count) {
 635			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
 636				__func__, adapter->ahw->total_nic_func, count);
 637			continue;
 638		}
 639		if (!adapter->npars[i].eswitch_status)
 640			continue;
 641
 642		esw_cfg[pci_func].pci_func = pci_func;
 643		if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
 644			return QL_STATUS_INVALID_PARAM;
 645	}
 
 646	return size;
 647}
 648
 649static int validate_npar_config(struct qlcnic_adapter *adapter,
 650				struct qlcnic_npar_func_cfg *np_cfg,
 651				int count)
 652{
 653	u8 pci_func, i;
 654
 655	for (i = 0; i < count; i++) {
 656		pci_func = np_cfg[i].pci_func;
 657		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
 658			return QL_STATUS_INVALID_PARAM;
 659
 660		if (!IS_VALID_BW(np_cfg[i].min_bw) ||
 661		    !IS_VALID_BW(np_cfg[i].max_bw))
 662			return QL_STATUS_INVALID_PARAM;
 663	}
 664	return 0;
 665}
 666
 667static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
 668					      struct kobject *kobj,
 669					      struct bin_attribute *attr,
 670					      char *buf, loff_t offset,
 671					      size_t size)
 672{
 673	struct device *dev = container_of(kobj, struct device, kobj);
 674	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 675	struct qlcnic_info nic_info;
 676	struct qlcnic_npar_func_cfg *np_cfg;
 677	int i, count, rem, ret, index;
 678	u8 pci_func;
 679
 680	count	= size / sizeof(struct qlcnic_npar_func_cfg);
 681	rem	= size % sizeof(struct qlcnic_npar_func_cfg);
 682	if (rem)
 683		return QL_STATUS_INVALID_PARAM;
 684
 
 685	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
 686	ret = validate_npar_config(adapter, np_cfg, count);
 687	if (ret)
 688		return ret;
 689
 690	for (i = 0; i < count; i++) {
 691		pci_func = np_cfg[i].pci_func;
 692
 693		memset(&nic_info, 0, sizeof(struct qlcnic_info));
 694		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
 695		if (ret)
 696			return ret;
 697		nic_info.pci_func = pci_func;
 698		nic_info.min_tx_bw = np_cfg[i].min_bw;
 699		nic_info.max_tx_bw = np_cfg[i].max_bw;
 700		ret = qlcnic_set_nic_info(adapter, &nic_info);
 701		if (ret)
 702			return ret;
 703		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 704		if (index < 0)
 705			return QL_STATUS_INVALID_PARAM;
 706		adapter->npars[index].min_bw = nic_info.min_tx_bw;
 707		adapter->npars[index].max_bw = nic_info.max_tx_bw;
 708	}
 709
 710	return size;
 711}
 712
 713static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
 714					     struct kobject *kobj,
 715					     struct bin_attribute *attr,
 716					     char *buf, loff_t offset,
 717					     size_t size)
 718{
 719	struct device *dev = container_of(kobj, struct device, kobj);
 720	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 721	struct qlcnic_npar_func_cfg *np_cfg;
 722	struct qlcnic_info nic_info;
 723	u8 pci_func;
 724	int i, ret;
 725	u32 count;
 726
 727	memset(&nic_info, 0, sizeof(struct qlcnic_info));
 728	memset(buf, 0, size);
 729	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
 730
 731	count = size / sizeof(struct qlcnic_npar_func_cfg);
 732	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 733		if (adapter->npars[i].pci_func >= count) {
 734			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
 735				__func__, adapter->ahw->total_nic_func, count);
 736			continue;
 737		}
 738		if (!adapter->npars[i].eswitch_status)
 739			continue;
 740		pci_func = adapter->npars[i].pci_func;
 741		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
 742			continue;
 743		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
 744		if (ret)
 745			return ret;
 746
 747		np_cfg[pci_func].pci_func = pci_func;
 748		np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
 749		np_cfg[pci_func].port_num = nic_info.phys_port;
 750		np_cfg[pci_func].fw_capab = nic_info.capabilities;
 751		np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
 752		np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
 753		np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
 754		np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
 755	}
 
 756	return size;
 757}
 758
 759static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
 760					   struct kobject *kobj,
 761					   struct bin_attribute *attr,
 762					   char *buf, loff_t offset,
 763					   size_t size)
 764{
 765	struct device *dev = container_of(kobj, struct device, kobj);
 766	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 767	struct qlcnic_esw_statistics port_stats;
 768	int ret;
 769
 770	if (qlcnic_83xx_check(adapter))
 771		return QLC_STATUS_UNSUPPORTED_CMD;
 772
 773	if (size != sizeof(struct qlcnic_esw_statistics))
 774		return QL_STATUS_INVALID_PARAM;
 775
 776	if (offset >= adapter->ahw->max_vnic_func)
 777		return QL_STATUS_INVALID_PARAM;
 778
 779	memset(&port_stats, 0, size);
 780	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
 781				    &port_stats.rx);
 782	if (ret)
 783		return ret;
 784
 785	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
 786				    &port_stats.tx);
 787	if (ret)
 788		return ret;
 789
 790	memcpy(buf, &port_stats, size);
 791	return size;
 792}
 793
 794static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
 795					  struct kobject *kobj,
 796					  struct bin_attribute *attr,
 797					  char *buf, loff_t offset,
 798					  size_t size)
 799{
 800	struct device *dev = container_of(kobj, struct device, kobj);
 801	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 802	struct qlcnic_esw_statistics esw_stats;
 803	int ret;
 804
 805	if (qlcnic_83xx_check(adapter))
 806		return QLC_STATUS_UNSUPPORTED_CMD;
 807
 808	if (size != sizeof(struct qlcnic_esw_statistics))
 809		return QL_STATUS_INVALID_PARAM;
 810
 811	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
 812		return QL_STATUS_INVALID_PARAM;
 813
 814	memset(&esw_stats, 0, size);
 815	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
 816				       &esw_stats.rx);
 817	if (ret)
 818		return ret;
 819
 820	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
 821				       &esw_stats.tx);
 822	if (ret)
 823		return ret;
 824
 825	memcpy(buf, &esw_stats, size);
 826	return size;
 827}
 828
 829static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
 830					    struct kobject *kobj,
 831					    struct bin_attribute *attr,
 832					    char *buf, loff_t offset,
 833					    size_t size)
 834{
 835	struct device *dev = container_of(kobj, struct device, kobj);
 836	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 837	int ret;
 838
 839	if (qlcnic_83xx_check(adapter))
 840		return QLC_STATUS_UNSUPPORTED_CMD;
 841
 842	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
 843		return QL_STATUS_INVALID_PARAM;
 844
 845	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
 846				     QLCNIC_QUERY_RX_COUNTER);
 847	if (ret)
 848		return ret;
 849
 850	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
 851				     QLCNIC_QUERY_TX_COUNTER);
 852	if (ret)
 853		return ret;
 854
 855	return size;
 856}
 857
 858static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
 859					     struct kobject *kobj,
 860					     struct bin_attribute *attr,
 861					     char *buf, loff_t offset,
 862					     size_t size)
 863{
 864
 865	struct device *dev = container_of(kobj, struct device, kobj);
 866	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 867	int ret;
 868
 869	if (qlcnic_83xx_check(adapter))
 870		return QLC_STATUS_UNSUPPORTED_CMD;
 871
 872	if (offset >= adapter->ahw->max_vnic_func)
 873		return QL_STATUS_INVALID_PARAM;
 874
 875	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
 876				     QLCNIC_QUERY_RX_COUNTER);
 877	if (ret)
 878		return ret;
 879
 880	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
 881				     QLCNIC_QUERY_TX_COUNTER);
 882	if (ret)
 883		return ret;
 884
 885	return size;
 886}
 887
 888static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
 889					    struct kobject *kobj,
 890					    struct bin_attribute *attr,
 891					    char *buf, loff_t offset,
 892					    size_t size)
 893{
 894	struct device *dev = container_of(kobj, struct device, kobj);
 895	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 896	struct qlcnic_pci_func_cfg *pci_cfg;
 897	struct qlcnic_pci_info *pci_info;
 898	int i, ret;
 899	u32 count;
 900
 901	pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
 902	if (!pci_info)
 903		return -ENOMEM;
 904
 905	ret = qlcnic_get_pci_info(adapter, pci_info);
 906	if (ret) {
 907		kfree(pci_info);
 908		return ret;
 909	}
 910
 911	pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
 912	count = size / sizeof(struct qlcnic_pci_func_cfg);
 
 913	for (i = 0; i < count; i++) {
 914		pci_cfg[i].pci_func = pci_info[i].id;
 915		pci_cfg[i].func_type = pci_info[i].type;
 916		pci_cfg[i].func_state = 0;
 917		pci_cfg[i].port_num = pci_info[i].default_port;
 918		pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
 919		pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
 920		memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
 921	}
 922
 923	kfree(pci_info);
 924	return size;
 925}
 926
 927static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
 928						    struct kobject *kobj,
 929						    struct bin_attribute *attr,
 930						    char *buf, loff_t offset,
 931						    size_t size)
 932{
 933	unsigned char *p_read_buf;
 934	int  ret, count;
 935	struct device *dev = container_of(kobj, struct device, kobj);
 936	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 937
 938	if (!size)
 939		return QL_STATUS_INVALID_PARAM;
 940	if (!buf)
 941		return QL_STATUS_INVALID_PARAM;
 942
 943	count = size / sizeof(u32);
 944
 945	if (size % sizeof(u32))
 946		count++;
 947
 948	p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
 949	if (!p_read_buf)
 950		return -ENOMEM;
 951	if (qlcnic_83xx_lock_flash(adapter) != 0) {
 952		kfree(p_read_buf);
 953		return -EIO;
 954	}
 955
 956	ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
 957						count);
 958
 959	if (ret) {
 960		qlcnic_83xx_unlock_flash(adapter);
 961		kfree(p_read_buf);
 962		return ret;
 963	}
 964
 965	qlcnic_83xx_unlock_flash(adapter);
 
 966	memcpy(buf, p_read_buf, size);
 967	kfree(p_read_buf);
 968
 969	return size;
 970}
 971
 972static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
 973					      char *buf, loff_t offset,
 974					      size_t size)
 975{
 976	int  i, ret, count;
 977	unsigned char *p_cache, *p_src;
 978
 979	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
 980	if (!p_cache)
 981		return -ENOMEM;
 982
 
 
 983	memcpy(p_cache, buf, size);
 984	p_src = p_cache;
 985	count = size / sizeof(u32);
 986
 987	if (qlcnic_83xx_lock_flash(adapter) != 0) {
 988		kfree(p_cache);
 989		return -EIO;
 990	}
 991
 992	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
 993		ret = qlcnic_83xx_enable_flash_write(adapter);
 994		if (ret) {
 995			kfree(p_cache);
 996			qlcnic_83xx_unlock_flash(adapter);
 997			return -EIO;
 998		}
 999	}
1000
1001	for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1002		ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1003						   (u32 *)p_src,
1004						   QLC_83XX_FLASH_WRITE_MAX);
1005
1006		if (ret) {
1007			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1008				ret = qlcnic_83xx_disable_flash_write(adapter);
1009				if (ret) {
1010					kfree(p_cache);
1011					qlcnic_83xx_unlock_flash(adapter);
1012					return -EIO;
1013				}
1014			}
1015
1016			kfree(p_cache);
1017			qlcnic_83xx_unlock_flash(adapter);
1018			return -EIO;
1019		}
1020
1021		p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1022		offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1023	}
1024
1025	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1026		ret = qlcnic_83xx_disable_flash_write(adapter);
1027		if (ret) {
1028			kfree(p_cache);
1029			qlcnic_83xx_unlock_flash(adapter);
1030			return -EIO;
1031		}
1032	}
1033
1034	kfree(p_cache);
1035	qlcnic_83xx_unlock_flash(adapter);
1036
1037	return 0;
1038}
1039
1040static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1041					 char *buf, loff_t offset, size_t size)
1042{
1043	int  i, ret, count;
1044	unsigned char *p_cache, *p_src;
1045
1046	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1047	if (!p_cache)
1048		return -ENOMEM;
1049
 
1050	memcpy(p_cache, buf, size);
1051	p_src = p_cache;
1052	count = size / sizeof(u32);
1053
1054	if (qlcnic_83xx_lock_flash(adapter) != 0) {
1055		kfree(p_cache);
1056		return -EIO;
1057	}
1058
1059	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1060		ret = qlcnic_83xx_enable_flash_write(adapter);
1061		if (ret) {
1062			kfree(p_cache);
1063			qlcnic_83xx_unlock_flash(adapter);
1064			return -EIO;
1065		}
1066	}
1067
1068	for (i = 0; i < count; i++) {
1069		ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1070		if (ret) {
1071			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1072				ret = qlcnic_83xx_disable_flash_write(adapter);
1073				if (ret) {
1074					kfree(p_cache);
1075					qlcnic_83xx_unlock_flash(adapter);
1076					return -EIO;
1077				}
1078			}
1079			kfree(p_cache);
1080			qlcnic_83xx_unlock_flash(adapter);
1081			return -EIO;
1082		}
1083
1084		p_src = p_src + sizeof(u32);
1085		offset = offset + sizeof(u32);
1086	}
1087
1088	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1089		ret = qlcnic_83xx_disable_flash_write(adapter);
1090		if (ret) {
1091			kfree(p_cache);
1092			qlcnic_83xx_unlock_flash(adapter);
1093			return -EIO;
1094		}
1095	}
1096
1097	kfree(p_cache);
1098	qlcnic_83xx_unlock_flash(adapter);
1099
1100	return 0;
1101}
1102
1103static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1104						     struct kobject *kobj,
1105						     struct bin_attribute *attr,
1106						     char *buf, loff_t offset,
1107						     size_t size)
1108{
1109	int  ret;
1110	static int flash_mode;
1111	unsigned long data;
1112	struct device *dev = container_of(kobj, struct device, kobj);
1113	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1114
1115	if (!buf)
1116		return QL_STATUS_INVALID_PARAM;
1117
1118	ret = kstrtoul(buf, 16, &data);
1119
1120	switch (data) {
1121	case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1122		flash_mode = QLC_83XX_ERASE_MODE;
1123		ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1124		if (ret) {
1125			dev_err(&adapter->pdev->dev,
1126				"%s failed at %d\n", __func__, __LINE__);
1127			return -EIO;
1128		}
1129		break;
1130
1131	case QLC_83XX_FLASH_BULK_WRITE_CMD:
1132		flash_mode = QLC_83XX_BULK_WRITE_MODE;
1133		break;
1134
1135	case QLC_83XX_FLASH_WRITE_CMD:
1136		flash_mode = QLC_83XX_WRITE_MODE;
1137		break;
1138	default:
1139		if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1140			ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1141								 offset, size);
1142			if (ret) {
1143				dev_err(&adapter->pdev->dev,
1144					"%s failed at %d\n",
1145					__func__, __LINE__);
1146				return -EIO;
1147			}
1148		}
1149
1150		if (flash_mode == QLC_83XX_WRITE_MODE) {
1151			ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1152							    offset, size);
1153			if (ret) {
1154				dev_err(&adapter->pdev->dev,
1155					"%s failed at %d\n", __func__,
1156					__LINE__);
1157				return -EIO;
1158			}
1159		}
1160	}
1161
1162	return size;
1163}
1164
1165static struct device_attribute dev_attr_bridged_mode = {
1166       .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1167       .show = qlcnic_show_bridged_mode,
1168       .store = qlcnic_store_bridged_mode,
1169};
1170
1171static struct device_attribute dev_attr_diag_mode = {
1172	.attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1173	.show = qlcnic_show_diag_mode,
1174	.store = qlcnic_store_diag_mode,
1175};
1176
1177static struct device_attribute dev_attr_beacon = {
1178	.attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1179	.show = qlcnic_show_beacon,
1180	.store = qlcnic_store_beacon,
1181};
1182
1183static struct bin_attribute bin_attr_crb = {
1184	.attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1185	.size = 0,
1186	.read = qlcnic_sysfs_read_crb,
1187	.write = qlcnic_sysfs_write_crb,
1188};
1189
1190static struct bin_attribute bin_attr_mem = {
1191	.attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1192	.size = 0,
1193	.read = qlcnic_sysfs_read_mem,
1194	.write = qlcnic_sysfs_write_mem,
1195};
1196
1197static struct bin_attribute bin_attr_npar_config = {
1198	.attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1199	.size = 0,
1200	.read = qlcnic_sysfs_read_npar_config,
1201	.write = qlcnic_sysfs_write_npar_config,
1202};
1203
1204static struct bin_attribute bin_attr_pci_config = {
1205	.attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1206	.size = 0,
1207	.read = qlcnic_sysfs_read_pci_config,
1208	.write = NULL,
1209};
1210
1211static struct bin_attribute bin_attr_port_stats = {
1212	.attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1213	.size = 0,
1214	.read = qlcnic_sysfs_get_port_stats,
1215	.write = qlcnic_sysfs_clear_port_stats,
1216};
1217
1218static struct bin_attribute bin_attr_esw_stats = {
1219	.attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1220	.size = 0,
1221	.read = qlcnic_sysfs_get_esw_stats,
1222	.write = qlcnic_sysfs_clear_esw_stats,
1223};
1224
1225static struct bin_attribute bin_attr_esw_config = {
1226	.attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1227	.size = 0,
1228	.read = qlcnic_sysfs_read_esw_config,
1229	.write = qlcnic_sysfs_write_esw_config,
1230};
1231
1232static struct bin_attribute bin_attr_pm_config = {
1233	.attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1234	.size = 0,
1235	.read = qlcnic_sysfs_read_pm_config,
1236	.write = qlcnic_sysfs_write_pm_config,
1237};
1238
1239static struct bin_attribute bin_attr_flash = {
1240	.attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1241	.size = 0,
1242	.read = qlcnic_83xx_sysfs_flash_read_handler,
1243	.write = qlcnic_83xx_sysfs_flash_write_handler,
1244};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1245
1246void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1247{
1248	struct device *dev = &adapter->pdev->dev;
1249
1250	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1251		if (device_create_file(dev, &dev_attr_bridged_mode))
1252			dev_warn(dev,
1253				 "failed to create bridged_mode sysfs entry\n");
1254}
1255
1256void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1257{
1258	struct device *dev = &adapter->pdev->dev;
1259
1260	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1261		device_remove_file(dev, &dev_attr_bridged_mode);
1262}
1263
1264static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1265{
1266	struct device *dev = &adapter->pdev->dev;
1267
1268	if (device_create_bin_file(dev, &bin_attr_port_stats))
1269		dev_info(dev, "failed to create port stats sysfs entry");
1270
1271	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1272		return;
1273	if (device_create_file(dev, &dev_attr_diag_mode))
1274		dev_info(dev, "failed to create diag_mode sysfs entry\n");
1275	if (device_create_bin_file(dev, &bin_attr_crb))
1276		dev_info(dev, "failed to create crb sysfs entry\n");
1277	if (device_create_bin_file(dev, &bin_attr_mem))
1278		dev_info(dev, "failed to create mem sysfs entry\n");
1279
1280	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1281		return;
1282
1283	if (device_create_bin_file(dev, &bin_attr_pci_config))
1284		dev_info(dev, "failed to create pci config sysfs entry");
1285
1286	if (device_create_file(dev, &dev_attr_beacon))
1287		dev_info(dev, "failed to create beacon sysfs entry");
1288
1289	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1290		return;
1291	if (device_create_bin_file(dev, &bin_attr_esw_config))
1292		dev_info(dev, "failed to create esw config sysfs entry");
1293	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1294		return;
1295	if (device_create_bin_file(dev, &bin_attr_npar_config))
1296		dev_info(dev, "failed to create npar config sysfs entry");
1297	if (device_create_bin_file(dev, &bin_attr_pm_config))
1298		dev_info(dev, "failed to create pm config sysfs entry");
1299	if (device_create_bin_file(dev, &bin_attr_esw_stats))
1300		dev_info(dev, "failed to create eswitch stats sysfs entry");
1301}
1302
1303static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1304{
1305	struct device *dev = &adapter->pdev->dev;
1306
1307	device_remove_bin_file(dev, &bin_attr_port_stats);
1308
1309	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1310		return;
1311	device_remove_file(dev, &dev_attr_diag_mode);
1312	device_remove_bin_file(dev, &bin_attr_crb);
1313	device_remove_bin_file(dev, &bin_attr_mem);
1314
1315	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1316		return;
1317
1318	device_remove_bin_file(dev, &bin_attr_pci_config);
1319	device_remove_file(dev, &dev_attr_beacon);
1320	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1321		return;
1322	device_remove_bin_file(dev, &bin_attr_esw_config);
1323	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1324		return;
1325	device_remove_bin_file(dev, &bin_attr_npar_config);
1326	device_remove_bin_file(dev, &bin_attr_pm_config);
1327	device_remove_bin_file(dev, &bin_attr_esw_stats);
1328}
1329
1330void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1331{
1332	qlcnic_create_diag_entries(adapter);
1333}
1334
1335void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1336{
1337	qlcnic_remove_diag_entries(adapter);
1338}
1339
1340void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1341{
1342	struct device *dev = &adapter->pdev->dev;
1343
1344	qlcnic_create_diag_entries(adapter);
1345
1346	if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1347		dev_info(dev, "failed to create flash sysfs entry\n");
1348}
1349
1350void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1351{
1352	struct device *dev = &adapter->pdev->dev;
1353
1354	qlcnic_remove_diag_entries(adapter);
1355	sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);
1356}
   1/*
   2 * QLogic qlcnic NIC Driver
   3 * Copyright (c) 2009-2013 QLogic Corporation
   4 *
   5 * See LICENSE.qlcnic for copyright and licensing details.
   6 */
   7
   8#include <linux/slab.h>
   9#include <linux/interrupt.h>
 
 
 
 
  10#include <linux/swab.h>
  11#include <linux/dma-mapping.h>
  12#include <net/ip.h>
  13#include <linux/ipv6.h>
  14#include <linux/inetdevice.h>
  15#include <linux/sysfs.h>
  16#include <linux/aer.h>
  17#include <linux/log2.h>
  18#ifdef CONFIG_QLCNIC_HWMON
  19#include <linux/hwmon.h>
  20#include <linux/hwmon-sysfs.h>
  21#endif
  22
  23#include "qlcnic.h"
  24#include "qlcnic_hw.h"
  25
  26int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
  27{
  28	return -EOPNOTSUPP;
  29}
  30
  31int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
  32{
  33	return -EOPNOTSUPP;
  34}
  35
  36static ssize_t qlcnic_store_bridged_mode(struct device *dev,
  37					 struct device_attribute *attr,
  38					 const char *buf, size_t len)
  39{
  40	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  41	unsigned long new;
  42	int ret = -EINVAL;
  43
  44	if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
  45		goto err_out;
  46
  47	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
  48		goto err_out;
  49
  50	if (kstrtoul(buf, 2, &new))
  51		goto err_out;
  52
  53	if (!qlcnic_config_bridged_mode(adapter, !!new))
  54		ret = len;
  55
  56err_out:
  57	return ret;
  58}
  59
  60static ssize_t qlcnic_show_bridged_mode(struct device *dev,
  61					struct device_attribute *attr,
  62					char *buf)
  63{
  64	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  65	int bridged_mode = 0;
  66
  67	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
  68		bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
  69
  70	return sprintf(buf, "%d\n", bridged_mode);
  71}
  72
  73static ssize_t qlcnic_store_diag_mode(struct device *dev,
  74				      struct device_attribute *attr,
  75				      const char *buf, size_t len)
  76{
  77	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  78	unsigned long new;
  79
  80	if (kstrtoul(buf, 2, &new))
  81		return -EINVAL;
  82
  83	if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
  84		adapter->flags ^= QLCNIC_DIAG_ENABLED;
  85
  86	return len;
  87}
  88
  89static ssize_t qlcnic_show_diag_mode(struct device *dev,
  90				     struct device_attribute *attr, char *buf)
  91{
  92	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  93	return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
  94}
  95
  96static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
  97				  u8 *state, u8 *rate)
  98{
  99	*rate = LSB(beacon);
 100	*state = MSB(beacon);
 101
 102	QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
 103
 104	if (!*state) {
 105		*rate = __QLCNIC_MAX_LED_RATE;
 106		return 0;
 107	} else if (*state > __QLCNIC_MAX_LED_STATE) {
 108		return -EINVAL;
 109	}
 110
 111	if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
 112		return -EINVAL;
 113
 114	return 0;
 115}
 116
 117static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
 118				    const char *buf, size_t len)
 119{
 120	struct qlcnic_hardware_context *ahw = adapter->ahw;
 121	unsigned long h_beacon;
 122	int err;
 123
 124	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
 125		return -EIO;
 126
 127	if (kstrtoul(buf, 2, &h_beacon))
 128		return -EINVAL;
 129
 130	qlcnic_get_beacon_state(adapter);
 131
 132	if (ahw->beacon_state == h_beacon)
 133		return len;
 134
 135	rtnl_lock();
 136	if (!ahw->beacon_state) {
 137		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
 138			rtnl_unlock();
 139			return -EBUSY;
 140		}
 141	}
 142
 143	if (h_beacon)
 144		err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
 145	else
 146		err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
 147	if (!err)
 148		ahw->beacon_state = h_beacon;
 149
 150	if (!ahw->beacon_state)
 151		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
 152
 153	rtnl_unlock();
 154	return len;
 155}
 156
 157static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
 158				    const char *buf, size_t len)
 159{
 160	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	memcpy(&beacon, buf, sizeof(u16));
 169	err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
 170	if (err)
 171		return err;
 172
 173	qlcnic_get_beacon_state(adapter);
 174
 175	if (ahw->beacon_state == b_state)
 176		return len;
 177
 178	rtnl_lock();
 179	if (!ahw->beacon_state) {
 180		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
 181			rtnl_unlock();
 182			return -EBUSY;
 183		}
 184	}
 185
 186	if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
 187		err = -EIO;
 188		goto out;
 189	}
 190
 191	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 192		err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
 193		if (err)
 194			goto out;
 195		set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
 196	}
 197
 198	err = qlcnic_config_led(adapter, b_state, b_rate);
 199	if (!err) {
 200		err = len;
 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
 207out:
 208	if (!ahw->beacon_state)
 209		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
 210	rtnl_unlock();
 211
 212	return err;
 213}
 214
 215static ssize_t qlcnic_store_beacon(struct device *dev,
 216				   struct device_attribute *attr,
 217				   const char *buf, size_t len)
 218{
 219	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 220	int err = 0;
 221
 222	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
 223		dev_warn(dev,
 224			 "LED test not supported in non privileged mode\n");
 225		return -EOPNOTSUPP;
 226	}
 227
 228	if (qlcnic_82xx_check(adapter))
 229		err = qlcnic_82xx_store_beacon(adapter, buf, len);
 230	else if (qlcnic_83xx_check(adapter))
 231		err = qlcnic_83xx_store_beacon(adapter, buf, len);
 232	else
 233		return -EIO;
 234
 235	return err;
 236}
 237
 238static ssize_t qlcnic_show_beacon(struct device *dev,
 239				  struct device_attribute *attr, char *buf)
 240{
 241	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 242
 243	return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
 244}
 245
 246static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
 247				     loff_t offset, size_t size)
 248{
 249	size_t crb_size = 4;
 250
 251	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
 252		return -EIO;
 253
 254	if (offset < QLCNIC_PCI_CRBSPACE) {
 255		if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
 256				  QLCNIC_PCI_CAMQM_END))
 257			crb_size = 8;
 258		else
 259			return -EINVAL;
 260	}
 261
 262	if ((size != crb_size) || (offset & (crb_size-1)))
 263		return  -EINVAL;
 264
 265	return 0;
 266}
 267
 268static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
 269				     struct bin_attribute *attr, char *buf,
 270				     loff_t offset, size_t size)
 271{
 272	struct device *dev = container_of(kobj, struct device, kobj);
 273	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 274	int ret;
 275
 276	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
 277	if (ret != 0)
 278		return ret;
 279	qlcnic_read_crb(adapter, buf, offset, size);
 280	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
 281
 282	return size;
 283}
 284
 285static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
 286				      struct bin_attribute *attr, char *buf,
 287				      loff_t offset, size_t size)
 288{
 289	struct device *dev = container_of(kobj, struct device, kobj);
 290	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 291	int ret;
 292
 293	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
 294	if (ret != 0)
 295		return ret;
 296
 297	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
 298	qlcnic_write_crb(adapter, buf, offset, size);
 299	return size;
 300}
 301
 302static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
 303				     loff_t offset, size_t size)
 304{
 305	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
 306		return -EIO;
 307
 308	if ((size != 8) || (offset & 0x7))
 309		return  -EIO;
 310
 311	return 0;
 312}
 313
 314static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
 315				     struct bin_attribute *attr, char *buf,
 316				     loff_t offset, size_t size)
 317{
 318	struct device *dev = container_of(kobj, struct device, kobj);
 319	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 320	u64 data;
 321	int ret;
 322
 323	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
 324	if (ret != 0)
 325		return ret;
 326
 327	if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
 328		return -EIO;
 329
 330	memcpy(buf, &data, size);
 331	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
 332
 333	return size;
 334}
 335
 336static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
 337				      struct bin_attribute *attr, char *buf,
 338				      loff_t offset, size_t size)
 339{
 340	struct device *dev = container_of(kobj, struct device, kobj);
 341	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 342	u64 data;
 343	int ret;
 344
 345	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
 346	if (ret != 0)
 347		return ret;
 348
 349	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
 350	memcpy(&data, buf, size);
 351
 352	if (qlcnic_pci_mem_write_2M(adapter, offset, data))
 353		return -EIO;
 354
 355	return size;
 356}
 357
 358int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
 359{
 360	int i;
 361
 362	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 363		if (adapter->npars[i].pci_func == pci_func)
 364			return i;
 365	}
 366
 367	dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
 368	return -EINVAL;
 369}
 370
 371static int validate_pm_config(struct qlcnic_adapter *adapter,
 372			      struct qlcnic_pm_func_cfg *pm_cfg, int count)
 373{
 374	u8 src_pci_func, s_esw_id, d_esw_id;
 375	u8 dest_pci_func;
 376	int i, src_index, dest_index;
 377
 378	for (i = 0; i < count; i++) {
 379		src_pci_func = pm_cfg[i].pci_func;
 380		dest_pci_func = pm_cfg[i].dest_npar;
 381		src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
 382		if (src_index < 0)
 383			return -EINVAL;
 384
 385		dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
 386		if (dest_index < 0)
 387			return -EINVAL;
 388
 389		s_esw_id = adapter->npars[src_index].phy_port;
 390		d_esw_id = adapter->npars[dest_index].phy_port;
 391
 392		if (s_esw_id != d_esw_id)
 393			return -EINVAL;
 394	}
 395
 396	return 0;
 397}
 398
 399static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
 400					    struct kobject *kobj,
 401					    struct bin_attribute *attr,
 402					    char *buf, loff_t offset,
 403					    size_t size)
 404{
 405	struct device *dev = container_of(kobj, struct device, kobj);
 406	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 407	struct qlcnic_pm_func_cfg *pm_cfg;
 408	u32 id, action, pci_func;
 409	int count, rem, i, ret, index;
 410
 411	count	= size / sizeof(struct qlcnic_pm_func_cfg);
 412	rem	= size % sizeof(struct qlcnic_pm_func_cfg);
 413	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);
 419
 420	if (ret)
 421		return ret;
 422	for (i = 0; i < count; i++) {
 423		pci_func = pm_cfg[i].pci_func;
 424		action = !!pm_cfg[i].action;
 425		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 426		if (index < 0)
 427			return -EINVAL;
 428
 429		id = adapter->npars[index].phy_port;
 430		ret = qlcnic_config_port_mirroring(adapter, id,
 431						   action, pci_func);
 432		if (ret)
 433			return ret;
 434	}
 435
 436	for (i = 0; i < count; i++) {
 437		pci_func = pm_cfg[i].pci_func;
 438		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 439		if (index < 0)
 440			return -EINVAL;
 441		id = adapter->npars[index].phy_port;
 442		adapter->npars[index].enable_pm = !!pm_cfg[i].action;
 443		adapter->npars[index].dest_npar = id;
 444	}
 445
 446	return size;
 447}
 448
 449static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
 450					   struct kobject *kobj,
 451					   struct bin_attribute *attr,
 452					   char *buf, loff_t offset,
 453					   size_t size)
 454{
 455	struct device *dev = container_of(kobj, struct device, kobj);
 456	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 457	struct qlcnic_pm_func_cfg *pm_cfg;
 458	u8 pci_func;
 459	u32 count;
 460	int i;
 461
 462	memset(buf, 0, size);
 463	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
 464	count = size / sizeof(struct qlcnic_pm_func_cfg);
 465	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 466		pci_func = adapter->npars[i].pci_func;
 467		if (pci_func >= count) {
 468			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
 469				__func__, adapter->ahw->total_nic_func, count);
 470			continue;
 471		}
 472		if (!adapter->npars[i].eswitch_status)
 473			continue;
 474
 475		pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
 476		pm_cfg[pci_func].dest_npar = 0;
 477		pm_cfg[pci_func].pci_func = i;
 478	}
 479	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
 480	return size;
 481}
 482
 483static int validate_esw_config(struct qlcnic_adapter *adapter,
 484			       struct qlcnic_esw_func_cfg *esw_cfg, int count)
 485{
 486	struct qlcnic_hardware_context *ahw = adapter->ahw;
 487	int i, ret;
 488	u32 op_mode;
 489	u8 pci_func;
 490
 491	if (qlcnic_82xx_check(adapter))
 492		op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
 493	else
 494		op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
 495
 496	for (i = 0; i < count; i++) {
 497		pci_func = esw_cfg[i].pci_func;
 498		if (pci_func >= ahw->max_vnic_func)
 499			return -EINVAL;
 500
 501		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
 502			if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
 503				return -EINVAL;
 504
 505		switch (esw_cfg[i].op_mode) {
 506		case QLCNIC_PORT_DEFAULTS:
 507			if (qlcnic_82xx_check(adapter)) {
 508				ret = QLC_DEV_GET_DRV(op_mode, pci_func);
 509			} else {
 510				ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
 511								  pci_func);
 512				esw_cfg[i].offload_flags = 0;
 513			}
 514
 515			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			break;
 524		case QLCNIC_ADD_VLAN:
 525			if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
 526				return -EINVAL;
 527			if (!esw_cfg[i].op_type)
 528				return -EINVAL;
 529			break;
 530		case QLCNIC_DEL_VLAN:
 531			if (!esw_cfg[i].op_type)
 532				return -EINVAL;
 533			break;
 534		default:
 535			return -EINVAL;
 536		}
 537	}
 538
 539	return 0;
 540}
 541
 542static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
 543					     struct kobject *kobj,
 544					     struct bin_attribute *attr,
 545					     char *buf, loff_t offset,
 546					     size_t size)
 547{
 548	struct device *dev = container_of(kobj, struct device, kobj);
 549	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 550	struct qlcnic_esw_func_cfg *esw_cfg;
 551	struct qlcnic_npar_info *npar;
 552	int count, rem, i, ret;
 553	int index;
 554	u8 op_mode = 0, pci_func;
 555
 556	count	= size / sizeof(struct qlcnic_esw_func_cfg);
 557	rem	= size % sizeof(struct qlcnic_esw_func_cfg);
 558	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	ret = validate_esw_config(adapter, esw_cfg, count);
 564	if (ret)
 565		return ret;
 566
 567	for (i = 0; i < count; i++) {
 568		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
 569			if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
 570				return -EINVAL;
 571
 572		if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
 573			continue;
 574
 575		op_mode = esw_cfg[i].op_mode;
 576		qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
 577		esw_cfg[i].op_mode = op_mode;
 578		esw_cfg[i].pci_func = adapter->ahw->pci_func;
 579
 580		switch (esw_cfg[i].op_mode) {
 581		case QLCNIC_PORT_DEFAULTS:
 582			qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
 583			rtnl_lock();
 584			qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
 585			rtnl_unlock();
 586			break;
 587		case QLCNIC_ADD_VLAN:
 588			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
 589			break;
 590		case QLCNIC_DEL_VLAN:
 591			esw_cfg[i].vlan_id = 0;
 592			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
 593			break;
 594		}
 595	}
 596
 597	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
 598		goto out;
 599
 600	for (i = 0; i < count; i++) {
 601		pci_func = esw_cfg[i].pci_func;
 602		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 603		if (index < 0)
 604			return -EINVAL;
 605		npar = &adapter->npars[index];
 606		switch (esw_cfg[i].op_mode) {
 607		case QLCNIC_PORT_DEFAULTS:
 608			npar->promisc_mode = esw_cfg[i].promisc_mode;
 609			npar->mac_override = esw_cfg[i].mac_override;
 610			npar->offload_flags = esw_cfg[i].offload_flags;
 611			npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
 612			npar->discard_tagged = esw_cfg[i].discard_tagged;
 613			break;
 614		case QLCNIC_ADD_VLAN:
 615			npar->pvid = esw_cfg[i].vlan_id;
 616			break;
 617		case QLCNIC_DEL_VLAN:
 618			npar->pvid = 0;
 619			break;
 620		}
 621	}
 622out:
 623	return size;
 624}
 625
 626static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
 627					    struct kobject *kobj,
 628					    struct bin_attribute *attr,
 629					    char *buf, loff_t offset,
 630					    size_t size)
 631{
 632	struct device *dev = container_of(kobj, struct device, kobj);
 633	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 634	struct qlcnic_esw_func_cfg *esw_cfg;
 635	u8 pci_func;
 636	u32 count;
 637	int i;
 638
 639	memset(buf, 0, size);
 640	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
 641	count = size / sizeof(struct qlcnic_esw_func_cfg);
 642	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 643		pci_func = adapter->npars[i].pci_func;
 644		if (pci_func >= count) {
 645			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
 646				__func__, adapter->ahw->total_nic_func, count);
 647			continue;
 648		}
 649		if (!adapter->npars[i].eswitch_status)
 650			continue;
 651
 652		esw_cfg[pci_func].pci_func = pci_func;
 653		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	return size;
 658}
 659
 660static int validate_npar_config(struct qlcnic_adapter *adapter,
 661				struct qlcnic_npar_func_cfg *np_cfg,
 662				int count)
 663{
 664	u8 pci_func, i;
 665
 666	for (i = 0; i < count; i++) {
 667		pci_func = np_cfg[i].pci_func;
 668		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
 669			return -EINVAL;
 670
 671		if (!IS_VALID_BW(np_cfg[i].min_bw) ||
 672		    !IS_VALID_BW(np_cfg[i].max_bw))
 673			return -EINVAL;
 674	}
 675	return 0;
 676}
 677
 678static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
 679					      struct kobject *kobj,
 680					      struct bin_attribute *attr,
 681					      char *buf, loff_t offset,
 682					      size_t size)
 683{
 684	struct device *dev = container_of(kobj, struct device, kobj);
 685	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 686	struct qlcnic_info nic_info;
 687	struct qlcnic_npar_func_cfg *np_cfg;
 688	int i, count, rem, ret, index;
 689	u8 pci_func;
 690
 691	count	= size / sizeof(struct qlcnic_npar_func_cfg);
 692	rem	= size % sizeof(struct qlcnic_npar_func_cfg);
 693	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	ret = validate_npar_config(adapter, np_cfg, count);
 699	if (ret)
 700		return ret;
 701
 702	for (i = 0; i < count; i++) {
 703		pci_func = np_cfg[i].pci_func;
 704
 705		memset(&nic_info, 0, sizeof(struct qlcnic_info));
 706		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
 707		if (ret)
 708			return ret;
 709		nic_info.pci_func = pci_func;
 710		nic_info.min_tx_bw = np_cfg[i].min_bw;
 711		nic_info.max_tx_bw = np_cfg[i].max_bw;
 712		ret = qlcnic_set_nic_info(adapter, &nic_info);
 713		if (ret)
 714			return ret;
 715		index = qlcnic_is_valid_nic_func(adapter, pci_func);
 716		if (index < 0)
 717			return -EINVAL;
 718		adapter->npars[index].min_bw = nic_info.min_tx_bw;
 719		adapter->npars[index].max_bw = nic_info.max_tx_bw;
 720	}
 721
 722	return size;
 723}
 724
 725static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
 726					     struct kobject *kobj,
 727					     struct bin_attribute *attr,
 728					     char *buf, loff_t offset,
 729					     size_t size)
 730{
 731	struct device *dev = container_of(kobj, struct device, kobj);
 732	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
 739	memset(&nic_info, 0, sizeof(struct qlcnic_info));
 740	memset(buf, 0, size);
 741	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
 742
 743	count = size / sizeof(struct qlcnic_npar_func_cfg);
 744	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
 745		if (adapter->npars[i].pci_func >= count) {
 746			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
 747				__func__, adapter->ahw->total_nic_func, count);
 748			continue;
 749		}
 750		if (!adapter->npars[i].eswitch_status)
 751			continue;
 752		pci_func = adapter->npars[i].pci_func;
 753		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
 754			continue;
 755		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
 756		if (ret)
 757			return ret;
 758
 759		np_cfg[pci_func].pci_func = pci_func;
 760		np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
 761		np_cfg[pci_func].port_num = nic_info.phys_port;
 762		np_cfg[pci_func].fw_capab = nic_info.capabilities;
 763		np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
 764		np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
 765		np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
 766		np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
 767	}
 768	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
 769	return size;
 770}
 771
 772static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
 773					   struct kobject *kobj,
 774					   struct bin_attribute *attr,
 775					   char *buf, loff_t offset,
 776					   size_t size)
 777{
 778	struct device *dev = container_of(kobj, struct device, kobj);
 779	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 780	struct qlcnic_esw_statistics port_stats;
 781	int ret;
 782
 783	if (qlcnic_83xx_check(adapter))
 784		return -EOPNOTSUPP;
 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	memset(&port_stats, 0, size);
 793	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
 794				    &port_stats.rx);
 795	if (ret)
 796		return ret;
 797
 798	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
 799				    &port_stats.tx);
 800	if (ret)
 801		return ret;
 802
 803	memcpy(buf, &port_stats, size);
 804	return size;
 805}
 806
 807static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
 808					  struct kobject *kobj,
 809					  struct bin_attribute *attr,
 810					  char *buf, loff_t offset,
 811					  size_t size)
 812{
 813	struct device *dev = container_of(kobj, struct device, kobj);
 814	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 815	struct qlcnic_esw_statistics esw_stats;
 816	int ret;
 817
 818	if (qlcnic_83xx_check(adapter))
 819		return -EOPNOTSUPP;
 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	memset(&esw_stats, 0, size);
 828	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
 829				       &esw_stats.rx);
 830	if (ret)
 831		return ret;
 832
 833	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
 834				       &esw_stats.tx);
 835	if (ret)
 836		return ret;
 837
 838	memcpy(buf, &esw_stats, size);
 839	return size;
 840}
 841
 842static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
 843					    struct kobject *kobj,
 844					    struct bin_attribute *attr,
 845					    char *buf, loff_t offset,
 846					    size_t size)
 847{
 848	struct device *dev = container_of(kobj, struct device, kobj);
 849	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 850	int ret;
 851
 852	if (qlcnic_83xx_check(adapter))
 853		return -EOPNOTSUPP;
 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	if (ret)
 861		return ret;
 862
 863	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
 864				     QLCNIC_QUERY_TX_COUNTER);
 865	if (ret)
 866		return ret;
 867
 868	return size;
 869}
 870
 871static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
 872					     struct kobject *kobj,
 873					     struct bin_attribute *attr,
 874					     char *buf, loff_t offset,
 875					     size_t size)
 876{
 877
 878	struct device *dev = container_of(kobj, struct device, kobj);
 879	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 880	int ret;
 881
 882	if (qlcnic_83xx_check(adapter))
 883		return -EOPNOTSUPP;
 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	if (ret)
 891		return ret;
 892
 893	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
 894				     QLCNIC_QUERY_TX_COUNTER);
 895	if (ret)
 896		return ret;
 897
 898	return size;
 899}
 900
 901static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
 902					    struct kobject *kobj,
 903					    struct bin_attribute *attr,
 904					    char *buf, loff_t offset,
 905					    size_t size)
 906{
 907	struct device *dev = container_of(kobj, struct device, kobj);
 908	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 909	struct qlcnic_pci_func_cfg *pci_cfg;
 910	struct qlcnic_pci_info *pci_info;
 911	int i, ret;
 912	u32 count;
 913
 914	pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
 915	if (!pci_info)
 916		return -ENOMEM;
 917
 918	ret = qlcnic_get_pci_info(adapter, pci_info);
 919	if (ret) {
 920		kfree(pci_info);
 921		return ret;
 922	}
 923
 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		pci_cfg[i].pci_func = pci_info[i].id;
 929		pci_cfg[i].func_type = pci_info[i].type;
 930		pci_cfg[i].func_state = 0;
 931		pci_cfg[i].port_num = pci_info[i].default_port;
 932		pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
 933		pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
 934		memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
 935	}
 936
 937	kfree(pci_info);
 938	return size;
 939}
 940
 941static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
 942						    struct kobject *kobj,
 943						    struct bin_attribute *attr,
 944						    char *buf, loff_t offset,
 945						    size_t size)
 946{
 947	unsigned char *p_read_buf;
 948	int  ret, count;
 949	struct device *dev = container_of(kobj, struct device, kobj);
 950	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
 951
 952	if (!size)
 953		return -EINVAL;
 
 
 954
 955	count = size / sizeof(u32);
 956
 957	if (size % sizeof(u32))
 958		count++;
 959
 960	p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
 961	if (!p_read_buf)
 962		return -ENOMEM;
 963	if (qlcnic_83xx_lock_flash(adapter) != 0) {
 964		kfree(p_read_buf);
 965		return -EIO;
 966	}
 967
 968	ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
 969						count);
 970
 971	if (ret) {
 972		qlcnic_83xx_unlock_flash(adapter);
 973		kfree(p_read_buf);
 974		return ret;
 975	}
 976
 977	qlcnic_83xx_unlock_flash(adapter);
 978	qlcnic_swap32_buffer((u32 *)p_read_buf, count);
 979	memcpy(buf, p_read_buf, size);
 980	kfree(p_read_buf);
 981
 982	return size;
 983}
 984
 985static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
 986					      char *buf, loff_t offset,
 987					      size_t size)
 988{
 989	int  i, ret, count;
 990	unsigned char *p_cache, *p_src;
 991
 992	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
 993	if (!p_cache)
 994		return -ENOMEM;
 995
 996	count = size / sizeof(u32);
 997	qlcnic_swap32_buffer((u32 *)buf, count);
 998	memcpy(p_cache, buf, size);
 999	p_src = p_cache;
 
1000
1001	if (qlcnic_83xx_lock_flash(adapter) != 0) {
1002		kfree(p_cache);
1003		return -EIO;
1004	}
1005
1006	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1007		ret = qlcnic_83xx_enable_flash_write(adapter);
1008		if (ret) {
1009			kfree(p_cache);
1010			qlcnic_83xx_unlock_flash(adapter);
1011			return -EIO;
1012		}
1013	}
1014
1015	for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1016		ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1017						   (u32 *)p_src,
1018						   QLC_83XX_FLASH_WRITE_MAX);
1019
1020		if (ret) {
1021			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1022				ret = qlcnic_83xx_disable_flash_write(adapter);
1023				if (ret) {
1024					kfree(p_cache);
1025					qlcnic_83xx_unlock_flash(adapter);
1026					return -EIO;
1027				}
1028			}
1029
1030			kfree(p_cache);
1031			qlcnic_83xx_unlock_flash(adapter);
1032			return -EIO;
1033		}
1034
1035		p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1036		offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1037	}
1038
1039	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1040		ret = qlcnic_83xx_disable_flash_write(adapter);
1041		if (ret) {
1042			kfree(p_cache);
1043			qlcnic_83xx_unlock_flash(adapter);
1044			return -EIO;
1045		}
1046	}
1047
1048	kfree(p_cache);
1049	qlcnic_83xx_unlock_flash(adapter);
1050
1051	return 0;
1052}
1053
1054static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1055					 char *buf, loff_t offset, size_t size)
1056{
1057	int  i, ret, count;
1058	unsigned char *p_cache, *p_src;
1059
1060	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1061	if (!p_cache)
1062		return -ENOMEM;
1063
1064	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1065	memcpy(p_cache, buf, size);
1066	p_src = p_cache;
1067	count = size / sizeof(u32);
1068
1069	if (qlcnic_83xx_lock_flash(adapter) != 0) {
1070		kfree(p_cache);
1071		return -EIO;
1072	}
1073
1074	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1075		ret = qlcnic_83xx_enable_flash_write(adapter);
1076		if (ret) {
1077			kfree(p_cache);
1078			qlcnic_83xx_unlock_flash(adapter);
1079			return -EIO;
1080		}
1081	}
1082
1083	for (i = 0; i < count; i++) {
1084		ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1085		if (ret) {
1086			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1087				ret = qlcnic_83xx_disable_flash_write(adapter);
1088				if (ret) {
1089					kfree(p_cache);
1090					qlcnic_83xx_unlock_flash(adapter);
1091					return -EIO;
1092				}
1093			}
1094			kfree(p_cache);
1095			qlcnic_83xx_unlock_flash(adapter);
1096			return -EIO;
1097		}
1098
1099		p_src = p_src + sizeof(u32);
1100		offset = offset + sizeof(u32);
1101	}
1102
1103	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1104		ret = qlcnic_83xx_disable_flash_write(adapter);
1105		if (ret) {
1106			kfree(p_cache);
1107			qlcnic_83xx_unlock_flash(adapter);
1108			return -EIO;
1109		}
1110	}
1111
1112	kfree(p_cache);
1113	qlcnic_83xx_unlock_flash(adapter);
1114
1115	return 0;
1116}
1117
1118static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1119						     struct kobject *kobj,
1120						     struct bin_attribute *attr,
1121						     char *buf, loff_t offset,
1122						     size_t size)
1123{
1124	int  ret;
1125	static int flash_mode;
1126	unsigned long data;
1127	struct device *dev = container_of(kobj, struct device, kobj);
1128	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1129
 
 
 
1130	ret = kstrtoul(buf, 16, &data);
1131
1132	switch (data) {
1133	case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1134		flash_mode = QLC_83XX_ERASE_MODE;
1135		ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1136		if (ret) {
1137			dev_err(&adapter->pdev->dev,
1138				"%s failed at %d\n", __func__, __LINE__);
1139			return -EIO;
1140		}
1141		break;
1142
1143	case QLC_83XX_FLASH_BULK_WRITE_CMD:
1144		flash_mode = QLC_83XX_BULK_WRITE_MODE;
1145		break;
1146
1147	case QLC_83XX_FLASH_WRITE_CMD:
1148		flash_mode = QLC_83XX_WRITE_MODE;
1149		break;
1150	default:
1151		if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1152			ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1153								 offset, size);
1154			if (ret) {
1155				dev_err(&adapter->pdev->dev,
1156					"%s failed at %d\n",
1157					__func__, __LINE__);
1158				return -EIO;
1159			}
1160		}
1161
1162		if (flash_mode == QLC_83XX_WRITE_MODE) {
1163			ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1164							    offset, size);
1165			if (ret) {
1166				dev_err(&adapter->pdev->dev,
1167					"%s failed at %d\n", __func__,
1168					__LINE__);
1169				return -EIO;
1170			}
1171		}
1172	}
1173
1174	return size;
1175}
1176
1177static struct device_attribute dev_attr_bridged_mode = {
1178       .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1179       .show = qlcnic_show_bridged_mode,
1180       .store = qlcnic_store_bridged_mode,
1181};
1182
1183static struct device_attribute dev_attr_diag_mode = {
1184	.attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1185	.show = qlcnic_show_diag_mode,
1186	.store = qlcnic_store_diag_mode,
1187};
1188
1189static struct device_attribute dev_attr_beacon = {
1190	.attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1191	.show = qlcnic_show_beacon,
1192	.store = qlcnic_store_beacon,
1193};
1194
1195static struct bin_attribute bin_attr_crb = {
1196	.attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1197	.size = 0,
1198	.read = qlcnic_sysfs_read_crb,
1199	.write = qlcnic_sysfs_write_crb,
1200};
1201
1202static struct bin_attribute bin_attr_mem = {
1203	.attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1204	.size = 0,
1205	.read = qlcnic_sysfs_read_mem,
1206	.write = qlcnic_sysfs_write_mem,
1207};
1208
1209static struct bin_attribute bin_attr_npar_config = {
1210	.attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1211	.size = 0,
1212	.read = qlcnic_sysfs_read_npar_config,
1213	.write = qlcnic_sysfs_write_npar_config,
1214};
1215
1216static struct bin_attribute bin_attr_pci_config = {
1217	.attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1218	.size = 0,
1219	.read = qlcnic_sysfs_read_pci_config,
1220	.write = NULL,
1221};
1222
1223static struct bin_attribute bin_attr_port_stats = {
1224	.attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1225	.size = 0,
1226	.read = qlcnic_sysfs_get_port_stats,
1227	.write = qlcnic_sysfs_clear_port_stats,
1228};
1229
1230static struct bin_attribute bin_attr_esw_stats = {
1231	.attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1232	.size = 0,
1233	.read = qlcnic_sysfs_get_esw_stats,
1234	.write = qlcnic_sysfs_clear_esw_stats,
1235};
1236
1237static struct bin_attribute bin_attr_esw_config = {
1238	.attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1239	.size = 0,
1240	.read = qlcnic_sysfs_read_esw_config,
1241	.write = qlcnic_sysfs_write_esw_config,
1242};
1243
1244static struct bin_attribute bin_attr_pm_config = {
1245	.attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1246	.size = 0,
1247	.read = qlcnic_sysfs_read_pm_config,
1248	.write = qlcnic_sysfs_write_pm_config,
1249};
1250
1251static struct bin_attribute bin_attr_flash = {
1252	.attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1253	.size = 0,
1254	.read = qlcnic_83xx_sysfs_flash_read_handler,
1255	.write = qlcnic_83xx_sysfs_flash_write_handler,
1256};
1257
1258#ifdef CONFIG_QLCNIC_HWMON
1259
1260static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1261				      struct device_attribute *dev_attr,
1262				      char *buf)
1263{
1264	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1265	unsigned int temperature = 0, value = 0;
1266
1267	if (qlcnic_83xx_check(adapter))
1268		value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1269	else if (qlcnic_82xx_check(adapter))
1270		value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1271
1272	temperature = qlcnic_get_temp_val(value);
1273	/* display millidegree celcius */
1274	temperature *= 1000;
1275	return sprintf(buf, "%u\n", temperature);
1276}
1277
1278/* hwmon-sysfs attributes */
1279static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1280			  qlcnic_hwmon_show_temp, NULL, 1);
1281
1282static struct attribute *qlcnic_hwmon_attrs[] = {
1283	&sensor_dev_attr_temp1_input.dev_attr.attr,
1284	NULL
1285};
1286
1287ATTRIBUTE_GROUPS(qlcnic_hwmon);
1288
1289void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1290{
1291	struct device *dev = &adapter->pdev->dev;
1292	struct device *hwmon_dev;
1293
1294	/* Skip hwmon registration for a VF device */
1295	if (qlcnic_sriov_vf_check(adapter)) {
1296		adapter->ahw->hwmon_dev = NULL;
1297		return;
1298	}
1299	hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1300						      adapter,
1301						      qlcnic_hwmon_groups);
1302	if (IS_ERR(hwmon_dev)) {
1303		dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1304			PTR_ERR(hwmon_dev));
1305		hwmon_dev = NULL;
1306	}
1307	adapter->ahw->hwmon_dev = hwmon_dev;
1308}
1309
1310void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1311{
1312	struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1313	if (hwmon_dev) {
1314		hwmon_device_unregister(hwmon_dev);
1315		adapter->ahw->hwmon_dev = NULL;
1316	}
1317}
1318#endif
1319
1320void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1321{
1322	struct device *dev = &adapter->pdev->dev;
1323
1324	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1325		if (device_create_file(dev, &dev_attr_bridged_mode))
1326			dev_warn(dev,
1327				 "failed to create bridged_mode sysfs entry\n");
1328}
1329
1330void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1331{
1332	struct device *dev = &adapter->pdev->dev;
1333
1334	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1335		device_remove_file(dev, &dev_attr_bridged_mode);
1336}
1337
1338static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1339{
1340	struct device *dev = &adapter->pdev->dev;
1341
1342	if (device_create_bin_file(dev, &bin_attr_port_stats))
1343		dev_info(dev, "failed to create port stats sysfs entry");
1344
1345	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1346		return;
1347	if (device_create_file(dev, &dev_attr_diag_mode))
1348		dev_info(dev, "failed to create diag_mode sysfs entry\n");
1349	if (device_create_bin_file(dev, &bin_attr_crb))
1350		dev_info(dev, "failed to create crb sysfs entry\n");
1351	if (device_create_bin_file(dev, &bin_attr_mem))
1352		dev_info(dev, "failed to create mem sysfs entry\n");
1353
1354	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1355		return;
1356
1357	if (device_create_bin_file(dev, &bin_attr_pci_config))
1358		dev_info(dev, "failed to create pci config sysfs entry");
1359
1360	if (device_create_file(dev, &dev_attr_beacon))
1361		dev_info(dev, "failed to create beacon sysfs entry");
1362
1363	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1364		return;
1365	if (device_create_bin_file(dev, &bin_attr_esw_config))
1366		dev_info(dev, "failed to create esw config sysfs entry");
1367	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1368		return;
1369	if (device_create_bin_file(dev, &bin_attr_npar_config))
1370		dev_info(dev, "failed to create npar config sysfs entry");
1371	if (device_create_bin_file(dev, &bin_attr_pm_config))
1372		dev_info(dev, "failed to create pm config sysfs entry");
1373	if (device_create_bin_file(dev, &bin_attr_esw_stats))
1374		dev_info(dev, "failed to create eswitch stats sysfs entry");
1375}
1376
1377static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1378{
1379	struct device *dev = &adapter->pdev->dev;
1380
1381	device_remove_bin_file(dev, &bin_attr_port_stats);
1382
1383	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1384		return;
1385	device_remove_file(dev, &dev_attr_diag_mode);
1386	device_remove_bin_file(dev, &bin_attr_crb);
1387	device_remove_bin_file(dev, &bin_attr_mem);
1388
1389	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1390		return;
1391
1392	device_remove_bin_file(dev, &bin_attr_pci_config);
1393	device_remove_file(dev, &dev_attr_beacon);
1394	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1395		return;
1396	device_remove_bin_file(dev, &bin_attr_esw_config);
1397	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1398		return;
1399	device_remove_bin_file(dev, &bin_attr_npar_config);
1400	device_remove_bin_file(dev, &bin_attr_pm_config);
1401	device_remove_bin_file(dev, &bin_attr_esw_stats);
1402}
1403
1404void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1405{
1406	qlcnic_create_diag_entries(adapter);
1407}
1408
1409void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1410{
1411	qlcnic_remove_diag_entries(adapter);
1412}
1413
1414void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1415{
1416	struct device *dev = &adapter->pdev->dev;
1417
1418	qlcnic_create_diag_entries(adapter);
1419
1420	if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1421		dev_info(dev, "failed to create flash sysfs entry\n");
1422}
1423
1424void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1425{
1426	struct device *dev = &adapter->pdev->dev;
1427
1428	qlcnic_remove_diag_entries(adapter);
1429	sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);
1430}