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