Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * QLogic qlcnic NIC Driver
0004  * Copyright (c) 2009-2013 QLogic Corporation
0005  */
0006 
0007 #include <linux/slab.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/swab.h>
0010 #include <linux/dma-mapping.h>
0011 #include <net/ip.h>
0012 #include <linux/ipv6.h>
0013 #include <linux/inetdevice.h>
0014 #include <linux/sysfs.h>
0015 #include <linux/aer.h>
0016 #include <linux/log2.h>
0017 #ifdef CONFIG_QLCNIC_HWMON
0018 #include <linux/hwmon.h>
0019 #include <linux/hwmon-sysfs.h>
0020 #endif
0021 
0022 #include "qlcnic.h"
0023 #include "qlcnic_hw.h"
0024 
0025 int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
0026 {
0027     return -EOPNOTSUPP;
0028 }
0029 
0030 int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
0031 {
0032     return -EOPNOTSUPP;
0033 }
0034 
0035 static ssize_t qlcnic_store_bridged_mode(struct device *dev,
0036                      struct device_attribute *attr,
0037                      const char *buf, size_t len)
0038 {
0039     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0040     unsigned long new;
0041     int ret = -EINVAL;
0042 
0043     if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
0044         goto err_out;
0045 
0046     if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
0047         goto err_out;
0048 
0049     if (kstrtoul(buf, 2, &new))
0050         goto err_out;
0051 
0052     if (!qlcnic_config_bridged_mode(adapter, !!new))
0053         ret = len;
0054 
0055 err_out:
0056     return ret;
0057 }
0058 
0059 static ssize_t qlcnic_show_bridged_mode(struct device *dev,
0060                     struct device_attribute *attr,
0061                     char *buf)
0062 {
0063     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0064     int bridged_mode = 0;
0065 
0066     if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
0067         bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
0068 
0069     return sprintf(buf, "%d\n", bridged_mode);
0070 }
0071 
0072 static ssize_t qlcnic_store_diag_mode(struct device *dev,
0073                       struct device_attribute *attr,
0074                       const char *buf, size_t len)
0075 {
0076     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0077     unsigned long new;
0078 
0079     if (kstrtoul(buf, 2, &new))
0080         return -EINVAL;
0081 
0082     if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
0083         adapter->flags ^= QLCNIC_DIAG_ENABLED;
0084 
0085     return len;
0086 }
0087 
0088 static ssize_t qlcnic_show_diag_mode(struct device *dev,
0089                      struct device_attribute *attr, char *buf)
0090 {
0091     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0092     return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
0093 }
0094 
0095 static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
0096                   u8 *state, u8 *rate)
0097 {
0098     *rate = LSB(beacon);
0099     *state = MSB(beacon);
0100 
0101     QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
0102 
0103     if (!*state) {
0104         *rate = __QLCNIC_MAX_LED_RATE;
0105         return 0;
0106     } else if (*state > __QLCNIC_MAX_LED_STATE) {
0107         return -EINVAL;
0108     }
0109 
0110     if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
0111         return -EINVAL;
0112 
0113     return 0;
0114 }
0115 
0116 static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
0117                     const char *buf, size_t len)
0118 {
0119     struct qlcnic_hardware_context *ahw = adapter->ahw;
0120     unsigned long h_beacon;
0121     int err;
0122 
0123     if (test_bit(__QLCNIC_RESETTING, &adapter->state))
0124         return -EIO;
0125 
0126     if (kstrtoul(buf, 2, &h_beacon))
0127         return -EINVAL;
0128 
0129     qlcnic_get_beacon_state(adapter);
0130 
0131     if (ahw->beacon_state == h_beacon)
0132         return len;
0133 
0134     rtnl_lock();
0135     if (!ahw->beacon_state) {
0136         if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
0137             rtnl_unlock();
0138             return -EBUSY;
0139         }
0140     }
0141 
0142     if (h_beacon)
0143         err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
0144     else
0145         err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
0146     if (!err)
0147         ahw->beacon_state = h_beacon;
0148 
0149     if (!ahw->beacon_state)
0150         clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
0151 
0152     rtnl_unlock();
0153     return len;
0154 }
0155 
0156 static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
0157                     const char *buf, size_t len)
0158 {
0159     struct qlcnic_hardware_context *ahw = adapter->ahw;
0160     int err, drv_sds_rings = adapter->drv_sds_rings;
0161     u16 beacon;
0162     u8 b_state, b_rate;
0163 
0164     if (len != sizeof(u16))
0165         return -EINVAL;
0166 
0167     memcpy(&beacon, buf, sizeof(u16));
0168     err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
0169     if (err)
0170         return err;
0171 
0172     qlcnic_get_beacon_state(adapter);
0173 
0174     if (ahw->beacon_state == b_state)
0175         return len;
0176 
0177     rtnl_lock();
0178     if (!ahw->beacon_state) {
0179         if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
0180             rtnl_unlock();
0181             return -EBUSY;
0182         }
0183     }
0184 
0185     if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
0186         err = -EIO;
0187         goto out;
0188     }
0189 
0190     if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
0191         err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
0192         if (err)
0193             goto out;
0194         set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
0195     }
0196 
0197     err = qlcnic_config_led(adapter, b_state, b_rate);
0198     if (!err) {
0199         err = len;
0200         ahw->beacon_state = b_state;
0201     }
0202 
0203     if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
0204         qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
0205 
0206 out:
0207     if (!ahw->beacon_state)
0208         clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
0209     rtnl_unlock();
0210 
0211     return err;
0212 }
0213 
0214 static ssize_t qlcnic_store_beacon(struct device *dev,
0215                    struct device_attribute *attr,
0216                    const char *buf, size_t len)
0217 {
0218     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0219     int err = 0;
0220 
0221     if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
0222         dev_warn(dev,
0223              "LED test not supported in non privileged mode\n");
0224         return -EOPNOTSUPP;
0225     }
0226 
0227     if (qlcnic_82xx_check(adapter))
0228         err = qlcnic_82xx_store_beacon(adapter, buf, len);
0229     else if (qlcnic_83xx_check(adapter))
0230         err = qlcnic_83xx_store_beacon(adapter, buf, len);
0231     else
0232         return -EIO;
0233 
0234     return err;
0235 }
0236 
0237 static ssize_t qlcnic_show_beacon(struct device *dev,
0238                   struct device_attribute *attr, char *buf)
0239 {
0240     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0241 
0242     return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
0243 }
0244 
0245 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
0246                      loff_t offset, size_t size)
0247 {
0248     size_t crb_size = 4;
0249 
0250     if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
0251         return -EIO;
0252 
0253     if (offset < QLCNIC_PCI_CRBSPACE) {
0254         if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
0255                   QLCNIC_PCI_CAMQM_END))
0256             crb_size = 8;
0257         else
0258             return -EINVAL;
0259     }
0260 
0261     if ((size != crb_size) || (offset & (crb_size-1)))
0262         return  -EINVAL;
0263 
0264     return 0;
0265 }
0266 
0267 static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
0268                      struct bin_attribute *attr, char *buf,
0269                      loff_t offset, size_t size)
0270 {
0271     struct device *dev = kobj_to_dev(kobj);
0272     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0273     int ret;
0274 
0275     ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
0276     if (ret != 0)
0277         return ret;
0278     qlcnic_read_crb(adapter, buf, offset, size);
0279     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0280 
0281     return size;
0282 }
0283 
0284 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
0285                       struct bin_attribute *attr, char *buf,
0286                       loff_t offset, size_t size)
0287 {
0288     struct device *dev = kobj_to_dev(kobj);
0289     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0290     int ret;
0291 
0292     ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
0293     if (ret != 0)
0294         return ret;
0295 
0296     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0297     qlcnic_write_crb(adapter, buf, offset, size);
0298     return size;
0299 }
0300 
0301 static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
0302                      loff_t offset, size_t size)
0303 {
0304     if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
0305         return -EIO;
0306 
0307     if ((size != 8) || (offset & 0x7))
0308         return  -EIO;
0309 
0310     return 0;
0311 }
0312 
0313 static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
0314                      struct bin_attribute *attr, char *buf,
0315                      loff_t offset, size_t size)
0316 {
0317     struct device *dev = kobj_to_dev(kobj);
0318     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0319     u64 data;
0320     int ret;
0321 
0322     ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
0323     if (ret != 0)
0324         return ret;
0325 
0326     if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
0327         return -EIO;
0328 
0329     memcpy(buf, &data, size);
0330     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0331 
0332     return size;
0333 }
0334 
0335 static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
0336                       struct bin_attribute *attr, char *buf,
0337                       loff_t offset, size_t size)
0338 {
0339     struct device *dev = kobj_to_dev(kobj);
0340     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0341     u64 data;
0342     int ret;
0343 
0344     ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
0345     if (ret != 0)
0346         return ret;
0347 
0348     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0349     memcpy(&data, buf, size);
0350 
0351     if (qlcnic_pci_mem_write_2M(adapter, offset, data))
0352         return -EIO;
0353 
0354     return size;
0355 }
0356 
0357 int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
0358 {
0359     int i;
0360 
0361     for (i = 0; i < adapter->ahw->total_nic_func; i++) {
0362         if (adapter->npars[i].pci_func == pci_func)
0363             return i;
0364     }
0365 
0366     dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
0367     return -EINVAL;
0368 }
0369 
0370 static int validate_pm_config(struct qlcnic_adapter *adapter,
0371                   struct qlcnic_pm_func_cfg *pm_cfg, int count)
0372 {
0373     u8 src_pci_func, s_esw_id, d_esw_id;
0374     u8 dest_pci_func;
0375     int i, src_index, dest_index;
0376 
0377     for (i = 0; i < count; i++) {
0378         src_pci_func = pm_cfg[i].pci_func;
0379         dest_pci_func = pm_cfg[i].dest_npar;
0380         src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
0381         if (src_index < 0)
0382             return -EINVAL;
0383 
0384         dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
0385         if (dest_index < 0)
0386             return -EINVAL;
0387 
0388         s_esw_id = adapter->npars[src_index].phy_port;
0389         d_esw_id = adapter->npars[dest_index].phy_port;
0390 
0391         if (s_esw_id != d_esw_id)
0392             return -EINVAL;
0393     }
0394 
0395     return 0;
0396 }
0397 
0398 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
0399                         struct kobject *kobj,
0400                         struct bin_attribute *attr,
0401                         char *buf, loff_t offset,
0402                         size_t size)
0403 {
0404     struct device *dev = kobj_to_dev(kobj);
0405     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0406     struct qlcnic_pm_func_cfg *pm_cfg;
0407     u32 id, action, pci_func;
0408     int count, rem, i, ret, index;
0409 
0410     count   = size / sizeof(struct qlcnic_pm_func_cfg);
0411     rem = size % sizeof(struct qlcnic_pm_func_cfg);
0412     if (rem)
0413         return -EINVAL;
0414 
0415     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0416     pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
0417     ret = validate_pm_config(adapter, pm_cfg, count);
0418 
0419     if (ret)
0420         return ret;
0421     for (i = 0; i < count; i++) {
0422         pci_func = pm_cfg[i].pci_func;
0423         action = !!pm_cfg[i].action;
0424         index = qlcnic_is_valid_nic_func(adapter, pci_func);
0425         if (index < 0)
0426             return -EINVAL;
0427 
0428         id = adapter->npars[index].phy_port;
0429         ret = qlcnic_config_port_mirroring(adapter, id,
0430                            action, pci_func);
0431         if (ret)
0432             return ret;
0433     }
0434 
0435     for (i = 0; i < count; i++) {
0436         pci_func = pm_cfg[i].pci_func;
0437         index = qlcnic_is_valid_nic_func(adapter, pci_func);
0438         if (index < 0)
0439             return -EINVAL;
0440         id = adapter->npars[index].phy_port;
0441         adapter->npars[index].enable_pm = !!pm_cfg[i].action;
0442         adapter->npars[index].dest_npar = id;
0443     }
0444 
0445     return size;
0446 }
0447 
0448 static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
0449                        struct kobject *kobj,
0450                        struct bin_attribute *attr,
0451                        char *buf, loff_t offset,
0452                        size_t size)
0453 {
0454     struct device *dev = kobj_to_dev(kobj);
0455     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0456     struct qlcnic_pm_func_cfg *pm_cfg;
0457     u8 pci_func;
0458     u32 count;
0459     int i;
0460 
0461     memset(buf, 0, size);
0462     pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
0463     count = size / sizeof(struct qlcnic_pm_func_cfg);
0464     for (i = 0; i < adapter->ahw->total_nic_func; i++) {
0465         pci_func = adapter->npars[i].pci_func;
0466         if (pci_func >= count) {
0467             dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
0468                 __func__, adapter->ahw->total_nic_func, count);
0469             continue;
0470         }
0471         if (!adapter->npars[i].eswitch_status)
0472             continue;
0473 
0474         pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
0475         pm_cfg[pci_func].dest_npar = 0;
0476         pm_cfg[pci_func].pci_func = i;
0477     }
0478     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0479     return size;
0480 }
0481 
0482 static int validate_esw_config(struct qlcnic_adapter *adapter,
0483                    struct qlcnic_esw_func_cfg *esw_cfg, int count)
0484 {
0485     struct qlcnic_hardware_context *ahw = adapter->ahw;
0486     int i, ret;
0487     u32 op_mode;
0488     u8 pci_func;
0489 
0490     if (qlcnic_82xx_check(adapter))
0491         op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
0492     else
0493         op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
0494 
0495     for (i = 0; i < count; i++) {
0496         pci_func = esw_cfg[i].pci_func;
0497         if (pci_func >= ahw->max_vnic_func)
0498             return -EINVAL;
0499 
0500         if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
0501             if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
0502                 return -EINVAL;
0503 
0504         switch (esw_cfg[i].op_mode) {
0505         case QLCNIC_PORT_DEFAULTS:
0506             if (qlcnic_82xx_check(adapter)) {
0507                 ret = QLC_DEV_GET_DRV(op_mode, pci_func);
0508             } else {
0509                 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
0510                                   pci_func);
0511                 esw_cfg[i].offload_flags = 0;
0512             }
0513 
0514             if (ret != QLCNIC_NON_PRIV_FUNC) {
0515                 if (esw_cfg[i].mac_anti_spoof != 0)
0516                     return -EINVAL;
0517                 if (esw_cfg[i].mac_override != 1)
0518                     return -EINVAL;
0519                 if (esw_cfg[i].promisc_mode != 1)
0520                     return -EINVAL;
0521             }
0522             break;
0523         case QLCNIC_ADD_VLAN:
0524             if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
0525                 return -EINVAL;
0526             if (!esw_cfg[i].op_type)
0527                 return -EINVAL;
0528             break;
0529         case QLCNIC_DEL_VLAN:
0530             if (!esw_cfg[i].op_type)
0531                 return -EINVAL;
0532             break;
0533         default:
0534             return -EINVAL;
0535         }
0536     }
0537 
0538     return 0;
0539 }
0540 
0541 static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
0542                          struct kobject *kobj,
0543                          struct bin_attribute *attr,
0544                          char *buf, loff_t offset,
0545                          size_t size)
0546 {
0547     struct device *dev = kobj_to_dev(kobj);
0548     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0549     struct qlcnic_esw_func_cfg *esw_cfg;
0550     struct qlcnic_npar_info *npar;
0551     int count, rem, i, ret;
0552     int index;
0553     u8 op_mode = 0, pci_func;
0554 
0555     count   = size / sizeof(struct qlcnic_esw_func_cfg);
0556     rem = size % sizeof(struct qlcnic_esw_func_cfg);
0557     if (rem)
0558         return -EINVAL;
0559 
0560     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0561     esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
0562     ret = validate_esw_config(adapter, esw_cfg, count);
0563     if (ret)
0564         return ret;
0565 
0566     for (i = 0; i < count; i++) {
0567         if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
0568             if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
0569                 return -EINVAL;
0570 
0571         if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
0572             continue;
0573 
0574         op_mode = esw_cfg[i].op_mode;
0575         qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
0576         esw_cfg[i].op_mode = op_mode;
0577         esw_cfg[i].pci_func = adapter->ahw->pci_func;
0578 
0579         switch (esw_cfg[i].op_mode) {
0580         case QLCNIC_PORT_DEFAULTS:
0581             qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
0582             rtnl_lock();
0583             qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
0584             rtnl_unlock();
0585             break;
0586         case QLCNIC_ADD_VLAN:
0587             qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
0588             break;
0589         case QLCNIC_DEL_VLAN:
0590             esw_cfg[i].vlan_id = 0;
0591             qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
0592             break;
0593         }
0594     }
0595 
0596     if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
0597         goto out;
0598 
0599     for (i = 0; i < count; i++) {
0600         pci_func = esw_cfg[i].pci_func;
0601         index = qlcnic_is_valid_nic_func(adapter, pci_func);
0602         if (index < 0)
0603             return -EINVAL;
0604         npar = &adapter->npars[index];
0605         switch (esw_cfg[i].op_mode) {
0606         case QLCNIC_PORT_DEFAULTS:
0607             npar->promisc_mode = esw_cfg[i].promisc_mode;
0608             npar->mac_override = esw_cfg[i].mac_override;
0609             npar->offload_flags = esw_cfg[i].offload_flags;
0610             npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
0611             npar->discard_tagged = esw_cfg[i].discard_tagged;
0612             break;
0613         case QLCNIC_ADD_VLAN:
0614             npar->pvid = esw_cfg[i].vlan_id;
0615             break;
0616         case QLCNIC_DEL_VLAN:
0617             npar->pvid = 0;
0618             break;
0619         }
0620     }
0621 out:
0622     return size;
0623 }
0624 
0625 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
0626                         struct kobject *kobj,
0627                         struct bin_attribute *attr,
0628                         char *buf, loff_t offset,
0629                         size_t size)
0630 {
0631     struct device *dev = kobj_to_dev(kobj);
0632     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0633     struct qlcnic_esw_func_cfg *esw_cfg;
0634     u8 pci_func;
0635     u32 count;
0636     int i;
0637 
0638     memset(buf, 0, size);
0639     esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
0640     count = size / sizeof(struct qlcnic_esw_func_cfg);
0641     for (i = 0; i < adapter->ahw->total_nic_func; i++) {
0642         pci_func = adapter->npars[i].pci_func;
0643         if (pci_func >= count) {
0644             dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
0645                 __func__, adapter->ahw->total_nic_func, count);
0646             continue;
0647         }
0648         if (!adapter->npars[i].eswitch_status)
0649             continue;
0650 
0651         esw_cfg[pci_func].pci_func = pci_func;
0652         if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
0653             return -EINVAL;
0654     }
0655     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0656     return size;
0657 }
0658 
0659 static int validate_npar_config(struct qlcnic_adapter *adapter,
0660                 struct qlcnic_npar_func_cfg *np_cfg,
0661                 int count)
0662 {
0663     u8 pci_func, i;
0664 
0665     for (i = 0; i < count; i++) {
0666         pci_func = np_cfg[i].pci_func;
0667         if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
0668             return -EINVAL;
0669 
0670         if (!IS_VALID_BW(np_cfg[i].min_bw) ||
0671             !IS_VALID_BW(np_cfg[i].max_bw))
0672             return -EINVAL;
0673     }
0674     return 0;
0675 }
0676 
0677 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
0678                           struct kobject *kobj,
0679                           struct bin_attribute *attr,
0680                           char *buf, loff_t offset,
0681                           size_t size)
0682 {
0683     struct device *dev = kobj_to_dev(kobj);
0684     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0685     struct qlcnic_info nic_info;
0686     struct qlcnic_npar_func_cfg *np_cfg;
0687     int i, count, rem, ret, index;
0688     u8 pci_func;
0689 
0690     count   = size / sizeof(struct qlcnic_npar_func_cfg);
0691     rem = size % sizeof(struct qlcnic_npar_func_cfg);
0692     if (rem)
0693         return -EINVAL;
0694 
0695     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0696     np_cfg = (struct qlcnic_npar_func_cfg *)buf;
0697     ret = validate_npar_config(adapter, np_cfg, count);
0698     if (ret)
0699         return ret;
0700 
0701     for (i = 0; i < count; i++) {
0702         pci_func = np_cfg[i].pci_func;
0703 
0704         memset(&nic_info, 0, sizeof(struct qlcnic_info));
0705         ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
0706         if (ret)
0707             return ret;
0708         nic_info.pci_func = pci_func;
0709         nic_info.min_tx_bw = np_cfg[i].min_bw;
0710         nic_info.max_tx_bw = np_cfg[i].max_bw;
0711         ret = qlcnic_set_nic_info(adapter, &nic_info);
0712         if (ret)
0713             return ret;
0714         index = qlcnic_is_valid_nic_func(adapter, pci_func);
0715         if (index < 0)
0716             return -EINVAL;
0717         adapter->npars[index].min_bw = nic_info.min_tx_bw;
0718         adapter->npars[index].max_bw = nic_info.max_tx_bw;
0719     }
0720 
0721     return size;
0722 }
0723 
0724 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
0725                          struct kobject *kobj,
0726                          struct bin_attribute *attr,
0727                          char *buf, loff_t offset,
0728                          size_t size)
0729 {
0730     struct device *dev = kobj_to_dev(kobj);
0731     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0732     struct qlcnic_npar_func_cfg *np_cfg;
0733     struct qlcnic_info nic_info;
0734     u8 pci_func;
0735     int i, ret;
0736     u32 count;
0737 
0738     memset(&nic_info, 0, sizeof(struct qlcnic_info));
0739     memset(buf, 0, size);
0740     np_cfg = (struct qlcnic_npar_func_cfg *)buf;
0741 
0742     count = size / sizeof(struct qlcnic_npar_func_cfg);
0743     for (i = 0; i < adapter->ahw->total_nic_func; i++) {
0744         if (adapter->npars[i].pci_func >= count) {
0745             dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
0746                 __func__, adapter->ahw->total_nic_func, count);
0747             continue;
0748         }
0749         if (!adapter->npars[i].eswitch_status)
0750             continue;
0751         pci_func = adapter->npars[i].pci_func;
0752         if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
0753             continue;
0754         ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
0755         if (ret)
0756             return ret;
0757 
0758         np_cfg[pci_func].pci_func = pci_func;
0759         np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
0760         np_cfg[pci_func].port_num = nic_info.phys_port;
0761         np_cfg[pci_func].fw_capab = nic_info.capabilities;
0762         np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
0763         np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
0764         np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
0765         np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
0766     }
0767     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
0768     return size;
0769 }
0770 
0771 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
0772                        struct kobject *kobj,
0773                        struct bin_attribute *attr,
0774                        char *buf, loff_t offset,
0775                        size_t size)
0776 {
0777     struct device *dev = kobj_to_dev(kobj);
0778     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0779     struct qlcnic_esw_statistics port_stats;
0780     int ret;
0781 
0782     if (qlcnic_83xx_check(adapter))
0783         return -EOPNOTSUPP;
0784 
0785     if (size != sizeof(struct qlcnic_esw_statistics))
0786         return -EINVAL;
0787 
0788     if (offset >= adapter->ahw->max_vnic_func)
0789         return -EINVAL;
0790 
0791     memset(&port_stats, 0, size);
0792     ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
0793                     &port_stats.rx);
0794     if (ret)
0795         return ret;
0796 
0797     ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
0798                     &port_stats.tx);
0799     if (ret)
0800         return ret;
0801 
0802     memcpy(buf, &port_stats, size);
0803     return size;
0804 }
0805 
0806 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
0807                       struct kobject *kobj,
0808                       struct bin_attribute *attr,
0809                       char *buf, loff_t offset,
0810                       size_t size)
0811 {
0812     struct device *dev = kobj_to_dev(kobj);
0813     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0814     struct qlcnic_esw_statistics esw_stats;
0815     int ret;
0816 
0817     if (qlcnic_83xx_check(adapter))
0818         return -EOPNOTSUPP;
0819 
0820     if (size != sizeof(struct qlcnic_esw_statistics))
0821         return -EINVAL;
0822 
0823     if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
0824         return -EINVAL;
0825 
0826     memset(&esw_stats, 0, size);
0827     ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
0828                        &esw_stats.rx);
0829     if (ret)
0830         return ret;
0831 
0832     ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
0833                        &esw_stats.tx);
0834     if (ret)
0835         return ret;
0836 
0837     memcpy(buf, &esw_stats, size);
0838     return size;
0839 }
0840 
0841 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
0842                         struct kobject *kobj,
0843                         struct bin_attribute *attr,
0844                         char *buf, loff_t offset,
0845                         size_t size)
0846 {
0847     struct device *dev = kobj_to_dev(kobj);
0848     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0849     int ret;
0850 
0851     if (qlcnic_83xx_check(adapter))
0852         return -EOPNOTSUPP;
0853 
0854     if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
0855         return -EINVAL;
0856 
0857     ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
0858                      QLCNIC_QUERY_RX_COUNTER);
0859     if (ret)
0860         return ret;
0861 
0862     ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
0863                      QLCNIC_QUERY_TX_COUNTER);
0864     if (ret)
0865         return ret;
0866 
0867     return size;
0868 }
0869 
0870 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
0871                          struct kobject *kobj,
0872                          struct bin_attribute *attr,
0873                          char *buf, loff_t offset,
0874                          size_t size)
0875 {
0876 
0877     struct device *dev = kobj_to_dev(kobj);
0878     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0879     int ret;
0880 
0881     if (qlcnic_83xx_check(adapter))
0882         return -EOPNOTSUPP;
0883 
0884     if (offset >= adapter->ahw->max_vnic_func)
0885         return -EINVAL;
0886 
0887     ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
0888                      QLCNIC_QUERY_RX_COUNTER);
0889     if (ret)
0890         return ret;
0891 
0892     ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
0893                      QLCNIC_QUERY_TX_COUNTER);
0894     if (ret)
0895         return ret;
0896 
0897     return size;
0898 }
0899 
0900 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
0901                         struct kobject *kobj,
0902                         struct bin_attribute *attr,
0903                         char *buf, loff_t offset,
0904                         size_t size)
0905 {
0906     struct device *dev = kobj_to_dev(kobj);
0907     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0908     struct qlcnic_pci_func_cfg *pci_cfg;
0909     struct qlcnic_pci_info *pci_info;
0910     int i, ret;
0911     u32 count;
0912 
0913     pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
0914     if (!pci_info)
0915         return -ENOMEM;
0916 
0917     ret = qlcnic_get_pci_info(adapter, pci_info);
0918     if (ret) {
0919         kfree(pci_info);
0920         return ret;
0921     }
0922 
0923     pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
0924     count = size / sizeof(struct qlcnic_pci_func_cfg);
0925     qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
0926     for (i = 0; i < count; i++) {
0927         pci_cfg[i].pci_func = pci_info[i].id;
0928         pci_cfg[i].func_type = pci_info[i].type;
0929         pci_cfg[i].func_state = 0;
0930         pci_cfg[i].port_num = pci_info[i].default_port;
0931         pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
0932         pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
0933         memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
0934     }
0935 
0936     kfree(pci_info);
0937     return size;
0938 }
0939 
0940 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
0941                             struct kobject *kobj,
0942                             struct bin_attribute *attr,
0943                             char *buf, loff_t offset,
0944                             size_t size)
0945 {
0946     unsigned char *p_read_buf;
0947     int  ret, count;
0948     struct device *dev = kobj_to_dev(kobj);
0949     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
0950 
0951     if (!size)
0952         return -EINVAL;
0953 
0954     count = size / sizeof(u32);
0955 
0956     if (size % sizeof(u32))
0957         count++;
0958 
0959     p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
0960     if (!p_read_buf)
0961         return -ENOMEM;
0962     if (qlcnic_83xx_lock_flash(adapter) != 0) {
0963         kfree(p_read_buf);
0964         return -EIO;
0965     }
0966 
0967     ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
0968                         count);
0969 
0970     if (ret) {
0971         qlcnic_83xx_unlock_flash(adapter);
0972         kfree(p_read_buf);
0973         return ret;
0974     }
0975 
0976     qlcnic_83xx_unlock_flash(adapter);
0977     qlcnic_swap32_buffer((u32 *)p_read_buf, count);
0978     memcpy(buf, p_read_buf, size);
0979     kfree(p_read_buf);
0980 
0981     return size;
0982 }
0983 
0984 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
0985                           char *buf, loff_t offset,
0986                           size_t size)
0987 {
0988     int  i, ret, count;
0989     unsigned char *p_cache, *p_src;
0990 
0991     p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
0992     if (!p_cache)
0993         return -ENOMEM;
0994 
0995     count = size / sizeof(u32);
0996     qlcnic_swap32_buffer((u32 *)buf, count);
0997     memcpy(p_cache, buf, size);
0998     p_src = p_cache;
0999 
1000     if (qlcnic_83xx_lock_flash(adapter) != 0) {
1001         kfree(p_cache);
1002         return -EIO;
1003     }
1004 
1005     if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1006         ret = qlcnic_83xx_enable_flash_write(adapter);
1007         if (ret) {
1008             kfree(p_cache);
1009             qlcnic_83xx_unlock_flash(adapter);
1010             return -EIO;
1011         }
1012     }
1013 
1014     for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1015         ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1016                            (u32 *)p_src,
1017                            QLC_83XX_FLASH_WRITE_MAX);
1018 
1019         if (ret) {
1020             if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1021                 ret = qlcnic_83xx_disable_flash_write(adapter);
1022                 if (ret) {
1023                     kfree(p_cache);
1024                     qlcnic_83xx_unlock_flash(adapter);
1025                     return -EIO;
1026                 }
1027             }
1028 
1029             kfree(p_cache);
1030             qlcnic_83xx_unlock_flash(adapter);
1031             return -EIO;
1032         }
1033 
1034         p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1035         offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1036     }
1037 
1038     if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1039         ret = qlcnic_83xx_disable_flash_write(adapter);
1040         if (ret) {
1041             kfree(p_cache);
1042             qlcnic_83xx_unlock_flash(adapter);
1043             return -EIO;
1044         }
1045     }
1046 
1047     kfree(p_cache);
1048     qlcnic_83xx_unlock_flash(adapter);
1049 
1050     return 0;
1051 }
1052 
1053 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1054                      char *buf, loff_t offset, size_t size)
1055 {
1056     int  i, ret, count;
1057     unsigned char *p_cache, *p_src;
1058 
1059     p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1060     if (!p_cache)
1061         return -ENOMEM;
1062 
1063     qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1064     memcpy(p_cache, buf, size);
1065     p_src = p_cache;
1066     count = size / sizeof(u32);
1067 
1068     if (qlcnic_83xx_lock_flash(adapter) != 0) {
1069         kfree(p_cache);
1070         return -EIO;
1071     }
1072 
1073     if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1074         ret = qlcnic_83xx_enable_flash_write(adapter);
1075         if (ret) {
1076             kfree(p_cache);
1077             qlcnic_83xx_unlock_flash(adapter);
1078             return -EIO;
1079         }
1080     }
1081 
1082     for (i = 0; i < count; i++) {
1083         ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1084         if (ret) {
1085             if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1086                 ret = qlcnic_83xx_disable_flash_write(adapter);
1087                 if (ret) {
1088                     kfree(p_cache);
1089                     qlcnic_83xx_unlock_flash(adapter);
1090                     return -EIO;
1091                 }
1092             }
1093             kfree(p_cache);
1094             qlcnic_83xx_unlock_flash(adapter);
1095             return -EIO;
1096         }
1097 
1098         p_src = p_src + sizeof(u32);
1099         offset = offset + sizeof(u32);
1100     }
1101 
1102     if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1103         ret = qlcnic_83xx_disable_flash_write(adapter);
1104         if (ret) {
1105             kfree(p_cache);
1106             qlcnic_83xx_unlock_flash(adapter);
1107             return -EIO;
1108         }
1109     }
1110 
1111     kfree(p_cache);
1112     qlcnic_83xx_unlock_flash(adapter);
1113 
1114     return 0;
1115 }
1116 
1117 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1118                              struct kobject *kobj,
1119                              struct bin_attribute *attr,
1120                              char *buf, loff_t offset,
1121                              size_t size)
1122 {
1123     int  ret;
1124     static int flash_mode;
1125     unsigned long data;
1126     struct device *dev = kobj_to_dev(kobj);
1127     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1128 
1129     ret = kstrtoul(buf, 16, &data);
1130     if (ret)
1131         return ret;
1132 
1133     switch (data) {
1134     case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1135         flash_mode = QLC_83XX_ERASE_MODE;
1136         ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1137         if (ret) {
1138             dev_err(&adapter->pdev->dev,
1139                 "%s failed at %d\n", __func__, __LINE__);
1140             return -EIO;
1141         }
1142         break;
1143 
1144     case QLC_83XX_FLASH_BULK_WRITE_CMD:
1145         flash_mode = QLC_83XX_BULK_WRITE_MODE;
1146         break;
1147 
1148     case QLC_83XX_FLASH_WRITE_CMD:
1149         flash_mode = QLC_83XX_WRITE_MODE;
1150         break;
1151     default:
1152         if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1153             ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1154                                  offset, size);
1155             if (ret) {
1156                 dev_err(&adapter->pdev->dev,
1157                     "%s failed at %d\n",
1158                     __func__, __LINE__);
1159                 return -EIO;
1160             }
1161         }
1162 
1163         if (flash_mode == QLC_83XX_WRITE_MODE) {
1164             ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1165                                 offset, size);
1166             if (ret) {
1167                 dev_err(&adapter->pdev->dev,
1168                     "%s failed at %d\n", __func__,
1169                     __LINE__);
1170                 return -EIO;
1171             }
1172         }
1173     }
1174 
1175     return size;
1176 }
1177 
1178 static const struct device_attribute dev_attr_bridged_mode = {
1179     .attr = { .name = "bridged_mode", .mode = 0644 },
1180     .show = qlcnic_show_bridged_mode,
1181     .store = qlcnic_store_bridged_mode,
1182 };
1183 
1184 static const struct device_attribute dev_attr_diag_mode = {
1185     .attr = { .name = "diag_mode", .mode = 0644 },
1186     .show = qlcnic_show_diag_mode,
1187     .store = qlcnic_store_diag_mode,
1188 };
1189 
1190 static const struct device_attribute dev_attr_beacon = {
1191     .attr = { .name = "beacon", .mode = 0644 },
1192     .show = qlcnic_show_beacon,
1193     .store = qlcnic_store_beacon,
1194 };
1195 
1196 static const struct bin_attribute bin_attr_crb = {
1197     .attr = { .name = "crb", .mode = 0644 },
1198     .size = 0,
1199     .read = qlcnic_sysfs_read_crb,
1200     .write = qlcnic_sysfs_write_crb,
1201 };
1202 
1203 static const struct bin_attribute bin_attr_mem = {
1204     .attr = { .name = "mem", .mode = 0644 },
1205     .size = 0,
1206     .read = qlcnic_sysfs_read_mem,
1207     .write = qlcnic_sysfs_write_mem,
1208 };
1209 
1210 static const struct bin_attribute bin_attr_npar_config = {
1211     .attr = { .name = "npar_config", .mode = 0644 },
1212     .size = 0,
1213     .read = qlcnic_sysfs_read_npar_config,
1214     .write = qlcnic_sysfs_write_npar_config,
1215 };
1216 
1217 static const struct bin_attribute bin_attr_pci_config = {
1218     .attr = { .name = "pci_config", .mode = 0644 },
1219     .size = 0,
1220     .read = qlcnic_sysfs_read_pci_config,
1221     .write = NULL,
1222 };
1223 
1224 static const struct bin_attribute bin_attr_port_stats = {
1225     .attr = { .name = "port_stats", .mode = 0644 },
1226     .size = 0,
1227     .read = qlcnic_sysfs_get_port_stats,
1228     .write = qlcnic_sysfs_clear_port_stats,
1229 };
1230 
1231 static const struct bin_attribute bin_attr_esw_stats = {
1232     .attr = { .name = "esw_stats", .mode = 0644 },
1233     .size = 0,
1234     .read = qlcnic_sysfs_get_esw_stats,
1235     .write = qlcnic_sysfs_clear_esw_stats,
1236 };
1237 
1238 static const struct bin_attribute bin_attr_esw_config = {
1239     .attr = { .name = "esw_config", .mode = 0644 },
1240     .size = 0,
1241     .read = qlcnic_sysfs_read_esw_config,
1242     .write = qlcnic_sysfs_write_esw_config,
1243 };
1244 
1245 static const struct bin_attribute bin_attr_pm_config = {
1246     .attr = { .name = "pm_config", .mode = 0644 },
1247     .size = 0,
1248     .read = qlcnic_sysfs_read_pm_config,
1249     .write = qlcnic_sysfs_write_pm_config,
1250 };
1251 
1252 static const struct bin_attribute bin_attr_flash = {
1253     .attr = { .name = "flash", .mode = 0644 },
1254     .size = 0,
1255     .read = qlcnic_83xx_sysfs_flash_read_handler,
1256     .write = qlcnic_83xx_sysfs_flash_write_handler,
1257 };
1258 
1259 #ifdef CONFIG_QLCNIC_HWMON
1260 
1261 static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1262                       struct device_attribute *dev_attr,
1263                       char *buf)
1264 {
1265     struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1266     unsigned int temperature = 0, value = 0;
1267 
1268     if (qlcnic_83xx_check(adapter))
1269         value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1270     else if (qlcnic_82xx_check(adapter))
1271         value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1272 
1273     temperature = qlcnic_get_temp_val(value);
1274     /* display millidegree celcius */
1275     temperature *= 1000;
1276     return sprintf(buf, "%u\n", temperature);
1277 }
1278 
1279 /* hwmon-sysfs attributes */
1280 static SENSOR_DEVICE_ATTR(temp1_input, 0444,
1281               qlcnic_hwmon_show_temp, NULL, 1);
1282 
1283 static struct attribute *qlcnic_hwmon_attrs[] = {
1284     &sensor_dev_attr_temp1_input.dev_attr.attr,
1285     NULL
1286 };
1287 
1288 ATTRIBUTE_GROUPS(qlcnic_hwmon);
1289 
1290 void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1291 {
1292     struct device *dev = &adapter->pdev->dev;
1293     struct device *hwmon_dev;
1294 
1295     /* Skip hwmon registration for a VF device */
1296     if (qlcnic_sriov_vf_check(adapter)) {
1297         adapter->ahw->hwmon_dev = NULL;
1298         return;
1299     }
1300     hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1301                               adapter,
1302                               qlcnic_hwmon_groups);
1303     if (IS_ERR(hwmon_dev)) {
1304         dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1305             PTR_ERR(hwmon_dev));
1306         hwmon_dev = NULL;
1307     }
1308     adapter->ahw->hwmon_dev = hwmon_dev;
1309 }
1310 
1311 void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1312 {
1313     struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1314     if (hwmon_dev) {
1315         hwmon_device_unregister(hwmon_dev);
1316         adapter->ahw->hwmon_dev = NULL;
1317     }
1318 }
1319 #endif
1320 
1321 void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1322 {
1323     struct device *dev = &adapter->pdev->dev;
1324 
1325     if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1326         if (device_create_file(dev, &dev_attr_bridged_mode))
1327             dev_warn(dev,
1328                  "failed to create bridged_mode sysfs entry\n");
1329 }
1330 
1331 void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1332 {
1333     struct device *dev = &adapter->pdev->dev;
1334 
1335     if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1336         device_remove_file(dev, &dev_attr_bridged_mode);
1337 }
1338 
1339 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1340 {
1341     struct device *dev = &adapter->pdev->dev;
1342 
1343     if (device_create_bin_file(dev, &bin_attr_port_stats))
1344         dev_info(dev, "failed to create port stats sysfs entry");
1345 
1346     if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1347         return;
1348     if (device_create_file(dev, &dev_attr_diag_mode))
1349         dev_info(dev, "failed to create diag_mode sysfs entry\n");
1350     if (device_create_bin_file(dev, &bin_attr_crb))
1351         dev_info(dev, "failed to create crb sysfs entry\n");
1352     if (device_create_bin_file(dev, &bin_attr_mem))
1353         dev_info(dev, "failed to create mem sysfs entry\n");
1354 
1355     if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1356         return;
1357 
1358     if (device_create_bin_file(dev, &bin_attr_pci_config))
1359         dev_info(dev, "failed to create pci config sysfs entry");
1360 
1361     if (device_create_file(dev, &dev_attr_beacon))
1362         dev_info(dev, "failed to create beacon sysfs entry");
1363 
1364     if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1365         return;
1366     if (device_create_bin_file(dev, &bin_attr_esw_config))
1367         dev_info(dev, "failed to create esw config sysfs entry");
1368     if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1369         return;
1370     if (device_create_bin_file(dev, &bin_attr_npar_config))
1371         dev_info(dev, "failed to create npar config sysfs entry");
1372     if (device_create_bin_file(dev, &bin_attr_pm_config))
1373         dev_info(dev, "failed to create pm config sysfs entry");
1374     if (device_create_bin_file(dev, &bin_attr_esw_stats))
1375         dev_info(dev, "failed to create eswitch stats sysfs entry");
1376 }
1377 
1378 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1379 {
1380     struct device *dev = &adapter->pdev->dev;
1381 
1382     device_remove_bin_file(dev, &bin_attr_port_stats);
1383 
1384     if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1385         return;
1386     device_remove_file(dev, &dev_attr_diag_mode);
1387     device_remove_bin_file(dev, &bin_attr_crb);
1388     device_remove_bin_file(dev, &bin_attr_mem);
1389 
1390     if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1391         return;
1392 
1393     device_remove_bin_file(dev, &bin_attr_pci_config);
1394     device_remove_file(dev, &dev_attr_beacon);
1395     if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1396         return;
1397     device_remove_bin_file(dev, &bin_attr_esw_config);
1398     if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1399         return;
1400     device_remove_bin_file(dev, &bin_attr_npar_config);
1401     device_remove_bin_file(dev, &bin_attr_pm_config);
1402     device_remove_bin_file(dev, &bin_attr_esw_stats);
1403 }
1404 
1405 void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1406 {
1407     qlcnic_create_diag_entries(adapter);
1408 }
1409 
1410 void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1411 {
1412     qlcnic_remove_diag_entries(adapter);
1413 }
1414 
1415 void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1416 {
1417     struct device *dev = &adapter->pdev->dev;
1418 
1419     qlcnic_create_diag_entries(adapter);
1420 
1421     if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1422         dev_info(dev, "failed to create flash sysfs entry\n");
1423 }
1424 
1425 void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1426 {
1427     struct device *dev = &adapter->pdev->dev;
1428 
1429     qlcnic_remove_diag_entries(adapter);
1430     sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);
1431 }