18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * QLogic qlcnic NIC Driver
48c2ecf20Sopenharmony_ci * Copyright (c) 2009-2013 QLogic Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/slab.h>
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/swab.h>
108c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
118c2ecf20Sopenharmony_ci#include <net/ip.h>
128c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
138c2ecf20Sopenharmony_ci#include <linux/inetdevice.h>
148c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
158c2ecf20Sopenharmony_ci#include <linux/aer.h>
168c2ecf20Sopenharmony_ci#include <linux/log2.h>
178c2ecf20Sopenharmony_ci#ifdef CONFIG_QLCNIC_HWMON
188c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
198c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
208c2ecf20Sopenharmony_ci#endif
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "qlcnic.h"
238c2ecf20Sopenharmony_ci#include "qlcnic_hw.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciint qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciint qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic ssize_t qlcnic_store_bridged_mode(struct device *dev,
368c2ecf20Sopenharmony_ci					 struct device_attribute *attr,
378c2ecf20Sopenharmony_ci					 const char *buf, size_t len)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
408c2ecf20Sopenharmony_ci	unsigned long new;
418c2ecf20Sopenharmony_ci	int ret = -EINVAL;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
448c2ecf20Sopenharmony_ci		goto err_out;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
478c2ecf20Sopenharmony_ci		goto err_out;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 2, &new))
508c2ecf20Sopenharmony_ci		goto err_out;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (!qlcnic_config_bridged_mode(adapter, !!new))
538c2ecf20Sopenharmony_ci		ret = len;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cierr_out:
568c2ecf20Sopenharmony_ci	return ret;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic ssize_t qlcnic_show_bridged_mode(struct device *dev,
608c2ecf20Sopenharmony_ci					struct device_attribute *attr,
618c2ecf20Sopenharmony_ci					char *buf)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
648c2ecf20Sopenharmony_ci	int bridged_mode = 0;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
678c2ecf20Sopenharmony_ci		bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", bridged_mode);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic ssize_t qlcnic_store_diag_mode(struct device *dev,
738c2ecf20Sopenharmony_ci				      struct device_attribute *attr,
748c2ecf20Sopenharmony_ci				      const char *buf, size_t len)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
778c2ecf20Sopenharmony_ci	unsigned long new;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 2, &new))
808c2ecf20Sopenharmony_ci		return -EINVAL;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
838c2ecf20Sopenharmony_ci		adapter->flags ^= QLCNIC_DIAG_ENABLED;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return len;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic ssize_t qlcnic_show_diag_mode(struct device *dev,
898c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *buf)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
928c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
968c2ecf20Sopenharmony_ci				  u8 *state, u8 *rate)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	*rate = LSB(beacon);
998c2ecf20Sopenharmony_ci	*state = MSB(beacon);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (!*state) {
1048c2ecf20Sopenharmony_ci		*rate = __QLCNIC_MAX_LED_RATE;
1058c2ecf20Sopenharmony_ci		return 0;
1068c2ecf20Sopenharmony_ci	} else if (*state > __QLCNIC_MAX_LED_STATE) {
1078c2ecf20Sopenharmony_ci		return -EINVAL;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
1118c2ecf20Sopenharmony_ci		return -EINVAL;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
1178c2ecf20Sopenharmony_ci				    const char *buf, size_t len)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct qlcnic_hardware_context *ahw = adapter->ahw;
1208c2ecf20Sopenharmony_ci	unsigned long h_beacon;
1218c2ecf20Sopenharmony_ci	int err;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1248c2ecf20Sopenharmony_ci		return -EIO;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 2, &h_beacon))
1278c2ecf20Sopenharmony_ci		return -EINVAL;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	qlcnic_get_beacon_state(adapter);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if (ahw->beacon_state == h_beacon)
1328c2ecf20Sopenharmony_ci		return len;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	rtnl_lock();
1358c2ecf20Sopenharmony_ci	if (!ahw->beacon_state) {
1368c2ecf20Sopenharmony_ci		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
1378c2ecf20Sopenharmony_ci			rtnl_unlock();
1388c2ecf20Sopenharmony_ci			return -EBUSY;
1398c2ecf20Sopenharmony_ci		}
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (h_beacon)
1438c2ecf20Sopenharmony_ci		err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
1448c2ecf20Sopenharmony_ci	else
1458c2ecf20Sopenharmony_ci		err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
1468c2ecf20Sopenharmony_ci	if (!err)
1478c2ecf20Sopenharmony_ci		ahw->beacon_state = h_beacon;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (!ahw->beacon_state)
1508c2ecf20Sopenharmony_ci		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	rtnl_unlock();
1538c2ecf20Sopenharmony_ci	return len;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
1578c2ecf20Sopenharmony_ci				    const char *buf, size_t len)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct qlcnic_hardware_context *ahw = adapter->ahw;
1608c2ecf20Sopenharmony_ci	int err, drv_sds_rings = adapter->drv_sds_rings;
1618c2ecf20Sopenharmony_ci	u16 beacon;
1628c2ecf20Sopenharmony_ci	u8 b_state, b_rate;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	if (len != sizeof(u16))
1658c2ecf20Sopenharmony_ci		return -EINVAL;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	memcpy(&beacon, buf, sizeof(u16));
1688c2ecf20Sopenharmony_ci	err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
1698c2ecf20Sopenharmony_ci	if (err)
1708c2ecf20Sopenharmony_ci		return err;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	qlcnic_get_beacon_state(adapter);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	if (ahw->beacon_state == b_state)
1758c2ecf20Sopenharmony_ci		return len;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	rtnl_lock();
1788c2ecf20Sopenharmony_ci	if (!ahw->beacon_state) {
1798c2ecf20Sopenharmony_ci		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
1808c2ecf20Sopenharmony_ci			rtnl_unlock();
1818c2ecf20Sopenharmony_ci			return -EBUSY;
1828c2ecf20Sopenharmony_ci		}
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
1868c2ecf20Sopenharmony_ci		err = -EIO;
1878c2ecf20Sopenharmony_ci		goto out;
1888c2ecf20Sopenharmony_ci	}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
1918c2ecf20Sopenharmony_ci		err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
1928c2ecf20Sopenharmony_ci		if (err)
1938c2ecf20Sopenharmony_ci			goto out;
1948c2ecf20Sopenharmony_ci		set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	err = qlcnic_config_led(adapter, b_state, b_rate);
1988c2ecf20Sopenharmony_ci	if (!err) {
1998c2ecf20Sopenharmony_ci		err = len;
2008c2ecf20Sopenharmony_ci		ahw->beacon_state = b_state;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
2048c2ecf20Sopenharmony_ci		qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciout:
2078c2ecf20Sopenharmony_ci	if (!ahw->beacon_state)
2088c2ecf20Sopenharmony_ci		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
2098c2ecf20Sopenharmony_ci	rtnl_unlock();
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return err;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic ssize_t qlcnic_store_beacon(struct device *dev,
2158c2ecf20Sopenharmony_ci				   struct device_attribute *attr,
2168c2ecf20Sopenharmony_ci				   const char *buf, size_t len)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2198c2ecf20Sopenharmony_ci	int err = 0;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
2228c2ecf20Sopenharmony_ci		dev_warn(dev,
2238c2ecf20Sopenharmony_ci			 "LED test not supported in non privileged mode\n");
2248c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (qlcnic_82xx_check(adapter))
2288c2ecf20Sopenharmony_ci		err = qlcnic_82xx_store_beacon(adapter, buf, len);
2298c2ecf20Sopenharmony_ci	else if (qlcnic_83xx_check(adapter))
2308c2ecf20Sopenharmony_ci		err = qlcnic_83xx_store_beacon(adapter, buf, len);
2318c2ecf20Sopenharmony_ci	else
2328c2ecf20Sopenharmony_ci		return -EIO;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	return err;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic ssize_t qlcnic_show_beacon(struct device *dev,
2388c2ecf20Sopenharmony_ci				  struct device_attribute *attr, char *buf)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2468c2ecf20Sopenharmony_ci				     loff_t offset, size_t size)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	size_t crb_size = 4;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2518c2ecf20Sopenharmony_ci		return -EIO;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (offset < QLCNIC_PCI_CRBSPACE) {
2548c2ecf20Sopenharmony_ci		if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2558c2ecf20Sopenharmony_ci				  QLCNIC_PCI_CAMQM_END))
2568c2ecf20Sopenharmony_ci			crb_size = 8;
2578c2ecf20Sopenharmony_ci		else
2588c2ecf20Sopenharmony_ci			return -EINVAL;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if ((size != crb_size) || (offset & (crb_size-1)))
2628c2ecf20Sopenharmony_ci		return  -EINVAL;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return 0;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2688c2ecf20Sopenharmony_ci				     struct bin_attribute *attr, char *buf,
2698c2ecf20Sopenharmony_ci				     loff_t offset, size_t size)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
2728c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2738c2ecf20Sopenharmony_ci	int ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2768c2ecf20Sopenharmony_ci	if (ret != 0)
2778c2ecf20Sopenharmony_ci		return ret;
2788c2ecf20Sopenharmony_ci	qlcnic_read_crb(adapter, buf, offset, size);
2798c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return size;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2858c2ecf20Sopenharmony_ci				      struct bin_attribute *attr, char *buf,
2868c2ecf20Sopenharmony_ci				      loff_t offset, size_t size)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
2898c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2908c2ecf20Sopenharmony_ci	int ret;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2938c2ecf20Sopenharmony_ci	if (ret != 0)
2948c2ecf20Sopenharmony_ci		return ret;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
2978c2ecf20Sopenharmony_ci	qlcnic_write_crb(adapter, buf, offset, size);
2988c2ecf20Sopenharmony_ci	return size;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3028c2ecf20Sopenharmony_ci				     loff_t offset, size_t size)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3058c2ecf20Sopenharmony_ci		return -EIO;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if ((size != 8) || (offset & 0x7))
3088c2ecf20Sopenharmony_ci		return  -EIO;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return 0;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3148c2ecf20Sopenharmony_ci				     struct bin_attribute *attr, char *buf,
3158c2ecf20Sopenharmony_ci				     loff_t offset, size_t size)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
3188c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3198c2ecf20Sopenharmony_ci	u64 data;
3208c2ecf20Sopenharmony_ci	int ret;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3238c2ecf20Sopenharmony_ci	if (ret != 0)
3248c2ecf20Sopenharmony_ci		return ret;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3278c2ecf20Sopenharmony_ci		return -EIO;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	memcpy(buf, &data, size);
3308c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return size;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3368c2ecf20Sopenharmony_ci				      struct bin_attribute *attr, char *buf,
3378c2ecf20Sopenharmony_ci				      loff_t offset, size_t size)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
3408c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3418c2ecf20Sopenharmony_ci	u64 data;
3428c2ecf20Sopenharmony_ci	int ret;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3458c2ecf20Sopenharmony_ci	if (ret != 0)
3468c2ecf20Sopenharmony_ci		return ret;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
3498c2ecf20Sopenharmony_ci	memcpy(&data, buf, size);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3528c2ecf20Sopenharmony_ci		return -EIO;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return size;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ciint qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	int i;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
3628c2ecf20Sopenharmony_ci		if (adapter->npars[i].pci_func == pci_func)
3638c2ecf20Sopenharmony_ci			return i;
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
3678c2ecf20Sopenharmony_ci	return -EINVAL;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic int validate_pm_config(struct qlcnic_adapter *adapter,
3718c2ecf20Sopenharmony_ci			      struct qlcnic_pm_func_cfg *pm_cfg, int count)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	u8 src_pci_func, s_esw_id, d_esw_id;
3748c2ecf20Sopenharmony_ci	u8 dest_pci_func;
3758c2ecf20Sopenharmony_ci	int i, src_index, dest_index;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
3788c2ecf20Sopenharmony_ci		src_pci_func = pm_cfg[i].pci_func;
3798c2ecf20Sopenharmony_ci		dest_pci_func = pm_cfg[i].dest_npar;
3808c2ecf20Sopenharmony_ci		src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
3818c2ecf20Sopenharmony_ci		if (src_index < 0)
3828c2ecf20Sopenharmony_ci			return -EINVAL;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci		dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
3858c2ecf20Sopenharmony_ci		if (dest_index < 0)
3868c2ecf20Sopenharmony_ci			return -EINVAL;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		s_esw_id = adapter->npars[src_index].phy_port;
3898c2ecf20Sopenharmony_ci		d_esw_id = adapter->npars[dest_index].phy_port;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		if (s_esw_id != d_esw_id)
3928c2ecf20Sopenharmony_ci			return -EINVAL;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	return 0;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
3998c2ecf20Sopenharmony_ci					    struct kobject *kobj,
4008c2ecf20Sopenharmony_ci					    struct bin_attribute *attr,
4018c2ecf20Sopenharmony_ci					    char *buf, loff_t offset,
4028c2ecf20Sopenharmony_ci					    size_t size)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
4058c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4068c2ecf20Sopenharmony_ci	struct qlcnic_pm_func_cfg *pm_cfg;
4078c2ecf20Sopenharmony_ci	u32 id, action, pci_func;
4088c2ecf20Sopenharmony_ci	int count, rem, i, ret, index;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	count	= size / sizeof(struct qlcnic_pm_func_cfg);
4118c2ecf20Sopenharmony_ci	rem	= size % sizeof(struct qlcnic_pm_func_cfg);
4128c2ecf20Sopenharmony_ci	if (rem)
4138c2ecf20Sopenharmony_ci		return -EINVAL;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
4168c2ecf20Sopenharmony_ci	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
4178c2ecf20Sopenharmony_ci	ret = validate_pm_config(adapter, pm_cfg, count);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	if (ret)
4208c2ecf20Sopenharmony_ci		return ret;
4218c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
4228c2ecf20Sopenharmony_ci		pci_func = pm_cfg[i].pci_func;
4238c2ecf20Sopenharmony_ci		action = !!pm_cfg[i].action;
4248c2ecf20Sopenharmony_ci		index = qlcnic_is_valid_nic_func(adapter, pci_func);
4258c2ecf20Sopenharmony_ci		if (index < 0)
4268c2ecf20Sopenharmony_ci			return -EINVAL;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		id = adapter->npars[index].phy_port;
4298c2ecf20Sopenharmony_ci		ret = qlcnic_config_port_mirroring(adapter, id,
4308c2ecf20Sopenharmony_ci						   action, pci_func);
4318c2ecf20Sopenharmony_ci		if (ret)
4328c2ecf20Sopenharmony_ci			return ret;
4338c2ecf20Sopenharmony_ci	}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
4368c2ecf20Sopenharmony_ci		pci_func = pm_cfg[i].pci_func;
4378c2ecf20Sopenharmony_ci		index = qlcnic_is_valid_nic_func(adapter, pci_func);
4388c2ecf20Sopenharmony_ci		if (index < 0)
4398c2ecf20Sopenharmony_ci			return -EINVAL;
4408c2ecf20Sopenharmony_ci		id = adapter->npars[index].phy_port;
4418c2ecf20Sopenharmony_ci		adapter->npars[index].enable_pm = !!pm_cfg[i].action;
4428c2ecf20Sopenharmony_ci		adapter->npars[index].dest_npar = id;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	return size;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
4498c2ecf20Sopenharmony_ci					   struct kobject *kobj,
4508c2ecf20Sopenharmony_ci					   struct bin_attribute *attr,
4518c2ecf20Sopenharmony_ci					   char *buf, loff_t offset,
4528c2ecf20Sopenharmony_ci					   size_t size)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
4558c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4568c2ecf20Sopenharmony_ci	struct qlcnic_pm_func_cfg *pm_cfg;
4578c2ecf20Sopenharmony_ci	u8 pci_func;
4588c2ecf20Sopenharmony_ci	u32 count;
4598c2ecf20Sopenharmony_ci	int i;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	memset(buf, 0, size);
4628c2ecf20Sopenharmony_ci	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
4638c2ecf20Sopenharmony_ci	count = size / sizeof(struct qlcnic_pm_func_cfg);
4648c2ecf20Sopenharmony_ci	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
4658c2ecf20Sopenharmony_ci		pci_func = adapter->npars[i].pci_func;
4668c2ecf20Sopenharmony_ci		if (pci_func >= count) {
4678c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
4688c2ecf20Sopenharmony_ci				__func__, adapter->ahw->total_nic_func, count);
4698c2ecf20Sopenharmony_ci			continue;
4708c2ecf20Sopenharmony_ci		}
4718c2ecf20Sopenharmony_ci		if (!adapter->npars[i].eswitch_status)
4728c2ecf20Sopenharmony_ci			continue;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci		pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
4758c2ecf20Sopenharmony_ci		pm_cfg[pci_func].dest_npar = 0;
4768c2ecf20Sopenharmony_ci		pm_cfg[pci_func].pci_func = i;
4778c2ecf20Sopenharmony_ci	}
4788c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
4798c2ecf20Sopenharmony_ci	return size;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic int validate_esw_config(struct qlcnic_adapter *adapter,
4838c2ecf20Sopenharmony_ci			       struct qlcnic_esw_func_cfg *esw_cfg, int count)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct qlcnic_hardware_context *ahw = adapter->ahw;
4868c2ecf20Sopenharmony_ci	int i, ret;
4878c2ecf20Sopenharmony_ci	u32 op_mode;
4888c2ecf20Sopenharmony_ci	u8 pci_func;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (qlcnic_82xx_check(adapter))
4918c2ecf20Sopenharmony_ci		op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
4928c2ecf20Sopenharmony_ci	else
4938c2ecf20Sopenharmony_ci		op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
4968c2ecf20Sopenharmony_ci		pci_func = esw_cfg[i].pci_func;
4978c2ecf20Sopenharmony_ci		if (pci_func >= ahw->max_vnic_func)
4988c2ecf20Sopenharmony_ci			return -EINVAL;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
5018c2ecf20Sopenharmony_ci			if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
5028c2ecf20Sopenharmony_ci				return -EINVAL;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci		switch (esw_cfg[i].op_mode) {
5058c2ecf20Sopenharmony_ci		case QLCNIC_PORT_DEFAULTS:
5068c2ecf20Sopenharmony_ci			if (qlcnic_82xx_check(adapter)) {
5078c2ecf20Sopenharmony_ci				ret = QLC_DEV_GET_DRV(op_mode, pci_func);
5088c2ecf20Sopenharmony_ci			} else {
5098c2ecf20Sopenharmony_ci				ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
5108c2ecf20Sopenharmony_ci								  pci_func);
5118c2ecf20Sopenharmony_ci				esw_cfg[i].offload_flags = 0;
5128c2ecf20Sopenharmony_ci			}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci			if (ret != QLCNIC_NON_PRIV_FUNC) {
5158c2ecf20Sopenharmony_ci				if (esw_cfg[i].mac_anti_spoof != 0)
5168c2ecf20Sopenharmony_ci					return -EINVAL;
5178c2ecf20Sopenharmony_ci				if (esw_cfg[i].mac_override != 1)
5188c2ecf20Sopenharmony_ci					return -EINVAL;
5198c2ecf20Sopenharmony_ci				if (esw_cfg[i].promisc_mode != 1)
5208c2ecf20Sopenharmony_ci					return -EINVAL;
5218c2ecf20Sopenharmony_ci			}
5228c2ecf20Sopenharmony_ci			break;
5238c2ecf20Sopenharmony_ci		case QLCNIC_ADD_VLAN:
5248c2ecf20Sopenharmony_ci			if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
5258c2ecf20Sopenharmony_ci				return -EINVAL;
5268c2ecf20Sopenharmony_ci			if (!esw_cfg[i].op_type)
5278c2ecf20Sopenharmony_ci				return -EINVAL;
5288c2ecf20Sopenharmony_ci			break;
5298c2ecf20Sopenharmony_ci		case QLCNIC_DEL_VLAN:
5308c2ecf20Sopenharmony_ci			if (!esw_cfg[i].op_type)
5318c2ecf20Sopenharmony_ci				return -EINVAL;
5328c2ecf20Sopenharmony_ci			break;
5338c2ecf20Sopenharmony_ci		default:
5348c2ecf20Sopenharmony_ci			return -EINVAL;
5358c2ecf20Sopenharmony_ci		}
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	return 0;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
5428c2ecf20Sopenharmony_ci					     struct kobject *kobj,
5438c2ecf20Sopenharmony_ci					     struct bin_attribute *attr,
5448c2ecf20Sopenharmony_ci					     char *buf, loff_t offset,
5458c2ecf20Sopenharmony_ci					     size_t size)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
5488c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
5498c2ecf20Sopenharmony_ci	struct qlcnic_esw_func_cfg *esw_cfg;
5508c2ecf20Sopenharmony_ci	struct qlcnic_npar_info *npar;
5518c2ecf20Sopenharmony_ci	int count, rem, i, ret;
5528c2ecf20Sopenharmony_ci	int index;
5538c2ecf20Sopenharmony_ci	u8 op_mode = 0, pci_func;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	count	= size / sizeof(struct qlcnic_esw_func_cfg);
5568c2ecf20Sopenharmony_ci	rem	= size % sizeof(struct qlcnic_esw_func_cfg);
5578c2ecf20Sopenharmony_ci	if (rem)
5588c2ecf20Sopenharmony_ci		return -EINVAL;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
5618c2ecf20Sopenharmony_ci	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
5628c2ecf20Sopenharmony_ci	ret = validate_esw_config(adapter, esw_cfg, count);
5638c2ecf20Sopenharmony_ci	if (ret)
5648c2ecf20Sopenharmony_ci		return ret;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
5678c2ecf20Sopenharmony_ci		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
5688c2ecf20Sopenharmony_ci			if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
5698c2ecf20Sopenharmony_ci				return -EINVAL;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci		if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
5728c2ecf20Sopenharmony_ci			continue;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		op_mode = esw_cfg[i].op_mode;
5758c2ecf20Sopenharmony_ci		qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
5768c2ecf20Sopenharmony_ci		esw_cfg[i].op_mode = op_mode;
5778c2ecf20Sopenharmony_ci		esw_cfg[i].pci_func = adapter->ahw->pci_func;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		switch (esw_cfg[i].op_mode) {
5808c2ecf20Sopenharmony_ci		case QLCNIC_PORT_DEFAULTS:
5818c2ecf20Sopenharmony_ci			qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
5828c2ecf20Sopenharmony_ci			rtnl_lock();
5838c2ecf20Sopenharmony_ci			qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
5848c2ecf20Sopenharmony_ci			rtnl_unlock();
5858c2ecf20Sopenharmony_ci			break;
5868c2ecf20Sopenharmony_ci		case QLCNIC_ADD_VLAN:
5878c2ecf20Sopenharmony_ci			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
5888c2ecf20Sopenharmony_ci			break;
5898c2ecf20Sopenharmony_ci		case QLCNIC_DEL_VLAN:
5908c2ecf20Sopenharmony_ci			esw_cfg[i].vlan_id = 0;
5918c2ecf20Sopenharmony_ci			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
5928c2ecf20Sopenharmony_ci			break;
5938c2ecf20Sopenharmony_ci		}
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
5978c2ecf20Sopenharmony_ci		goto out;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
6008c2ecf20Sopenharmony_ci		pci_func = esw_cfg[i].pci_func;
6018c2ecf20Sopenharmony_ci		index = qlcnic_is_valid_nic_func(adapter, pci_func);
6028c2ecf20Sopenharmony_ci		if (index < 0)
6038c2ecf20Sopenharmony_ci			return -EINVAL;
6048c2ecf20Sopenharmony_ci		npar = &adapter->npars[index];
6058c2ecf20Sopenharmony_ci		switch (esw_cfg[i].op_mode) {
6068c2ecf20Sopenharmony_ci		case QLCNIC_PORT_DEFAULTS:
6078c2ecf20Sopenharmony_ci			npar->promisc_mode = esw_cfg[i].promisc_mode;
6088c2ecf20Sopenharmony_ci			npar->mac_override = esw_cfg[i].mac_override;
6098c2ecf20Sopenharmony_ci			npar->offload_flags = esw_cfg[i].offload_flags;
6108c2ecf20Sopenharmony_ci			npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
6118c2ecf20Sopenharmony_ci			npar->discard_tagged = esw_cfg[i].discard_tagged;
6128c2ecf20Sopenharmony_ci			break;
6138c2ecf20Sopenharmony_ci		case QLCNIC_ADD_VLAN:
6148c2ecf20Sopenharmony_ci			npar->pvid = esw_cfg[i].vlan_id;
6158c2ecf20Sopenharmony_ci			break;
6168c2ecf20Sopenharmony_ci		case QLCNIC_DEL_VLAN:
6178c2ecf20Sopenharmony_ci			npar->pvid = 0;
6188c2ecf20Sopenharmony_ci			break;
6198c2ecf20Sopenharmony_ci		}
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ciout:
6228c2ecf20Sopenharmony_ci	return size;
6238c2ecf20Sopenharmony_ci}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
6268c2ecf20Sopenharmony_ci					    struct kobject *kobj,
6278c2ecf20Sopenharmony_ci					    struct bin_attribute *attr,
6288c2ecf20Sopenharmony_ci					    char *buf, loff_t offset,
6298c2ecf20Sopenharmony_ci					    size_t size)
6308c2ecf20Sopenharmony_ci{
6318c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
6328c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
6338c2ecf20Sopenharmony_ci	struct qlcnic_esw_func_cfg *esw_cfg;
6348c2ecf20Sopenharmony_ci	u8 pci_func;
6358c2ecf20Sopenharmony_ci	u32 count;
6368c2ecf20Sopenharmony_ci	int i;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	memset(buf, 0, size);
6398c2ecf20Sopenharmony_ci	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
6408c2ecf20Sopenharmony_ci	count = size / sizeof(struct qlcnic_esw_func_cfg);
6418c2ecf20Sopenharmony_ci	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
6428c2ecf20Sopenharmony_ci		pci_func = adapter->npars[i].pci_func;
6438c2ecf20Sopenharmony_ci		if (pci_func >= count) {
6448c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
6458c2ecf20Sopenharmony_ci				__func__, adapter->ahw->total_nic_func, count);
6468c2ecf20Sopenharmony_ci			continue;
6478c2ecf20Sopenharmony_ci		}
6488c2ecf20Sopenharmony_ci		if (!adapter->npars[i].eswitch_status)
6498c2ecf20Sopenharmony_ci			continue;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci		esw_cfg[pci_func].pci_func = pci_func;
6528c2ecf20Sopenharmony_ci		if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
6538c2ecf20Sopenharmony_ci			return -EINVAL;
6548c2ecf20Sopenharmony_ci	}
6558c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
6568c2ecf20Sopenharmony_ci	return size;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_cistatic int validate_npar_config(struct qlcnic_adapter *adapter,
6608c2ecf20Sopenharmony_ci				struct qlcnic_npar_func_cfg *np_cfg,
6618c2ecf20Sopenharmony_ci				int count)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	u8 pci_func, i;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
6668c2ecf20Sopenharmony_ci		pci_func = np_cfg[i].pci_func;
6678c2ecf20Sopenharmony_ci		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
6688c2ecf20Sopenharmony_ci			return -EINVAL;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci		if (!IS_VALID_BW(np_cfg[i].min_bw) ||
6718c2ecf20Sopenharmony_ci		    !IS_VALID_BW(np_cfg[i].max_bw))
6728c2ecf20Sopenharmony_ci			return -EINVAL;
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci	return 0;
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
6788c2ecf20Sopenharmony_ci					      struct kobject *kobj,
6798c2ecf20Sopenharmony_ci					      struct bin_attribute *attr,
6808c2ecf20Sopenharmony_ci					      char *buf, loff_t offset,
6818c2ecf20Sopenharmony_ci					      size_t size)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
6848c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
6858c2ecf20Sopenharmony_ci	struct qlcnic_info nic_info;
6868c2ecf20Sopenharmony_ci	struct qlcnic_npar_func_cfg *np_cfg;
6878c2ecf20Sopenharmony_ci	int i, count, rem, ret, index;
6888c2ecf20Sopenharmony_ci	u8 pci_func;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	count	= size / sizeof(struct qlcnic_npar_func_cfg);
6918c2ecf20Sopenharmony_ci	rem	= size % sizeof(struct qlcnic_npar_func_cfg);
6928c2ecf20Sopenharmony_ci	if (rem)
6938c2ecf20Sopenharmony_ci		return -EINVAL;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
6968c2ecf20Sopenharmony_ci	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
6978c2ecf20Sopenharmony_ci	ret = validate_npar_config(adapter, np_cfg, count);
6988c2ecf20Sopenharmony_ci	if (ret)
6998c2ecf20Sopenharmony_ci		return ret;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
7028c2ecf20Sopenharmony_ci		pci_func = np_cfg[i].pci_func;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci		memset(&nic_info, 0, sizeof(struct qlcnic_info));
7058c2ecf20Sopenharmony_ci		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
7068c2ecf20Sopenharmony_ci		if (ret)
7078c2ecf20Sopenharmony_ci			return ret;
7088c2ecf20Sopenharmony_ci		nic_info.pci_func = pci_func;
7098c2ecf20Sopenharmony_ci		nic_info.min_tx_bw = np_cfg[i].min_bw;
7108c2ecf20Sopenharmony_ci		nic_info.max_tx_bw = np_cfg[i].max_bw;
7118c2ecf20Sopenharmony_ci		ret = qlcnic_set_nic_info(adapter, &nic_info);
7128c2ecf20Sopenharmony_ci		if (ret)
7138c2ecf20Sopenharmony_ci			return ret;
7148c2ecf20Sopenharmony_ci		index = qlcnic_is_valid_nic_func(adapter, pci_func);
7158c2ecf20Sopenharmony_ci		if (index < 0)
7168c2ecf20Sopenharmony_ci			return -EINVAL;
7178c2ecf20Sopenharmony_ci		adapter->npars[index].min_bw = nic_info.min_tx_bw;
7188c2ecf20Sopenharmony_ci		adapter->npars[index].max_bw = nic_info.max_tx_bw;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	return size;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
7258c2ecf20Sopenharmony_ci					     struct kobject *kobj,
7268c2ecf20Sopenharmony_ci					     struct bin_attribute *attr,
7278c2ecf20Sopenharmony_ci					     char *buf, loff_t offset,
7288c2ecf20Sopenharmony_ci					     size_t size)
7298c2ecf20Sopenharmony_ci{
7308c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
7318c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
7328c2ecf20Sopenharmony_ci	struct qlcnic_npar_func_cfg *np_cfg;
7338c2ecf20Sopenharmony_ci	struct qlcnic_info nic_info;
7348c2ecf20Sopenharmony_ci	u8 pci_func;
7358c2ecf20Sopenharmony_ci	int i, ret;
7368c2ecf20Sopenharmony_ci	u32 count;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	memset(&nic_info, 0, sizeof(struct qlcnic_info));
7398c2ecf20Sopenharmony_ci	memset(buf, 0, size);
7408c2ecf20Sopenharmony_ci	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	count = size / sizeof(struct qlcnic_npar_func_cfg);
7438c2ecf20Sopenharmony_ci	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
7448c2ecf20Sopenharmony_ci		if (adapter->npars[i].pci_func >= count) {
7458c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
7468c2ecf20Sopenharmony_ci				__func__, adapter->ahw->total_nic_func, count);
7478c2ecf20Sopenharmony_ci			continue;
7488c2ecf20Sopenharmony_ci		}
7498c2ecf20Sopenharmony_ci		if (!adapter->npars[i].eswitch_status)
7508c2ecf20Sopenharmony_ci			continue;
7518c2ecf20Sopenharmony_ci		pci_func = adapter->npars[i].pci_func;
7528c2ecf20Sopenharmony_ci		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
7538c2ecf20Sopenharmony_ci			continue;
7548c2ecf20Sopenharmony_ci		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
7558c2ecf20Sopenharmony_ci		if (ret)
7568c2ecf20Sopenharmony_ci			return ret;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		np_cfg[pci_func].pci_func = pci_func;
7598c2ecf20Sopenharmony_ci		np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
7608c2ecf20Sopenharmony_ci		np_cfg[pci_func].port_num = nic_info.phys_port;
7618c2ecf20Sopenharmony_ci		np_cfg[pci_func].fw_capab = nic_info.capabilities;
7628c2ecf20Sopenharmony_ci		np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
7638c2ecf20Sopenharmony_ci		np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
7648c2ecf20Sopenharmony_ci		np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
7658c2ecf20Sopenharmony_ci		np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
7668c2ecf20Sopenharmony_ci	}
7678c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
7688c2ecf20Sopenharmony_ci	return size;
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
7728c2ecf20Sopenharmony_ci					   struct kobject *kobj,
7738c2ecf20Sopenharmony_ci					   struct bin_attribute *attr,
7748c2ecf20Sopenharmony_ci					   char *buf, loff_t offset,
7758c2ecf20Sopenharmony_ci					   size_t size)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
7788c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
7798c2ecf20Sopenharmony_ci	struct qlcnic_esw_statistics port_stats;
7808c2ecf20Sopenharmony_ci	int ret;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	if (qlcnic_83xx_check(adapter))
7838c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	if (size != sizeof(struct qlcnic_esw_statistics))
7868c2ecf20Sopenharmony_ci		return -EINVAL;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	if (offset >= adapter->ahw->max_vnic_func)
7898c2ecf20Sopenharmony_ci		return -EINVAL;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	memset(&port_stats, 0, size);
7928c2ecf20Sopenharmony_ci	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
7938c2ecf20Sopenharmony_ci				    &port_stats.rx);
7948c2ecf20Sopenharmony_ci	if (ret)
7958c2ecf20Sopenharmony_ci		return ret;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
7988c2ecf20Sopenharmony_ci				    &port_stats.tx);
7998c2ecf20Sopenharmony_ci	if (ret)
8008c2ecf20Sopenharmony_ci		return ret;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	memcpy(buf, &port_stats, size);
8038c2ecf20Sopenharmony_ci	return size;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
8078c2ecf20Sopenharmony_ci					  struct kobject *kobj,
8088c2ecf20Sopenharmony_ci					  struct bin_attribute *attr,
8098c2ecf20Sopenharmony_ci					  char *buf, loff_t offset,
8108c2ecf20Sopenharmony_ci					  size_t size)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
8138c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
8148c2ecf20Sopenharmony_ci	struct qlcnic_esw_statistics esw_stats;
8158c2ecf20Sopenharmony_ci	int ret;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (qlcnic_83xx_check(adapter))
8188c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	if (size != sizeof(struct qlcnic_esw_statistics))
8218c2ecf20Sopenharmony_ci		return -EINVAL;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
8248c2ecf20Sopenharmony_ci		return -EINVAL;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	memset(&esw_stats, 0, size);
8278c2ecf20Sopenharmony_ci	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
8288c2ecf20Sopenharmony_ci				       &esw_stats.rx);
8298c2ecf20Sopenharmony_ci	if (ret)
8308c2ecf20Sopenharmony_ci		return ret;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
8338c2ecf20Sopenharmony_ci				       &esw_stats.tx);
8348c2ecf20Sopenharmony_ci	if (ret)
8358c2ecf20Sopenharmony_ci		return ret;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	memcpy(buf, &esw_stats, size);
8388c2ecf20Sopenharmony_ci	return size;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
8428c2ecf20Sopenharmony_ci					    struct kobject *kobj,
8438c2ecf20Sopenharmony_ci					    struct bin_attribute *attr,
8448c2ecf20Sopenharmony_ci					    char *buf, loff_t offset,
8458c2ecf20Sopenharmony_ci					    size_t size)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
8488c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
8498c2ecf20Sopenharmony_ci	int ret;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	if (qlcnic_83xx_check(adapter))
8528c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
8558c2ecf20Sopenharmony_ci		return -EINVAL;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
8588c2ecf20Sopenharmony_ci				     QLCNIC_QUERY_RX_COUNTER);
8598c2ecf20Sopenharmony_ci	if (ret)
8608c2ecf20Sopenharmony_ci		return ret;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
8638c2ecf20Sopenharmony_ci				     QLCNIC_QUERY_TX_COUNTER);
8648c2ecf20Sopenharmony_ci	if (ret)
8658c2ecf20Sopenharmony_ci		return ret;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	return size;
8688c2ecf20Sopenharmony_ci}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
8718c2ecf20Sopenharmony_ci					     struct kobject *kobj,
8728c2ecf20Sopenharmony_ci					     struct bin_attribute *attr,
8738c2ecf20Sopenharmony_ci					     char *buf, loff_t offset,
8748c2ecf20Sopenharmony_ci					     size_t size)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
8788c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
8798c2ecf20Sopenharmony_ci	int ret;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (qlcnic_83xx_check(adapter))
8828c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	if (offset >= adapter->ahw->max_vnic_func)
8858c2ecf20Sopenharmony_ci		return -EINVAL;
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
8888c2ecf20Sopenharmony_ci				     QLCNIC_QUERY_RX_COUNTER);
8898c2ecf20Sopenharmony_ci	if (ret)
8908c2ecf20Sopenharmony_ci		return ret;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
8938c2ecf20Sopenharmony_ci				     QLCNIC_QUERY_TX_COUNTER);
8948c2ecf20Sopenharmony_ci	if (ret)
8958c2ecf20Sopenharmony_ci		return ret;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	return size;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cistatic ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
9018c2ecf20Sopenharmony_ci					    struct kobject *kobj,
9028c2ecf20Sopenharmony_ci					    struct bin_attribute *attr,
9038c2ecf20Sopenharmony_ci					    char *buf, loff_t offset,
9048c2ecf20Sopenharmony_ci					    size_t size)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
9078c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
9088c2ecf20Sopenharmony_ci	struct qlcnic_pci_func_cfg *pci_cfg;
9098c2ecf20Sopenharmony_ci	struct qlcnic_pci_info *pci_info;
9108c2ecf20Sopenharmony_ci	int i, ret;
9118c2ecf20Sopenharmony_ci	u32 count;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
9148c2ecf20Sopenharmony_ci	if (!pci_info)
9158c2ecf20Sopenharmony_ci		return -ENOMEM;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	ret = qlcnic_get_pci_info(adapter, pci_info);
9188c2ecf20Sopenharmony_ci	if (ret) {
9198c2ecf20Sopenharmony_ci		kfree(pci_info);
9208c2ecf20Sopenharmony_ci		return ret;
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
9248c2ecf20Sopenharmony_ci	count = size / sizeof(struct qlcnic_pci_func_cfg);
9258c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
9268c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
9278c2ecf20Sopenharmony_ci		pci_cfg[i].pci_func = pci_info[i].id;
9288c2ecf20Sopenharmony_ci		pci_cfg[i].func_type = pci_info[i].type;
9298c2ecf20Sopenharmony_ci		pci_cfg[i].func_state = 0;
9308c2ecf20Sopenharmony_ci		pci_cfg[i].port_num = pci_info[i].default_port;
9318c2ecf20Sopenharmony_ci		pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
9328c2ecf20Sopenharmony_ci		pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
9338c2ecf20Sopenharmony_ci		memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	kfree(pci_info);
9378c2ecf20Sopenharmony_ci	return size;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
9418c2ecf20Sopenharmony_ci						    struct kobject *kobj,
9428c2ecf20Sopenharmony_ci						    struct bin_attribute *attr,
9438c2ecf20Sopenharmony_ci						    char *buf, loff_t offset,
9448c2ecf20Sopenharmony_ci						    size_t size)
9458c2ecf20Sopenharmony_ci{
9468c2ecf20Sopenharmony_ci	unsigned char *p_read_buf;
9478c2ecf20Sopenharmony_ci	int  ret, count;
9488c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
9498c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	if (!size)
9528c2ecf20Sopenharmony_ci		return -EINVAL;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	count = size / sizeof(u32);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	if (size % sizeof(u32))
9578c2ecf20Sopenharmony_ci		count++;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
9608c2ecf20Sopenharmony_ci	if (!p_read_buf)
9618c2ecf20Sopenharmony_ci		return -ENOMEM;
9628c2ecf20Sopenharmony_ci	if (qlcnic_83xx_lock_flash(adapter) != 0) {
9638c2ecf20Sopenharmony_ci		kfree(p_read_buf);
9648c2ecf20Sopenharmony_ci		return -EIO;
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
9688c2ecf20Sopenharmony_ci						count);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	if (ret) {
9718c2ecf20Sopenharmony_ci		qlcnic_83xx_unlock_flash(adapter);
9728c2ecf20Sopenharmony_ci		kfree(p_read_buf);
9738c2ecf20Sopenharmony_ci		return ret;
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	qlcnic_83xx_unlock_flash(adapter);
9778c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)p_read_buf, count);
9788c2ecf20Sopenharmony_ci	memcpy(buf, p_read_buf, size);
9798c2ecf20Sopenharmony_ci	kfree(p_read_buf);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	return size;
9828c2ecf20Sopenharmony_ci}
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_cistatic int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
9858c2ecf20Sopenharmony_ci					      char *buf, loff_t offset,
9868c2ecf20Sopenharmony_ci					      size_t size)
9878c2ecf20Sopenharmony_ci{
9888c2ecf20Sopenharmony_ci	int  i, ret, count;
9898c2ecf20Sopenharmony_ci	unsigned char *p_cache, *p_src;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
9928c2ecf20Sopenharmony_ci	if (!p_cache)
9938c2ecf20Sopenharmony_ci		return -ENOMEM;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	count = size / sizeof(u32);
9968c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, count);
9978c2ecf20Sopenharmony_ci	memcpy(p_cache, buf, size);
9988c2ecf20Sopenharmony_ci	p_src = p_cache;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	if (qlcnic_83xx_lock_flash(adapter) != 0) {
10018c2ecf20Sopenharmony_ci		kfree(p_cache);
10028c2ecf20Sopenharmony_ci		return -EIO;
10038c2ecf20Sopenharmony_ci	}
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
10068c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_enable_flash_write(adapter);
10078c2ecf20Sopenharmony_ci		if (ret) {
10088c2ecf20Sopenharmony_ci			kfree(p_cache);
10098c2ecf20Sopenharmony_ci			qlcnic_83xx_unlock_flash(adapter);
10108c2ecf20Sopenharmony_ci			return -EIO;
10118c2ecf20Sopenharmony_ci		}
10128c2ecf20Sopenharmony_ci	}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
10158c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
10168c2ecf20Sopenharmony_ci						   (u32 *)p_src,
10178c2ecf20Sopenharmony_ci						   QLC_83XX_FLASH_WRITE_MAX);
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci		if (ret) {
10208c2ecf20Sopenharmony_ci			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
10218c2ecf20Sopenharmony_ci				ret = qlcnic_83xx_disable_flash_write(adapter);
10228c2ecf20Sopenharmony_ci				if (ret) {
10238c2ecf20Sopenharmony_ci					kfree(p_cache);
10248c2ecf20Sopenharmony_ci					qlcnic_83xx_unlock_flash(adapter);
10258c2ecf20Sopenharmony_ci					return -EIO;
10268c2ecf20Sopenharmony_ci				}
10278c2ecf20Sopenharmony_ci			}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci			kfree(p_cache);
10308c2ecf20Sopenharmony_ci			qlcnic_83xx_unlock_flash(adapter);
10318c2ecf20Sopenharmony_ci			return -EIO;
10328c2ecf20Sopenharmony_ci		}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci		p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
10358c2ecf20Sopenharmony_ci		offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
10368c2ecf20Sopenharmony_ci	}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
10398c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_disable_flash_write(adapter);
10408c2ecf20Sopenharmony_ci		if (ret) {
10418c2ecf20Sopenharmony_ci			kfree(p_cache);
10428c2ecf20Sopenharmony_ci			qlcnic_83xx_unlock_flash(adapter);
10438c2ecf20Sopenharmony_ci			return -EIO;
10448c2ecf20Sopenharmony_ci		}
10458c2ecf20Sopenharmony_ci	}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	kfree(p_cache);
10488c2ecf20Sopenharmony_ci	qlcnic_83xx_unlock_flash(adapter);
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	return 0;
10518c2ecf20Sopenharmony_ci}
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_cistatic int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
10548c2ecf20Sopenharmony_ci					 char *buf, loff_t offset, size_t size)
10558c2ecf20Sopenharmony_ci{
10568c2ecf20Sopenharmony_ci	int  i, ret, count;
10578c2ecf20Sopenharmony_ci	unsigned char *p_cache, *p_src;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
10608c2ecf20Sopenharmony_ci	if (!p_cache)
10618c2ecf20Sopenharmony_ci		return -ENOMEM;
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
10648c2ecf20Sopenharmony_ci	memcpy(p_cache, buf, size);
10658c2ecf20Sopenharmony_ci	p_src = p_cache;
10668c2ecf20Sopenharmony_ci	count = size / sizeof(u32);
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	if (qlcnic_83xx_lock_flash(adapter) != 0) {
10698c2ecf20Sopenharmony_ci		kfree(p_cache);
10708c2ecf20Sopenharmony_ci		return -EIO;
10718c2ecf20Sopenharmony_ci	}
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
10748c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_enable_flash_write(adapter);
10758c2ecf20Sopenharmony_ci		if (ret) {
10768c2ecf20Sopenharmony_ci			kfree(p_cache);
10778c2ecf20Sopenharmony_ci			qlcnic_83xx_unlock_flash(adapter);
10788c2ecf20Sopenharmony_ci			return -EIO;
10798c2ecf20Sopenharmony_ci		}
10808c2ecf20Sopenharmony_ci	}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
10838c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
10848c2ecf20Sopenharmony_ci		if (ret) {
10858c2ecf20Sopenharmony_ci			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
10868c2ecf20Sopenharmony_ci				ret = qlcnic_83xx_disable_flash_write(adapter);
10878c2ecf20Sopenharmony_ci				if (ret) {
10888c2ecf20Sopenharmony_ci					kfree(p_cache);
10898c2ecf20Sopenharmony_ci					qlcnic_83xx_unlock_flash(adapter);
10908c2ecf20Sopenharmony_ci					return -EIO;
10918c2ecf20Sopenharmony_ci				}
10928c2ecf20Sopenharmony_ci			}
10938c2ecf20Sopenharmony_ci			kfree(p_cache);
10948c2ecf20Sopenharmony_ci			qlcnic_83xx_unlock_flash(adapter);
10958c2ecf20Sopenharmony_ci			return -EIO;
10968c2ecf20Sopenharmony_ci		}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci		p_src = p_src + sizeof(u32);
10998c2ecf20Sopenharmony_ci		offset = offset + sizeof(u32);
11008c2ecf20Sopenharmony_ci	}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
11038c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_disable_flash_write(adapter);
11048c2ecf20Sopenharmony_ci		if (ret) {
11058c2ecf20Sopenharmony_ci			kfree(p_cache);
11068c2ecf20Sopenharmony_ci			qlcnic_83xx_unlock_flash(adapter);
11078c2ecf20Sopenharmony_ci			return -EIO;
11088c2ecf20Sopenharmony_ci		}
11098c2ecf20Sopenharmony_ci	}
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	kfree(p_cache);
11128c2ecf20Sopenharmony_ci	qlcnic_83xx_unlock_flash(adapter);
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	return 0;
11158c2ecf20Sopenharmony_ci}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_cistatic ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
11188c2ecf20Sopenharmony_ci						     struct kobject *kobj,
11198c2ecf20Sopenharmony_ci						     struct bin_attribute *attr,
11208c2ecf20Sopenharmony_ci						     char *buf, loff_t offset,
11218c2ecf20Sopenharmony_ci						     size_t size)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	int  ret;
11248c2ecf20Sopenharmony_ci	static int flash_mode;
11258c2ecf20Sopenharmony_ci	unsigned long data;
11268c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
11278c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	ret = kstrtoul(buf, 16, &data);
11308c2ecf20Sopenharmony_ci	if (ret)
11318c2ecf20Sopenharmony_ci		return ret;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	switch (data) {
11348c2ecf20Sopenharmony_ci	case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
11358c2ecf20Sopenharmony_ci		flash_mode = QLC_83XX_ERASE_MODE;
11368c2ecf20Sopenharmony_ci		ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
11378c2ecf20Sopenharmony_ci		if (ret) {
11388c2ecf20Sopenharmony_ci			dev_err(&adapter->pdev->dev,
11398c2ecf20Sopenharmony_ci				"%s failed at %d\n", __func__, __LINE__);
11408c2ecf20Sopenharmony_ci			return -EIO;
11418c2ecf20Sopenharmony_ci		}
11428c2ecf20Sopenharmony_ci		break;
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	case QLC_83XX_FLASH_BULK_WRITE_CMD:
11458c2ecf20Sopenharmony_ci		flash_mode = QLC_83XX_BULK_WRITE_MODE;
11468c2ecf20Sopenharmony_ci		break;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	case QLC_83XX_FLASH_WRITE_CMD:
11498c2ecf20Sopenharmony_ci		flash_mode = QLC_83XX_WRITE_MODE;
11508c2ecf20Sopenharmony_ci		break;
11518c2ecf20Sopenharmony_ci	default:
11528c2ecf20Sopenharmony_ci		if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
11538c2ecf20Sopenharmony_ci			ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
11548c2ecf20Sopenharmony_ci								 offset, size);
11558c2ecf20Sopenharmony_ci			if (ret) {
11568c2ecf20Sopenharmony_ci				dev_err(&adapter->pdev->dev,
11578c2ecf20Sopenharmony_ci					"%s failed at %d\n",
11588c2ecf20Sopenharmony_ci					__func__, __LINE__);
11598c2ecf20Sopenharmony_ci				return -EIO;
11608c2ecf20Sopenharmony_ci			}
11618c2ecf20Sopenharmony_ci		}
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci		if (flash_mode == QLC_83XX_WRITE_MODE) {
11648c2ecf20Sopenharmony_ci			ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
11658c2ecf20Sopenharmony_ci							    offset, size);
11668c2ecf20Sopenharmony_ci			if (ret) {
11678c2ecf20Sopenharmony_ci				dev_err(&adapter->pdev->dev,
11688c2ecf20Sopenharmony_ci					"%s failed at %d\n", __func__,
11698c2ecf20Sopenharmony_ci					__LINE__);
11708c2ecf20Sopenharmony_ci				return -EIO;
11718c2ecf20Sopenharmony_ci			}
11728c2ecf20Sopenharmony_ci		}
11738c2ecf20Sopenharmony_ci	}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	return size;
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_cistatic const struct device_attribute dev_attr_bridged_mode = {
11798c2ecf20Sopenharmony_ci	.attr = { .name = "bridged_mode", .mode = 0644 },
11808c2ecf20Sopenharmony_ci	.show = qlcnic_show_bridged_mode,
11818c2ecf20Sopenharmony_ci	.store = qlcnic_store_bridged_mode,
11828c2ecf20Sopenharmony_ci};
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_cistatic const struct device_attribute dev_attr_diag_mode = {
11858c2ecf20Sopenharmony_ci	.attr = { .name = "diag_mode", .mode = 0644 },
11868c2ecf20Sopenharmony_ci	.show = qlcnic_show_diag_mode,
11878c2ecf20Sopenharmony_ci	.store = qlcnic_store_diag_mode,
11888c2ecf20Sopenharmony_ci};
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cistatic const struct device_attribute dev_attr_beacon = {
11918c2ecf20Sopenharmony_ci	.attr = { .name = "beacon", .mode = 0644 },
11928c2ecf20Sopenharmony_ci	.show = qlcnic_show_beacon,
11938c2ecf20Sopenharmony_ci	.store = qlcnic_store_beacon,
11948c2ecf20Sopenharmony_ci};
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_crb = {
11978c2ecf20Sopenharmony_ci	.attr = { .name = "crb", .mode = 0644 },
11988c2ecf20Sopenharmony_ci	.size = 0,
11998c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_read_crb,
12008c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_write_crb,
12018c2ecf20Sopenharmony_ci};
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_mem = {
12048c2ecf20Sopenharmony_ci	.attr = { .name = "mem", .mode = 0644 },
12058c2ecf20Sopenharmony_ci	.size = 0,
12068c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_read_mem,
12078c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_write_mem,
12088c2ecf20Sopenharmony_ci};
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_npar_config = {
12118c2ecf20Sopenharmony_ci	.attr = { .name = "npar_config", .mode = 0644 },
12128c2ecf20Sopenharmony_ci	.size = 0,
12138c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_read_npar_config,
12148c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_write_npar_config,
12158c2ecf20Sopenharmony_ci};
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_pci_config = {
12188c2ecf20Sopenharmony_ci	.attr = { .name = "pci_config", .mode = 0644 },
12198c2ecf20Sopenharmony_ci	.size = 0,
12208c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_read_pci_config,
12218c2ecf20Sopenharmony_ci	.write = NULL,
12228c2ecf20Sopenharmony_ci};
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_port_stats = {
12258c2ecf20Sopenharmony_ci	.attr = { .name = "port_stats", .mode = 0644 },
12268c2ecf20Sopenharmony_ci	.size = 0,
12278c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_get_port_stats,
12288c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_clear_port_stats,
12298c2ecf20Sopenharmony_ci};
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_esw_stats = {
12328c2ecf20Sopenharmony_ci	.attr = { .name = "esw_stats", .mode = 0644 },
12338c2ecf20Sopenharmony_ci	.size = 0,
12348c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_get_esw_stats,
12358c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_clear_esw_stats,
12368c2ecf20Sopenharmony_ci};
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_esw_config = {
12398c2ecf20Sopenharmony_ci	.attr = { .name = "esw_config", .mode = 0644 },
12408c2ecf20Sopenharmony_ci	.size = 0,
12418c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_read_esw_config,
12428c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_write_esw_config,
12438c2ecf20Sopenharmony_ci};
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_pm_config = {
12468c2ecf20Sopenharmony_ci	.attr = { .name = "pm_config", .mode = 0644 },
12478c2ecf20Sopenharmony_ci	.size = 0,
12488c2ecf20Sopenharmony_ci	.read = qlcnic_sysfs_read_pm_config,
12498c2ecf20Sopenharmony_ci	.write = qlcnic_sysfs_write_pm_config,
12508c2ecf20Sopenharmony_ci};
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_cistatic const struct bin_attribute bin_attr_flash = {
12538c2ecf20Sopenharmony_ci	.attr = { .name = "flash", .mode = 0644 },
12548c2ecf20Sopenharmony_ci	.size = 0,
12558c2ecf20Sopenharmony_ci	.read = qlcnic_83xx_sysfs_flash_read_handler,
12568c2ecf20Sopenharmony_ci	.write = qlcnic_83xx_sysfs_flash_write_handler,
12578c2ecf20Sopenharmony_ci};
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci#ifdef CONFIG_QLCNIC_HWMON
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_cistatic ssize_t qlcnic_hwmon_show_temp(struct device *dev,
12628c2ecf20Sopenharmony_ci				      struct device_attribute *dev_attr,
12638c2ecf20Sopenharmony_ci				      char *buf)
12648c2ecf20Sopenharmony_ci{
12658c2ecf20Sopenharmony_ci	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
12668c2ecf20Sopenharmony_ci	unsigned int temperature = 0, value = 0;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	if (qlcnic_83xx_check(adapter))
12698c2ecf20Sopenharmony_ci		value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
12708c2ecf20Sopenharmony_ci	else if (qlcnic_82xx_check(adapter))
12718c2ecf20Sopenharmony_ci		value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	temperature = qlcnic_get_temp_val(value);
12748c2ecf20Sopenharmony_ci	/* display millidegree celcius */
12758c2ecf20Sopenharmony_ci	temperature *= 1000;
12768c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", temperature);
12778c2ecf20Sopenharmony_ci}
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci/* hwmon-sysfs attributes */
12808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_input, 0444,
12818c2ecf20Sopenharmony_ci			  qlcnic_hwmon_show_temp, NULL, 1);
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_cistatic struct attribute *qlcnic_hwmon_attrs[] = {
12848c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
12858c2ecf20Sopenharmony_ci	NULL
12868c2ecf20Sopenharmony_ci};
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(qlcnic_hwmon);
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_civoid qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
12918c2ecf20Sopenharmony_ci{
12928c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
12938c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/* Skip hwmon registration for a VF device */
12968c2ecf20Sopenharmony_ci	if (qlcnic_sriov_vf_check(adapter)) {
12978c2ecf20Sopenharmony_ci		adapter->ahw->hwmon_dev = NULL;
12988c2ecf20Sopenharmony_ci		return;
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci	hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
13018c2ecf20Sopenharmony_ci						      adapter,
13028c2ecf20Sopenharmony_ci						      qlcnic_hwmon_groups);
13038c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon_dev)) {
13048c2ecf20Sopenharmony_ci		dev_err(dev, "Cannot register with hwmon, err=%ld\n",
13058c2ecf20Sopenharmony_ci			PTR_ERR(hwmon_dev));
13068c2ecf20Sopenharmony_ci		hwmon_dev = NULL;
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci	adapter->ahw->hwmon_dev = hwmon_dev;
13098c2ecf20Sopenharmony_ci}
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_civoid qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
13128c2ecf20Sopenharmony_ci{
13138c2ecf20Sopenharmony_ci	struct device *hwmon_dev = adapter->ahw->hwmon_dev;
13148c2ecf20Sopenharmony_ci	if (hwmon_dev) {
13158c2ecf20Sopenharmony_ci		hwmon_device_unregister(hwmon_dev);
13168c2ecf20Sopenharmony_ci		adapter->ahw->hwmon_dev = NULL;
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci}
13198c2ecf20Sopenharmony_ci#endif
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_civoid qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
13228c2ecf20Sopenharmony_ci{
13238c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
13268c2ecf20Sopenharmony_ci		if (device_create_file(dev, &dev_attr_bridged_mode))
13278c2ecf20Sopenharmony_ci			dev_warn(dev,
13288c2ecf20Sopenharmony_ci				 "failed to create bridged_mode sysfs entry\n");
13298c2ecf20Sopenharmony_ci}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_civoid qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
13328c2ecf20Sopenharmony_ci{
13338c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
13368c2ecf20Sopenharmony_ci		device_remove_file(dev, &dev_attr_bridged_mode);
13378c2ecf20Sopenharmony_ci}
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_cistatic void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
13408c2ecf20Sopenharmony_ci{
13418c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_port_stats))
13448c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create port stats sysfs entry");
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
13478c2ecf20Sopenharmony_ci		return;
13488c2ecf20Sopenharmony_ci	if (device_create_file(dev, &dev_attr_diag_mode))
13498c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create diag_mode sysfs entry\n");
13508c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_crb))
13518c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create crb sysfs entry\n");
13528c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_mem))
13538c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create mem sysfs entry\n");
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
13568c2ecf20Sopenharmony_ci		return;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_pci_config))
13598c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create pci config sysfs entry");
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	if (device_create_file(dev, &dev_attr_beacon))
13628c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create beacon sysfs entry");
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
13658c2ecf20Sopenharmony_ci		return;
13668c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_esw_config))
13678c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create esw config sysfs entry");
13688c2ecf20Sopenharmony_ci	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
13698c2ecf20Sopenharmony_ci		return;
13708c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_npar_config))
13718c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create npar config sysfs entry");
13728c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_pm_config))
13738c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create pm config sysfs entry");
13748c2ecf20Sopenharmony_ci	if (device_create_bin_file(dev, &bin_attr_esw_stats))
13758c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create eswitch stats sysfs entry");
13768c2ecf20Sopenharmony_ci}
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_cistatic void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
13798c2ecf20Sopenharmony_ci{
13808c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_port_stats);
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
13858c2ecf20Sopenharmony_ci		return;
13868c2ecf20Sopenharmony_ci	device_remove_file(dev, &dev_attr_diag_mode);
13878c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_crb);
13888c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_mem);
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
13918c2ecf20Sopenharmony_ci		return;
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_pci_config);
13948c2ecf20Sopenharmony_ci	device_remove_file(dev, &dev_attr_beacon);
13958c2ecf20Sopenharmony_ci	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
13968c2ecf20Sopenharmony_ci		return;
13978c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_esw_config);
13988c2ecf20Sopenharmony_ci	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
13998c2ecf20Sopenharmony_ci		return;
14008c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_npar_config);
14018c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_pm_config);
14028c2ecf20Sopenharmony_ci	device_remove_bin_file(dev, &bin_attr_esw_stats);
14038c2ecf20Sopenharmony_ci}
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_civoid qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
14068c2ecf20Sopenharmony_ci{
14078c2ecf20Sopenharmony_ci	qlcnic_create_diag_entries(adapter);
14088c2ecf20Sopenharmony_ci}
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_civoid qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
14118c2ecf20Sopenharmony_ci{
14128c2ecf20Sopenharmony_ci	qlcnic_remove_diag_entries(adapter);
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_civoid qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
14168c2ecf20Sopenharmony_ci{
14178c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	qlcnic_create_diag_entries(adapter);
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
14228c2ecf20Sopenharmony_ci		dev_info(dev, "failed to create flash sysfs entry\n");
14238c2ecf20Sopenharmony_ci}
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_civoid qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
14268c2ecf20Sopenharmony_ci{
14278c2ecf20Sopenharmony_ci	struct device *dev = &adapter->pdev->dev;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	qlcnic_remove_diag_entries(adapter);
14308c2ecf20Sopenharmony_ci	sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);
14318c2ecf20Sopenharmony_ci}
1432