05c28f2c6df702ff5ccb7d8bf026f73a80cafe67
[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 int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
28 {
29         return -EOPNOTSUPP;
30 }
31
32 int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
33 {
34         return -EOPNOTSUPP;
35 }
36
37 static ssize_t qlcnic_store_bridged_mode(struct device *dev,
38                                          struct device_attribute *attr,
39                                          const char *buf, size_t len)
40 {
41         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
42         unsigned long new;
43         int ret = -EINVAL;
44
45         if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
46                 goto err_out;
47
48         if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
49                 goto err_out;
50
51         if (kstrtoul(buf, 2, &new))
52                 goto err_out;
53
54         if (!qlcnic_config_bridged_mode(adapter, !!new))
55                 ret = len;
56
57 err_out:
58         return ret;
59 }
60
61 static ssize_t qlcnic_show_bridged_mode(struct device *dev,
62                                         struct device_attribute *attr,
63                                         char *buf)
64 {
65         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
66         int bridged_mode = 0;
67
68         if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
69                 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
70
71         return sprintf(buf, "%d\n", bridged_mode);
72 }
73
74 static ssize_t qlcnic_store_diag_mode(struct device *dev,
75                                       struct device_attribute *attr,
76                                       const char *buf, size_t len)
77 {
78         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
79         unsigned long new;
80
81         if (kstrtoul(buf, 2, &new))
82                 return -EINVAL;
83
84         if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
85                 adapter->flags ^= QLCNIC_DIAG_ENABLED;
86
87         return len;
88 }
89
90 static ssize_t qlcnic_show_diag_mode(struct device *dev,
91                                      struct device_attribute *attr, char *buf)
92 {
93         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
94         return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
95 }
96
97 static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
98                                   u8 *state, u8 *rate)
99 {
100         *rate = LSB(beacon);
101         *state = MSB(beacon);
102
103         QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
104
105         if (!*state) {
106                 *rate = __QLCNIC_MAX_LED_RATE;
107                 return 0;
108         } else if (*state > __QLCNIC_MAX_LED_STATE) {
109                 return -EINVAL;
110         }
111
112         if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
113                 return -EINVAL;
114
115         return 0;
116 }
117
118 static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
119                                     const char *buf, size_t len)
120 {
121         struct qlcnic_hardware_context *ahw = adapter->ahw;
122         unsigned long h_beacon;
123         int err;
124
125         if (test_bit(__QLCNIC_RESETTING, &adapter->state))
126                 return -EIO;
127
128         if (kstrtoul(buf, 2, &h_beacon))
129                 return -EINVAL;
130
131         qlcnic_get_beacon_state(adapter);
132
133         if (ahw->beacon_state == h_beacon)
134                 return len;
135
136         rtnl_lock();
137         if (!ahw->beacon_state) {
138                 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
139                         rtnl_unlock();
140                         return -EBUSY;
141                 }
142         }
143
144         if (h_beacon)
145                 err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
146         else
147                 err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
148         if (!err)
149                 ahw->beacon_state = h_beacon;
150
151         if (!ahw->beacon_state)
152                 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
153
154         rtnl_unlock();
155         return len;
156 }
157
158 static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
159                                     const char *buf, size_t len)
160 {
161         struct qlcnic_hardware_context *ahw = adapter->ahw;
162         int err, drv_sds_rings = adapter->drv_sds_rings;
163         u16 beacon;
164         u8 b_state, b_rate;
165
166         if (len != sizeof(u16))
167                 return -EINVAL;
168
169         memcpy(&beacon, buf, sizeof(u16));
170         err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
171         if (err)
172                 return err;
173
174         qlcnic_get_beacon_state(adapter);
175
176         if (ahw->beacon_state == b_state)
177                 return len;
178
179         rtnl_lock();
180         if (!ahw->beacon_state) {
181                 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
182                         rtnl_unlock();
183                         return -EBUSY;
184                 }
185         }
186
187         if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
188                 err = -EIO;
189                 goto out;
190         }
191
192         if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
193                 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
194                 if (err)
195                         goto out;
196                 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
197         }
198
199         err = qlcnic_config_led(adapter, b_state, b_rate);
200         if (!err) {
201                 err = len;
202                 ahw->beacon_state = b_state;
203         }
204
205         if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
206                 qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
207
208 out:
209         if (!ahw->beacon_state)
210                 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
211         rtnl_unlock();
212
213         return err;
214 }
215
216 static ssize_t qlcnic_store_beacon(struct device *dev,
217                                    struct device_attribute *attr,
218                                    const char *buf, size_t len)
219 {
220         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
221         int err = 0;
222
223         if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
224                 dev_warn(dev,
225                          "LED test not supported in non privileged mode\n");
226                 return -EOPNOTSUPP;
227         }
228
229         if (qlcnic_82xx_check(adapter))
230                 err = qlcnic_82xx_store_beacon(adapter, buf, len);
231         else if (qlcnic_83xx_check(adapter))
232                 err = qlcnic_83xx_store_beacon(adapter, buf, len);
233         else
234                 return -EIO;
235
236         return err;
237 }
238
239 static ssize_t qlcnic_show_beacon(struct device *dev,
240                                   struct device_attribute *attr, char *buf)
241 {
242         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
243
244         return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
245 }
246
247 static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
248                                      loff_t offset, size_t size)
249 {
250         size_t crb_size = 4;
251
252         if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
253                 return -EIO;
254
255         if (offset < QLCNIC_PCI_CRBSPACE) {
256                 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
257                                   QLCNIC_PCI_CAMQM_END))
258                         crb_size = 8;
259                 else
260                         return -EINVAL;
261         }
262
263         if ((size != crb_size) || (offset & (crb_size-1)))
264                 return  -EINVAL;
265
266         return 0;
267 }
268
269 static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
270                                      struct bin_attribute *attr, char *buf,
271                                      loff_t offset, size_t size)
272 {
273         struct device *dev = container_of(kobj, struct device, kobj);
274         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
275         int ret;
276
277         ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
278         if (ret != 0)
279                 return ret;
280         qlcnic_read_crb(adapter, buf, offset, size);
281         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
282
283         return size;
284 }
285
286 static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
287                                       struct bin_attribute *attr, char *buf,
288                                       loff_t offset, size_t size)
289 {
290         struct device *dev = container_of(kobj, struct device, kobj);
291         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
292         int ret;
293
294         ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
295         if (ret != 0)
296                 return ret;
297
298         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
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         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
333
334         return size;
335 }
336
337 static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
338                                       struct bin_attribute *attr, char *buf,
339                                       loff_t offset, size_t size)
340 {
341         struct device *dev = container_of(kobj, struct device, kobj);
342         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
343         u64 data;
344         int ret;
345
346         ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
347         if (ret != 0)
348                 return ret;
349
350         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
351         memcpy(&data, buf, size);
352
353         if (qlcnic_pci_mem_write_2M(adapter, offset, data))
354                 return -EIO;
355
356         return size;
357 }
358
359 int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
360 {
361         int i;
362
363         for (i = 0; i < adapter->ahw->total_nic_func; i++) {
364                 if (adapter->npars[i].pci_func == pci_func)
365                         return i;
366         }
367
368         dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
369         return -EINVAL;
370 }
371
372 static int validate_pm_config(struct qlcnic_adapter *adapter,
373                               struct qlcnic_pm_func_cfg *pm_cfg, int count)
374 {
375         u8 src_pci_func, s_esw_id, d_esw_id;
376         u8 dest_pci_func;
377         int i, src_index, dest_index;
378
379         for (i = 0; i < count; i++) {
380                 src_pci_func = pm_cfg[i].pci_func;
381                 dest_pci_func = pm_cfg[i].dest_npar;
382                 src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
383                 if (src_index < 0)
384                         return -EINVAL;
385
386                 dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
387                 if (dest_index < 0)
388                         return -EINVAL;
389
390                 s_esw_id = adapter->npars[src_index].phy_port;
391                 d_esw_id = adapter->npars[dest_index].phy_port;
392
393                 if (s_esw_id != d_esw_id)
394                         return -EINVAL;
395         }
396
397         return 0;
398 }
399
400 static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
401                                             struct kobject *kobj,
402                                             struct bin_attribute *attr,
403                                             char *buf, loff_t offset,
404                                             size_t size)
405 {
406         struct device *dev = container_of(kobj, struct device, kobj);
407         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
408         struct qlcnic_pm_func_cfg *pm_cfg;
409         u32 id, action, pci_func;
410         int count, rem, i, ret, index;
411
412         count   = size / sizeof(struct qlcnic_pm_func_cfg);
413         rem     = size % sizeof(struct qlcnic_pm_func_cfg);
414         if (rem)
415                 return -EINVAL;
416
417         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
418         pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
419         ret = validate_pm_config(adapter, pm_cfg, count);
420
421         if (ret)
422                 return ret;
423         for (i = 0; i < count; i++) {
424                 pci_func = pm_cfg[i].pci_func;
425                 action = !!pm_cfg[i].action;
426                 index = qlcnic_is_valid_nic_func(adapter, pci_func);
427                 if (index < 0)
428                         return -EINVAL;
429
430                 id = adapter->npars[index].phy_port;
431                 ret = qlcnic_config_port_mirroring(adapter, id,
432                                                    action, pci_func);
433                 if (ret)
434                         return ret;
435         }
436
437         for (i = 0; i < count; i++) {
438                 pci_func = pm_cfg[i].pci_func;
439                 index = qlcnic_is_valid_nic_func(adapter, pci_func);
440                 if (index < 0)
441                         return -EINVAL;
442                 id = adapter->npars[index].phy_port;
443                 adapter->npars[index].enable_pm = !!pm_cfg[i].action;
444                 adapter->npars[index].dest_npar = id;
445         }
446
447         return size;
448 }
449
450 static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
451                                            struct kobject *kobj,
452                                            struct bin_attribute *attr,
453                                            char *buf, loff_t offset,
454                                            size_t size)
455 {
456         struct device *dev = container_of(kobj, struct device, kobj);
457         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
458         struct qlcnic_pm_func_cfg *pm_cfg;
459         u8 pci_func;
460         u32 count;
461         int i;
462
463         memset(buf, 0, size);
464         pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
465         count = size / sizeof(struct qlcnic_pm_func_cfg);
466         for (i = 0; i < adapter->ahw->total_nic_func; i++) {
467                 pci_func = adapter->npars[i].pci_func;
468                 if (pci_func >= count) {
469                         dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
470                                 __func__, adapter->ahw->total_nic_func, count);
471                         continue;
472                 }
473                 if (!adapter->npars[i].eswitch_status)
474                         continue;
475
476                 pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
477                 pm_cfg[pci_func].dest_npar = 0;
478                 pm_cfg[pci_func].pci_func = i;
479         }
480         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
481         return size;
482 }
483
484 static int validate_esw_config(struct qlcnic_adapter *adapter,
485                                struct qlcnic_esw_func_cfg *esw_cfg, int count)
486 {
487         struct qlcnic_hardware_context *ahw = adapter->ahw;
488         int i, ret;
489         u32 op_mode;
490         u8 pci_func;
491
492         if (qlcnic_82xx_check(adapter))
493                 op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
494         else
495                 op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
496
497         for (i = 0; i < count; i++) {
498                 pci_func = esw_cfg[i].pci_func;
499                 if (pci_func >= ahw->max_vnic_func)
500                         return -EINVAL;
501
502                 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
503                         if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
504                                 return -EINVAL;
505
506                 switch (esw_cfg[i].op_mode) {
507                 case QLCNIC_PORT_DEFAULTS:
508                         if (qlcnic_82xx_check(adapter)) {
509                                 ret = QLC_DEV_GET_DRV(op_mode, pci_func);
510                         } else {
511                                 ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
512                                                                   pci_func);
513                                 esw_cfg[i].offload_flags = 0;
514                         }
515
516                         if (ret != QLCNIC_NON_PRIV_FUNC) {
517                                 if (esw_cfg[i].mac_anti_spoof != 0)
518                                         return -EINVAL;
519                                 if (esw_cfg[i].mac_override != 1)
520                                         return -EINVAL;
521                                 if (esw_cfg[i].promisc_mode != 1)
522                                         return -EINVAL;
523                         }
524                         break;
525                 case QLCNIC_ADD_VLAN:
526                         if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
527                                 return -EINVAL;
528                         if (!esw_cfg[i].op_type)
529                                 return -EINVAL;
530                         break;
531                 case QLCNIC_DEL_VLAN:
532                         if (!esw_cfg[i].op_type)
533                                 return -EINVAL;
534                         break;
535                 default:
536                         return -EINVAL;
537                 }
538         }
539
540         return 0;
541 }
542
543 static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
544                                              struct kobject *kobj,
545                                              struct bin_attribute *attr,
546                                              char *buf, loff_t offset,
547                                              size_t size)
548 {
549         struct device *dev = container_of(kobj, struct device, kobj);
550         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
551         struct qlcnic_esw_func_cfg *esw_cfg;
552         struct qlcnic_npar_info *npar;
553         int count, rem, i, ret;
554         int index;
555         u8 op_mode = 0, pci_func;
556
557         count   = size / sizeof(struct qlcnic_esw_func_cfg);
558         rem     = size % sizeof(struct qlcnic_esw_func_cfg);
559         if (rem)
560                 return -EINVAL;
561
562         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
563         esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
564         ret = validate_esw_config(adapter, esw_cfg, count);
565         if (ret)
566                 return ret;
567
568         for (i = 0; i < count; i++) {
569                 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
570                         if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
571                                 return -EINVAL;
572
573                 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
574                         continue;
575
576                 op_mode = esw_cfg[i].op_mode;
577                 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
578                 esw_cfg[i].op_mode = op_mode;
579                 esw_cfg[i].pci_func = adapter->ahw->pci_func;
580
581                 switch (esw_cfg[i].op_mode) {
582                 case QLCNIC_PORT_DEFAULTS:
583                         qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
584                         rtnl_lock();
585                         qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
586                         rtnl_unlock();
587                         break;
588                 case QLCNIC_ADD_VLAN:
589                         qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
590                         break;
591                 case QLCNIC_DEL_VLAN:
592                         esw_cfg[i].vlan_id = 0;
593                         qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
594                         break;
595                 }
596         }
597
598         if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
599                 goto out;
600
601         for (i = 0; i < count; i++) {
602                 pci_func = esw_cfg[i].pci_func;
603                 index = qlcnic_is_valid_nic_func(adapter, pci_func);
604                 if (index < 0)
605                         return -EINVAL;
606                 npar = &adapter->npars[index];
607                 switch (esw_cfg[i].op_mode) {
608                 case QLCNIC_PORT_DEFAULTS:
609                         npar->promisc_mode = esw_cfg[i].promisc_mode;
610                         npar->mac_override = esw_cfg[i].mac_override;
611                         npar->offload_flags = esw_cfg[i].offload_flags;
612                         npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
613                         npar->discard_tagged = esw_cfg[i].discard_tagged;
614                         break;
615                 case QLCNIC_ADD_VLAN:
616                         npar->pvid = esw_cfg[i].vlan_id;
617                         break;
618                 case QLCNIC_DEL_VLAN:
619                         npar->pvid = 0;
620                         break;
621                 }
622         }
623 out:
624         return size;
625 }
626
627 static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
628                                             struct kobject *kobj,
629                                             struct bin_attribute *attr,
630                                             char *buf, loff_t offset,
631                                             size_t size)
632 {
633         struct device *dev = container_of(kobj, struct device, kobj);
634         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
635         struct qlcnic_esw_func_cfg *esw_cfg;
636         u8 pci_func;
637         u32 count;
638         int i;
639
640         memset(buf, 0, size);
641         esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
642         count = size / sizeof(struct qlcnic_esw_func_cfg);
643         for (i = 0; i < adapter->ahw->total_nic_func; i++) {
644                 pci_func = adapter->npars[i].pci_func;
645                 if (pci_func >= count) {
646                         dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
647                                 __func__, adapter->ahw->total_nic_func, count);
648                         continue;
649                 }
650                 if (!adapter->npars[i].eswitch_status)
651                         continue;
652
653                 esw_cfg[pci_func].pci_func = pci_func;
654                 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
655                         return -EINVAL;
656         }
657         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
658         return size;
659 }
660
661 static int validate_npar_config(struct qlcnic_adapter *adapter,
662                                 struct qlcnic_npar_func_cfg *np_cfg,
663                                 int count)
664 {
665         u8 pci_func, i;
666
667         for (i = 0; i < count; i++) {
668                 pci_func = np_cfg[i].pci_func;
669                 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
670                         return -EINVAL;
671
672                 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
673                     !IS_VALID_BW(np_cfg[i].max_bw))
674                         return -EINVAL;
675         }
676         return 0;
677 }
678
679 static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
680                                               struct kobject *kobj,
681                                               struct bin_attribute *attr,
682                                               char *buf, loff_t offset,
683                                               size_t size)
684 {
685         struct device *dev = container_of(kobj, struct device, kobj);
686         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
687         struct qlcnic_info nic_info;
688         struct qlcnic_npar_func_cfg *np_cfg;
689         int i, count, rem, ret, index;
690         u8 pci_func;
691
692         count   = size / sizeof(struct qlcnic_npar_func_cfg);
693         rem     = size % sizeof(struct qlcnic_npar_func_cfg);
694         if (rem)
695                 return -EINVAL;
696
697         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
698         np_cfg = (struct qlcnic_npar_func_cfg *)buf;
699         ret = validate_npar_config(adapter, np_cfg, count);
700         if (ret)
701                 return ret;
702
703         for (i = 0; i < count; i++) {
704                 pci_func = np_cfg[i].pci_func;
705
706                 memset(&nic_info, 0, sizeof(struct qlcnic_info));
707                 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
708                 if (ret)
709                         return ret;
710                 nic_info.pci_func = pci_func;
711                 nic_info.min_tx_bw = np_cfg[i].min_bw;
712                 nic_info.max_tx_bw = np_cfg[i].max_bw;
713                 ret = qlcnic_set_nic_info(adapter, &nic_info);
714                 if (ret)
715                         return ret;
716                 index = qlcnic_is_valid_nic_func(adapter, pci_func);
717                 if (index < 0)
718                         return -EINVAL;
719                 adapter->npars[index].min_bw = nic_info.min_tx_bw;
720                 adapter->npars[index].max_bw = nic_info.max_tx_bw;
721         }
722
723         return size;
724 }
725
726 static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
727                                              struct kobject *kobj,
728                                              struct bin_attribute *attr,
729                                              char *buf, loff_t offset,
730                                              size_t size)
731 {
732         struct device *dev = container_of(kobj, struct device, kobj);
733         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
734         struct qlcnic_npar_func_cfg *np_cfg;
735         struct qlcnic_info nic_info;
736         u8 pci_func;
737         int i, ret;
738         u32 count;
739
740         memset(&nic_info, 0, sizeof(struct qlcnic_info));
741         memset(buf, 0, size);
742         np_cfg = (struct qlcnic_npar_func_cfg *)buf;
743
744         count = size / sizeof(struct qlcnic_npar_func_cfg);
745         for (i = 0; i < adapter->ahw->total_nic_func; i++) {
746                 if (adapter->npars[i].pci_func >= count) {
747                         dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
748                                 __func__, adapter->ahw->total_nic_func, count);
749                         continue;
750                 }
751                 if (!adapter->npars[i].eswitch_status)
752                         continue;
753                 pci_func = adapter->npars[i].pci_func;
754                 if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
755                         continue;
756                 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
757                 if (ret)
758                         return ret;
759
760                 np_cfg[pci_func].pci_func = pci_func;
761                 np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
762                 np_cfg[pci_func].port_num = nic_info.phys_port;
763                 np_cfg[pci_func].fw_capab = nic_info.capabilities;
764                 np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
765                 np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
766                 np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
767                 np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
768         }
769         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
770         return size;
771 }
772
773 static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
774                                            struct kobject *kobj,
775                                            struct bin_attribute *attr,
776                                            char *buf, loff_t offset,
777                                            size_t size)
778 {
779         struct device *dev = container_of(kobj, struct device, kobj);
780         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
781         struct qlcnic_esw_statistics port_stats;
782         int ret;
783
784         if (qlcnic_83xx_check(adapter))
785                 return -EOPNOTSUPP;
786
787         if (size != sizeof(struct qlcnic_esw_statistics))
788                 return -EINVAL;
789
790         if (offset >= adapter->ahw->max_vnic_func)
791                 return -EINVAL;
792
793         memset(&port_stats, 0, size);
794         ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
795                                     &port_stats.rx);
796         if (ret)
797                 return ret;
798
799         ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
800                                     &port_stats.tx);
801         if (ret)
802                 return ret;
803
804         memcpy(buf, &port_stats, size);
805         return size;
806 }
807
808 static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
809                                           struct kobject *kobj,
810                                           struct bin_attribute *attr,
811                                           char *buf, loff_t offset,
812                                           size_t size)
813 {
814         struct device *dev = container_of(kobj, struct device, kobj);
815         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
816         struct qlcnic_esw_statistics esw_stats;
817         int ret;
818
819         if (qlcnic_83xx_check(adapter))
820                 return -EOPNOTSUPP;
821
822         if (size != sizeof(struct qlcnic_esw_statistics))
823                 return -EINVAL;
824
825         if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
826                 return -EINVAL;
827
828         memset(&esw_stats, 0, size);
829         ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
830                                        &esw_stats.rx);
831         if (ret)
832                 return ret;
833
834         ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
835                                        &esw_stats.tx);
836         if (ret)
837                 return ret;
838
839         memcpy(buf, &esw_stats, size);
840         return size;
841 }
842
843 static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
844                                             struct kobject *kobj,
845                                             struct bin_attribute *attr,
846                                             char *buf, loff_t offset,
847                                             size_t size)
848 {
849         struct device *dev = container_of(kobj, struct device, kobj);
850         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
851         int ret;
852
853         if (qlcnic_83xx_check(adapter))
854                 return -EOPNOTSUPP;
855
856         if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
857                 return -EINVAL;
858
859         ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
860                                      QLCNIC_QUERY_RX_COUNTER);
861         if (ret)
862                 return ret;
863
864         ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
865                                      QLCNIC_QUERY_TX_COUNTER);
866         if (ret)
867                 return ret;
868
869         return size;
870 }
871
872 static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
873                                              struct kobject *kobj,
874                                              struct bin_attribute *attr,
875                                              char *buf, loff_t offset,
876                                              size_t size)
877 {
878
879         struct device *dev = container_of(kobj, struct device, kobj);
880         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
881         int ret;
882
883         if (qlcnic_83xx_check(adapter))
884                 return -EOPNOTSUPP;
885
886         if (offset >= adapter->ahw->max_vnic_func)
887                 return -EINVAL;
888
889         ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
890                                      QLCNIC_QUERY_RX_COUNTER);
891         if (ret)
892                 return ret;
893
894         ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
895                                      QLCNIC_QUERY_TX_COUNTER);
896         if (ret)
897                 return ret;
898
899         return size;
900 }
901
902 static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
903                                             struct kobject *kobj,
904                                             struct bin_attribute *attr,
905                                             char *buf, loff_t offset,
906                                             size_t size)
907 {
908         struct device *dev = container_of(kobj, struct device, kobj);
909         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
910         struct qlcnic_pci_func_cfg *pci_cfg;
911         struct qlcnic_pci_info *pci_info;
912         int i, ret;
913         u32 count;
914
915         pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
916         if (!pci_info)
917                 return -ENOMEM;
918
919         ret = qlcnic_get_pci_info(adapter, pci_info);
920         if (ret) {
921                 kfree(pci_info);
922                 return ret;
923         }
924
925         pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
926         count = size / sizeof(struct qlcnic_pci_func_cfg);
927         qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
928         for (i = 0; i < count; i++) {
929                 pci_cfg[i].pci_func = pci_info[i].id;
930                 pci_cfg[i].func_type = pci_info[i].type;
931                 pci_cfg[i].func_state = 0;
932                 pci_cfg[i].port_num = pci_info[i].default_port;
933                 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
934                 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
935                 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
936         }
937
938         kfree(pci_info);
939         return size;
940 }
941
942 static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
943                                                     struct kobject *kobj,
944                                                     struct bin_attribute *attr,
945                                                     char *buf, loff_t offset,
946                                                     size_t size)
947 {
948         unsigned char *p_read_buf;
949         int  ret, count;
950         struct device *dev = container_of(kobj, struct device, kobj);
951         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
952
953         if (!size)
954                 return -EINVAL;
955
956         count = size / sizeof(u32);
957
958         if (size % sizeof(u32))
959                 count++;
960
961         p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
962         if (!p_read_buf)
963                 return -ENOMEM;
964         if (qlcnic_83xx_lock_flash(adapter) != 0) {
965                 kfree(p_read_buf);
966                 return -EIO;
967         }
968
969         ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
970                                                 count);
971
972         if (ret) {
973                 qlcnic_83xx_unlock_flash(adapter);
974                 kfree(p_read_buf);
975                 return ret;
976         }
977
978         qlcnic_83xx_unlock_flash(adapter);
979         qlcnic_swap32_buffer((u32 *)p_read_buf, count);
980         memcpy(buf, p_read_buf, size);
981         kfree(p_read_buf);
982
983         return size;
984 }
985
986 static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
987                                               char *buf, loff_t offset,
988                                               size_t size)
989 {
990         int  i, ret, count;
991         unsigned char *p_cache, *p_src;
992
993         p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
994         if (!p_cache)
995                 return -ENOMEM;
996
997         count = size / sizeof(u32);
998         qlcnic_swap32_buffer((u32 *)buf, count);
999         memcpy(p_cache, buf, size);
1000         p_src = p_cache;
1001
1002         if (qlcnic_83xx_lock_flash(adapter) != 0) {
1003                 kfree(p_cache);
1004                 return -EIO;
1005         }
1006
1007         if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1008                 ret = qlcnic_83xx_enable_flash_write(adapter);
1009                 if (ret) {
1010                         kfree(p_cache);
1011                         qlcnic_83xx_unlock_flash(adapter);
1012                         return -EIO;
1013                 }
1014         }
1015
1016         for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1017                 ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1018                                                    (u32 *)p_src,
1019                                                    QLC_83XX_FLASH_WRITE_MAX);
1020
1021                 if (ret) {
1022                         if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1023                                 ret = qlcnic_83xx_disable_flash_write(adapter);
1024                                 if (ret) {
1025                                         kfree(p_cache);
1026                                         qlcnic_83xx_unlock_flash(adapter);
1027                                         return -EIO;
1028                                 }
1029                         }
1030
1031                         kfree(p_cache);
1032                         qlcnic_83xx_unlock_flash(adapter);
1033                         return -EIO;
1034                 }
1035
1036                 p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1037                 offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1038         }
1039
1040         if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1041                 ret = qlcnic_83xx_disable_flash_write(adapter);
1042                 if (ret) {
1043                         kfree(p_cache);
1044                         qlcnic_83xx_unlock_flash(adapter);
1045                         return -EIO;
1046                 }
1047         }
1048
1049         kfree(p_cache);
1050         qlcnic_83xx_unlock_flash(adapter);
1051
1052         return 0;
1053 }
1054
1055 static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1056                                          char *buf, loff_t offset, size_t size)
1057 {
1058         int  i, ret, count;
1059         unsigned char *p_cache, *p_src;
1060
1061         p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1062         if (!p_cache)
1063                 return -ENOMEM;
1064
1065         qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1066         memcpy(p_cache, buf, size);
1067         p_src = p_cache;
1068         count = size / sizeof(u32);
1069
1070         if (qlcnic_83xx_lock_flash(adapter) != 0) {
1071                 kfree(p_cache);
1072                 return -EIO;
1073         }
1074
1075         if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1076                 ret = qlcnic_83xx_enable_flash_write(adapter);
1077                 if (ret) {
1078                         kfree(p_cache);
1079                         qlcnic_83xx_unlock_flash(adapter);
1080                         return -EIO;
1081                 }
1082         }
1083
1084         for (i = 0; i < count; i++) {
1085                 ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1086                 if (ret) {
1087                         if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1088                                 ret = qlcnic_83xx_disable_flash_write(adapter);
1089                                 if (ret) {
1090                                         kfree(p_cache);
1091                                         qlcnic_83xx_unlock_flash(adapter);
1092                                         return -EIO;
1093                                 }
1094                         }
1095                         kfree(p_cache);
1096                         qlcnic_83xx_unlock_flash(adapter);
1097                         return -EIO;
1098                 }
1099
1100                 p_src = p_src + sizeof(u32);
1101                 offset = offset + sizeof(u32);
1102         }
1103
1104         if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1105                 ret = qlcnic_83xx_disable_flash_write(adapter);
1106                 if (ret) {
1107                         kfree(p_cache);
1108                         qlcnic_83xx_unlock_flash(adapter);
1109                         return -EIO;
1110                 }
1111         }
1112
1113         kfree(p_cache);
1114         qlcnic_83xx_unlock_flash(adapter);
1115
1116         return 0;
1117 }
1118
1119 static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1120                                                      struct kobject *kobj,
1121                                                      struct bin_attribute *attr,
1122                                                      char *buf, loff_t offset,
1123                                                      size_t size)
1124 {
1125         int  ret;
1126         static int flash_mode;
1127         unsigned long data;
1128         struct device *dev = container_of(kobj, struct device, kobj);
1129         struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1130
1131         ret = kstrtoul(buf, 16, &data);
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 struct device_attribute dev_attr_bridged_mode = {
1179        .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1180        .show = qlcnic_show_bridged_mode,
1181        .store = qlcnic_store_bridged_mode,
1182 };
1183
1184 static struct device_attribute dev_attr_diag_mode = {
1185         .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1186         .show = qlcnic_show_diag_mode,
1187         .store = qlcnic_store_diag_mode,
1188 };
1189
1190 static struct device_attribute dev_attr_beacon = {
1191         .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1192         .show = qlcnic_show_beacon,
1193         .store = qlcnic_store_beacon,
1194 };
1195
1196 static struct bin_attribute bin_attr_crb = {
1197         .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1198         .size = 0,
1199         .read = qlcnic_sysfs_read_crb,
1200         .write = qlcnic_sysfs_write_crb,
1201 };
1202
1203 static struct bin_attribute bin_attr_mem = {
1204         .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1205         .size = 0,
1206         .read = qlcnic_sysfs_read_mem,
1207         .write = qlcnic_sysfs_write_mem,
1208 };
1209
1210 static struct bin_attribute bin_attr_npar_config = {
1211         .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1212         .size = 0,
1213         .read = qlcnic_sysfs_read_npar_config,
1214         .write = qlcnic_sysfs_write_npar_config,
1215 };
1216
1217 static struct bin_attribute bin_attr_pci_config = {
1218         .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1219         .size = 0,
1220         .read = qlcnic_sysfs_read_pci_config,
1221         .write = NULL,
1222 };
1223
1224 static struct bin_attribute bin_attr_port_stats = {
1225         .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1226         .size = 0,
1227         .read = qlcnic_sysfs_get_port_stats,
1228         .write = qlcnic_sysfs_clear_port_stats,
1229 };
1230
1231 static struct bin_attribute bin_attr_esw_stats = {
1232         .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1233         .size = 0,
1234         .read = qlcnic_sysfs_get_esw_stats,
1235         .write = qlcnic_sysfs_clear_esw_stats,
1236 };
1237
1238 static struct bin_attribute bin_attr_esw_config = {
1239         .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1240         .size = 0,
1241         .read = qlcnic_sysfs_read_esw_config,
1242         .write = qlcnic_sysfs_write_esw_config,
1243 };
1244
1245 static struct bin_attribute bin_attr_pm_config = {
1246         .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1247         .size = 0,
1248         .read = qlcnic_sysfs_read_pm_config,
1249         .write = qlcnic_sysfs_write_pm_config,
1250 };
1251
1252 static struct bin_attribute bin_attr_flash = {
1253         .attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
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, S_IRUGO,
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 }