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